editingkeystrokehandler.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module core/editingkeystrokehandler
  7. */
  8. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  9. /**
  10. * A keystroke handler for editor editing. Its instance is available
  11. * in {@link module:core/editor/editor~Editor#keystrokes} so plugins
  12. * can register their keystrokes.
  13. *
  14. * E.g. an undo plugin would do this:
  15. *
  16. * editor.keystrokes.set( 'Ctrl+Z', 'undo' );
  17. * editor.keystrokes.set( 'Ctrl+Shift+Z', 'redo' );
  18. * editor.keystrokes.set( 'Ctrl+Y', 'redo' );
  19. *
  20. * @extends module:utils/keystrokehandler~KeystrokeHandler
  21. */
  22. export default class EditingKeystrokeHandler extends KeystrokeHandler {
  23. /**
  24. * Creates an instance of the keystroke handler.
  25. *
  26. * @param {module:core/editor/editor~Editor} editor
  27. */
  28. constructor( editor ) {
  29. super();
  30. /**
  31. * The editor instance.
  32. *
  33. * @readonly
  34. * @member {module:core/editor/editor~Editor}
  35. */
  36. this.editor = editor;
  37. }
  38. /**
  39. * Registers a handler for the specified keystroke.
  40. *
  41. * The handler can be specified as a command name or a callback.
  42. *
  43. * @param {String|Array.<String|Number>} keystroke Keystroke defined in a format accepted by
  44. * the {@link module:utils/keyboard~parseKeystroke} function.
  45. * @param {Function|String} callback If a string is passed, then the keystroke will
  46. * {@link module:core/editor/editor~Editor#execute execute a command}.
  47. * If a function, then it will be called with the
  48. * {@link module:engine/view/observer/keyobserver~KeyEventData key event data} object and
  49. * a `cancel()` helper to both `preventDefault()` and `stopPropagation()` of the event.
  50. * @param {Object} [options={}] Additional options.
  51. * @param {module:utils/priorities~PriorityString|Number} [options.priority='normal'] The priority of the keystroke
  52. * callback. The higher the priority value the sooner the callback will be executed. Keystrokes having the same priority
  53. * are called in the order they were added.
  54. */
  55. set( keystroke, callback, options = {} ) {
  56. if ( typeof callback == 'string' ) {
  57. const commandName = callback;
  58. callback = ( evtData, cancel ) => {
  59. this.editor.execute( commandName );
  60. cancel();
  61. };
  62. }
  63. super.set( keystroke, callback, options );
  64. }
  65. }