Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #102 from ckeditor/t/ckeditor5/447
Browse files Browse the repository at this point in the history
Feature: `EditingKeystrokeHandler` should support priorities and proper cancelling. Closes #101.
  • Loading branch information
Reinmar authored Aug 21, 2017
2 parents 3af7d07 + d9099d0 commit c74b9a3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/editingkeystrokehandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -63,6 +67,6 @@ export default class EditingKeystrokeHandler extends KeystrokeHandler {
};
}

super.set( keystroke, callback );
super.set( keystroke, callback, options );
}
}
34 changes: 34 additions & 0 deletions tests/editingkeystrokehandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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()', () => {
Expand Down

0 comments on commit c74b9a3

Please sign in to comment.