@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
package/dist/parse.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { MODIFIER_ALIASES, MODIFIER_ORDER, detectPlatform, normalizeKeyName, resolveModifier } from "./constants.js";
|
|
2
|
+
|
|
3
|
+
//#region src/parse.ts
|
|
4
|
+
/**
|
|
5
|
+
* Parses a hotkey string into its component parts.
|
|
6
|
+
*
|
|
7
|
+
* @param hotkey - The hotkey string to parse (e.g., 'Mod+Shift+S')
|
|
8
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
9
|
+
* @returns A ParsedHotkey object with the key and modifier flags
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* parseHotkey('Mod+S') // On Mac: { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
14
|
+
* parseHotkey('Mod+S') // On Windows: { key: 'S', ctrl: true, shift: false, alt: false, meta: false, modifiers: ['Control'] }
|
|
15
|
+
* parseHotkey('Control+Shift+A') // { key: 'A', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function parseHotkey(hotkey, platform = detectPlatform()) {
|
|
19
|
+
const parts = hotkey.split("+");
|
|
20
|
+
const modifiers = /* @__PURE__ */ new Set();
|
|
21
|
+
let key = "";
|
|
22
|
+
for (let i = 0; i < parts.length; i++) {
|
|
23
|
+
const part = parts[i].trim();
|
|
24
|
+
if (i === parts.length - 1) key = normalizeKeyName(part);
|
|
25
|
+
else {
|
|
26
|
+
const alias = MODIFIER_ALIASES[part] ?? MODIFIER_ALIASES[part.toLowerCase()];
|
|
27
|
+
if (alias) {
|
|
28
|
+
const resolved = resolveModifier(alias, platform);
|
|
29
|
+
modifiers.add(resolved);
|
|
30
|
+
} else if (parts.length === 1) key = normalizeKeyName(part);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (!key && parts.length > 0) key = normalizeKeyName(parts[parts.length - 1].trim());
|
|
34
|
+
return {
|
|
35
|
+
key,
|
|
36
|
+
ctrl: modifiers.has("Control"),
|
|
37
|
+
shift: modifiers.has("Shift"),
|
|
38
|
+
alt: modifiers.has("Alt"),
|
|
39
|
+
meta: modifiers.has("Meta"),
|
|
40
|
+
modifiers: MODIFIER_ORDER.filter((m) => modifiers.has(m))
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Converts a RawHotkey object to a ParsedHotkey.
|
|
45
|
+
* Optional modifier booleans default to false; modifiers array is derived from them.
|
|
46
|
+
* When `mod` is true, it is resolved to Control or Meta based on platform.
|
|
47
|
+
*
|
|
48
|
+
* @param raw - The raw hotkey object
|
|
49
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
50
|
+
* @returns A ParsedHotkey suitable for matching and formatting
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* rawHotkeyToParsedHotkey({ key: 'Escape' })
|
|
55
|
+
* // { key: 'Escape', ctrl: false, shift: false, alt: false, meta: false, modifiers: [] }
|
|
56
|
+
*
|
|
57
|
+
* rawHotkeyToParsedHotkey({ key: 'S', mod: true }, 'mac')
|
|
58
|
+
* // { key: 'S', ctrl: false, shift: false, alt: false, meta: true, modifiers: ['Meta'] }
|
|
59
|
+
*
|
|
60
|
+
* rawHotkeyToParsedHotkey({ key: 'S', mod: true, shift: true }, 'windows')
|
|
61
|
+
* // { key: 'S', ctrl: true, shift: true, alt: false, meta: false, modifiers: ['Control', 'Shift'] }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function rawHotkeyToParsedHotkey(raw, platform = detectPlatform()) {
|
|
65
|
+
let ctrl = raw.ctrl ?? false;
|
|
66
|
+
const shift = raw.shift ?? false;
|
|
67
|
+
const alt = raw.alt ?? false;
|
|
68
|
+
let meta = raw.meta ?? false;
|
|
69
|
+
if (raw.mod) if (resolveModifier("Mod", platform) === "Control") ctrl = true;
|
|
70
|
+
else meta = true;
|
|
71
|
+
const modifiers = MODIFIER_ORDER.filter((m) => {
|
|
72
|
+
switch (m) {
|
|
73
|
+
case "Control": return ctrl;
|
|
74
|
+
case "Shift": return shift;
|
|
75
|
+
case "Alt": return alt;
|
|
76
|
+
case "Meta": return meta;
|
|
77
|
+
default: return false;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
return {
|
|
81
|
+
key: raw.key,
|
|
82
|
+
ctrl,
|
|
83
|
+
shift,
|
|
84
|
+
alt,
|
|
85
|
+
meta,
|
|
86
|
+
modifiers
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Normalizes a hotkey string to its canonical form.
|
|
91
|
+
*
|
|
92
|
+
* The canonical form uses:
|
|
93
|
+
* - Full modifier names (Control, Alt, Shift, Meta)
|
|
94
|
+
* - Modifiers in order: Control+Alt+Shift+Meta
|
|
95
|
+
* - Uppercase letters for single-character keys
|
|
96
|
+
* - Proper casing for special keys (Escape, not escape)
|
|
97
|
+
*
|
|
98
|
+
* @param hotkey - The hotkey string to normalize
|
|
99
|
+
* @param platform - The target platform for resolving 'Mod' (defaults to auto-detection)
|
|
100
|
+
* @returns The normalized hotkey string
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* normalizeHotkey('mod+shift+s') // On Mac: 'Shift+Meta+S'
|
|
105
|
+
* normalizeHotkey('ctrl+a') // 'Control+A'
|
|
106
|
+
* normalizeHotkey('esc') // 'Escape'
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
function normalizeHotkey(hotkey, platform = detectPlatform()) {
|
|
110
|
+
const parsed = parseHotkey(hotkey, platform);
|
|
111
|
+
const parts = [];
|
|
112
|
+
for (const modifier of MODIFIER_ORDER) if (parsed.modifiers.includes(modifier)) parts.push(modifier);
|
|
113
|
+
parts.push(parsed.key);
|
|
114
|
+
return parts.join("+");
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Checks if a string represents a modifier key.
|
|
118
|
+
*
|
|
119
|
+
* @param key - The string to check
|
|
120
|
+
* @returns True if the string is a recognized modifier
|
|
121
|
+
*/
|
|
122
|
+
function isModifier(key) {
|
|
123
|
+
return key in MODIFIER_ALIASES || key.toLowerCase() in MODIFIER_ALIASES;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Parses a KeyboardEvent into a ParsedHotkey object.
|
|
127
|
+
*
|
|
128
|
+
* This function extracts the key and modifier state from a keyboard event
|
|
129
|
+
* and converts it into the same format used by `parseHotkey()`.
|
|
130
|
+
*
|
|
131
|
+
* @param event - The KeyboardEvent to parse
|
|
132
|
+
* @param platform - The target platform for resolving modifiers (defaults to auto-detection)
|
|
133
|
+
* @returns A ParsedHotkey object representing the keyboard event
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* document.addEventListener('keydown', (event) => {
|
|
138
|
+
* const parsed = parseKeyboardEvent(event)
|
|
139
|
+
* console.log(parsed) // { key: 'S', ctrl: true, shift: false, ... }
|
|
140
|
+
* })
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
function parseKeyboardEvent(event) {
|
|
144
|
+
const normalizedKey = normalizeKeyName(event.key);
|
|
145
|
+
const modifiers = [];
|
|
146
|
+
if (event.ctrlKey) modifiers.push("Control");
|
|
147
|
+
if (event.altKey) modifiers.push("Alt");
|
|
148
|
+
if (event.shiftKey) modifiers.push("Shift");
|
|
149
|
+
if (event.metaKey) modifiers.push("Meta");
|
|
150
|
+
return {
|
|
151
|
+
key: normalizedKey,
|
|
152
|
+
ctrl: event.ctrlKey,
|
|
153
|
+
shift: event.shiftKey,
|
|
154
|
+
alt: event.altKey,
|
|
155
|
+
meta: event.metaKey,
|
|
156
|
+
modifiers
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Converts a KeyboardEvent directly to a hotkey string.
|
|
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
|
+
*
|
|
189
|
+
* Modifier-only keys are keys like 'Control', 'Shift', 'Alt', 'Meta', etc.
|
|
190
|
+
* that don't have an associated character or action key. This is useful
|
|
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
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
function isModifierKey(event) {
|
|
208
|
+
const key = event.key;
|
|
209
|
+
return key === "Control" || key === "Shift" || key === "Alt" || key === "Meta" || key === "Command" || key === "OS" || key === "Win";
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Checks if a hotkey or ParsedHotkey contains at least one non-modifier key.
|
|
213
|
+
*
|
|
214
|
+
* This is useful for validating that a recorded hotkey is complete and not
|
|
215
|
+
* just a combination of modifiers without an action key.
|
|
216
|
+
*
|
|
217
|
+
* @param hotkey - The hotkey string or ParsedHotkey to check
|
|
218
|
+
* @param platform - The target platform for parsing (defaults to auto-detection)
|
|
219
|
+
* @returns True if the hotkey contains at least one non-modifier key
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* hasNonModifierKey('Control+Shift+S') // true
|
|
224
|
+
* hasNonModifierKey('Control+Shift') // false (no action key)
|
|
225
|
+
* hasNonModifierKey(parseHotkey('Mod+A')) // true
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
function hasNonModifierKey(hotkey, platform = detectPlatform()) {
|
|
229
|
+
const parsed = typeof hotkey === "string" ? parseHotkey(hotkey, platform) : hotkey;
|
|
230
|
+
return !isModifier(parsed.key) && parsed.key.length > 0;
|
|
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;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
//#endregion
|
|
257
|
+
export { convertToModFormat, hasNonModifierKey, isModifier, isModifierKey, keyboardEventToHotkey, normalizeHotkey, parseHotkey, parseKeyboardEvent, rawHotkeyToParsedHotkey };
|
|
258
|
+
//# sourceMappingURL=parse.js.map
|
|
@@ -0,0 +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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CAC3D,MAAM,MAAM,MAAM;AAClB,QACE,QAAQ,aACR,QAAQ,WACR,QAAQ,SACR,QAAQ,UACR,QAAQ,aACR,QAAQ,QACR,QAAQ;;;;;;;;;;;;;;;;;;;AAqBZ,SAAgB,kBACd,QACA,WAAwC,gBAAgB,EAC/C;CACT,MAAM,SACJ,OAAO,WAAW,WAAW,YAAY,QAAQ,SAAS,GAAG;AAM/D,QAAO,CAHe,WAAW,OAAO,IAAI,IAGnB,OAAO,IAAI,SAAS;;;;;;;;;;;;;;;;;;;AAoB/C,SAAgB,mBACd,QACA,WAAwC,gBAAgB,EAChD;CACR,MAAM,SAAS,YAAY,QAAQ,SAAS;AAG5C,KAAI,aAAa,SAAS,OAAO,QAAQ,CAAC,OAAO,KAG/C,QADc,OAAO,MAAM,IAAI,CAE5B,KAAK,SAAU,SAAS,SAAS,QAAQ,KAAM,CAC/C,KAAK,IAAI;UACH,aAAa,SAAS,OAAO,QAAQ,CAAC,OAAO,KAGtD,QADc,OAAO,MAAM,IAAI,CAE5B,KAAK,SAAU,SAAS,YAAY,QAAQ,KAAM,CAClD,KAAK,IAAI;AAId,QAAO"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
const require_constants = require('./constants.cjs');
|
|
2
|
+
const require_parse = require('./parse.cjs');
|
|
3
|
+
let _tanstack_store = require("@tanstack/store");
|
|
4
|
+
|
|
5
|
+
//#region src/recorder.ts
|
|
6
|
+
/**
|
|
7
|
+
* Framework-agnostic class for recording keyboard shortcuts.
|
|
8
|
+
*
|
|
9
|
+
* This class handles all the complexity of capturing keyboard events,
|
|
10
|
+
* converting them to hotkey strings, and handling edge cases like
|
|
11
|
+
* Escape to cancel or Backspace/Delete to clear.
|
|
12
|
+
*
|
|
13
|
+
* State Management:
|
|
14
|
+
* - Uses TanStack Store for reactive state management
|
|
15
|
+
* - State can be accessed via `recorder.store.state` when using the class directly
|
|
16
|
+
* - When using framework adapters (React), use `useStore` hooks for reactive state
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const recorder = new HotkeyRecorder({
|
|
21
|
+
* onRecord: (hotkey) => {
|
|
22
|
+
* console.log('Recorded:', hotkey)
|
|
23
|
+
* },
|
|
24
|
+
* onCancel: () => {
|
|
25
|
+
* console.log('Recording cancelled')
|
|
26
|
+
* },
|
|
27
|
+
* })
|
|
28
|
+
*
|
|
29
|
+
* // Start recording
|
|
30
|
+
* recorder.start()
|
|
31
|
+
*
|
|
32
|
+
* // Access state directly
|
|
33
|
+
* console.log(recorder.store.state.isRecording) // true
|
|
34
|
+
*
|
|
35
|
+
* // Subscribe to changes with TanStack Store
|
|
36
|
+
* const unsubscribe = recorder.store.subscribe(() => {
|
|
37
|
+
* console.log('Recording:', recorder.store.state.isRecording)
|
|
38
|
+
* })
|
|
39
|
+
*
|
|
40
|
+
* // Cleanup
|
|
41
|
+
* recorder.destroy()
|
|
42
|
+
* unsubscribe()
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
var HotkeyRecorder = class {
|
|
46
|
+
#keydownHandler = null;
|
|
47
|
+
#options;
|
|
48
|
+
#platform;
|
|
49
|
+
constructor(options) {
|
|
50
|
+
this.store = new _tanstack_store.Store({
|
|
51
|
+
isRecording: false,
|
|
52
|
+
recordedHotkey: null
|
|
53
|
+
});
|
|
54
|
+
this.#options = options;
|
|
55
|
+
this.#platform = require_constants.detectPlatform();
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Updates the recorder options, including callbacks.
|
|
59
|
+
* This allows framework adapters to sync callback changes without recreating the recorder.
|
|
60
|
+
*/
|
|
61
|
+
setOptions(options) {
|
|
62
|
+
this.#options = {
|
|
63
|
+
...this.#options,
|
|
64
|
+
...options
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Start recording a new hotkey.
|
|
69
|
+
*
|
|
70
|
+
* Sets up a keydown event listener that captures keyboard events
|
|
71
|
+
* and converts them to hotkey strings. Recording continues until
|
|
72
|
+
* a valid hotkey is recorded, Escape is pressed, or stop/cancel is called.
|
|
73
|
+
*/
|
|
74
|
+
start() {
|
|
75
|
+
if (this.#keydownHandler) return;
|
|
76
|
+
this.store.setState(() => ({
|
|
77
|
+
isRecording: true,
|
|
78
|
+
recordedHotkey: null
|
|
79
|
+
}));
|
|
80
|
+
const handler = (event) => {
|
|
81
|
+
if (!this.#keydownHandler) return;
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
event.stopPropagation();
|
|
84
|
+
if (event.key === "Escape") {
|
|
85
|
+
this.cancel();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (event.key === "Backspace" || event.key === "Delete") {
|
|
89
|
+
if (!event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey) {
|
|
90
|
+
this.#options.onClear?.();
|
|
91
|
+
this.#options.onRecord("");
|
|
92
|
+
this.stop();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (require_parse.isModifierKey(event)) return;
|
|
97
|
+
const finalHotkey = require_parse.convertToModFormat(require_parse.keyboardEventToHotkey(event), this.#platform);
|
|
98
|
+
if (require_parse.hasNonModifierKey(finalHotkey, this.#platform)) {
|
|
99
|
+
const handlerToRemove = this.#keydownHandler;
|
|
100
|
+
if (handlerToRemove) {
|
|
101
|
+
this.#removeListener(handlerToRemove);
|
|
102
|
+
this.#keydownHandler = null;
|
|
103
|
+
}
|
|
104
|
+
this.store.setState(() => ({
|
|
105
|
+
isRecording: false,
|
|
106
|
+
recordedHotkey: finalHotkey
|
|
107
|
+
}));
|
|
108
|
+
this.#options.onRecord(finalHotkey);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
this.#keydownHandler = handler;
|
|
112
|
+
this.#addListener(handler);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Stop recording (same as cancel, but doesn't call onCancel).
|
|
116
|
+
*
|
|
117
|
+
* Removes the event listener and resets the recording state.
|
|
118
|
+
*/
|
|
119
|
+
stop() {
|
|
120
|
+
if (this.#keydownHandler) {
|
|
121
|
+
this.#removeListener(this.#keydownHandler);
|
|
122
|
+
this.#keydownHandler = null;
|
|
123
|
+
}
|
|
124
|
+
this.store.setState(() => ({
|
|
125
|
+
isRecording: false,
|
|
126
|
+
recordedHotkey: null
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Cancel recording without saving.
|
|
131
|
+
*
|
|
132
|
+
* Removes the event listener, resets the recording state, and calls
|
|
133
|
+
* the onCancel callback if provided.
|
|
134
|
+
*/
|
|
135
|
+
cancel() {
|
|
136
|
+
if (this.#keydownHandler) {
|
|
137
|
+
this.#removeListener(this.#keydownHandler);
|
|
138
|
+
this.#keydownHandler = null;
|
|
139
|
+
}
|
|
140
|
+
this.store.setState(() => ({
|
|
141
|
+
isRecording: false,
|
|
142
|
+
recordedHotkey: null
|
|
143
|
+
}));
|
|
144
|
+
this.#options.onCancel?.();
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Adds the keydown event listener to the document.
|
|
148
|
+
*/
|
|
149
|
+
#addListener(handler) {
|
|
150
|
+
if (typeof document === "undefined") return;
|
|
151
|
+
document.addEventListener("keydown", handler, true);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Removes the keydown event listener from the document.
|
|
155
|
+
*/
|
|
156
|
+
#removeListener(handler) {
|
|
157
|
+
if (typeof document === "undefined") return;
|
|
158
|
+
document.removeEventListener("keydown", handler, true);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Clean up event listeners and reset state.
|
|
162
|
+
*
|
|
163
|
+
* Call this when you're done with the recorder to ensure
|
|
164
|
+
* all event listeners are properly removed.
|
|
165
|
+
*/
|
|
166
|
+
destroy() {
|
|
167
|
+
this.stop();
|
|
168
|
+
this.store.setState(() => ({
|
|
169
|
+
isRecording: false,
|
|
170
|
+
recordedHotkey: null
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
//#endregion
|
|
176
|
+
exports.HotkeyRecorder = HotkeyRecorder;
|
|
177
|
+
//# sourceMappingURL=recorder.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recorder.cjs","names":["Store","#options","#platform","detectPlatform","#keydownHandler","isModifierKey","convertToModFormat","keyboardEventToHotkey","hasNonModifierKey","#removeListener","#addListener"],"sources":["../src/recorder.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { detectPlatform } from './constants'\nimport {\n convertToModFormat,\n hasNonModifierKey,\n isModifierKey,\n keyboardEventToHotkey,\n} from './parse'\nimport type { Hotkey } from './hotkey'\n\n/**\n * State interface for the HotkeyRecorder.\n */\nexport interface HotkeyRecorderState {\n /** Whether recording is currently active */\n isRecording: boolean\n /** The currently recorded hotkey (for live preview) */\n recordedHotkey: Hotkey | null\n}\n\n/**\n * Options for configuring a HotkeyRecorder instance.\n */\nexport interface HotkeyRecorderOptions {\n /** Callback when a hotkey is successfully recorded */\n onRecord: (hotkey: Hotkey) => void\n /** Optional callback when recording is cancelled (Escape pressed) */\n onCancel?: () => void\n /** Optional callback when shortcut is cleared (Backspace/Delete pressed) */\n onClear?: () => void\n}\n\n/**\n * Framework-agnostic class for recording keyboard shortcuts.\n *\n * This class handles all the complexity of capturing keyboard events,\n * converting them to hotkey strings, and handling edge cases like\n * Escape to cancel or Backspace/Delete to clear.\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - State can be accessed via `recorder.store.state` when using the class directly\n * - When using framework adapters (React), use `useStore` hooks for reactive state\n *\n * @example\n * ```ts\n * const recorder = new HotkeyRecorder({\n * onRecord: (hotkey) => {\n * console.log('Recorded:', hotkey)\n * },\n * onCancel: () => {\n * console.log('Recording cancelled')\n * },\n * })\n *\n * // Start recording\n * recorder.start()\n *\n * // Access state directly\n * console.log(recorder.store.state.isRecording) // true\n *\n * // Subscribe to changes with TanStack Store\n * const unsubscribe = recorder.store.subscribe(() => {\n * console.log('Recording:', recorder.store.state.isRecording)\n * })\n *\n * // Cleanup\n * recorder.destroy()\n * unsubscribe()\n * ```\n */\nexport class HotkeyRecorder {\n /**\n * The TanStack Store instance containing the recorder state.\n * Use this to subscribe to state changes or access current state.\n */\n readonly store: Store<HotkeyRecorderState> = new Store<HotkeyRecorderState>({\n isRecording: false,\n recordedHotkey: null,\n })\n\n #keydownHandler: ((event: KeyboardEvent) => void) | null = null\n #options: HotkeyRecorderOptions\n #platform: 'mac' | 'windows' | 'linux'\n\n constructor(options: HotkeyRecorderOptions) {\n this.#options = options\n this.#platform = detectPlatform()\n }\n\n /**\n * Updates the recorder options, including callbacks.\n * This allows framework adapters to sync callback changes without recreating the recorder.\n */\n setOptions(options: Partial<HotkeyRecorderOptions>): void {\n this.#options = {\n ...this.#options,\n ...options,\n }\n }\n\n /**\n * Start recording a new hotkey.\n *\n * Sets up a keydown event listener that captures keyboard events\n * and converts them to hotkey strings. Recording continues until\n * a valid hotkey is recorded, Escape is pressed, or stop/cancel is called.\n */\n start(): void {\n // Prevent starting recording if already recording\n if (this.#keydownHandler) {\n return\n }\n\n // Update store state\n this.store.setState(() => ({\n isRecording: true,\n recordedHotkey: null,\n }))\n\n // Create keydown handler\n const handler = (event: KeyboardEvent) => {\n // Check if we're still recording (handler might be called after stop/cancel)\n if (!this.#keydownHandler) {\n return\n }\n\n event.preventDefault()\n event.stopPropagation()\n\n // Handle Escape to cancel\n if (event.key === 'Escape') {\n this.cancel()\n return\n }\n\n // Handle Backspace/Delete to clear shortcut\n if (event.key === 'Backspace' || event.key === 'Delete') {\n if (\n !event.ctrlKey &&\n !event.shiftKey &&\n !event.altKey &&\n !event.metaKey\n ) {\n this.#options.onClear?.()\n this.#options.onRecord('' as Hotkey)\n this.stop()\n return\n }\n }\n\n // Ignore pure modifier keys (wait for a non-modifier key)\n if (isModifierKey(event)) {\n return\n }\n\n // Convert event to hotkey string using library function\n const hotkey = keyboardEventToHotkey(event)\n\n // Always convert to Mod format for portability\n const finalHotkey = convertToModFormat(hotkey, this.#platform)\n\n // Validate: must have at least one non-modifier key\n if (hasNonModifierKey(finalHotkey, this.#platform)) {\n // Remove listener FIRST to prevent any additional events\n const handlerToRemove = this.#keydownHandler as\n | ((event: KeyboardEvent) => void)\n | null\n if (handlerToRemove) {\n this.#removeListener(handlerToRemove)\n this.#keydownHandler = null\n }\n\n // Update store state immediately\n this.store.setState(() => ({\n isRecording: false,\n recordedHotkey: finalHotkey,\n }))\n\n // Call callback AFTER listener is removed and state is set\n this.#options.onRecord(finalHotkey)\n }\n }\n\n this.#keydownHandler = handler\n this.#addListener(handler)\n }\n\n /**\n * Stop recording (same as cancel, but doesn't call onCancel).\n *\n * Removes the event listener and resets the recording state.\n */\n stop(): void {\n // Remove event listener immediately\n if (this.#keydownHandler) {\n this.#removeListener(this.#keydownHandler)\n this.#keydownHandler = null\n }\n\n // Update store state\n this.store.setState(() => ({\n isRecording: false,\n recordedHotkey: null,\n }))\n }\n\n /**\n * Cancel recording without saving.\n *\n * Removes the event listener, resets the recording state, and calls\n * the onCancel callback if provided.\n */\n cancel(): void {\n // Remove event listener immediately\n if (this.#keydownHandler) {\n this.#removeListener(this.#keydownHandler)\n this.#keydownHandler = null\n }\n\n // Update store state\n this.store.setState(() => ({\n isRecording: false,\n recordedHotkey: null,\n }))\n\n // Call cancel callback\n this.#options.onCancel?.()\n }\n\n /**\n * Adds the keydown event listener to the document.\n */\n #addListener(handler: (event: KeyboardEvent) => void): void {\n if (typeof document === 'undefined') {\n return // SSR safety\n }\n\n document.addEventListener('keydown', handler, true)\n }\n\n /**\n * Removes the keydown event listener from the document.\n */\n #removeListener(handler: (event: KeyboardEvent) => void): void {\n if (typeof document === 'undefined') {\n return\n }\n\n document.removeEventListener('keydown', handler, true)\n }\n\n /**\n * Clean up event listeners and reset state.\n *\n * Call this when you're done with the recorder to ensure\n * all event listeners are properly removed.\n */\n destroy(): void {\n this.stop()\n this.store.setState(() => ({\n isRec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B,OAAa;AAEX,MAAI,MAAKN,gBAAiB;AACxB,SAAKK,eAAgB,MAAKL,eAAgB;AAC1C,SAAKA,iBAAkB;;AAIzB,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,gBAAgB;GACjB,EAAE;;;;;;;;CASL,SAAe;AAEb,MAAI,MAAKA,gBAAiB;AACxB,SAAKK,eAAgB,MAAKL,eAAgB;AAC1C,SAAKA,iBAAkB;;AAIzB,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,gBAAgB;GACjB,EAAE;AAGH,QAAKH,QAAS,YAAY;;;;;CAM5B,aAAa,SAA+C;AAC1D,MAAI,OAAO,aAAa,YACtB;AAGF,WAAS,iBAAiB,WAAW,SAAS,KAAK;;;;;CAMrD,gBAAgB,SAA+C;AAC7D,MAAI,OAAO,aAAa,YACtB;AAGF,WAAS,oBAAoB,WAAW,SAAS,KAAK;;;;;;;;CASxD,UAAgB;AACd,OAAK,MAAM;AACX,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,gBAAgB;GACjB,EAAE"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Hotkey } from "./hotkey.cjs";
|
|
2
|
+
import { Store } from "@tanstack/store";
|
|
3
|
+
|
|
4
|
+
//#region src/recorder.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* State interface for the HotkeyRecorder.
|
|
7
|
+
*/
|
|
8
|
+
interface HotkeyRecorderState {
|
|
9
|
+
/** Whether recording is currently active */
|
|
10
|
+
isRecording: boolean;
|
|
11
|
+
/** The currently recorded hotkey (for live preview) */
|
|
12
|
+
recordedHotkey: Hotkey | null;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Options for configuring a HotkeyRecorder instance.
|
|
16
|
+
*/
|
|
17
|
+
interface HotkeyRecorderOptions {
|
|
18
|
+
/** Callback when a hotkey is successfully recorded */
|
|
19
|
+
onRecord: (hotkey: Hotkey) => void;
|
|
20
|
+
/** Optional callback when recording is cancelled (Escape pressed) */
|
|
21
|
+
onCancel?: () => void;
|
|
22
|
+
/** Optional callback when shortcut is cleared (Backspace/Delete pressed) */
|
|
23
|
+
onClear?: () => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Framework-agnostic class for recording keyboard shortcuts.
|
|
27
|
+
*
|
|
28
|
+
* This class handles all the complexity of capturing keyboard events,
|
|
29
|
+
* converting them to hotkey strings, and handling edge cases like
|
|
30
|
+
* Escape to cancel or Backspace/Delete to clear.
|
|
31
|
+
*
|
|
32
|
+
* State Management:
|
|
33
|
+
* - Uses TanStack Store for reactive state management
|
|
34
|
+
* - State can be accessed via `recorder.store.state` when using the class directly
|
|
35
|
+
* - When using framework adapters (React), use `useStore` hooks for reactive state
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const recorder = new HotkeyRecorder({
|
|
40
|
+
* onRecord: (hotkey) => {
|
|
41
|
+
* console.log('Recorded:', hotkey)
|
|
42
|
+
* },
|
|
43
|
+
* onCancel: () => {
|
|
44
|
+
* console.log('Recording cancelled')
|
|
45
|
+
* },
|
|
46
|
+
* })
|
|
47
|
+
*
|
|
48
|
+
* // Start recording
|
|
49
|
+
* recorder.start()
|
|
50
|
+
*
|
|
51
|
+
* // Access state directly
|
|
52
|
+
* console.log(recorder.store.state.isRecording) // true
|
|
53
|
+
*
|
|
54
|
+
* // Subscribe to changes with TanStack Store
|
|
55
|
+
* const unsubscribe = recorder.store.subscribe(() => {
|
|
56
|
+
* console.log('Recording:', recorder.store.state.isRecording)
|
|
57
|
+
* })
|
|
58
|
+
*
|
|
59
|
+
* // Cleanup
|
|
60
|
+
* recorder.destroy()
|
|
61
|
+
* unsubscribe()
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
declare class HotkeyRecorder {
|
|
65
|
+
#private;
|
|
66
|
+
/**
|
|
67
|
+
* The TanStack Store instance containing the recorder state.
|
|
68
|
+
* Use this to subscribe to state changes or access current state.
|
|
69
|
+
*/
|
|
70
|
+
readonly store: Store<HotkeyRecorderState>;
|
|
71
|
+
constructor(options: HotkeyRecorderOptions);
|
|
72
|
+
/**
|
|
73
|
+
* Updates the recorder options, including callbacks.
|
|
74
|
+
* This allows framework adapters to sync callback changes without recreating the recorder.
|
|
75
|
+
*/
|
|
76
|
+
setOptions(options: Partial<HotkeyRecorderOptions>): void;
|
|
77
|
+
/**
|
|
78
|
+
* Start recording a new hotkey.
|
|
79
|
+
*
|
|
80
|
+
* Sets up a keydown event listener that captures keyboard events
|
|
81
|
+
* and converts them to hotkey strings. Recording continues until
|
|
82
|
+
* a valid hotkey is recorded, Escape is pressed, or stop/cancel is called.
|
|
83
|
+
*/
|
|
84
|
+
start(): void;
|
|
85
|
+
/**
|
|
86
|
+
* Stop recording (same as cancel, but doesn't call onCancel).
|
|
87
|
+
*
|
|
88
|
+
* Removes the event listener and resets the recording state.
|
|
89
|
+
*/
|
|
90
|
+
stop(): void;
|
|
91
|
+
/**
|
|
92
|
+
* Cancel recording without saving.
|
|
93
|
+
*
|
|
94
|
+
* Removes the event listener, resets the recording state, and calls
|
|
95
|
+
* the onCancel callback if provided.
|
|
96
|
+
*/
|
|
97
|
+
cancel(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Clean up event listeners and reset state.
|
|
100
|
+
*
|
|
101
|
+
* Call this when you're done with the recorder to ensure
|
|
102
|
+
* all event listeners are properly removed.
|
|
103
|
+
*/
|
|
104
|
+
destroy(): void;
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
export { HotkeyRecorder, HotkeyRecorderOptions, HotkeyRecorderState };
|
|
108
|
+
//# sourceMappingURL=recorder.d.cts.map
|