keystrokehandler.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. /**
  6. * Controls keystrokes typing in an editor instance.
  7. *
  8. * @class
  9. * @constructor Creates a keystrokeHandler class instance.
  10. * @param {CKEDITOR.editor} editor The editor instance.
  11. */
  12. CKEDITOR.keystrokeHandler = function( editor ) {
  13. if ( editor.keystrokeHandler )
  14. return editor.keystrokeHandler;
  15. /**
  16. * A list of keystrokes associated with commands. Each entry points to the
  17. * command to be executed.
  18. *
  19. * Since CKEditor 4 there is no need to modify this property directly during the runtime.
  20. * Use {@link CKEDITOR.editor#setKeystroke} instead.
  21. */
  22. this.keystrokes = {};
  23. /**
  24. * A list of keystrokes that should be blocked if not defined in
  25. * {@link #keystrokes}. In this way it is possible to block the default
  26. * browser behavior for those keystrokes.
  27. */
  28. this.blockedKeystrokes = {};
  29. this._ = {
  30. editor: editor
  31. };
  32. return this;
  33. };
  34. ( function() {
  35. var cancel;
  36. var onKeyDown = function( event ) {
  37. // The DOM event object is passed by the "data" property.
  38. event = event.data;
  39. var keyCombination = event.getKeystroke();
  40. var command = this.keystrokes[ keyCombination ];
  41. var editor = this._.editor;
  42. cancel = ( editor.fire( 'key', { keyCode: keyCombination, domEvent: event } ) === false );
  43. if ( !cancel ) {
  44. if ( command ) {
  45. var data = { from: 'keystrokeHandler' };
  46. cancel = ( editor.execCommand( command, data ) !== false );
  47. }
  48. if ( !cancel )
  49. cancel = !!this.blockedKeystrokes[ keyCombination ];
  50. }
  51. if ( cancel )
  52. event.preventDefault( true );
  53. return !cancel;
  54. };
  55. var onKeyPress = function( event ) {
  56. if ( cancel ) {
  57. cancel = false;
  58. event.data.preventDefault( true );
  59. }
  60. };
  61. CKEDITOR.keystrokeHandler.prototype = {
  62. /**
  63. * Attaches this keystroke handle to a DOM object. Keystrokes typed
  64. * over this object will be handled by this keystrokeHandler.
  65. *
  66. * @param {CKEDITOR.dom.domObject} domObject The DOM object to attach to.
  67. */
  68. attach: function( domObject ) {
  69. // For most browsers, it is enough to listen to the keydown event
  70. // only.
  71. domObject.on( 'keydown', onKeyDown, this );
  72. // Some browsers instead, don't cancel key events in the keydown, but in the
  73. // keypress. So we must do a longer trip in those cases.
  74. if ( CKEDITOR.env.gecko && CKEDITOR.env.mac )
  75. domObject.on( 'keypress', onKeyPress, this );
  76. }
  77. };
  78. } )();
  79. /**
  80. * A list associating keystrokes with editor commands. Each element in the list
  81. * is an array where the first item is the keystroke, and the second is the
  82. * name of the command to be executed.
  83. *
  84. * This setting should be used to define (as well as to overwrite or remove) keystrokes
  85. * set by plugins (like `link` and `basicstyles`). If you want to set a keystroke
  86. * for your plugin or during the runtime, use {@link CKEDITOR.editor#setKeystroke} instead.
  87. *
  88. * Since default keystrokes are set by the {@link CKEDITOR.editor#setKeystroke}
  89. * method, by default `config.keystrokes` is an empty array.
  90. *
  91. * See {@link CKEDITOR.editor#setKeystroke} documentation for more details
  92. * regarding the start up order.
  93. *
  94. * // Change default Ctrl+L keystroke for 'link' command to Ctrl+Shift+L.
  95. * config.keystrokes = [
  96. * ...
  97. * [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 76, 'link' ], // Ctrl+Shift+L
  98. * ...
  99. * ];
  100. *
  101. * To reset a particular keystroke, the following approach can be used:
  102. *
  103. * // Disable default Ctrl+L keystroke which executes the 'link' command by default.
  104. * config.keystrokes = [
  105. * ...
  106. * [ CKEDITOR.CTRL + 76, null ], // Ctrl+L
  107. * ...
  108. * ];
  109. *
  110. * In order to reset all default keystrokes, a {@link CKEDITOR#instanceReady} callback should be
  111. * used. This is since editor defaults are merged rather than overwritten by
  112. * user keystrokes.
  113. *
  114. * **Note**: This can be potentially harmful for the editor. Avoid this unless you are
  115. * aware of the consequences.
  116. *
  117. * // Reset all default keystrokes.
  118. * config.on.instanceReady = function() {
  119. * this.keystrokeHandler.keystrokes = [];
  120. * };
  121. *
  122. * @cfg {Array} [keystrokes=[]]
  123. * @member CKEDITOR.config
  124. */
  125. /**
  126. * Fired when any keyboard key (or a combination thereof) is pressed in the editing area.
  127. *
  128. * editor.on( 'key', function( evt ) {
  129. * if ( evt.data.keyCode == CKEDITOR.CTRL + 90 ) {
  130. * // Do something...
  131. *
  132. * // Cancel the event, so other listeners will not be executed and
  133. * // the keydown's default behavior will be prevented.
  134. * evt.cancel();
  135. * }
  136. * } );
  137. *
  138. * Usually you will want to use the {@link CKEDITOR.editor#setKeystroke} method or
  139. * the {@link CKEDITOR.config#keystrokes} option to attach a keystroke to some {@link CKEDITOR.command command}.
  140. * Key event listeners are usuful when some action should be executed conditionally, based
  141. * for example on precise selection location.
  142. *
  143. * @event key
  144. * @member CKEDITOR.editor
  145. * @param data
  146. * @param {Number} data.keyCode A number representing the key code (or a combination thereof).
  147. * It is the sum of the current key code and the {@link CKEDITOR#CTRL}, {@link CKEDITOR#SHIFT}
  148. * and {@link CKEDITOR#ALT} constants, if those are pressed.
  149. * @param {CKEDITOR.dom.event} data.domEvent A `keydown` DOM event instance. Available since CKEditor 4.4.1.
  150. * @param {CKEDITOR.editor} editor This editor instance.
  151. */