Browse Source

Merge pull request #172 from ckeditor/t/72

Feature: Added two-step caret movement for links. See #72.
Piotrek Koszuliński 7 years ago
parent
commit
8ac9d60885
2 changed files with 25 additions and 1 deletions
  1. 4 0
      packages/ckeditor5-link/src/link.js
  2. 21 1
      packages/ckeditor5-link/tests/link.js

+ 4 - 0
packages/ckeditor5-link/src/link.js

@@ -15,6 +15,7 @@ import { isLinkElement } from './utils';
 import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
 
 import clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsidehandler';
+import bindTwoStepCaretToAttribute from '@ckeditor/ckeditor5-engine/src/utils/bindtwostepcarettoattribute';
 
 import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
 import LinkFormView from './ui/linkformview';
@@ -82,6 +83,9 @@ export default class Link extends Plugin {
 
 		// Attach lifecycle actions to the the balloon.
 		this._enableUserBalloonInteractions();
+
+		// Enable two-step caret movement for `linkHref` attribute.
+		bindTwoStepCaretToAttribute( editor.editing.view, editor.model, this, 'linkHref' );
 	}
 
 	/**

+ 21 - 1
packages/ckeditor5-link/tests/link.js

@@ -69,7 +69,7 @@ describe( 'Link', () => {
 		expect( editor.plugins.get( ContextualBalloon ) ).to.be.instanceOf( ContextualBalloon );
 	} );
 
-	describe( 'init', () => {
+	describe( 'init()', () => {
 		it( 'should register click observer', () => {
 			expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
 		} );
@@ -82,6 +82,26 @@ describe( 'Link', () => {
 			expect( formView ).to.be.instanceOf( LinkFormView );
 		} );
 
+		it( 'should bind two-step caret movement to `linkHref` attribute', () => {
+			// Let's check only the minimum to not duplicated `bindTwoStepCaretToAttribute()` tests.
+			// Testing minimum is better then testing using spies that might give false positive results.
+
+			// Put selection before the link element.
+			setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
+
+			// The selection's gravity is not overridden because selection land here not as a result of `keydown`.
+			expect( editor.model.document.selection.isGravityOverridden ).to.false;
+
+			// So let's simulate `keydown` event.
+			editor.editing.view.document.fire( 'keydown', {
+				keyCode: keyCodes.arrowright,
+				preventDefault: () => {},
+				domTarget: document.body
+			} );
+
+			expect( editor.model.document.selection.isGravityOverridden ).to.true;
+		} );
+
 		describe( 'link toolbar button', () => {
 			it( 'should be registered', () => {
 				expect( linkButton ).to.be.instanceOf( ButtonView );