@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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +121 -45
  3. package/dist/constants.cjs +444 -0
  4. package/dist/constants.cjs.map +1 -0
  5. package/dist/constants.d.cts +226 -0
  6. package/dist/constants.d.ts +226 -0
  7. package/dist/constants.js +428 -0
  8. package/dist/constants.js.map +1 -0
  9. package/dist/format.cjs +178 -0
  10. package/dist/format.cjs.map +1 -0
  11. package/dist/format.d.cts +110 -0
  12. package/dist/format.d.ts +110 -0
  13. package/dist/format.js +175 -0
  14. package/dist/format.js.map +1 -0
  15. package/dist/hotkey-manager.cjs +420 -0
  16. package/dist/hotkey-manager.cjs.map +1 -0
  17. package/dist/hotkey-manager.d.cts +207 -0
  18. package/dist/hotkey-manager.d.ts +207 -0
  19. package/dist/hotkey-manager.js +419 -0
  20. package/dist/hotkey-manager.js.map +1 -0
  21. package/dist/hotkey.d.cts +278 -0
  22. package/dist/hotkey.d.ts +278 -0
  23. package/dist/index.cjs +54 -0
  24. package/dist/index.d.cts +11 -0
  25. package/dist/index.d.ts +11 -0
  26. package/dist/index.js +11 -0
  27. package/dist/key-state-tracker.cjs +197 -0
  28. package/dist/key-state-tracker.cjs.map +1 -0
  29. package/dist/key-state-tracker.d.cts +107 -0
  30. package/dist/key-state-tracker.d.ts +107 -0
  31. package/dist/key-state-tracker.js +196 -0
  32. package/dist/key-state-tracker.js.map +1 -0
  33. package/dist/match.cjs +143 -0
  34. package/dist/match.cjs.map +1 -0
  35. package/dist/match.d.cts +79 -0
  36. package/dist/match.d.ts +79 -0
  37. package/dist/match.js +141 -0
  38. package/dist/match.js.map +1 -0
  39. package/dist/parse.cjs +266 -0
  40. package/dist/parse.cjs.map +1 -0
  41. package/dist/parse.d.cts +169 -0
  42. package/dist/parse.d.ts +169 -0
  43. package/dist/parse.js +258 -0
  44. package/dist/parse.js.map +1 -0
  45. package/dist/recorder.cjs +177 -0
  46. package/dist/recorder.cjs.map +1 -0
  47. package/dist/recorder.d.cts +108 -0
  48. package/dist/recorder.d.ts +108 -0
  49. package/dist/recorder.js +177 -0
  50. package/dist/recorder.js.map +1 -0
  51. package/dist/sequence.cjs +242 -0
  52. package/dist/sequence.cjs.map +1 -0
  53. package/dist/sequence.d.cts +109 -0
  54. package/dist/sequence.d.ts +109 -0
  55. package/dist/sequence.js +240 -0
  56. package/dist/sequence.js.map +1 -0
  57. package/dist/validate.cjs +116 -0
  58. package/dist/validate.cjs.map +1 -0
  59. package/dist/validate.d.cts +56 -0
  60. package/dist/validate.d.ts +56 -0
  61. package/dist/validate.js +114 -0
  62. package/dist/validate.js.map +1 -0
  63. package/package.json +55 -7
  64. package/src/constants.ts +514 -0
  65. package/src/format.ts +261 -0
  66. package/src/hotkey-manager.ts +822 -0
  67. package/src/hotkey.ts +411 -0
  68. package/src/index.ts +10 -0
  69. package/src/key-state-tracker.ts +249 -0
  70. package/src/match.ts +222 -0
  71. package/src/parse.ts +368 -0
  72. package/src/recorder.ts +266 -0
  73. package/src/sequence.ts +391 -0
  74. package/src/validate.ts +171 -0
