keyboard.js 5.2 KB

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