Przeglądaj źródła

AutoLink should work inside paragraphs.

Maciej Gołaszewski 5 lat temu
rodzic
commit
e72744f525

+ 9 - 3
packages/ckeditor5-link/src/autolink.js

@@ -32,6 +32,9 @@ export default class AutoLink extends Plugin {
 		const editor = this.editor;
 
 		const watcher = new TextWatcher( editor.model, text => {
+			// TODO - should be 2-step:
+			// 1. Detect "space" or "enter".
+			// 2. Check text before "space" or "enter".
 			const match = regexp.exec( text );
 
 			if ( match ) {
@@ -52,10 +55,13 @@ export default class AutoLink extends Plugin {
 
 			// Enqueue change to make undo step.
 			editor.model.enqueueChange( writer => {
-				writer.setAttribute( 'linkHref', url, writer.createRange(
-					range.start,
+				const linkRange = writer.createRange(
+					range.end.getShiftedBy( -( 1 + url.length ) ),
 					range.end.getShiftedBy( -1 )
-				) );
+				);
+
+				// TODO: use command for decorators support.
+				writer.setAttribute( 'linkHref', url, linkRange );
 			} );
 		} );
 	}

+ 17 - 5
packages/ckeditor5-link/tests/autolink.js

@@ -18,18 +18,20 @@ describe( 'AutoLink', () => {
 	} );
 
 	describe( 'auto link behavior', () => {
-		let editor;
+		let editor, model;
 
 		beforeEach( async () => {
 			editor = await ModelTestEditor.create( { plugins: [ Paragraph, Input, LinkEditing, AutoLink, UndoEditing ] } );
 
-			setData( editor.model, '<paragraph>[]</paragraph>' );
+			model = editor.model;
+
+			setData( model, '<paragraph>[]</paragraph>' );
 		} );
 
 		it( 'does not add linkHref attribute to a text link while typing', () => {
 			simulateTyping( 'https://www.cksource.com' );
 
-			expect( getData( editor.model ) ).to.equal(
+			expect( getData( model ) ).to.equal(
 				'<paragraph>https://www.cksource.com[]</paragraph>'
 			);
 		} );
@@ -37,17 +39,27 @@ describe( 'AutoLink', () => {
 		it( 'adds linkHref attribute to a text link after space', () => {
 			simulateTyping( 'https://www.cksource.com ' );
 
-			expect( getData( editor.model ) ).to.equal(
+			expect( getData( model ) ).to.equal(
 				'<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> []</paragraph>'
 			);
 		} );
 
+		it( 'adds linkHref attribute to a text link after space (inside paragraph)', () => {
+			setData( model, '<paragraph>Foo Bar [] Baz</paragraph>' );
+
+			simulateTyping( 'https://www.cksource.com ' );
+
+			expect( getData( model ) ).to.equal(
+				'<paragraph>Foo Bar <$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> [] Baz</paragraph>'
+			);
+		} );
+
 		it( 'can undo auto-linking', () => {
 			simulateTyping( 'https://www.cksource.com ' );
 
 			editor.commands.execute( 'undo' );
 
-			expect( getData( editor.model ) ).to.equal(
+			expect( getData( model ) ).to.equal(
 				'<paragraph>https://www.cksource.com []</paragraph>'
 			);
 		} );