1 | import "core-js/modules/es.regexp.exec.js";
|
2 | import "core-js/modules/es.string.match.js";
|
3 | import "core-js/modules/es.array.includes.js";
|
4 | import "core-js/modules/es.array.join.js";
|
5 | import "core-js/modules/es.array.find.js";
|
6 | import "core-js/modules/es.object.to-string.js";
|
7 | import "core-js/modules/es.array.map.js";
|
8 | import global from 'global';
|
9 |
|
10 | var navigator = global.navigator;
|
11 | export var isMacLike = function isMacLike() {
|
12 | return navigator && navigator.platform ? !!navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i) : false;
|
13 | };
|
14 | export var controlOrMetaSymbol = function controlOrMetaSymbol() {
|
15 | return isMacLike() ? '⌘' : 'ctrl';
|
16 | };
|
17 | export var controlOrMetaKey = function controlOrMetaKey() {
|
18 | return isMacLike() ? 'meta' : 'control';
|
19 | };
|
20 | export var optionOrAltSymbol = function optionOrAltSymbol() {
|
21 | return isMacLike() ? '⌥' : 'alt';
|
22 | };
|
23 | export var isShortcutTaken = function isShortcutTaken(arr1, arr2) {
|
24 | return JSON.stringify(arr1) === JSON.stringify(arr2);
|
25 | };
|
26 |
|
27 |
|
28 | export var eventToShortcut = function eventToShortcut(e) {
|
29 |
|
30 | if (['Meta', 'Alt', 'Control', 'Shift'].includes(e.key)) {
|
31 | return null;
|
32 | }
|
33 |
|
34 | var keys = [];
|
35 |
|
36 | if (e.altKey) {
|
37 | keys.push('alt');
|
38 | }
|
39 |
|
40 | if (e.ctrlKey) {
|
41 | keys.push('control');
|
42 | }
|
43 |
|
44 | if (e.metaKey) {
|
45 | keys.push('meta');
|
46 | }
|
47 |
|
48 | if (e.shiftKey) {
|
49 | keys.push('shift');
|
50 | }
|
51 |
|
52 | if (e.key && e.key.length === 1 && e.key !== ' ') {
|
53 | keys.push(e.key.toUpperCase());
|
54 | }
|
55 |
|
56 | if (e.key === ' ') {
|
57 | keys.push('space');
|
58 | }
|
59 |
|
60 | if (e.key === 'Escape') {
|
61 | keys.push('escape');
|
62 | }
|
63 |
|
64 | if (e.key === 'ArrowRight') {
|
65 | keys.push('ArrowRight');
|
66 | }
|
67 |
|
68 | if (e.key === 'ArrowDown') {
|
69 | keys.push('ArrowDown');
|
70 | }
|
71 |
|
72 | if (e.key === 'ArrowUp') {
|
73 | keys.push('ArrowUp');
|
74 | }
|
75 |
|
76 | if (e.key === 'ArrowLeft') {
|
77 | keys.push('ArrowLeft');
|
78 | }
|
79 |
|
80 | return keys.length > 0 ? keys : null;
|
81 | };
|
82 | export var shortcutMatchesShortcut = function shortcutMatchesShortcut(inputShortcut, shortcut) {
|
83 | if (!inputShortcut || !shortcut) return false;
|
84 | if (inputShortcut.join('') === 'shift/') inputShortcut.shift();
|
85 |
|
86 | if (inputShortcut.length !== shortcut.length) return false;
|
87 | return !inputShortcut.find(function (key, i) {
|
88 | return key !== shortcut[i];
|
89 | });
|
90 | };
|
91 |
|
92 | export var eventMatchesShortcut = function eventMatchesShortcut(e, shortcut) {
|
93 | return shortcutMatchesShortcut(eventToShortcut(e), shortcut);
|
94 | };
|
95 | export var keyToSymbol = function keyToSymbol(key) {
|
96 | if (key === 'alt') {
|
97 | return optionOrAltSymbol();
|
98 | }
|
99 |
|
100 | if (key === 'control') {
|
101 | return '⌃';
|
102 | }
|
103 |
|
104 | if (key === 'meta') {
|
105 | return '⌘';
|
106 | }
|
107 |
|
108 | if (key === 'shift') {
|
109 | return '⇧';
|
110 | }
|
111 |
|
112 | if (key === 'Enter' || key === 'Backspace' || key === 'Esc') {
|
113 | return '';
|
114 | }
|
115 |
|
116 | if (key === 'escape') {
|
117 | return '';
|
118 | }
|
119 |
|
120 | if (key === ' ') {
|
121 | return 'SPACE';
|
122 | }
|
123 |
|
124 | if (key === 'ArrowUp') {
|
125 | return '↑';
|
126 | }
|
127 |
|
128 | if (key === 'ArrowDown') {
|
129 | return '↓';
|
130 | }
|
131 |
|
132 | if (key === 'ArrowLeft') {
|
133 | return '←';
|
134 | }
|
135 |
|
136 | if (key === 'ArrowRight') {
|
137 | return '→';
|
138 | }
|
139 |
|
140 | return key.toUpperCase();
|
141 | };
|
142 |
|
143 | export var shortcutToHumanString = function shortcutToHumanString(shortcut) {
|
144 | return shortcut.map(keyToSymbol).join(' ');
|
145 | }; |
\ | No newline at end of file |