@pyreon/hotkeys 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/lib/analysis/index.js.html +5406 -0
- package/lib/index.js +221 -0
- package/lib/index.js.map +1 -0
- package/lib/types/index.d.ts +198 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index2.d.ts +130 -0
- package/lib/types/index2.d.ts.map +1 -0
- package/package.json +52 -0
- package/src/index.ts +49 -0
- package/src/parse.ts +93 -0
- package/src/registry.ts +151 -0
- package/src/tests/hotkeys.test.ts +461 -0
- package/src/types.ts +54 -0
- package/src/use-hotkey-scope.ts +20 -0
- package/src/use-hotkey.ts +26 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { onUnmount } from "@pyreon/core";
|
|
2
|
+
import { signal } from "@pyreon/reactivity";
|
|
3
|
+
|
|
4
|
+
//#region src/parse.ts
|
|
5
|
+
const KEY_ALIASES = {
|
|
6
|
+
esc: "escape",
|
|
7
|
+
return: "enter",
|
|
8
|
+
del: "delete",
|
|
9
|
+
ins: "insert",
|
|
10
|
+
space: " ",
|
|
11
|
+
spacebar: " ",
|
|
12
|
+
up: "arrowup",
|
|
13
|
+
down: "arrowdown",
|
|
14
|
+
left: "arrowleft",
|
|
15
|
+
right: "arrowright",
|
|
16
|
+
plus: "+"
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Parse a shortcut string like 'ctrl+shift+s' into a KeyCombo.
|
|
20
|
+
* Supports aliases (esc, del, space, etc.) and mod (ctrl on Windows/Linux, meta on Mac).
|
|
21
|
+
*/
|
|
22
|
+
function parseShortcut(shortcut) {
|
|
23
|
+
const parts = shortcut.toLowerCase().trim().split("+");
|
|
24
|
+
const combo = {
|
|
25
|
+
ctrl: false,
|
|
26
|
+
shift: false,
|
|
27
|
+
alt: false,
|
|
28
|
+
meta: false,
|
|
29
|
+
key: ""
|
|
30
|
+
};
|
|
31
|
+
for (const part of parts) {
|
|
32
|
+
const p = part.trim();
|
|
33
|
+
if (p === "ctrl" || p === "control") combo.ctrl = true;
|
|
34
|
+
else if (p === "shift") combo.shift = true;
|
|
35
|
+
else if (p === "alt") combo.alt = true;
|
|
36
|
+
else if (p === "meta" || p === "cmd" || p === "command") combo.meta = true;
|
|
37
|
+
else if (p === "mod") if (isMac()) combo.meta = true;
|
|
38
|
+
else combo.ctrl = true;
|
|
39
|
+
else combo.key = KEY_ALIASES[p] ?? p;
|
|
40
|
+
}
|
|
41
|
+
return combo;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Check if a KeyboardEvent matches a KeyCombo.
|
|
45
|
+
*/
|
|
46
|
+
function matchesCombo(event, combo) {
|
|
47
|
+
if (event.ctrlKey !== combo.ctrl) return false;
|
|
48
|
+
if (event.shiftKey !== combo.shift) return false;
|
|
49
|
+
if (event.altKey !== combo.alt) return false;
|
|
50
|
+
if (event.metaKey !== combo.meta) return false;
|
|
51
|
+
return event.key.toLowerCase() === combo.key;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Format a KeyCombo back to a human-readable string.
|
|
55
|
+
*/
|
|
56
|
+
function formatCombo(combo) {
|
|
57
|
+
const parts = [];
|
|
58
|
+
if (combo.ctrl) parts.push("Ctrl");
|
|
59
|
+
if (combo.shift) parts.push("Shift");
|
|
60
|
+
if (combo.alt) parts.push("Alt");
|
|
61
|
+
if (combo.meta) parts.push(isMac() ? "⌘" : "Meta");
|
|
62
|
+
parts.push(combo.key.length === 1 ? combo.key.toUpperCase() : capitalize(combo.key));
|
|
63
|
+
return parts.join("+");
|
|
64
|
+
}
|
|
65
|
+
function capitalize(s) {
|
|
66
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
67
|
+
}
|
|
68
|
+
function isMac() {
|
|
69
|
+
if (typeof navigator === "undefined") return false;
|
|
70
|
+
return /mac|iphone|ipad|ipod/i.test(navigator.userAgent);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/registry.ts
|
|
75
|
+
const entries = [];
|
|
76
|
+
const activeScopes = signal(new Set(["global"]));
|
|
77
|
+
let listenerAttached = false;
|
|
78
|
+
const INPUT_TAGS = new Set([
|
|
79
|
+
"INPUT",
|
|
80
|
+
"TEXTAREA",
|
|
81
|
+
"SELECT"
|
|
82
|
+
]);
|
|
83
|
+
function isInputFocused(event) {
|
|
84
|
+
const target = event.target;
|
|
85
|
+
if (!target) return false;
|
|
86
|
+
if (INPUT_TAGS.has(target.tagName)) return true;
|
|
87
|
+
if (target.isContentEditable) return true;
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
function attachListener() {
|
|
91
|
+
if (listenerAttached) return;
|
|
92
|
+
if (typeof window === "undefined") return;
|
|
93
|
+
listenerAttached = true;
|
|
94
|
+
window.addEventListener("keydown", (event) => {
|
|
95
|
+
const scopes = activeScopes.peek();
|
|
96
|
+
for (const entry of entries) {
|
|
97
|
+
if (!scopes.has(entry.options.scope)) continue;
|
|
98
|
+
if (!(typeof entry.options.enabled === "function" ? entry.options.enabled() : entry.options.enabled)) continue;
|
|
99
|
+
if (!entry.options.enableOnInputs && isInputFocused(event)) continue;
|
|
100
|
+
if (!matchesCombo(event, entry.combo)) continue;
|
|
101
|
+
if (entry.options.preventDefault) event.preventDefault();
|
|
102
|
+
if (entry.options.stopPropagation) event.stopPropagation();
|
|
103
|
+
entry.handler(event);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Register a keyboard shortcut. Returns an unregister function.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* const unregister = registerHotkey('ctrl+s', (e) => save(), { description: 'Save' })
|
|
113
|
+
* // later: unregister()
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
function registerHotkey(shortcut, handler, options) {
|
|
117
|
+
attachListener();
|
|
118
|
+
const entry = {
|
|
119
|
+
shortcut,
|
|
120
|
+
combo: parseShortcut(shortcut),
|
|
121
|
+
handler,
|
|
122
|
+
options: {
|
|
123
|
+
scope: options?.scope ?? "global",
|
|
124
|
+
preventDefault: options?.preventDefault !== false,
|
|
125
|
+
stopPropagation: options?.stopPropagation === true,
|
|
126
|
+
enableOnInputs: options?.enableOnInputs === true,
|
|
127
|
+
enabled: options?.enabled ?? true,
|
|
128
|
+
description: options?.description
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
entries.push(entry);
|
|
132
|
+
return () => {
|
|
133
|
+
const idx = entries.indexOf(entry);
|
|
134
|
+
if (idx !== -1) entries.splice(idx, 1);
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Activate a hotkey scope. 'global' is always active.
|
|
139
|
+
*/
|
|
140
|
+
function enableScope(scope) {
|
|
141
|
+
const current = activeScopes.peek();
|
|
142
|
+
if (current.has(scope)) return;
|
|
143
|
+
const next = new Set(current);
|
|
144
|
+
next.add(scope);
|
|
145
|
+
activeScopes.set(next);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Deactivate a hotkey scope. Cannot deactivate 'global'.
|
|
149
|
+
*/
|
|
150
|
+
function disableScope(scope) {
|
|
151
|
+
if (scope === "global") return;
|
|
152
|
+
const current = activeScopes.peek();
|
|
153
|
+
if (!current.has(scope)) return;
|
|
154
|
+
const next = new Set(current);
|
|
155
|
+
next.delete(scope);
|
|
156
|
+
activeScopes.set(next);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Get the currently active scopes as a reactive signal.
|
|
160
|
+
*/
|
|
161
|
+
function getActiveScopes() {
|
|
162
|
+
return activeScopes;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get all registered hotkeys (for building help dialogs).
|
|
166
|
+
*/
|
|
167
|
+
function getRegisteredHotkeys() {
|
|
168
|
+
return entries.map((e) => ({
|
|
169
|
+
shortcut: e.shortcut,
|
|
170
|
+
scope: e.options.scope,
|
|
171
|
+
description: e.options.description
|
|
172
|
+
}));
|
|
173
|
+
}
|
|
174
|
+
function _resetHotkeys() {
|
|
175
|
+
entries.length = 0;
|
|
176
|
+
activeScopes.set(new Set(["global"]));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region src/use-hotkey.ts
|
|
181
|
+
/**
|
|
182
|
+
* Register a keyboard shortcut scoped to a component's lifecycle.
|
|
183
|
+
* Automatically unregisters when the component unmounts.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* function Editor() {
|
|
188
|
+
* useHotkey('ctrl+s', () => save(), { description: 'Save document' })
|
|
189
|
+
* useHotkey('ctrl+z', () => undo())
|
|
190
|
+
* useHotkey('ctrl+shift+z', () => redo())
|
|
191
|
+
* // ...
|
|
192
|
+
* }
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
function useHotkey(shortcut, handler, options) {
|
|
196
|
+
onUnmount(registerHotkey(shortcut, handler, options));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/use-hotkey-scope.ts
|
|
201
|
+
/**
|
|
202
|
+
* Activate a hotkey scope for the lifetime of a component.
|
|
203
|
+
* When the component unmounts, the scope is deactivated.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```tsx
|
|
207
|
+
* function Modal() {
|
|
208
|
+
* useHotkeyScope('modal')
|
|
209
|
+
* useHotkey('escape', () => closeModal(), { scope: 'modal' })
|
|
210
|
+
* // ...
|
|
211
|
+
* }
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
function useHotkeyScope(scope) {
|
|
215
|
+
enableScope(scope);
|
|
216
|
+
onUnmount(() => disableScope(scope));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
//#endregion
|
|
220
|
+
export { _resetHotkeys, disableScope, enableScope, formatCombo, getActiveScopes, getRegisteredHotkeys, matchesCombo, parseShortcut, registerHotkey, useHotkey, useHotkeyScope };
|
|
221
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/parse.ts","../src/registry.ts","../src/use-hotkey.ts","../src/use-hotkey-scope.ts"],"sourcesContent":["import type { KeyCombo } from './types'\n\n// ─── Key aliases ─────────────────────────────────────────────────────────────\n\nconst KEY_ALIASES: Record<string, string> = {\n esc: 'escape',\n return: 'enter',\n del: 'delete',\n ins: 'insert',\n space: ' ',\n spacebar: ' ',\n up: 'arrowup',\n down: 'arrowdown',\n left: 'arrowleft',\n right: 'arrowright',\n plus: '+',\n}\n\n/**\n * Parse a shortcut string like 'ctrl+shift+s' into a KeyCombo.\n * Supports aliases (esc, del, space, etc.) and mod (ctrl on Windows/Linux, meta on Mac).\n */\nexport function parseShortcut(shortcut: string): KeyCombo {\n const parts = shortcut.toLowerCase().trim().split('+')\n const combo: KeyCombo = {\n ctrl: false,\n shift: false,\n alt: false,\n meta: false,\n key: '',\n }\n\n for (const part of parts) {\n const p = part.trim()\n if (p === 'ctrl' || p === 'control') {\n combo.ctrl = true\n } else if (p === 'shift') {\n combo.shift = true\n } else if (p === 'alt') {\n combo.alt = true\n } else if (p === 'meta' || p === 'cmd' || p === 'command') {\n combo.meta = true\n } else if (p === 'mod') {\n // mod = meta on Mac, ctrl elsewhere\n if (isMac()) {\n combo.meta = true\n } else {\n combo.ctrl = true\n }\n } else {\n combo.key = KEY_ALIASES[p] ?? p\n }\n }\n\n return combo\n}\n\n/**\n * Check if a KeyboardEvent matches a KeyCombo.\n */\nexport function matchesCombo(event: KeyboardEvent, combo: KeyCombo): boolean {\n if (event.ctrlKey !== combo.ctrl) return false\n if (event.shiftKey !== combo.shift) return false\n if (event.altKey !== combo.alt) return false\n if (event.metaKey !== combo.meta) return false\n\n const eventKey = event.key.toLowerCase()\n return eventKey === combo.key\n}\n\n/**\n * Format a KeyCombo back to a human-readable string.\n */\nexport function formatCombo(combo: KeyCombo): string {\n const parts: string[] = []\n if (combo.ctrl) parts.push('Ctrl')\n if (combo.shift) parts.push('Shift')\n if (combo.alt) parts.push('Alt')\n if (combo.meta) parts.push(isMac() ? '⌘' : 'Meta')\n parts.push(\n combo.key.length === 1 ? combo.key.toUpperCase() : capitalize(combo.key),\n )\n return parts.join('+')\n}\n\nfunction capitalize(s: string): string {\n return s.charAt(0).toUpperCase() + s.slice(1)\n}\n\nfunction isMac(): boolean {\n if (typeof navigator === 'undefined') return false\n return /mac|iphone|ipad|ipod/i.test(navigator.userAgent)\n}\n","import type { Signal } from '@pyreon/reactivity'\nimport { signal } from '@pyreon/reactivity'\nimport { matchesCombo, parseShortcut } from './parse'\nimport type { HotkeyEntry, HotkeyOptions } from './types'\n\n// ─── State ───────────────────────────────────────────────────────────────────\n\nconst entries: HotkeyEntry[] = []\nconst activeScopes = signal<Set<string>>(new Set(['global']))\nlet listenerAttached = false\n\n// ─── Input detection ─────────────────────────────────────────────────────────\n\nconst INPUT_TAGS = new Set(['INPUT', 'TEXTAREA', 'SELECT'])\n\nfunction isInputFocused(event: KeyboardEvent): boolean {\n const target = event.target as HTMLElement | null\n if (!target) return false\n if (INPUT_TAGS.has(target.tagName)) return true\n if (target.isContentEditable) return true\n return false\n}\n\n// ─── Global listener ─────────────────────────────────────────────────────────\n\nfunction attachListener(): void {\n if (listenerAttached) return\n if (typeof window === 'undefined') return\n listenerAttached = true\n\n window.addEventListener('keydown', (event) => {\n const scopes = activeScopes.peek()\n\n for (const entry of entries) {\n // Check scope\n if (!scopes.has(entry.options.scope)) continue\n\n // Check enabled\n const enabled =\n typeof entry.options.enabled === 'function'\n ? entry.options.enabled()\n : entry.options.enabled\n if (!enabled) continue\n\n // Check input focus\n if (!entry.options.enableOnInputs && isInputFocused(event)) continue\n\n // Check key match\n if (!matchesCombo(event, entry.combo)) continue\n\n // Match found\n if (entry.options.preventDefault) event.preventDefault()\n if (entry.options.stopPropagation) event.stopPropagation()\n entry.handler(event)\n }\n })\n}\n\n// ─── Registration ────────────────────────────────────────────────────────────\n\n/**\n * Register a keyboard shortcut. Returns an unregister function.\n *\n * @example\n * ```ts\n * const unregister = registerHotkey('ctrl+s', (e) => save(), { description: 'Save' })\n * // later: unregister()\n * ```\n */\nexport function registerHotkey(\n shortcut: string,\n handler: (event: KeyboardEvent) => void,\n options?: HotkeyOptions,\n): () => void {\n attachListener()\n\n const entry: HotkeyEntry = {\n shortcut,\n combo: parseShortcut(shortcut),\n handler,\n options: {\n scope: options?.scope ?? 'global',\n preventDefault: options?.preventDefault !== false,\n stopPropagation: options?.stopPropagation === true,\n enableOnInputs: options?.enableOnInputs === true,\n enabled: options?.enabled ?? true,\n description: options?.description,\n },\n }\n\n entries.push(entry)\n\n return () => {\n const idx = entries.indexOf(entry)\n if (idx !== -1) entries.splice(idx, 1)\n }\n}\n\n// ─── Scope management ────────────────────────────────────────────────────────\n\n/**\n * Activate a hotkey scope. 'global' is always active.\n */\nexport function enableScope(scope: string): void {\n const current = activeScopes.peek()\n if (current.has(scope)) return\n const next = new Set(current)\n next.add(scope)\n activeScopes.set(next)\n}\n\n/**\n * Deactivate a hotkey scope. Cannot deactivate 'global'.\n */\nexport function disableScope(scope: string): void {\n if (scope === 'global') return\n const current = activeScopes.peek()\n if (!current.has(scope)) return\n const next = new Set(current)\n next.delete(scope)\n activeScopes.set(next)\n}\n\n/**\n * Get the currently active scopes as a reactive signal.\n */\nexport function getActiveScopes(): Signal<Set<string>> {\n return activeScopes\n}\n\n/**\n * Get all registered hotkeys (for building help dialogs).\n */\nexport function getRegisteredHotkeys(): ReadonlyArray<{\n shortcut: string\n scope: string\n description?: string\n}> {\n return entries.map((e) => ({\n shortcut: e.shortcut,\n scope: e.options.scope,\n description: e.options.description,\n }))\n}\n\n// ─── Reset (for testing) ────────────────────────────────────────────────────\n\nexport function _resetHotkeys(): void {\n entries.length = 0\n activeScopes.set(new Set(['global']))\n}\n","import { onUnmount } from '@pyreon/core'\nimport { registerHotkey } from './registry'\nimport type { HotkeyOptions } from './types'\n\n/**\n * Register a keyboard shortcut scoped to a component's lifecycle.\n * Automatically unregisters when the component unmounts.\n *\n * @example\n * ```ts\n * function Editor() {\n * useHotkey('ctrl+s', () => save(), { description: 'Save document' })\n * useHotkey('ctrl+z', () => undo())\n * useHotkey('ctrl+shift+z', () => redo())\n * // ...\n * }\n * ```\n */\nexport function useHotkey(\n shortcut: string,\n handler: (event: KeyboardEvent) => void,\n options?: HotkeyOptions,\n): void {\n const unregister = registerHotkey(shortcut, handler, options)\n onUnmount(unregister)\n}\n","import { onUnmount } from '@pyreon/core'\nimport { disableScope, enableScope } from './registry'\n\n/**\n * Activate a hotkey scope for the lifetime of a component.\n * When the component unmounts, the scope is deactivated.\n *\n * @example\n * ```tsx\n * function Modal() {\n * useHotkeyScope('modal')\n * useHotkey('escape', () => closeModal(), { scope: 'modal' })\n * // ...\n * }\n * ```\n */\nexport function useHotkeyScope(scope: string): void {\n enableScope(scope)\n onUnmount(() => disableScope(scope))\n}\n"],"mappings":";;;;AAIA,MAAM,cAAsC;CAC1C,KAAK;CACL,QAAQ;CACR,KAAK;CACL,KAAK;CACL,OAAO;CACP,UAAU;CACV,IAAI;CACJ,MAAM;CACN,MAAM;CACN,OAAO;CACP,MAAM;CACP;;;;;AAMD,SAAgB,cAAc,UAA4B;CACxD,MAAM,QAAQ,SAAS,aAAa,CAAC,MAAM,CAAC,MAAM,IAAI;CACtD,MAAM,QAAkB;EACtB,MAAM;EACN,OAAO;EACP,KAAK;EACL,MAAM;EACN,KAAK;EACN;AAED,MAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,IAAI,KAAK,MAAM;AACrB,MAAI,MAAM,UAAU,MAAM,UACxB,OAAM,OAAO;WACJ,MAAM,QACf,OAAM,QAAQ;WACL,MAAM,MACf,OAAM,MAAM;WACH,MAAM,UAAU,MAAM,SAAS,MA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IAAI,KAAK;;;;;AAMxB,SAAgB,aAAa,OAAqB;AAChD,KAAI,UAAU,SAAU;CACxB,MAAM,UAAU,aAAa,MAAM;AACnC,KAAI,CAAC,QAAQ,IAAI,MAAM,CAAE;CACzB,MAAM,OAAO,IAAI,IAAI,QAAQ;AAC7B,MAAK,OAAO,MAAM;AAClB,cAAa,IAAI,KAAK;;;;;AAMxB,SAAgB,kBAAuC;AACrD,QAAO;;;;;AAMT,SAAgB,uBAIb;AACD,QAAO,QAAQ,KAAK,OAAO;EACzB,UAAU,EAAE;EACZ,OAAO,EAAE,QAAQ;EACjB,aAAa,EAAE,QAAQ;EACxB,EAAE;;AAKL,SAAgB,gBAAsB;AACpC,SAAQ,SAAS;AACjB,cAAa,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC;;;;;;;;;;;;;;;;;;;ACnIvC,SAAgB,UACd,UACA,SACA,SACM;AAEN,WADmB,eAAe,UAAU,SAAS,QAAQ,CACxC;;;;;;;;;;;;;;;;;;ACRvB,SAAgB,eAAe,OAAqB;AAClD,aAAY,MAAM;AAClB,iBAAgB,aAAa,MAAM,CAAC"}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { onUnmount } from "@pyreon/core";
|
|
2
|
+
import { signal } from "@pyreon/reactivity";
|
|
3
|
+
|
|
4
|
+
//#region src/parse.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Parse a shortcut string like 'ctrl+shift+s' into a KeyCombo.
|
|
8
|
+
* Supports aliases (esc, del, space, etc.) and mod (ctrl on Windows/Linux, meta on Mac).
|
|
9
|
+
*/
|
|
10
|
+
function parseShortcut(shortcut) {
|
|
11
|
+
const parts = shortcut.toLowerCase().trim().split("+");
|
|
12
|
+
const combo = {
|
|
13
|
+
ctrl: false,
|
|
14
|
+
shift: false,
|
|
15
|
+
alt: false,
|
|
16
|
+
meta: false,
|
|
17
|
+
key: ""
|
|
18
|
+
};
|
|
19
|
+
for (const part of parts) {
|
|
20
|
+
const p = part.trim();
|
|
21
|
+
if (p === "ctrl" || p === "control") combo.ctrl = true;else if (p === "shift") combo.shift = true;else if (p === "alt") combo.alt = true;else if (p === "meta" || p === "cmd" || p === "command") combo.meta = true;else if (p === "mod") {
|
|
22
|
+
if (isMac()) combo.meta = true;else combo.ctrl = true;
|
|
23
|
+
} else combo.key = KEY_ALIASES[p] ?? p;
|
|
24
|
+
}
|
|
25
|
+
return combo;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Check if a KeyboardEvent matches a KeyCombo.
|
|
29
|
+
*/
|
|
30
|
+
function matchesCombo(event, combo) {
|
|
31
|
+
if (event.ctrlKey !== combo.ctrl) return false;
|
|
32
|
+
if (event.shiftKey !== combo.shift) return false;
|
|
33
|
+
if (event.altKey !== combo.alt) return false;
|
|
34
|
+
if (event.metaKey !== combo.meta) return false;
|
|
35
|
+
return event.key.toLowerCase() === combo.key;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Format a KeyCombo back to a human-readable string.
|
|
39
|
+
*/
|
|
40
|
+
function formatCombo(combo) {
|
|
41
|
+
const parts = [];
|
|
42
|
+
if (combo.ctrl) parts.push("Ctrl");
|
|
43
|
+
if (combo.shift) parts.push("Shift");
|
|
44
|
+
if (combo.alt) parts.push("Alt");
|
|
45
|
+
if (combo.meta) parts.push(isMac() ? "⌘" : "Meta");
|
|
46
|
+
parts.push(combo.key.length === 1 ? combo.key.toUpperCase() : capitalize(combo.key));
|
|
47
|
+
return parts.join("+");
|
|
48
|
+
}
|
|
49
|
+
function capitalize(s) {
|
|
50
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
51
|
+
}
|
|
52
|
+
function isMac() {
|
|
53
|
+
if (typeof navigator === "undefined") return false;
|
|
54
|
+
return /mac|iphone|ipad|ipod/i.test(navigator.userAgent);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/registry.ts
|
|
59
|
+
|
|
60
|
+
function isInputFocused(event) {
|
|
61
|
+
const target = event.target;
|
|
62
|
+
if (!target) return false;
|
|
63
|
+
if (INPUT_TAGS.has(target.tagName)) return true;
|
|
64
|
+
if (target.isContentEditable) return true;
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
function attachListener() {
|
|
68
|
+
if (listenerAttached) return;
|
|
69
|
+
if (typeof window === "undefined") return;
|
|
70
|
+
listenerAttached = true;
|
|
71
|
+
window.addEventListener("keydown", event => {
|
|
72
|
+
const scopes = activeScopes.peek();
|
|
73
|
+
for (const entry of entries) {
|
|
74
|
+
if (!scopes.has(entry.options.scope)) continue;
|
|
75
|
+
if (!(typeof entry.options.enabled === "function" ? entry.options.enabled() : entry.options.enabled)) continue;
|
|
76
|
+
if (!entry.options.enableOnInputs && isInputFocused(event)) continue;
|
|
77
|
+
if (!matchesCombo(event, entry.combo)) continue;
|
|
78
|
+
if (entry.options.preventDefault) event.preventDefault();
|
|
79
|
+
if (entry.options.stopPropagation) event.stopPropagation();
|
|
80
|
+
entry.handler(event);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Register a keyboard shortcut. Returns an unregister function.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const unregister = registerHotkey('ctrl+s', (e) => save(), { description: 'Save' })
|
|
90
|
+
* // later: unregister()
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
function registerHotkey(shortcut, handler, options) {
|
|
94
|
+
attachListener();
|
|
95
|
+
const entry = {
|
|
96
|
+
shortcut,
|
|
97
|
+
combo: parseShortcut(shortcut),
|
|
98
|
+
handler,
|
|
99
|
+
options: {
|
|
100
|
+
scope: options?.scope ?? "global",
|
|
101
|
+
preventDefault: options?.preventDefault !== false,
|
|
102
|
+
stopPropagation: options?.stopPropagation === true,
|
|
103
|
+
enableOnInputs: options?.enableOnInputs === true,
|
|
104
|
+
enabled: options?.enabled ?? true,
|
|
105
|
+
description: options?.description
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
entries.push(entry);
|
|
109
|
+
return () => {
|
|
110
|
+
const idx = entries.indexOf(entry);
|
|
111
|
+
if (idx !== -1) entries.splice(idx, 1);
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Activate a hotkey scope. 'global' is always active.
|
|
116
|
+
*/
|
|
117
|
+
function enableScope(scope) {
|
|
118
|
+
const current = activeScopes.peek();
|
|
119
|
+
if (current.has(scope)) return;
|
|
120
|
+
const next = new Set(current);
|
|
121
|
+
next.add(scope);
|
|
122
|
+
activeScopes.set(next);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Deactivate a hotkey scope. Cannot deactivate 'global'.
|
|
126
|
+
*/
|
|
127
|
+
function disableScope(scope) {
|
|
128
|
+
if (scope === "global") return;
|
|
129
|
+
const current = activeScopes.peek();
|
|
130
|
+
if (!current.has(scope)) return;
|
|
131
|
+
const next = new Set(current);
|
|
132
|
+
next.delete(scope);
|
|
133
|
+
activeScopes.set(next);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Get the currently active scopes as a reactive signal.
|
|
137
|
+
*/
|
|
138
|
+
function getActiveScopes() {
|
|
139
|
+
return activeScopes;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Get all registered hotkeys (for building help dialogs).
|
|
143
|
+
*/
|
|
144
|
+
function getRegisteredHotkeys() {
|
|
145
|
+
return entries.map(e => ({
|
|
146
|
+
shortcut: e.shortcut,
|
|
147
|
+
scope: e.options.scope,
|
|
148
|
+
description: e.options.description
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
function _resetHotkeys() {
|
|
152
|
+
entries.length = 0;
|
|
153
|
+
activeScopes.set(new Set(["global"]));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/use-hotkey.ts
|
|
158
|
+
/**
|
|
159
|
+
* Register a keyboard shortcut scoped to a component's lifecycle.
|
|
160
|
+
* Automatically unregisters when the component unmounts.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* function Editor() {
|
|
165
|
+
* useHotkey('ctrl+s', () => save(), { description: 'Save document' })
|
|
166
|
+
* useHotkey('ctrl+z', () => undo())
|
|
167
|
+
* useHotkey('ctrl+shift+z', () => redo())
|
|
168
|
+
* // ...
|
|
169
|
+
* }
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
function useHotkey(shortcut, handler, options) {
|
|
173
|
+
onUnmount(registerHotkey(shortcut, handler, options));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/use-hotkey-scope.ts
|
|
178
|
+
/**
|
|
179
|
+
* Activate a hotkey scope for the lifetime of a component.
|
|
180
|
+
* When the component unmounts, the scope is deactivated.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```tsx
|
|
184
|
+
* function Modal() {
|
|
185
|
+
* useHotkeyScope('modal')
|
|
186
|
+
* useHotkey('escape', () => closeModal(), { scope: 'modal' })
|
|
187
|
+
* // ...
|
|
188
|
+
* }
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
function useHotkeyScope(scope) {
|
|
192
|
+
enableScope(scope);
|
|
193
|
+
onUnmount(() => disableScope(scope));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
//#endregion
|
|
197
|
+
export { _resetHotkeys, disableScope, enableScope, formatCombo, getActiveScopes, getRegisteredHotkeys, matchesCombo, parseShortcut, registerHotkey, useHotkey, useHotkeyScope };
|
|
198
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/parse.ts","../../src/registry.ts","../../src/use-hotkey.ts","../../src/use-hotkey-scope.ts"],"mappings":";;;;;;;;;AAsBA,SAAg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AO,OAAA,CAAQ,GAAA,CAAK,CAAA,KAAO;IACzB,QAAA,EAAU,CAAA,CAAE,QAAA;IACZ,KAAA,EAAO,CAAA,CAAE,OAAA,CAAQ,KAAA;IACjB,WAAA,EAAa,CAAA,CAAE,OAAA,CAAQ;GACxB,CAAA,CAAE;;AAKL,SAAgB,aAAA,CAAA,EAAsB;EACpC,OAAA,CAAQ,MAAA,GAAS,CAAA;EACjB,YAAA,CAAa,GAAA,CAAI,IAAI,GAAA,CAAI,CAAC,QAAA,CAAS,CAAC,CAAC;;;;;;;;;;;;;;;;;;;ACnIvC,SAAgB,SAAA,CACd,QAAA,EACA,OAAA,EACA,OAAA,EACM;EAEN,SAAA,CADmB,cAAA,CAAe,QAAA,EAAU,OAAA,EAAS,OAAA,CAAQ,CACxC;;;;;;;;;;;;;;;;;;ACRvB,SAAgB,cAAA,CAAe,KAAA,EAAqB;EAClD,WAAA,CAAY,KAAA,CAAM;EAClB,SAAA,CAAA,MAAgB,YAAA,CAAa,KAAA,CAAM,CAAC"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { Signal } from "@pyreon/reactivity";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A parsed key combination.
|
|
6
|
+
* Example: 'ctrl+shift+s' → { ctrl: true, shift: true, alt: false, meta: false, key: 's' }
|
|
7
|
+
*/
|
|
8
|
+
interface KeyCombo {
|
|
9
|
+
ctrl: boolean;
|
|
10
|
+
shift: boolean;
|
|
11
|
+
alt: boolean;
|
|
12
|
+
meta: boolean;
|
|
13
|
+
key: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Options for registering a hotkey.
|
|
17
|
+
*/
|
|
18
|
+
interface HotkeyOptions {
|
|
19
|
+
/** Scope for the hotkey — only active when this scope is active. Default: 'global' */
|
|
20
|
+
scope?: string;
|
|
21
|
+
/** Whether to prevent default browser behavior — default: true */
|
|
22
|
+
preventDefault?: boolean;
|
|
23
|
+
/** Whether to stop event propagation — default: false */
|
|
24
|
+
stopPropagation?: boolean;
|
|
25
|
+
/** Whether the hotkey fires when an input/textarea/contenteditable is focused — default: false */
|
|
26
|
+
enableOnInputs?: boolean;
|
|
27
|
+
/** Description of what this hotkey does — useful for help dialogs */
|
|
28
|
+
description?: string;
|
|
29
|
+
/** Whether the hotkey is enabled — default: true */
|
|
30
|
+
enabled?: boolean | (() => boolean);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A registered hotkey entry.
|
|
34
|
+
*/
|
|
35
|
+
interface HotkeyEntry {
|
|
36
|
+
/** The original shortcut string (e.g. 'ctrl+s') */
|
|
37
|
+
shortcut: string;
|
|
38
|
+
/** Parsed key combination */
|
|
39
|
+
combo: KeyCombo;
|
|
40
|
+
/** The callback to invoke */
|
|
41
|
+
handler: (event: KeyboardEvent) => void;
|
|
42
|
+
/** Options */
|
|
43
|
+
options: Required<Pick<HotkeyOptions, 'scope' | 'preventDefault' | 'stopPropagation' | 'enableOnInputs' | 'enabled'>> & {
|
|
44
|
+
description?: string;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/use-hotkey.d.ts
|
|
49
|
+
/**
|
|
50
|
+
* Register a keyboard shortcut scoped to a component's lifecycle.
|
|
51
|
+
* Automatically unregisters when the component unmounts.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* function Editor() {
|
|
56
|
+
* useHotkey('ctrl+s', () => save(), { description: 'Save document' })
|
|
57
|
+
* useHotkey('ctrl+z', () => undo())
|
|
58
|
+
* useHotkey('ctrl+shift+z', () => redo())
|
|
59
|
+
* // ...
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare function useHotkey(shortcut: string, handler: (event: KeyboardEvent) => void, options?: HotkeyOptions): void;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/use-hotkey-scope.d.ts
|
|
66
|
+
/**
|
|
67
|
+
* Activate a hotkey scope for the lifetime of a component.
|
|
68
|
+
* When the component unmounts, the scope is deactivated.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```tsx
|
|
72
|
+
* function Modal() {
|
|
73
|
+
* useHotkeyScope('modal')
|
|
74
|
+
* useHotkey('escape', () => closeModal(), { scope: 'modal' })
|
|
75
|
+
* // ...
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
declare function useHotkeyScope(scope: string): void;
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/registry.d.ts
|
|
82
|
+
/**
|
|
83
|
+
* Register a keyboard shortcut. Returns an unregister function.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* const unregister = registerHotkey('ctrl+s', (e) => save(), { description: 'Save' })
|
|
88
|
+
* // later: unregister()
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
declare function registerHotkey(shortcut: string, handler: (event: KeyboardEvent) => void, options?: HotkeyOptions): () => void;
|
|
92
|
+
/**
|
|
93
|
+
* Activate a hotkey scope. 'global' is always active.
|
|
94
|
+
*/
|
|
95
|
+
declare function enableScope(scope: string): void;
|
|
96
|
+
/**
|
|
97
|
+
* Deactivate a hotkey scope. Cannot deactivate 'global'.
|
|
98
|
+
*/
|
|
99
|
+
declare function disableScope(scope: string): void;
|
|
100
|
+
/**
|
|
101
|
+
* Get the currently active scopes as a reactive signal.
|
|
102
|
+
*/
|
|
103
|
+
declare function getActiveScopes(): Signal<Set<string>>;
|
|
104
|
+
/**
|
|
105
|
+
* Get all registered hotkeys (for building help dialogs).
|
|
106
|
+
*/
|
|
107
|
+
declare function getRegisteredHotkeys(): ReadonlyArray<{
|
|
108
|
+
shortcut: string;
|
|
109
|
+
scope: string;
|
|
110
|
+
description?: string;
|
|
111
|
+
}>;
|
|
112
|
+
declare function _resetHotkeys(): void;
|
|
113
|
+
//#endregion
|
|
114
|
+
//#region src/parse.d.ts
|
|
115
|
+
/**
|
|
116
|
+
* Parse a shortcut string like 'ctrl+shift+s' into a KeyCombo.
|
|
117
|
+
* Supports aliases (esc, del, space, etc.) and mod (ctrl on Windows/Linux, meta on Mac).
|
|
118
|
+
*/
|
|
119
|
+
declare function parseShortcut(shortcut: string): KeyCombo;
|
|
120
|
+
/**
|
|
121
|
+
* Check if a KeyboardEvent matches a KeyCombo.
|
|
122
|
+
*/
|
|
123
|
+
declare function matchesCombo(event: KeyboardEvent, combo: KeyCombo): boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Format a KeyCombo back to a human-readable string.
|
|
126
|
+
*/
|
|
127
|
+
declare function formatCombo(combo: KeyCombo): string;
|
|
128
|
+
//#endregion
|
|
129
|
+
export { type HotkeyEntry, type HotkeyOptions, type KeyCombo, _resetHotkeys, disableScope, enableScope, formatCombo, getActiveScopes, getRegisteredHotkeys, matchesCombo, parseShortcut, registerHotkey, useHotkey, useHotkeyScope };
|
|
130
|
+
//# sourceMappingURL=index2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index2.d.ts","names":[],"sources":["../../src/types.ts","../../src/use-hotkey.ts","../../src/use-hotkey-scope.ts","../../src/registry.ts","../../src/parse.ts"],"mappings":";;;;;;AAMA;UAAiB,QAAA;EACf,IAAA;EACA,KAAA;EACA,GAAA;EACA,IAAA;EACA,GAAA;AAAA;;;;UAMe,aAAA;EAAa;EAE5B,KAAA;EAF4B;EAI5B,cAAA;EAAA;EAEA,eAAA;EAEA;EAAA,cAAA;EAIA;EAFA,WAAA;EAEO;EAAP,OAAA;AAAA;;;;UAMe,WAAA;EASb;EAPF,QAAA;EAMiB;EAJjB,KAAA,EAAO,QAAA;EAFP;EAIA,OAAA,GAAU,KAAA,EAAO,aAAA;EAFV;EAIP,OAAA,EAAS,QAAA,CACP,IAAA,CACE,aAAA;IAOE,WAAA;EAAA;AAAA;;;;;AA9CR;;;;;;;;;;;AAWA;iBCCgB,SAAA,CACd,QAAA,UACA,OAAA,GAAU,KAAA,EAAO,aAAA,WACjB,OAAA,GAAU,aAAA;;;;;;ADfZ;;;;;;;;;;iBEUgB,cAAA,CAAe,KAAA;;;;AFV/B;;;;;;;;iBG+DgB,cAAA,CACd,QAAA,UACA,OAAA,GAAU,KAAA,EAAO,aAAA,WACjB,OAAA,GAAU,aAAA;;;AHvDZ;iBGsFgB,WAAA,CAAY,KAAA;;;;iBAWZ,YAAA,CAAa,KAAA;;;;iBAYb,eAAA,CAAA,GAAmB,MAAA,CAAO,GAAA;;;AH3F1C;iBGkGgB,oBAAA,CAAA,GAAwB,aAAA;EACtC,QAAA;EACA,KAAA;EACA,WAAA;AAAA;AAAA,iBAWc,aAAA,CAAA;;;;;AH7IhB;;iBIgBgB,aAAA,CAAc,QAAA,WAAmB,QAAA;;;;iBAsCjC,YAAA,CAAa,KAAA,EAAO,aAAA,EAAe,KAAA,EAAO,QAAA;;;;iBAa1C,WAAA,CAAY,KAAA,EAAO,QAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pyreon/hotkeys",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Reactive keyboard shortcut management for Pyreon — scope-aware, conflict detection",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/pyreon/fundamentals.git",
|
|
9
|
+
"directory": "packages/hotkeys"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/pyreon/fundamentals/tree/main/packages/hotkeys#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/pyreon/fundamentals/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"lib",
|
|
20
|
+
"src",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./lib/index.js",
|
|
26
|
+
"module": "./lib/index.js",
|
|
27
|
+
"types": "./lib/types/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"bun": "./src/index.ts",
|
|
31
|
+
"import": "./lib/index.js",
|
|
32
|
+
"types": "./lib/types/index.d.ts"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"sideEffects": false,
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "vl_rolldown_build",
|
|
38
|
+
"dev": "vl_rolldown_build-watch",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"typecheck": "tsc --noEmit"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@pyreon/core": ">=0.5.0 <1.0.0",
|
|
44
|
+
"@pyreon/reactivity": ">=0.5.0 <1.0.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@happy-dom/global-registrator": "^20.8.3",
|
|
48
|
+
"@pyreon/core": ">=0.5.0 <1.0.0",
|
|
49
|
+
"@pyreon/reactivity": ">=0.5.0 <1.0.0",
|
|
50
|
+
"@vitus-labs/tools-lint": "^1.11.0"
|
|
51
|
+
}
|
|
52
|
+
}
|