@tanstack/hotkeys 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +121 -45
- package/dist/constants.cjs +444 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +226 -0
- package/dist/constants.d.ts +226 -0
- package/dist/constants.js +428 -0
- package/dist/constants.js.map +1 -0
- package/dist/format.cjs +178 -0
- package/dist/format.cjs.map +1 -0
- package/dist/format.d.cts +110 -0
- package/dist/format.d.ts +110 -0
- package/dist/format.js +175 -0
- package/dist/format.js.map +1 -0
- package/dist/hotkey-manager.cjs +420 -0
- package/dist/hotkey-manager.cjs.map +1 -0
- package/dist/hotkey-manager.d.cts +207 -0
- package/dist/hotkey-manager.d.ts +207 -0
- package/dist/hotkey-manager.js +419 -0
- package/dist/hotkey-manager.js.map +1 -0
- package/dist/hotkey.d.cts +278 -0
- package/dist/hotkey.d.ts +278 -0
- package/dist/index.cjs +54 -0
- package/dist/index.d.cts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/key-state-tracker.cjs +197 -0
- package/dist/key-state-tracker.cjs.map +1 -0
- package/dist/key-state-tracker.d.cts +107 -0
- package/dist/key-state-tracker.d.ts +107 -0
- package/dist/key-state-tracker.js +196 -0
- package/dist/key-state-tracker.js.map +1 -0
- package/dist/match.cjs +143 -0
- package/dist/match.cjs.map +1 -0
- package/dist/match.d.cts +79 -0
- package/dist/match.d.ts +79 -0
- package/dist/match.js +141 -0
- package/dist/match.js.map +1 -0
- package/dist/parse.cjs +266 -0
- package/dist/parse.cjs.map +1 -0
- package/dist/parse.d.cts +169 -0
- package/dist/parse.d.ts +169 -0
- package/dist/parse.js +258 -0
- package/dist/parse.js.map +1 -0
- package/dist/recorder.cjs +177 -0
- package/dist/recorder.cjs.map +1 -0
- package/dist/recorder.d.cts +108 -0
- package/dist/recorder.d.ts +108 -0
- package/dist/recorder.js +177 -0
- package/dist/recorder.js.map +1 -0
- package/dist/sequence.cjs +242 -0
- package/dist/sequence.cjs.map +1 -0
- package/dist/sequence.d.cts +109 -0
- package/dist/sequence.d.ts +109 -0
- package/dist/sequence.js +240 -0
- package/dist/sequence.js.map +1 -0
- package/dist/validate.cjs +116 -0
- package/dist/validate.cjs.map +1 -0
- package/dist/validate.d.cts +56 -0
- package/dist/validate.d.ts +56 -0
- package/dist/validate.js +114 -0
- package/dist/validate.js.map +1 -0
- package/package.json +55 -7
- package/src/constants.ts +514 -0
- package/src/format.ts +261 -0
- package/src/hotkey-manager.ts +822 -0
- package/src/hotkey.ts +411 -0
- package/src/index.ts +10 -0
- package/src/key-state-tracker.ts +249 -0
- package/src/match.ts +222 -0
- package/src/parse.ts +368 -0
- package/src/recorder.ts +266 -0
- package/src/sequence.ts +391 -0
- package/src/validate.ts +171 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Hotkey } from "./hotkey.js";
|
|
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.ts.map
|
package/dist/recorder.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { detectPlatform } from "./constants.js";
|
|
2
|
+
import { convertToModFormat, hasNonModifierKey, isModifierKey, keyboardEventToHotkey } from "./parse.js";
|
|
3
|
+
import { Store } from "@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 Store({
|
|
51
|
+
isRecording: false,
|
|
52
|
+
recordedHotkey: null
|
|
53
|
+
});
|
|
54
|
+
this.#options = options;
|
|
55
|
+
this.#platform = 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 (isModifierKey(event)) return;
|
|
97
|
+
const finalHotkey = convertToModFormat(keyboardEventToHotkey(event), this.#platform);
|
|
98
|
+
if (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
|
+
export { HotkeyRecorder };
|
|
177
|
+
//# sourceMappingURL=recorder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
const require_constants = require('./constants.cjs');
|
|
2
|
+
const require_parse = require('./parse.cjs');
|
|
3
|
+
const require_match = require('./match.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/sequence.ts
|
|
6
|
+
/**
|
|
7
|
+
* Default timeout between keys in a sequence (in milliseconds).
|
|
8
|
+
*/
|
|
9
|
+
const DEFAULT_SEQUENCE_TIMEOUT = 1e3;
|
|
10
|
+
let sequenceIdCounter = 0;
|
|
11
|
+
/**
|
|
12
|
+
* Generates a unique ID for sequence registrations.
|
|
13
|
+
*/
|
|
14
|
+
function generateSequenceId() {
|
|
15
|
+
return `sequence_${++sequenceIdCounter}`;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Manages keyboard sequence matching for Vim-style shortcuts.
|
|
19
|
+
*
|
|
20
|
+
* This class allows registering multi-key sequences like 'g g' or 'd d'
|
|
21
|
+
* that trigger callbacks when the full sequence is pressed within
|
|
22
|
+
* a configurable timeout.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const matcher = SequenceManager.getInstance()
|
|
27
|
+
*
|
|
28
|
+
* // Register 'g g' to go to top
|
|
29
|
+
* const unregister = matcher.register(['G', 'G'], (event, context) => {
|
|
30
|
+
* scrollToTop()
|
|
31
|
+
* }, { timeout: 500 })
|
|
32
|
+
*
|
|
33
|
+
* // Later, to unregister:
|
|
34
|
+
* unregister()
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
var SequenceManager = class SequenceManager {
|
|
38
|
+
static #instance = null;
|
|
39
|
+
#registrations = /* @__PURE__ */ new Map();
|
|
40
|
+
#keydownListener = null;
|
|
41
|
+
#platform;
|
|
42
|
+
constructor() {
|
|
43
|
+
this.#platform = require_constants.detectPlatform();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Gets the singleton instance of SequenceManager.
|
|
47
|
+
*/
|
|
48
|
+
static getInstance() {
|
|
49
|
+
if (!SequenceManager.#instance) SequenceManager.#instance = new SequenceManager();
|
|
50
|
+
return SequenceManager.#instance;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Resets the singleton instance. Useful for testing.
|
|
54
|
+
*/
|
|
55
|
+
static resetInstance() {
|
|
56
|
+
if (SequenceManager.#instance) {
|
|
57
|
+
SequenceManager.#instance.destroy();
|
|
58
|
+
SequenceManager.#instance = null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Registers a hotkey sequence handler.
|
|
63
|
+
*
|
|
64
|
+
* @param sequence - Array of hotkey strings that form the sequence
|
|
65
|
+
* @param callback - Function to call when the sequence is completed
|
|
66
|
+
* @param options - Options for the sequence behavior
|
|
67
|
+
* @returns A function to unregister the sequence
|
|
68
|
+
*/
|
|
69
|
+
register(sequence, callback, options = {}) {
|
|
70
|
+
if (sequence.length === 0) throw new Error("Sequence must contain at least one hotkey");
|
|
71
|
+
const id = generateSequenceId();
|
|
72
|
+
const platform = options.platform ?? this.#platform;
|
|
73
|
+
const registration = {
|
|
74
|
+
id,
|
|
75
|
+
sequence,
|
|
76
|
+
parsedSequence: sequence.map((hotkey) => require_parse.parseHotkey(hotkey, platform)),
|
|
77
|
+
callback,
|
|
78
|
+
options: {
|
|
79
|
+
timeout: DEFAULT_SEQUENCE_TIMEOUT,
|
|
80
|
+
preventDefault: true,
|
|
81
|
+
stopPropagation: true,
|
|
82
|
+
enabled: true,
|
|
83
|
+
...options,
|
|
84
|
+
platform
|
|
85
|
+
},
|
|
86
|
+
currentIndex: 0,
|
|
87
|
+
lastKeyTime: 0
|
|
88
|
+
};
|
|
89
|
+
this.#registrations.set(id, registration);
|
|
90
|
+
this.#ensureListener();
|
|
91
|
+
return () => {
|
|
92
|
+
this.#unregister(id);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Unregisters a sequence by its registration ID.
|
|
97
|
+
*/
|
|
98
|
+
#unregister(id) {
|
|
99
|
+
this.#registrations.delete(id);
|
|
100
|
+
if (this.#registrations.size === 0) this.#removeListener();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Ensures the keydown listener is attached.
|
|
104
|
+
*/
|
|
105
|
+
#ensureListener() {
|
|
106
|
+
if (typeof document === "undefined") return;
|
|
107
|
+
if (!this.#keydownListener) {
|
|
108
|
+
this.#keydownListener = this.#handleKeyDown.bind(this);
|
|
109
|
+
document.addEventListener("keydown", this.#keydownListener);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Removes the keydown listener.
|
|
114
|
+
*/
|
|
115
|
+
#removeListener() {
|
|
116
|
+
if (typeof document === "undefined") return;
|
|
117
|
+
if (this.#keydownListener) {
|
|
118
|
+
document.removeEventListener("keydown", this.#keydownListener);
|
|
119
|
+
this.#keydownListener = null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Handles keydown events for sequence matching.
|
|
124
|
+
*/
|
|
125
|
+
#handleKeyDown(event) {
|
|
126
|
+
const now = Date.now();
|
|
127
|
+
for (const registration of this.#registrations.values()) {
|
|
128
|
+
if (!registration.options.enabled) continue;
|
|
129
|
+
const timeout = registration.options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT;
|
|
130
|
+
if (registration.currentIndex > 0 && now - registration.lastKeyTime > timeout) registration.currentIndex = 0;
|
|
131
|
+
const expectedHotkey = registration.parsedSequence[registration.currentIndex];
|
|
132
|
+
if (!expectedHotkey) continue;
|
|
133
|
+
if (require_match.matchesKeyboardEvent(event, expectedHotkey, registration.options.platform)) {
|
|
134
|
+
registration.lastKeyTime = now;
|
|
135
|
+
registration.currentIndex++;
|
|
136
|
+
if (registration.currentIndex >= registration.parsedSequence.length) {
|
|
137
|
+
if (registration.options.preventDefault) event.preventDefault();
|
|
138
|
+
if (registration.options.stopPropagation) event.stopPropagation();
|
|
139
|
+
const context = {
|
|
140
|
+
hotkey: registration.sequence.join(" "),
|
|
141
|
+
parsedHotkey: registration.parsedSequence[registration.parsedSequence.length - 1]
|
|
142
|
+
};
|
|
143
|
+
registration.callback(event, context);
|
|
144
|
+
registration.currentIndex = 0;
|
|
145
|
+
}
|
|
146
|
+
} else if (registration.currentIndex > 0) {
|
|
147
|
+
const firstHotkey = registration.parsedSequence[0];
|
|
148
|
+
if (require_match.matchesKeyboardEvent(event, firstHotkey, registration.options.platform)) {
|
|
149
|
+
registration.currentIndex = 1;
|
|
150
|
+
registration.lastKeyTime = now;
|
|
151
|
+
} else registration.currentIndex = 0;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Resets all sequence progress.
|
|
157
|
+
*/
|
|
158
|
+
resetAll() {
|
|
159
|
+
for (const registration of this.#registrations.values()) {
|
|
160
|
+
registration.currentIndex = 0;
|
|
161
|
+
registration.lastKeyTime = 0;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Gets the number of registered sequences.
|
|
166
|
+
*/
|
|
167
|
+
getRegistrationCount() {
|
|
168
|
+
return this.#registrations.size;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Destroys the manager and removes all listeners.
|
|
172
|
+
*/
|
|
173
|
+
destroy() {
|
|
174
|
+
this.#removeListener();
|
|
175
|
+
this.#registrations.clear();
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Gets the singleton SequenceManager instance.
|
|
180
|
+
* Convenience function for accessing the manager.
|
|
181
|
+
*/
|
|
182
|
+
function getSequenceManager() {
|
|
183
|
+
return SequenceManager.getInstance();
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Creates a simple sequence matcher for one-off use.
|
|
187
|
+
*
|
|
188
|
+
* @param sequence - The sequence of hotkeys to match
|
|
189
|
+
* @param options - Options including timeout
|
|
190
|
+
* @returns An object with match() and reset() methods
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* const matcher = createSequenceMatcher(['G', 'G'], { timeout: 500 })
|
|
195
|
+
*
|
|
196
|
+
* document.addEventListener('keydown', (event) => {
|
|
197
|
+
* if (matcher.match(event)) {
|
|
198
|
+
* console.log('Sequence matched!')
|
|
199
|
+
* }
|
|
200
|
+
* })
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
function createSequenceMatcher(sequence, options = {}) {
|
|
204
|
+
const platform = options.platform ?? require_constants.detectPlatform();
|
|
205
|
+
const timeout = options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT;
|
|
206
|
+
const parsedSequence = sequence.map((hotkey) => require_parse.parseHotkey(hotkey, platform));
|
|
207
|
+
let currentIndex = 0;
|
|
208
|
+
let lastKeyTime = 0;
|
|
209
|
+
return {
|
|
210
|
+
match(event) {
|
|
211
|
+
const now = Date.now();
|
|
212
|
+
if (currentIndex > 0 && now - lastKeyTime > timeout) currentIndex = 0;
|
|
213
|
+
const expected = parsedSequence[currentIndex];
|
|
214
|
+
if (!expected) return false;
|
|
215
|
+
if (require_match.matchesKeyboardEvent(event, expected, platform)) {
|
|
216
|
+
lastKeyTime = now;
|
|
217
|
+
currentIndex++;
|
|
218
|
+
if (currentIndex >= parsedSequence.length) {
|
|
219
|
+
currentIndex = 0;
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
} else if (currentIndex > 0) if (require_match.matchesKeyboardEvent(event, parsedSequence[0], platform)) {
|
|
223
|
+
currentIndex = 1;
|
|
224
|
+
lastKeyTime = now;
|
|
225
|
+
} else currentIndex = 0;
|
|
226
|
+
return false;
|
|
227
|
+
},
|
|
228
|
+
reset() {
|
|
229
|
+
currentIndex = 0;
|
|
230
|
+
lastKeyTime = 0;
|
|
231
|
+
},
|
|
232
|
+
getProgress() {
|
|
233
|
+
return currentIndex;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
//#endregion
|
|
239
|
+
exports.SequenceManager = SequenceManager;
|
|
240
|
+
exports.createSequenceMatcher = createSequenceMatcher;
|
|
241
|
+
exports.getSequenceManager = getSequenceManager;
|
|
242
|
+
//# sourceMappingURL=sequence.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sequence.cjs","names":["#instance","#platform","detectPlatform","parseHotkey","#registrations","#ensureListener","#unregister","#removeListener","#keydownListener","#handleKeyDown","matchesKeyboardEvent"],"sources":["../src/sequence.ts"],"sourcesContent":["import { detectPlatform } from './constants'\nimport { parseHotkey } from './parse'\nimport { matchesKeyboardEvent } from './match'\nimport type { HotkeyOptions } from './hotkey-manager'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n ParsedHotkey,\n} from './hotkey'\n\n/**\n * Options for hotkey sequence matching.\n */\nexport interface SequenceOptions extends HotkeyOptions {\n /** Timeout between keys in milliseconds. Default: 1000 */\n timeout?: number\n}\n\n/**\n * A sequence of hotkeys for Vim-style shortcuts.\n *\n * @example\n * ```ts\n * const gotoTop: HotkeySequence = ['G', 'G'] // gg\n * const deleteLine: HotkeySequence = ['D', 'D'] // dd\n * const deleteWord: HotkeySequence = ['D', 'I', 'W'] // diw\n * ```\n */\nexport type HotkeySequence = Array<Hotkey>\n\n/**\n * Default timeout between keys in a sequence (in milliseconds).\n */\nconst DEFAULT_SEQUENCE_TIMEOUT = 1000\n\nlet sequenceIdCounter = 0\n\n/**\n * Generates a unique ID for sequence registrations.\n */\nfunction generateSequenceId(): string {\n return `sequence_${++sequenceIdCounter}`\n}\n\n/**\n * Internal representation of a sequence registration.\n */\ninterface SequenceRegistration {\n id: string\n sequence: HotkeySequence\n parsedSequence: Array<ParsedHotkey>\n callback: HotkeyCallback\n options: SequenceOptions\n currentIndex: number\n lastKeyTime: number\n}\n\n/**\n * Manages keyboard sequence matching for Vim-style shortcuts.\n *\n * This class allows registering multi-key sequences like 'g g' or 'd d'\n * that trigger callbacks when the full sequence is pressed within\n * a configurable timeout.\n *\n * @example\n * ```ts\n * const matcher = SequenceManager.getInstance()\n *\n * // Register 'g g' to go to top\n * const unregister = matcher.register(['G', 'G'], (event, context) => {\n * scrollToTop()\n * }, { timeout: 500 })\n *\n * // Later, to unregister:\n * unregister()\n * ```\n */\nexport class SequenceManager {\n static #instance: SequenceManager | null = null\n\n #registrations: Map<string, SequenceRegistration> = new Map()\n #keydownListener: ((event: KeyboardEvent) => void) | null = null\n #platform: 'mac' | 'windows' | 'linux'\n\n private constructor() {\n this.#platform = detectPlatform()\n }\n\n /**\n * Gets the singleton instance of SequenceManager.\n */\n static getInstance(): SequenceManager {\n if (!SequenceManager.#instance) {\n SequenceManager.#instance = new SequenceManager()\n }\n return SequenceManager.#instance\n }\n\n /**\n * Resets the singleton instance. Useful for testing.\n */\n static resetInstance(): void {\n if (SequenceManager.#instance) {\n SequenceManager.#instance.destroy()\n SequenceManager.#instance = null\n }\n }\n\n /**\n * Registers a hotkey sequence handler.\n *\n * @param sequence - Array of hotkey strings that form the sequence\n * @param callback - Function to call when the sequence is completed\n * @param options - Options for the sequence behavior\n * @returns A function to unregister the sequence\n */\n register(\n sequence: HotkeySequence,\n callback: HotkeyCallback,\n options: SequenceOptions = {},\n ): () => void {\n if (sequence.length === 0) {\n throw new Error('Sequence must contain at least one hotkey')\n }\n\n const id = generateSequenceId()\n const platform = options.platform ?? this.#platform\n const parsedSequence = sequence.map((hotkey) =>\n parseHotkey(hotkey, platform),\n )\n\n const registration: SequenceRegistration = {\n id,\n sequence,\n parsedSequence,\n callback,\n options: {\n timeout: DEFAULT_SEQUENCE_TIMEOUT,\n preventDefault: true,\n stopPropagation: true,\n enabled: true,\n ...options,\n platform,\n },\n currentIndex: 0,\n lastKeyTime: 0,\n }\n\n this.#registrations.set(id, registration)\n this.#ensureListener()\n\n return () => {\n this.#unregister(id)\n }\n }\n\n /**\n * Unregisters a sequence by its registration ID.\n */\n #unregister(id: string): void {\n this.#registrations.delete(id)\n\n if (this.#registrations.size === 0) {\n this.#removeListener()\n }\n }\n\n /**\n * Ensures the keydown listener is attached.\n */\n #ensureListener(): void {\n if (typeof document === 'undefined') {\n return // SSR safety\n }\n\n if (!this.#keydownListener) {\n this.#keydownListener = this.#handleKeyDown.bind(this)\n document.addEventListener('keydown', this.#keydownListener)\n }\n }\n\n /**\n * Removes the keydown listener.\n */\n #removeListener(): void {\n if (typeof document === 'undefined') {\n return\n }\n\n if (this.#keydownListener) {\n document.removeEventListener('keydown', this.#keydownListener)\n this.#keydownListener = null\n }\n }\n\n /**\n * Handles keydown events for sequence matching.\n */\n #handleKeyDown(event: KeyboardEvent): void {\n const now = Date.now()\n\n for (const registration of this.#registrations.values()) {\n if (!registration.options.enabled) {\n continue\n }\n\n const timeout = registration.options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT\n\n // Check if sequence has timed out\n if (\n registration.currentIndex > 0 &&\n now - registration.lastKeyTime > timeout\n ) {\n // Reset the sequence\n registration.currentIndex = 0\n }\n\n const expectedHotkey =\n registration.parsedSequence[registration.currentIndex]\n if (!expectedHotkey) {\n continue\n }\n\n // Check if current key matches the expected key in sequence\n if (\n matchesKeyboardEvent(\n event,\n expectedHotkey,\n registration.options.platform,\n )\n ) {\n registration.lastKeyTime = now\n registration.currentIndex++\n\n // Check if sequence is complete\n if (registration.currentIndex >= registration.parsedSequence.length) {\n // Sequence complete!\n if (registration.options.preventDefault) {\n event.preventDefault()\n }\n if (registration.options.stopPropagation) {\n event.stopPropagation()\n }\n\n const context: HotkeyCallbackContext = {\n hotkey: registration.sequence.join(' ') as Hotkey,\n parsedHotkey:\n registration.parsedSequence[\n registration.parsedSequence.length - 1\n ]!,\n }\n\n registration.callback(event, context)\n\n // Reset for next sequence\n registration.currentIndex = 0\n }\n } else if (registration.currentIndex > 0) {\n // Key didn't match and we were in the middle of a sequence\n // Check if it matches the start of the sequence (for overlapping sequences)\n const firstHotkey = registration.parsedSequence[0]!\n if (\n matchesKeyboardEvent(\n event,\n firstHotkey,\n registration.options.platform,\n )\n ) {\n registration.currentIndex = 1\n registration.lastKeyTime = now\n } else {\n // Reset the sequence\n registration.currentIndex = 0\n }\n }\n }\n }\n\n /**\n * Resets all sequence progress.\n */\n resetAll(): void {\n for (const registration of this.#registrations.values()) {\n registration.currentIndex = 0\n registration.lastKeyTime = 0\n }\n }\n\n /**\n * Gets the number of registered sequences.\n */\n getRegistrationCount(): number {\n return this.#registrations.size\n }\n\n /**\n * Destroys the manager and removes all listeners.\n */\n destroy(): void {\n this.#removeListener()\n this.#registrations.clear()\n }\n}\n\n/**\n * Gets the singleton SequenceManager instance.\n * Convenience function for accessing the manager.\n */\nexport function getSequenceManager(): SequenceManager {\n return SequenceManager.getInstance()\n}\n\n/**\n * Creates a simple sequence matcher for one-off use.\n *\n * @param sequence - The sequence of hotkeys to match\n * @param options - Options including timeout\n * @returns An object with match() and reset() methods\n *\n * @example\n * ```ts\n * const matcher = createSequenceMatcher(['G', 'G'], { timeout: 500 })\n *\n * document.addEventListener('keydown', (event) => {\n * if (matcher.match(event)) {\n * console.log('Sequence matched!')\n * }\n * })\n * ```\n */\nexport function createSequenceMatcher(\n sequence: HotkeySequence,\n options: { timeout?: number; platform?: 'mac' | 'windows' | 'linux' } = {},\n): {\n match: (event: KeyboardEvent) => boolean\n reset: () => void\n getProgress: () => number\n} {\n const platform = options.platform ?? detectPlatform()\n const timeout = options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT\n const parsedSequence = sequence.map((hotkey) => parseHotkey(hotkey, platform))\n\n let currentIndex = 0\n let lastKeyTime = 0\n\n return {\n match(event: KeyboardEvent): boolean {\n const now = Date.now()\n\n // Check timeout\n if (currentIndex > 0 && now - lastKeyTime > timeout) {\n currentIndex = 0\n }\n\n const expected = parsedSequence[currentIndex]\n if (!expected) {\n return false\n }\n\n if (matchesKeyboardEvent(event, expected, platform)) {\n lastKeyTime = now\n currentIndex++\n\n if (currentIndex >= parsedSequence.length) {\n currentIndex = 0\n return true\n }\n } else if (currentIndex > 0) {\n // Check if it matches start of sequence\n if (matchesKeyboardEvent(event, parsedSequence[0]!, platform)) {\n currentIndex = 1\n lastKeyTime = now\n } else {\n currentIndex = 0\n }\n }\n\n return false\n },\n\n reset(): void {\n currentIndex = 0\n lastKeyTime = 0\n },\n\n getProgress(): number {\n return currentIndex\n },\n }\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Hotkey, HotkeyCallback } from "./hotkey.cjs";
|
|
2
|
+
import { HotkeyOptions } from "./hotkey-manager.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/sequence.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Options for hotkey sequence matching.
|
|
7
|
+
*/
|
|
8
|
+
interface SequenceOptions extends HotkeyOptions {
|
|
9
|
+
/** Timeout between keys in milliseconds. Default: 1000 */
|
|
10
|
+
timeout?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A sequence of hotkeys for Vim-style shortcuts.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const gotoTop: HotkeySequence = ['G', 'G'] // gg
|
|
18
|
+
* const deleteLine: HotkeySequence = ['D', 'D'] // dd
|
|
19
|
+
* const deleteWord: HotkeySequence = ['D', 'I', 'W'] // diw
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
type HotkeySequence = Array<Hotkey>;
|
|
23
|
+
/**
|
|
24
|
+
* Manages keyboard sequence matching for Vim-style shortcuts.
|
|
25
|
+
*
|
|
26
|
+
* This class allows registering multi-key sequences like 'g g' or 'd d'
|
|
27
|
+
* that trigger callbacks when the full sequence is pressed within
|
|
28
|
+
* a configurable timeout.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const matcher = SequenceManager.getInstance()
|
|
33
|
+
*
|
|
34
|
+
* // Register 'g g' to go to top
|
|
35
|
+
* const unregister = matcher.register(['G', 'G'], (event, context) => {
|
|
36
|
+
* scrollToTop()
|
|
37
|
+
* }, { timeout: 500 })
|
|
38
|
+
*
|
|
39
|
+
* // Later, to unregister:
|
|
40
|
+
* unregister()
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare class SequenceManager {
|
|
44
|
+
#private;
|
|
45
|
+
private constructor();
|
|
46
|
+
/**
|
|
47
|
+
* Gets the singleton instance of SequenceManager.
|
|
48
|
+
*/
|
|
49
|
+
static getInstance(): SequenceManager;
|
|
50
|
+
/**
|
|
51
|
+
* Resets the singleton instance. Useful for testing.
|
|
52
|
+
*/
|
|
53
|
+
static resetInstance(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Registers a hotkey sequence handler.
|
|
56
|
+
*
|
|
57
|
+
* @param sequence - Array of hotkey strings that form the sequence
|
|
58
|
+
* @param callback - Function to call when the sequence is completed
|
|
59
|
+
* @param options - Options for the sequence behavior
|
|
60
|
+
* @returns A function to unregister the sequence
|
|
61
|
+
*/
|
|
62
|
+
register(sequence: HotkeySequence, callback: HotkeyCallback, options?: SequenceOptions): () => void;
|
|
63
|
+
/**
|
|
64
|
+
* Resets all sequence progress.
|
|
65
|
+
*/
|
|
66
|
+
resetAll(): void;
|
|
67
|
+
/**
|
|
68
|
+
* Gets the number of registered sequences.
|
|
69
|
+
*/
|
|
70
|
+
getRegistrationCount(): number;
|
|
71
|
+
/**
|
|
72
|
+
* Destroys the manager and removes all listeners.
|
|
73
|
+
*/
|
|
74
|
+
destroy(): void;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Gets the singleton SequenceManager instance.
|
|
78
|
+
* Convenience function for accessing the manager.
|
|
79
|
+
*/
|
|
80
|
+
declare function getSequenceManager(): SequenceManager;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a simple sequence matcher for one-off use.
|
|
83
|
+
*
|
|
84
|
+
* @param sequence - The sequence of hotkeys to match
|
|
85
|
+
* @param options - Options including timeout
|
|
86
|
+
* @returns An object with match() and reset() methods
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* const matcher = createSequenceMatcher(['G', 'G'], { timeout: 500 })
|
|
91
|
+
*
|
|
92
|
+
* document.addEventListener('keydown', (event) => {
|
|
93
|
+
* if (matcher.match(event)) {
|
|
94
|
+
* console.log('Sequence matched!')
|
|
95
|
+
* }
|
|
96
|
+
* })
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare function createSequenceMatcher(sequence: HotkeySequence, options?: {
|
|
100
|
+
timeout?: number;
|
|
101
|
+
platform?: 'mac' | 'windows' | 'linux';
|
|
102
|
+
}): {
|
|
103
|
+
match: (event: KeyboardEvent) => boolean;
|
|
104
|
+
reset: () => void;
|
|
105
|
+
getProgress: () => number;
|
|
106
|
+
};
|
|
107
|
+
//#endregion
|
|
108
|
+
export { HotkeySequence, SequenceManager, SequenceOptions, createSequenceMatcher, getSequenceManager };
|
|
109
|
+
//# sourceMappingURL=sequence.d.cts.map
|