8
0
Quellcode durchsuchen

Disable AutoLink in code block.

Maciej Gołaszewski vor 5 Jahren
Ursprung
Commit
f4231444a3

+ 6 - 0
packages/ckeditor5-link/src/autolink.js

@@ -47,6 +47,12 @@ export default class AutoLink extends Plugin {
 	 */
 	init() {
 		const editor = this.editor;
+		const selection = editor.model.document.selection;
+
+		selection.on( 'change:range', () => {
+			// Disable plugin when selection is inside a code block.
+			this.isEnabled = !selection.anchor.parent.is( 'codeBlock' );
+		} );
 
 		const watcher = new TextWatcher( editor.model, text => {
 			// 1. Detect "space" after a text with a potential link.

+ 27 - 2
packages/ckeditor5-link/tests/autolink.js

@@ -4,6 +4,7 @@
  */
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
+import CodeBlockEditing from '@ckeditor/ckeditor5-code-block/src/codeblockediting';
 import Enter from '@ckeditor/ckeditor5-enter/src/enter';
 import Input from '@ckeditor/ckeditor5-typing/src/input';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
@@ -26,7 +27,7 @@ describe( 'AutoLink', () => {
 
 		beforeEach( async () => {
 			editor = await ModelTestEditor.create( {
-				plugins: [ Paragraph, Input, LinkEditing, AutoLink, Enter, ShiftEnter ]
+				plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink ]
 			} );
 
 			model = editor.model;
@@ -150,7 +151,7 @@ describe( 'AutoLink', () => {
 
 		beforeEach( async () => {
 			editor = await ModelTestEditor.create( {
-				plugins: [ Paragraph, Input, LinkEditing, AutoLink, UndoEditing, Enter, ShiftEnter ]
+				plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink, UndoEditing ]
 			} );
 
 			model = editor.model;
@@ -190,6 +191,30 @@ describe( 'AutoLink', () => {
 		} );
 	} );
 
+	describe( 'Code blocks integration', () => {
+		let model;
+
+		beforeEach( async () => {
+			editor = await ModelTestEditor.create( {
+				plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink, CodeBlockEditing ]
+			} );
+
+			model = editor.model;
+		} );
+
+		it( 'should be disabled inside code blocks', () => {
+			setData( model, '<codeBlock language="plaintext">some [] code</codeBlock>' );
+
+			const plugin = editor.plugins.get( 'AutoLink' );
+
+			simulateTyping( 'www.cksource.com' );
+
+			expect( plugin.isEnabled ).to.be.false;
+			expect( getData( model, { withoutSelection: true } ) )
+				.to.equal( '<codeBlock language="plaintext">some www.cksource.com code</codeBlock>' );
+		} );
+	} );
+
 	function simulateTyping( text ) {
 		const letters = text.split( '' );