|
@@ -6,25 +6,54 @@
|
|
|
'use strict';
|
|
'use strict';
|
|
|
|
|
|
|
|
import EmitterMixin from './utils/emittermixin.js';
|
|
import EmitterMixin from './utils/emittermixin.js';
|
|
|
-import KeyObserver from './engine/treeview/observer/keyobserver.js';
|
|
|
|
|
|
|
+import KeyObserver from './engine/view/observer/keyobserver.js';
|
|
|
import { getCode, parseKeystroke } from './utils/keyboard.js';
|
|
import { getCode, parseKeystroke } from './utils/keyboard.js';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Keystroke handler.
|
|
|
|
|
|
|
+ * Keystroke handler. Its instance is available in {@link ckeditor5.Editor#keystrokes} so features
|
|
|
|
|
+ * can register their keystrokes.
|
|
|
|
|
+ *
|
|
|
|
|
+ * E.g. an undo feature would do this:
|
|
|
|
|
+ *
|
|
|
|
|
+ * editor.keystrokes.set( 'ctrl + Z', 'undo' );
|
|
|
|
|
+ * editor.keystrokes.set( 'ctrl + shift + Z', 'redo' );
|
|
|
|
|
+ * editor.keystrokes.set( 'ctrl + Y', 'redo' );
|
|
|
*
|
|
*
|
|
|
* @memberOf ckeditor5
|
|
* @memberOf ckeditor5
|
|
|
*/
|
|
*/
|
|
|
export default class KeystrokeHandler {
|
|
export default class KeystrokeHandler {
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * Creates an instance of the keystroke handler.
|
|
|
|
|
+ *
|
|
|
* @param {engine.treeView.TreeView} editingView
|
|
* @param {engine.treeView.TreeView} editingView
|
|
|
*/
|
|
*/
|
|
|
constructor( editor ) {
|
|
constructor( editor ) {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * The editor instance.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @readonly
|
|
|
|
|
+ * @member {ckeditor5.Editor} ckeditor5.KeystrokeHandler#editor
|
|
|
|
|
+ */
|
|
|
this.editor = editor;
|
|
this.editor = editor;
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Observer used to listen to events for easier keystroke handler destruction.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @private
|
|
|
|
|
+ * @member {utils.Emitter} ckeditor5.KeystrokeHandler#_observer
|
|
|
|
|
+ */
|
|
|
this._observer = Object.create( EmitterMixin );
|
|
this._observer = Object.create( EmitterMixin );
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Map of the defined keystrokes. Keystroke codes are the keys.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @private
|
|
|
|
|
+ * @member {Map} ckeditor5.KeystrokeHandler#_keystrokes
|
|
|
|
|
+ */
|
|
|
this._keystrokes = new Map();
|
|
this._keystrokes = new Map();
|
|
|
|
|
|
|
|
editor.editing.view.addObserver( KeyObserver );
|
|
editor.editing.view.addObserver( KeyObserver );
|
|
|
|
|
+
|
|
|
this._observer.listenTo( editor.editing.view, 'keydown', ( evt, data ) => {
|
|
this._observer.listenTo( editor.editing.view, 'keydown', ( evt, data ) => {
|
|
|
const handled = this.press( data );
|
|
const handled = this.press( data );
|
|
|
|
|
|
|
@@ -35,22 +64,29 @@ export default class KeystrokeHandler {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * Registers a handler for the specified keystroke.
|
|
|
|
|
+ *
|
|
|
|
|
+ * The handler can be specified as a command name or a callback.
|
|
|
|
|
+ *
|
|
|
* @param {String|Array.<String|Number>} keystroke Keystroke defined in a format accepted by
|
|
* @param {String|Array.<String|Number>} keystroke Keystroke defined in a format accepted by
|
|
|
* the {@link utils.keyboard.parseKeystroke} function.
|
|
* the {@link utils.keyboard.parseKeystroke} function.
|
|
|
- * @param {String|Function} callback If a string is passed, then the keystroke will trigger a command.
|
|
|
|
|
- * If a function, then it will be called with the {@link {utils.keyboard.KeystrokeInfo keystroke info} object.
|
|
|
|
|
|
|
+ * @param {String|Function} callback If a string is passed, then the keystroke will
|
|
|
|
|
+ * {@link ckeditor5.Editor#execute execute a command}.
|
|
|
|
|
+ * If a function, then it will be called with the
|
|
|
|
|
+ * {@link engine.view.observer.keyObserver.KeyEventData key event data} object.
|
|
|
*/
|
|
*/
|
|
|
- add( keystroke, callback ) {
|
|
|
|
|
|
|
+ set( keystroke, callback ) {
|
|
|
this._keystrokes.set( parseKeystroke( keystroke ), callback );
|
|
this._keystrokes.set( parseKeystroke( keystroke ), callback );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * Triggers a keystroke handler for a specified key combination, if such a keystroke was {@link #set defined}.
|
|
|
*
|
|
*
|
|
|
- * @param {utils.keyboard.KeystrokeInfo} keystrokeInfo Keystroke info object.
|
|
|
|
|
|
|
+ * @param {engine.view.observer.keyObserver.KeyEventData} keyEventData Key event data.
|
|
|
* @returns {Boolean} Whether the keystroke was handled.
|
|
* @returns {Boolean} Whether the keystroke was handled.
|
|
|
*/
|
|
*/
|
|
|
- press( keystrokeInfo ) {
|
|
|
|
|
- const keyCode = getCode( keystrokeInfo );
|
|
|
|
|
|
|
+ press( keyEventData ) {
|
|
|
|
|
+ const keyCode = getCode( keyEventData );
|
|
|
const callback = this._keystrokes.get( keyCode );
|
|
const callback = this._keystrokes.get( keyCode );
|
|
|
|
|
|
|
|
if ( !callback ) {
|
|
if ( !callback ) {
|
|
@@ -60,12 +96,15 @@ export default class KeystrokeHandler {
|
|
|
if ( typeof callback == 'string' ) {
|
|
if ( typeof callback == 'string' ) {
|
|
|
this.editor.execute( callback );
|
|
this.editor.execute( callback );
|
|
|
} else {
|
|
} else {
|
|
|
- callback( keystrokeInfo );
|
|
|
|
|
|
|
+ callback( keyEventData );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Destroys the keystroke handler.
|
|
|
|
|
+ */
|
|
|
destroy() {
|
|
destroy() {
|
|
|
this._keystrokes = new Map();
|
|
this._keystrokes = new Map();
|
|
|
this._observer.stopListening();
|
|
this._observer.stopListening();
|