浏览代码

Tests: Added tests to check priorities and cancellation in the EditingKeystrokeHandler.

Aleksander Nowodzinski 8 年之前
父节点
当前提交
e6918b0564

+ 4 - 0
packages/ckeditor5-core/src/editingkeystrokehandler.js

@@ -52,6 +52,10 @@ 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, options = {} ) {
 		if ( typeof callback == 'string' ) {

+ 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()', () => {