index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. /**
  6. * Constants.
  7. */
  8. var IS_MAC = typeof window != 'undefined' && /Mac|iPod|iPhone|iPad/.test(window.navigator.platform);
  9. var MODIFIERS = {
  10. alt: 'altKey',
  11. control: 'ctrlKey',
  12. meta: 'metaKey',
  13. shift: 'shiftKey'
  14. };
  15. var ALIASES = {
  16. add: '+',
  17. break: 'pause',
  18. cmd: 'meta',
  19. command: 'meta',
  20. ctl: 'control',
  21. ctrl: 'control',
  22. del: 'delete',
  23. down: 'arrowdown',
  24. esc: 'escape',
  25. ins: 'insert',
  26. left: 'arrowleft',
  27. mod: IS_MAC ? 'meta' : 'control',
  28. opt: 'alt',
  29. option: 'alt',
  30. return: 'enter',
  31. right: 'arrowright',
  32. space: ' ',
  33. spacebar: ' ',
  34. up: 'arrowup',
  35. win: 'meta',
  36. windows: 'meta'
  37. };
  38. var CODES = {
  39. backspace: 8,
  40. tab: 9,
  41. enter: 13,
  42. shift: 16,
  43. control: 17,
  44. alt: 18,
  45. pause: 19,
  46. capslock: 20,
  47. escape: 27,
  48. ' ': 32,
  49. pageup: 33,
  50. pagedown: 34,
  51. end: 35,
  52. home: 36,
  53. arrowleft: 37,
  54. arrowup: 38,
  55. arrowright: 39,
  56. arrowdown: 40,
  57. insert: 45,
  58. delete: 46,
  59. meta: 91,
  60. numlock: 144,
  61. scrolllock: 145,
  62. ';': 186,
  63. '=': 187,
  64. ',': 188,
  65. '-': 189,
  66. '.': 190,
  67. '/': 191,
  68. '`': 192,
  69. '[': 219,
  70. '\\': 220,
  71. ']': 221,
  72. '\'': 222
  73. };
  74. for (var f = 1; f < 20; f++) {
  75. CODES['f' + f] = 111 + f;
  76. }
  77. /**
  78. * Is hotkey?
  79. */
  80. function isHotkey(hotkey, options, event) {
  81. if (options && !('byKey' in options)) {
  82. event = options;
  83. options = null;
  84. }
  85. if (!Array.isArray(hotkey)) {
  86. hotkey = [hotkey];
  87. }
  88. var array = hotkey.map(function (string) {
  89. return parseHotkey(string, options);
  90. });
  91. var check = function check(e) {
  92. return array.some(function (object) {
  93. return compareHotkey(object, e);
  94. });
  95. };
  96. var ret = event == null ? check : check(event);
  97. return ret;
  98. }
  99. function isCodeHotkey(hotkey, event) {
  100. return isHotkey(hotkey, event);
  101. }
  102. function isKeyHotkey(hotkey, event) {
  103. return isHotkey(hotkey, { byKey: true }, event);
  104. }
  105. /**
  106. * Parse.
  107. */
  108. function parseHotkey(hotkey, options) {
  109. var byKey = options && options.byKey;
  110. var ret = {};
  111. // Special case to handle the `+` key since we use it as a separator.
  112. hotkey = hotkey.replace('++', '+add');
  113. var values = hotkey.split('+');
  114. var length = values.length;
  115. // Ensure that all the modifiers are set to false unless the hotkey has them.
  116. for (var k in MODIFIERS) {
  117. ret[MODIFIERS[k]] = false;
  118. }
  119. var _iteratorNormalCompletion = true;
  120. var _didIteratorError = false;
  121. var _iteratorError = undefined;
  122. try {
  123. for (var _iterator = values[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  124. var value = _step.value;
  125. var optional = value.endsWith('?') && value.length > 1;
  126. if (optional) {
  127. value = value.slice(0, -1);
  128. }
  129. var name = toKeyName(value);
  130. var modifier = MODIFIERS[name];
  131. if (value.length > 1 && !modifier && !ALIASES[value] && !CODES[name]) {
  132. throw new TypeError('Unknown modifier: "' + value + '"');
  133. }
  134. if (length === 1 || !modifier) {
  135. if (byKey) {
  136. ret.key = name;
  137. } else {
  138. ret.which = toKeyCode(value);
  139. }
  140. }
  141. if (modifier) {
  142. ret[modifier] = optional ? null : true;
  143. }
  144. }
  145. } catch (err) {
  146. _didIteratorError = true;
  147. _iteratorError = err;
  148. } finally {
  149. try {
  150. if (!_iteratorNormalCompletion && _iterator.return) {
  151. _iterator.return();
  152. }
  153. } finally {
  154. if (_didIteratorError) {
  155. throw _iteratorError;
  156. }
  157. }
  158. }
  159. return ret;
  160. }
  161. /**
  162. * Compare.
  163. */
  164. function compareHotkey(object, event) {
  165. for (var key in object) {
  166. var expected = object[key];
  167. var actual = void 0;
  168. if (expected == null) {
  169. continue;
  170. }
  171. if (key === 'key' && event.key != null) {
  172. actual = event.key.toLowerCase();
  173. } else if (key === 'which') {
  174. actual = expected === 91 && event.which === 93 ? 91 : event.which;
  175. } else {
  176. actual = event[key];
  177. }
  178. if (actual == null && expected === false) {
  179. continue;
  180. }
  181. if (actual !== expected) {
  182. return false;
  183. }
  184. }
  185. return true;
  186. }
  187. /**
  188. * Utils.
  189. */
  190. function toKeyCode(name) {
  191. name = toKeyName(name);
  192. var code = CODES[name] || name.toUpperCase().charCodeAt(0);
  193. return code;
  194. }
  195. function toKeyName(name) {
  196. name = name.toLowerCase();
  197. name = ALIASES[name] || name;
  198. return name;
  199. }
  200. /**
  201. * Export.
  202. */
  203. exports.default = isHotkey;
  204. exports.isHotkey = isHotkey;
  205. exports.isCodeHotkey = isCodeHotkey;
  206. exports.isKeyHotkey = isKeyHotkey;
  207. exports.parseHotkey = parseHotkey;
  208. exports.compareHotkey = compareHotkey;
  209. exports.toKeyCode = toKeyCode;
  210. exports.toKeyName = toKeyName;