@tanstack/hotkeys 0.4.3 → 0.6.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/README.md +1 -1
- package/dist/constants.cjs +70 -10
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +57 -4
- package/dist/constants.d.ts +57 -4
- package/dist/constants.js +67 -10
- package/dist/constants.js.map +1 -1
- package/dist/format.cjs +20 -125
- package/dist/format.cjs.map +1 -1
- package/dist/format.d.cts +8 -72
- package/dist/format.d.ts +8 -72
- package/dist/format.js +23 -127
- package/dist/format.js.map +1 -1
- package/dist/hotkey-manager.cjs.map +1 -1
- package/dist/hotkey-manager.d.cts +5 -1
- package/dist/hotkey-manager.d.ts +5 -1
- package/dist/hotkey-manager.js.map +1 -1
- package/dist/{recorder.cjs → hotkey-recorder.cjs} +14 -16
- package/dist/hotkey-recorder.cjs.map +1 -0
- package/dist/{recorder.d.cts → hotkey-recorder.d.cts} +2 -2
- package/dist/{recorder.d.ts → hotkey-recorder.d.ts} +2 -2
- package/dist/{recorder.js → hotkey-recorder.js} +14 -16
- package/dist/hotkey-recorder.js.map +1 -0
- package/dist/hotkey-sequence-recorder.cjs +188 -0
- package/dist/hotkey-sequence-recorder.cjs.map +1 -0
- package/dist/hotkey-sequence-recorder.d.cts +71 -0
- package/dist/hotkey-sequence-recorder.d.ts +71 -0
- package/dist/hotkey-sequence-recorder.js +188 -0
- package/dist/hotkey-sequence-recorder.js.map +1 -0
- package/dist/hotkey.d.cts +2 -0
- package/dist/hotkey.d.ts +2 -0
- package/dist/index.cjs +12 -7
- package/dist/index.d.cts +7 -6
- package/dist/index.d.ts +7 -6
- package/dist/index.js +7 -6
- package/dist/parse.cjs +53 -91
- package/dist/parse.cjs.map +1 -1
- package/dist/parse.d.cts +31 -72
- package/dist/parse.d.ts +31 -72
- package/dist/parse.js +51 -89
- package/dist/parse.js.map +1 -1
- package/dist/recorder-chord.cjs +18 -0
- package/dist/recorder-chord.cjs.map +1 -0
- package/dist/recorder-chord.js +18 -0
- package/dist/recorder-chord.js.map +1 -0
- package/dist/sequence-manager.cjs +24 -8
- package/dist/sequence-manager.cjs.map +1 -1
- package/dist/sequence-manager.d.cts +11 -2
- package/dist/sequence-manager.d.ts +11 -2
- package/dist/sequence-manager.js +25 -10
- package/dist/sequence-manager.js.map +1 -1
- package/package.json +1 -1
- package/src/constants.ts +75 -21
- package/src/format.ts +58 -196
- package/src/hotkey-manager.ts +5 -1
- package/src/{recorder.ts → hotkey-recorder.ts} +18 -32
- package/src/hotkey-sequence-recorder.ts +308 -0
- package/src/hotkey.ts +2 -0
- package/src/index.ts +2 -1
- package/src/parse.ts +91 -131
- package/src/recorder-chord.ts +25 -0
- package/src/sequence-manager.ts +30 -9
- package/dist/recorder.cjs.map +0 -1
- package/dist/recorder.js.map +0 -1
package/dist/parse.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Hotkey, Key, ParsedHotkey, RawHotkey } from "./hotkey.js";
|
|
1
|
+
import { Hotkey, Key, ParsedHotkey, RawHotkey, RegisterableHotkey } from "./hotkey.js";
|
|
2
|
+
import { MODIFIER_ALIASES } from "./constants.js";
|
|
2
3
|
|
|
3
4
|
//#region src/parse.d.ts
|
|
4
5
|
/**
|
|
@@ -41,31 +42,43 @@ declare function rawHotkeyToParsedHotkey(raw: RawHotkey, platform?: 'mac' | 'win
|
|
|
41
42
|
/**
|
|
42
43
|
* Normalizes a hotkey string to its canonical form.
|
|
43
44
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* -
|
|
48
|
-
* -
|
|
45
|
+
* - When `Mod` is allowed for the platform (Command on Mac without Control;
|
|
46
|
+
* Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,
|
|
47
|
+
* then the key (e.g. `Mod+Shift+E`, `Mod+S`).
|
|
48
|
+
* - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.
|
|
49
|
+
* - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).
|
|
49
50
|
*
|
|
50
51
|
* @param hotkey - The hotkey string to normalize
|
|
51
|
-
* @param platform - The target platform for resolving
|
|
52
|
+
* @param platform - The target platform for resolving `Mod` (defaults to auto-detection)
|
|
52
53
|
* @returns The normalized hotkey string
|
|
53
54
|
*
|
|
54
55
|
* @example
|
|
55
56
|
* ```ts
|
|
56
|
-
* normalizeHotkey('
|
|
57
|
-
* normalizeHotkey('ctrl+a') // '
|
|
57
|
+
* normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'
|
|
58
|
+
* normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'
|
|
58
59
|
* normalizeHotkey('esc') // 'Escape'
|
|
59
60
|
* ```
|
|
60
61
|
*/
|
|
61
|
-
declare function normalizeHotkey(hotkey: Key | (string & {}), platform?: 'mac' | 'windows' | 'linux'):
|
|
62
|
+
declare function normalizeHotkey(hotkey: Key | (string & {}), platform?: 'mac' | 'windows' | 'linux'): Hotkey;
|
|
63
|
+
/**
|
|
64
|
+
* Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.
|
|
65
|
+
*/
|
|
66
|
+
declare function normalizeHotkeyFromParsed(parsed: ParsedHotkey, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
|
|
67
|
+
/**
|
|
68
|
+
* Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.
|
|
69
|
+
* Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.
|
|
70
|
+
*/
|
|
71
|
+
declare function normalizeRegisterableHotkey(hotkey: RegisterableHotkey, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
|
|
62
72
|
/**
|
|
63
|
-
* Checks if a string
|
|
73
|
+
* Checks if a string is a recognized modifier token (including aliases).
|
|
74
|
+
*
|
|
75
|
+
* For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM
|
|
76
|
+
* spellings like `OS` / `Win` match the same alias table.
|
|
64
77
|
*
|
|
65
|
-
* @param key -
|
|
78
|
+
* @param key - Key name or alias (e.g. from a hotkey string or `event.key`)
|
|
66
79
|
* @returns True if the string is a recognized modifier
|
|
67
80
|
*/
|
|
68
|
-
declare function
|
|
81
|
+
declare function isModifierKey(key: Key | (string & {})): key is keyof typeof MODIFIER_ALIASES;
|
|
69
82
|
/**
|
|
70
83
|
* Parses a KeyboardEvent into a ParsedHotkey object.
|
|
71
84
|
*
|
|
@@ -86,48 +99,12 @@ declare function isModifier(key: string): boolean;
|
|
|
86
99
|
*/
|
|
87
100
|
declare function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey;
|
|
88
101
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* This is a convenience function that combines `parseKeyboardEvent()` and formatting.
|
|
92
|
-
* The resulting hotkey string uses canonical modifier names (Control, Alt, Shift, Meta)
|
|
93
|
-
* and is suitable for use with `useHotkey()` and other hotkey functions.
|
|
94
|
-
*
|
|
95
|
-
* @param event - The KeyboardEvent to convert
|
|
96
|
-
* @param platform - The target platform (defaults to auto-detection)
|
|
97
|
-
* @returns A hotkey string in canonical form (e.g., 'Control+Shift+S')
|
|
102
|
+
* Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
|
|
98
103
|
*
|
|
99
|
-
* @
|
|
100
|
-
*
|
|
101
|
-
* document.addEventListener('keydown', (event) => {
|
|
102
|
-
* const hotkey = keyboardEventToHotkey(event)
|
|
103
|
-
* console.log(hotkey) // 'Control+Shift+S'
|
|
104
|
-
* useHotkey(hotkey, () => console.log('Shortcut triggered'))
|
|
105
|
-
* })
|
|
106
|
-
* ```
|
|
104
|
+
* @param event - The keyboard event (typically `keydown`)
|
|
105
|
+
* @param platform - Target platform for `Mod` eligibility
|
|
107
106
|
*/
|
|
108
|
-
declare function
|
|
109
|
-
/**
|
|
110
|
-
* Checks if a KeyboardEvent represents a modifier-only key press.
|
|
111
|
-
*
|
|
112
|
-
* Modifier-only keys are keys like 'Control', 'Shift', 'Alt', 'Meta', etc.
|
|
113
|
-
* that don't have an associated character or action key. This is useful
|
|
114
|
-
* for filtering out modifier key presses when recording shortcuts.
|
|
115
|
-
*
|
|
116
|
-
* @param event - The KeyboardEvent to check
|
|
117
|
-
* @returns True if the event represents a modifier-only key
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* ```ts
|
|
121
|
-
* document.addEventListener('keydown', (event) => {
|
|
122
|
-
* if (isModifierKey(event)) {
|
|
123
|
-
* console.log('Modifier key pressed, waiting for action key...')
|
|
124
|
-
* return
|
|
125
|
-
* }
|
|
126
|
-
* // Process non-modifier key
|
|
127
|
-
* })
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
declare function isModifierKey(event: KeyboardEvent): boolean;
|
|
107
|
+
declare function normalizeHotkeyFromEvent(event: KeyboardEvent, platform?: 'mac' | 'windows' | 'linux'): Hotkey;
|
|
131
108
|
/**
|
|
132
109
|
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
133
110
|
*
|
|
@@ -146,24 +123,6 @@ declare function isModifierKey(event: KeyboardEvent): boolean;
|
|
|
146
123
|
* ```
|
|
147
124
|
*/
|
|
148
125
|
declare function hasNonModifierKey(hotkey: Hotkey | ParsedHotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): boolean;
|
|
149
|
-
/**
|
|
150
|
-
* Converts a hotkey string to use 'Mod' format for portability.
|
|
151
|
-
*
|
|
152
|
-
* On macOS, converts 'Meta' to 'Mod'. On Windows/Linux, converts 'Control' to 'Mod'.
|
|
153
|
-
* This enables cross-platform hotkey definitions that work consistently.
|
|
154
|
-
*
|
|
155
|
-
* @param hotkey - The hotkey string to convert
|
|
156
|
-
* @param platform - The target platform (defaults to auto-detection)
|
|
157
|
-
* @returns The hotkey string with 'Mod' format applied
|
|
158
|
-
*
|
|
159
|
-
* @example
|
|
160
|
-
* ```ts
|
|
161
|
-
* convertToModFormat('Meta+S', 'mac') // 'Mod+S'
|
|
162
|
-
* convertToModFormat('Control+S', 'windows') // 'Mod+S'
|
|
163
|
-
* convertToModFormat('Control+Meta+S', 'mac') // 'Control+Meta+S' (both present, no conversion)
|
|
164
|
-
* ```
|
|
165
|
-
*/
|
|
166
|
-
declare function convertToModFormat(hotkey: Hotkey | (string & {}), platform?: 'mac' | 'windows' | 'linux'): Hotkey;
|
|
167
126
|
//#endregion
|
|
168
|
-
export {
|
|
127
|
+
export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
|
|
169
128
|
//# sourceMappingURL=parse.d.ts.map
|
package/dist/parse.js
CHANGED
|
@@ -87,39 +87,67 @@ function rawHotkeyToParsedHotkey(raw, platform = detectPlatform()) {
|
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
|
+
* Serializes a parsed hotkey to the canonical string form used for registration
|
|
91
|
+
* and storage: `Mod` first when the platform allows, then `Alt`, then `Shift`;
|
|
92
|
+
* otherwise full `Control+Alt+Shift+Meta` order.
|
|
93
|
+
*/
|
|
94
|
+
function normalizedHotkeyStringFromParsed(parsed, platform) {
|
|
95
|
+
const canUseMod = platform === "mac" ? parsed.meta && !parsed.ctrl : parsed.ctrl && !parsed.meta;
|
|
96
|
+
const parts = [];
|
|
97
|
+
if (canUseMod) {
|
|
98
|
+
parts.push("Mod");
|
|
99
|
+
if (parsed.alt) parts.push("Alt");
|
|
100
|
+
if (parsed.shift) parts.push("Shift");
|
|
101
|
+
} else for (const modifier of MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
|
|
102
|
+
parts.push(normalizeKeyName(parsed.key));
|
|
103
|
+
return parts.join("+");
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
90
106
|
* Normalizes a hotkey string to its canonical form.
|
|
91
107
|
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* -
|
|
96
|
-
* -
|
|
108
|
+
* - When `Mod` is allowed for the platform (Command on Mac without Control;
|
|
109
|
+
* Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,
|
|
110
|
+
* then the key (e.g. `Mod+Shift+E`, `Mod+S`).
|
|
111
|
+
* - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.
|
|
112
|
+
* - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).
|
|
97
113
|
*
|
|
98
114
|
* @param hotkey - The hotkey string to normalize
|
|
99
|
-
* @param platform - The target platform for resolving
|
|
115
|
+
* @param platform - The target platform for resolving `Mod` (defaults to auto-detection)
|
|
100
116
|
* @returns The normalized hotkey string
|
|
101
117
|
*
|
|
102
118
|
* @example
|
|
103
119
|
* ```ts
|
|
104
|
-
* normalizeHotkey('
|
|
105
|
-
* normalizeHotkey('ctrl+a') // '
|
|
120
|
+
* normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'
|
|
121
|
+
* normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'
|
|
106
122
|
* normalizeHotkey('esc') // 'Escape'
|
|
107
123
|
* ```
|
|
108
124
|
*/
|
|
109
125
|
function normalizeHotkey(hotkey, platform = detectPlatform()) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
126
|
+
return normalizedHotkeyStringFromParsed(parseHotkey(hotkey, platform), platform);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.
|
|
130
|
+
*/
|
|
131
|
+
function normalizeHotkeyFromParsed(parsed, platform = detectPlatform()) {
|
|
132
|
+
return normalizedHotkeyStringFromParsed(parsed, platform);
|
|
115
133
|
}
|
|
116
134
|
/**
|
|
117
|
-
*
|
|
135
|
+
* Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.
|
|
136
|
+
* Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.
|
|
137
|
+
*/
|
|
138
|
+
function normalizeRegisterableHotkey(hotkey, platform = detectPlatform()) {
|
|
139
|
+
return typeof hotkey === "string" ? normalizeHotkey(hotkey, platform) : normalizeHotkeyFromParsed(rawHotkeyToParsedHotkey(hotkey, platform), platform);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Checks if a string is a recognized modifier token (including aliases).
|
|
118
143
|
*
|
|
119
|
-
*
|
|
144
|
+
* For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM
|
|
145
|
+
* spellings like `OS` / `Win` match the same alias table.
|
|
146
|
+
*
|
|
147
|
+
* @param key - Key name or alias (e.g. from a hotkey string or `event.key`)
|
|
120
148
|
* @returns True if the string is a recognized modifier
|
|
121
149
|
*/
|
|
122
|
-
function
|
|
150
|
+
function isModifierKey(key) {
|
|
123
151
|
return key in MODIFIER_ALIASES || key.toLowerCase() in MODIFIER_ALIASES;
|
|
124
152
|
}
|
|
125
153
|
/**
|
|
@@ -157,56 +185,13 @@ function parseKeyboardEvent(event) {
|
|
|
157
185
|
};
|
|
158
186
|
}
|
|
159
187
|
/**
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
* This is a convenience function that combines `parseKeyboardEvent()` and formatting.
|
|
163
|
-
* The resulting hotkey string uses canonical modifier names (Control, Alt, Shift, Meta)
|
|
164
|
-
* and is suitable for use with `useHotkey()` and other hotkey functions.
|
|
165
|
-
*
|
|
166
|
-
* @param event - The KeyboardEvent to convert
|
|
167
|
-
* @param platform - The target platform (defaults to auto-detection)
|
|
168
|
-
* @returns A hotkey string in canonical form (e.g., 'Control+Shift+S')
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* ```ts
|
|
172
|
-
* document.addEventListener('keydown', (event) => {
|
|
173
|
-
* const hotkey = keyboardEventToHotkey(event)
|
|
174
|
-
* console.log(hotkey) // 'Control+Shift+S'
|
|
175
|
-
* useHotkey(hotkey, () => console.log('Shortcut triggered'))
|
|
176
|
-
* })
|
|
177
|
-
* ```
|
|
178
|
-
*/
|
|
179
|
-
function keyboardEventToHotkey(event) {
|
|
180
|
-
const parsed = parseKeyboardEvent(event);
|
|
181
|
-
const parts = [];
|
|
182
|
-
for (const modifier of MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
|
|
183
|
-
parts.push(parsed.key);
|
|
184
|
-
return parts.join("+");
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Checks if a KeyboardEvent represents a modifier-only key press.
|
|
188
|
+
* Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.
|
|
188
189
|
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* for filtering out modifier key presses when recording shortcuts.
|
|
192
|
-
*
|
|
193
|
-
* @param event - The KeyboardEvent to check
|
|
194
|
-
* @returns True if the event represents a modifier-only key
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* ```ts
|
|
198
|
-
* document.addEventListener('keydown', (event) => {
|
|
199
|
-
* if (isModifierKey(event)) {
|
|
200
|
-
* console.log('Modifier key pressed, waiting for action key...')
|
|
201
|
-
* return
|
|
202
|
-
* }
|
|
203
|
-
* // Process non-modifier key
|
|
204
|
-
* })
|
|
205
|
-
* ```
|
|
190
|
+
* @param event - The keyboard event (typically `keydown`)
|
|
191
|
+
* @param platform - Target platform for `Mod` eligibility
|
|
206
192
|
*/
|
|
207
|
-
function
|
|
208
|
-
|
|
209
|
-
return key === "Control" || key === "Shift" || key === "Alt" || key === "Meta" || key === "Command" || key === "OS" || key === "Win";
|
|
193
|
+
function normalizeHotkeyFromEvent(event, platform = detectPlatform()) {
|
|
194
|
+
return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform);
|
|
210
195
|
}
|
|
211
196
|
/**
|
|
212
197
|
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
@@ -227,32 +212,9 @@ function isModifierKey(event) {
|
|
|
227
212
|
*/
|
|
228
213
|
function hasNonModifierKey(hotkey, platform = detectPlatform()) {
|
|
229
214
|
const parsed = typeof hotkey === "string" ? parseHotkey(hotkey, platform) : hotkey;
|
|
230
|
-
return !
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* Converts a hotkey string to use 'Mod' format for portability.
|
|
234
|
-
*
|
|
235
|
-
* On macOS, converts 'Meta' to 'Mod'. On Windows/Linux, converts 'Control' to 'Mod'.
|
|
236
|
-
* This enables cross-platform hotkey definitions that work consistently.
|
|
237
|
-
*
|
|
238
|
-
* @param hotkey - The hotkey string to convert
|
|
239
|
-
* @param platform - The target platform (defaults to auto-detection)
|
|
240
|
-
* @returns The hotkey string with 'Mod' format applied
|
|
241
|
-
*
|
|
242
|
-
* @example
|
|
243
|
-
* ```ts
|
|
244
|
-
* convertToModFormat('Meta+S', 'mac') // 'Mod+S'
|
|
245
|
-
* convertToModFormat('Control+S', 'windows') // 'Mod+S'
|
|
246
|
-
* convertToModFormat('Control+Meta+S', 'mac') // 'Control+Meta+S' (both present, no conversion)
|
|
247
|
-
* ```
|
|
248
|
-
*/
|
|
249
|
-
function convertToModFormat(hotkey, platform = detectPlatform()) {
|
|
250
|
-
const parsed = parseHotkey(hotkey, platform);
|
|
251
|
-
if (platform === "mac" && parsed.meta && !parsed.ctrl) return hotkey.split("+").map((part) => part === "Meta" ? "Mod" : part).join("+");
|
|
252
|
-
else if (platform !== "mac" && parsed.ctrl && !parsed.meta) return hotkey.split("+").map((part) => part === "Control" ? "Mod" : part).join("+");
|
|
253
|
-
return hotkey;
|
|
215
|
+
return !isModifierKey(parsed.key) && parsed.key.length > 0;
|
|
254
216
|
}
|
|
255
217
|
|
|
256
218
|
//#endregion
|
|
257
|
-
export {
|
|
219
|
+
export { hasNonModifierKey, isModifierKey, normalizeHotkey, normalizeHotkeyFromEvent, normalizeHotkeyFromParsed, normalizeRegisterableHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
|
|
258
220
|
//# sourceMappingURL=parse.js.map
|
package/dist/parse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.js","names":[],"sources":["../src/parse.ts"],"sourcesContent":["import {\n MODIFIER_ALIASES,\n MODIFIER_ORDER,\n detectPlatform,\n normalizeKeyName,\n resolveModifier,\n} from './constants'\nimport type {\n CanonicalModifier,\n Hotkey,\n Key,\n ParsedHotkey,\n RawHotkey,\n} from './hotkey'\n\n/**\n * Parses a hotkey string into its component parts.\n *\n * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey object with the key and modifier flags\n *\n * @example\n * ```ts\n * parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n * parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }\n * parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function parseHotkey(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n const parts = hotkey.split('+')\n const modifiers: Set<CanonicalModifier> = new Set()\n let key = ''\n\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i]!.trim()\n\n if (i === parts.length - 1) {\n // Last part is always the key\n key = normalizeKeyName(part)\n } else {\n // All other parts are modifiers\n const alias =\n MODIFIER_ALIASES[part] ?? MODIFIER_ALIASES[part.toLowerCase()]\n\n if (alias) {\n const resolved = resolveModifier(alias, platform)\n modifiers.add(resolved)\n } else {\n // Unknown modifier, treat as part of the key if it's the only part\n // or ignore if there are more parts\n if (parts.length === 1) {\n key = normalizeKeyName(part)\n }\n }\n }\n }\n\n // If no key was found (empty string), use the last part as-is\n if (!key && parts.length > 0) {\n key = normalizeKeyName(parts[parts.length - 1]!.trim())\n }\n\n return {\n key,\n ctrl: modifiers.has('Control'),\n shift: modifiers.has('Shift'),\n alt: modifiers.has('Alt'),\n meta: modifiers.has('Meta'),\n modifiers: MODIFIER_ORDER.filter((m) => modifiers.has(m)),\n }\n}\n\n/**\n * Converts a RawHotkey object to a ParsedHotkey.\n * Optional modifier booleans default to false; modifiers array is derived from them.\n * When `mod` is true, it is resolved to Control or Meta based on platform.\n *\n * @param raw - The raw hotkey object\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey suitable for matching and formatting\n *\n * @example\n * ```ts\n * rawHotkeyToParsedHotkey({ key: 'Escape' })\n * // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')\n * // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')\n * // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function rawHotkeyToParsedHotkey(\n raw: RawHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n let ctrl = raw.ctrl ?? false\n const shift = raw.shift ?? false\n const alt = raw.alt ?? false\n let meta = raw.meta ?? false\n\n if (raw.mod) {\n const resolved = resolveModifier('Mod', platform)\n if (resolved === 'Control') {\n ctrl = true\n } else {\n meta = true\n }\n }\n\n const modifiers: Array<CanonicalModifier> = MODIFIER_ORDER.filter((m) => {\n switch (m) {\n case 'Control':\n return ctrl\n case 'Shift':\n return shift\n case 'Alt':\n return alt\n case 'Meta':\n return meta\n default:\n return false\n }\n })\n return {\n key: raw.key,\n ctrl,\n shift,\n alt,\n meta,\n modifiers,\n }\n}\n\n/**\n * Normalizes a hotkey string to its canonical form.\n *\n * The canonical form uses:\n * - Full modifier names (Control, Alt, Shift, Meta)\n * - Modifiers in order: Control+Alt+Shift+Meta\n * - Uppercase letters for single-character keys\n * - Proper casing for special keys (Escape, not escape)\n *\n * @param hotkey - The hotkey string to normalize\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns The normalized hotkey string\n *\n * @example\n * ```ts\n * normalizeHotkey('mod+shift+s') // On Mac: 'Shift+Meta+S'\n * normalizeHotkey('ctrl+a') // 'Control+A'\n * normalizeHotkey('esc') // 'Escape'\n * ```\n */\nexport function normalizeHotkey(\n hotkey: Key | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): string {\n const parsed = parseHotkey(hotkey, platform)\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 * Checks if a string represents a modifier key.\n *\n * @param key - The string to check\n * @returns True if the string is a recognized modifier\n */\nexport function isModifier(key: string): boolean {\n return key in MODIFIER_ALIASES || key.toLowerCase() in MODIFIER_ALIASES\n}\n\n/**\n * Parses a KeyboardEvent into a ParsedHotkey object.\n *\n * This function extracts the key and modifier state from a keyboard event\n * and converts it into the same format used by `parseHotkey()`.\n *\n * @param event - The KeyboardEvent to parse\n * @param platform - The target platform for resolving modifiers (defaults to auto-detection)\n * @returns A ParsedHotkey object representing the keyboard event\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * const parsed = parseKeyboardEvent(event)\n * console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }\n * })\n * ```\n */\nexport function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey {\n const normalizedKey = normalizeKeyName(event.key)\n\n // Build modifiers array in canonical order\n const modifiers: Array<CanonicalModifier> = []\n if (event.ctrlKey) modifiers.push('Control')\n if (event.altKey) modifiers.push('Alt')\n if (event.shiftKey) modifiers.push('Shift')\n if (event.metaKey) modifiers.push('Meta')\n\n return {\n key: normalizedKey,\n ctrl: event.ctrlKey,\n shift: event.shiftKey,\n alt: event.altKey,\n meta: event.metaKey,\n modifiers,\n }\n}\n\n/**\n * Converts a KeyboardEvent directly to a hotkey string.\n *\n * This is a convenience function that combines `parseKeyboardEvent()` and formatting.\n * The resulting hotkey string uses canonical modifier names (Control, Alt, Shift, Meta)\n * and is suitable for use with `useHotkey()` and other hotkey functions.\n *\n * @param event - The KeyboardEvent to convert\n * @param platform - The target platform (defaults to auto-detection)\n * @returns A hotkey string in canonical form (e.g., 'Control+Shift+S')\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * const hotkey = keyboardEventToHotkey(event)\n * console.log(hotkey) // 'Control+Shift+S'\n * useHotkey(hotkey, () => console.log('Shortcut triggered'))\n * })\n * ```\n */\nexport function keyboardEventToHotkey(event: KeyboardEvent): Hotkey {\n const parsed = parseKeyboardEvent(event)\n\n // Build hotkey string in canonical order (same as formatHotkey)\n const parts: Array<string> = []\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n parts.push(parsed.key)\n\n return parts.join('+') as Hotkey\n}\n\n/**\n * Checks if a KeyboardEvent represents a modifier-only key press.\n *\n * Modifier-only keys are keys like 'Control', 'Shift', 'Alt', 'Meta', etc.\n * that don't have an associated character or action key. This is useful\n * for filtering out modifier key presses when recording shortcuts.\n *\n * @param event - The KeyboardEvent to check\n * @returns True if the event represents a modifier-only key\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * if (isModifierKey(event)) {\n * console.log('Modifier key pressed, waiting for action key...')\n * return\n * }\n * // Process non-modifier key\n * })\n * ```\n */\nexport function isModifierKey(event: KeyboardEvent): boolean {\n const key = event.key\n return (\n key === 'Control' ||\n key === 'Shift' ||\n key === 'Alt' ||\n key === 'Meta' ||\n key === 'Command' ||\n key === 'OS' ||\n key === 'Win'\n )\n}\n\n/**\n * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.\n *\n * This is useful for validating that a recorded hotkey is complete and not\n * just a combination of modifiers without an action key.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to check\n * @param platform - The target platform for parsing (defaults to auto-detection)\n * @returns True if the hotkey contains at least one non-modifier key\n *\n * @example\n * ```ts\n * hasNonModifierKey('Control+Shift+S') // true\n * hasNonModifierKey('Control+Shift') // false (no action key)\n * hasNonModifierKey(parseHotkey('Mod+A')) // true\n * ```\n */\nexport function hasNonModifierKey(\n hotkey: Hotkey | ParsedHotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n // Check if the key part is actually a modifier\n const keyIsModifier = isModifier(parsed.key)\n\n // A valid hotkey must have a non-modifier key\n return !keyIsModifier && parsed.key.length > 0\n}\n\n/**\n * Converts a hotkey string to use 'Mod' format for portability.\n *\n * On macOS, converts 'Meta' to 'Mod'. On Windows/Linux, converts 'Control' to 'Mod'.\n * This enables cross-platform hotkey definitions that work consistently.\n *\n * @param hotkey - The hotkey string to convert\n * @param platform - The target platform (defaults to auto-detection)\n * @returns The hotkey string with 'Mod' format applied\n *\n * @example\n * ```ts\n * convertToModFormat('Meta+S', 'mac') // 'Mod+S'\n * convertToModFormat('Control+S', 'windows') // 'Mod+S'\n * convertToModFormat('Control+Meta+S', 'mac') // 'Control+Meta+S' (both present, no conversion)\n * ```\n */\nexport function convertToModFormat(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n const parsed = parseHotkey(hotkey, platform)\n\n // Only convert if we have exactly one primary modifier\n if (platform === 'mac' && parsed.meta && !parsed.ctrl) {\n // Convert Meta to Mod on Mac\n const parts = hotkey.split('+')\n return parts\n .map((part) => (part === 'Meta' ? 'Mod' : part))\n .join('+') as Hotkey\n } else if (platform !== 'mac' && parsed.ctrl && !parsed.meta) {\n // Convert Control to Mod on Windows/Linux\n const parts = hotkey.split('+')\n return parts\n .map((part) => (part === 'Control' ? 'Mod' : part))\n .join('+') as Hotkey\n }\n\n // No conversion needed\n return hotkey as Hotkey\n}\n"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"parse.js","names":[],"sources":["../src/parse.ts"],"sourcesContent":["import {\n MODIFIER_ALIASES,\n MODIFIER_ORDER,\n detectPlatform,\n normalizeKeyName,\n resolveModifier,\n} from './constants'\nimport type {\n CanonicalModifier,\n Hotkey,\n Key,\n ParsedHotkey,\n RawHotkey,\n RegisterableHotkey,\n} from './hotkey'\n\n/**\n * Parses a hotkey string into its component parts.\n *\n * @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey object with the key and modifier flags\n *\n * @example\n * ```ts\n * parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n * parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }\n * parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function parseHotkey(\n hotkey: Hotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n const parts = hotkey.split('+')\n const modifiers: Set<CanonicalModifier> = new Set()\n let key = ''\n\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i]!.trim()\n\n if (i === parts.length - 1) {\n // Last part is always the key\n key = normalizeKeyName(part)\n } else {\n // All other parts are modifiers\n const alias =\n MODIFIER_ALIASES[part] ?? MODIFIER_ALIASES[part.toLowerCase()]\n\n if (alias) {\n const resolved = resolveModifier(alias, platform)\n modifiers.add(resolved)\n } else {\n // Unknown modifier, treat as part of the key if it's the only part\n // or ignore if there are more parts\n if (parts.length === 1) {\n key = normalizeKeyName(part)\n }\n }\n }\n }\n\n // If no key was found (empty string), use the last part as-is\n if (!key && parts.length > 0) {\n key = normalizeKeyName(parts[parts.length - 1]!.trim())\n }\n\n return {\n key,\n ctrl: modifiers.has('Control'),\n shift: modifiers.has('Shift'),\n alt: modifiers.has('Alt'),\n meta: modifiers.has('Meta'),\n modifiers: MODIFIER_ORDER.filter((m) => modifiers.has(m)),\n }\n}\n\n/**\n * Converts a RawHotkey object to a ParsedHotkey.\n * Optional modifier booleans default to false; modifiers array is derived from them.\n * When `mod` is true, it is resolved to Control or Meta based on platform.\n *\n * @param raw - The raw hotkey object\n * @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)\n * @returns A ParsedHotkey suitable for matching and formatting\n *\n * @example\n * ```ts\n * rawHotkeyToParsedHotkey({ key: 'Escape' })\n * // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')\n * // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }\n *\n * rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')\n * // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }\n * ```\n */\nexport function rawHotkeyToParsedHotkey(\n raw: RawHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): ParsedHotkey {\n let ctrl = raw.ctrl ?? false\n const shift = raw.shift ?? false\n const alt = raw.alt ?? false\n let meta = raw.meta ?? false\n\n if (raw.mod) {\n const resolved = resolveModifier('Mod', platform)\n if (resolved === 'Control') {\n ctrl = true\n } else {\n meta = true\n }\n }\n\n const modifiers: Array<CanonicalModifier> = MODIFIER_ORDER.filter((m) => {\n switch (m) {\n case 'Control':\n return ctrl\n case 'Shift':\n return shift\n case 'Alt':\n return alt\n case 'Meta':\n return meta\n default:\n return false\n }\n })\n return {\n key: raw.key,\n ctrl,\n shift,\n alt,\n meta,\n modifiers,\n }\n}\n\n/**\n * Serializes a parsed hotkey to the canonical string form used for registration\n * and storage: `Mod` first when the platform allows, then `Alt`, then `Shift`;\n * otherwise full `Control+Alt+Shift+Meta` order.\n */\nfunction normalizedHotkeyStringFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey {\n const canUseMod =\n platform === 'mac'\n ? parsed.meta && !parsed.ctrl\n : parsed.ctrl && !parsed.meta\n\n const parts: Array<string> = []\n\n if (canUseMod) {\n parts.push('Mod')\n if (parsed.alt) {\n parts.push('Alt')\n }\n if (parsed.shift) {\n parts.push('Shift')\n }\n } else {\n for (const modifier of MODIFIER_ORDER) {\n if (parsed.modifiers.includes(modifier)) {\n parts.push(modifier)\n }\n }\n }\n\n parts.push(normalizeKeyName(parsed.key))\n return parts.join('+') as Hotkey\n}\n\n/**\n * Normalizes a hotkey string to its canonical form.\n *\n * - When `Mod` is allowed for the platform (Command on Mac without Control;\n * Control on Windows/Linux without Meta): emits `Mod`, then `Alt`, then `Shift`,\n * then the key (e.g. `Mod+Shift+E`, `Mod+S`).\n * - Otherwise: literal modifiers in `Control+Alt+Shift+Meta` order, then the key.\n * - Resolves aliases and normalizes key casing (e.g. `esc` → `Escape`, `a` → `A`).\n *\n * @param hotkey - The hotkey string to normalize\n * @param platform - The target platform for resolving `Mod` (defaults to auto-detection)\n * @returns The normalized hotkey string\n *\n * @example\n * ```ts\n * normalizeHotkey('shift+meta+e', 'mac') // 'Mod+Shift+E'\n * normalizeHotkey('ctrl+a', 'windows') // 'Mod+A'\n * normalizeHotkey('esc') // 'Escape'\n * ```\n */\nexport function normalizeHotkey(\n hotkey: Key | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(\n parseHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Same canonical string as {@link normalizeHotkey}, but from an already-parsed hotkey.\n */\nexport function normalizeHotkeyFromParsed(\n parsed: ParsedHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parsed, platform)\n}\n\n/**\n * Normalizes a string or {@link RawHotkey} object to the same canonical hotkey string.\n * Use this in framework adapters instead of branching on `formatHotkey(rawHotkeyToParsedHotkey(...))`.\n */\nexport function normalizeRegisterableHotkey(\n hotkey: RegisterableHotkey,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return typeof hotkey === 'string'\n ? normalizeHotkey(hotkey, platform)\n : normalizeHotkeyFromParsed(\n rawHotkeyToParsedHotkey(hotkey, platform),\n platform,\n )\n}\n\n/**\n * Checks if a string is a recognized modifier token (including aliases).\n *\n * For a `KeyboardEvent`, use `isModifier(normalizeKeyName(event.key))` so DOM\n * spellings like `OS` / `Win` match the same alias table.\n *\n * @param key - Key name or alias (e.g. from a hotkey string or `event.key`)\n * @returns True if the string is a recognized modifier\n */\nexport function isModifierKey(\n key: Key | (string & {}),\n): key is keyof typeof MODIFIER_ALIASES {\n return key in MODIFIER_ALIASES || key.toLowerCase() in MODIFIER_ALIASES\n}\n\n/**\n * Parses a KeyboardEvent into a ParsedHotkey object.\n *\n * This function extracts the key and modifier state from a keyboard event\n * and converts it into the same format used by `parseHotkey()`.\n *\n * @param event - The KeyboardEvent to parse\n * @param platform - The target platform for resolving modifiers (defaults to auto-detection)\n * @returns A ParsedHotkey object representing the keyboard event\n *\n * @example\n * ```ts\n * document.addEventListener('keydown', (event) => {\n * const parsed = parseKeyboardEvent(event)\n * console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }\n * })\n * ```\n */\nexport function parseKeyboardEvent(event: KeyboardEvent): ParsedHotkey {\n const normalizedKey = normalizeKeyName(event.key)\n\n // Build modifiers array in canonical order\n const modifiers: Array<CanonicalModifier> = []\n if (event.ctrlKey) modifiers.push('Control')\n if (event.altKey) modifiers.push('Alt')\n if (event.shiftKey) modifiers.push('Shift')\n if (event.metaKey) modifiers.push('Meta')\n\n return {\n key: normalizedKey,\n ctrl: event.ctrlKey,\n shift: event.shiftKey,\n alt: event.altKey,\n meta: event.metaKey,\n modifiers,\n }\n}\n\n/**\n * Normalizes a keyboard event to the same canonical hotkey string as {@link normalizeHotkey}.\n *\n * @param event - The keyboard event (typically `keydown`)\n * @param platform - Target platform for `Mod` eligibility\n */\nexport function normalizeHotkeyFromEvent(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): Hotkey {\n return normalizedHotkeyStringFromParsed(parseKeyboardEvent(event), platform)\n}\n\n/**\n * Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.\n *\n * This is useful for validating that a recorded hotkey is complete and not\n * just a combination of modifiers without an action key.\n *\n * @param hotkey - The hotkey string or ParsedHotkey to check\n * @param platform - The target platform for parsing (defaults to auto-detection)\n * @returns True if the hotkey contains at least one non-modifier key\n *\n * @example\n * ```ts\n * hasNonModifierKey('Control+Shift+S') // true\n * hasNonModifierKey('Control+Shift') // false (no action key)\n * hasNonModifierKey(parseHotkey('Mod+A')) // true\n * ```\n */\nexport function hasNonModifierKey(\n hotkey: Hotkey | ParsedHotkey | (string & {}),\n platform: 'mac' | 'windows' | 'linux' = detectPlatform(),\n): boolean {\n const parsed =\n typeof hotkey === 'string' ? parseHotkey(hotkey, platform) : hotkey\n\n // Check if the key part is actually a modifier\n const keyIsModifier = isModifierKey(parsed.key)\n\n // A valid hotkey must have a non-modifier key\n return !keyIsModifier && parsed.key.length > 0\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA8BA,SAAgB,YACd,QACA,WAAwC,gBAAgB,EAC1C;CACd,MAAM,QAAQ,OAAO,MAAM,IAAI;CAC/B,MAAM,4BAAoC,IAAI,KAAK;CACnD,IAAI,MAAM;AAEV,MAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;EACrC,MAAM,OAAO,MAAM,GAAI,MAAM;AAE7B,MAAI,MAAM,MAAM,SAAS,EAEvB,OAAM,iBAAiB,KAAK;OACvB;GAEL,MAAM,QACJ,iBAAiB,SAAS,iBAAiB,KAAK,aAAa;AAE/D,OAAI,OAAO;IACT,MAAM,WAAW,gBAAgB,OAAO,SAAS;AACjD,cAAU,IAAI,SAAS;cAInB,MAAM,WAAW,EACnB,OAAM,iBAAiB,KAAK;;;AAOpC,KAAI,CAAC,OAAO,MAAM,SAAS,EACzB,OAAM,iBAAiB,MAAM,MAAM,SAAS,GAAI,MAAM,CAAC;AAGzD,QAAO;EACL;EACA,MAAM,UAAU,IAAI,UAAU;EAC9B,OAAO,UAAU,IAAI,QAAQ;EAC7B,KAAK,UAAU,IAAI,MAAM;EACzB,MAAM,UAAU,IAAI,OAAO;EAC3B,WAAW,eAAe,QAAQ,MAAM,UAAU,IAAI,EAAE,CAAC;EAC1D;;;;;;;;;;;;;;;;;;;;;;;AAwBH,SAAgB,wBACd,KACA,WAAwC,gBAAgB,EAC1C;CACd,IAAI,OAAO,IAAI,QAAQ;CACvB,MAAM,QAAQ,IAAI,SAAS;CAC3B,MAAM,MAAM,IAAI,OAAO;CACvB,IAAI,OAAO,IAAI,QAAQ;AAEvB,KAAI,IAAI,IAEN,KADiB,gBAAgB,OAAO,SAAS,KAChC,UACf,QAAO;KAEP,QAAO;CAIX,MAAM,YAAsC,eAAe,QAAQ,MAAM;AACvE,UAAQ,GAAR;GACE,KAAK,UACH,QAAO;GACT,KAAK,QACH,QAAO;GACT,KAAK,MACH,QAAO;GACT,KAAK,OACH,QAAO;GACT,QACE,QAAO;;GAEX;AACF,QAAO;EACL,KAAK,IAAI;EACT;EACA;EACA;EACA;EACA;EACD;;;;;;;AAQH,SAAS,iCACP,QACA,UACQ;CACR,MAAM,YACJ,aAAa,QACT,OAAO,QAAQ,CAAC,OAAO,OACvB,OAAO,QAAQ,CAAC,OAAO;CAE7B,MAAM,QAAuB,EAAE;AAE/B,KAAI,WAAW;AACb,QAAM,KAAK,MAAM;AACjB,MAAI,OAAO,IACT,OAAM,KAAK,MAAM;AAEnB,MAAI,OAAO,MACT,OAAM,KAAK,QAAQ;OAGrB,MAAK,MAAM,YAAY,eACrB,KAAI,OAAO,UAAU,SAAS,SAAS,CACrC,OAAM,KAAK,SAAS;AAK1B,OAAM,KAAK,iBAAiB,OAAO,IAAI,CAAC;AACxC,QAAO,MAAM,KAAK,IAAI;;;;;;;;;;;;;;;;;;;;;;AAuBxB,SAAgB,gBACd,QACA,WAAwC,gBAAgB,EAChD;AACR,QAAO,iCACL,YAAY,QAAQ,SAAS,EAC7B,SACD;;;;;AAMH,SAAgB,0BACd,QACA,WAAwC,gBAAgB,EAChD;AACR,QAAO,iCAAiC,QAAQ,SAAS;;;;;;AAO3D,SAAgB,4BACd,QACA,WAAwC,gBAAgB,EAChD;AACR,QAAO,OAAO,WAAW,WACrB,gBAAgB,QAAQ,SAAS,GACjC,0BACE,wBAAwB,QAAQ,SAAS,EACzC,SACD;;;;;;;;;;;AAYP,SAAgB,cACd,KACsC;AACtC,QAAO,OAAO,oBAAoB,IAAI,aAAa,IAAI;;;;;;;;;;;;;;;;;;;;AAqBzD,SAAgB,mBAAmB,OAAoC;CACrE,MAAM,gBAAgB,iBAAiB,MAAM,IAAI;CAGjD,MAAM,YAAsC,EAAE;AAC9C,KAAI,MAAM,QAAS,WAAU,KAAK,UAAU;AAC5C,KAAI,MAAM,OAAQ,WAAU,KAAK,MAAM;AACvC,KAAI,MAAM,SAAU,WAAU,KAAK,QAAQ;AAC3C,KAAI,MAAM,QAAS,WAAU,KAAK,OAAO;AAEzC,QAAO;EACL,KAAK;EACL,MAAM,MAAM;EACZ,OAAO,MAAM;EACb,KAAK,MAAM;EACX,MAAM,MAAM;EACZ;EACD;;;;;;;;AASH,SAAgB,yBACd,OACA,WAAwC,gBAAgB,EAChD;AACR,QAAO,iCAAiC,mBAAmB,MAAM,EAAE,SAAS;;;;;;;;;;;;;;;;;;;AAoB9E,SAAgB,kBACd,QACA,WAAwC,gBAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAM/D,QAAO,CAHe,cAAc,OAAO,IAAI,IAGtB,OAAO,IAAI,SAAS"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const require_constants = require('./constants.cjs');
|
|
2
|
+
const require_parse = require('./parse.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/recorder-chord.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parses a keydown event into a single normalized hotkey chord, or null if
|
|
7
|
+
* the event should be ignored (modifier-only or invalid).
|
|
8
|
+
*/
|
|
9
|
+
function hotkeyChordFromKeydown(event, platform) {
|
|
10
|
+
if (require_parse.isModifierKey(require_constants.normalizeKeyName(event.key))) return null;
|
|
11
|
+
const finalHotkey = require_parse.normalizeHotkeyFromEvent(event, platform);
|
|
12
|
+
if (!require_parse.hasNonModifierKey(finalHotkey, platform)) return null;
|
|
13
|
+
return finalHotkey;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
exports.hotkeyChordFromKeydown = hotkeyChordFromKeydown;
|
|
18
|
+
//# sourceMappingURL=recorder-chord.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recorder-chord.cjs","names":["isModifierKey","normalizeKeyName","normalizeHotkeyFromEvent","hasNonModifierKey"],"sources":["../src/recorder-chord.ts"],"sourcesContent":["import { normalizeKeyName } from './constants'\nimport {\n hasNonModifierKey,\n isModifierKey,\n normalizeHotkeyFromEvent,\n} from './parse'\nimport type { Hotkey } from './hotkey'\n\n/**\n * Parses a keydown event into a single normalized hotkey chord, or null if\n * the event should be ignored (modifier-only or invalid).\n */\nexport function hotkeyChordFromKeydown(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey | null {\n if (isModifierKey(normalizeKeyName(event.key))) {\n return null\n }\n const finalHotkey = normalizeHotkeyFromEvent(event, platform)\n if (!hasNonModifierKey(finalHotkey, platform)) {\n return null\n }\n return finalHotkey\n}\n"],"mappings":";;;;;;;;AAYA,SAAgB,uBACd,OACA,UACe;AACf,KAAIA,4BAAcC,mCAAiB,MAAM,IAAI,CAAC,CAC5C,QAAO;CAET,MAAM,cAAcC,uCAAyB,OAAO,SAAS;AAC7D,KAAI,CAACC,gCAAkB,aAAa,SAAS,CAC3C,QAAO;AAET,QAAO"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { normalizeKeyName } from "./constants.js";
|
|
2
|
+
import { hasNonModifierKey, isModifierKey, normalizeHotkeyFromEvent } from "./parse.js";
|
|
3
|
+
|
|
4
|
+
//#region src/recorder-chord.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parses a keydown event into a single normalized hotkey chord, or null if
|
|
7
|
+
* the event should be ignored (modifier-only or invalid).
|
|
8
|
+
*/
|
|
9
|
+
function hotkeyChordFromKeydown(event, platform) {
|
|
10
|
+
if (isModifierKey(normalizeKeyName(event.key))) return null;
|
|
11
|
+
const finalHotkey = normalizeHotkeyFromEvent(event, platform);
|
|
12
|
+
if (!hasNonModifierKey(finalHotkey, platform)) return null;
|
|
13
|
+
return finalHotkey;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { hotkeyChordFromKeydown };
|
|
18
|
+
//# sourceMappingURL=recorder-chord.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recorder-chord.js","names":[],"sources":["../src/recorder-chord.ts"],"sourcesContent":["import { normalizeKeyName } from './constants'\nimport {\n hasNonModifierKey,\n isModifierKey,\n normalizeHotkeyFromEvent,\n} from './parse'\nimport type { Hotkey } from './hotkey'\n\n/**\n * Parses a keydown event into a single normalized hotkey chord, or null if\n * the event should be ignored (modifier-only or invalid).\n */\nexport function hotkeyChordFromKeydown(\n event: KeyboardEvent,\n platform: 'mac' | 'windows' | 'linux',\n): Hotkey | null {\n if (isModifierKey(normalizeKeyName(event.key))) {\n return null\n }\n const finalHotkey = normalizeHotkeyFromEvent(event, platform)\n if (!hasNonModifierKey(finalHotkey, platform)) {\n return null\n }\n return finalHotkey\n}\n"],"mappings":";;;;;;;;AAYA,SAAgB,uBACd,OACA,UACe;AACf,KAAI,cAAc,iBAAiB,MAAM,IAAI,CAAC,CAC5C,QAAO;CAET,MAAM,cAAc,yBAAyB,OAAO,SAAS;AAC7D,KAAI,CAAC,kBAAkB,aAAa,SAAS,CAC3C,QAAO;AAET,QAAO"}
|
|
@@ -8,6 +8,7 @@ let _tanstack_store = require("@tanstack/store");
|
|
|
8
8
|
//#region src/sequence-manager.ts
|
|
9
9
|
/**
|
|
10
10
|
* Default timeout between keys in a sequence (in milliseconds).
|
|
11
|
+
* Exported for consumers that display sequence state (e.g. devtools).
|
|
11
12
|
*/
|
|
12
13
|
const DEFAULT_SEQUENCE_TIMEOUT = 1e3;
|
|
13
14
|
let sequenceIdCounter = 0;
|
|
@@ -52,12 +53,16 @@ function sequenceKey(sequence) {
|
|
|
52
53
|
* Builds a devtools view from an internal registration.
|
|
53
54
|
*/
|
|
54
55
|
function toRegistrationView(reg) {
|
|
56
|
+
const len = reg.parsedSequence.length;
|
|
57
|
+
const inProgress = reg.currentIndex > 0 && reg.currentIndex < len;
|
|
55
58
|
return {
|
|
56
59
|
id: reg.id,
|
|
57
60
|
sequence: reg.sequence,
|
|
58
61
|
options: reg.options,
|
|
59
62
|
target: reg.target,
|
|
60
|
-
triggerCount: reg.triggerCount
|
|
63
|
+
triggerCount: reg.triggerCount,
|
|
64
|
+
matchedStepCount: inProgress ? reg.currentIndex : 0,
|
|
65
|
+
partialMatchLastKeyTime: inProgress ? reg.lastKeyTime : 0
|
|
61
66
|
};
|
|
62
67
|
}
|
|
63
68
|
var SequenceManager = class SequenceManager {
|
|
@@ -86,6 +91,9 @@ var SequenceManager = class SequenceManager {
|
|
|
86
91
|
SequenceManager.#instance = null;
|
|
87
92
|
}
|
|
88
93
|
}
|
|
94
|
+
#syncRegistrationView(registration) {
|
|
95
|
+
this.registrations.setState((prev) => new Map(prev).set(registration.id, toRegistrationView(registration)));
|
|
96
|
+
}
|
|
89
97
|
/**
|
|
90
98
|
* Registers a hotkey sequence handler.
|
|
91
99
|
*
|
|
@@ -223,7 +231,7 @@ var SequenceManager = class SequenceManager {
|
|
|
223
231
|
* Processes keyboard events for a specific target and event type.
|
|
224
232
|
*/
|
|
225
233
|
#processTargetEvent(event, target, eventType) {
|
|
226
|
-
if (require_parse.isModifierKey(event)) return;
|
|
234
|
+
if (require_parse.isModifierKey(require_constants.normalizeKeyName(event.key))) return;
|
|
227
235
|
const targetRegs = this.#targetRegistrations.get(target);
|
|
228
236
|
if (!targetRegs) return;
|
|
229
237
|
const now = Date.now();
|
|
@@ -238,8 +246,11 @@ var SequenceManager = class SequenceManager {
|
|
|
238
246
|
}
|
|
239
247
|
}
|
|
240
248
|
if (registration.options.eventType !== eventType) continue;
|
|
241
|
-
const timeout = registration.options.timeout ??
|
|
242
|
-
if (registration.currentIndex > 0 && now - registration.lastKeyTime > timeout)
|
|
249
|
+
const timeout = registration.options.timeout ?? 1e3;
|
|
250
|
+
if (registration.currentIndex > 0 && now - registration.lastKeyTime > timeout) {
|
|
251
|
+
registration.currentIndex = 0;
|
|
252
|
+
this.#syncRegistrationView(registration);
|
|
253
|
+
}
|
|
243
254
|
const expectedHotkey = registration.parsedSequence[registration.currentIndex];
|
|
244
255
|
if (!expectedHotkey) continue;
|
|
245
256
|
if (require_match.matchesKeyboardEvent(event, expectedHotkey, registration.options.platform)) {
|
|
@@ -252,15 +263,18 @@ var SequenceManager = class SequenceManager {
|
|
|
252
263
|
hotkey: registration.sequence.join(" "),
|
|
253
264
|
parsedHotkey: registration.parsedSequence[registration.parsedSequence.length - 1]
|
|
254
265
|
};
|
|
255
|
-
registration.
|
|
266
|
+
registration.triggerCount++;
|
|
256
267
|
registration.currentIndex = 0;
|
|
257
|
-
|
|
268
|
+
this.#syncRegistrationView(registration);
|
|
269
|
+
registration.callback(event, context);
|
|
270
|
+
} else this.#syncRegistrationView(registration);
|
|
258
271
|
} else if (registration.currentIndex > 0) {
|
|
259
272
|
const firstHotkey = registration.parsedSequence[0];
|
|
260
273
|
if (require_match.matchesKeyboardEvent(event, firstHotkey, registration.options.platform)) {
|
|
261
274
|
registration.currentIndex = 1;
|
|
262
275
|
registration.lastKeyTime = now;
|
|
263
276
|
} else registration.currentIndex = 0;
|
|
277
|
+
this.#syncRegistrationView(registration);
|
|
264
278
|
}
|
|
265
279
|
}
|
|
266
280
|
}
|
|
@@ -279,6 +293,7 @@ var SequenceManager = class SequenceManager {
|
|
|
279
293
|
for (const registration of this.#registrations.values()) {
|
|
280
294
|
registration.currentIndex = 0;
|
|
281
295
|
registration.lastKeyTime = 0;
|
|
296
|
+
this.#syncRegistrationView(registration);
|
|
282
297
|
}
|
|
283
298
|
}
|
|
284
299
|
/**
|
|
@@ -303,7 +318,7 @@ var SequenceManager = class SequenceManager {
|
|
|
303
318
|
cancelable: true
|
|
304
319
|
});
|
|
305
320
|
registration.triggerCount++;
|
|
306
|
-
this
|
|
321
|
+
this.#syncRegistrationView(registration);
|
|
307
322
|
const context = {
|
|
308
323
|
hotkey: registration.sequence.join(" "),
|
|
309
324
|
parsedHotkey: lastParsed
|
|
@@ -357,7 +372,7 @@ function getSequenceManager() {
|
|
|
357
372
|
*/
|
|
358
373
|
function createSequenceMatcher(sequence, options = {}) {
|
|
359
374
|
const platform = options.platform ?? require_constants.detectPlatform();
|
|
360
|
-
const timeout = options.timeout ??
|
|
375
|
+
const timeout = options.timeout ?? 1e3;
|
|
361
376
|
const parsedSequence = sequence.map((hotkey) => require_parse.parseHotkey(hotkey, platform));
|
|
362
377
|
let currentIndex = 0;
|
|
363
378
|
let lastKeyTime = 0;
|
|
@@ -391,6 +406,7 @@ function createSequenceMatcher(sequence, options = {}) {
|
|
|
391
406
|
}
|
|
392
407
|
|
|
393
408
|
//#endregion
|
|
409
|
+
exports.DEFAULT_SEQUENCE_TIMEOUT = DEFAULT_SEQUENCE_TIMEOUT;
|
|
394
410
|
exports.SequenceManager = SequenceManager;
|
|
395
411
|
exports.createSequenceMatcher = createSequenceMatcher;
|
|
396
412
|
exports.getSequenceManager = getSequenceManager;
|