index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ModifierKey = void 0;
  4. // eslint-disable-next-line
  5. var ModifierKey;
  6. (function (ModifierKey) {
  7. function parse(modifiers) {
  8. const or = [];
  9. const and = [];
  10. if (Array.isArray(modifiers)) {
  11. or.push(...modifiers);
  12. }
  13. else {
  14. modifiers.split('|').forEach((item) => {
  15. if (item.indexOf('&') === -1) {
  16. or.push(item);
  17. }
  18. else {
  19. and.push(...item.split('&'));
  20. }
  21. });
  22. }
  23. return { or, and };
  24. }
  25. ModifierKey.parse = parse;
  26. function equals(modifiers1, modifiers2) {
  27. if (modifiers1 != null && modifiers2 != null) {
  28. const m1 = parse(modifiers1);
  29. const m2 = parse(modifiers2);
  30. const or1 = m1.or.sort();
  31. const or2 = m2.or.sort();
  32. const and1 = m1.and.sort();
  33. const and2 = m2.and.sort();
  34. const equal = (a1, a2) => {
  35. return (a1.length === a2.length &&
  36. (a1.length === 0 || a1.every((a, i) => a === a2[i])));
  37. };
  38. return equal(or1, or2) && equal(and1, and2);
  39. }
  40. if (modifiers1 == null && modifiers2 == null) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. ModifierKey.equals = equals;
  46. function isMatch(e, modifiers, strict) {
  47. if (modifiers == null ||
  48. (Array.isArray(modifiers) && modifiers.length === 0)) {
  49. return strict
  50. ? e.altKey !== true &&
  51. e.ctrlKey !== true &&
  52. e.metaKey !== true &&
  53. e.shiftKey !== true
  54. : true;
  55. }
  56. const { or, and } = parse(modifiers);
  57. const match = (key) => {
  58. const name = `${key.toLowerCase()}Key`;
  59. return e[name] === true;
  60. };
  61. return or.some((key) => match(key)) && and.every((key) => match(key));
  62. }
  63. ModifierKey.isMatch = isMatch;
  64. })(ModifierKey = exports.ModifierKey || (exports.ModifierKey = {}));
  65. //# sourceMappingURL=index.js.map