@ckeditor/ckeditor5-core
Version:
The core architecture of CKEditor 5 – the best browser-based rich text editor.
62 lines (61 loc) • 2.24 kB
JavaScript
/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/
/**
* @module core/editingkeystrokehandler
*/
import { KeystrokeHandler } from '@ckeditor/ckeditor5-utils';
/**
* A keystroke handler for editor editing. Its instance is available
* in {@link module:core/editor/editor~Editor#keystrokes} so plugins
* can register their keystrokes.
*
* E.g. an undo plugin would do this:
*
* ```ts
* editor.keystrokes.set( 'Ctrl+Z', 'undo' );
* editor.keystrokes.set( 'Ctrl+Shift+Z', 'redo' );
* editor.keystrokes.set( 'Ctrl+Y', 'redo' );
* ```
*/
export default class EditingKeystrokeHandler extends KeystrokeHandler {
/**
* The editor instance.
*/
editor;
/**
* Creates an instance of the keystroke handler.
*/
constructor(editor) {
super();
this.editor = editor;
}
/**
* Registers a handler for the specified keystroke.
*
* The handler can be specified as a command name or a callback.
*
* @param keystroke Keystroke defined in a format accepted by
* the {@link module:utils/keyboard~parseKeystroke} function.
* @param callback If a string is passed, then the keystroke will
* {@link module:core/editor/editor~Editor#execute execute a command}.
* 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 options Additional options.
* @param options.priority 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') {
const commandName = callback;
callback = (evtData, cancel) => {
this.editor.execute(commandName);
cancel();
};
}
super.set(keystroke, callback, options);
}
}