@tanstack/hotkeys 0.0.2 → 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/README.md +14 -82
- package/dist/hotkey-manager.cjs +22 -3
- package/dist/hotkey-manager.cjs.map +1 -1
- package/dist/hotkey-manager.d.cts +1 -1
- package/dist/hotkey-manager.d.ts +1 -1
- package/dist/hotkey-manager.js +22 -3
- package/dist/hotkey-manager.js.map +1 -1
- package/package.json +1 -1
- package/src/hotkey-manager.ts +33 -9
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<img src="./media/
|
|
2
|
+
<img src="./media/header_hotkeys.png" alt="TanStack Hotkeys" />
|
|
3
3
|
</div>
|
|
4
4
|
|
|
5
5
|
<br />
|
|
@@ -36,54 +36,15 @@
|
|
|
36
36
|
|
|
37
37
|
# TanStack Hotkeys
|
|
38
38
|
|
|
39
|
-
Type-safe keyboard shortcuts for the web. Template strings, parsed objects, cross-platform `Mod`, a singleton Hotkey Manager, and utilities for cheatsheet UIs. Built to stay SSR-friendly.
|
|
40
|
-
|
|
41
39
|
> [!NOTE]
|
|
42
40
|
> TanStack Hotkeys is pre-alpha (prototyping phase). We are actively developing the library and are open to feedback and contributions.
|
|
43
41
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
- **Options**
|
|
51
|
-
- `keydown` / `keyup` via `eventType`
|
|
52
|
-
- `preventDefault`, `stopPropagation`
|
|
53
|
-
- Conditional `enabled` to turn hotkeys on or off
|
|
54
|
-
- `requireReset`: trigger once until all keys are released
|
|
55
|
-
- **Cross-Platform Mod**
|
|
56
|
-
- `Mod` maps to **Cmd (Meta)** on macOS and **Ctrl** on Windows/Linux
|
|
57
|
-
- **Singleton Hotkey Manager**
|
|
58
|
-
- `getHotkeyManager()`, `HotkeyManager.getInstance()` to register global keyboard shortcuts
|
|
59
|
-
- Single shared listener for efficiency
|
|
60
|
-
- **Display Utilities**
|
|
61
|
-
- `formatForDisplay(hotkey)` for cheatsheet UIs (symbols on Mac, labels on Windows/Linux)
|
|
62
|
-
- `formatWithLabels`, `formatHotkey` for flexible output
|
|
63
|
-
- **Validation & Matching**
|
|
64
|
-
- `validateHotkey`, `assertValidHotkey`, `checkHotkey` for correctness validation
|
|
65
|
-
- `matchesKeyboardEvent`, `createHotkeyHandler`, `createMultiHotkeyHandler`
|
|
66
|
-
- **Sequences**
|
|
67
|
-
- `SequenceManager`, `createSequenceMatcher` for Vim-style multi-key shortcuts (e.g. `['G','G']`, `['D','I','W']`)
|
|
68
|
-
- **Key State**
|
|
69
|
-
- `KeyStateTracker`, `getKeyStateTracker` for held-key tracking
|
|
70
|
-
- **Hotkey Recorder**
|
|
71
|
-
- `HotkeyRecorder` class for capturing keyboard shortcuts interactively
|
|
72
|
-
- Supports live preview, cancellation, and clearing during recording
|
|
73
|
-
- **React Hooks**
|
|
74
|
-
- `useHotkey` – register a keyboard shortcut (global, via singleton manager)
|
|
75
|
-
- `useHotkeySequence` – detect keys pressed in order within a timeout
|
|
76
|
-
- `useHeldKeys` – reactive list of currently held keys
|
|
77
|
-
- `useKeyHold` – reactive boolean for whether a given key is held
|
|
78
|
-
- `useHotkeyRecorder` – record keyboard shortcuts interactively with live preview
|
|
79
|
-
- **Devtools**
|
|
80
|
-
- Devtools are a core focus: visibility into all registered hotkeys, scopes, and options
|
|
81
|
-
- `@tanstack/hotkeys-devtools` and `@tanstack/react-hotkeys-devtools` (in active development)
|
|
82
|
-
- **Planned**
|
|
83
|
-
- Scoping hotkeys to a DOM element or React ref
|
|
84
|
-
- Warn/error on conflicting shortcuts (TBD)
|
|
85
|
-
- Ignore hotkeys when certain inputs are focused (e.g. `input`, `textarea`)
|
|
86
|
-
- Focus traps and tab-order utilities
|
|
42
|
+
Type-safe keyboard shortcuts for the web. Template-string bindings, parsed objects, a cross-platform `Mod` key, a singleton Hotkey Manager, and utilities for cheatsheet UIs—built to stay SSR-friendly.
|
|
43
|
+
|
|
44
|
+
- Type-safe bindings — template strings (`Mod+Shift+S`, `Escape`) or parsed objects for full control
|
|
45
|
+
- Flexible options — `keydown`/`keyup`, `preventDefault`, `stopPropagation`, conditional enabled, `requireReset`
|
|
46
|
+
- Cross-platform Mod — maps to Cmd on macOS and Ctrl on Windows/Linux
|
|
47
|
+
- Batteries included — validation + matching, sequences (Vim-style), key-state tracking, recorder UI helpers, React hooks, and devtools (in progress)
|
|
87
48
|
|
|
88
49
|
### <a href="https://tanstack.com/hotkeys">Read the docs →</a>
|
|
89
50
|
|
|
@@ -98,37 +59,6 @@ Type-safe keyboard shortcuts for the web. Template strings, parsed objects, cros
|
|
|
98
59
|
> - Svelte Hotkeys – needs a contributor!
|
|
99
60
|
> - Vue Hotkeys – needs a contributor!
|
|
100
61
|
|
|
101
|
-
## Quick Example
|
|
102
|
-
|
|
103
|
-
```tsx
|
|
104
|
-
import { useHotkey, formatForDisplay } from '@tanstack/react-hotkeys'
|
|
105
|
-
|
|
106
|
-
function Editor() {
|
|
107
|
-
useHotkey(
|
|
108
|
-
'Mod+S',
|
|
109
|
-
(e, { hotkey }) => {
|
|
110
|
-
save()
|
|
111
|
-
},
|
|
112
|
-
{ requireReset: true },
|
|
113
|
-
)
|
|
114
|
-
|
|
115
|
-
return (
|
|
116
|
-
<div>
|
|
117
|
-
<button>Save</button>
|
|
118
|
-
<span>{formatForDisplay('Mod+S')}</span>{' '}
|
|
119
|
-
{/* e.g. "⌘S" on Mac, "Ctrl+S" on Windows */}
|
|
120
|
-
</div>
|
|
121
|
-
)
|
|
122
|
-
}
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
## Packages
|
|
126
|
-
|
|
127
|
-
- **`@tanstack/hotkeys`** – Core: parse, format, match, validate, manager, sequence, key-state
|
|
128
|
-
- **`@tanstack/react-hotkeys`** – React: `useHotkey`, `useHotkeySequence`, `useHeldKeys`, `useKeyHold`, `useHotkeyRecorder`
|
|
129
|
-
- **`@tanstack/hotkeys-devtools`** – Base devtools (in development)
|
|
130
|
-
- **`@tanstack/react-hotkeys-devtools`** – React devtools (in development)
|
|
131
|
-
|
|
132
62
|
## Get Involved
|
|
133
63
|
|
|
134
64
|
- We welcome issues and pull requests!
|
|
@@ -138,13 +68,15 @@ function Editor() {
|
|
|
138
68
|
|
|
139
69
|
## Partners
|
|
140
70
|
|
|
71
|
+
<div align="center">
|
|
72
|
+
|
|
141
73
|
<table align="center">
|
|
142
74
|
<tr>
|
|
143
75
|
<td>
|
|
144
76
|
<a href="https://www.coderabbit.ai/?via=tanstack&dub_id=aCcEEdAOqqutX6OS" >
|
|
145
77
|
<picture>
|
|
146
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://tanstack.com/assets/coderabbit-dark-
|
|
147
|
-
<source media="(prefers-color-scheme: light)" srcset="https://tanstack.com/assets/coderabbit-light-
|
|
78
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://tanstack.com/assets/coderabbit-dark-D643Zkrv.svg" height="40" />
|
|
79
|
+
<source media="(prefers-color-scheme: light)" srcset="https://tanstack.com/assets/coderabbit-light-CIzGLYU_.svg" height="40" />
|
|
148
80
|
<img src="https://tanstack.com/assets/coderabbit-light-DVMJ2jHi.svg" height="40" alt="CodeRabbit" />
|
|
149
81
|
</picture>
|
|
150
82
|
</a>
|
|
@@ -152,8 +84,8 @@ function Editor() {
|
|
|
152
84
|
<td>
|
|
153
85
|
<a href="https://www.cloudflare.com?utm_source=tanstack">
|
|
154
86
|
<picture>
|
|
155
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://tanstack.com/assets/cloudflare-white-
|
|
156
|
-
<source media="(prefers-color-scheme: light)" srcset="https://tanstack.com/assets/cloudflare-black-
|
|
87
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://tanstack.com/assets/cloudflare-white-Co-Tyjbl.svg" height="60" />
|
|
88
|
+
<source media="(prefers-color-scheme: light)" srcset="https://tanstack.com/assets/cloudflare-black-6Ojsn8yh.svg" height="60" />
|
|
157
89
|
<img src="https://tanstack.com/assets/cloudflare-black-CPufaW0B.svg" height="60" alt="Cloudflare" />
|
|
158
90
|
</picture>
|
|
159
91
|
</a>
|
|
@@ -162,7 +94,7 @@ function Editor() {
|
|
|
162
94
|
</table>
|
|
163
95
|
|
|
164
96
|
<div align="center">
|
|
165
|
-
<img src="
|
|
97
|
+
<img src="media/partner_logo.svg" alt="Keys & you?" height="65">
|
|
166
98
|
<p>
|
|
167
99
|
We're looking for TanStack Hotkeys Partners to join our mission! Partner with us to push the boundaries of TanStack Hotkeys and build amazing things together.
|
|
168
100
|
</p>
|
package/dist/hotkey-manager.cjs
CHANGED
|
@@ -25,6 +25,15 @@ function generateId() {
|
|
|
25
25
|
return `hotkey_${++registrationIdCounter}`;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
+
* Computes the default ignoreInputs value based on the hotkey.
|
|
29
|
+
* Ctrl/Meta shortcuts and Escape fire in inputs; single keys and Shift/Alt combos are ignored.
|
|
30
|
+
*/
|
|
31
|
+
function getDefaultIgnoreInputs(parsedHotkey) {
|
|
32
|
+
if (parsedHotkey.ctrl || parsedHotkey.meta) return false;
|
|
33
|
+
if (parsedHotkey.key === "Escape") return false;
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
28
37
|
* Singleton manager for hotkey registrations.
|
|
29
38
|
*
|
|
30
39
|
* This class provides a centralized way to register and manage keyboard hotkeys.
|
|
@@ -102,6 +111,7 @@ var HotkeyManager = class HotkeyManager {
|
|
|
102
111
|
const conflictBehavior = options.conflictBehavior ?? "warn";
|
|
103
112
|
const conflictingRegistration = this.#findConflictingRegistration(hotkeyStr, target);
|
|
104
113
|
if (conflictingRegistration) this.#handleConflict(conflictingRegistration, hotkeyStr, conflictBehavior);
|
|
114
|
+
const resolvedIgnoreInputs = options.ignoreInputs ?? getDefaultIgnoreInputs(parsedHotkey);
|
|
105
115
|
const registration = {
|
|
106
116
|
id,
|
|
107
117
|
hotkey: hotkeyStr,
|
|
@@ -110,7 +120,8 @@ var HotkeyManager = class HotkeyManager {
|
|
|
110
120
|
options: {
|
|
111
121
|
...defaultHotkeyOptions,
|
|
112
122
|
...options,
|
|
113
|
-
platform
|
|
123
|
+
platform,
|
|
124
|
+
ignoreInputs: resolvedIgnoreInputs
|
|
114
125
|
},
|
|
115
126
|
hasFired: false,
|
|
116
127
|
triggerCount: 0,
|
|
@@ -302,14 +313,22 @@ var HotkeyManager = class HotkeyManager {
|
|
|
302
313
|
* Checks if an element is an input-like element that should be ignored.
|
|
303
314
|
*
|
|
304
315
|
* This includes:
|
|
305
|
-
* - HTMLInputElement (all input types)
|
|
316
|
+
* - HTMLInputElement (all input types except button, submit, reset)
|
|
306
317
|
* - HTMLTextAreaElement
|
|
307
318
|
* - HTMLSelectElement
|
|
308
319
|
* - Elements with contentEditable enabled
|
|
320
|
+
*
|
|
321
|
+
* Button-type inputs (button, submit, reset) are excluded so hotkeys like
|
|
322
|
+
* Mod+S and Escape fire when the user has tabbed to a form button.
|
|
309
323
|
*/
|
|
310
324
|
#isInputElement(element) {
|
|
311
325
|
if (!element) return false;
|
|
312
|
-
if (element instanceof HTMLInputElement
|
|
326
|
+
if (element instanceof HTMLInputElement) {
|
|
327
|
+
const type = element.type.toLowerCase();
|
|
328
|
+
if (type === "button" || type === "submit" || type === "reset") return false;
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
if (element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) return true;
|
|
313
332
|
if (element instanceof HTMLElement) {
|
|
314
333
|
const contentEditable = element.contentEditable;
|
|
315
334
|
if (contentEditable === "true" || contentEditable === "") return true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hotkey-manager.cjs","names":["#instance","Store","#platform","detectPlatform","parseHotkey","rawHotkeyToParsedHotkey","formatHotkey","#findConflictingRegistration","#handleConflict","#targetRegistrations","#ensureListenersForTarget","#unregister","#removeListenersForTarget","#targetListeners","#createTargetKeyDownHandler","#createTargetKeyUpHandler","#isEventForTarget","#isInputElement","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 type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n ParsedHotkey,\n RegisterableHotkey,\n} from './hotkey'\n\n/**\n * Behavior when registering a hotkey that conflicts with an existing registration.\n *\n * - `'warn'` - Log a warning to the console but allow both registrations (default)\n * - `'error'` - Throw an error and prevent the new registration\n * - `'replace'` - Unregister the existing hotkey and register the new one\n * - `'allow'` - Allow multiple registrations of the same hotkey without warning\n */\nexport type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow'\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 /** Whether the hotkey is enabled. Defaults to true */\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 (input, textarea, select, contenteditable). Defaults to true */\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}\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 * 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\n/**\n * Default options for hotkey registration.\n */\nconst defaultHotkeyOptions: Omit<\n Required<HotkeyOptions>,\n 'platform' | 'target'\n> = {\n preventDefault: true,\n stopPropagation: true,\n eventType: 'keydown',\n requireReset: false,\n enabled: true,\n ignoreInputs: true,\n conflictBehavior: 'warn',\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 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 or null\n const target =\n options.target ??\n (typeof document !== 'undefined' ? document : ({} as 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 this.#handleConflict(conflictingRegistration, hotkeyStr, conflictBehavior)\n }\n\n const registration: HotkeyRegistration = {\n id,\n hotkey: hotkeyStr,\n parsedHotkey,\n callback,\n options: {\n ...defaultHotkeyOptions,\n ...options,\n platform,\n },\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 (!this.#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 (this.#isInputElement(event.target)) {\n // Don't ignore if the hotkey is explicitly scoped to this input element\n if (event.target !== registration.target) {\n continue\n }\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 * Checks if an event is for the given target (originated from or bubbled to it).\n */\n #isEventForTarget(\n event: KeyboardEvent,\n target: HTMLElement | Document | Window,\n ): boolean {\n // For Document and Window, check if currentTarget matches\n if (target === document || target === window) {\n return event.currentTarget === target\n }\n\n // For HTMLElement, check if event originated from or bubbled to the element\n if (target instanceof HTMLElement) {\n // Check if the event's currentTarget is the target (capturing/bubbling)\n if (event.currentTarget === target) {\n return true\n }\n\n // Check if the event's target is a descendant of our target\n if (event.target instanceof Node && target.contains(event.target)) {\n return true\n }\n }\n\n return false\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 * Handles conflicts between hotkey registrations based on conflict behavior.\n */\n #handleConflict(\n conflictingRegistration: HotkeyRegistration,\n hotkey: Hotkey,\n conflictBehavior: ConflictBehavior,\n ): void {\n if (conflictBehavior === 'allow') {\n return\n }\n\n if (conflictBehavior === 'warn') {\n console.warn(\n `Hotkey '${hotkey}' is already registered. Multiple handlers will be triggered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to suppress this warning.`,\n )\n return\n }\n\n if (conflictBehavior === 'error') {\n throw new Error(\n `Hotkey '${hotkey}' is already registered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to allow multiple registrations.`,\n )\n }\n\n // At this point, conflictBehavior must be 'replace'\n this.#unregister(conflictingRegistration.id)\n }\n\n /**\n * Checks if an element is an input-like element that should be ignored.\n *\n * This includes:\n * - HTMLInputElement (all input types)\n * - HTMLTextAreaElement\n * - HTMLSelectElement\n * - Elements with contentEditable enabled\n */\n #isInputElement(element: EventTarget | null): boolean {\n if (!element) {\n return false\n }\n\n // Check for standard input elements\n if (\n element instanceof HTMLInputElement ||\n element instanceof HTMLTextAreaElement ||\n element instanceof HTMLSelectElement\n ) {\n return true\n }\n\n // Check for contenteditable elements\n if (element instanceof HTMLElement) {\n const contentEditable = element.contentEditable\n if (contentEditable === 'true' || contentEditable === '') {\n return true\n }\n }\n\n return false\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":";;;;;;;;;;AAkHA,MAAM,uBAGF;CACF,gBAAgB;CAChB,iBAAiB;CACjB,WAAW;CACX,cAAc;CACd,SAAS;CACT,cAAc;CACd,kBAAkB;CACnB;AAED,IAAI,wBAAwB;;;;AAK5B,SAAS,aAAqB;AAC5B,QAAO,UAAU,EAAE;;;;;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,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,QAAKJ,oBAAqB,OAAO;;;;;;;AAQrC,SAAgB,mBAAkC;AAChD,QAAO,cAAc,aAAa"}
|
|
1
|
+
{"version":3,"file":"hotkey-manager.cjs","names":["#instance","Store","#platform","detectPlatform","parseHotkey","rawHotkeyToParsedHotkey","formatHotkey","#findConflictingRegistration","#handleConflict","#targetRegistrations","#ensureListenersForTarget","#unregister","#removeListenersForTarget","#targetListeners","#createTargetKeyDownHandler","#createTargetKeyUpHandler","#isEventForTarget","#isInputElement","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 type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n ParsedHotkey,\n RegisterableHotkey,\n} from './hotkey'\n\n/**\n * Behavior when registering a hotkey that conflicts with an existing registration.\n *\n * - `'warn'` - Log a warning to the console but allow both registrations (default)\n * - `'error'` - Throw an error and prevent the new registration\n * - `'replace'` - Unregister the existing hotkey and register the new one\n * - `'allow'` - Allow multiple registrations of the same hotkey without warning\n */\nexport type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow'\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 /** Whether the hotkey is enabled. Defaults to true */\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}\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 * 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\n/**\n * Default options for hotkey registration.\n */\nconst defaultHotkeyOptions: Omit<\n Required<HotkeyOptions>,\n 'platform' | 'target'\n> = {\n preventDefault: true,\n stopPropagation: true,\n eventType: 'keydown',\n requireReset: false,\n enabled: true,\n ignoreInputs: true,\n conflictBehavior: 'warn',\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 * Computes the default ignoreInputs value based on the hotkey.\n * Ctrl/Meta shortcuts and Escape fire in inputs; single keys and Shift/Alt combos are ignored.\n */\nfunction getDefaultIgnoreInputs(parsedHotkey: ParsedHotkey): boolean {\n if (parsedHotkey.ctrl || parsedHotkey.meta) return false // Mod+S, Ctrl+C, etc.\n if (parsedHotkey.key === 'Escape') return false // Close modal, etc.\n return true // Single keys, Shift+key, Alt+key\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 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 or null\n const target =\n options.target ??\n (typeof document !== 'undefined' ? document : ({} as 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 this.#handleConflict(conflictingRegistration, hotkeyStr, conflictBehavior)\n }\n\n const resolvedIgnoreInputs =\n options.ignoreInputs ?? getDefaultIgnoreInputs(parsedHotkey)\n\n const baseOptions = {\n ...defaultHotkeyOptions,\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 (!this.#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 (this.#isInputElement(event.target)) {\n // Don't ignore if the hotkey is explicitly scoped to this input element\n if (event.target !== registration.target) {\n continue\n }\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 * Checks if an event is for the given target (originated from or bubbled to it).\n */\n #isEventForTarget(\n event: KeyboardEvent,\n target: HTMLElement | Document | Window,\n ): boolean {\n // For Document and Window, check if currentTarget matches\n if (target === document || target === window) {\n return event.currentTarget === target\n }\n\n // For HTMLElement, check if event originated from or bubbled to the element\n if (target instanceof HTMLElement) {\n // Check if the event's currentTarget is the target (capturing/bubbling)\n if (event.currentTarget === target) {\n return true\n }\n\n // Check if the event's target is a descendant of our target\n if (event.target instanceof Node && target.contains(event.target)) {\n return true\n }\n }\n\n return false\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 * Handles conflicts between hotkey registrations based on conflict behavior.\n */\n #handleConflict(\n conflictingRegistration: HotkeyRegistration,\n hotkey: Hotkey,\n conflictBehavior: ConflictBehavior,\n ): void {\n if (conflictBehavior === 'allow') {\n return\n }\n\n if (conflictBehavior === 'warn') {\n console.warn(\n `Hotkey '${hotkey}' is already registered. Multiple handlers will be triggered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to suppress this warning.`,\n )\n return\n }\n\n if (conflictBehavior === 'error') {\n throw new Error(\n `Hotkey '${hotkey}' is already registered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to allow multiple registrations.`,\n )\n }\n\n // At this point, conflictBehavior must be 'replace'\n this.#unregister(conflictingRegistration.id)\n }\n\n /**\n * Checks if an element is an input-like element that should be ignored.\n *\n * This includes:\n * - HTMLInputElement (all input types except button, submit, reset)\n * - HTMLTextAreaElement\n * - HTMLSelectElement\n * - Elements with contentEditable enabled\n *\n * Button-type inputs (button, submit, reset) are excluded so hotkeys like\n * Mod+S and Escape fire when the user has tabbed to a form button.\n */\n #isInputElement(element: EventTarget | null): boolean {\n if (!element) {\n return false\n }\n\n if (element instanceof HTMLInputElement) {\n const type = element.type.toLowerCase()\n if (type === 'button' || type === 'submit' || type === 'reset') {\n return false\n }\n return true\n }\n\n if (\n element instanceof HTMLTextAreaElement ||\n element instanceof HTMLSelectElement\n ) {\n return true\n }\n\n // Check for contenteditable elements\n if (element instanceof HTMLElement) {\n const contentEditable = element.contentEditable\n if (contentEditable === 'true' || contentEditable === '') {\n return true\n }\n }\n\n return false\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":";;;;;;;;;;AAkHA,MAAM,uBAGF;CACF,gBAAgB;CAChB,iBAAiB;CACjB,WAAW;CACX,cAAc;CACd,SAAS;CACT,cAAc;CACd,kBAAkB;CACnB;AAED,IAAI,wBAAwB;;;;AAK5B,SAAS,aAAqB;AAC5B,QAAO,UAAU,EAAE;;;;;;AAOrB,SAAS,uBAAuB,cAAqC;AACnE,KAAI,aAAa,QAAQ,aAAa,KAAM,QAAO;AACnD,KAAI,aAAa,QAAQ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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,QAAKJ,oBAAqB,OAAO;;;;;;;AAQrC,SAAgB,mBAAkC;AAChD,QAAO,cAAc,aAAa"}
|
|
@@ -21,7 +21,7 @@ interface HotkeyOptions {
|
|
|
21
21
|
enabled?: boolean;
|
|
22
22
|
/** The event type to listen for. Defaults to 'keydown' */
|
|
23
23
|
eventType?: 'keydown' | 'keyup';
|
|
24
|
-
/** Whether to ignore hotkeys when keyboard events originate from input-like elements (
|
|
24
|
+
/** 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 */
|
|
25
25
|
ignoreInputs?: boolean;
|
|
26
26
|
/** The target platform for resolving 'Mod' */
|
|
27
27
|
platform?: 'mac' | 'windows' | 'linux';
|
package/dist/hotkey-manager.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ interface HotkeyOptions {
|
|
|
21
21
|
enabled?: boolean;
|
|
22
22
|
/** The event type to listen for. Defaults to 'keydown' */
|
|
23
23
|
eventType?: 'keydown' | 'keyup';
|
|
24
|
-
/** Whether to ignore hotkeys when keyboard events originate from input-like elements (
|
|
24
|
+
/** 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 */
|
|
25
25
|
ignoreInputs?: boolean;
|
|
26
26
|
/** The target platform for resolving 'Mod' */
|
|
27
27
|
platform?: 'mac' | 'windows' | 'linux';
|
package/dist/hotkey-manager.js
CHANGED
|
@@ -25,6 +25,15 @@ function generateId() {
|
|
|
25
25
|
return `hotkey_${++registrationIdCounter}`;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
+
* Computes the default ignoreInputs value based on the hotkey.
|
|
29
|
+
* Ctrl/Meta shortcuts and Escape fire in inputs; single keys and Shift/Alt combos are ignored.
|
|
30
|
+
*/
|
|
31
|
+
function getDefaultIgnoreInputs(parsedHotkey) {
|
|
32
|
+
if (parsedHotkey.ctrl || parsedHotkey.meta) return false;
|
|
33
|
+
if (parsedHotkey.key === "Escape") return false;
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
28
37
|
* Singleton manager for hotkey registrations.
|
|
29
38
|
*
|
|
30
39
|
* This class provides a centralized way to register and manage keyboard hotkeys.
|
|
@@ -102,6 +111,7 @@ var HotkeyManager = class HotkeyManager {
|
|
|
102
111
|
const conflictBehavior = options.conflictBehavior ?? "warn";
|
|
103
112
|
const conflictingRegistration = this.#findConflictingRegistration(hotkeyStr, target);
|
|
104
113
|
if (conflictingRegistration) this.#handleConflict(conflictingRegistration, hotkeyStr, conflictBehavior);
|
|
114
|
+
const resolvedIgnoreInputs = options.ignoreInputs ?? getDefaultIgnoreInputs(parsedHotkey);
|
|
105
115
|
const registration = {
|
|
106
116
|
id,
|
|
107
117
|
hotkey: hotkeyStr,
|
|
@@ -110,7 +120,8 @@ var HotkeyManager = class HotkeyManager {
|
|
|
110
120
|
options: {
|
|
111
121
|
...defaultHotkeyOptions,
|
|
112
122
|
...options,
|
|
113
|
-
platform
|
|
123
|
+
platform,
|
|
124
|
+
ignoreInputs: resolvedIgnoreInputs
|
|
114
125
|
},
|
|
115
126
|
hasFired: false,
|
|
116
127
|
triggerCount: 0,
|
|
@@ -302,14 +313,22 @@ var HotkeyManager = class HotkeyManager {
|
|
|
302
313
|
* Checks if an element is an input-like element that should be ignored.
|
|
303
314
|
*
|
|
304
315
|
* This includes:
|
|
305
|
-
* - HTMLInputElement (all input types)
|
|
316
|
+
* - HTMLInputElement (all input types except button, submit, reset)
|
|
306
317
|
* - HTMLTextAreaElement
|
|
307
318
|
* - HTMLSelectElement
|
|
308
319
|
* - Elements with contentEditable enabled
|
|
320
|
+
*
|
|
321
|
+
* Button-type inputs (button, submit, reset) are excluded so hotkeys like
|
|
322
|
+
* Mod+S and Escape fire when the user has tabbed to a form button.
|
|
309
323
|
*/
|
|
310
324
|
#isInputElement(element) {
|
|
311
325
|
if (!element) return false;
|
|
312
|
-
if (element instanceof HTMLInputElement
|
|
326
|
+
if (element instanceof HTMLInputElement) {
|
|
327
|
+
const type = element.type.toLowerCase();
|
|
328
|
+
if (type === "button" || type === "submit" || type === "reset") return false;
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
if (element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) return true;
|
|
313
332
|
if (element instanceof HTMLElement) {
|
|
314
333
|
const contentEditable = element.contentEditable;
|
|
315
334
|
if (contentEditable === "true" || contentEditable === "") return true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hotkey-manager.js","names":["#instance","#platform","#findConflictingRegistration","#handleConflict","#targetRegistrations","#ensureListenersForTarget","#unregister","#removeListenersForTarget","#targetListeners","#createTargetKeyDownHandler","#createTargetKeyUpHandler","#isEventForTarget","#isInputElement","#executeHotkeyCallback","#shouldResetRegistration","#processTargetEvent"],"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 type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n ParsedHotkey,\n RegisterableHotkey,\n} from './hotkey'\n\n/**\n * Behavior when registering a hotkey that conflicts with an existing registration.\n *\n * - `'warn'` - Log a warning to the console but allow both registrations (default)\n * - `'error'` - Throw an error and prevent the new registration\n * - `'replace'` - Unregister the existing hotkey and register the new one\n * - `'allow'` - Allow multiple registrations of the same hotkey without warning\n */\nexport type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow'\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 /** Whether the hotkey is enabled. Defaults to true */\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 (input, textarea, select, contenteditable). Defaults to true */\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}\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 * 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\n/**\n * Default options for hotkey registration.\n */\nconst defaultHotkeyOptions: Omit<\n Required<HotkeyOptions>,\n 'platform' | 'target'\n> = {\n preventDefault: true,\n stopPropagation: true,\n eventType: 'keydown',\n requireReset: false,\n enabled: true,\n ignoreInputs: true,\n conflictBehavior: 'warn',\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 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 or null\n const target =\n options.target ??\n (typeof document !== 'undefined' ? document : ({} as 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 this.#handleConflict(conflictingRegistration, hotkeyStr, conflictBehavior)\n }\n\n const registration: HotkeyRegistration = {\n id,\n hotkey: hotkeyStr,\n parsedHotkey,\n callback,\n options: {\n ...defaultHotkeyOptions,\n ...options,\n platform,\n },\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 (!this.#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 (this.#isInputElement(event.target)) {\n // Don't ignore if the hotkey is explicitly scoped to this input element\n if (event.target !== registration.target) {\n continue\n }\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 * Checks if an event is for the given target (originated from or bubbled to it).\n */\n #isEventForTarget(\n event: KeyboardEvent,\n target: HTMLElement | Document | Window,\n ): boolean {\n // For Document and Window, check if currentTarget matches\n if (target === document || target === window) {\n return event.currentTarget === target\n }\n\n // For HTMLElement, check if event originated from or bubbled to the element\n if (target instanceof HTMLElement) {\n // Check if the event's currentTarget is the target (capturing/bubbling)\n if (event.currentTarget === target) {\n return true\n }\n\n // Check if the event's target is a descendant of our target\n if (event.target instanceof Node && target.contains(event.target)) {\n return true\n }\n }\n\n return false\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 * Handles conflicts between hotkey registrations based on conflict behavior.\n */\n #handleConflict(\n conflictingRegistration: HotkeyRegistration,\n hotkey: Hotkey,\n conflictBehavior: ConflictBehavior,\n ): void {\n if (conflictBehavior === 'allow') {\n return\n }\n\n if (conflictBehavior === 'warn') {\n console.warn(\n `Hotkey '${hotkey}' is already registered. Multiple handlers will be triggered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to suppress this warning.`,\n )\n return\n }\n\n if (conflictBehavior === 'error') {\n throw new Error(\n `Hotkey '${hotkey}' is already registered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to allow multiple registrations.`,\n )\n }\n\n // At this point, conflictBehavior must be 'replace'\n this.#unregister(conflictingRegistration.id)\n }\n\n /**\n * Checks if an element is an input-like element that should be ignored.\n *\n * This includes:\n * - HTMLInputElement (all input types)\n * - HTMLTextAreaElement\n * - HTMLSelectElement\n * - Elements with contentEditable enabled\n */\n #isInputElement(element: EventTarget | null): boolean {\n if (!element) {\n return false\n }\n\n // Check for standard input elements\n if (\n element instanceof HTMLInputElement ||\n element instanceof HTMLTextAreaElement ||\n element instanceof HTMLSelectElement\n ) {\n return true\n }\n\n // Check for contenteditable elements\n if (element instanceof HTMLElement) {\n const contentEditable = element.contentEditable\n if (contentEditable === 'true' || contentEditable === '') {\n return true\n }\n }\n\n return false\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":";;;;;;;;;;AAkHA,MAAM,uBAGF;CACF,gBAAgB;CAChB,iBAAiB;CACjB,WAAW;CACX,cAAc;CACd,SAAS;CACT,cAAc;CACd,kBAAkB;CACnB;AAED,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,IAAI,sBACnE,IAAI,KAAK,CACV;AAaC,QAAKC,WA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0B,OAAO;AAGxC,OAAK,cAAc,+BAAe,IAAI,KAAK,CAAC;AAC5C,QAAKC,gBAAiB,OAAO;AAC7B,QAAKJ,oBAAqB,OAAO;;;;;;;AAQrC,SAAgB,mBAAkC;AAChD,QAAO,cAAc,aAAa"}
|
|
1
|
+
{"version":3,"file":"hotkey-manager.js","names":["#instance","#platform","#findConflictingRegistration","#handleConflict","#targetRegistrations","#ensureListenersForTarget","#unregister","#removeListenersForTarget","#targetListeners","#createTargetKeyDownHandler","#createTargetKeyUpHandler","#isEventForTarget","#isInputElement","#executeHotkeyCallback","#shouldResetRegistration","#processTargetEvent"],"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 type {\n Hotkey,\n HotkeyCallback,\n HotkeyCallbackContext,\n ParsedHotkey,\n RegisterableHotkey,\n} from './hotkey'\n\n/**\n * Behavior when registering a hotkey that conflicts with an existing registration.\n *\n * - `'warn'` - Log a warning to the console but allow both registrations (default)\n * - `'error'` - Throw an error and prevent the new registration\n * - `'replace'` - Unregister the existing hotkey and register the new one\n * - `'allow'` - Allow multiple registrations of the same hotkey without warning\n */\nexport type ConflictBehavior = 'warn' | 'error' | 'replace' | 'allow'\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 /** Whether the hotkey is enabled. Defaults to true */\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}\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 * 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\n/**\n * Default options for hotkey registration.\n */\nconst defaultHotkeyOptions: Omit<\n Required<HotkeyOptions>,\n 'platform' | 'target'\n> = {\n preventDefault: true,\n stopPropagation: true,\n eventType: 'keydown',\n requireReset: false,\n enabled: true,\n ignoreInputs: true,\n conflictBehavior: 'warn',\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 * Computes the default ignoreInputs value based on the hotkey.\n * Ctrl/Meta shortcuts and Escape fire in inputs; single keys and Shift/Alt combos are ignored.\n */\nfunction getDefaultIgnoreInputs(parsedHotkey: ParsedHotkey): boolean {\n if (parsedHotkey.ctrl || parsedHotkey.meta) return false // Mod+S, Ctrl+C, etc.\n if (parsedHotkey.key === 'Escape') return false // Close modal, etc.\n return true // Single keys, Shift+key, Alt+key\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 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 or null\n const target =\n options.target ??\n (typeof document !== 'undefined' ? document : ({} as 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 this.#handleConflict(conflictingRegistration, hotkeyStr, conflictBehavior)\n }\n\n const resolvedIgnoreInputs =\n options.ignoreInputs ?? getDefaultIgnoreInputs(parsedHotkey)\n\n const baseOptions = {\n ...defaultHotkeyOptions,\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 (!this.#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 (this.#isInputElement(event.target)) {\n // Don't ignore if the hotkey is explicitly scoped to this input element\n if (event.target !== registration.target) {\n continue\n }\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 * Checks if an event is for the given target (originated from or bubbled to it).\n */\n #isEventForTarget(\n event: KeyboardEvent,\n target: HTMLElement | Document | Window,\n ): boolean {\n // For Document and Window, check if currentTarget matches\n if (target === document || target === window) {\n return event.currentTarget === target\n }\n\n // For HTMLElement, check if event originated from or bubbled to the element\n if (target instanceof HTMLElement) {\n // Check if the event's currentTarget is the target (capturing/bubbling)\n if (event.currentTarget === target) {\n return true\n }\n\n // Check if the event's target is a descendant of our target\n if (event.target instanceof Node && target.contains(event.target)) {\n return true\n }\n }\n\n return false\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 * Handles conflicts between hotkey registrations based on conflict behavior.\n */\n #handleConflict(\n conflictingRegistration: HotkeyRegistration,\n hotkey: Hotkey,\n conflictBehavior: ConflictBehavior,\n ): void {\n if (conflictBehavior === 'allow') {\n return\n }\n\n if (conflictBehavior === 'warn') {\n console.warn(\n `Hotkey '${hotkey}' is already registered. Multiple handlers will be triggered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to suppress this warning.`,\n )\n return\n }\n\n if (conflictBehavior === 'error') {\n throw new Error(\n `Hotkey '${hotkey}' is already registered. ` +\n `Use conflictBehavior: 'replace' to replace the existing handler, ` +\n `or conflictBehavior: 'allow' to allow multiple registrations.`,\n )\n }\n\n // At this point, conflictBehavior must be 'replace'\n this.#unregister(conflictingRegistration.id)\n }\n\n /**\n * Checks if an element is an input-like element that should be ignored.\n *\n * This includes:\n * - HTMLInputElement (all input types except button, submit, reset)\n * - HTMLTextAreaElement\n * - HTMLSelectElement\n * - Elements with contentEditable enabled\n *\n * Button-type inputs (button, submit, reset) are excluded so hotkeys like\n * Mod+S and Escape fire when the user has tabbed to a form button.\n */\n #isInputElement(element: EventTarget | null): boolean {\n if (!element) {\n return false\n }\n\n if (element instanceof HTMLInputElement) {\n const type = element.type.toLowerCase()\n if (type === 'button' || type === 'submit' || type === 'reset') {\n return false\n }\n return true\n }\n\n if (\n element instanceof HTMLTextAreaElement ||\n element instanceof HTMLSelectElement\n ) {\n return true\n }\n\n // Check for contenteditable elements\n if (element instanceof HTMLElement) {\n const contentEditable = element.contentEditable\n if (contentEditable === 'true' || contentEditable === '') {\n return true\n }\n }\n\n return false\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":";;;;;;;;;;AAkHA,MAAM,uBAGF;CACF,gBAAgB;CAChB,iBAAiB;CACjB,WAAW;CACX,cAAc;CACd,SAAS;CACT,cAAc;CACd,kBAAkB;CACnB;AAED,IAAI,wBAAwB;;;;AAK5B,SAAS,aAAqB;AAC5B,QAAO,UAAU,EAAE;;;;;;AAOrB,SAAS,uBAAuB,cAAqC;AACnE,KAAI,aAAa,QAAQ,aAAa,KAAM,QAAO;AACnD,KAAI,aAAa,QAAQ,SAAU,QAAO;AAC1C,QAAO;;;;;;;;;;;;;;;;;;;;;AAsBT,IAAa,gBAAb,MAAa,cAAc;CACzB,QAAOA,WAAkC;CAwBzC;CACA,mCAMI,IAAI,KAAK;CACb,uCACE,IAAI,KAAK;CAEX,AAAQ,cAAc;uBAd2C,IAAI,sBACnE,IAAI,KAAK,CACV;AAaC,QAAKC,WAAY,gBAAgB;;;;;CAMnC,OAAO,cAA6B;AAClC,MAAI,CAAC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OAAI,WAAW,UAAa,aAAa,WAAW,OAClD,QAAO;;AAIb,SAAO;;;;;CAMT,UAAgB;AAEd,OAAK,MAAM,UAAU,MAAKE,gBAAiB,MAAM,CAC/C,OAAKD,yBAA0B,OAAO;AAGxC,OAAK,cAAc,+BAAe,IAAI,KAAK,CAAC;AAC5C,QAAKC,gBAAiB,OAAO;AAC7B,QAAKJ,oBAAqB,OAAO;;;;;;;AAQrC,SAAgB,mBAAkC;AAChD,QAAO,cAAc,aAAa"}
|
package/package.json
CHANGED
package/src/hotkey-manager.ts
CHANGED
|
@@ -31,7 +31,7 @@ export interface HotkeyOptions {
|
|
|
31
31
|
enabled?: boolean
|
|
32
32
|
/** The event type to listen for. Defaults to 'keydown' */
|
|
33
33
|
eventType?: 'keydown' | 'keyup'
|
|
34
|
-
/** Whether to ignore hotkeys when keyboard events originate from input-like elements (
|
|
34
|
+
/** 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 */
|
|
35
35
|
ignoreInputs?: boolean
|
|
36
36
|
/** The target platform for resolving 'Mod' */
|
|
37
37
|
platform?: 'mac' | 'windows' | 'linux'
|
|
@@ -134,6 +134,16 @@ function generateId(): string {
|
|
|
134
134
|
return `hotkey_${++registrationIdCounter}`
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
+
/**
|
|
138
|
+
* Computes the default ignoreInputs value based on the hotkey.
|
|
139
|
+
* Ctrl/Meta shortcuts and Escape fire in inputs; single keys and Shift/Alt combos are ignored.
|
|
140
|
+
*/
|
|
141
|
+
function getDefaultIgnoreInputs(parsedHotkey: ParsedHotkey): boolean {
|
|
142
|
+
if (parsedHotkey.ctrl || parsedHotkey.meta) return false // Mod+S, Ctrl+C, etc.
|
|
143
|
+
if (parsedHotkey.key === 'Escape') return false // Close modal, etc.
|
|
144
|
+
return true // Single keys, Shift+key, Alt+key
|
|
145
|
+
}
|
|
146
|
+
|
|
137
147
|
/**
|
|
138
148
|
* Singleton manager for hotkey registrations.
|
|
139
149
|
*
|
|
@@ -271,16 +281,21 @@ export class HotkeyManager {
|
|
|
271
281
|
this.#handleConflict(conflictingRegistration, hotkeyStr, conflictBehavior)
|
|
272
282
|
}
|
|
273
283
|
|
|
284
|
+
const resolvedIgnoreInputs =
|
|
285
|
+
options.ignoreInputs ?? getDefaultIgnoreInputs(parsedHotkey)
|
|
286
|
+
|
|
287
|
+
const baseOptions = {
|
|
288
|
+
...defaultHotkeyOptions,
|
|
289
|
+
...options,
|
|
290
|
+
platform,
|
|
291
|
+
}
|
|
292
|
+
|
|
274
293
|
const registration: HotkeyRegistration = {
|
|
275
294
|
id,
|
|
276
295
|
hotkey: hotkeyStr,
|
|
277
296
|
parsedHotkey,
|
|
278
297
|
callback,
|
|
279
|
-
options: {
|
|
280
|
-
...defaultHotkeyOptions,
|
|
281
|
-
...options,
|
|
282
|
-
platform,
|
|
283
|
-
},
|
|
298
|
+
options: { ...baseOptions, ignoreInputs: resolvedIgnoreInputs },
|
|
284
299
|
hasFired: false,
|
|
285
300
|
triggerCount: 0,
|
|
286
301
|
target,
|
|
@@ -636,19 +651,28 @@ export class HotkeyManager {
|
|
|
636
651
|
* Checks if an element is an input-like element that should be ignored.
|
|
637
652
|
*
|
|
638
653
|
* This includes:
|
|
639
|
-
* - HTMLInputElement (all input types)
|
|
654
|
+
* - HTMLInputElement (all input types except button, submit, reset)
|
|
640
655
|
* - HTMLTextAreaElement
|
|
641
656
|
* - HTMLSelectElement
|
|
642
657
|
* - Elements with contentEditable enabled
|
|
658
|
+
*
|
|
659
|
+
* Button-type inputs (button, submit, reset) are excluded so hotkeys like
|
|
660
|
+
* Mod+S and Escape fire when the user has tabbed to a form button.
|
|
643
661
|
*/
|
|
644
662
|
#isInputElement(element: EventTarget | null): boolean {
|
|
645
663
|
if (!element) {
|
|
646
664
|
return false
|
|
647
665
|
}
|
|
648
666
|
|
|
649
|
-
|
|
667
|
+
if (element instanceof HTMLInputElement) {
|
|
668
|
+
const type = element.type.toLowerCase()
|
|
669
|
+
if (type === 'button' || type === 'submit' || type === 'reset') {
|
|
670
|
+
return false
|
|
671
|
+
}
|
|
672
|
+
return true
|
|
673
|
+
}
|
|
674
|
+
|
|
650
675
|
if (
|
|
651
|
-
element instanceof HTMLInputElement ||
|
|
652
676
|
element instanceof HTMLTextAreaElement ||
|
|
653
677
|
element instanceof HTMLSelectElement
|
|
654
678
|
) {
|