@tanstack/hotkeys 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +121 -45
- package/dist/constants.cjs +444 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +226 -0
- package/dist/constants.d.ts +226 -0
- package/dist/constants.js +428 -0
- package/dist/constants.js.map +1 -0
- package/dist/format.cjs +178 -0
- package/dist/format.cjs.map +1 -0
- package/dist/format.d.cts +110 -0
- package/dist/format.d.ts +110 -0
- package/dist/format.js +175 -0
- package/dist/format.js.map +1 -0
- package/dist/hotkey-manager.cjs +420 -0
- package/dist/hotkey-manager.cjs.map +1 -0
- package/dist/hotkey-manager.d.cts +207 -0
- package/dist/hotkey-manager.d.ts +207 -0
- package/dist/hotkey-manager.js +419 -0
- package/dist/hotkey-manager.js.map +1 -0
- package/dist/hotkey.d.cts +278 -0
- package/dist/hotkey.d.ts +278 -0
- package/dist/index.cjs +54 -0
- package/dist/index.d.cts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/key-state-tracker.cjs +197 -0
- package/dist/key-state-tracker.cjs.map +1 -0
- package/dist/key-state-tracker.d.cts +107 -0
- package/dist/key-state-tracker.d.ts +107 -0
- package/dist/key-state-tracker.js +196 -0
- package/dist/key-state-tracker.js.map +1 -0
- package/dist/match.cjs +143 -0
- package/dist/match.cjs.map +1 -0
- package/dist/match.d.cts +79 -0
- package/dist/match.d.ts +79 -0
- package/dist/match.js +141 -0
- package/dist/match.js.map +1 -0
- package/dist/parse.cjs +266 -0
- package/dist/parse.cjs.map +1 -0
- package/dist/parse.d.cts +169 -0
- package/dist/parse.d.ts +169 -0
- package/dist/parse.js +258 -0
- package/dist/parse.js.map +1 -0
- package/dist/recorder.cjs +177 -0
- package/dist/recorder.cjs.map +1 -0
- package/dist/recorder.d.cts +108 -0
- package/dist/recorder.d.ts +108 -0
- package/dist/recorder.js +177 -0
- package/dist/recorder.js.map +1 -0
- package/dist/sequence.cjs +242 -0
- package/dist/sequence.cjs.map +1 -0
- package/dist/sequence.d.cts +109 -0
- package/dist/sequence.d.ts +109 -0
- package/dist/sequence.js +240 -0
- package/dist/sequence.js.map +1 -0
- package/dist/validate.cjs +116 -0
- package/dist/validate.cjs.map +1 -0
- package/dist/validate.d.cts +56 -0
- package/dist/validate.d.ts +56 -0
- package/dist/validate.js +114 -0
- package/dist/validate.js.map +1 -0
- package/package.json +55 -7
- package/src/constants.ts +514 -0
- package/src/format.ts +261 -0
- package/src/hotkey-manager.ts +822 -0
- package/src/hotkey.ts +411 -0
- package/src/index.ts +10 -0
- package/src/key-state-tracker.ts +249 -0
- package/src/match.ts +222 -0
- package/src/parse.ts +368 -0
- package/src/recorder.ts +266 -0
- package/src/sequence.ts +391 -0
- package/src/validate.ts +171 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
//#region src/constants.ts
|
|
2
|
+
/**
|
|
3
|
+
* Detects the current platform based on browser navigator properties.
|
|
4
|
+
*
|
|
5
|
+
* Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,
|
|
6
|
+
* Control elsewhere) and for platform-specific hotkey formatting.
|
|
7
|
+
*
|
|
8
|
+
* @returns The detected platform: 'mac', 'windows', or 'linux'
|
|
9
|
+
* @remarks Defaults to 'linux' in SSR environments where navigator is undefined
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const platform = detectPlatform() // 'mac' | 'windows' | 'linux'
|
|
14
|
+
* const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function detectPlatform() {
|
|
18
|
+
if (typeof navigator === "undefined") return "linux";
|
|
19
|
+
const platform = navigator.platform.toLowerCase();
|
|
20
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
21
|
+
if (platform.includes("mac") || userAgent.includes("mac")) return "mac";
|
|
22
|
+
if (platform.includes("win") || userAgent.includes("win")) return "windows";
|
|
23
|
+
return "linux";
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Canonical order for modifiers in normalized hotkey strings.
|
|
27
|
+
*
|
|
28
|
+
* Defines the standard order in which modifiers should appear when formatting
|
|
29
|
+
* hotkeys. This ensures consistent, predictable output across the library.
|
|
30
|
+
*
|
|
31
|
+
* Order: Control → Alt → Shift → Meta
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* // Input: 'Shift+Control+Meta+S'
|
|
36
|
+
* // Normalized: 'Control+Alt+Shift+Meta+S' (following MODIFIER_ORDER)
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
const MODIFIER_ORDER = [
|
|
40
|
+
"Control",
|
|
41
|
+
"Alt",
|
|
42
|
+
"Shift",
|
|
43
|
+
"Meta"
|
|
44
|
+
];
|
|
45
|
+
/**
|
|
46
|
+
* Set of canonical modifier key names for fast lookup.
|
|
47
|
+
*
|
|
48
|
+
* Derived from `MODIFIER_ORDER` to ensure consistency. Used to detect when
|
|
49
|
+
* a modifier is released so we can clear non-modifier keys whose keyup events
|
|
50
|
+
* may have been swallowed by the OS (e.g. macOS Cmd+key combos).
|
|
51
|
+
*/
|
|
52
|
+
const MODIFIER_KEYS = new Set(MODIFIER_ORDER);
|
|
53
|
+
/**
|
|
54
|
+
* Maps modifier key aliases to their canonical form or platform-adaptive 'Mod'.
|
|
55
|
+
*
|
|
56
|
+
* This map allows users to write hotkeys using various aliases (e.g., 'Ctrl', 'Cmd', 'Option')
|
|
57
|
+
* which are then normalized to canonical names ('Control', 'Meta', 'Alt') or the
|
|
58
|
+
* platform-adaptive 'Mod' token.
|
|
59
|
+
*
|
|
60
|
+
* The 'Mod' and 'CommandOrControl' aliases are resolved at runtime via `resolveModifier()`
|
|
61
|
+
* based on the detected platform (Command on Mac, Control elsewhere).
|
|
62
|
+
*
|
|
63
|
+
* @remarks Case-insensitive lookups are supported via lowercase variants
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* MODIFIER_ALIASES['Ctrl'] // 'Control'
|
|
68
|
+
* MODIFIER_ALIASES['Cmd'] // 'Meta'
|
|
69
|
+
* MODIFIER_ALIASES['Mod'] // 'Mod' (resolved at runtime)
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
const MODIFIER_ALIASES = {
|
|
73
|
+
Control: "Control",
|
|
74
|
+
Ctrl: "Control",
|
|
75
|
+
control: "Control",
|
|
76
|
+
ctrl: "Control",
|
|
77
|
+
Shift: "Shift",
|
|
78
|
+
shift: "Shift",
|
|
79
|
+
Alt: "Alt",
|
|
80
|
+
Option: "Alt",
|
|
81
|
+
alt: "Alt",
|
|
82
|
+
option: "Alt",
|
|
83
|
+
Command: "Meta",
|
|
84
|
+
Cmd: "Meta",
|
|
85
|
+
Meta: "Meta",
|
|
86
|
+
command: "Meta",
|
|
87
|
+
cmd: "Meta",
|
|
88
|
+
meta: "Meta",
|
|
89
|
+
CommandOrControl: "Mod",
|
|
90
|
+
Mod: "Mod",
|
|
91
|
+
commandorcontrol: "Mod",
|
|
92
|
+
mod: "Mod"
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.
|
|
96
|
+
*
|
|
97
|
+
* The 'Mod' token represents the "primary modifier" on each platform:
|
|
98
|
+
* - macOS: 'Meta' (Command key ⌘)
|
|
99
|
+
* - Windows/Linux: 'Control' (Ctrl key)
|
|
100
|
+
*
|
|
101
|
+
* This enables cross-platform hotkey definitions like 'Mod+S' that automatically
|
|
102
|
+
* map to Command+S on Mac and Ctrl+S on Windows/Linux.
|
|
103
|
+
*
|
|
104
|
+
* @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.
|
|
105
|
+
* @param platform - The target platform. Defaults to auto-detection.
|
|
106
|
+
* @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* resolveModifier('Mod', 'mac') // 'Meta'
|
|
111
|
+
* resolveModifier('Mod', 'windows') // 'Control'
|
|
112
|
+
* resolveModifier('Control', 'mac') // 'Control' (unchanged)
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
function resolveModifier(modifier, platform = detectPlatform()) {
|
|
116
|
+
if (modifier === "Mod") return platform === "mac" ? "Meta" : "Control";
|
|
117
|
+
return modifier;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Set of all valid letter keys (A-Z).
|
|
121
|
+
*
|
|
122
|
+
* Used for validation and type checking. Letter keys are matched case-insensitively
|
|
123
|
+
* in hotkey matching, but normalized to uppercase in canonical form.
|
|
124
|
+
*/
|
|
125
|
+
const LETTER_KEYS = new Set([
|
|
126
|
+
"A",
|
|
127
|
+
"B",
|
|
128
|
+
"C",
|
|
129
|
+
"D",
|
|
130
|
+
"E",
|
|
131
|
+
"F",
|
|
132
|
+
"G",
|
|
133
|
+
"H",
|
|
134
|
+
"I",
|
|
135
|
+
"J",
|
|
136
|
+
"K",
|
|
137
|
+
"L",
|
|
138
|
+
"M",
|
|
139
|
+
"N",
|
|
140
|
+
"O",
|
|
141
|
+
"P",
|
|
142
|
+
"Q",
|
|
143
|
+
"R",
|
|
144
|
+
"S",
|
|
145
|
+
"T",
|
|
146
|
+
"U",
|
|
147
|
+
"V",
|
|
148
|
+
"W",
|
|
149
|
+
"X",
|
|
150
|
+
"Y",
|
|
151
|
+
"Z"
|
|
152
|
+
]);
|
|
153
|
+
/**
|
|
154
|
+
* Set of all valid number keys (0-9).
|
|
155
|
+
*
|
|
156
|
+
* Note: Number keys are affected by Shift (Shift+1 → '!' on US layout),
|
|
157
|
+
* so they're excluded from Shift-based hotkey combinations to avoid
|
|
158
|
+
* layout-dependent behavior.
|
|
159
|
+
*/
|
|
160
|
+
const NUMBER_KEYS = new Set([
|
|
161
|
+
"0",
|
|
162
|
+
"1",
|
|
163
|
+
"2",
|
|
164
|
+
"3",
|
|
165
|
+
"4",
|
|
166
|
+
"5",
|
|
167
|
+
"6",
|
|
168
|
+
"7",
|
|
169
|
+
"8",
|
|
170
|
+
"9"
|
|
171
|
+
]);
|
|
172
|
+
/**
|
|
173
|
+
* Set of all valid function keys (F1-F12).
|
|
174
|
+
*
|
|
175
|
+
* Function keys are commonly used for system shortcuts (e.g., F12 for DevTools,
|
|
176
|
+
* Alt+F4 to close windows) and application-specific commands.
|
|
177
|
+
*/
|
|
178
|
+
const FUNCTION_KEYS = new Set([
|
|
179
|
+
"F1",
|
|
180
|
+
"F2",
|
|
181
|
+
"F3",
|
|
182
|
+
"F4",
|
|
183
|
+
"F5",
|
|
184
|
+
"F6",
|
|
185
|
+
"F7",
|
|
186
|
+
"F8",
|
|
187
|
+
"F9",
|
|
188
|
+
"F10",
|
|
189
|
+
"F11",
|
|
190
|
+
"F12"
|
|
191
|
+
]);
|
|
192
|
+
/**
|
|
193
|
+
* Set of all valid navigation keys for cursor movement and document navigation.
|
|
194
|
+
*
|
|
195
|
+
* Includes arrow keys, Home/End (line navigation), and PageUp/PageDown (page navigation).
|
|
196
|
+
* These keys are commonly combined with modifiers for selection (Shift+ArrowUp) or
|
|
197
|
+
* navigation shortcuts (Alt+ArrowLeft for back).
|
|
198
|
+
*/
|
|
199
|
+
const NAVIGATION_KEYS = new Set([
|
|
200
|
+
"ArrowUp",
|
|
201
|
+
"ArrowDown",
|
|
202
|
+
"ArrowLeft",
|
|
203
|
+
"ArrowRight",
|
|
204
|
+
"Home",
|
|
205
|
+
"End",
|
|
206
|
+
"PageUp",
|
|
207
|
+
"PageDown"
|
|
208
|
+
]);
|
|
209
|
+
/**
|
|
210
|
+
* Set of all valid editing and special keys.
|
|
211
|
+
*
|
|
212
|
+
* Includes keys commonly used for text editing (Enter, Backspace, Delete, Tab) and
|
|
213
|
+
* special actions (Escape, Space). These keys are frequently combined with modifiers
|
|
214
|
+
* for editor shortcuts (Mod+Enter to submit, Shift+Tab to go back).
|
|
215
|
+
*/
|
|
216
|
+
const EDITING_KEYS = new Set([
|
|
217
|
+
"Enter",
|
|
218
|
+
"Escape",
|
|
219
|
+
"Space",
|
|
220
|
+
"Tab",
|
|
221
|
+
"Backspace",
|
|
222
|
+
"Delete"
|
|
223
|
+
]);
|
|
224
|
+
/**
|
|
225
|
+
* Set of all valid punctuation keys commonly used in keyboard shortcuts.
|
|
226
|
+
*
|
|
227
|
+
* These are the literal characters as they appear in `KeyboardEvent.key` (layout-dependent,
|
|
228
|
+
* typically US keyboard layout). Common shortcuts include:
|
|
229
|
+
* - `Mod+/` - Toggle comment
|
|
230
|
+
* - `Mod+[` / `Mod+]` - Indent/outdent
|
|
231
|
+
* - `Mod+=` / `Mod+-` - Zoom in/out
|
|
232
|
+
*
|
|
233
|
+
* Note: Punctuation keys are affected by Shift (Shift+',' → '<' on US layout),
|
|
234
|
+
* so they're excluded from Shift-based hotkey combinations to avoid layout-dependent behavior.
|
|
235
|
+
*/
|
|
236
|
+
const PUNCTUATION_KEYS = new Set([
|
|
237
|
+
"/",
|
|
238
|
+
"[",
|
|
239
|
+
"]",
|
|
240
|
+
"\\",
|
|
241
|
+
"=",
|
|
242
|
+
"-",
|
|
243
|
+
",",
|
|
244
|
+
".",
|
|
245
|
+
"`"
|
|
246
|
+
]);
|
|
247
|
+
/**
|
|
248
|
+
* Set of all valid non-modifier keys.
|
|
249
|
+
*
|
|
250
|
+
* This is the union of all key type sets (letters, numbers, function keys, navigation,
|
|
251
|
+
* editing, and punctuation). Used primarily for validation to check if a key string
|
|
252
|
+
* is recognized and will have type-safe autocomplete support.
|
|
253
|
+
*
|
|
254
|
+
* @see {@link LETTER_KEYS}
|
|
255
|
+
* @see {@link NUMBER_KEYS}
|
|
256
|
+
* @see {@link FUNCTION_KEYS}
|
|
257
|
+
* @see {@link NAVIGATION_KEYS}
|
|
258
|
+
* @see {@link EDITING_KEYS}
|
|
259
|
+
* @see {@link PUNCTUATION_KEYS}
|
|
260
|
+
*/
|
|
261
|
+
const ALL_KEYS = new Set([
|
|
262
|
+
...LETTER_KEYS,
|
|
263
|
+
...NUMBER_KEYS,
|
|
264
|
+
...FUNCTION_KEYS,
|
|
265
|
+
...NAVIGATION_KEYS,
|
|
266
|
+
...EDITING_KEYS,
|
|
267
|
+
...PUNCTUATION_KEYS
|
|
268
|
+
]);
|
|
269
|
+
/**
|
|
270
|
+
* Maps key name aliases to their canonical form.
|
|
271
|
+
*
|
|
272
|
+
* Handles common variations and alternative names for keys to provide a more
|
|
273
|
+
* forgiving API. For example, users can write 'Esc', 'esc', or 'escape' and
|
|
274
|
+
* they'll all normalize to 'Escape'.
|
|
275
|
+
*
|
|
276
|
+
* This map is used internally by `normalizeKeyName()` to convert user input
|
|
277
|
+
* into the canonical key names used throughout the library.
|
|
278
|
+
*
|
|
279
|
+
* @remarks Case-insensitive lookups are supported via lowercase variants
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```ts
|
|
283
|
+
* KEY_ALIASES['Esc'] // 'Escape'
|
|
284
|
+
* KEY_ALIASES['Del'] // 'Delete'
|
|
285
|
+
* KEY_ALIASES['Up'] // 'ArrowUp'
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
const KEY_ALIASES = {
|
|
289
|
+
Esc: "Escape",
|
|
290
|
+
esc: "Escape",
|
|
291
|
+
escape: "Escape",
|
|
292
|
+
Return: "Enter",
|
|
293
|
+
return: "Enter",
|
|
294
|
+
enter: "Enter",
|
|
295
|
+
" ": "Space",
|
|
296
|
+
space: "Space",
|
|
297
|
+
Spacebar: "Space",
|
|
298
|
+
spacebar: "Space",
|
|
299
|
+
tab: "Tab",
|
|
300
|
+
backspace: "Backspace",
|
|
301
|
+
Del: "Delete",
|
|
302
|
+
del: "Delete",
|
|
303
|
+
delete: "Delete",
|
|
304
|
+
Up: "ArrowUp",
|
|
305
|
+
up: "ArrowUp",
|
|
306
|
+
arrowup: "ArrowUp",
|
|
307
|
+
Down: "ArrowDown",
|
|
308
|
+
down: "ArrowDown",
|
|
309
|
+
arrowdown: "ArrowDown",
|
|
310
|
+
Left: "ArrowLeft",
|
|
311
|
+
left: "ArrowLeft",
|
|
312
|
+
arrowleft: "ArrowLeft",
|
|
313
|
+
Right: "ArrowRight",
|
|
314
|
+
right: "ArrowRight",
|
|
315
|
+
arrowright: "ArrowRight",
|
|
316
|
+
home: "Home",
|
|
317
|
+
end: "End",
|
|
318
|
+
pageup: "PageUp",
|
|
319
|
+
pagedown: "PageDown",
|
|
320
|
+
PgUp: "PageUp",
|
|
321
|
+
PgDn: "PageDown",
|
|
322
|
+
pgup: "PageUp",
|
|
323
|
+
pgdn: "PageDown"
|
|
324
|
+
};
|
|
325
|
+
/**
|
|
326
|
+
* Normalizes a key name to its canonical form.
|
|
327
|
+
*
|
|
328
|
+
* Converts various key name formats (aliases, case variations) into the standard
|
|
329
|
+
* canonical names used throughout the library. This enables a more forgiving API
|
|
330
|
+
* where users can write keys in different ways and still get correct behavior.
|
|
331
|
+
*
|
|
332
|
+
* Normalization rules:
|
|
333
|
+
* 1. Check aliases first (e.g., 'Esc' → 'Escape', 'Del' → 'Delete')
|
|
334
|
+
* 2. Single letters → uppercase (e.g., 'a' → 'A', 's' → 'S')
|
|
335
|
+
* 3. Function keys → uppercase (e.g., 'f1' → 'F1', 'F12' → 'F12')
|
|
336
|
+
* 4. Other keys → returned as-is (already canonical or unknown)
|
|
337
|
+
*
|
|
338
|
+
* @param key - The key name to normalize (can be an alias, lowercase, etc.)
|
|
339
|
+
* @returns The canonical key name
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* ```ts
|
|
343
|
+
* normalizeKeyName('esc') // 'Escape'
|
|
344
|
+
* normalizeKeyName('a') // 'A'
|
|
345
|
+
* normalizeKeyName('f1') // 'F1'
|
|
346
|
+
* normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical)
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
function normalizeKeyName(key) {
|
|
350
|
+
if (key in KEY_ALIASES) return KEY_ALIASES[key];
|
|
351
|
+
if (key.length === 1 && /^[a-zA-Z]$/.test(key)) return key.toUpperCase();
|
|
352
|
+
const upperKey = key.toUpperCase();
|
|
353
|
+
if (/^F([1-9]|1[0-2])$/.test(upperKey)) return upperKey;
|
|
354
|
+
return key;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Modifier key symbols for macOS display.
|
|
358
|
+
*
|
|
359
|
+
* Used by formatting functions to display hotkeys with macOS-style symbols
|
|
360
|
+
* (e.g., ⌘ for Command, ⌃ for Control) instead of text labels. This provides
|
|
361
|
+
* a native macOS look and feel in hotkey displays.
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```ts
|
|
365
|
+
* MAC_MODIFIER_SYMBOLS['Meta'] // '⌘'
|
|
366
|
+
* MAC_MODIFIER_SYMBOLS['Control'] // '⌃'
|
|
367
|
+
* MAC_MODIFIER_SYMBOLS['Alt'] // '⌥'
|
|
368
|
+
* MAC_MODIFIER_SYMBOLS['Shift'] // '⇧'
|
|
369
|
+
* ```
|
|
370
|
+
*/
|
|
371
|
+
const MAC_MODIFIER_SYMBOLS = {
|
|
372
|
+
Control: "⌃",
|
|
373
|
+
Alt: "⌥",
|
|
374
|
+
Shift: "⇧",
|
|
375
|
+
Meta: "⌘"
|
|
376
|
+
};
|
|
377
|
+
/**
|
|
378
|
+
* Modifier key labels for Windows/Linux display.
|
|
379
|
+
*
|
|
380
|
+
* Used by formatting functions to display hotkeys with standard text labels
|
|
381
|
+
* (e.g., 'Ctrl' for Control, 'Win' for Meta/Windows key) instead of symbols.
|
|
382
|
+
* This provides a familiar Windows/Linux look and feel in hotkey displays.
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```ts
|
|
386
|
+
* STANDARD_MODIFIER_LABELS['Control'] // 'Ctrl'
|
|
387
|
+
* STANDARD_MODIFIER_LABELS['Meta'] // 'Win'
|
|
388
|
+
* STANDARD_MODIFIER_LABELS['Alt'] // 'Alt'
|
|
389
|
+
* STANDARD_MODIFIER_LABELS['Shift'] // 'Shift'
|
|
390
|
+
* ```
|
|
391
|
+
*/
|
|
392
|
+
const STANDARD_MODIFIER_LABELS = {
|
|
393
|
+
Control: "Ctrl",
|
|
394
|
+
Alt: "Alt",
|
|
395
|
+
Shift: "Shift",
|
|
396
|
+
Meta: "Win"
|
|
397
|
+
};
|
|
398
|
+
/**
|
|
399
|
+
* Special key symbols for display formatting.
|
|
400
|
+
*
|
|
401
|
+
* Maps certain keys to their visual symbols for better readability in hotkey displays.
|
|
402
|
+
* Used by formatting functions to show symbols like ↑ for ArrowUp or ↵ for Enter
|
|
403
|
+
* instead of text labels.
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* ```ts
|
|
407
|
+
* KEY_DISPLAY_SYMBOLS['ArrowUp'] // '↑'
|
|
408
|
+
* KEY_DISPLAY_SYMBOLS['Enter'] // '↵'
|
|
409
|
+
* KEY_DISPLAY_SYMBOLS['Escape'] // 'Esc'
|
|
410
|
+
* KEY_DISPLAY_SYMBOLS['Space'] // '␣'
|
|
411
|
+
* ```
|
|
412
|
+
*/
|
|
413
|
+
const KEY_DISPLAY_SYMBOLS = {
|
|
414
|
+
ArrowUp: "↑",
|
|
415
|
+
ArrowDown: "↓",
|
|
416
|
+
ArrowLeft: "←",
|
|
417
|
+
ArrowRight: "→",
|
|
418
|
+
Enter: "↵",
|
|
419
|
+
Escape: "Esc",
|
|
420
|
+
Backspace: "⌫",
|
|
421
|
+
Delete: "⌦",
|
|
422
|
+
Tab: "⇥",
|
|
423
|
+
Space: "␣"
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
//#endregion
|
|
427
|
+
export { ALL_KEYS, EDITING_KEYS, FUNCTION_KEYS, KEY_DISPLAY_SYMBOLS, LETTER_KEYS, MAC_MODIFIER_SYMBOLS, MODIFIER_ALIASES, MODIFIER_KEYS, MODIFIER_ORDER, NAVIGATION_KEYS, NUMBER_KEYS, PUNCTUATION_KEYS, STANDARD_MODIFIER_LABELS, detectPlatform, normalizeKeyName, resolveModifier };
|
|
428
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","names":[],"sources":["../src/constants.ts"],"sourcesContent":["import type {\n CanonicalModifier,\n EditingKey,\n FunctionKey,\n LetterKey,\n NavigationKey,\n NumberKey,\n PunctuationKey,\n} from './hotkey'\n\n// =============================================================================\n// Platform Detection\n// =============================================================================\n\n/**\n * Detects the current platform based on browser navigator properties.\n *\n * Used internally to resolve platform-adaptive modifiers like 'Mod' (Command on Mac,\n * Control elsewhere) and for platform-specific hotkey formatting.\n *\n * @returns The detected platform: 'mac', 'windows', or 'linux'\n * @remarks Defaults to 'linux' in SSR environments where navigator is undefined\n *\n * @example\n * ```ts\n * const platform = detectPlatform() // 'mac' | 'windows' | 'linux'\n * const modifier = resolveModifier('Mod', platform) // 'Meta' on Mac, 'Control' elsewhere\n * ```\n */\nexport function detectPlatform(): 'mac' | 'windows' | 'linux' {\n if (typeof navigator === 'undefined') {\n return 'linux' // Default for SSR\n }\n\n const platform = navigator.platform.toLowerCase()\n const userAgent = navigator.userAgent.toLowerCase()\n\n if (platform.includes('mac') || userAgent.includes('mac')) {\n return 'mac'\n }\n if (platform.includes('win') || userAgent.includes('win')) {\n return 'windows'\n }\n return 'linux'\n}\n\n// =============================================================================\n// Modifier Aliases\n// =============================================================================\n\n/**\n * Canonical order for modifiers in normalized hotkey strings.\n *\n * Defines the standard order in which modifiers should appear when formatting\n * hotkeys. This ensures consistent, predictable output across the library.\n *\n * Order: Control → Alt → Shift → Meta\n *\n * @example\n * ```ts\n * // Input: 'Shift+Control+Meta+S'\n * // Normalized: 'Control+Alt+Shift+Meta+S' (following MODIFIER_ORDER)\n * ```\n */\nexport const MODIFIER_ORDER: Array<CanonicalModifier> = [\n 'Control',\n 'Alt',\n 'Shift',\n 'Meta',\n]\n\n/**\n * Set of canonical modifier key names for fast lookup.\n *\n * Derived from `MODIFIER_ORDER` to ensure consistency. Used to detect when\n * a modifier is released so we can clear non-modifier keys whose keyup events\n * may have been swallowed by the OS (e.g. macOS Cmd+key combos).\n */\nexport const MODIFIER_KEYS = new Set<string>(MODIFIER_ORDER)\n\n/**\n * Maps modifier key aliases to their canonical form or platform-adaptive 'Mod'.\n *\n * This map allows users to write hotkeys using various aliases (e.g., 'Ctrl', 'Cmd', 'Option')\n * which are then normalized to canonical names ('Control', 'Meta', 'Alt') or the\n * platform-adaptive 'Mod' token.\n *\n * The 'Mod' and 'CommandOrControl' aliases are resolved at runtime via `resolveModifier()`\n * based on the detected platform (Command on Mac, Control elsewhere).\n *\n * @remarks Case-insensitive lookups are supported via lowercase variants\n *\n * @example\n * ```ts\n * MODIFIER_ALIASES['Ctrl'] // 'Control'\n * MODIFIER_ALIASES['Cmd'] // 'Meta'\n * MODIFIER_ALIASES['Mod'] // 'Mod' (resolved at runtime)\n * ```\n */\nexport const MODIFIER_ALIASES: Record<string, CanonicalModifier | 'Mod'> = {\n // Control variants\n Control: 'Control',\n Ctrl: 'Control',\n control: 'Control',\n ctrl: 'Control',\n\n // Shift variants\n Shift: 'Shift',\n shift: 'Shift',\n\n // Alt variants\n Alt: 'Alt',\n Option: 'Alt',\n alt: 'Alt',\n option: 'Alt',\n\n // Meta/Command variants\n Command: 'Meta',\n Cmd: 'Meta',\n Meta: 'Meta',\n command: 'Meta',\n cmd: 'Meta',\n meta: 'Meta',\n\n // Platform-adaptive (resolved at runtime)\n CommandOrControl: 'Mod',\n Mod: 'Mod',\n commandorcontrol: 'Mod',\n mod: 'Mod',\n}\n\n/**\n * Resolves the platform-adaptive 'Mod' modifier to the appropriate canonical modifier.\n *\n * The 'Mod' token represents the \"primary modifier\" on each platform:\n * - macOS: 'Meta' (Command key ⌘)\n * - Windows/Linux: 'Control' (Ctrl key)\n *\n * This enables cross-platform hotkey definitions like 'Mod+S' that automatically\n * map to Command+S on Mac and Ctrl+S on Windows/Linux.\n *\n * @param modifier - The modifier to resolve. If 'Mod', resolves based on platform.\n * @param platform - The target platform. Defaults to auto-detection.\n * @returns The canonical modifier name ('Control', 'Shift', 'Alt', or 'Meta')\n *\n * @example\n * ```ts\n * resolveModifier('Mod', 'mac') // 'Meta'\n * resolveModifier('Mod', 'windows') // 'Control'\n * resolveModifier('Control', 'mac') // 'Control' (unchanged)\n * ```\n */\nexport function resolveModifier(\n modifier: CanonicalModifier | 'Mod',\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): CanonicalModifier {\n if (modifier === 'Mod') {\n return platform === 'mac' ? 'Meta' : 'Control'\n }\n return modifier\n}\n\n// =============================================================================\n// Valid Keys\n// =============================================================================\n\n/**\n * Set of all valid letter keys (A-Z).\n *\n * Used for validation and type checking. Letter keys are matched case-insensitively\n * in hotkey matching, but normalized to uppercase in canonical form.\n */\nexport const LETTER_KEYS = new Set<LetterKey>([\n 'A',\n 'B',\n 'C',\n 'D',\n 'E',\n 'F',\n 'G',\n 'H',\n 'I',\n 'J',\n 'K',\n 'L',\n 'M',\n 'N',\n 'O',\n 'P',\n 'Q',\n 'R',\n 'S',\n 'T',\n 'U',\n 'V',\n 'W',\n 'X',\n 'Y',\n 'Z',\n])\n\n/**\n * Set of all valid number keys (0-9).\n *\n * Note: Number keys are affected by Shift (Shift+1 → '!' on US layout),\n * so they're excluded from Shift-based hotkey combinations to avoid\n * layout-dependent behavior.\n */\nexport const NUMBER_KEYS = new Set<NumberKey>([\n '0',\n '1',\n '2',\n '3',\n '4',\n '5',\n '6',\n '7',\n '8',\n '9',\n])\n\n/**\n * Set of all valid function keys (F1-F12).\n *\n * Function keys are commonly used for system shortcuts (e.g., F12 for DevTools,\n * Alt+F4 to close windows) and application-specific commands.\n */\nexport const FUNCTION_KEYS = new Set<FunctionKey>([\n 'F1',\n 'F2',\n 'F3',\n 'F4',\n 'F5',\n 'F6',\n 'F7',\n 'F8',\n 'F9',\n 'F10',\n 'F11',\n 'F12',\n])\n\n/**\n * Set of all valid navigation keys for cursor movement and document navigation.\n *\n * Includes arrow keys, Home/End (line navigation), and PageUp/PageDown (page navigation).\n * These keys are commonly combined with modifiers for selection (Shift+ArrowUp) or\n * navigation shortcuts (Alt+ArrowLeft for back).\n */\nexport const NAVIGATION_KEYS = new Set<NavigationKey>([\n 'ArrowUp',\n 'ArrowDown',\n 'ArrowLeft',\n 'ArrowRight',\n 'Home',\n 'End',\n 'PageUp',\n 'PageDown',\n])\n\n/**\n * Set of all valid editing and special keys.\n *\n * Includes keys commonly used for text editing (Enter, Backspace, Delete, Tab) and\n * special actions (Escape, Space). These keys are frequently combined with modifiers\n * for editor shortcuts (Mod+Enter to submit, Shift+Tab to go back).\n */\nexport const EDITING_KEYS = new Set<EditingKey>([\n 'Enter',\n 'Escape',\n 'Space',\n 'Tab',\n 'Backspace',\n 'Delete',\n])\n\n/**\n * Set of all valid punctuation keys commonly used in keyboard shortcuts.\n *\n * These are the literal characters as they appear in `KeyboardEvent.key` (layout-dependent,\n * typically US keyboard layout). Common shortcuts include:\n * - `Mod+/` - Toggle comment\n * - `Mod+[` / `Mod+]` - Indent/outdent\n * - `Mod+=` / `Mod+-` - Zoom in/out\n *\n * Note: Punctuation keys are affected by Shift (Shift+',' → '<' on US layout),\n * so they're excluded from Shift-based hotkey combinations to avoid layout-dependent behavior.\n */\nexport const PUNCTUATION_KEYS = new Set<PunctuationKey>([\n '/',\n '[',\n ']',\n '\\\\',\n '=',\n '-',\n ',',\n '.',\n '`',\n])\n\n/**\n * Set of all valid non-modifier keys.\n *\n * This is the union of all key type sets (letters, numbers, function keys, navigation,\n * editing, and punctuation). Used primarily for validation to check if a key string\n * is recognized and will have type-safe autocomplete support.\n *\n * @see {@link LETTER_KEYS}\n * @see {@link NUMBER_KEYS}\n * @see {@link FUNCTION_KEYS}\n * @see {@link NAVIGATION_KEYS}\n * @see {@link EDITING_KEYS}\n * @see {@link PUNCTUATION_KEYS}\n */\nexport const ALL_KEYS = new Set([\n ...LETTER_KEYS,\n ...NUMBER_KEYS,\n ...FUNCTION_KEYS,\n ...NAVIGATION_KEYS,\n ...EDITING_KEYS,\n ...PUNCTUATION_KEYS,\n])\n\n/**\n * Maps key name aliases to their canonical form.\n *\n * Handles common variations and alternative names for keys to provide a more\n * forgiving API. For example, users can write 'Esc', 'esc', or 'escape' and\n * they'll all normalize to 'Escape'.\n *\n * This map is used internally by `normalizeKeyName()` to convert user input\n * into the canonical key names used throughout the library.\n *\n * @remarks Case-insensitive lookups are supported via lowercase variants\n *\n * @example\n * ```ts\n * KEY_ALIASES['Esc'] // 'Escape'\n * KEY_ALIASES['Del'] // 'Delete'\n * KEY_ALIASES['Up'] // 'ArrowUp'\n * ```\n */\nconst KEY_ALIASES: Record<string, string> = {\n // Escape variants\n Esc: 'Escape',\n esc: 'Escape',\n escape: 'Escape',\n\n // Enter variants\n Return: 'Enter',\n return: 'Enter',\n enter: 'Enter',\n\n // Space variants\n ' ': 'Space',\n space: 'Space',\n Spacebar: 'Space',\n spacebar: 'Space',\n\n // Tab variants\n tab: 'Tab',\n\n // Backspace variants\n backspace: 'Backspace',\n\n // Delete variants\n Del: 'Delete',\n del: 'Delete',\n delete: 'Delete',\n\n // Arrow key variants\n Up: 'ArrowUp',\n up: 'ArrowUp',\n arrowup: 'ArrowUp',\n Down: 'ArrowDown',\n down: 'ArrowDown',\n arrowdown: 'ArrowDown',\n Left: 'ArrowLeft',\n left: 'ArrowLeft',\n arrowleft: 'ArrowLeft',\n Right: 'ArrowRight',\n right: 'ArrowRight',\n arrowright: 'ArrowRight',\n\n // Navigation variants\n home: 'Home',\n end: 'End',\n pageup: 'PageUp',\n pagedown: 'PageDown',\n PgUp: 'PageUp',\n PgDn: 'PageDown',\n pgup: 'PageUp',\n pgdn: 'PageDown',\n}\n\n/**\n * Normalizes a key name to its canonical form.\n *\n * Converts various key name formats (aliases, case variations) into the standard\n * canonical names used throughout the library. This enables a more forgiving API\n * where users can write keys in different ways and still get correct behavior.\n *\n * Normalization rules:\n * 1. Check aliases first (e.g., 'Esc' → 'Escape', 'Del' → 'Delete')\n * 2. Single letters → uppercase (e.g., 'a' → 'A', 's' → 'S')\n * 3. Function keys → uppercase (e.g., 'f1' → 'F1', 'F12' → 'F12')\n * 4. Other keys → returned as-is (already canonical or unknown)\n *\n * @param key - The key name to normalize (can be an alias, lowercase, etc.)\n * @returns The canonical key name\n *\n * @example\n * ```ts\n * normalizeKeyName('esc') // 'Escape'\n * normalizeKeyName('a') // 'A'\n * normalizeKeyName('f1') // 'F1'\n * normalizeKeyName('ArrowUp') // 'ArrowUp' (already canonical)\n * ```\n */\nexport function normalizeKeyName(key: string): string {\n // Check aliases first\n if (key in KEY_ALIASES) {\n return KEY_ALIASES[key]!\n }\n\n // Check if it's a single letter (normalize to uppercase)\n if (key.length === 1 && /^[a-zA-Z]$/.test(key)) {\n return key.toUpperCase()\n }\n\n // Check if it's a function key (normalize case)\n const upperKey = key.toUpperCase()\n if (/^F([1-9]|1[0-2])$/.test(upperKey)) {\n return upperKey\n }\n\n return key\n}\n\n// =============================================================================\n// Display Symbols\n// =============================================================================\n\n/**\n * Modifier key symbols for macOS display.\n *\n * Used by formatting functions to display hotkeys with macOS-style symbols\n * (e.g., ⌘ for Command, ⌃ for Control) instead of text labels. This provides\n * a native macOS look and feel in hotkey displays.\n *\n * @example\n * ```ts\n * MAC_MODIFIER_SYMBOLS['Meta'] // '⌘'\n * MAC_MODIFIER_SYMBOLS['Control'] // '⌃'\n * MAC_MODIFIER_SYMBOLS['Alt'] // '⌥'\n * MAC_MODIFIER_SYMBOLS['Shift'] // '⇧'\n * ```\n */\nexport const MAC_MODIFIER_SYMBOLS: Record<CanonicalModifier, string> = {\n Control: '⌃',\n Alt: '⌥',\n Shift: '⇧',\n Meta: '⌘',\n}\n\n/**\n * Modifier key labels for Windows/Linux display.\n *\n * Used by formatting functions to display hotkeys with standard text labels\n * (e.g., 'Ctrl' for Control, 'Win' for Meta/Windows key) instead of symbols.\n * This provides a familiar Windows/Linux look and feel in hotkey displays.\n *\n * @example\n * ```ts\n * STANDARD_MODIFIER_LABELS['Control'] // 'Ctrl'\n * STANDARD_MODIFIER_LABELS['Meta'] // 'Win'\n * STANDARD_MODIFIER_LABELS['Alt'] // 'Alt'\n * STANDARD_MODIFIER_LABELS['Shift'] // 'Shift'\n * ```\n */\nexport const STANDARD_MODIFIER_LABELS: Record<CanonicalModifier, string> = {\n Control: 'Ctrl',\n Alt: 'Alt',\n Shift: 'Shift',\n Meta: 'Win',\n}\n\n/**\n * Special key symbols for display formatting.\n *\n * Maps certain keys to their visual symbols for better readability in hotkey displays.\n * Used by formatting functions to show symbols like ↑ for ArrowUp or ↵ for Enter\n * instead of text labels.\n *\n * @example\n * ```ts\n * KEY_DISPLAY_SYMBOLS['ArrowUp'] // '↑'\n * KEY_DISPLAY_SYMBOLS['Enter'] // '↵'\n * KEY_DISPLAY_SYMBOLS['Escape'] // 'Esc'\n * KEY_DISPLAY_SYMBOLS['Space'] // '␣'\n * ```\n */\nexport const KEY_DISPLAY_SYMBOLS: Record<string, string> = {\n ArrowUp: '↑',\n ArrowDown: '↓',\n ArrowLeft: '←',\n ArrowRight: '→',\n Enter: '↵',\n Escape: 'Esc',\n Backspace: '⌫',\n Delete: '⌦',\n Tab: '⇥',\n Space: '␣',\n}\n"],"mappings":"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"}
|
package/dist/format.cjs
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
const require_constants = require('./constants.cjs');
|
|
2
|
+
const require_parse = require('./parse.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/format.ts
|
|
5
|
+
/**
|
|
6
|
+
* Converts a ParsedHotkey back to a hotkey string.
|
|
7
|
+
*
|
|
8
|
+
* @param parsed - The parsed hotkey object
|
|
9
|
+
* @returns A hotkey string in canonical form
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* formatHotkey({ key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] })
|
|
14
|
+
* // Returns: 'Control+Shift+S'
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function formatHotkey(parsed) {
|
|
18
|
+
const parts = [];
|
|
19
|
+
for (const modifier of require_constants.MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
|
|
20
|
+
parts.push(parsed.key);
|
|
21
|
+
return parts.join("+");
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Formats a hotkey for display in a user interface.
|
|
25
|
+
*
|
|
26
|
+
* On macOS, uses symbols (⌘⇧S).
|
|
27
|
+
* On Windows/Linux, uses text (Ctrl+Shift+S).
|
|
28
|
+
*
|
|
29
|
+
* @param hotkey - The hotkey string or ParsedHotkey to format
|
|
30
|
+
* @param options - Formatting options
|
|
31
|
+
* @returns A formatted string suitable for display
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* formatForDisplay('Mod+Shift+S', { platform: 'mac' })
|
|
36
|
+
* // Returns: '⇧⌘S'
|
|
37
|
+
*
|
|
38
|
+
* formatForDisplay('Mod+Shift+S', { platform: 'windows' })
|
|
39
|
+
* // Returns: 'Ctrl+Shift+S'
|
|
40
|
+
*
|
|
41
|
+
* formatForDisplay('Escape')
|
|
42
|
+
* // Returns: 'Esc' (on all platforms)
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
function formatForDisplay(hotkey, options = {}) {
|
|
46
|
+
const platform = options.platform ?? require_constants.detectPlatform();
|
|
47
|
+
const parsed = typeof hotkey === "string" ? require_parse.parseHotkey(hotkey, platform) : hotkey;
|
|
48
|
+
if (platform === "mac") return formatForMac(parsed);
|
|
49
|
+
return formatForStandard(parsed);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Formats a hotkey for macOS display using symbols.
|
|
53
|
+
*/
|
|
54
|
+
function formatForMac(parsed) {
|
|
55
|
+
const parts = [];
|
|
56
|
+
for (const modifier of require_constants.MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(require_constants.MAC_MODIFIER_SYMBOLS[modifier]);
|
|
57
|
+
const keyDisplay = require_constants.KEY_DISPLAY_SYMBOLS[parsed.key] ?? parsed.key;
|
|
58
|
+
parts.push(keyDisplay);
|
|
59
|
+
return parts.join("");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Formats a hotkey for Windows/Linux display using text labels.
|
|
63
|
+
*/
|
|
64
|
+
function formatForStandard(parsed) {
|
|
65
|
+
const parts = [];
|
|
66
|
+
for (const modifier of require_constants.MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(require_constants.STANDARD_MODIFIER_LABELS[modifier]);
|
|
67
|
+
const keyDisplay = require_constants.KEY_DISPLAY_SYMBOLS[parsed.key] ?? parsed.key;
|
|
68
|
+
parts.push(keyDisplay);
|
|
69
|
+
return parts.join("+");
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Formats a hotkey using platform-agnostic labels.
|
|
73
|
+
* Uses 'Cmd' on Mac and 'Ctrl' for Control, etc.
|
|
74
|
+
*
|
|
75
|
+
* @param hotkey - The hotkey string or ParsedHotkey to format
|
|
76
|
+
* @param platform - The target platform
|
|
77
|
+
* @returns A formatted string with platform-appropriate labels
|
|
78
|
+
*/
|
|
79
|
+
function formatWithLabels(hotkey, platform = require_constants.detectPlatform()) {
|
|
80
|
+
const parsed = typeof hotkey === "string" ? require_parse.parseHotkey(hotkey, platform) : hotkey;
|
|
81
|
+
const parts = [];
|
|
82
|
+
const labels = {
|
|
83
|
+
Control: "Ctrl",
|
|
84
|
+
Alt: platform === "mac" ? "Option" : "Alt",
|
|
85
|
+
Shift: "Shift",
|
|
86
|
+
Meta: platform === "mac" ? "Cmd" : "Win"
|
|
87
|
+
};
|
|
88
|
+
for (const modifier of require_constants.MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(labels[modifier] ?? modifier);
|
|
89
|
+
parts.push(parsed.key);
|
|
90
|
+
return parts.join("+");
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Maps canonical modifier names to debugging-friendly labels per platform.
|
|
94
|
+
*/
|
|
95
|
+
const MODIFIER_DEBUG_LABELS = {
|
|
96
|
+
mac: {
|
|
97
|
+
Meta: "Mod (Cmd)",
|
|
98
|
+
Control: "Ctrl",
|
|
99
|
+
Alt: "Opt",
|
|
100
|
+
Shift: "Shift"
|
|
101
|
+
},
|
|
102
|
+
windows: {
|
|
103
|
+
Control: "Mod (Ctrl)",
|
|
104
|
+
Meta: "Win",
|
|
105
|
+
Alt: "Alt",
|
|
106
|
+
Shift: "Shift"
|
|
107
|
+
},
|
|
108
|
+
linux: {
|
|
109
|
+
Control: "Mod (Ctrl)",
|
|
110
|
+
Meta: "Super",
|
|
111
|
+
Alt: "Alt",
|
|
112
|
+
Shift: "Shift"
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Formats a single key name for debugging/devtools display.
|
|
117
|
+
*
|
|
118
|
+
* Unlike `formatForDisplay` which formats full hotkey strings for end-user UIs,
|
|
119
|
+
* this function formats individual key names (from `event.key`) with rich
|
|
120
|
+
* platform-aware labels suitable for debugging tools and developer-facing displays.
|
|
121
|
+
*
|
|
122
|
+
* Features:
|
|
123
|
+
* - Modifier keys show their platform role (e.g., "Mod (Cmd)" for Meta on Mac)
|
|
124
|
+
* - On macOS, modifier keys are prefixed with their symbol (e.g., "⌘ Mod (Cmd)")
|
|
125
|
+
* - Special keys use display symbols (ArrowUp -> "↑", Escape -> "Esc")
|
|
126
|
+
* - Regular keys pass through unchanged
|
|
127
|
+
*
|
|
128
|
+
* @param key - A single key name (e.g., "Meta", "Shift", "ArrowUp", "A")
|
|
129
|
+
* @param options - Formatting options
|
|
130
|
+
* @returns A formatted label suitable for debugging display
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* // On macOS:
|
|
135
|
+
* formatKeyForDebuggingDisplay('Meta') // '⌘ Mod (Cmd)'
|
|
136
|
+
* formatKeyForDebuggingDisplay('Control') // '⌃ Ctrl'
|
|
137
|
+
* formatKeyForDebuggingDisplay('Alt') // '⌥ Opt'
|
|
138
|
+
* formatKeyForDebuggingDisplay('Shift') // '⇧ Shift'
|
|
139
|
+
*
|
|
140
|
+
* // On Windows:
|
|
141
|
+
* formatKeyForDebuggingDisplay('Control') // 'Mod (Ctrl)'
|
|
142
|
+
* formatKeyForDebuggingDisplay('Meta') // 'Win'
|
|
143
|
+
*
|
|
144
|
+
* // Special keys (all platforms):
|
|
145
|
+
* formatKeyForDebuggingDisplay('ArrowUp') // '↑'
|
|
146
|
+
* formatKeyForDebuggingDisplay('Escape') // 'Esc'
|
|
147
|
+
* formatKeyForDebuggingDisplay('Space') // '␣'
|
|
148
|
+
*
|
|
149
|
+
* // Regular keys pass through:
|
|
150
|
+
* formatKeyForDebuggingDisplay('A') // 'A'
|
|
151
|
+
*
|
|
152
|
+
* // With source: 'code', values pass through unchanged:
|
|
153
|
+
* formatKeyForDebuggingDisplay('MetaLeft', { source: 'code' }) // 'MetaLeft'
|
|
154
|
+
* formatKeyForDebuggingDisplay('KeyA', { source: 'code' }) // 'KeyA'
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
function formatKeyForDebuggingDisplay(key, options = {}) {
|
|
158
|
+
if (options.source === "code") return key;
|
|
159
|
+
const platform = options.platform ?? require_constants.detectPlatform();
|
|
160
|
+
const modLabel = MODIFIER_DEBUG_LABELS[platform]?.[key];
|
|
161
|
+
if (modLabel) {
|
|
162
|
+
if (platform === "mac") {
|
|
163
|
+
const symbol = require_constants.MAC_MODIFIER_SYMBOLS[key];
|
|
164
|
+
if (symbol) return `${symbol} ${modLabel}`;
|
|
165
|
+
}
|
|
166
|
+
return modLabel;
|
|
167
|
+
}
|
|
168
|
+
const symbol = require_constants.KEY_DISPLAY_SYMBOLS[key];
|
|
169
|
+
if (symbol) return symbol;
|
|
170
|
+
return key;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
//#endregion
|
|
174
|
+
exports.formatForDisplay = formatForDisplay;
|
|
175
|
+
exports.formatHotkey = formatHotkey;
|
|
176
|
+
exports.formatKeyForDebuggingDisplay = formatKeyForDebuggingDisplay;
|
|
177
|
+
exports.formatWithLabels = formatWithLabels;
|
|
178
|
+
//# sourceMappingURL=format.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.cjs","names":["MODIFIER_ORDER","detectPlatform","parseHotkey","MAC_MODIFIER_SYMBOLS","KEY_DISPLAY_SYMBOLS","STANDARD_MODIFIER_LABELS"],"sources":["../src/format.ts"],"sourcesContent":["import {\n KEY_DISPLAY_SYMBOLS,\n MAC_MODIFIER_SYMBOLS,\n MODIFIER_ORDER,\n STANDARD_MODIFIER_LABELS,\n detectPlatform,\n} from './constants'\nimport { parseHotkey } from './parse'\nimport type { FormatDisplayOptions, Hotkey, ParsedHotkey } from './hotkey'\n\n/**\n * Converts a ParsedHotkey back to a hotkey string.\n *\n * @param parsed - The parsed hotkey object\n * @returns A hotkey string in canonical form\n *\n * @example\n * ```ts\n * formatHotkey({ key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] })\n * // Returns: 'Control+Shift+S'\n * ```\n */\nexport function formatHotkey(parsed: ParsedHotkey): string {\n const parts: Array<string> = []\n\n // Add modifiers in canonical order\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n\n // Add the key\n parts.push(parsed.key)\n\n return parts.join('+')\n}\n\n/**\n * Formats a hotkey for display in a user interface.\n *\n * On macOS, uses symbols (⌘⇧S).\n * On Windows/Linux, uses text (Ctrl+Shift+S).\n *\n * @param hotkey - The hotkey string or ParsedHotkey to format\n * @param options - Formatting options\n * @returns A formatted string suitable for display\n *\n * @example\n * ```ts\n * formatForDisplay('Mod+Shift+S', { platform: 'mac' })\n * // Returns: '⇧⌘S'\n *\n * formatForDisplay('Mod+Shift+S', { platform: 'windows' })\n * // Returns: 'Ctrl+Shift+S'\n *\n * formatForDisplay('Escape')\n * // Returns: 'Esc' (on all platforms)\n * ```\n */\nexport function formatForDisplay(\n hotkey: Hotkey | (string & {}) | ParsedHotkey,\n options: FormatDisplayOptions = {},\n): string {\n const platform = options.platform ?? detectPlatform()\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n if (platform === 'mac') {\n return formatForMac(parsed)\n }\n\n return formatForStandard(parsed)\n}\n\n/**\n * Formats a hotkey for macOS display using symbols.\n */\nfunction formatForMac(parsed: ParsedHotkey): string {\n const parts: Array<string> = []\n\n // Add modifiers in macOS order (typically Control, Option, Shift, Command)\n // But we'll use our canonical order and just use symbols\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(MAC_MODIFIER_SYMBOLS[modifier])\n }\n }\n\n // Add the key (use symbol if available, otherwise the key itself)\n const keyDisplay = KEY_DISPLAY_SYMBOLS[parsed.key] ?? parsed.key\n parts.push(keyDisplay)\n\n // On Mac, modifiers are typically concatenated without separators\n return parts.join('')\n}\n\n/**\n * Formats a hotkey for Windows/Linux display using text labels.\n */\nfunction formatForStandard(parsed: ParsedHotkey): string {\n const parts: Array<string> = []\n\n // Add modifiers in canonical order\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(STANDARD_MODIFIER_LABELS[modifier])\n }\n }\n\n // Add the key (use symbol/short form if available)\n const keyDisplay = KEY_DISPLAY_SYMBOLS[parsed.key] ?? parsed.key\n parts.push(keyDisplay)\n\n // On Windows/Linux, use + as separator\n return parts.join('+')\n}\n\n/**\n * Formats a hotkey using platform-agnostic labels.\n * Uses 'Cmd' on Mac and 'Ctrl' for Control, etc.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to format\n * @param platform - The target platform\n * @returns A formatted string with platform-appropriate labels\n */\nexport function formatWithLabels(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): string {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n const parts: Array<string> = []\n\n // Custom labels for more readable output\n const labels: Record<string, string> = {\n Control: 'Ctrl',\n Alt: platform === 'mac' ? 'Option' : 'Alt',\n Shift: 'Shift',\n Meta: platform === 'mac' ? 'Cmd' : 'Win',\n }\n\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(labels[modifier] ?? modifier)\n }\n }\n\n // Add the key\n parts.push(parsed.key)\n\n return parts.join('+')\n}\n\n// =============================================================================\n// Debugging Display Labels\n// =============================================================================\n\n/**\n * Maps canonical modifier names to debugging-friendly labels per platform.\n */\nconst MODIFIER_DEBUG_LABELS: Record<string, Record<string, string>> = {\n mac: { Meta: 'Mod (Cmd)', Control: 'Ctrl', Alt: 'Opt', Shift: 'Shift' },\n windows: { Control: 'Mod (Ctrl)', Meta: 'Win', Alt: 'Alt', Shift: 'Shift' },\n linux: { Control: 'Mod (Ctrl)', Meta: 'Super', Alt: 'Alt', Shift: 'Shift' },\n}\n\n/**\n * Options for formatting a single key for debugging display.\n */\nexport interface FormatKeyDebuggingOptions {\n /** The target platform. Defaults to auto-detection. */\n platform?: 'mac' | 'windows' | 'linux'\n /**\n * Whether the input value comes from `event.key` or `event.code`.\n *\n * - `'key'` (default): Applies rich platform-aware formatting (modifier\n * labels, special-key symbols, etc.).\n * - `'code'`: Returns the value unchanged — physical key codes like\n * `\"MetaLeft\"` or `\"KeyA\"` are already descriptive for debugging.\n */\n source?: 'key' | 'code'\n}\n\n/**\n * Formats a single key name for debugging/devtools display.\n *\n * Unlike `formatForDisplay` which formats full hotkey strings for end-user UIs,\n * this function formats individual key names (from `event.key`) with rich\n * platform-aware labels suitable for debugging tools and developer-facing displays.\n *\n * Features:\n * - Modifier keys show their platform role (e.g., \"Mod (Cmd)\" for Meta on Mac)\n * - On macOS, modifier keys are prefixed with their symbol (e.g., \"⌘ Mod (Cmd)\")\n * - Special keys use display symbols (ArrowUp -> \"↑\", Escape -> \"Esc\")\n * - Regular keys pass through unchanged\n *\n * @param key - A single key name (e.g., \"Meta\", \"Shift\", \"ArrowUp\", \"A\")\n * @param options - Formatting options\n * @returns A formatted label suitable for debugging display\n *\n * @example\n * ```ts\n * // On macOS:\n * formatKeyForDebuggingDisplay('Meta') // '⌘ Mod (Cmd)'\n * formatKeyForDebuggingDisplay('Control') // '⌃ Ctrl'\n * formatKeyForDebuggingDisplay('Alt') // '⌥ Opt'\n * formatKeyForDebuggingDisplay('Shift') // '⇧ Shift'\n *\n * // On Windows:\n * formatKeyForDebuggingDisplay('Control') // 'Mod (Ctrl)'\n * formatKeyForDebuggingDisplay('Meta') // 'Win'\n *\n * // Special keys (all platforms):\n * formatKeyForDebuggingDisplay('ArrowUp') // '↑'\n * formatKeyForDebuggingDisplay('Escape') // 'Esc'\n * formatKeyForDebuggingDisplay('Space') // '␣'\n *\n * // Regular keys pass through:\n * formatKeyForDebuggingDisplay('A') // 'A'\n *\n * // With source: 'code', values pass through unchanged:\n * formatKeyForDebuggingDisplay('MetaLeft', { source: 'code' }) // 'MetaLeft'\n * formatKeyForDebuggingDisplay('KeyA', { source: 'code' }) // 'KeyA'\n * ```\n */\nexport function formatKeyForDebuggingDisplay(\n key: string,\n options: FormatKeyDebuggingOptions = {},\n): string {\n // For event.code values, pass through unchanged — they're already\n // descriptive for debugging (e.g. \"MetaLeft\", \"KeyA\", \"ShiftRight\").\n if (options.source === 'code') {\n return key\n }\n\n const platform = options.platform ?? detectPlatform()\n\n // Check if it's a modifier key\n const modLabel = MODIFIER_DEBUG_LABELS[platform]?.[key]\n if (modLabel) {\n // On Mac, prefix modifier labels with their symbol\n if (platform === 'mac') {\n const symbol =\n MAC_MODIFIER_SYMBOLS[key as keyof typeof MAC_MODIFIER_SYMBOLS]\n if (symbol) {\n return `${symbol} ${modLabel}`\n }\n }\n return modLabel\n }\n\n // Check if it's a special key with a display symbol\n const symbol = KEY_DISPLAY_SYMBOLS[key]\n if (symbol) {\n return symbol\n }\n\n // Regular key — pass through\n return key\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAsBA,SAAgB,aAAa,QAA8B;CACzD,MAAM,QAAuB,EAAE;AAG/B,MAAK,MAAM,YAAYA,iCACrB,KAAI,OAAO,UAAU,SAAS,SAAS,CACrC,OAAM,KAAK,SAAS;AAKxB,OAAM,KAAK,OAAO,IAAI;AAEtB,QAAO,MAAM,KAAK,IAAI;;;;;;;;;;;;;;;;;;;;;;;;AAyBxB,SAAgB,iBACd,QACA,UAAgC,EAAE,EAC1B;CACR,MAAM,WAAW,QAAQ,YAAYC,kCAAgB;CACrD,MAAM,SACJ,OAAO,WAAW,WAAWC,0BAAY,QAAQ,SAAS,GAAG;AAE/D,KAAI,aAAa,MACf,QAAO,aAAa,OAAO;AAG7B,QAAO,kBAAkB,OAAO;;;;;AAMlC,SAAS,aAAa,QAA8B;CAClD,MAAM,QAAuB,EAAE;AAI/B,MAAK,MAAM,YAAYF,iCACrB,KAAI,OAAO,UAAU,SAAS,SAAS,CACrC,OAAM,KAAKG,uCAAqB,UAAU;CAK9C,MAAM,aAAaC,sCAAoB,OAAO,QAAQ,OAAO;AAC7D,OAAM,KAAK,WAAW;AAGtB,QAAO,MAAM,KAAK,GAAG;;;;;AAMvB,SAAS,kBAAkB,QAA8B;CACvD,MAAM,QAAuB,EAAE;AAG/B,MAAK,MAAM,YAAYJ,iCACrB,KAAI,OAAO,UAAU,SAAS,SAAS,CACrC,OAAM,KAAKK,2CAAyB,UAAU;CAKlD,MAAM,aAAaD,sCAAoB,OAAO,QAAQ,OAAO;AAC7D,OAAM,KAAK,WAAW;AAGtB,QAAO,MAAM,KAAK,IAAI;;;;;;;;;;AAWxB,SAAgB,iBACd,QACA,WAAwCH,kCAAgB,EAChD;CACR,MAAM,SACJ,OAAO,WAAW,WAAWC,0BAAY,QAAQ,SAAS,GAAG;CAC/D,MAAM,QAAuB,EAAE;CAG/B,MAAM,SAAiC;EACrC,SAAS;EACT,KAAK,aAAa,QAAQ,WAAW;EACrC,OAAO;EACP,MAAM,aAAa,QAAQ,QAAQ;EACpC;AAED,MAAK,MAAM,YAAYF,iCACrB,KAAI,OAAO,UAAU,SAAS,SAAS,CACrC,OAAM,KAAK,OAAO,aAAa,SAAS;AAK5C,OAAM,KAAK,OAAO,IAAI;AAEtB,QAAO,MAAM,KAAK,IAAI;;;;;AAUxB,MAAM,wBAAgE;CACpE,KAAK;EAAE,MAAM;EAAa,SAAS;EAAQ,KAAK;EAAO,OAAO;EAAS;CACvE,SAAS;EAAE,SAAS;EAAc,MAAM;EAAO,KAAK;EAAO,OAAO;EAAS;CAC3E,OAAO;EAAE,SAAS;EAAc,MAAM;EAAS,KAAK;EAAO,OAAO;EAAS;CAC5E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6DD,SAAgB,6BACd,KACA,UAAqC,EAAE,EAC/B;AAGR,KAAI,QAAQ,WAAW,OACrB,QAAO;CAGT,MAAM,WAAW,QAAQ,YAAYC,kCAAgB;CAGrD,MAAM,WAAW,sBAAsB,YAAY;AACnD,KAAI,UAAU;AAEZ,MAAI,aAAa,OAAO;GACtB,MAAM,SACJE,uCAAqB;AACvB,OAAI,OACF,QAAO,GAAG,OAAO,GAAG;;AAGxB,SAAO;;CAIT,MAAM,SAASC,sCAAoB;AACnC,KAAI,OACF,QAAO;AAIT,QAAO"}
|