@tanstack/hotkeys 0.1.3 → 0.3.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/format.cjs +16 -0
- package/dist/format.cjs.map +1 -1
- package/dist/format.d.cts +14 -1
- package/dist/format.d.ts +14 -1
- package/dist/format.js +16 -1
- package/dist/format.js.map +1 -1
- package/dist/hotkey-manager.cjs +7 -75
- package/dist/hotkey-manager.cjs.map +1 -1
- package/dist/hotkey-manager.d.cts +2 -10
- package/dist/hotkey-manager.d.ts +2 -10
- package/dist/hotkey-manager.js +5 -73
- package/dist/hotkey-manager.js.map +1 -1
- package/dist/index.cjs +5 -4
- package/dist/index.d.cts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +3 -3
- package/dist/manager.utils.cjs +90 -0
- package/dist/manager.utils.cjs.map +1 -0
- package/dist/manager.utils.d.cts +13 -0
- package/dist/manager.utils.d.ts +13 -0
- package/dist/manager.utils.js +85 -0
- package/dist/manager.utils.js.map +1 -0
- package/dist/sequence-manager.cjs +391 -0
- package/dist/sequence-manager.cjs.map +1 -0
- package/dist/{sequence.d.cts → sequence-manager.d.cts} +50 -19
- package/dist/{sequence.d.ts → sequence-manager.d.ts} +50 -19
- package/dist/sequence-manager.js +389 -0
- package/dist/sequence-manager.js.map +1 -0
- package/package.json +2 -2
- package/src/format.ts +16 -0
- package/src/hotkey-manager.ts +18 -156
- package/src/index.ts +1 -1
- package/src/manager.utils.ts +171 -0
- package/src/sequence-manager.ts +693 -0
- package/dist/sequence.cjs +0 -242
- package/dist/sequence.cjs.map +0 -1
- package/dist/sequence.js +0 -240
- package/dist/sequence.js.map +0 -1
- package/src/sequence.ts +0 -391
package/dist/sequence.cjs
DELETED
|
@@ -1,242 +0,0 @@
|
|
|
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
|
package/dist/sequence.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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":";;;;;;;;AAkCA,MAAM,2BAA2B;AAEjC,IAAI,oBAAoB;;;;AAKxB,SAAS,qBAA6B;AACpC,QAAO,YAAY,EAAE;;;;;;;;;;;;;;;;;;;;;;AAoCvB,IAAa,kBAAb,MAAa,gBAAgB;CAC3B,QAAOA,WAAoC;CAE3C,iCAAoD,IAAI,KAAK;CAC7D,mBAA4D;CAC5D;CAEA,AAAQ,cAAc;AACpB,QAAKC,WAAYC,kCAAgB;;;;;CAMnC,OAAO,cAA+B;AACpC,MAAI,CAAC,iBAAgBF,SACnB,kBAAgBA,WAAY,IAAI,iBAAiB;AAEnD,SAAO,iBAAgBA;;;;;CAMzB,OAAO,gBAAsB;AAC3B,MAAI,iBAAgBA,UAAW;AAC7B,oBAAgBA,SAAU,SAAS;AACnC,oBAAgBA,WAAY;;;;;;;;;;;CAYhC,SACE,UACA,UACA,UAA2B,EAAE,EACjB;AACZ,MAAI,SAAS,WAAW,EACtB,OAAM,IAAI,MAAM,4CAA4C;EAG9D,MAAM,KAAK,oBAAoB;EAC/B,MAAM,WAAW,QAAQ,YAAY,MAAKC;EAK1C,MAAM,eAAqC;GACzC;GACA;GACA,gBAPqB,SAAS,KAAK,WACnCE,0BAAY,QAAQ,SAAS,CAC9B;GAMC;GACA,SAAS;IACP,SAAS;IACT,gBAAgB;IAChB,iBAAiB;IACjB,SAAS;IACT,GAAG;IACH;IACD;GACD,cAAc;GACd,aAAa;GACd;AAED,QAAKC,cAAe,IAAI,IAAI,aAAa;AACzC,QAAKC,gBAAiB;AAEtB,eAAa;AACX,SAAKC,WAAY,GAAG;;;;;;CAOxB,YAAY,IAAkB;AAC5B,QAAKF,cAAe,OAAO,GAAG;AAE9B,MAAI,MAAKA,cAAe,SAAS,EAC/B,OAAKG,gBAAiB;;;;;CAO1B,kBAAwB;AACtB,MAAI,OA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a,eAAe;AAC5B,gBAAa,cAAc;;;;;;CAO/B,uBAA+B;AAC7B,SAAO,MAAKA,cAAe;;;;;CAM7B,UAAgB;AACd,QAAKG,gBAAiB;AACtB,QAAKH,cAAe,OAAO;;;;;;;AAQ/B,SAAgB,qBAAsC;AACpD,QAAO,gBAAgB,aAAa;;;;;;;;;;;;;;;;;;;;AAqBtC,SAAgB,sBACd,UACA,UAAwE,EAAE,EAK1E;CACA,MAAM,WAAW,QAAQ,YAAYF,kCAAgB;CACrD,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,iBAAiB,SAAS,KAAK,WAAWC,0BAAY,QAAQ,SAAS,CAAC;CAE9E,IAAI,eAAe;CACnB,IAAI,cAAc;AAElB,QAAO;EACL,MAAM,OAA+B;GACnC,MAAM,MAAM,KAAK,KAAK;AAGtB,OAAI,eAAe,KAAK,MAAM,cAAc,QAC1C,gBAAe;GAGjB,MAAM,WAAW,eAAe;AAChC,OAAI,CAAC,SACH,QAAO;AAGT,OAAIO,mCAAqB,OAAO,UAAU,SAAS,EAAE;AACnD,kBAAc;AACd;AAEA,QAAI,gBAAgB,eAAe,QAAQ;AACzC,oBAAe;AACf,YAAO;;cAEA,eAAe,EAExB,KAAIA,mCAAqB,OAAO,eAAe,IAAK,SAAS,EAAE;AAC7D,mBAAe;AACf,kBAAc;SAEd,gBAAe;AAInB,UAAO;;EAGT,QAAc;AACZ,kBAAe;AACf,iBAAc;;EAGhB,cAAsB;AACpB,UAAO;;EAEV"}
|
package/dist/sequence.js
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import { detectPlatform } from "./constants.js";
|
|
2
|
-
import { parseHotkey } from "./parse.js";
|
|
3
|
-
import { matchesKeyboardEvent } from "./match.js";
|
|
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 = 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) => 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 (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 (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 ?? detectPlatform();
|
|
205
|
-
const timeout = options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT;
|
|
206
|
-
const parsedSequence = sequence.map((hotkey) => 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 (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 (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
|
-
export { SequenceManager, createSequenceMatcher, getSequenceManager };
|
|
240
|
-
//# sourceMappingURL=sequence.js.map
|
package/dist/sequence.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sequence.js","names":["#instance","#platform","#registrations","#ensureListener","#unregister","#removeListener","#keydownListener","#handleKeyDown"],"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":";;;;;;;;AAkCA,MAAM,2BAA2B;AAEjC,IAAI,oBAAoB;;;;AAKxB,SAAS,qBAA6B;AACpC,QAAO,YAAY,EAAE;;;;;;;;;;;;;;;;;;;;;;AAoCvB,IAAa,kBAAb,MAAa,gBAAgB;CAC3B,QAA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