@@ -0,0 +1,207 @@
1
+ import { Hotkey, HotkeyCallback, ParsedHotkey, RegisterableHotkey } from "./hotkey.cjs";
2
+ import { Store } from "@tanstack/store";
3
+
4
+ //#region src/hotkey-manager.d.ts
5
+ /**
6
+ * Behavior when registering a hotkey that conflicts with an existing registration.
7
+ *
8
+ * - `'warn'` - Log a warning to the console but allow both registrations (default)
9
+ * - `'error'` - Throw an error and prevent the new registration
10
+ * - `'replace'` - Unregister the existing hotkey and register the new one
11
+ * - `'allow'` - Allow multiple registrations of the same hotkey without warning
12
+ */
13
+ type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow';
14
+ /**
15
+ * Options for registering a hotkey.
16
+ */
17
+ interface HotkeyOptions {
18
+ /** Behavior when this hotkey conflicts with an existing registration on the same target. Defaults to 'warn' */
19
+ conflictBehavior?: ConflictBehavior;
20
+ /** Whether the hotkey is enabled. Defaults to true */
21
+ enabled?: boolean;
22
+ /** The event type to listen for. Defaults to 'keydown' */
23
+ eventType?: 'keydown' | 'keyup';
24
+ /** Whether to ignore hotkeys when keyboard events originate from input-like elements (text inputs, textarea, select, contenteditable — button-type inputs like type=button/submit/reset are not ignored). Defaults based on hotkey: true for single keys and Shift/Alt combos; false for Ctrl/Meta shortcuts and Escape */
25
+ ignoreInputs?: boolean;
26
+ /** The target platform for resolving 'Mod' */
27
+ platform?: 'mac' | 'windows' | 'linux';
28
+ /** Prevent the default browser action when the hotkey matches. Defaults to true */
29
+ preventDefault?: boolean;
30
+ /** If true, only trigger once until all keys are released. Default: false */
31
+ requireReset?: boolean;
32
+ /** Stop event propagation when the hotkey matches. Defaults to true */
33
+ stopPropagation?: boolean;
34
+ /** The DOM element to attach the event listener to. Defaults to document. */
35
+ target?: HTMLElement | Document | Window | null;
36
+ }
37
+ /**
38
+ * A registered hotkey handler in the HotkeyManager.
39
+ */
40
+ interface HotkeyRegistration {
41
+ /** The callback to invoke */
42
+ callback: HotkeyCallback;
43
+ /** Whether this registration has fired and needs reset (for requireReset) */
44
+ hasFired: boolean;
45
+ /** The original hotkey string */
46
+ hotkey: Hotkey;
47
+ /** Unique identifier for this registration */
48
+ id: string;
49
+ /** Options for this registration */
50
+ options: HotkeyOptions;
51
+ /** The parsed hotkey */
52
+ parsedHotkey: ParsedHotkey;
53
+ /** The resolved target element for this registration */
54
+ target: HTMLElement | Document | Window;
55
+ /** How many times this registration's callback has been triggered */
56
+ triggerCount: number;
57
+ }
58
+ /**
59
+ * A handle returned from HotkeyManager.register() that allows updating
60
+ * the callback and options without re-registering the hotkey.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const handle = manager.register('Mod+S', callback, options)
65
+ *
66
+ * // Update callback without re-registering (avoids stale closures)
67
+ * handle.callback = newCallback
68
+ *
69
+ * // Update options without re-registering
70
+ * handle.setOptions({ enabled: false })
71
+ *
72
+ * // Check if still active
73
+ * if (handle.isActive) {
74
+ * // ...
75
+ * }
76
+ *
77
+ * // Unregister when done
78
+ * handle.unregister()
79
+ * ```
80
+ */
81
+ interface HotkeyRegistrationHandle {
82
+ /**
83
+ * The callback function. Can be set directly to update without re-registering.
84
+ * This avoids stale closures when the callback references React state.
85
+ */
86
+ callback: HotkeyCallback;
87
+ /** Unique identifier for this registration */
88
+ readonly id: string;
89
+ /** Check if this registration is still active (not unregistered) */
90
+ readonly isActive: boolean;
91
+ /**
92
+ * Update options (merged with existing options).
93
+ * Useful for updating `enabled`, `preventDefault`, etc. without re-registering.
94
+ */
95
+ setOptions: (options: Partial<HotkeyOptions>) => void;
96
+ /** Unregister this hotkey */
97
+ unregister: () => void;
98
+ }
99
+ /**
100
+ * Singleton manager for hotkey registrations.
101
+ *
102
+ * This class provides a centralized way to register and manage keyboard hotkeys.
103
+ * It uses a single event listener for efficiency, regardless of how many hotkeys
104
+ * are registered.
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * const manager = HotkeyManager.getInstance()
109
+ *
110
+ * const unregister = manager.register('Mod+S', (event, context) => {
111
+ * console.log('Save triggered!')
112
+ * })
113
+ *
114
+ * // Later, to unregister:
115
+ * unregister()
116
+ * ```
117
+ */
118
+ declare class HotkeyManager {
119
+ #private;
120
+ /**
121
+ * The TanStack Store containing all hotkey registrations.
122
+ * Use this to subscribe to registration changes or access current registrations.
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * const manager = HotkeyManager.getInstance()
127
+ *
128
+ * // Subscribe to registration changes
129
+ * const unsubscribe = manager.registrations.subscribe(() => {
130
+ * console.log('Registrations changed:', manager.registrations.state.size)
131
+ * })
132
+ *
133
+ * // Access current registrations
134
+ * for (const [id, reg] of manager.registrations.state) {
135
+ * console.log(reg.hotkey, reg.options.enabled)
136
+ * }
137
+ * ```
138
+ */
139
+ readonly registrations: Store<Map<string, HotkeyRegistration>>;
140
+ private constructor();
141
+ /**
142
+ * Gets the singleton instance of HotkeyManager.
143
+ */
144
+ static getInstance(): HotkeyManager;
145
+ /**
146
+ * Resets the singleton instance. Useful for testing.
147
+ */
148
+ static resetInstance(): void;
149
+ /**
150
+ * Registers a hotkey handler and returns a handle for updating the registration.
151
+ *
152
+ * The returned handle allows updating the callback and options without
153
+ * re-registering, which is useful for avoiding stale closures in React.
154
+ *
155
+ * @param hotkey - The hotkey string (e.g., 'Mod+S') or RawHotkey object
156
+ * @param callback - The function to call when the hotkey is pressed
157
+ * @param options - Options for the hotkey behavior
158
+ * @returns A handle for managing the registration
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * const handle = manager.register('Mod+S', callback)
163
+ *
164
+ * // Update callback without re-registering (avoids stale closures)
165
+ * handle.callback = newCallback
166
+ *
167
+ * // Update options
168
+ * handle.setOptions({ enabled: false })
169
+ *
170
+ * // Unregister when done
171
+ * handle.unregister()
172
+ * ```
173
+ */
174
+ register(hotkey: RegisterableHotkey, callback: HotkeyCallback, options?: HotkeyOptions): HotkeyRegistrationHandle;
175
+ /**
176
+ * Triggers a registration's callback programmatically from devtools.
177
+ * Creates a synthetic KeyboardEvent and invokes the callback.
178
+ *
179
+ * @param id - The registration ID to trigger
180
+ * @returns True if the registration was found and triggered
181
+ */
182
+ triggerRegistration(id: string): boolean;
183
+ /**
184
+ * Gets the number of registered hotkeys.
185
+ */
186
+ getRegistrationCount(): number;
187
+ /**
188
+ * Checks if a specific hotkey is registered.
189
+ *
190
+ * @param hotkey - The hotkey string to check
191
+ * @param target - Optional target element to match (if provided, both hotkey and target must match)
192
+ * @returns True if a matching registration exists
193
+ */
194
+ isRegistered(hotkey: Hotkey, target?: HTMLElement | Document | Window): boolean;
195
+ /**
196
+ * Destroys the manager and removes all listeners.
197
+ */
198
+ destroy(): void;
199
+ }
200
+ /**
201
+ * Gets the singleton HotkeyManager instance.
202
+ * Convenience function for accessing the manager.
203
+ */
204
+ declare function getHotkeyManager(): HotkeyManager;
205
+ //#endregion
206
+ export { ConflictBehavior, HotkeyManager, HotkeyOptions, HotkeyRegistration, HotkeyRegistrationHandle, getHotkeyManager };
207
+ //# sourceMappingURL=hotkey-manager.d.cts.map
@@ -0,0 +1,207 @@
1
+ import { Hotkey, HotkeyCallback, ParsedHotkey, RegisterableHotkey } from "./hotkey.js";
2
+ import { Store } from "@tanstack/store";
3
+
4
+ //#region src/hotkey-manager.d.ts
5
+ /**
6
+ * Behavior when registering a hotkey that conflicts with an existing registration.
7
+ *
8
+ * - `'warn'` - Log a warning to the console but allow both registrations (default)
9
+ * - `'error'` - Throw an error and prevent the new registration
10
+ * - `'replace'` - Unregister the existing hotkey and register the new one
11
+ * - `'allow'` - Allow multiple registrations of the same hotkey without warning
12
+ */
13
+ type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow';
14
+ /**
15
+ * Options for registering a hotkey.
16
+ */
17
+ interface HotkeyOptions {
18
+ /** Behavior when this hotkey conflicts with an existing registration on the same target. Defaults to 'warn' */
19
+ conflictBehavior?: ConflictBehavior;
20
+ /** Whether the hotkey is enabled. Defaults to true */
21
+ enabled?: boolean;
22
+ /** The event type to listen for. Defaults to 'keydown' */
23
+ eventType?: 'keydown' | 'keyup';
24
+ /** Whether to ignore hotkeys when keyboard events originate from input-like elements (text inputs, textarea, select, contenteditable — button-type inputs like type=button/submit/reset are not ignored). Defaults based on hotkey: true for single keys and Shift/Alt combos; false for Ctrl/Meta shortcuts and Escape */
25
+ ignoreInputs?: boolean;
26
+ /** The target platform for resolving 'Mod' */
27
+ platform?: 'mac' | 'windows' | 'linux';
28
+ /** Prevent the default browser action when the hotkey matches. Defaults to true */
29
+ preventDefault?: boolean;
30
+ /** If true, only trigger once until all keys are released. Default: false */
31
+ requireReset?: boolean;
32
+ /** Stop event propagation when the hotkey matches. Defaults to true */
33
+ stopPropagation?: boolean;
34
+ /** The DOM element to attach the event listener to. Defaults to document. */
35
+ target?: HTMLElement | Document | Window | null;
36
+ }
37
+ /**
38
+ * A registered hotkey handler in the HotkeyManager.
39
+ */
40
+ interface HotkeyRegistration {
41
+ /** The callback to invoke */
42
+ callback: HotkeyCallback;
43
+ /** Whether this registration has fired and needs reset (for requireReset) */
44
+ hasFired: boolean;
45
+ /** The original hotkey string */
46
+ hotkey: Hotkey;
47
+ /** Unique identifier for this registration */
48
+ id: string;
49
+ /** Options for this registration */
50
+ options: HotkeyOptions;
51
+ /** The parsed hotkey */
52
+ parsedHotkey: ParsedHotkey;
53
+ /** The resolved target element for this registration */
54
+ target: HTMLElement | Document | Window;
55
+ /** How many times this registration's callback has been triggered */
56
+ triggerCount: number;
57
+ }
58
+ /**
59
+ * A handle returned from HotkeyManager.register() that allows updating
60
+ * the callback and options without re-registering the hotkey.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const handle = manager.register('Mod+S', callback, options)
65
+ *
66
+ * // Update callback without re-registering (avoids stale closures)
67
+ * handle.callback = newCallback
68
+ *
69
+ * // Update options without re-registering
70
+ * handle.setOptions({ enabled: false })
71
+ *
72
+ * // Check if still active
73
+ * if (handle.isActive) {
74
+ * // ...
75
+ * }
76
+ *
77
+ * // Unregister when done
78
+ * handle.unregister()
79
+ * ```
80
+ */
81
+ interface HotkeyRegistrationHandle {
82
+ /**
83
+ * The callback function. Can be set directly to update without re-registering.
84
+ * This avoids stale closures when the callback references React state.
85
+ */
86
+ callback: HotkeyCallback;
87
+ /** Unique identifier for this registration */
88
+ readonly id: string;
89
+ /** Check if this registration is still active (not unregistered) */
90
+ readonly isActive: boolean;
91
+ /**
92
+ * Update options (merged with existing options).
93
+ * Useful for updating `enabled`, `preventDefault`, etc. without re-registering.
94
+ */
95
+ setOptions: (options: Partial<HotkeyOptions>) => void;
96
+ /** Unregister this hotkey */
97
+ unregister: () => void;
98
+ }
99
+ /**
100
+ * Singleton manager for hotkey registrations.
101
+ *
102
+ * This class provides a centralized way to register and manage keyboard hotkeys.
103
+ * It uses a single event listener for efficiency, regardless of how many hotkeys
104
+ * are registered.
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * const manager = HotkeyManager.getInstance()
109
+ *
110
+ * const unregister = manager.register('Mod+S', (event, context) => {
111
+ * console.log('Save triggered!')
112
+ * })
113
+ *
114
+ * // Later, to unregister:
115
+ * unregister()
116
+ * ```
117
+ */
118
+ declare class HotkeyManager {
119
+ #private;
120
+ /**
121
+ * The TanStack Store containing all hotkey registrations.
122
+ * Use this to subscribe to registration changes or access current registrations.
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * const manager = HotkeyManager.getInstance()
127
+ *
128
+ * // Subscribe to registration changes
129
+ * const unsubscribe = manager.registrations.subscribe(() => {
130
+ * console.log('Registrations changed:', manager.registrations.state.size)
131
+ * })
132
+ *
133
+ * // Access current registrations
134
+ * for (const [id, reg] of manager.registrations.state) {
135
+ * console.log(reg.hotkey, reg.options.enabled)
136
+ * }
137
+ * ```
138
+ */
139
+ readonly registrations: Store<Map<string, HotkeyRegistration>>;
140
+ private constructor();
141
+ /**
142
+ * Gets the singleton instance of HotkeyManager.
143
+ */
144
+ static getInstance(): HotkeyManager;
145
+ /**
146
+ * Resets the singleton instance. Useful for testing.
147
+ */
148
+ static resetInstance(): void;
149
+ /**
150
+ * Registers a hotkey handler and returns a handle for updating the registration.
151
+ *
152
+ * The returned handle allows updating the callback and options without
153
+ * re-registering, which is useful for avoiding stale closures in React.
154
+ *
155
+ * @param hotkey - The hotkey string (e.g., 'Mod+S') or RawHotkey object
156
+ * @param callback - The function to call when the hotkey is pressed
157
+ * @param options - Options for the hotkey behavior
158
+ * @returns A handle for managing the registration
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * const handle = manager.register('Mod+S', callback)
163
+ *
164
+ * // Update callback without re-registering (avoids stale closures)
165
+ * handle.callback = newCallback
166
+ *
167
+ * // Update options
168
+ * handle.setOptions({ enabled: false })
169
+ *
170
+ * // Unregister when done
171
+ * handle.unregister()
172
+ * ```
173
+ */
174
+ register(hotkey: RegisterableHotkey, callback: HotkeyCallback, options?: HotkeyOptions): HotkeyRegistrationHandle;
175
+ /**
176
+ * Triggers a registration's callback programmatically from devtools.
177
+ * Creates a synthetic KeyboardEvent and invokes the callback.
178
+ *
179
+ * @param id - The registration ID to trigger
180
+ * @returns True if the registration was found and triggered
181
+ */
182
+ triggerRegistration(id: string): boolean;
183
+ /**
184
+ * Gets the number of registered hotkeys.
185
+ */
186
+ getRegistrationCount(): number;
187
+ /**
188
+ * Checks if a specific hotkey is registered.
189
+ *
190
+ * @param hotkey - The hotkey string to check
191
+ * @param target - Optional target element to match (if provided, both hotkey and target must match)
192
+ * @returns True if a matching registration exists
193
+ */
194
+ isRegistered(hotkey: Hotkey, target?: HTMLElement | Document | Window): boolean;
195
+ /**
196
+ * Destroys the manager and removes all listeners.
197
+ */
198
+ destroy(): void;
199
+ }
200
+ /**
201
+ * Gets the singleton HotkeyManager instance.
202
+ * Convenience function for accessing the manager.
203
+ */
204
+ declare function getHotkeyManager(): HotkeyManager;
205
+ //#endregion
206
+ export { ConflictBehavior, HotkeyManager, HotkeyOptions, HotkeyRegistration, HotkeyRegistrationHandle, getHotkeyManager };
207
+ //# sourceMappingURL=hotkey-manager.d.ts.map