UNPKG

4.43 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.shortcutToHumanString = exports.shortcutMatchesShortcut = exports.optionOrAltSymbol = exports.keyToSymbol = exports.isShortcutTaken = exports.isMacLike = exports.eventToShortcut = exports.eventMatchesShortcut = exports.controlOrMetaSymbol = exports.controlOrMetaKey = void 0;
7
8require("core-js/modules/es.regexp.exec.js");
9
10require("core-js/modules/es.string.match.js");
11
12require("core-js/modules/es.array.includes.js");
13
14require("core-js/modules/es.array.join.js");
15
16require("core-js/modules/es.array.find.js");
17
18require("core-js/modules/es.object.to-string.js");
19
20require("core-js/modules/es.array.map.js");
21
22var _global = _interopRequireDefault(require("global"));
23
24function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26var navigator = _global.default.navigator;
27
28var isMacLike = function isMacLike() {
29 return navigator && navigator.platform ? !!navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i) : false;
30};
31
32exports.isMacLike = isMacLike;
33
34var controlOrMetaSymbol = function controlOrMetaSymbol() {
35 return isMacLike() ? '⌘' : 'ctrl';
36};
37
38exports.controlOrMetaSymbol = controlOrMetaSymbol;
39
40var controlOrMetaKey = function controlOrMetaKey() {
41 return isMacLike() ? 'meta' : 'control';
42};
43
44exports.controlOrMetaKey = controlOrMetaKey;
45
46var optionOrAltSymbol = function optionOrAltSymbol() {
47 return isMacLike() ? '⌥' : 'alt';
48};
49
50exports.optionOrAltSymbol = optionOrAltSymbol;
51
52var isShortcutTaken = function isShortcutTaken(arr1, arr2) {
53 return JSON.stringify(arr1) === JSON.stringify(arr2);
54}; // Map a keyboard event to a keyboard shortcut
55// NOTE: if we change the fields on the event that we need, we'll need to update the serialization in core/preview/start.js
56
57
58exports.isShortcutTaken = isShortcutTaken;
59
60var eventToShortcut = function eventToShortcut(e) {
61 // Meta key only doesn't map to a shortcut
62 if (['Meta', 'Alt', 'Control', 'Shift'].includes(e.key)) {
63 return null;
64 }
65
66 var keys = [];
67
68 if (e.altKey) {
69 keys.push('alt');
70 }
71
72 if (e.ctrlKey) {
73 keys.push('control');
74 }
75
76 if (e.metaKey) {
77 keys.push('meta');
78 }
79
80 if (e.shiftKey) {
81 keys.push('shift');
82 }
83
84 if (e.key && e.key.length === 1 && e.key !== ' ') {
85 keys.push(e.key.toUpperCase());
86 }
87
88 if (e.key === ' ') {
89 keys.push('space');
90 }
91
92 if (e.key === 'Escape') {
93 keys.push('escape');
94 }
95
96 if (e.key === 'ArrowRight') {
97 keys.push('ArrowRight');
98 }
99
100 if (e.key === 'ArrowDown') {
101 keys.push('ArrowDown');
102 }
103
104 if (e.key === 'ArrowUp') {
105 keys.push('ArrowUp');
106 }
107
108 if (e.key === 'ArrowLeft') {
109 keys.push('ArrowLeft');
110 }
111
112 return keys.length > 0 ? keys : null;
113};
114
115exports.eventToShortcut = eventToShortcut;
116
117var shortcutMatchesShortcut = function shortcutMatchesShortcut(inputShortcut, shortcut) {
118 if (!inputShortcut || !shortcut) return false;
119 if (inputShortcut.join('') === 'shift/') inputShortcut.shift(); // shift is optional for `/`
120
121 if (inputShortcut.length !== shortcut.length) return false;
122 return !inputShortcut.find(function (key, i) {
123 return key !== shortcut[i];
124 });
125}; // Should this keyboard event trigger this keyboard shortcut?
126
127
128exports.shortcutMatchesShortcut = shortcutMatchesShortcut;
129
130var eventMatchesShortcut = function eventMatchesShortcut(e, shortcut) {
131 return shortcutMatchesShortcut(eventToShortcut(e), shortcut);
132};
133
134exports.eventMatchesShortcut = eventMatchesShortcut;
135
136var keyToSymbol = function keyToSymbol(key) {
137 if (key === 'alt') {
138 return optionOrAltSymbol();
139 }
140
141 if (key === 'control') {
142 return '⌃';
143 }
144
145 if (key === 'meta') {
146 return '⌘';
147 }
148
149 if (key === 'shift') {
150 return '⇧​';
151 }
152
153 if (key === 'Enter' || key === 'Backspace' || key === 'Esc') {
154 return '';
155 }
156
157 if (key === 'escape') {
158 return '';
159 }
160
161 if (key === ' ') {
162 return 'SPACE';
163 }
164
165 if (key === 'ArrowUp') {
166 return '↑';
167 }
168
169 if (key === 'ArrowDown') {
170 return '↓';
171 }
172
173 if (key === 'ArrowLeft') {
174 return '←';
175 }
176
177 if (key === 'ArrowRight') {
178 return '→';
179 }
180
181 return key.toUpperCase();
182}; // Display the shortcut as a human readable string
183
184
185exports.keyToSymbol = keyToSymbol;
186
187var shortcutToHumanString = function shortcutToHumanString(shortcut) {
188 return shortcut.map(keyToSymbol).join(' ');
189};
190
191exports.shortcutToHumanString = shortcutToHumanString;
\No newline at end of file