Procházet zdrojové kódy

Merge branch 'master' into t/ckeditor5-link/52

Aleksander Nowodzinski před 8 roky
rodič
revize
c7fdc00c7e

+ 6 - 2
packages/ckeditor5-core/src/editingkeystrokehandler.js

@@ -52,8 +52,12 @@ export default class EditingKeystrokeHandler extends KeystrokeHandler {
 	 * If a function, then it will be called with the
 	 * {@link module:engine/view/observer/keyobserver~KeyEventData key event data} object and
 	 * a `cancel()` helper to both `preventDefault()` and `stopPropagation()` of the event.
+	 * @param {Object} [options={}] Additional options.
+	 * @param {module:utils/priorities~PriorityString|Number} [options.priority='normal'] The priority of the keystroke
+	 * callback. The higher the priority value the sooner the callback will be executed. Keystrokes having the same priority
+	 * are called in the order they were added.
 	 */
-	set( keystroke, callback ) {
+	set( keystroke, callback, options = {} ) {
 		if ( typeof callback == 'string' ) {
 			const commandName = callback;
 
@@ -63,6 +67,6 @@ export default class EditingKeystrokeHandler extends KeystrokeHandler {
 			};
 		}
 
-		super.set( keystroke, callback );
+		super.set( keystroke, callback, options );
 	}
 }

+ 34 - 0
packages/ckeditor5-core/tests/editingkeystrokehandler.js

@@ -41,6 +41,25 @@ describe( 'EditingKeystrokeHandler', () => {
 				sinon.assert.notCalled( keyEvtData.preventDefault );
 				sinon.assert.notCalled( keyEvtData.stopPropagation );
 			} );
+
+			it( 'provides a callback which stops the event and remaining callbacks in the keystroke handler', () => {
+				const spy1 = sinon.spy();
+				const spy2 = sinon.spy();
+				const spy3 = sinon.spy();
+
+				keystrokes.set( [ 'ctrl', 'A' ], spy1 );
+				keystrokes.set( [ 'ctrl', 'A' ], spy2, { priority: 'high' } );
+				keystrokes.set( [ 'ctrl', 'A' ], 'foo', { priority: 'low' } );
+				keystrokes.set( [ 'ctrl', 'A' ], ( keyEvtData, cancel ) => {
+					spy3();
+					cancel();
+				} );
+
+				keystrokes.press( getCtrlA() );
+
+				sinon.assert.callOrder( spy2, spy1, spy3 );
+				sinon.assert.notCalled( executeSpy );
+			} );
 		} );
 
 		describe( 'with a callback', () => {
@@ -56,6 +75,21 @@ describe( 'EditingKeystrokeHandler', () => {
 				sinon.assert.notCalled( keyEvtData.stopPropagation );
 			} );
 		} );
+
+		it( 'supports priorities', () => {
+			const spy1 = sinon.spy();
+			const spy2 = sinon.spy();
+			const spy3 = sinon.spy();
+
+			keystrokes.set( [ 'ctrl', 'A' ], spy1 );
+			keystrokes.set( [ 'ctrl', 'A' ], spy2, { priority: 'high' } );
+			keystrokes.set( [ 'ctrl', 'A' ], 'foo', { priority: 'low' } );
+			keystrokes.set( [ 'ctrl', 'A' ], spy3 );
+
+			keystrokes.press( getCtrlA() );
+
+			sinon.assert.callOrder( spy2, spy1, spy3, executeSpy );
+		} );
 	} );
 
 	describe( 'press()', () => {