@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,109 @@
|
|
|
1
|
+
import { Hotkey, HotkeyCallback } from "./hotkey.js";
|
|
2
|
+
import { HotkeyOptions } from "./hotkey-manager.js";
|
|
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.ts.map
|
package/dist/sequence.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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":"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"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
const require_constants = require('./constants.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/validate.ts
|
|
4
|
+
/**
|
|
5
|
+
* Validates a hotkey string and returns any warnings or errors.
|
|
6
|
+
*
|
|
7
|
+
* Checks for:
|
|
8
|
+
* - Valid syntax (modifier+...+key format)
|
|
9
|
+
* - Known modifiers
|
|
10
|
+
* - Known keys
|
|
11
|
+
*
|
|
12
|
+
* @param hotkey - The hotkey string to validate
|
|
13
|
+
* @returns A ValidationResult with validity status, warnings, and errors
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* validateHotkey('Mod+S')
|
|
18
|
+
* // { valid: true, warnings: [], errors: [] }
|
|
19
|
+
*
|
|
20
|
+
* validateHotkey('')
|
|
21
|
+
* // { valid: false, warnings: [], errors: ['Hotkey cannot be empty'] }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function validateHotkey(hotkey) {
|
|
25
|
+
const warnings = [];
|
|
26
|
+
const errors = [];
|
|
27
|
+
if (!hotkey || hotkey.trim() === "") return {
|
|
28
|
+
valid: false,
|
|
29
|
+
warnings: [],
|
|
30
|
+
errors: ["Hotkey cannot be empty"]
|
|
31
|
+
};
|
|
32
|
+
const parts = hotkey.split("+").map((p) => p.trim());
|
|
33
|
+
if (parts.length === 0 || parts.some((p) => p === "")) return {
|
|
34
|
+
valid: false,
|
|
35
|
+
warnings: [],
|
|
36
|
+
errors: ["Invalid hotkey format: empty parts detected"]
|
|
37
|
+
};
|
|
38
|
+
const modifierParts = parts.slice(0, -1);
|
|
39
|
+
const keyPart = parts[parts.length - 1];
|
|
40
|
+
for (const modifier of modifierParts) if (!(require_constants.MODIFIER_ALIASES[modifier] ?? require_constants.MODIFIER_ALIASES[modifier.toLowerCase()])) errors.push(`Unknown modifier: '${modifier}'`);
|
|
41
|
+
if (!isKnownKey(normalizeKeyForValidation(keyPart)) && !isKnownKey(keyPart)) warnings.push(`Unknown key: '${keyPart}'. This may still work but won't have type-safe autocomplete.`);
|
|
42
|
+
return {
|
|
43
|
+
valid: errors.length === 0,
|
|
44
|
+
warnings,
|
|
45
|
+
errors
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Normalizes a key for validation checking.
|
|
50
|
+
*/
|
|
51
|
+
function normalizeKeyForValidation(key) {
|
|
52
|
+
if (key.length === 1 && /^[a-zA-Z]$/.test(key)) return key.toUpperCase();
|
|
53
|
+
if (/^f([1-9]|1[0-2])$/i.test(key)) return key.toUpperCase();
|
|
54
|
+
return key;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Checks if a key is in the known keys set.
|
|
58
|
+
*/
|
|
59
|
+
function isKnownKey(key) {
|
|
60
|
+
if (require_constants.ALL_KEYS.has(key)) return true;
|
|
61
|
+
if (key.length === 1 && require_constants.ALL_KEYS.has(key.toUpperCase())) return true;
|
|
62
|
+
return key in {
|
|
63
|
+
Esc: true,
|
|
64
|
+
Return: true,
|
|
65
|
+
Space: true,
|
|
66
|
+
" ": true,
|
|
67
|
+
Del: true,
|
|
68
|
+
Up: true,
|
|
69
|
+
Down: true,
|
|
70
|
+
Left: true,
|
|
71
|
+
Right: true
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Validates a hotkey and throws an error if invalid.
|
|
76
|
+
* Useful for development-time validation.
|
|
77
|
+
*
|
|
78
|
+
* @param hotkey - The hotkey string to validate
|
|
79
|
+
* @throws Error if the hotkey is invalid
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* assertValidHotkey('Mod+S') // OK
|
|
84
|
+
* assertValidHotkey('') // Throws Error: Invalid hotkey: Hotkey cannot be empty
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
function assertValidHotkey(hotkey) {
|
|
88
|
+
const result = validateHotkey(hotkey);
|
|
89
|
+
if (!result.valid) throw new Error(`Invalid hotkey '${hotkey}': ${result.errors.join(", ")}`);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Validates a hotkey and logs warnings to the console.
|
|
93
|
+
* Useful for development-time feedback.
|
|
94
|
+
*
|
|
95
|
+
* @param hotkey - The hotkey string to validate
|
|
96
|
+
* @returns True if the hotkey is valid (may still have warnings)
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* checkHotkey('Alt+C')
|
|
101
|
+
* // Console: Warning: Alt+C may not work reliably on macOS...
|
|
102
|
+
* // Returns: true
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
function checkHotkey(hotkey) {
|
|
106
|
+
const result = validateHotkey(hotkey);
|
|
107
|
+
if (result.errors.length > 0) console.error(`Hotkey '${hotkey}' errors:`, result.errors.join("; "));
|
|
108
|
+
if (result.warnings.length > 0) console.warn(`Hotkey '${hotkey}' warnings:`, result.warnings.join("; "));
|
|
109
|
+
return result.valid;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
//#endregion
|
|
113
|
+
exports.assertValidHotkey = assertValidHotkey;
|
|
114
|
+
exports.checkHotkey = checkHotkey;
|
|
115
|
+
exports.validateHotkey = validateHotkey;
|
|
116
|
+
//# sourceMappingURL=validate.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.cjs","names":["MODIFIER_ALIASES","ALL_KEYS"],"sources":["../src/validate.ts"],"sourcesContent":["import { ALL_KEYS, MODIFIER_ALIASES } from './constants'\nimport type { Hotkey, ValidationResult } from './hotkey'\n\n/**\n * Validates a hotkey string and returns any warnings or errors.\n *\n * Checks for:\n * - Valid syntax (modifier+...+key format)\n * - Known modifiers\n * - Known keys\n *\n * @param hotkey - The hotkey string to validate\n * @returns A ValidationResult with validity status, warnings, and errors\n *\n * @example\n * ```ts\n * validateHotkey('Mod+S')\n * // { valid: true, warnings: [], errors: [] }\n *\n * validateHotkey('')\n * // { valid: false, warnings: [], errors: ['Hotkey cannot be empty'] }\n * ```\n */\nexport function validateHotkey(\n hotkey: Hotkey | (string & {}),\n): ValidationResult {\n const warnings: Array<string> = []\n const errors: Array<string> = []\n\n // Check for empty string\n if (!hotkey || hotkey.trim() === '') {\n return {\n valid: false,\n warnings: [],\n errors: ['Hotkey cannot be empty'],\n }\n }\n\n const parts = hotkey.split('+').map((p) => p.trim())\n\n // Must have at least one part (the key)\n if (parts.length === 0 || parts.some((p) => p === '')) {\n return {\n valid: false,\n warnings: [],\n errors: ['Invalid hotkey format: empty parts detected'],\n }\n }\n\n // Validate modifiers (all parts except the last)\n const modifierParts = parts.slice(0, -1)\n const keyPart = parts[parts.length - 1]!\n\n // Check for unknown modifiers\n for (const modifier of modifierParts) {\n const normalized =\n MODIFIER_ALIASES[modifier] ?? MODIFIER_ALIASES[modifier.toLowerCase()]\n if (!normalized) {\n errors.push(`Unknown modifier: '${modifier}'`)\n }\n }\n\n // Check if key is known\n const normalizedKey = normalizeKeyForValidation(keyPart)\n if (!isKnownKey(normalizedKey) && !isKnownKey(keyPart)) {\n warnings.push(\n `Unknown key: '${keyPart}'. This may still work but won't have type-safe autocomplete.`,\n )\n }\n\n return {\n valid: errors.length === 0,\n warnings,\n errors,\n }\n}\n\n/**\n * Normalizes a key for validation checking.\n */\nfunction normalizeKeyForValidation(key: string): string {\n // Single letter to uppercase\n if (key.length === 1 && /^[a-zA-Z]$/.test(key)) {\n return key.toUpperCase()\n }\n\n // Function keys to uppercase\n if (/^f([1-9]|1[0-2])$/i.test(key)) {\n return key.toUpperCase()\n }\n\n return key\n}\n\n/**\n * Checks if a key is in the known keys set.\n */\nfunction isKnownKey(key: string): boolean {\n // Check direct match\n if (ALL_KEYS.has(key as any)) {\n return true\n }\n\n // Check uppercase version for letters\n if (key.length === 1 && ALL_KEYS.has(key.toUpperCase() as any)) {\n return true\n }\n\n // Check common aliases\n const aliases: Record<string, boolean> = {\n Esc: true,\n Return: true,\n Space: true,\n ' ': true,\n Del: true,\n Up: true,\n Down: true,\n Left: true,\n Right: true,\n }\n\n return key in aliases\n}\n\n/**\n * Validates a hotkey and throws an error if invalid.\n * Useful for development-time validation.\n *\n * @param hotkey - The hotkey string to validate\n * @throws Error if the hotkey is invalid\n *\n * @example\n * ```ts\n * assertValidHotkey('Mod+S') // OK\n * assertValidHotkey('') // Throws Error: Invalid hotkey: Hotkey cannot be empty\n * ```\n */\nexport function assertValidHotkey(hotkey: Hotkey | (string & {})): void {\n const result = validateHotkey(hotkey)\n if (!result.valid) {\n throw new Error(`Invalid hotkey '${hotkey}': ${result.errors.join(', ')}`)\n }\n}\n\n/**\n * Validates a hotkey and logs warnings to the console.\n * Useful for development-time feedback.\n *\n * @param hotkey - The hotkey string to validate\n * @returns True if the hotkey is valid (may still have warnings)\n *\n * @example\n * ```ts\n * checkHotkey('Alt+C')\n * // Console: Warning: Alt+C may not work reliably on macOS...\n * // Returns: true\n * ```\n */\nexport function checkHotkey(hotkey: Hotkey | (string & {})): boolean {\n const result = validateHotkey(hotkey)\n\n if (result.errors.length > 0) {\n console.error(`Hotkey '${hotkey}' errors:`, result.errors.join('; '))\n }\n\n if (result.warnings.length > 0) {\n console.warn(`Hotkey '${hotkey}' warnings:`, result.warnings.join('; '))\n }\n\n return result.valid\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,eACd,QACkB;CAClB,MAAM,WAA0B,EAAE;CAClC,MAAM,SAAwB,EAAE;AAGhC,KAAI,CAAC,UAAU,OAAO,MAAM,KAAK,GAC/B,QAAO;EACL,OAAO;EACP,UAAU,EAAE;EACZ,QAAQ,CAAC,yBAAyB;EACnC;CAGH,MAAM,QAAQ,OAAO,MAAM,IAAI,CAAC,KAAK,MAAM,EAAE,MAAM,CAAC;AAGpD,KAAI,MAAM,WAAW,KAAK,MAAM,MAAM,MAAM,MAAM,GAAG,CACnD,QAAO;EACL,OAAO;EACP,UAAU,EAAE;EACZ,QAAQ,CAAC,8CAA8C;EACxD;CAIH,MAAM,gBAAgB,MAAM,MAAM,GAAG,GAAG;CACxC,MAAM,UAAU,MAAM,MAAM,SAAS;AAGrC,MAAK,MAAM,YAAY,cAGrB,KAAI,EADFA,mCAAiB,aAAaA,mCAAiB,SAAS,aAAa,GAErE,QAAO,KAAK,sBAAsB,SAAS,GAAG;AAMlD,KAAI,CAAC,WADiB,0BAA0B,QAAQ,CAC1B,IAAI,CAAC,WAAW,QAAQ,CACpD,UAAS,KACP,iBAAiB,QAAQ,+DAC1B;AAGH,QAAO;EACL,OAAO,OAAO,WAAW;EACzB;EACA;EACD;;;;;AAMH,SAAS,0BAA0B,KAAqB;AAEtD,KAAI,IAAI,WAAW,KAAK,aAAa,KAAK,IAAI,CAC5C,QAAO,IAAI,aAAa;AAI1B,KAAI,qBAAqB,KAAK,IAAI,CAChC,QAAO,IAAI,aAAa;AAG1B,QAAO;;;;;AAMT,SAAS,WAAW,KAAsB;AAExC,KAAIC,2BAAS,IAAI,IAAW,CAC1B,QAAO;AAIT,KAAI,IAAI,WAAW,KAAKA,2BAAS,IAAI,IAAI,aAAa,CAAQ,CAC5D,QAAO;AAgBT,QAAO,OAZkC;EACvC,KAAK;EACL,QAAQ;EACR,OAAO;EACP,KAAK;EACL,KAAK;EACL,IAAI;EACJ,MAAM;EACN,MAAM;EACN,OAAO;EACR;;;;;;;;;;;;;;;AAkBH,SAAgB,kBAAkB,QAAsC;CACtE,MAAM,SAAS,eAAe,OAAO;AACrC,KAAI,CAAC,OAAO,MACV,OAAM,IAAI,MAAM,mBAAmB,OAAO,KAAK,OAAO,OAAO,KAAK,KAAK,GAAG;;;;;;;;;;;;;;;;AAkB9E,SAAgB,YAAY,QAAyC;CACnE,MAAM,SAAS,eAAe,OAAO;AAErC,KAAI,OAAO,OAAO,SAAS,EACzB,SAAQ,MAAM,WAAW,OAAO,YAAY,OAAO,OAAO,KAAK,KAAK,CAAC;AAGvE,KAAI,OAAO,SAAS,SAAS,EAC3B,SAAQ,KAAK,WAAW,OAAO,cAAc,OAAO,SAAS,KAAK,KAAK,CAAC;AAG1E,QAAO,OAAO"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Hotkey, ValidationResult } from "./hotkey.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/validate.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Validates a hotkey string and returns any warnings or errors.
|
|
6
|
+
*
|
|
7
|
+
* Checks for:
|
|
8
|
+
* - Valid syntax (modifier+...+key format)
|
|
9
|
+
* - Known modifiers
|
|
10
|
+
* - Known keys
|
|
11
|
+
*
|
|
12
|
+
* @param hotkey - The hotkey string to validate
|
|
13
|
+
* @returns A ValidationResult with validity status, warnings, and errors
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* validateHotkey('Mod+S')
|
|
18
|
+
* // { valid: true, warnings: [], errors: [] }
|
|
19
|
+
*
|
|
20
|
+
* validateHotkey('')
|
|
21
|
+
* // { valid: false, warnings: [], errors: ['Hotkey cannot be empty'] }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function validateHotkey(hotkey: Hotkey | (string & {})): ValidationResult;
|
|
25
|
+
/**
|
|
26
|
+
* Validates a hotkey and throws an error if invalid.
|
|
27
|
+
* Useful for development-time validation.
|
|
28
|
+
*
|
|
29
|
+
* @param hotkey - The hotkey string to validate
|
|
30
|
+
* @throws Error if the hotkey is invalid
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* assertValidHotkey('Mod+S') // OK
|
|
35
|
+
* assertValidHotkey('') // Throws Error: Invalid hotkey: Hotkey cannot be empty
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare function assertValidHotkey(hotkey: Hotkey | (string & {})): void;
|
|
39
|
+
/**
|
|
40
|
+
* Validates a hotkey and logs warnings to the console.
|
|
41
|
+
* Useful for development-time feedback.
|
|
42
|
+
*
|
|
43
|
+
* @param hotkey - The hotkey string to validate
|
|
44
|
+
* @returns True if the hotkey is valid (may still have warnings)
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* checkHotkey('Alt+C')
|
|
49
|
+
* // Console: Warning: Alt+C may not work reliably on macOS...
|
|
50
|
+
* // Returns: true
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
declare function checkHotkey(hotkey: Hotkey | (string & {})): boolean;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { assertValidHotkey, checkHotkey, validateHotkey };
|
|
56
|
+
//# sourceMappingURL=validate.d.cts.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Hotkey, ValidationResult } from "./hotkey.js";
|
|
2
|
+
|
|
3
|
+
//#region src/validate.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Validates a hotkey string and returns any warnings or errors.
|
|
6
|
+
*
|
|
7
|
+
* Checks for:
|
|
8
|
+
* - Valid syntax (modifier+...+key format)
|
|
9
|
+
* - Known modifiers
|
|
10
|
+
* - Known keys
|
|
11
|
+
*
|
|
12
|
+
* @param hotkey - The hotkey string to validate
|
|
13
|
+
* @returns A ValidationResult with validity status, warnings, and errors
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* validateHotkey('Mod+S')
|
|
18
|
+
* // { valid: true, warnings: [], errors: [] }
|
|
19
|
+
*
|
|
20
|
+
* validateHotkey('')
|
|
21
|
+
* // { valid: false, warnings: [], errors: ['Hotkey cannot be empty'] }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function validateHotkey(hotkey: Hotkey | (string & {})): ValidationResult;
|
|
25
|
+
/**
|
|
26
|
+
* Validates a hotkey and throws an error if invalid.
|
|
27
|
+
* Useful for development-time validation.
|
|
28
|
+
*
|
|
29
|
+
* @param hotkey - The hotkey string to validate
|
|
30
|
+
* @throws Error if the hotkey is invalid
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* assertValidHotkey('Mod+S') // OK
|
|
35
|
+
* assertValidHotkey('') // Throws Error: Invalid hotkey: Hotkey cannot be empty
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare function assertValidHotkey(hotkey: Hotkey | (string & {})): void;
|
|
39
|
+
/**
|
|
40
|
+
* Validates a hotkey and logs warnings to the console.
|
|
41
|
+
* Useful for development-time feedback.
|
|
42
|
+
*
|
|
43
|
+
* @param hotkey - The hotkey string to validate
|
|
44
|
+
* @returns True if the hotkey is valid (may still have warnings)
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* checkHotkey('Alt+C')
|
|
49
|
+
* // Console: Warning: Alt+C may not work reliably on macOS...
|
|
50
|
+
* // Returns: true
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
declare function checkHotkey(hotkey: Hotkey | (string & {})): boolean;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { assertValidHotkey, checkHotkey, validateHotkey };
|
|
56
|
+
//# sourceMappingURL=validate.d.ts.map
|