UNPKG

3.58 kBJavaScriptView Raw
1import "core-js/modules/es.regexp.exec.js";
2import "core-js/modules/es.string.match.js";
3import "core-js/modules/es.array.includes.js";
4import "core-js/modules/es.array.join.js";
5import "core-js/modules/es.array.find.js";
6import "core-js/modules/es.object.to-string.js";
7import "core-js/modules/es.array.map.js";
8import global from 'global'; // The shortcut is our JSON-ifiable representation of a shortcut combination
9
10var navigator = global.navigator;
11export var isMacLike = function isMacLike() {
12 return navigator && navigator.platform ? !!navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i) : false;
13};
14export var controlOrMetaSymbol = function controlOrMetaSymbol() {
15 return isMacLike() ? '⌘' : 'ctrl';
16};
17export var controlOrMetaKey = function controlOrMetaKey() {
18 return isMacLike() ? 'meta' : 'control';
19};
20export var optionOrAltSymbol = function optionOrAltSymbol() {
21 return isMacLike() ? '⌥' : 'alt';
22};
23export var isShortcutTaken = function isShortcutTaken(arr1, arr2) {
24 return JSON.stringify(arr1) === JSON.stringify(arr2);
25}; // Map a keyboard event to a keyboard shortcut
26// NOTE: if we change the fields on the event that we need, we'll need to update the serialization in core/preview/start.js
27
28export var eventToShortcut = function eventToShortcut(e) {
29 // Meta key only doesn't map to a shortcut
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};
82export var shortcutMatchesShortcut = function shortcutMatchesShortcut(inputShortcut, shortcut) {
83 if (!inputShortcut || !shortcut) return false;
84 if (inputShortcut.join('') === 'shift/') inputShortcut.shift(); // shift is optional for `/`
85
86 if (inputShortcut.length !== shortcut.length) return false;
87 return !inputShortcut.find(function (key, i) {
88 return key !== shortcut[i];
89 });
90}; // Should this keyboard event trigger this keyboard shortcut?
91
92export var eventMatchesShortcut = function eventMatchesShortcut(e, shortcut) {
93 return shortcutMatchesShortcut(eventToShortcut(e), shortcut);
94};
95export 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}; // Display the shortcut as a human readable string
142
143export var shortcutToHumanString = function shortcutToHumanString(shortcut) {
144 return shortcut.map(keyToSymbol).join(' ');
145};
\No newline at end of file