Преглед изворни кода

Replaced keystroke handler with keydown handler for arrow keys.

Kuba Niegowski пре 5 година
родитељ
комит
ade8e28eb0

+ 19 - 6
packages/ckeditor5-list/src/todolistediting.js

@@ -21,6 +21,7 @@ import {
 	modelViewChangeType,
 	modelViewInsertion
 } from './todolistconverters';
+import { getLocalizedArrowKeyCodeDirection } from '@ckeditor/ckeditor5-utils/src/keyboard';
 
 /**
  * The engine of the to-do list feature. It handles creating, editing and removing to-do lists and their items.
@@ -104,10 +105,19 @@ export default class TodoListEditing extends Plugin {
 		// <blockquote><p>Foo{}</p></blockquote>
 		// <ul><li><checkbox/>Bar</li></ul>
 		//
-		// Note: When content language direction is RTL, the behaviour is mirrored.
-		const localizedJumpOverCheckmarkKey = editor.locale.contentLanguageDirection === 'ltr' ? 'arrowleft' : 'arrowright';
+		this.listenTo( editing.view.document, 'keydown', ( eventInfo, domEventData ) => {
+			const direction = getLocalizedArrowKeyCodeDirection( domEventData.keyCode, editor.locale.contentLanguageDirection );
 
-		editor.keystrokes.set( localizedJumpOverCheckmarkKey, ( evt, stop ) => jumpOverCheckmarkOnSideArrowKeyPress( stop, model ) );
+			if ( direction != 'left' ) {
+				return;
+			}
+
+			if ( jumpOverCheckmarkOnSideArrowKeyPress( model ) ) {
+				domEventData.preventDefault();
+				domEventData.stopPropagation();
+				eventInfo.stop();
+			}
+		} );
 
 		// Toggle check state of selected to-do list items on keystroke.
 		editor.keystrokes.set( 'Ctrl+space', () => editor.execute( 'todoListCheck' ) );
@@ -176,9 +186,9 @@ export default class TodoListEditing extends Plugin {
 // moving the selection to the left/right (LTR/RTL).
 //
 // @private
-// @param {Function} stopKeyEvent
 // @param {module:engine/model/model~Model} model
-function jumpOverCheckmarkOnSideArrowKeyPress( stopKeyEvent, model ) {
+// @returns {Boolean} True if event was handled.
+function jumpOverCheckmarkOnSideArrowKeyPress( model ) {
 	const schema = model.schema;
 	const selection = model.document.selection;
 
@@ -193,8 +203,11 @@ function jumpOverCheckmarkOnSideArrowKeyPress( stopKeyEvent, model ) {
 		const newRange = schema.getNearestSelectionRange( model.createPositionBefore( parent ), 'backward' );
 
 		if ( newRange ) {
-			stopKeyEvent();
 			model.change( writer => writer.setSelection( newRange ) );
 		}
+
+		return true;
 	}
+
+	return false;
 }

+ 3 - 3
packages/ckeditor5-list/tests/todolistediting.js

@@ -1093,13 +1093,13 @@ describe( 'TodoListEditing', () => {
 				sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
 			} );
 
-			it( 'should do nothing when list item is a first block element in the root', () => {
+			it( 'should prevent default handler when list item is a first block element in the root', () => {
 				setModelData( model, '<listItem listIndent="0" listType="todo">[]bar</listItem>' );
 
 				viewDoc.fire( 'keydown', domEvtDataStub );
 
-				sinon.assert.notCalled( domEvtDataStub.preventDefault );
-				sinon.assert.notCalled( domEvtDataStub.stopPropagation );
+				sinon.assert.calledOnce( domEvtDataStub.preventDefault );
+				sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
 
 				assertEqualMarkup( getModelData( model ), '<listItem listIndent="0" listType="todo">[]bar</listItem>' );
 			} );