keyboard.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * Set of utils related to keyboard support.
  7. *
  8. * @module utils/keyboard
  9. */
  10. import CKEditorError from './ckeditorerror';
  11. import env from './env';
  12. /**
  13. * Object with `keyName => keyCode` pairs for a set of known keys.
  14. *
  15. * Contains:
  16. *
  17. * * `a-z`,
  18. * * `0-9`,
  19. * * `f1-f12`,
  20. * * `arrow(left|up|right|bottom)`,
  21. * * `backspace`, `delete`, `enter`, `esc`, `tab`,
  22. * * `ctrl`, `cmd`, `shift`, `alt`.
  23. */
  24. export const keyCodes = generateKnownKeyCodes();
  25. /**
  26. * Converts a key name or a {@link module:utils/keyboard~KeystrokeInfo keystroke info} into a key code.
  27. *
  28. * Note: Key names are matched with {@link module:utils/keyboard~keyCodes} in a case-insensitive way.
  29. *
  30. * @param {String|module:utils/keyboard~KeystrokeInfo} Key name (see {@link module:utils/keyboard~keyCodes})
  31. * or a keystroke data object.
  32. * @returns {Number} Key or keystroke code.
  33. */
  34. export function getCode( key ) {
  35. let keyCode;
  36. if ( typeof key == 'string' ) {
  37. keyCode = keyCodes[ key.toLowerCase() ];
  38. if ( !keyCode ) {
  39. /**
  40. * Unknown key name. Only key names contained by the {@link module:utils/keyboard~keyCodes} can be used.
  41. *
  42. * @errror keyboard-unknown-key
  43. * @param {String} key
  44. */
  45. throw new CKEditorError( 'keyboard-unknown-key: Unknown key name.', { key } );
  46. }
  47. } else {
  48. keyCode = key.keyCode +
  49. ( key.altKey ? keyCodes.alt : 0 ) +
  50. ( key.ctrlKey ? keyCodes.ctrl : 0 ) +
  51. ( key.shiftKey ? keyCodes.shift : 0 );
  52. }
  53. return keyCode;
  54. }
  55. /**
  56. * Parses keystroke and returns a keystroke code that will match the code returned by
  57. * link {@link module:utils/keyboard.getCode} for a corresponding {@link module:utils/keyboard~KeystrokeInfo keystroke info}.
  58. *
  59. * The keystroke can be passed in two formats:
  60. *
  61. * * as a single string – e.g. `ctrl + A`,
  62. * * as an array of {@link module:utils/keyboard~keyCodes known key names} and key codes – e.g.:
  63. * * `[ 'ctrl', 32 ]` (ctrl + space),
  64. * * `[ 'ctrl', 'a' ]` (ctrl + A).
  65. *
  66. * Note: Key names are matched with {@link module:utils/keyboard~keyCodes} in a case-insensitive way.
  67. *
  68. * Note: Only keystrokes with a single non-modifier key are supported (e.g. `ctrl+A` is OK, but `ctrl+A+B` is not).
  69. *
  70. * @param {String|Array.<Number|String>} keystroke Keystroke definition.
  71. * @returns {Number} Keystroke code.
  72. */
  73. export function parseKeystroke( keystroke ) {
  74. if ( typeof keystroke == 'string' ) {
  75. keystroke = splitKeystrokeText( keystroke );
  76. }
  77. return keystroke
  78. .map( key => ( typeof key == 'string' ) ? getCode( key ) : key )
  79. .reduce( ( key, sum ) => sum + key, 0 );
  80. }
  81. /**
  82. * It translates any keystroke string text like `"CTRL+A"` to an
  83. * environment–specific keystroke, i.e. `"⌘A"` on Mac OSX.
  84. *
  85. * @param {String} keystroke Keystroke text.
  86. * @returns {String} Keystroke text specific for the environment.
  87. */
  88. export function getEnvKeystrokeText( keystroke ) {
  89. const split = splitKeystrokeText( keystroke );
  90. if ( env.mac ) {
  91. if ( split[ 0 ].toLowerCase() == 'ctrl' ) {
  92. return '⌘' + ( split[ 1 ] || '' );
  93. }
  94. }
  95. return keystroke;
  96. }
  97. function generateKnownKeyCodes() {
  98. const keyCodes = {
  99. arrowleft: 37,
  100. arrowup: 38,
  101. arrowright: 39,
  102. arrowdown: 40,
  103. backspace: 8,
  104. delete: 46,
  105. enter: 13,
  106. space: 32,
  107. esc: 27,
  108. tab: 9,
  109. // The idea about these numbers is that they do not collide with any real key codes, so we can use them
  110. // like bit masks.
  111. ctrl: 0x110000,
  112. // Has the same code as ctrl, because their behaviour should be unified across the editor.
  113. // See http://ckeditor.github.io/editor-recommendations/general-policies#ctrl-vs-cmd
  114. cmd: 0x110000,
  115. shift: 0x220000,
  116. alt: 0x440000
  117. };
  118. // a-z
  119. for ( let code = 65; code <= 90; code++ ) {
  120. const letter = String.fromCharCode( code );
  121. keyCodes[ letter.toLowerCase() ] = code;
  122. }
  123. // 0-9
  124. for ( let code = 48; code <= 57; code++ ) {
  125. keyCodes[ code - 48 ] = code;
  126. }
  127. // F1-F12
  128. for ( let code = 112; code <= 123; code++ ) {
  129. keyCodes[ 'f' + ( code - 111 ) ] = code;
  130. }
  131. return keyCodes;
  132. }
  133. function splitKeystrokeText( keystroke ) {
  134. return keystroke.split( /\s*\+\s*/ );
  135. }
  136. /**
  137. * Information about a keystroke.
  138. *
  139. * @interface module:utils/keyboard~KeystrokeInfo
  140. */
  141. /**
  142. * The [key code](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode).
  143. *
  144. * @member {Number} module:utils/keyboard~KeystrokeInfo#keyCode
  145. */
  146. /**
  147. * Whether the <kbd>Alt</kbd> modifier was pressed.
  148. *
  149. * @member {Bolean} module:utils/keyboard~KeystrokeInfo#altKey
  150. */
  151. /**
  152. * Whether the <kbd>Ctrl</kbd> or <kbd>Cmd</kbd> modifier was pressed.
  153. *
  154. * @member {Bolean} module:utils/keyboard~KeystrokeInfo#ctrlKey
  155. */
  156. /**
  157. * Whether the <kbd>Shift</kbd> modifier was pressed.
  158. *
  159. * @member {Bolean} module:utils/keyboard~KeystrokeInfo#shiftKey
  160. */