@tanstack/hotkeys 0.4.3 → 0.5.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/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/index.cjs +5 -2
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- package/dist/recorder-chord.cjs +17 -0
- package/dist/recorder-chord.cjs.map +1 -0
- package/dist/recorder-chord.js +17 -0
- package/dist/recorder-chord.js.map +1 -0
- package/dist/sequence-manager.cjs +23 -7
- 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 +23 -8
- package/dist/sequence-manager.js.map +1 -1
- package/package.json +1 -1
- package/src/{recorder.ts → hotkey-recorder.ts} +18 -32
- package/src/hotkey-sequence-recorder.ts +308 -0
- package/src/index.ts +2 -1
- package/src/recorder-chord.ts +26 -0
- package/src/sequence-manager.ts +28 -7
- package/dist/recorder.cjs.map +0 -1
- package/dist/recorder.js.map +0 -1
package/src/sequence-manager.ts
CHANGED
|
@@ -49,8 +49,9 @@ export type HotkeySequence = Array<Hotkey>
|
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
51
|
* Default timeout between keys in a sequence (in milliseconds).
|
|
52
|
+
* Exported for consumers that display sequence state (e.g. devtools).
|
|
52
53
|
*/
|
|
53
|
-
const DEFAULT_SEQUENCE_TIMEOUT = 1000
|
|
54
|
+
export const DEFAULT_SEQUENCE_TIMEOUT = 1000
|
|
54
55
|
|
|
55
56
|
let sequenceIdCounter = 0
|
|
56
57
|
|
|
@@ -70,7 +71,7 @@ function sequenceKey(sequence: HotkeySequence): string {
|
|
|
70
71
|
|
|
71
72
|
/**
|
|
72
73
|
* View of a sequence registration for devtools display.
|
|
73
|
-
*
|
|
74
|
+
* Progress fields reflect an in-progress match (between first key and completion or timeout).
|
|
74
75
|
*/
|
|
75
76
|
export interface SequenceRegistrationView {
|
|
76
77
|
id: string
|
|
@@ -78,6 +79,10 @@ export interface SequenceRegistrationView {
|
|
|
78
79
|
options: SequenceOptions
|
|
79
80
|
target: Target
|
|
80
81
|
triggerCount: number
|
|
82
|
+
/** Steps matched in the current attempt (0 when idle or just completed). */
|
|
83
|
+
matchedStepCount: number
|
|
84
|
+
/** `Date.now()` when the last step in the current attempt matched; 0 if none. */
|
|
85
|
+
partialMatchLastKeyTime: number
|
|
81
86
|
}
|
|
82
87
|
|
|
83
88
|
/**
|
|
@@ -147,12 +152,16 @@ export interface SequenceRegistrationHandle {
|
|
|
147
152
|
function toRegistrationView(
|
|
148
153
|
reg: SequenceRegistration,
|
|
149
154
|
): SequenceRegistrationView {
|
|
155
|
+
const len = reg.parsedSequence.length
|
|
156
|
+
const inProgress = reg.currentIndex > 0 && reg.currentIndex < len
|
|
150
157
|
return {
|
|
151
158
|
id: reg.id,
|
|
152
159
|
sequence: reg.sequence,
|
|
153
160
|
options: reg.options,
|
|
154
161
|
target: reg.target,
|
|
155
162
|
triggerCount: reg.triggerCount,
|
|
163
|
+
matchedStepCount: inProgress ? reg.currentIndex : 0,
|
|
164
|
+
partialMatchLastKeyTime: inProgress ? reg.lastKeyTime : 0,
|
|
156
165
|
}
|
|
157
166
|
}
|
|
158
167
|
|
|
@@ -201,6 +210,12 @@ export class SequenceManager {
|
|
|
201
210
|
}
|
|
202
211
|
}
|
|
203
212
|
|
|
213
|
+
#syncRegistrationView(registration: SequenceRegistration): void {
|
|
214
|
+
this.registrations.setState((prev) =>
|
|
215
|
+
new Map(prev).set(registration.id, toRegistrationView(registration)),
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
|
|
204
219
|
/**
|
|
205
220
|
* Registers a hotkey sequence handler.
|
|
206
221
|
*
|
|
@@ -467,6 +482,7 @@ export class SequenceManager {
|
|
|
467
482
|
now - registration.lastKeyTime > timeout
|
|
468
483
|
) {
|
|
469
484
|
registration.currentIndex = 0
|
|
485
|
+
this.#syncRegistrationView(registration)
|
|
470
486
|
}
|
|
471
487
|
|
|
472
488
|
const expectedHotkey =
|
|
@@ -501,9 +517,14 @@ export class SequenceManager {
|
|
|
501
517
|
]!,
|
|
502
518
|
}
|
|
503
519
|
|
|
504
|
-
registration.
|
|
505
|
-
|
|
520
|
+
registration.triggerCount++
|
|
506
521
|
registration.currentIndex = 0
|
|
522
|
+
|
|
523
|
+
this.#syncRegistrationView(registration)
|
|
524
|
+
|
|
525
|
+
registration.callback(event, context)
|
|
526
|
+
} else {
|
|
527
|
+
this.#syncRegistrationView(registration)
|
|
507
528
|
}
|
|
508
529
|
} else if (registration.currentIndex > 0) {
|
|
509
530
|
const firstHotkey = registration.parsedSequence[0]!
|
|
@@ -519,6 +540,7 @@ export class SequenceManager {
|
|
|
519
540
|
} else {
|
|
520
541
|
registration.currentIndex = 0
|
|
521
542
|
}
|
|
543
|
+
this.#syncRegistrationView(registration)
|
|
522
544
|
}
|
|
523
545
|
}
|
|
524
546
|
}
|
|
@@ -549,6 +571,7 @@ export class SequenceManager {
|
|
|
549
571
|
for (const registration of this.#registrations.values()) {
|
|
550
572
|
registration.currentIndex = 0
|
|
551
573
|
registration.lastKeyTime = 0
|
|
574
|
+
this.#syncRegistrationView(registration)
|
|
552
575
|
}
|
|
553
576
|
}
|
|
554
577
|
|
|
@@ -586,9 +609,7 @@ export class SequenceManager {
|
|
|
586
609
|
|
|
587
610
|
registration.triggerCount++
|
|
588
611
|
|
|
589
|
-
this
|
|
590
|
-
new Map(prev).set(id, toRegistrationView(registration)),
|
|
591
|
-
)
|
|
612
|
+
this.#syncRegistrationView(registration)
|
|
592
613
|
|
|
593
614
|
const context: HotkeyCallbackContext = {
|
|
594
615
|
hotkey: registration.sequence.join(' ') as Hotkey,
|
package/dist/recorder.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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 isRecording: false,\n recordedHotkey: null,\n }))\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,IAAa,iBAAb,MAA4B;CAU1B,kBAA2D;CAC3D;CACA;CAEA,YAAY,SAAgC;eATC,IAAIA,sBAA2B;GAC1E,aAAa;GACb,gBAAgB;GACjB,CAAC;AAOA,QAAKC,UAAW;AAChB,QAAKC,WAAYC,kCAAgB;;;;;;CAOnC,WAAW,SAA+C;AACxD,QAAKF,UAAW;GACd,GAAG,MAAKA;GACR,GAAG;GACJ;;;;;;;;;CAUH,QAAc;AAEZ,MAAI,MAAKG,eACP;AAIF,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,gBAAgB;GACjB,EAAE;EAGH,MAAM,WAAW,UAAyB;AAExC,OAAI,CAAC,MAAKA,eACR;AAGF,SAAM,gBAAgB;AACtB,SAAM,iBAAiB;AAGvB,OAAI,MAAM,QAAQ,UAAU;AAC1B,SAAK,QAAQ;AACb;;AAIF,OAAI,MAAM,QAAQ,eAAe,MAAM,QAAQ,UAC7C;QACE,CAAC,MAAM,WACP,CAAC,MAAM,YACP,CAAC,MAAM,UACP,CAAC,MAAM,SACP;AACA,WAAKH,QAAS,WAAW;AACzB,WAAKA,QAAS,SAAS,GAAa;AACpC,UAAK,MAAM;AACX;;;AAKJ,OAAII,4BAAc,MAAM,CACtB;GAOF,MAAM,cAAcC,iCAHLC,oCAAsB,MAAM,EAGI,MAAKL,SAAU;AAG9D,OAAIM,gCAAkB,aAAa,MAAKN,SAAU,EAAE;IAElD,MAAM,kBAAkB,MAAKE;AAG7B,QAAI,iBAAiB;AACnB,WAAKK,eAAgB,gBAAgB;AACrC,WAAKL,iBAAkB;;AAIzB,SAAK,MAAM,gBAAgB;KACzB,aAAa;KACb,gBAAgB;KACjB,EAAE;AAGH,UAAKH,QAAS,SAAS,YAAY;;;AAIvC,QAAKG,iBAAkB;AACvB,QAAKM,YAAa,QAAQ;;;;;;;CAQ5B,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"}
|
package/dist/recorder.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"recorder.js","names":["#options","#platform","#keydownHandler","#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 isRecording: false,\n recordedHotkey: null,\n }))\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,IAAa,iBAAb,MAA4B;CAU1B,kBAA2D;CAC3D;CACA;CAEA,YAAY,SAAgC;eATC,IAAI,MAA2B;GAC1E,aAAa;GACb,gBAAgB;GACjB,CAAC;AAOA,QAAKA,UAAW;AAChB,QAAKC,WAAY,gBAAgB;;;;;;CAOnC,WAAW,SAA+C;AACxD,QAAKD,UAAW;GACd,GAAG,MAAKA;GACR,GAAG;GACJ;;;;;;;;;CAUH,QAAc;AAEZ,MAAI,MAAKE,eACP;AAIF,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,gBAAgB;GACjB,EAAE;EAGH,MAAM,WAAW,UAAyB;AAExC,OAAI,CAAC,MAAKA,eACR;AAGF,SAAM,gBAAgB;AACtB,SAAM,iBAAiB;AAGvB,OAAI,MAAM,QAAQ,UAAU;AAC1B,SAAK,QAAQ;AACb;;AAIF,OAAI,MAAM,QAAQ,eAAe,MAAM,QAAQ,UAC7C;QACE,CAAC,MAAM,WACP,CAAC,MAAM,YACP,CAAC,MAAM,UACP,CAAC,MAAM,SACP;AACA,WAAKF,QAAS,WAAW;AACzB,WAAKA,QAAS,SAAS,GAAa;AACpC,UAAK,MAAM;AACX;;;AAKJ,OAAI,cAAc,MAAM,CACtB;GAOF,MAAM,cAAc,mBAHL,sBAAsB,MAAM,EAGI,MAAKC,SAAU;AAG9D,OAAI,kBAAkB,aAAa,MAAKA,SAAU,EAAE;IAElD,MAAM,kBAAkB,MAAKC;AAG7B,QAAI,iBAAiB;AACnB,WAAKC,eAAgB,gBAAgB;AACrC,WAAKD,iBAAkB;;AAIzB,SAAK,MAAM,gBAAgB;KACzB,aAAa;KACb,gBAAgB;KACjB,EAAE;AAGH,UAAKF,QAAS,SAAS,YAAY;;;AAIvC,QAAKE,iBAAkB;AACvB,QAAKE,YAAa,QAAQ;;;;;;;CAQ5B,OAAa;AAEX,MAAI,MAAKF,gBAAiB;AACxB,SAAKC,eAAgB,MAAKD,eAAgB;AAC1C,SAAKA,iBAAkB;;AAIzB,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,gBAAgB;GACjB,EAAE;;;;;;;;CASL,SAAe;AAEb,MAAI,MAAKA,gBAAiB;AACxB,SAAKC,eAAgB,MAAKD,eAAgB;AAC1C,SAAKA,iBAAkB;;AAIzB,OAAK,MAAM,gBAAgB;GACzB,aAAa;GACb,gBAAgB;GACjB,EAAE;AAGH,QAAKF,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"}
|