hotkey-router 0.3.3 → 0.4.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 +104 -4
- package/dist/auto.js +1 -1
- package/dist/auto.min.js +2 -2
- package/dist/core.js +697 -0
- package/dist/core.js.map +7 -0
- package/dist/format.js +1 -1
- package/dist/hotkey-router.cjs +344 -289
- package/dist/hotkey-router.cjs.map +4 -4
- package/dist/hotkey-router.js +6 -638
- package/dist/hotkey-router.js.map +3 -3
- package/dist/hotkey-router.min.js +2 -2
- package/dist/reservations.js +1 -1
- package/dist/sites.js +1 -1
- package/dist/types/core.d.ts +113 -0
- package/dist/types/hotkey-router.d.ts +20 -195
- package/dist/validate.js +1 -1
- package/package.json +10 -2
package/dist/hotkey-router.js
CHANGED
|
@@ -1,646 +1,14 @@
|
|
|
1
|
-
// hotkey-router v0.
|
|
1
|
+
// hotkey-router v0.4.0 | SEE LICENSE IN LICENSE
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
// platform.js
|
|
5
|
-
function fromPlatformString(s) {
|
|
6
|
-
if (!s) return null;
|
|
7
|
-
if (/Chrome ?OS|CrOS/i.test(s)) return "chromeos";
|
|
8
|
-
if (/Mac|iPhone|iPad|iPod/i.test(s)) return "mac";
|
|
9
|
-
if (/Win/i.test(s)) return "windows";
|
|
10
|
-
if (/Linux|X11/i.test(s)) return "linux";
|
|
11
|
-
return null;
|
|
12
|
-
}
|
|
13
|
-
function detectPlatform(nav = typeof navigator !== "undefined" ? navigator : null, { chromeos = false } = {}) {
|
|
14
|
-
if (!nav) return null;
|
|
15
|
-
const ua = nav.userAgent || "";
|
|
16
|
-
const found = (/\bCrOS\b/.test(ua) ? "chromeos" : null) || fromPlatformString(nav.userAgentData?.platform) || fromPlatformString(nav.platform) || (/Mac OS X/i.test(ua) ? "mac" : /Windows/i.test(ua) ? "windows" : /Linux|X11/i.test(ua) ? "linux" : null);
|
|
17
|
-
if (found === "chromeos" && !chromeos) return "linux";
|
|
18
|
-
return found;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// parse.js
|
|
22
|
-
var DEFAULT_PLATFORM = detectPlatform();
|
|
23
|
-
var modifierTokens = /* @__PURE__ */ new Set(["ctrl", "shift", "alt", "meta", "mod"]);
|
|
24
|
-
var ahkPrefixMap = /* @__PURE__ */ new Map([
|
|
25
|
-
["^", "ctrl"],
|
|
26
|
-
// Ctrl
|
|
27
|
-
["!", "alt"],
|
|
28
|
-
// Alt
|
|
29
|
-
["+", "shift"],
|
|
30
|
-
// Shift
|
|
31
|
-
["#", "meta"]
|
|
32
|
-
// Win / Command
|
|
33
|
-
]);
|
|
34
|
-
var keyAliases = Object.freeze({
|
|
35
|
-
// common
|
|
36
|
-
esc: "escape",
|
|
37
|
-
escape: "escape",
|
|
38
|
-
space: " ",
|
|
39
|
-
spacebar: " ",
|
|
40
|
-
enter: "enter",
|
|
41
|
-
return: "enter",
|
|
42
|
-
del: "delete",
|
|
43
|
-
delete: "delete",
|
|
44
|
-
backspace: "backspace",
|
|
45
|
-
tab: "tab",
|
|
46
|
-
// arrows
|
|
47
|
-
up: "arrowup",
|
|
48
|
-
down: "arrowdown",
|
|
49
|
-
left: "arrowleft",
|
|
50
|
-
right: "arrowright",
|
|
51
|
-
// navigation
|
|
52
|
-
home: "home",
|
|
53
|
-
end: "end",
|
|
54
|
-
pageup: "pageup",
|
|
55
|
-
pagedown: "pagedown",
|
|
56
|
-
pgup: "pageup",
|
|
57
|
-
pgdn: "pagedown",
|
|
58
|
-
// modifiers (words)
|
|
59
|
-
cmd: "meta",
|
|
60
|
-
command: "meta",
|
|
61
|
-
win: "meta",
|
|
62
|
-
meta: "meta",
|
|
63
|
-
control: "ctrl",
|
|
64
|
-
ctrl: "ctrl",
|
|
65
|
-
option: "alt",
|
|
66
|
-
opt: "alt",
|
|
67
|
-
alt: "alt",
|
|
68
|
-
shift: "shift",
|
|
69
|
-
// modifiers (symbols)
|
|
70
|
-
"\u2318": "meta",
|
|
71
|
-
"\u2303": "ctrl",
|
|
72
|
-
"\u2325": "alt",
|
|
73
|
-
"\u21E7": "shift",
|
|
74
|
-
// cross-platform "mod", resolved per platform while parsing
|
|
75
|
-
mod: "mod",
|
|
76
|
-
// plus key ergonomics
|
|
77
|
-
plus: "+"
|
|
78
|
-
});
|
|
79
|
-
function normalizeKey(key) {
|
|
80
|
-
const original = String(key ?? "");
|
|
81
|
-
if (original === " ") return " ";
|
|
82
|
-
const trimmed = original.trim();
|
|
83
|
-
const k = trimmed.toLowerCase();
|
|
84
|
-
return keyAliases[k] ?? k;
|
|
85
|
-
}
|
|
86
|
-
function isModifierToken(token) {
|
|
87
|
-
return modifierTokens.has(normalizeKey(token));
|
|
88
|
-
}
|
|
89
|
-
var CODE_TOKEN_RE = /(^|\+)code:([A-Za-z][A-Za-z0-9_]*)/;
|
|
90
|
-
function readHotkey(hotkeyStr, { platform = DEFAULT_PLATFORM } = {}) {
|
|
91
|
-
const raw = String(hotkeyStr || "").trim();
|
|
92
|
-
let error = null;
|
|
93
|
-
const fail = (msg) => {
|
|
94
|
-
error = error || `Invalid hotkey "${hotkeyStr}": ${msg}`;
|
|
95
|
-
};
|
|
96
|
-
const isUp = /\s+up$/i.test(raw);
|
|
97
|
-
const baseRaw = isUp ? raw.replace(/\s+up$/i, "").trim() : raw;
|
|
98
|
-
const combo = {
|
|
99
|
-
ctrl: false,
|
|
100
|
-
shift: false,
|
|
101
|
-
alt: false,
|
|
102
|
-
meta: false,
|
|
103
|
-
key: null,
|
|
104
|
-
code: null,
|
|
105
|
-
bareModifier: null
|
|
106
|
-
};
|
|
107
|
-
const setMod = (mod) => {
|
|
108
|
-
if (mod === "mod") combo[platform === "mac" ? "meta" : "ctrl"] = true;
|
|
109
|
-
else combo[mod] = true;
|
|
110
|
-
};
|
|
111
|
-
let stripped = baseRaw;
|
|
112
|
-
while (stripped.length) {
|
|
113
|
-
const mod = ahkPrefixMap.get(stripped[0]) ?? null;
|
|
114
|
-
if (!mod) break;
|
|
115
|
-
setMod(mod);
|
|
116
|
-
stripped = stripped.slice(1).trimStart();
|
|
117
|
-
}
|
|
118
|
-
const codeMatches = stripped.match(/code:[A-Za-z][A-Za-z0-9_]*/g);
|
|
119
|
-
if (codeMatches && codeMatches.length > 1) {
|
|
120
|
-
fail(`multiple code: tokens not supported (got ${codeMatches.length}).`);
|
|
121
|
-
}
|
|
122
|
-
let codeValue = null;
|
|
123
|
-
const scrubbed = stripped.replace(CODE_TOKEN_RE, (_, sep, val) => {
|
|
124
|
-
codeValue = val;
|
|
125
|
-
return sep + "__code__";
|
|
126
|
-
});
|
|
127
|
-
const cleanStr = scrubbed.toLowerCase();
|
|
128
|
-
const rawParts = cleanStr.split("+").map((p) => p.trim());
|
|
129
|
-
const parts = rawParts.filter(Boolean);
|
|
130
|
-
const impliedPlusKey = rawParts.some((p) => p === "") && cleanStr.includes("++");
|
|
131
|
-
if (!impliedPlusKey) {
|
|
132
|
-
let seenBase = false;
|
|
133
|
-
for (const part of parts) {
|
|
134
|
-
if (isModifierToken(part)) {
|
|
135
|
-
if (seenBase) fail('modifiers must come before the base key (e.g. "ctrl+k", not "k+ctrl")');
|
|
136
|
-
} else {
|
|
137
|
-
seenBase = true;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
for (const part of parts) {
|
|
142
|
-
if (part === "__code__") {
|
|
143
|
-
combo.code = codeValue;
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
const actual = keyAliases[part] ?? part;
|
|
147
|
-
if (modifierTokens.has(actual)) setMod(actual);
|
|
148
|
-
else combo.key = normalizeKey(actual);
|
|
149
|
-
}
|
|
150
|
-
if (!combo.key && !combo.code && impliedPlusKey) combo.key = "+";
|
|
151
|
-
if (!combo.key && !combo.code) {
|
|
152
|
-
const modCount = (combo.ctrl ? 1 : 0) + (combo.shift ? 1 : 0) + (combo.alt ? 1 : 0) + (combo.meta ? 1 : 0);
|
|
153
|
-
if (modCount === 1) {
|
|
154
|
-
combo.bareModifier = combo.ctrl ? "ctrl" : combo.shift ? "shift" : combo.alt ? "alt" : "meta";
|
|
155
|
-
} else if (modCount > 1) {
|
|
156
|
-
fail('multi-modifier bare bindings not supported. Add a base key (e.g. "ctrl+alt+x").');
|
|
157
|
-
} else {
|
|
158
|
-
error = error || `Invalid hotkey "${hotkeyStr}" (missing base key)`;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return { combo, type: isUp ? "keyup" : "keydown", raw: hotkeyStr, error };
|
|
162
|
-
}
|
|
163
|
-
function parseHotkey(hotkeyStr, opts) {
|
|
164
|
-
const { combo, type, raw, error } = readHotkey(hotkeyStr, opts);
|
|
165
|
-
if (error) throw new Error(error);
|
|
166
|
-
return { combo, type, raw };
|
|
167
|
-
}
|
|
168
|
-
var SHIFTED_PUNCT = {
|
|
169
|
-
",": ["<", "Comma"],
|
|
170
|
-
".": [">", "Period"],
|
|
171
|
-
"/": ["?", "Slash"],
|
|
172
|
-
";": [":", "Semicolon"],
|
|
173
|
-
"'": ['"', "Quote"],
|
|
174
|
-
"[": ["{", "BracketLeft"],
|
|
175
|
-
"]": ["}", "BracketRight"],
|
|
176
|
-
"\\": ["|", "Backslash"],
|
|
177
|
-
"-": ["_", "Minus"],
|
|
178
|
-
"=": ["+", "Equal"],
|
|
179
|
-
"`": ["~", "Backquote"]
|
|
180
|
-
};
|
|
181
|
-
function shiftedPunctuationHint(hotkeyStr) {
|
|
182
|
-
const { combo, type, error } = readHotkey(hotkeyStr);
|
|
183
|
-
if (error || !combo.shift || combo.code) return null;
|
|
184
|
-
const shifted = SHIFTED_PUNCT[combo.key];
|
|
185
|
-
if (!shifted) return null;
|
|
186
|
-
const [typed, code] = shifted;
|
|
187
|
-
const base = String(hotkeyStr).trim().replace(/\s+up$/i, "");
|
|
188
|
-
const suggestion = base.slice(0, -1) + "code:" + code + (type === "keyup" ? " up" : "");
|
|
189
|
-
return {
|
|
190
|
-
message: `"${hotkeyStr}" never fires on US layouts: Shift turns "${combo.key}" into "${typed}". Bind "${suggestion}" instead.`,
|
|
191
|
-
suggestion
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
|
|
195
4
|
// hotkey-router.js
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
var PLATFORM = detectPlatform();
|
|
201
|
-
var MODIFIER_KEY_TOKEN = Object.freeze({
|
|
202
|
-
Alt: "alt",
|
|
203
|
-
AltGraph: "alt",
|
|
204
|
-
Control: "ctrl",
|
|
205
|
-
Shift: "shift",
|
|
206
|
-
Meta: "meta",
|
|
207
|
-
OS: "meta"
|
|
208
|
-
});
|
|
209
|
-
var ignoreEditable = true;
|
|
210
|
-
var defaultTarget = typeof window !== "undefined" ? window : null;
|
|
211
|
-
var bindHooks = [];
|
|
212
|
-
var registry = /* @__PURE__ */ new Map();
|
|
213
|
-
var plugins = /* @__PURE__ */ new Map();
|
|
214
|
-
var bindingIndex = /* @__PURE__ */ new Map();
|
|
215
|
-
var paused = false;
|
|
216
|
-
var nextId = 1;
|
|
217
|
-
var warnedShifted = /* @__PURE__ */ new Set();
|
|
218
|
-
function comboKeyFromParts({ ctrl, shift, alt, meta, key, code, bareModifier }) {
|
|
219
|
-
if (bareModifier) return `__bare__:${bareModifier}`;
|
|
220
|
-
let s = "";
|
|
221
|
-
if (ctrl) s += "ctrl+";
|
|
222
|
-
if (shift) s += "shift+";
|
|
223
|
-
if (alt) s += "alt+";
|
|
224
|
-
if (meta) s += "meta+";
|
|
225
|
-
if (code) return s + "code:" + code;
|
|
226
|
-
return s ? s + key : key;
|
|
227
|
-
}
|
|
228
|
-
function comboKeyFromEvent(e) {
|
|
229
|
-
const bareMod = MODIFIER_KEY_TOKEN[e.key];
|
|
230
|
-
if (bareMod) return `__bare__:${bareMod}`;
|
|
231
|
-
return comboKeyFromParts({
|
|
232
|
-
ctrl: e.ctrlKey,
|
|
233
|
-
shift: e.shiftKey,
|
|
234
|
-
alt: e.altKey,
|
|
235
|
-
meta: e.metaKey,
|
|
236
|
-
key: normalizeKey(e.key)
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
function codeKeyFromEvent(e) {
|
|
240
|
-
if (!e.code) return null;
|
|
241
|
-
let s = "";
|
|
242
|
-
if (e.ctrlKey) s += "ctrl+";
|
|
243
|
-
if (e.shiftKey) s += "shift+";
|
|
244
|
-
if (e.altKey) s += "alt+";
|
|
245
|
-
if (e.metaKey) s += "meta+";
|
|
246
|
-
return s + "code:" + e.code;
|
|
247
|
-
}
|
|
248
|
-
function isFromInputTarget(e) {
|
|
249
|
-
const t = e?.composedPath?.()[0] ?? e?.target;
|
|
250
|
-
if (!t) return false;
|
|
251
|
-
const tag = String(t.tagName || "").toUpperCase();
|
|
252
|
-
return t.isContentEditable || tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || t.getAttribute?.("role") === "textbox";
|
|
253
|
-
}
|
|
254
|
-
function normalizeOptions(type, options) {
|
|
255
|
-
const o = options ? { ...options } : {};
|
|
256
|
-
if (type === "keydown" && o.repeat == null) o.repeat = false;
|
|
257
|
-
if (o.priority == null) o.priority = 0;
|
|
258
|
-
return o;
|
|
259
|
-
}
|
|
260
|
-
function ensureSlot(comboStr, type) {
|
|
261
|
-
let typeMap = registry.get(comboStr);
|
|
262
|
-
if (!typeMap) {
|
|
263
|
-
typeMap = /* @__PURE__ */ new Map();
|
|
264
|
-
registry.set(comboStr, typeMap);
|
|
265
|
-
}
|
|
266
|
-
let arr = typeMap.get(type);
|
|
267
|
-
if (!arr) {
|
|
268
|
-
arr = [];
|
|
269
|
-
typeMap.set(type, arr);
|
|
270
|
-
}
|
|
271
|
-
return arr;
|
|
272
|
-
}
|
|
273
|
-
function removeById(id) {
|
|
274
|
-
const idx = bindingIndex.get(id);
|
|
275
|
-
if (!idx) return false;
|
|
276
|
-
const { comboStr, type } = idx;
|
|
277
|
-
const typeMap = registry.get(comboStr);
|
|
278
|
-
const arr = typeMap?.get(type);
|
|
279
|
-
if (!arr?.length) {
|
|
280
|
-
bindingIndex.delete(id);
|
|
281
|
-
return false;
|
|
282
|
-
}
|
|
283
|
-
const next = arr.filter((b) => b.id !== id);
|
|
284
|
-
if (next.length) typeMap.set(type, next);
|
|
285
|
-
else typeMap.delete(type);
|
|
286
|
-
if (!typeMap.size) registry.delete(comboStr);
|
|
287
|
-
bindingIndex.delete(id);
|
|
288
|
-
return true;
|
|
289
|
-
}
|
|
290
|
-
function bind(hotkeyStr, handler, plugin = null, options = {}) {
|
|
291
|
-
const { combo, type, raw } = parseHotkey(hotkeyStr);
|
|
292
|
-
const comboStr = comboKeyFromParts(combo);
|
|
293
|
-
const opts = normalizeOptions(type, options);
|
|
294
|
-
const binding = {
|
|
295
|
-
id: nextId++,
|
|
296
|
-
comboStr,
|
|
297
|
-
type,
|
|
298
|
-
combo,
|
|
299
|
-
handler,
|
|
300
|
-
plugin,
|
|
301
|
-
raw,
|
|
302
|
-
options: opts
|
|
303
|
-
};
|
|
304
|
-
ensureSlot(comboStr, type).push(binding);
|
|
305
|
-
bindingIndex.set(binding.id, { comboStr, type });
|
|
306
|
-
const warnKey = `${comboStr} ${type}`;
|
|
307
|
-
if (!warnedShifted.has(warnKey)) {
|
|
308
|
-
const hint = shiftedPunctuationHint(hotkeyStr);
|
|
309
|
-
if (hint) {
|
|
310
|
-
warnedShifted.add(warnKey);
|
|
311
|
-
console.warn(`[hotkey-router] ${hint.message}`);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
if (bindHooks.length) {
|
|
315
|
-
for (const hook of bindHooks) {
|
|
316
|
-
try {
|
|
317
|
-
hook({ combo, raw, options: opts, plugin, id: binding.id });
|
|
318
|
-
} catch (err) {
|
|
319
|
-
console.error("[hotkey-router] onBind hook threw:", err);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
const off = () => removeById(binding.id);
|
|
324
|
-
off.id = binding.id;
|
|
325
|
-
off.hotkey = raw;
|
|
326
|
-
return off;
|
|
327
|
-
}
|
|
328
|
-
function onBind(hook) {
|
|
329
|
-
if (typeof hook !== "function") {
|
|
330
|
-
throw new TypeError("hotkeys.onBind(hook) expects a function");
|
|
331
|
-
}
|
|
332
|
-
bindHooks.push(hook);
|
|
333
|
-
return () => {
|
|
334
|
-
bindHooks = bindHooks.filter((h) => h !== hook);
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
|
-
function unbind(hotkeyStr, handler) {
|
|
338
|
-
const { combo, type } = parseHotkey(hotkeyStr);
|
|
339
|
-
const comboStr = comboKeyFromParts(combo);
|
|
340
|
-
const typeMap = registry.get(comboStr);
|
|
341
|
-
if (!typeMap) return;
|
|
342
|
-
const arr = typeMap.get(type);
|
|
343
|
-
if (!arr?.length) return;
|
|
344
|
-
if (!handler) {
|
|
345
|
-
for (const b of arr) bindingIndex.delete(b.id);
|
|
346
|
-
typeMap.delete(type);
|
|
347
|
-
} else {
|
|
348
|
-
const keep = [];
|
|
349
|
-
for (const b of arr) {
|
|
350
|
-
if (b.handler === handler) bindingIndex.delete(b.id);
|
|
351
|
-
else keep.push(b);
|
|
352
|
-
}
|
|
353
|
-
if (keep.length) typeMap.set(type, keep);
|
|
354
|
-
else typeMap.delete(type);
|
|
355
|
-
}
|
|
356
|
-
if (!typeMap.size) registry.delete(comboStr);
|
|
357
|
-
}
|
|
358
|
-
function registerPlugin(name, hotkeyMap) {
|
|
359
|
-
if (plugins.has(name)) throw new Error(`Plugin "${name}" already registered`);
|
|
360
|
-
const entries = Object.entries(hotkeyMap);
|
|
361
|
-
for (const [hotkeyStr] of entries) parseHotkey(hotkeyStr);
|
|
362
|
-
const ids = [];
|
|
363
|
-
for (const [hotkeyStr, fnOrTuple] of entries) {
|
|
364
|
-
const [fn, options] = Array.isArray(fnOrTuple) ? fnOrTuple : [fnOrTuple, {}];
|
|
365
|
-
const off = bind(hotkeyStr, fn, name, options);
|
|
366
|
-
ids.push(off.id);
|
|
367
|
-
}
|
|
368
|
-
plugins.set(name, ids);
|
|
369
|
-
return () => unregisterPlugin(name);
|
|
370
|
-
}
|
|
371
|
-
function unregisterPlugin(name) {
|
|
372
|
-
const ids = plugins.get(name);
|
|
373
|
-
if (!ids) return;
|
|
374
|
-
for (const id of ids) removeById(id);
|
|
375
|
-
plugins.delete(name);
|
|
376
|
-
}
|
|
377
|
-
function pause() {
|
|
378
|
-
paused = true;
|
|
379
|
-
}
|
|
380
|
-
function resume() {
|
|
381
|
-
paused = false;
|
|
382
|
-
}
|
|
383
|
-
function ignoreInput(value = true) {
|
|
384
|
-
ignoreEditable = !!value;
|
|
385
|
-
}
|
|
386
|
-
function setTarget(target) {
|
|
387
|
-
defaultTarget = target;
|
|
388
|
-
}
|
|
389
|
-
function pickWinner(bindings) {
|
|
390
|
-
let best = null;
|
|
391
|
-
for (const b of bindings) {
|
|
392
|
-
if (!best) best = b;
|
|
393
|
-
else if (b.options.priority > best.options.priority) best = b;
|
|
394
|
-
else if (b.options.priority === best.options.priority && b.id > best.id) best = b;
|
|
395
|
-
}
|
|
396
|
-
return best;
|
|
397
|
-
}
|
|
398
|
-
function isAltGraphCharacter(e) {
|
|
399
|
-
return Boolean(
|
|
400
|
-
e.ctrlKey && e.altKey && typeof e.getModifierState === "function" && e.getModifierState("AltGraph") && typeof e.key === "string" && e.key.length === 1
|
|
401
|
-
);
|
|
402
|
-
}
|
|
403
|
-
function isComposingEvent(e) {
|
|
404
|
-
return Boolean(e.isComposing || e.keyCode === 229);
|
|
405
|
-
}
|
|
406
|
-
var NON_LATIN_LETTER = /^(?!\p{Script=Latin})\p{L}$/u;
|
|
407
|
-
function latinKeyFromEvent(e) {
|
|
408
|
-
if (!(e.ctrlKey || e.metaKey) || e.altKey) return null;
|
|
409
|
-
if (isComposingEvent(e)) return null;
|
|
410
|
-
if (typeof e.getModifierState === "function" && e.getModifierState("AltGraph")) return null;
|
|
411
|
-
const letter = /^Key([A-Z])$/.exec(e.code || "");
|
|
412
|
-
if (!letter || typeof e.key !== "string" || !NON_LATIN_LETTER.test(e.key)) return null;
|
|
413
|
-
return comboKeyFromParts({
|
|
414
|
-
ctrl: e.ctrlKey,
|
|
415
|
-
shift: e.shiftKey,
|
|
416
|
-
alt: false,
|
|
417
|
-
meta: e.metaKey,
|
|
418
|
-
key: letter[1].toLowerCase()
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
function eligibleBindings(e, type, phase) {
|
|
422
|
-
const guards = {
|
|
423
|
-
altGraphChar: isAltGraphCharacter(e),
|
|
424
|
-
composing: isComposingEvent(e),
|
|
425
|
-
fromInput: ignoreEditable && isFromInputTarget(e)
|
|
426
|
-
};
|
|
427
|
-
const candidates = [];
|
|
428
|
-
const seen = /* @__PURE__ */ new Set();
|
|
429
|
-
for (const lookupKey of [comboKeyFromEvent(e), codeKeyFromEvent(e)]) {
|
|
430
|
-
if (!lookupKey || seen.has(lookupKey)) continue;
|
|
431
|
-
seen.add(lookupKey);
|
|
432
|
-
collectEligible(candidates, lookupKey, e, type, phase, guards);
|
|
433
|
-
}
|
|
434
|
-
if (!candidates.length) {
|
|
435
|
-
const latinKey = latinKeyFromEvent(e);
|
|
436
|
-
if (latinKey && !seen.has(latinKey)) collectEligible(candidates, latinKey, e, type, phase, guards);
|
|
437
|
-
}
|
|
438
|
-
return candidates;
|
|
439
|
-
}
|
|
440
|
-
function collectEligible(candidates, lookupKey, e, type, phase, { altGraphChar, composing, fromInput }) {
|
|
441
|
-
const bindings = registry.get(lookupKey)?.get(type);
|
|
442
|
-
if (!bindings?.length) return;
|
|
443
|
-
for (const b of bindings) {
|
|
444
|
-
const o = b.options;
|
|
445
|
-
if (phase === "capture" && !o.capture) continue;
|
|
446
|
-
if (phase === "bubble" && o.capture) continue;
|
|
447
|
-
if (altGraphChar && !o.altGraph) continue;
|
|
448
|
-
if (composing && !o.composing) continue;
|
|
449
|
-
if (fromInput && !o.allowIn?.(e)) continue;
|
|
450
|
-
if (type === "keydown" && o.repeat === false && e.repeat) continue;
|
|
451
|
-
if (o.when && o.when(e) === false) continue;
|
|
452
|
-
candidates.push(b);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
function fire(winner, e) {
|
|
456
|
-
const o = winner.options;
|
|
457
|
-
if (o.preventDefault) e.preventDefault();
|
|
458
|
-
if (o.stopImmediatePropagation) e.stopImmediatePropagation();
|
|
459
|
-
else if (o.stopPropagation) e.stopPropagation();
|
|
460
|
-
try {
|
|
461
|
-
winner.handler(e);
|
|
462
|
-
} finally {
|
|
463
|
-
if (o.once) removeById(winner.id);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
function handleEvent(type, phase = "all") {
|
|
467
|
-
return (e) => {
|
|
468
|
-
if (paused) return;
|
|
469
|
-
const candidates = eligibleBindings(e, type, phase);
|
|
470
|
-
if (candidates.length) fire(pickWinner(candidates), e);
|
|
471
|
-
};
|
|
472
|
-
}
|
|
473
|
-
var stopDown = null;
|
|
474
|
-
var stopUp = null;
|
|
475
|
-
var stopCaptureDown = null;
|
|
476
|
-
var stopCaptureUp = null;
|
|
477
|
-
function init({ target = defaultTarget, capture = false } = {}) {
|
|
478
|
-
if (!target) throw new Error("hotkeys.init() requires a target (e.g. window)");
|
|
479
|
-
if (stopDown || stopUp) destroy();
|
|
480
|
-
if (capture) {
|
|
481
|
-
stopDown = on(target, "keydown", handleEvent("keydown"), { capture: true });
|
|
482
|
-
stopUp = on(target, "keyup", handleEvent("keyup"), { capture: true });
|
|
483
|
-
return;
|
|
484
|
-
}
|
|
485
|
-
stopDown = on(target, "keydown", handleEvent("keydown", "bubble"), { capture: false });
|
|
486
|
-
stopUp = on(target, "keyup", handleEvent("keyup", "bubble"), { capture: false });
|
|
487
|
-
stopCaptureDown = on(target, "keydown", handleEvent("keydown", "capture"), { capture: true });
|
|
488
|
-
stopCaptureUp = on(target, "keyup", handleEvent("keyup", "capture"), { capture: true });
|
|
489
|
-
}
|
|
490
|
-
function destroy() {
|
|
491
|
-
stopDown?.();
|
|
492
|
-
stopUp?.();
|
|
493
|
-
stopCaptureDown?.();
|
|
494
|
-
stopCaptureUp?.();
|
|
495
|
-
stopDown = null;
|
|
496
|
-
stopUp = null;
|
|
497
|
-
stopCaptureDown = null;
|
|
498
|
-
stopCaptureUp = null;
|
|
499
|
-
registry.clear();
|
|
500
|
-
plugins.clear();
|
|
501
|
-
bindingIndex.clear();
|
|
502
|
-
}
|
|
503
|
-
var BARE_MOD_TO_KEY = {
|
|
504
|
-
ctrl: "Control",
|
|
505
|
-
shift: "Shift",
|
|
506
|
-
alt: "Alt",
|
|
507
|
-
meta: "Meta"
|
|
508
|
-
};
|
|
509
|
-
var CODE_TO_KEY = {
|
|
510
|
-
Period: ".",
|
|
511
|
-
Comma: ",",
|
|
512
|
-
Semicolon: ";",
|
|
513
|
-
Quote: "'",
|
|
514
|
-
Slash: "/",
|
|
515
|
-
Backslash: "\\",
|
|
516
|
-
BracketLeft: "[",
|
|
517
|
-
BracketRight: "]",
|
|
518
|
-
Minus: "-",
|
|
519
|
-
Equal: "=",
|
|
520
|
-
Backquote: "`",
|
|
521
|
-
Space: " ",
|
|
522
|
-
Enter: "Enter",
|
|
523
|
-
NumpadEnter: "Enter",
|
|
524
|
-
Tab: "Tab",
|
|
525
|
-
Escape: "Escape",
|
|
526
|
-
Backspace: "Backspace",
|
|
527
|
-
Delete: "Delete",
|
|
528
|
-
Insert: "Insert",
|
|
529
|
-
Home: "Home",
|
|
530
|
-
End: "End",
|
|
531
|
-
PageUp: "PageUp",
|
|
532
|
-
PageDown: "PageDown",
|
|
533
|
-
ArrowUp: "ArrowUp",
|
|
534
|
-
ArrowDown: "ArrowDown",
|
|
535
|
-
ArrowLeft: "ArrowLeft",
|
|
536
|
-
ArrowRight: "ArrowRight"
|
|
537
|
-
};
|
|
538
|
-
var KEY_TO_CODE = {};
|
|
539
|
-
var _a;
|
|
540
|
-
for (const [code, key] of Object.entries(CODE_TO_KEY)) {
|
|
541
|
-
KEY_TO_CODE[_a = normalizeKey(key)] ?? (KEY_TO_CODE[_a] = code);
|
|
542
|
-
}
|
|
543
|
-
function keyForCode(code) {
|
|
544
|
-
if (!code) return null;
|
|
545
|
-
const letter = /^Key([A-Z])$/.exec(code);
|
|
546
|
-
if (letter) return letter[1].toLowerCase();
|
|
547
|
-
const digit = /^(?:Digit|Numpad)(\d)$/.exec(code);
|
|
548
|
-
if (digit) return digit[1];
|
|
549
|
-
if (/^F\d{1,2}$/.test(code)) return code;
|
|
550
|
-
return CODE_TO_KEY[code] ?? null;
|
|
551
|
-
}
|
|
552
|
-
function codeForKey(key) {
|
|
553
|
-
if (!key) return null;
|
|
554
|
-
if (/^[a-z]$/.test(key)) return "Key" + key.toUpperCase();
|
|
555
|
-
if (/^\d$/.test(key)) return "Digit" + key;
|
|
556
|
-
if (/^f\d{1,2}$/.test(key)) return key.toUpperCase();
|
|
557
|
-
return KEY_TO_CODE[key] ?? null;
|
|
558
|
-
}
|
|
559
|
-
function trigger(hotkeyStr, { type: forcedType, target = null } = {}) {
|
|
560
|
-
const { combo, type } = parseHotkey(hotkeyStr);
|
|
561
|
-
const actualType = forcedType || type;
|
|
562
|
-
if (paused) return false;
|
|
563
|
-
const eventKey = combo.bareModifier ? BARE_MOD_TO_KEY[combo.bareModifier] : combo.key ?? keyForCode(combo.code);
|
|
564
|
-
const eventCode = combo.bareModifier ? null : combo.code ?? codeForKey(combo.key);
|
|
565
|
-
let altKey = !!combo.alt;
|
|
566
|
-
let ctrlKey = !!combo.ctrl;
|
|
567
|
-
let shiftKey = !!combo.shift;
|
|
568
|
-
let metaKey = !!combo.meta;
|
|
569
|
-
if (combo.bareModifier && actualType === "keyup") {
|
|
570
|
-
if (combo.bareModifier === "alt") altKey = false;
|
|
571
|
-
else if (combo.bareModifier === "ctrl") ctrlKey = false;
|
|
572
|
-
else if (combo.bareModifier === "shift") shiftKey = false;
|
|
573
|
-
else if (combo.bareModifier === "meta") metaKey = false;
|
|
574
|
-
}
|
|
575
|
-
const e = {
|
|
576
|
-
type: actualType,
|
|
577
|
-
key: eventKey,
|
|
578
|
-
code: eventCode,
|
|
579
|
-
ctrlKey,
|
|
580
|
-
shiftKey,
|
|
581
|
-
altKey,
|
|
582
|
-
metaKey,
|
|
583
|
-
repeat: false,
|
|
584
|
-
isComposing: false,
|
|
585
|
-
getModifierState() {
|
|
586
|
-
return false;
|
|
587
|
-
},
|
|
588
|
-
preventDefault() {
|
|
589
|
-
},
|
|
590
|
-
stopPropagation() {
|
|
591
|
-
},
|
|
592
|
-
stopImmediatePropagation() {
|
|
593
|
-
},
|
|
594
|
-
target
|
|
595
|
-
};
|
|
596
|
-
const candidates = eligibleBindings(e, actualType, "all");
|
|
597
|
-
if (!candidates.length) return false;
|
|
598
|
-
fire(pickWinner(candidates), e);
|
|
599
|
-
return true;
|
|
600
|
-
}
|
|
601
|
-
function comboFromEvent(e, { physical = true, useMod = false, platform = PLATFORM } = {}) {
|
|
602
|
-
if (!e || MODIFIER_KEY_TOKEN[e.key]) return null;
|
|
603
|
-
const parts = [];
|
|
604
|
-
const primaryIsMeta = platform === "mac";
|
|
605
|
-
if (useMod && (primaryIsMeta ? e.metaKey : e.ctrlKey)) parts.push("mod");
|
|
606
|
-
if (e.ctrlKey && !(useMod && !primaryIsMeta)) parts.push("ctrl");
|
|
607
|
-
if (e.metaKey && !(useMod && primaryIsMeta)) parts.push("meta");
|
|
608
|
-
if (e.altKey) parts.push("alt");
|
|
609
|
-
if (e.shiftKey) parts.push("shift");
|
|
610
|
-
if (physical && e.code) parts.push(`code:${e.code}`);
|
|
611
|
-
else {
|
|
612
|
-
const key = normalizeKey(e.key);
|
|
613
|
-
if (!key) return null;
|
|
614
|
-
parts.push(key === " " ? "space" : key);
|
|
615
|
-
}
|
|
616
|
-
return parts.join("+");
|
|
617
|
-
}
|
|
618
|
-
if (defaultTarget) init();
|
|
619
|
-
var hotkey_router_default = {
|
|
620
|
-
// lifecycle
|
|
621
|
-
init,
|
|
622
|
-
destroy,
|
|
623
|
-
setTarget,
|
|
624
|
-
// bindings
|
|
625
|
-
bind,
|
|
626
|
-
unbind,
|
|
627
|
-
registerPlugin,
|
|
628
|
-
unregisterPlugin,
|
|
629
|
-
// extension hooks
|
|
630
|
-
onBind,
|
|
631
|
-
// control
|
|
632
|
-
pause,
|
|
633
|
-
resume,
|
|
634
|
-
ignoreInput,
|
|
635
|
-
// testing / automation
|
|
636
|
-
trigger,
|
|
637
|
-
// helpers
|
|
638
|
-
parseHotkey,
|
|
639
|
-
comboFromEvent,
|
|
640
|
-
detectPlatform
|
|
641
|
-
};
|
|
5
|
+
import { createRouter, parseHotkey, comboFromEvent, detectPlatform } from "./core.js";
|
|
6
|
+
var hotkeys = createRouter();
|
|
7
|
+
if (typeof window !== "undefined") hotkeys.init();
|
|
8
|
+
var hotkey_router_default = hotkeys;
|
|
642
9
|
export {
|
|
643
10
|
comboFromEvent,
|
|
11
|
+
createRouter,
|
|
644
12
|
hotkey_router_default as default,
|
|
645
13
|
detectPlatform,
|
|
646
14
|
parseHotkey
|