@tanstack/hotkeys 0.8.0 → 0.10.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/README.md +15 -1
- package/dist/{manager.utils.js → _event-target.js} +2 -41
- package/dist/_keyboard-event.js +75 -0
- package/dist/_match.js +48 -0
- package/dist/_named-keys.d.ts +6 -0
- package/dist/_named-keys.js +65 -0
- package/dist/_recorder-chord.js +42 -0
- package/dist/_recording-guard.js +55 -0
- package/dist/_registration.js +49 -0
- package/dist/conflicts.d.ts +36 -0
- package/dist/conflicts.js +54 -0
- package/dist/constants.d.ts +16 -144
- package/dist/constants.js +55 -172
- package/dist/display-labels.d.ts +90 -0
- package/dist/display-labels.js +112 -0
- package/dist/format.d.ts +48 -12
- package/dist/format.js +35 -39
- package/dist/hint.d.ts +20 -0
- package/dist/hint.js +23 -0
- package/dist/hotkey-manager.d.ts +14 -12
- package/dist/hotkey-manager.js +70 -21
- package/dist/hotkey-recorder.d.ts +8 -8
- package/dist/hotkey-recorder.js +74 -15
- package/dist/hotkey-sequence-recorder.d.ts +13 -9
- package/dist/hotkey-sequence-recorder.js +90 -28
- package/dist/{hotkey.d.ts → hotkey.types.d.ts} +58 -126
- package/dist/index.d.ts +14 -9
- package/dist/index.js +10 -6
- package/dist/key-state-tracker.d.ts +4 -7
- package/dist/key-state-tracker.js +30 -27
- package/dist/key.types.d.ts +79 -0
- package/dist/match.d.ts +24 -11
- package/dist/match.js +19 -30
- package/dist/parse.d.ts +18 -15
- package/dist/parse.js +39 -22
- package/dist/platform.d.ts +41 -0
- package/dist/platform.js +52 -0
- package/dist/recorder-options.d.ts +31 -0
- package/dist/sequence-manager.d.ts +27 -15
- package/dist/sequence-manager.js +61 -30
- package/dist/validate.d.ts +16 -8
- package/dist/validate.js +8 -13
- package/package.json +8 -13
- package/dist/constants.cjs +0 -535
- package/dist/constants.cjs.map +0 -1
- package/dist/constants.d.cts +0 -290
- package/dist/constants.js.map +0 -1
- package/dist/format.cjs +0 -91
- package/dist/format.cjs.map +0 -1
- package/dist/format.d.cts +0 -60
- package/dist/format.js.map +0 -1
- package/dist/hotkey-manager.cjs +0 -383
- package/dist/hotkey-manager.cjs.map +0 -1
- package/dist/hotkey-manager.d.cts +0 -230
- package/dist/hotkey-manager.js.map +0 -1
- package/dist/hotkey-recorder.cjs +0 -179
- package/dist/hotkey-recorder.cjs.map +0 -1
- package/dist/hotkey-recorder.d.cts +0 -116
- package/dist/hotkey-recorder.js.map +0 -1
- package/dist/hotkey-sequence-recorder.cjs +0 -192
- package/dist/hotkey-sequence-recorder.cjs.map +0 -1
- package/dist/hotkey-sequence-recorder.d.cts +0 -79
- package/dist/hotkey-sequence-recorder.js.map +0 -1
- package/dist/hotkey.d.cts +0 -301
- package/dist/index.cjs +0 -63
- package/dist/index.d.cts +0 -13
- package/dist/key-state-tracker.cjs +0 -197
- package/dist/key-state-tracker.cjs.map +0 -1
- package/dist/key-state-tracker.d.cts +0 -107
- package/dist/key-state-tracker.js.map +0 -1
- package/dist/manager.utils.cjs +0 -115
- package/dist/manager.utils.cjs.map +0 -1
- package/dist/manager.utils.d.cts +0 -13
- package/dist/manager.utils.d.ts +0 -13
- package/dist/manager.utils.js.map +0 -1
- package/dist/match.cjs +0 -153
- package/dist/match.cjs.map +0 -1
- package/dist/match.d.cts +0 -85
- package/dist/match.js.map +0 -1
- package/dist/parse.cjs +0 -228
- package/dist/parse.cjs.map +0 -1
- package/dist/parse.d.cts +0 -128
- package/dist/parse.js.map +0 -1
- package/dist/recorder-chord.cjs +0 -18
- package/dist/recorder-chord.cjs.map +0 -1
- package/dist/recorder-chord.js +0 -18
- package/dist/recorder-chord.js.map +0 -1
- package/dist/sequence-manager.cjs +0 -415
- package/dist/sequence-manager.cjs.map +0 -1
- package/dist/sequence-manager.d.cts +0 -157
- package/dist/sequence-manager.js.map +0 -1
- package/dist/validate.cjs +0 -116
- package/dist/validate.cjs.map +0 -1
- package/dist/validate.d.cts +0 -56
- package/dist/validate.js.map +0 -1
- package/src/constants.ts +0 -603
- package/src/format.ts +0 -142
- package/src/hotkey-manager.ts +0 -759
- package/src/hotkey-recorder.ts +0 -272
- package/src/hotkey-sequence-recorder.ts +0 -328
- package/src/hotkey.ts +0 -436
- package/src/index.ts +0 -11
- package/src/key-state-tracker.ts +0 -249
- package/src/manager.utils.ts +0 -231
- package/src/match.ts +0 -259
- package/src/parse.ts +0 -328
- package/src/recorder-chord.ts +0 -25
- package/src/sequence-manager.ts +0 -734
- package/src/validate.ts +0 -171
package/dist/hotkey-manager.cjs
DELETED
|
@@ -1,383 +0,0 @@
|
|
|
1
|
-
const require_constants = require('./constants.cjs');
|
|
2
|
-
const require_parse = require('./parse.cjs');
|
|
3
|
-
const require_format = require('./format.cjs');
|
|
4
|
-
const require_match = require('./match.cjs');
|
|
5
|
-
const require_manager_utils = require('./manager.utils.cjs');
|
|
6
|
-
let _tanstack_store = require("@tanstack/store");
|
|
7
|
-
|
|
8
|
-
//#region src/hotkey-manager.ts
|
|
9
|
-
/**
|
|
10
|
-
* Creates a public view from an internal registration,
|
|
11
|
-
* stripping the callback function.
|
|
12
|
-
*/
|
|
13
|
-
function toHotkeyRegistrationView(reg) {
|
|
14
|
-
return {
|
|
15
|
-
hotkey: reg.hotkey,
|
|
16
|
-
id: reg.id,
|
|
17
|
-
options: reg.options,
|
|
18
|
-
parsedHotkey: reg.parsedHotkey,
|
|
19
|
-
target: reg.target,
|
|
20
|
-
triggerCount: reg.triggerCount,
|
|
21
|
-
hasFired: reg.hasFired
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
let registrationIdCounter = 0;
|
|
25
|
-
/**
|
|
26
|
-
* Generates a unique ID for hotkey registrations.
|
|
27
|
-
*/
|
|
28
|
-
function generateId() {
|
|
29
|
-
return `hotkey_${++registrationIdCounter}`;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Singleton manager for hotkey registrations.
|
|
33
|
-
*
|
|
34
|
-
* This class provides a centralized way to register and manage keyboard hotkeys.
|
|
35
|
-
* It uses a single event listener for efficiency, regardless of how many hotkeys
|
|
36
|
-
* are registered.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```ts
|
|
40
|
-
* const manager = HotkeyManager.getInstance()
|
|
41
|
-
*
|
|
42
|
-
* const unregister = manager.register('Mod+S', (event, context) => {
|
|
43
|
-
* console.log('Save triggered!')
|
|
44
|
-
* })
|
|
45
|
-
*
|
|
46
|
-
* // Later, to unregister:
|
|
47
|
-
* unregister()
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
var HotkeyManager = class HotkeyManager {
|
|
51
|
-
static #instance = null;
|
|
52
|
-
#platform;
|
|
53
|
-
#targetListeners = /* @__PURE__ */ new Map();
|
|
54
|
-
#targetRegistrations = /* @__PURE__ */ new Map();
|
|
55
|
-
constructor() {
|
|
56
|
-
this.registrations = new _tanstack_store.Store(/* @__PURE__ */ new Map());
|
|
57
|
-
this.#platform = require_constants.detectPlatform();
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Gets the singleton instance of HotkeyManager.
|
|
61
|
-
*/
|
|
62
|
-
static getInstance() {
|
|
63
|
-
if (!HotkeyManager.#instance) HotkeyManager.#instance = new HotkeyManager();
|
|
64
|
-
return HotkeyManager.#instance;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Resets the singleton instance. Useful for testing.
|
|
68
|
-
*/
|
|
69
|
-
static resetInstance() {
|
|
70
|
-
if (HotkeyManager.#instance) {
|
|
71
|
-
HotkeyManager.#instance.destroy();
|
|
72
|
-
HotkeyManager.#instance = null;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Registers a hotkey handler and returns a handle for updating the registration.
|
|
77
|
-
*
|
|
78
|
-
* The returned handle allows updating the callback and options without
|
|
79
|
-
* re-registering, which is useful for avoiding stale closures in React.
|
|
80
|
-
*
|
|
81
|
-
* @param hotkey - The hotkey string (e.g., 'Mod+S') or RawHotkey object
|
|
82
|
-
* @param callback - The function to call when the hotkey is pressed
|
|
83
|
-
* @param options - Options for the hotkey behavior
|
|
84
|
-
* @returns A handle for managing the registration
|
|
85
|
-
*
|
|
86
|
-
* @example
|
|
87
|
-
* ```ts
|
|
88
|
-
* const handle = manager.register('Mod+S', callback)
|
|
89
|
-
*
|
|
90
|
-
* // Update callback without re-registering (avoids stale closures)
|
|
91
|
-
* handle.callback = newCallback
|
|
92
|
-
*
|
|
93
|
-
* // Update options
|
|
94
|
-
* handle.setOptions({ enabled: false })
|
|
95
|
-
*
|
|
96
|
-
* // Unregister when done
|
|
97
|
-
* handle.unregister()
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
register(hotkey, callback, options = {}) {
|
|
101
|
-
if (typeof document === "undefined" && !options.target) {
|
|
102
|
-
let currentCallback = callback;
|
|
103
|
-
return {
|
|
104
|
-
id: generateId(),
|
|
105
|
-
unregister: () => {},
|
|
106
|
-
get callback() {
|
|
107
|
-
return currentCallback;
|
|
108
|
-
},
|
|
109
|
-
set callback(newCallback) {
|
|
110
|
-
currentCallback = newCallback;
|
|
111
|
-
},
|
|
112
|
-
setOptions: () => {},
|
|
113
|
-
get isActive() {
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
const id = generateId();
|
|
119
|
-
const platform = options.platform ?? this.#platform;
|
|
120
|
-
const parsedHotkey = typeof hotkey === "string" ? require_parse.parseHotkey(hotkey, platform) : require_parse.rawHotkeyToParsedHotkey(hotkey, platform);
|
|
121
|
-
const hotkeyStr = typeof hotkey === "string" ? hotkey : require_format.formatHotkey(parsedHotkey);
|
|
122
|
-
const target = options.target ?? document;
|
|
123
|
-
const conflictBehavior = options.conflictBehavior ?? "warn";
|
|
124
|
-
const conflictingRegistration = this.#findConflictingRegistration(hotkeyStr, target);
|
|
125
|
-
if (conflictingRegistration) require_manager_utils.handleConflict(conflictingRegistration.id, hotkeyStr, conflictBehavior, (id) => this.#unregister(id));
|
|
126
|
-
const resolvedIgnoreInputs = options.ignoreInputs ?? require_manager_utils.getDefaultIgnoreInputs(parsedHotkey);
|
|
127
|
-
const registration = {
|
|
128
|
-
id,
|
|
129
|
-
hotkey: hotkeyStr,
|
|
130
|
-
parsedHotkey,
|
|
131
|
-
callback,
|
|
132
|
-
options: {
|
|
133
|
-
...require_manager_utils.defaultHotkeyOptions,
|
|
134
|
-
requireReset: false,
|
|
135
|
-
...options,
|
|
136
|
-
platform,
|
|
137
|
-
ignoreInputs: resolvedIgnoreInputs
|
|
138
|
-
},
|
|
139
|
-
hasFired: false,
|
|
140
|
-
triggerCount: 0,
|
|
141
|
-
target
|
|
142
|
-
};
|
|
143
|
-
this.registrations.setState((prev) => new Map(prev).set(id, registration));
|
|
144
|
-
if (!this.#targetRegistrations.has(target)) this.#targetRegistrations.set(target, /* @__PURE__ */ new Set());
|
|
145
|
-
this.#targetRegistrations.get(target).add(id);
|
|
146
|
-
this.#ensureListenersForTarget(target);
|
|
147
|
-
const manager = this;
|
|
148
|
-
return {
|
|
149
|
-
get id() {
|
|
150
|
-
return id;
|
|
151
|
-
},
|
|
152
|
-
unregister: () => {
|
|
153
|
-
manager.#unregister(id);
|
|
154
|
-
},
|
|
155
|
-
get callback() {
|
|
156
|
-
return manager.registrations.state.get(id)?.callback ?? callback;
|
|
157
|
-
},
|
|
158
|
-
set callback(newCallback) {
|
|
159
|
-
const reg = manager.registrations.state.get(id);
|
|
160
|
-
if (reg) reg.callback = newCallback;
|
|
161
|
-
},
|
|
162
|
-
setOptions: (newOptions) => {
|
|
163
|
-
manager.registrations.setState((prev) => {
|
|
164
|
-
const reg = prev.get(id);
|
|
165
|
-
if (reg) {
|
|
166
|
-
const next = new Map(prev);
|
|
167
|
-
next.set(id, {
|
|
168
|
-
...reg,
|
|
169
|
-
options: {
|
|
170
|
-
...reg.options,
|
|
171
|
-
...newOptions
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
return next;
|
|
175
|
-
}
|
|
176
|
-
return prev;
|
|
177
|
-
});
|
|
178
|
-
},
|
|
179
|
-
get isActive() {
|
|
180
|
-
return manager.registrations.state.has(id);
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Unregisters a hotkey by its registration ID.
|
|
186
|
-
*/
|
|
187
|
-
#unregister(id) {
|
|
188
|
-
const registration = this.registrations.state.get(id);
|
|
189
|
-
if (!registration) return;
|
|
190
|
-
const target = registration.target;
|
|
191
|
-
this.registrations.setState((prev) => {
|
|
192
|
-
const next = new Map(prev);
|
|
193
|
-
next.delete(id);
|
|
194
|
-
return next;
|
|
195
|
-
});
|
|
196
|
-
const targetRegs = this.#targetRegistrations.get(target);
|
|
197
|
-
if (targetRegs) {
|
|
198
|
-
targetRegs.delete(id);
|
|
199
|
-
if (targetRegs.size === 0) this.#removeListenersForTarget(target);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Ensures event listeners are attached for a specific target.
|
|
204
|
-
*/
|
|
205
|
-
#ensureListenersForTarget(target) {
|
|
206
|
-
if (typeof document === "undefined") return;
|
|
207
|
-
if (this.#targetListeners.has(target)) return;
|
|
208
|
-
const keydownHandler = this.#createTargetKeyDownHandler(target);
|
|
209
|
-
const keyupHandler = this.#createTargetKeyUpHandler(target);
|
|
210
|
-
target.addEventListener("keydown", keydownHandler);
|
|
211
|
-
target.addEventListener("keyup", keyupHandler);
|
|
212
|
-
this.#targetListeners.set(target, {
|
|
213
|
-
keydown: keydownHandler,
|
|
214
|
-
keyup: keyupHandler
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Removes event listeners for a specific target.
|
|
219
|
-
*/
|
|
220
|
-
#removeListenersForTarget(target) {
|
|
221
|
-
if (typeof document === "undefined") return;
|
|
222
|
-
const listeners = this.#targetListeners.get(target);
|
|
223
|
-
if (!listeners) return;
|
|
224
|
-
target.removeEventListener("keydown", listeners.keydown);
|
|
225
|
-
target.removeEventListener("keyup", listeners.keyup);
|
|
226
|
-
this.#targetListeners.delete(target);
|
|
227
|
-
this.#targetRegistrations.delete(target);
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Processes keyboard events for a specific target and event type.
|
|
231
|
-
*/
|
|
232
|
-
#processTargetEvent(event, target, eventType) {
|
|
233
|
-
const targetRegs = this.#targetRegistrations.get(target);
|
|
234
|
-
if (!targetRegs) return;
|
|
235
|
-
for (const id of targetRegs) {
|
|
236
|
-
const registration = this.registrations.state.get(id);
|
|
237
|
-
if (!registration) continue;
|
|
238
|
-
if (!require_manager_utils.isEventForTarget(event, target)) continue;
|
|
239
|
-
if (!registration.options.enabled) continue;
|
|
240
|
-
if (registration.options.ignoreInputs !== false) {
|
|
241
|
-
if (require_manager_utils.shouldIgnoreInputEvent(event, target, registration.target)) continue;
|
|
242
|
-
}
|
|
243
|
-
if (eventType === "keydown") {
|
|
244
|
-
if (registration.options.eventType !== "keydown") continue;
|
|
245
|
-
if (require_match.matchesKeyboardEvent(event, registration.parsedHotkey, registration.options.platform)) {
|
|
246
|
-
if (registration.options.preventDefault) event.preventDefault();
|
|
247
|
-
if (registration.options.stopPropagation) event.stopPropagation();
|
|
248
|
-
if (!registration.options.requireReset || !registration.hasFired) {
|
|
249
|
-
this.#executeHotkeyCallback(registration, event);
|
|
250
|
-
if (registration.options.requireReset) registration.hasFired = true;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
} else {
|
|
254
|
-
if (registration.options.eventType === "keyup") {
|
|
255
|
-
if (require_match.matchesKeyboardEvent(event, registration.parsedHotkey, registration.options.platform)) this.#executeHotkeyCallback(registration, event);
|
|
256
|
-
}
|
|
257
|
-
if (registration.options.requireReset && registration.hasFired) {
|
|
258
|
-
if (this.#shouldResetRegistration(registration, event)) registration.hasFired = false;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Executes a hotkey callback with proper event handling.
|
|
265
|
-
*/
|
|
266
|
-
#executeHotkeyCallback(registration, event) {
|
|
267
|
-
if (registration.options.preventDefault) event.preventDefault();
|
|
268
|
-
if (registration.options.stopPropagation) event.stopPropagation();
|
|
269
|
-
registration.triggerCount++;
|
|
270
|
-
this.registrations.setState((prev) => new Map(prev));
|
|
271
|
-
const context = {
|
|
272
|
-
hotkey: registration.hotkey,
|
|
273
|
-
parsedHotkey: registration.parsedHotkey
|
|
274
|
-
};
|
|
275
|
-
registration.callback(event, context);
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Creates a keydown handler for a specific target.
|
|
279
|
-
*/
|
|
280
|
-
#createTargetKeyDownHandler(target) {
|
|
281
|
-
return (event) => {
|
|
282
|
-
this.#processTargetEvent(event, target, "keydown");
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* Creates a keyup handler for a specific target.
|
|
287
|
-
*/
|
|
288
|
-
#createTargetKeyUpHandler(target) {
|
|
289
|
-
return (event) => {
|
|
290
|
-
this.#processTargetEvent(event, target, "keyup");
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* Finds an existing registration with the same hotkey and target.
|
|
295
|
-
*/
|
|
296
|
-
#findConflictingRegistration(hotkey, target) {
|
|
297
|
-
for (const registration of this.registrations.state.values()) if (registration.hotkey === hotkey && registration.target === target) return registration;
|
|
298
|
-
return null;
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* Determines if a registration should be reset based on the keyup event.
|
|
302
|
-
*/
|
|
303
|
-
#shouldResetRegistration(registration, event) {
|
|
304
|
-
const parsed = registration.parsedHotkey;
|
|
305
|
-
const releasedKey = require_constants.normalizeKeyName(event.key);
|
|
306
|
-
const parsedKeyNormalized = parsed.key.length === 1 ? parsed.key.toUpperCase() : parsed.key;
|
|
307
|
-
if ((releasedKey.length === 1 ? releasedKey.toUpperCase() : releasedKey) === parsedKeyNormalized) return true;
|
|
308
|
-
if (parsed.ctrl && releasedKey === "Control") return true;
|
|
309
|
-
if (parsed.shift && releasedKey === "Shift") return true;
|
|
310
|
-
if (parsed.alt && releasedKey === "Alt") return true;
|
|
311
|
-
if (parsed.meta && releasedKey === "Meta") return true;
|
|
312
|
-
return false;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Triggers a registration's callback programmatically from devtools.
|
|
316
|
-
* Creates a synthetic KeyboardEvent and invokes the callback.
|
|
317
|
-
*
|
|
318
|
-
* @param id - The registration ID to trigger
|
|
319
|
-
* @returns True if the registration was found and triggered
|
|
320
|
-
*/
|
|
321
|
-
triggerRegistration(id) {
|
|
322
|
-
const registration = this.registrations.state.get(id);
|
|
323
|
-
if (!registration) return false;
|
|
324
|
-
const parsed = registration.parsedHotkey;
|
|
325
|
-
const syntheticEvent = new KeyboardEvent(registration.options.eventType ?? "keydown", {
|
|
326
|
-
key: parsed.key,
|
|
327
|
-
ctrlKey: parsed.ctrl,
|
|
328
|
-
shiftKey: parsed.shift,
|
|
329
|
-
altKey: parsed.alt,
|
|
330
|
-
metaKey: parsed.meta,
|
|
331
|
-
bubbles: true,
|
|
332
|
-
cancelable: true
|
|
333
|
-
});
|
|
334
|
-
registration.triggerCount++;
|
|
335
|
-
this.registrations.setState((prev) => new Map(prev));
|
|
336
|
-
registration.callback(syntheticEvent, {
|
|
337
|
-
hotkey: registration.hotkey,
|
|
338
|
-
parsedHotkey: registration.parsedHotkey
|
|
339
|
-
});
|
|
340
|
-
return true;
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Gets the number of registered hotkeys.
|
|
344
|
-
*/
|
|
345
|
-
getRegistrationCount() {
|
|
346
|
-
return this.registrations.state.size;
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* Checks if a specific hotkey is registered.
|
|
350
|
-
*
|
|
351
|
-
* @param hotkey - The hotkey string to check
|
|
352
|
-
* @param target - Optional target element to match (if provided, both hotkey and target must match)
|
|
353
|
-
* @returns True if a matching registration exists
|
|
354
|
-
*/
|
|
355
|
-
isRegistered(hotkey, target) {
|
|
356
|
-
for (const registration of this.registrations.state.values()) if (registration.hotkey === hotkey) {
|
|
357
|
-
if (target === void 0 || registration.target === target) return true;
|
|
358
|
-
}
|
|
359
|
-
return false;
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* Destroys the manager and removes all listeners.
|
|
363
|
-
*/
|
|
364
|
-
destroy() {
|
|
365
|
-
for (const target of this.#targetListeners.keys()) this.#removeListenersForTarget(target);
|
|
366
|
-
this.registrations.setState(() => /* @__PURE__ */ new Map());
|
|
367
|
-
this.#targetListeners.clear();
|
|
368
|
-
this.#targetRegistrations.clear();
|
|
369
|
-
}
|
|
370
|
-
};
|
|
371
|
-
/**
|
|
372
|
-
* Gets the singleton HotkeyManager instance.
|
|
373
|
-
* Convenience function for accessing the manager.
|
|
374
|
-
*/
|
|
375
|
-
function getHotkeyManager() {
|
|
376
|
-
return HotkeyManager.getInstance();
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
//#endregion
|
|
380
|
-
exports.HotkeyManager = HotkeyManager;
|
|
381
|
-
exports.getHotkeyManager = getHotkeyManager;
|
|
382
|
-
exports.toHotkeyRegistrationView = toHotkeyRegistrationView;
|
|
383
|
-
//# sourceMappingURL=hotkey-manager.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hotkey-manager.cjs","names":["#instance","Store","#platform","detectPlatform","parseHotkey","rawHotkeyToParsedHotkey","formatHotkey","#findConflictingRegistration","#unregister","getDefaultIgnoreInputs","defaultHotkeyOptions","#targetRegistrations","#ensureListenersForTarget","#removeListenersForTarget","#targetListeners","#createTargetKeyDownHandler","#createTargetKeyUpHandler","isEventForTarget","shouldIgnoreInputEvent","matchesKeyboardEvent","#executeHotkeyCallback","#shouldResetRegistration","#processTargetEvent","normalizeKeyName"],"sources":["../src/hotkey-manager.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { detectPlatform, normalizeKeyName } from './constants'\nimport { formatHotkey } from './format'\nimport { parseHotkey, rawHotkeyToParsedHotkey } from './parse'\nimport { matchesKeyboardEvent } from './match'\nimport {\n defaultHotkeyOptions,\n getDefaultIgnoreInputs,\n handleConflict,\n isEventForTarget,\n shouldIgnoreInputEvent,\n} from './manager.utils'\nimport type { ConflictBehavior } from './manager.utils'\nimport type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n HotkeyMeta,\n ParsedHotkey,\n RegisterableHotkey,\n} from './hotkey'\n\nexport type { ConflictBehavior }\n\n/**\n * Options for registering a hotkey.\n */\nexport interface HotkeyOptions {\n /** Behavior when this hotkey conflicts with an existing registration on the same target. Defaults to 'warn' */\n conflictBehavior?: ConflictBehavior\n /**\n * Soft-disable: when `false`, the callback does not run but the registration\n * stays in `HotkeyManager` (and in devtools). Toggling this should update the\n * existing handle via `setOptions` rather than unregistering. Defaults to `true`.\n */\n enabled?: boolean\n /** The event type to listen for. Defaults to 'keydown' */\n eventType?: 'keydown' | 'keyup'\n /** Whether to ignore hotkeys when keyboard events originate from input-like elements (text inputs, textarea, select, contenteditable — button-type inputs like type=button/submit/reset are not ignored). Defaults based on hotkey: true for single keys and Shift/Alt combos; false for Ctrl/Meta shortcuts and Escape */\n ignoreInputs?: boolean\n /** The target platform for resolving 'Mod' */\n platform?: 'mac' | 'windows' | 'linux'\n /** Prevent the default browser action when the hotkey matches. Defaults to true */\n preventDefault?: boolean\n /** If true, only trigger once until all keys are released. Default: false */\n requireReset?: boolean\n /** Stop event propagation when the hotkey matches. Defaults to true */\n stopPropagation?: boolean\n /** The DOM element to attach the event listener to. Defaults to document. */\n target?: HTMLElement | Document | Window | null\n /** Optional metadata (name, description, custom fields via declaration merging) */\n meta?: HotkeyMeta\n}\n\n/**\n * A registered hotkey handler in the HotkeyManager.\n */\nexport interface HotkeyRegistration {\n /** The callback to invoke */\n callback: HotkeyCallback\n /** Whether this registration has fired and needs reset (for requireReset) */\n hasFired: boolean\n /** The original hotkey string */\n hotkey: Hotkey\n /** Unique identifier for this registration */\n id: string\n /** Options for this registration */\n options: HotkeyOptions\n /** The parsed hotkey */\n parsedHotkey: ParsedHotkey\n /** The resolved target element for this registration */\n target: HTMLElement | Document | Window\n /** How many times this registration's callback has been triggered */\n triggerCount: number\n}\n\n/**\n * Public view of a hotkey registration for display and introspection.\n * Omits the callback function which is an internal implementation detail.\n */\nexport interface HotkeyRegistrationView {\n /** The original hotkey string */\n hotkey: Hotkey\n /** Unique identifier for this registration */\n id: string\n /** Options for this registration */\n options: HotkeyOptions\n /** The parsed hotkey */\n parsedHotkey: ParsedHotkey\n /** The resolved target element for this registration */\n target: HTMLElement | Document | Window\n /** How many times this registration's callback has been triggered */\n triggerCount: number\n /** Whether this registration has fired and needs reset (for requireReset) */\n hasFired: boolean\n}\n\n/**\n * Creates a public view from an internal registration,\n * stripping the callback function.\n */\nexport function toHotkeyRegistrationView(\n reg: HotkeyRegistration,\n): HotkeyRegistrationView {\n return {\n hotkey: reg.hotkey,\n id: reg.id,\n options: reg.options,\n parsedHotkey: reg.parsedHotkey,\n target: reg.target,\n triggerCount: reg.triggerCount,\n hasFired: reg.hasFired,\n }\n}\n\n/**\n * A handle returned from HotkeyManager.register() that allows updating\n * the callback and options without re-registering the hotkey.\n *\n * @example\n * ```ts\n * const handle = manager.register('Mod+S', callback, options)\n *\n * // Update callback without re-registering (avoids stale closures)\n * handle.callback = newCallback\n *\n * // Update options without re-registering\n * handle.setOptions({ enabled: false })\n *\n * // Check if still active\n * if (handle.isActive) {\n * // ...\n * }\n *\n * // Unregister when done\n * handle.unregister()\n * ```\n */\nexport interface HotkeyRegistrationHandle {\n /**\n * The callback function. Can be set directly to update without re-registering.\n * This avoids stale closures when the callback references React state.\n */\n callback: HotkeyCallback\n /** Unique identifier for this registration */\n readonly id: string\n /** Check if this registration is still active (not unregistered) */\n readonly isActive: boolean\n /**\n * Update options (merged with existing options).\n * Useful for updating `enabled`, `preventDefault`, etc. without re-registering.\n */\n setOptions: (options: Partial<HotkeyOptions>) => void\n /** Unregister this hotkey */\n unregister: () => void\n}\n\nlet registrationIdCounter = 0\n\n/**\n * Generates a unique ID for hotkey registrations.\n */\nfunction generateId(): string {\n return `hotkey_${++registrationIdCounter}`\n}\n\n/**\n * Singleton manager for hotkey registrations.\n *\n * This class provides a centralized way to register and manage keyboard hotkeys.\n * It uses a single event listener for efficiency, regardless of how many hotkeys\n * are registered.\n *\n * @example\n * ```ts\n * const manager = HotkeyManager.getInstance()\n *\n * const unregister = manager.register('Mod+S', (event, context) => {\n * console.log('Save triggered!')\n * })\n *\n * // Later, to unregister:\n * unregister()\n * ```\n */\nexport class HotkeyManager {\n static #instance: HotkeyManager | null = null\n\n /**\n * The TanStack Store containing all hotkey registrations.\n * Use this to subscribe to registration changes or access current registrations.\n *\n * @example\n * ```ts\n * const manager = HotkeyManager.getInstance()\n *\n * // Subscribe to registration changes\n * const unsubscribe = manager.registrations.subscribe(() => {\n * console.log('Registrations changed:', manager.registrations.state.size)\n * })\n *\n * // Access current registrations\n * for (const [id, reg] of manager.registrations.state) {\n * console.log(reg.hotkey, reg.options.enabled)\n * }\n * ```\n */\n readonly registrations: Store<Map<string, HotkeyRegistration>> = new Store(\n new Map(),\n )\n #platform: 'mac' | 'windows' | 'linux'\n #targetListeners: Map<\n HTMLElement | Document | Window,\n {\n keydown: (event: KeyboardEvent) => void\n keyup: (event: KeyboardEvent) => void\n }\n > = new Map()\n #targetRegistrations: Map<HTMLElement | Document | Window, Set<string>> =\n new Map()\n\n private constructor() {\n this.#platform = detectPlatform()\n }\n\n /**\n * Gets the singleton instance of HotkeyManager.\n */\n static getInstance(): HotkeyManager {\n if (!HotkeyManager.#instance) {\n HotkeyManager.#instance = new HotkeyManager()\n }\n return HotkeyManager.#instance\n }\n\n /**\n * Resets the singleton instance. Useful for testing.\n */\n static resetInstance(): void {\n if (HotkeyManager.#instance) {\n HotkeyManager.#instance.destroy()\n HotkeyManager.#instance = null\n }\n }\n\n /**\n * Registers a hotkey handler and returns a handle for updating the registration.\n *\n * The returned handle allows updating the callback and options without\n * re-registering, which is useful for avoiding stale closures in React.\n *\n * @param hotkey - The hotkey string (e.g., 'Mod+S') or RawHotkey object\n * @param callback - The function to call when the hotkey is pressed\n * @param options - Options for the hotkey behavior\n * @returns A handle for managing the registration\n *\n * @example\n * ```ts\n * const handle = manager.register('Mod+S', callback)\n *\n * // Update callback without re-registering (avoids stale closures)\n * handle.callback = newCallback\n *\n * // Update options\n * handle.setOptions({ enabled: false })\n *\n * // Unregister when done\n * handle.unregister()\n * ```\n */\n register(\n hotkey: RegisterableHotkey,\n callback: HotkeyCallback,\n options: HotkeyOptions = {},\n ): HotkeyRegistrationHandle {\n // In SSR environments, return a no-op handle since there's no DOM to attach listeners to\n if (typeof document === 'undefined' && !options.target) {\n let currentCallback = callback\n return {\n id: generateId(),\n unregister: () => {},\n get callback() {\n return currentCallback\n },\n set callback(newCallback: HotkeyCallback) {\n currentCallback = newCallback\n },\n setOptions: () => {},\n get isActive() {\n return false\n },\n }\n }\n\n const id = generateId()\n const platform = options.platform ?? this.#platform\n const parsedHotkey =\n typeof hotkey === 'string'\n ? parseHotkey(hotkey, platform)\n : rawHotkeyToParsedHotkey(hotkey, platform)\n const hotkeyStr = (\n typeof hotkey === 'string' ? hotkey : formatHotkey(parsedHotkey)\n ) as Hotkey\n\n // Resolve target: default to document if not provided\n // Note: SSR case without explicit target is handled above\n const target = options.target ?? document\n\n // Resolve conflict behavior\n const conflictBehavior = options.conflictBehavior ?? 'warn'\n\n // Check for existing registrations with the same hotkey and target\n const conflictingRegistration = this.#findConflictingRegistration(\n hotkeyStr,\n target,\n )\n\n if (conflictingRegistration) {\n handleConflict(\n conflictingRegistration.id,\n hotkeyStr,\n conflictBehavior,\n (id) => this.#unregister(id),\n )\n }\n\n const resolvedIgnoreInputs =\n options.ignoreInputs ?? getDefaultIgnoreInputs(parsedHotkey)\n\n const baseOptions = {\n ...defaultHotkeyOptions,\n requireReset: false,\n ...options,\n platform,\n }\n\n const registration: HotkeyRegistration = {\n id,\n hotkey: hotkeyStr,\n parsedHotkey,\n callback,\n options: { ...baseOptions, ignoreInputs: resolvedIgnoreInputs },\n hasFired: false,\n triggerCount: 0,\n target,\n }\n\n this.registrations.setState((prev) => new Map(prev).set(id, registration))\n\n // Track registration for this target\n if (!this.#targetRegistrations.has(target)) {\n this.#targetRegistrations.set(target, new Set())\n }\n this.#targetRegistrations.get(target)!.add(id)\n\n // Ensure listeners are attached for this target\n this.#ensureListenersForTarget(target)\n\n // Create and return the handle\n const manager = this\n const handle: HotkeyRegistrationHandle = {\n get id() {\n return id\n },\n unregister: () => {\n manager.#unregister(id)\n },\n get callback() {\n const reg = manager.registrations.state.get(id)\n return reg?.callback ?? callback\n },\n set callback(newCallback: HotkeyCallback) {\n const reg = manager.registrations.state.get(id)\n if (reg) {\n reg.callback = newCallback\n }\n },\n setOptions: (newOptions: Partial<HotkeyOptions>) => {\n manager.registrations.setState((prev) => {\n const reg = prev.get(id)\n if (reg) {\n const next = new Map(prev)\n next.set(id, { ...reg, options: { ...reg.options, ...newOptions } })\n return next\n }\n return prev\n })\n },\n get isActive() {\n return manager.registrations.state.has(id)\n },\n }\n\n return handle\n }\n\n /**\n * Unregisters a hotkey by its registration ID.\n */\n #unregister(id: string): void {\n const registration = this.registrations.state.get(id)\n if (!registration) {\n return\n }\n\n const target = registration.target\n\n // Remove registration\n this.registrations.setState((prev) => {\n const next = new Map(prev)\n next.delete(id)\n return next\n })\n\n // Remove from target registrations tracking\n const targetRegs = this.#targetRegistrations.get(target)\n if (targetRegs) {\n targetRegs.delete(id)\n // If no more registrations for this target, remove listeners\n if (targetRegs.size === 0) {\n this.#removeListenersForTarget(target)\n }\n }\n }\n\n /**\n * Ensures event listeners are attached for a specific target.\n */\n #ensureListenersForTarget(target: HTMLElement | Document | Window): void {\n if (typeof document === 'undefined') {\n return // SSR safety\n }\n\n // Skip if listeners already exist for this target\n if (this.#targetListeners.has(target)) {\n return\n }\n\n const keydownHandler = this.#createTargetKeyDownHandler(target)\n const keyupHandler = this.#createTargetKeyUpHandler(target)\n\n target.addEventListener('keydown', keydownHandler as EventListener)\n target.addEventListener('keyup', keyupHandler as EventListener)\n\n this.#targetListeners.set(target, {\n keydown: keydownHandler,\n keyup: keyupHandler,\n })\n }\n\n /**\n * Removes event listeners for a specific target.\n */\n #removeListenersForTarget(target: HTMLElement | Document | Window): void {\n if (typeof document === 'undefined') {\n return\n }\n\n const listeners = this.#targetListeners.get(target)\n if (!listeners) {\n return\n }\n\n target.removeEventListener('keydown', listeners.keydown as EventListener)\n target.removeEventListener('keyup', listeners.keyup as EventListener)\n\n this.#targetListeners.delete(target)\n this.#targetRegistrations.delete(target)\n }\n\n /**\n * Processes keyboard events for a specific target and event type.\n */\n #processTargetEvent(\n event: KeyboardEvent,\n target: HTMLElement | Document | Window,\n eventType: 'keydown' | 'keyup',\n ): void {\n const targetRegs = this.#targetRegistrations.get(target)\n if (!targetRegs) {\n return\n }\n\n for (const id of targetRegs) {\n const registration = this.registrations.state.get(id)\n if (!registration) {\n continue\n }\n\n // Check if event originated from or bubbled to this target\n if (!isEventForTarget(event, target)) {\n continue\n }\n\n if (!registration.options.enabled) {\n continue\n }\n\n // Check if we should ignore input elements (defaults to true)\n if (registration.options.ignoreInputs !== false) {\n if (shouldIgnoreInputEvent(event, target, registration.target)) {\n continue\n }\n }\n\n // Handle keydown events\n if (eventType === 'keydown') {\n if (registration.options.eventType !== 'keydown') {\n continue\n }\n\n // Check if the hotkey matches first\n const matches = matchesKeyboardEvent(\n event,\n registration.parsedHotkey,\n registration.options.platform,\n )\n\n if (matches) {\n // Always apply preventDefault/stopPropagation if the hotkey matches,\n // even when requireReset is active and has already fired\n if (registration.options.preventDefault) {\n event.preventDefault()\n }\n if (registration.options.stopPropagation) {\n event.stopPropagation()\n }\n\n // Only execute callback if requireReset is not active or hasn't fired yet\n if (!registration.options.requireReset || !registration.hasFired) {\n this.#executeHotkeyCallback(registration, event)\n\n // Mark as fired if requireReset is enabled\n if (registration.options.requireReset) {\n registration.hasFired = true\n }\n }\n }\n }\n // Handle keyup events\n else {\n if (registration.options.eventType === 'keyup') {\n if (\n matchesKeyboardEvent(\n event,\n registration.parsedHotkey,\n registration.options.platform,\n )\n ) {\n this.#executeHotkeyCallback(registration, event)\n }\n }\n\n // Reset hasFired when any key in the hotkey is released\n if (registration.options.requireReset && registration.hasFired) {\n if (this.#shouldResetRegistration(registration, event)) {\n registration.hasFired = false\n }\n }\n }\n }\n }\n\n /**\n * Executes a hotkey callback with proper event handling.\n */\n #executeHotkeyCallback(\n registration: HotkeyRegistration,\n event: KeyboardEvent,\n ): void {\n if (registration.options.preventDefault) {\n event.preventDefault()\n }\n if (registration.options.stopPropagation) {\n event.stopPropagation()\n }\n\n registration.triggerCount++\n\n // Notify the store so subscribers (e.g. devtools) see the updated count.\n // We create a new Map but keep the same registration reference to preserve\n // identity for mutation-based fields like hasFired.\n this.registrations.setState((prev) => new Map(prev))\n\n const context: HotkeyCallbackContext = {\n hotkey: registration.hotkey,\n parsedHotkey: registration.parsedHotkey,\n }\n\n registration.callback(event, context)\n }\n\n /**\n * Creates a keydown handler for a specific target.\n */\n #createTargetKeyDownHandler(\n target: HTMLElement | Document | Window,\n ): (event: KeyboardEvent) => void {\n return (event: KeyboardEvent) => {\n this.#processTargetEvent(event, target, 'keydown')\n }\n }\n\n /**\n * Creates a keyup handler for a specific target.\n */\n #createTargetKeyUpHandler(\n target: HTMLElement | Document | Window,\n ): (event: KeyboardEvent) => void {\n return (event: KeyboardEvent) => {\n this.#processTargetEvent(event, target, 'keyup')\n }\n }\n\n /**\n * Finds an existing registration with the same hotkey and target.\n */\n #findConflictingRegistration(\n hotkey: Hotkey,\n target: HTMLElement | Document | Window,\n ): HotkeyRegistration | null {\n for (const registration of this.registrations.state.values()) {\n if (registration.hotkey === hotkey && registration.target === target) {\n return registration\n }\n }\n return null\n }\n\n /**\n * Determines if a registration should be reset based on the keyup event.\n */\n #shouldResetRegistration(\n registration: HotkeyRegistration,\n event: KeyboardEvent,\n ): boolean {\n const parsed = registration.parsedHotkey\n const releasedKey = normalizeKeyName(event.key)\n\n // Reset if the main key is released\n // Compare case-insensitively for single-letter keys\n const parsedKeyNormalized =\n parsed.key.length === 1 ? parsed.key.toUpperCase() : parsed.key\n const releasedKeyNormalized =\n releasedKey.length === 1 ? releasedKey.toUpperCase() : releasedKey\n\n if (releasedKeyNormalized === parsedKeyNormalized) {\n return true\n }\n\n // Reset if any required modifier is released\n // Use normalized key names and check against canonical modifier names\n if (parsed.ctrl && releasedKey === 'Control') {\n return true\n }\n if (parsed.shift && releasedKey === 'Shift') {\n return true\n }\n if (parsed.alt && releasedKey === 'Alt') {\n return true\n }\n if (parsed.meta && releasedKey === 'Meta') {\n return true\n }\n\n return false\n }\n\n /**\n * Triggers a registration's callback programmatically from devtools.\n * Creates a synthetic KeyboardEvent and invokes the callback.\n *\n * @param id - The registration ID to trigger\n * @returns True if the registration was found and triggered\n */\n triggerRegistration(id: string): boolean {\n const registration = this.registrations.state.get(id)\n if (!registration) {\n return false\n }\n\n const parsed = registration.parsedHotkey\n const syntheticEvent = new KeyboardEvent(\n registration.options.eventType ?? 'keydown',\n {\n key: parsed.key,\n ctrlKey: parsed.ctrl,\n shiftKey: parsed.shift,\n altKey: parsed.alt,\n metaKey: parsed.meta,\n bubbles: true,\n cancelable: true,\n },\n )\n\n registration.triggerCount++\n\n // Notify the store so subscribers (e.g. devtools) see the updated count\n this.registrations.setState((prev) => new Map(prev))\n\n registration.callback(syntheticEvent, {\n hotkey: registration.hotkey,\n parsedHotkey: registration.parsedHotkey,\n })\n\n return true\n }\n\n /**\n * Gets the number of registered hotkeys.\n */\n getRegistrationCount(): number {\n return this.registrations.state.size\n }\n\n /**\n * Checks if a specific hotkey is registered.\n *\n * @param hotkey - The hotkey string to check\n * @param target - Optional target element to match (if provided, both hotkey and target must match)\n * @returns True if a matching registration exists\n */\n isRegistered(\n hotkey: Hotkey,\n target?: HTMLElement | Document | Window,\n ): boolean {\n for (const registration of this.registrations.state.values()) {\n if (registration.hotkey === hotkey) {\n // If target is specified, both must match\n if (target === undefined || registration.target === target) {\n return true\n }\n }\n }\n return false\n }\n\n /**\n * Destroys the manager and removes all listeners.\n */\n destroy(): void {\n // Remove all target listeners\n for (const target of this.#targetListeners.keys()) {\n this.#removeListenersForTarget(target)\n }\n\n this.registrations.setState(() => new Map())\n this.#targetListeners.clear()\n this.#targetRegistrations.clear()\n }\n}\n\n/**\n * Gets the singleton HotkeyManager instance.\n * Convenience function for accessing the manager.\n */\nexport function getHotkeyManager(): HotkeyManager {\n return HotkeyManager.getInstance()\n}\n"],"mappings":";;;;;;;;;;;;AAqGA,SAAgB,yBACd,KACwB;AACxB,QAAO;EACL,QAAQ,IAAI;EACZ,IAAI,IAAI;EACR,SAAS,IAAI;EACb,cAAc,IAAI;EAClB,QAAQ,IAAI;EACZ,cAAc,IAAI;EAClB,UAAU,IAAI;EACf;;AA6CH,IAAI,wBAAwB;;;;AAK5B,SAAS,aAAqB;AAC5B,QAAO,UAAU,EAAE;;;;;;;;;;;;;;;;;;;;;AAsBrB,IAAa,gBAAb,MAAa,cAAc;CACzB,QAAOA,WAAkC;CAwBzC;CACA,mCAMI,IAAI,KAAK;CACb,uCACE,IAAI,KAAK;CAEX,AAAQ,cAAc;uBAd2C,IAAIC,sCACnE,IAAI,KAAK,CACV;AAaC,QAAKC,WAAYC,kCAAgB;;;;;CAMnC,OAAO,cAA6B;AAClC,MAAI,CAAC,eAAcH,SACjB,gBAAcA,WAAY,IAAI,eAAe;AAE/C,SAAO,eAAcA;;;;;CAMvB,OAAO,gBAAsB;AAC3B,MAAI,eAAcA,UAAW;AAC3B,kBAAcA,SAAU,SAAS;AACjC,kBAAcA,WAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6B9B,SACE,QACA,UACA,UAAyB,EAAE,EACD;AAE1B,MAAI,OAAO,aAA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GACf,SAAS,OAAO;GAChB,SAAS;GACT,YAAY;GACb,CACF;AAED,eAAa;AAGb,OAAK,cAAc,UAAU,SAAS,IAAI,IAAI,KAAK,CAAC;AAEpD,eAAa,SAAS,gBAAgB;GACpC,QAAQ,aAAa;GACrB,cAAc,aAAa;GAC5B,CAAC;AAEF,SAAO;;;;;CAMT,uBAA+B;AAC7B,SAAO,KAAK,cAAc,MAAM;;;;;;;;;CAUlC,aACE,QACA,QACS;AACT,OAAK,MAAM,gBAAgB,KAAK,cAAc,MAAM,QAAQ,CAC1D,KAAI,aAAa,WAAW,QAE1B;OAAI,WAAW,UAAa,aAAa,WAAW,OAClD,QAAO;;AAIb,SAAO;;;;;CAMT,UAAgB;AAEd,OAAK,MAAM,UAAU,MAAKT,gBAAiB,MAAM,CAC/C,OAAKD,yBAA0B,OAAO;AAGxC,OAAK,cAAc,+BAAe,IAAI,KAAK,CAAC;AAC5C,QAAKC,gBAAiB,OAAO;AAC7B,QAAKH,oBAAqB,OAAO;;;;;;;AAQrC,SAAgB,mBAAkC;AAChD,QAAO,cAAc,aAAa"}
|
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import { Hotkey, HotkeyCallback, HotkeyMeta, ParsedHotkey, RegisterableHotkey } from "./hotkey.cjs";
|
|
2
|
-
import { ConflictBehavior } from "./manager.utils.cjs";
|
|
3
|
-
import { Store } from "@tanstack/store";
|
|
4
|
-
|
|
5
|
-
//#region src/hotkey-manager.d.ts
|
|
6
|
-
/**
|
|
7
|
-
* Options for registering a hotkey.
|
|
8
|
-
*/
|
|
9
|
-
interface HotkeyOptions {
|
|
10
|
-
/** Behavior when this hotkey conflicts with an existing registration on the same target. Defaults to 'warn' */
|
|
11
|
-
conflictBehavior?: ConflictBehavior;
|
|
12
|
-
/**
|
|
13
|
-
* Soft-disable: when `false`, the callback does not run but the registration
|
|
14
|
-
* stays in `HotkeyManager` (and in devtools). Toggling this should update the
|
|
15
|
-
* existing handle via `setOptions` rather than unregistering. Defaults to `true`.
|
|
16
|
-
*/
|
|
17
|
-
enabled?: boolean;
|
|
18
|
-
/** The event type to listen for. Defaults to 'keydown' */
|
|
19
|
-
eventType?: 'keydown' | 'keyup';
|
|
20
|
-
/** Whether to ignore hotkeys when keyboard events originate from input-like elements (text inputs, textarea, select, contenteditable — button-type inputs like type=button/submit/reset are not ignored). Defaults based on hotkey: true for single keys and Shift/Alt combos; false for Ctrl/Meta shortcuts and Escape */
|
|
21
|
-
ignoreInputs?: boolean;
|
|
22
|
-
/** The target platform for resolving 'Mod' */
|
|
23
|
-
platform?: 'mac' | 'windows' | 'linux';
|
|
24
|
-
/** Prevent the default browser action when the hotkey matches. Defaults to true */
|
|
25
|
-
preventDefault?: boolean;
|
|
26
|
-
/** If true, only trigger once until all keys are released. Default: false */
|
|
27
|
-
requireReset?: boolean;
|
|
28
|
-
/** Stop event propagation when the hotkey matches. Defaults to true */
|
|
29
|
-
stopPropagation?: boolean;
|
|
30
|
-
/** The DOM element to attach the event listener to. Defaults to document. */
|
|
31
|
-
target?: HTMLElement | Document | Window | null;
|
|
32
|
-
/** Optional metadata (name, description, custom fields via declaration merging) */
|
|
33
|
-
meta?: HotkeyMeta;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* A registered hotkey handler in the HotkeyManager.
|
|
37
|
-
*/
|
|
38
|
-
interface HotkeyRegistration {
|
|
39
|
-
/** The callback to invoke */
|
|
40
|
-
callback: HotkeyCallback;
|
|
41
|
-
/** Whether this registration has fired and needs reset (for requireReset) */
|
|
42
|
-
hasFired: boolean;
|
|
43
|
-
/** The original hotkey string */
|
|
44
|
-
hotkey: Hotkey;
|
|
45
|
-
/** Unique identifier for this registration */
|
|
46
|
-
id: string;
|
|
47
|
-
/** Options for this registration */
|
|
48
|
-
options: HotkeyOptions;
|
|
49
|
-
/** The parsed hotkey */
|
|
50
|
-
parsedHotkey: ParsedHotkey;
|
|
51
|
-
/** The resolved target element for this registration */
|
|
52
|
-
target: HTMLElement | Document | Window;
|
|
53
|
-
/** How many times this registration's callback has been triggered */
|
|
54
|
-
triggerCount: number;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Public view of a hotkey registration for display and introspection.
|
|
58
|
-
* Omits the callback function which is an internal implementation detail.
|
|
59
|
-
*/
|
|
60
|
-
interface HotkeyRegistrationView {
|
|
61
|
-
/** The original hotkey string */
|
|
62
|
-
hotkey: Hotkey;
|
|
63
|
-
/** Unique identifier for this registration */
|
|
64
|
-
id: string;
|
|
65
|
-
/** Options for this registration */
|
|
66
|
-
options: HotkeyOptions;
|
|
67
|
-
/** The parsed hotkey */
|
|
68
|
-
parsedHotkey: ParsedHotkey;
|
|
69
|
-
/** The resolved target element for this registration */
|
|
70
|
-
target: HTMLElement | Document | Window;
|
|
71
|
-
/** How many times this registration's callback has been triggered */
|
|
72
|
-
triggerCount: number;
|
|
73
|
-
/** Whether this registration has fired and needs reset (for requireReset) */
|
|
74
|
-
hasFired: boolean;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Creates a public view from an internal registration,
|
|
78
|
-
* stripping the callback function.
|
|
79
|
-
*/
|
|
80
|
-
declare function toHotkeyRegistrationView(reg: HotkeyRegistration): HotkeyRegistrationView;
|
|
81
|
-
/**
|
|
82
|
-
* A handle returned from HotkeyManager.register() that allows updating
|
|
83
|
-
* the callback and options without re-registering the hotkey.
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```ts
|
|
87
|
-
* const handle = manager.register('Mod+S', callback, options)
|
|
88
|
-
*
|
|
89
|
-
* // Update callback without re-registering (avoids stale closures)
|
|
90
|
-
* handle.callback = newCallback
|
|
91
|
-
*
|
|
92
|
-
* // Update options without re-registering
|
|
93
|
-
* handle.setOptions({ enabled: false })
|
|
94
|
-
*
|
|
95
|
-
* // Check if still active
|
|
96
|
-
* if (handle.isActive) {
|
|
97
|
-
* // ...
|
|
98
|
-
* }
|
|
99
|
-
*
|
|
100
|
-
* // Unregister when done
|
|
101
|
-
* handle.unregister()
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
|
-
interface HotkeyRegistrationHandle {
|
|
105
|
-
/**
|
|
106
|
-
* The callback function. Can be set directly to update without re-registering.
|
|
107
|
-
* This avoids stale closures when the callback references React state.
|
|
108
|
-
*/
|
|
109
|
-
callback: HotkeyCallback;
|
|
110
|
-
/** Unique identifier for this registration */
|
|
111
|
-
readonly id: string;
|
|
112
|
-
/** Check if this registration is still active (not unregistered) */
|
|
113
|
-
readonly isActive: boolean;
|
|
114
|
-
/**
|
|
115
|
-
* Update options (merged with existing options).
|
|
116
|
-
* Useful for updating `enabled`, `preventDefault`, etc. without re-registering.
|
|
117
|
-
*/
|
|
118
|
-
setOptions: (options: Partial<HotkeyOptions>) => void;
|
|
119
|
-
/** Unregister this hotkey */
|
|
120
|
-
unregister: () => void;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Singleton manager for hotkey registrations.
|
|
124
|
-
*
|
|
125
|
-
* This class provides a centralized way to register and manage keyboard hotkeys.
|
|
126
|
-
* It uses a single event listener for efficiency, regardless of how many hotkeys
|
|
127
|
-
* are registered.
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* const manager = HotkeyManager.getInstance()
|
|
132
|
-
*
|
|
133
|
-
* const unregister = manager.register('Mod+S', (event, context) => {
|
|
134
|
-
* console.log('Save triggered!')
|
|
135
|
-
* })
|
|
136
|
-
*
|
|
137
|
-
* // Later, to unregister:
|
|
138
|
-
* unregister()
|
|
139
|
-
* ```
|
|
140
|
-
*/
|
|
141
|
-
declare class HotkeyManager {
|
|
142
|
-
#private;
|
|
143
|
-
/**
|
|
144
|
-
* The TanStack Store containing all hotkey registrations.
|
|
145
|
-
* Use this to subscribe to registration changes or access current registrations.
|
|
146
|
-
*
|
|
147
|
-
* @example
|
|
148
|
-
* ```ts
|
|
149
|
-
* const manager = HotkeyManager.getInstance()
|
|
150
|
-
*
|
|
151
|
-
* // Subscribe to registration changes
|
|
152
|
-
* const unsubscribe = manager.registrations.subscribe(() => {
|
|
153
|
-
* console.log('Registrations changed:', manager.registrations.state.size)
|
|
154
|
-
* })
|
|
155
|
-
*
|
|
156
|
-
* // Access current registrations
|
|
157
|
-
* for (const [id, reg] of manager.registrations.state) {
|
|
158
|
-
* console.log(reg.hotkey, reg.options.enabled)
|
|
159
|
-
* }
|
|
160
|
-
* ```
|
|
161
|
-
*/
|
|
162
|
-
readonly registrations: Store<Map<string, HotkeyRegistration>>;
|
|
163
|
-
private constructor();
|
|
164
|
-
/**
|
|
165
|
-
* Gets the singleton instance of HotkeyManager.
|
|
166
|
-
*/
|
|
167
|
-
static getInstance(): HotkeyManager;
|
|
168
|
-
/**
|
|
169
|
-
* Resets the singleton instance. Useful for testing.
|
|
170
|
-
*/
|
|
171
|
-
static resetInstance(): void;
|
|
172
|
-
/**
|
|
173
|
-
* Registers a hotkey handler and returns a handle for updating the registration.
|
|
174
|
-
*
|
|
175
|
-
* The returned handle allows updating the callback and options without
|
|
176
|
-
* re-registering, which is useful for avoiding stale closures in React.
|
|
177
|
-
*
|
|
178
|
-
* @param hotkey - The hotkey string (e.g., 'Mod+S') or RawHotkey object
|
|
179
|
-
* @param callback - The function to call when the hotkey is pressed
|
|
180
|
-
* @param options - Options for the hotkey behavior
|
|
181
|
-
* @returns A handle for managing the registration
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* ```ts
|
|
185
|
-
* const handle = manager.register('Mod+S', callback)
|
|
186
|
-
*
|
|
187
|
-
* // Update callback without re-registering (avoids stale closures)
|
|
188
|
-
* handle.callback = newCallback
|
|
189
|
-
*
|
|
190
|
-
* // Update options
|
|
191
|
-
* handle.setOptions({ enabled: false })
|
|
192
|
-
*
|
|
193
|
-
* // Unregister when done
|
|
194
|
-
* handle.unregister()
|
|
195
|
-
* ```
|
|
196
|
-
*/
|
|
197
|
-
register(hotkey: RegisterableHotkey, callback: HotkeyCallback, options?: HotkeyOptions): HotkeyRegistrationHandle;
|
|
198
|
-
/**
|
|
199
|
-
* Triggers a registration's callback programmatically from devtools.
|
|
200
|
-
* Creates a synthetic KeyboardEvent and invokes the callback.
|
|
201
|
-
*
|
|
202
|
-
* @param id - The registration ID to trigger
|
|
203
|
-
* @returns True if the registration was found and triggered
|
|
204
|
-
*/
|
|
205
|
-
triggerRegistration(id: string): boolean;
|
|
206
|
-
/**
|
|
207
|
-
* Gets the number of registered hotkeys.
|
|
208
|
-
*/
|
|
209
|
-
getRegistrationCount(): number;
|
|
210
|
-
/**
|
|
211
|
-
* Checks if a specific hotkey is registered.
|
|
212
|
-
*
|
|
213
|
-
* @param hotkey - The hotkey string to check
|
|
214
|
-
* @param target - Optional target element to match (if provided, both hotkey and target must match)
|
|
215
|
-
* @returns True if a matching registration exists
|
|
216
|
-
*/
|
|
217
|
-
isRegistered(hotkey: Hotkey, target?: HTMLElement | Document | Window): boolean;
|
|
218
|
-
/**
|
|
219
|
-
* Destroys the manager and removes all listeners.
|
|
220
|
-
*/
|
|
221
|
-
destroy(): void;
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Gets the singleton HotkeyManager instance.
|
|
225
|
-
* Convenience function for accessing the manager.
|
|
226
|
-
*/
|
|
227
|
-
declare function getHotkeyManager(): HotkeyManager;
|
|
228
|
-
//#endregion
|
|
229
|
-
export { HotkeyManager, HotkeyOptions, HotkeyRegistration, HotkeyRegistrationHandle, HotkeyRegistrationView, getHotkeyManager, toHotkeyRegistrationView };
|
|
230
|
-
//# sourceMappingURL=hotkey-manager.d.cts.map
|