keyboard.js 5.3 KB

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