hotkey-router 0.3.3 → 0.4.1
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 +169 -23
- 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.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// hotkey-router v0.
|
|
1
|
+
// hotkey-router v0.4.1 | SEE LICENSE IN LICENSE
|
|
2
2
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -22,6 +22,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
22
22
|
var hotkey_router_exports = {};
|
|
23
23
|
__export(hotkey_router_exports, {
|
|
24
24
|
comboFromEvent: () => comboFromEvent,
|
|
25
|
+
createRouter: () => createRouter,
|
|
25
26
|
default: () => hotkey_router_default,
|
|
26
27
|
detectPlatform: () => detectPlatform,
|
|
27
28
|
parseHotkey: () => parseHotkey
|
|
@@ -219,7 +220,7 @@ function shiftedPunctuationHint(hotkeyStr) {
|
|
|
219
220
|
};
|
|
220
221
|
}
|
|
221
222
|
|
|
222
|
-
//
|
|
223
|
+
// core.js
|
|
223
224
|
function on(target, type, fn, options) {
|
|
224
225
|
target.addEventListener(type, fn, options);
|
|
225
226
|
return () => target.removeEventListener(type, fn, options);
|
|
@@ -233,14 +234,6 @@ var MODIFIER_KEY_TOKEN = Object.freeze({
|
|
|
233
234
|
Meta: "meta",
|
|
234
235
|
OS: "meta"
|
|
235
236
|
});
|
|
236
|
-
var ignoreEditable = true;
|
|
237
|
-
var defaultTarget = typeof window !== "undefined" ? window : null;
|
|
238
|
-
var bindHooks = [];
|
|
239
|
-
var registry = /* @__PURE__ */ new Map();
|
|
240
|
-
var plugins = /* @__PURE__ */ new Map();
|
|
241
|
-
var bindingIndex = /* @__PURE__ */ new Map();
|
|
242
|
-
var paused = false;
|
|
243
|
-
var nextId = 1;
|
|
244
237
|
var warnedShifted = /* @__PURE__ */ new Set();
|
|
245
238
|
function comboKeyFromParts({ ctrl, shift, alt, meta, key, code, bareModifier }) {
|
|
246
239
|
if (bareModifier) return `__bare__:${bareModifier}`;
|
|
@@ -272,146 +265,59 @@ function codeKeyFromEvent(e) {
|
|
|
272
265
|
if (e.metaKey) s += "meta+";
|
|
273
266
|
return s + "code:" + e.code;
|
|
274
267
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
268
|
+
var CONTROL_INPUT_TYPES = /* @__PURE__ */ new Set([
|
|
269
|
+
"checkbox",
|
|
270
|
+
"radio",
|
|
271
|
+
"range",
|
|
272
|
+
"color",
|
|
273
|
+
"file",
|
|
274
|
+
"button",
|
|
275
|
+
"submit",
|
|
276
|
+
"reset",
|
|
277
|
+
"image"
|
|
278
|
+
]);
|
|
279
|
+
function eventPath(e) {
|
|
280
|
+
const path = e?.composedPath?.();
|
|
281
|
+
if (path?.length) return path;
|
|
282
|
+
const out = [];
|
|
283
|
+
for (let n = e?.target; n; n = n.parentNode || n.host) out.push(n);
|
|
284
|
+
return out;
|
|
285
|
+
}
|
|
286
|
+
function focusKind(e, mode) {
|
|
287
|
+
const path = eventPath(e);
|
|
288
|
+
const t = path[0];
|
|
289
|
+
if (!t) return null;
|
|
278
290
|
const tag = String(t.tagName || "").toUpperCase();
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
function normalizeOptions(type, options) {
|
|
282
|
-
const o = options ? { ...options } : {};
|
|
283
|
-
if (type === "keydown" && o.repeat == null) o.repeat = false;
|
|
284
|
-
if (o.priority == null) o.priority = 0;
|
|
285
|
-
return o;
|
|
286
|
-
}
|
|
287
|
-
function ensureSlot(comboStr, type) {
|
|
288
|
-
let typeMap = registry.get(comboStr);
|
|
289
|
-
if (!typeMap) {
|
|
290
|
-
typeMap = /* @__PURE__ */ new Map();
|
|
291
|
-
registry.set(comboStr, typeMap);
|
|
292
|
-
}
|
|
293
|
-
let arr = typeMap.get(type);
|
|
294
|
-
if (!arr) {
|
|
295
|
-
arr = [];
|
|
296
|
-
typeMap.set(type, arr);
|
|
297
|
-
}
|
|
298
|
-
return arr;
|
|
299
|
-
}
|
|
300
|
-
function removeById(id) {
|
|
301
|
-
const idx = bindingIndex.get(id);
|
|
302
|
-
if (!idx) return false;
|
|
303
|
-
const { comboStr, type } = idx;
|
|
304
|
-
const typeMap = registry.get(comboStr);
|
|
305
|
-
const arr = typeMap?.get(type);
|
|
306
|
-
if (!arr?.length) {
|
|
307
|
-
bindingIndex.delete(id);
|
|
308
|
-
return false;
|
|
291
|
+
if (t.isContentEditable || tag === "TEXTAREA" || tag === "SELECT" || t.getAttribute?.("role") === "textbox") {
|
|
292
|
+
return "text";
|
|
309
293
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
bindingIndex.delete(id);
|
|
315
|
-
return true;
|
|
316
|
-
}
|
|
317
|
-
function bind(hotkeyStr, handler, plugin = null, options = {}) {
|
|
318
|
-
const { combo, type, raw } = parseHotkey(hotkeyStr);
|
|
319
|
-
const comboStr = comboKeyFromParts(combo);
|
|
320
|
-
const opts = normalizeOptions(type, options);
|
|
321
|
-
const binding = {
|
|
322
|
-
id: nextId++,
|
|
323
|
-
comboStr,
|
|
324
|
-
type,
|
|
325
|
-
combo,
|
|
326
|
-
handler,
|
|
327
|
-
plugin,
|
|
328
|
-
raw,
|
|
329
|
-
options: opts
|
|
330
|
-
};
|
|
331
|
-
ensureSlot(comboStr, type).push(binding);
|
|
332
|
-
bindingIndex.set(binding.id, { comboStr, type });
|
|
333
|
-
const warnKey = `${comboStr} ${type}`;
|
|
334
|
-
if (!warnedShifted.has(warnKey)) {
|
|
335
|
-
const hint = shiftedPunctuationHint(hotkeyStr);
|
|
336
|
-
if (hint) {
|
|
337
|
-
warnedShifted.add(warnKey);
|
|
338
|
-
console.warn(`[hotkey-router] ${hint.message}`);
|
|
339
|
-
}
|
|
294
|
+
if (tag === "INPUT") {
|
|
295
|
+
if (mode === "all") return "text";
|
|
296
|
+
const type = String(t.getAttribute?.("type") || "text").toLowerCase();
|
|
297
|
+
return CONTROL_INPUT_TYPES.has(type) ? "control" : "text";
|
|
340
298
|
}
|
|
341
|
-
if (
|
|
342
|
-
for (const
|
|
343
|
-
|
|
344
|
-
hook({ combo, raw, options: opts, plugin, id: binding.id });
|
|
345
|
-
} catch (err) {
|
|
346
|
-
console.error("[hotkey-router] onBind hook threw:", err);
|
|
347
|
-
}
|
|
299
|
+
if (mode === "text") {
|
|
300
|
+
for (const el of path) {
|
|
301
|
+
if (String(el.tagName || "").toUpperCase() === "BUTTON" || el.getAttribute?.("role") === "button") return "control";
|
|
348
302
|
}
|
|
349
303
|
}
|
|
350
|
-
|
|
351
|
-
off.id = binding.id;
|
|
352
|
-
off.hotkey = raw;
|
|
353
|
-
return off;
|
|
354
|
-
}
|
|
355
|
-
function onBind(hook) {
|
|
356
|
-
if (typeof hook !== "function") {
|
|
357
|
-
throw new TypeError("hotkeys.onBind(hook) expects a function");
|
|
358
|
-
}
|
|
359
|
-
bindHooks.push(hook);
|
|
360
|
-
return () => {
|
|
361
|
-
bindHooks = bindHooks.filter((h) => h !== hook);
|
|
362
|
-
};
|
|
304
|
+
return null;
|
|
363
305
|
}
|
|
364
|
-
function
|
|
365
|
-
|
|
366
|
-
const comboStr = comboKeyFromParts(combo);
|
|
367
|
-
const typeMap = registry.get(comboStr);
|
|
368
|
-
if (!typeMap) return;
|
|
369
|
-
const arr = typeMap.get(type);
|
|
370
|
-
if (!arr?.length) return;
|
|
371
|
-
if (!handler) {
|
|
372
|
-
for (const b of arr) bindingIndex.delete(b.id);
|
|
373
|
-
typeMap.delete(type);
|
|
374
|
-
} else {
|
|
375
|
-
const keep = [];
|
|
376
|
-
for (const b of arr) {
|
|
377
|
-
if (b.handler === handler) bindingIndex.delete(b.id);
|
|
378
|
-
else keep.push(b);
|
|
379
|
-
}
|
|
380
|
-
if (keep.length) typeMap.set(type, keep);
|
|
381
|
-
else typeMap.delete(type);
|
|
382
|
-
}
|
|
383
|
-
if (!typeMap.size) registry.delete(comboStr);
|
|
306
|
+
function isChordEvent(e) {
|
|
307
|
+
return Boolean((e.ctrlKey || e.altKey || e.metaKey) && !MODIFIER_KEY_TOKEN[e.key]);
|
|
384
308
|
}
|
|
385
|
-
function
|
|
386
|
-
if (
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
const ids = [];
|
|
390
|
-
for (const [hotkeyStr, fnOrTuple] of entries) {
|
|
391
|
-
const [fn, options] = Array.isArray(fnOrTuple) ? fnOrTuple : [fnOrTuple, {}];
|
|
392
|
-
const off = bind(hotkeyStr, fn, name, options);
|
|
393
|
-
ids.push(off.id);
|
|
309
|
+
function inputMode(value) {
|
|
310
|
+
if (value === "all" || value === "text") return value;
|
|
311
|
+
if (typeof value === "string") {
|
|
312
|
+
throw new TypeError(`hotkeys.ignoreInput() expects true, false, 'all' or 'text', got "${value}"`);
|
|
394
313
|
}
|
|
395
|
-
|
|
396
|
-
return () => unregisterPlugin(name);
|
|
314
|
+
return value ? "all" : null;
|
|
397
315
|
}
|
|
398
|
-
function
|
|
399
|
-
const
|
|
400
|
-
if (
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
}
|
|
404
|
-
function pause() {
|
|
405
|
-
paused = true;
|
|
406
|
-
}
|
|
407
|
-
function resume() {
|
|
408
|
-
paused = false;
|
|
409
|
-
}
|
|
410
|
-
function ignoreInput(value = true) {
|
|
411
|
-
ignoreEditable = !!value;
|
|
412
|
-
}
|
|
413
|
-
function setTarget(target) {
|
|
414
|
-
defaultTarget = target;
|
|
316
|
+
function normalizeOptions(type, options) {
|
|
317
|
+
const o = options ? { ...options } : {};
|
|
318
|
+
if (type === "keydown" && o.repeat == null) o.repeat = false;
|
|
319
|
+
if (o.priority == null) o.priority = 0;
|
|
320
|
+
return o;
|
|
415
321
|
}
|
|
416
322
|
function pickWinner(bindings) {
|
|
417
323
|
let best = null;
|
|
@@ -445,88 +351,6 @@ function latinKeyFromEvent(e) {
|
|
|
445
351
|
key: letter[1].toLowerCase()
|
|
446
352
|
});
|
|
447
353
|
}
|
|
448
|
-
function eligibleBindings(e, type, phase) {
|
|
449
|
-
const guards = {
|
|
450
|
-
altGraphChar: isAltGraphCharacter(e),
|
|
451
|
-
composing: isComposingEvent(e),
|
|
452
|
-
fromInput: ignoreEditable && isFromInputTarget(e)
|
|
453
|
-
};
|
|
454
|
-
const candidates = [];
|
|
455
|
-
const seen = /* @__PURE__ */ new Set();
|
|
456
|
-
for (const lookupKey of [comboKeyFromEvent(e), codeKeyFromEvent(e)]) {
|
|
457
|
-
if (!lookupKey || seen.has(lookupKey)) continue;
|
|
458
|
-
seen.add(lookupKey);
|
|
459
|
-
collectEligible(candidates, lookupKey, e, type, phase, guards);
|
|
460
|
-
}
|
|
461
|
-
if (!candidates.length) {
|
|
462
|
-
const latinKey = latinKeyFromEvent(e);
|
|
463
|
-
if (latinKey && !seen.has(latinKey)) collectEligible(candidates, latinKey, e, type, phase, guards);
|
|
464
|
-
}
|
|
465
|
-
return candidates;
|
|
466
|
-
}
|
|
467
|
-
function collectEligible(candidates, lookupKey, e, type, phase, { altGraphChar, composing, fromInput }) {
|
|
468
|
-
const bindings = registry.get(lookupKey)?.get(type);
|
|
469
|
-
if (!bindings?.length) return;
|
|
470
|
-
for (const b of bindings) {
|
|
471
|
-
const o = b.options;
|
|
472
|
-
if (phase === "capture" && !o.capture) continue;
|
|
473
|
-
if (phase === "bubble" && o.capture) continue;
|
|
474
|
-
if (altGraphChar && !o.altGraph) continue;
|
|
475
|
-
if (composing && !o.composing) continue;
|
|
476
|
-
if (fromInput && !o.allowIn?.(e)) continue;
|
|
477
|
-
if (type === "keydown" && o.repeat === false && e.repeat) continue;
|
|
478
|
-
if (o.when && o.when(e) === false) continue;
|
|
479
|
-
candidates.push(b);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
function fire(winner, e) {
|
|
483
|
-
const o = winner.options;
|
|
484
|
-
if (o.preventDefault) e.preventDefault();
|
|
485
|
-
if (o.stopImmediatePropagation) e.stopImmediatePropagation();
|
|
486
|
-
else if (o.stopPropagation) e.stopPropagation();
|
|
487
|
-
try {
|
|
488
|
-
winner.handler(e);
|
|
489
|
-
} finally {
|
|
490
|
-
if (o.once) removeById(winner.id);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
function handleEvent(type, phase = "all") {
|
|
494
|
-
return (e) => {
|
|
495
|
-
if (paused) return;
|
|
496
|
-
const candidates = eligibleBindings(e, type, phase);
|
|
497
|
-
if (candidates.length) fire(pickWinner(candidates), e);
|
|
498
|
-
};
|
|
499
|
-
}
|
|
500
|
-
var stopDown = null;
|
|
501
|
-
var stopUp = null;
|
|
502
|
-
var stopCaptureDown = null;
|
|
503
|
-
var stopCaptureUp = null;
|
|
504
|
-
function init({ target = defaultTarget, capture = false } = {}) {
|
|
505
|
-
if (!target) throw new Error("hotkeys.init() requires a target (e.g. window)");
|
|
506
|
-
if (stopDown || stopUp) destroy();
|
|
507
|
-
if (capture) {
|
|
508
|
-
stopDown = on(target, "keydown", handleEvent("keydown"), { capture: true });
|
|
509
|
-
stopUp = on(target, "keyup", handleEvent("keyup"), { capture: true });
|
|
510
|
-
return;
|
|
511
|
-
}
|
|
512
|
-
stopDown = on(target, "keydown", handleEvent("keydown", "bubble"), { capture: false });
|
|
513
|
-
stopUp = on(target, "keyup", handleEvent("keyup", "bubble"), { capture: false });
|
|
514
|
-
stopCaptureDown = on(target, "keydown", handleEvent("keydown", "capture"), { capture: true });
|
|
515
|
-
stopCaptureUp = on(target, "keyup", handleEvent("keyup", "capture"), { capture: true });
|
|
516
|
-
}
|
|
517
|
-
function destroy() {
|
|
518
|
-
stopDown?.();
|
|
519
|
-
stopUp?.();
|
|
520
|
-
stopCaptureDown?.();
|
|
521
|
-
stopCaptureUp?.();
|
|
522
|
-
stopDown = null;
|
|
523
|
-
stopUp = null;
|
|
524
|
-
stopCaptureDown = null;
|
|
525
|
-
stopCaptureUp = null;
|
|
526
|
-
registry.clear();
|
|
527
|
-
plugins.clear();
|
|
528
|
-
bindingIndex.clear();
|
|
529
|
-
}
|
|
530
354
|
var BARE_MOD_TO_KEY = {
|
|
531
355
|
ctrl: "Control",
|
|
532
356
|
shift: "Shift",
|
|
@@ -583,48 +407,6 @@ function codeForKey(key) {
|
|
|
583
407
|
if (/^f\d{1,2}$/.test(key)) return key.toUpperCase();
|
|
584
408
|
return KEY_TO_CODE[key] ?? null;
|
|
585
409
|
}
|
|
586
|
-
function trigger(hotkeyStr, { type: forcedType, target = null } = {}) {
|
|
587
|
-
const { combo, type } = parseHotkey(hotkeyStr);
|
|
588
|
-
const actualType = forcedType || type;
|
|
589
|
-
if (paused) return false;
|
|
590
|
-
const eventKey = combo.bareModifier ? BARE_MOD_TO_KEY[combo.bareModifier] : combo.key ?? keyForCode(combo.code);
|
|
591
|
-
const eventCode = combo.bareModifier ? null : combo.code ?? codeForKey(combo.key);
|
|
592
|
-
let altKey = !!combo.alt;
|
|
593
|
-
let ctrlKey = !!combo.ctrl;
|
|
594
|
-
let shiftKey = !!combo.shift;
|
|
595
|
-
let metaKey = !!combo.meta;
|
|
596
|
-
if (combo.bareModifier && actualType === "keyup") {
|
|
597
|
-
if (combo.bareModifier === "alt") altKey = false;
|
|
598
|
-
else if (combo.bareModifier === "ctrl") ctrlKey = false;
|
|
599
|
-
else if (combo.bareModifier === "shift") shiftKey = false;
|
|
600
|
-
else if (combo.bareModifier === "meta") metaKey = false;
|
|
601
|
-
}
|
|
602
|
-
const e = {
|
|
603
|
-
type: actualType,
|
|
604
|
-
key: eventKey,
|
|
605
|
-
code: eventCode,
|
|
606
|
-
ctrlKey,
|
|
607
|
-
shiftKey,
|
|
608
|
-
altKey,
|
|
609
|
-
metaKey,
|
|
610
|
-
repeat: false,
|
|
611
|
-
isComposing: false,
|
|
612
|
-
getModifierState() {
|
|
613
|
-
return false;
|
|
614
|
-
},
|
|
615
|
-
preventDefault() {
|
|
616
|
-
},
|
|
617
|
-
stopPropagation() {
|
|
618
|
-
},
|
|
619
|
-
stopImmediatePropagation() {
|
|
620
|
-
},
|
|
621
|
-
target
|
|
622
|
-
};
|
|
623
|
-
const candidates = eligibleBindings(e, actualType, "all");
|
|
624
|
-
if (!candidates.length) return false;
|
|
625
|
-
fire(pickWinner(candidates), e);
|
|
626
|
-
return true;
|
|
627
|
-
}
|
|
628
410
|
function comboFromEvent(e, { physical = true, useMod = false, platform = PLATFORM } = {}) {
|
|
629
411
|
if (!e || MODIFIER_KEY_TOKEN[e.key]) return null;
|
|
630
412
|
const parts = [];
|
|
@@ -642,28 +424,301 @@ function comboFromEvent(e, { physical = true, useMod = false, platform = PLATFOR
|
|
|
642
424
|
}
|
|
643
425
|
return parts.join("+");
|
|
644
426
|
}
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
427
|
+
function createRouter({
|
|
428
|
+
target = typeof window !== "undefined" ? window : null,
|
|
429
|
+
capture = false,
|
|
430
|
+
ignoreInput: ignoreInputOption = true
|
|
431
|
+
} = {}) {
|
|
432
|
+
let focusMode = inputMode(ignoreInputOption);
|
|
433
|
+
let defaultTarget = target;
|
|
434
|
+
const defaultCapture = capture;
|
|
435
|
+
let bindHooks = [];
|
|
436
|
+
const registry = /* @__PURE__ */ new Map();
|
|
437
|
+
const plugins = /* @__PURE__ */ new Map();
|
|
438
|
+
const bindingIndex = /* @__PURE__ */ new Map();
|
|
439
|
+
let paused = false;
|
|
440
|
+
let nextId = 1;
|
|
441
|
+
function ensureSlot(comboStr, type) {
|
|
442
|
+
let typeMap = registry.get(comboStr);
|
|
443
|
+
if (!typeMap) {
|
|
444
|
+
typeMap = /* @__PURE__ */ new Map();
|
|
445
|
+
registry.set(comboStr, typeMap);
|
|
446
|
+
}
|
|
447
|
+
let arr = typeMap.get(type);
|
|
448
|
+
if (!arr) {
|
|
449
|
+
arr = [];
|
|
450
|
+
typeMap.set(type, arr);
|
|
451
|
+
}
|
|
452
|
+
return arr;
|
|
453
|
+
}
|
|
454
|
+
function removeById(id) {
|
|
455
|
+
const idx = bindingIndex.get(id);
|
|
456
|
+
if (!idx) return false;
|
|
457
|
+
const { comboStr, type } = idx;
|
|
458
|
+
const typeMap = registry.get(comboStr);
|
|
459
|
+
const arr = typeMap?.get(type);
|
|
460
|
+
if (!arr?.length) {
|
|
461
|
+
bindingIndex.delete(id);
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
const next = arr.filter((b) => b.id !== id);
|
|
465
|
+
if (next.length) typeMap.set(type, next);
|
|
466
|
+
else typeMap.delete(type);
|
|
467
|
+
if (!typeMap.size) registry.delete(comboStr);
|
|
468
|
+
bindingIndex.delete(id);
|
|
469
|
+
return true;
|
|
470
|
+
}
|
|
471
|
+
function bind(hotkeyStr, handler, plugin = null, options = {}) {
|
|
472
|
+
const { combo, type, raw } = parseHotkey(hotkeyStr);
|
|
473
|
+
const comboStr = comboKeyFromParts(combo);
|
|
474
|
+
const opts = normalizeOptions(type, options);
|
|
475
|
+
const binding = {
|
|
476
|
+
id: nextId++,
|
|
477
|
+
comboStr,
|
|
478
|
+
type,
|
|
479
|
+
combo,
|
|
480
|
+
handler,
|
|
481
|
+
plugin,
|
|
482
|
+
raw,
|
|
483
|
+
options: opts
|
|
484
|
+
};
|
|
485
|
+
ensureSlot(comboStr, type).push(binding);
|
|
486
|
+
bindingIndex.set(binding.id, { comboStr, type });
|
|
487
|
+
const warnKey = `${comboStr} ${type}`;
|
|
488
|
+
if (!warnedShifted.has(warnKey)) {
|
|
489
|
+
const hint = shiftedPunctuationHint(hotkeyStr);
|
|
490
|
+
if (hint) {
|
|
491
|
+
warnedShifted.add(warnKey);
|
|
492
|
+
console.warn(`[hotkey-router] ${hint.message}`);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if (bindHooks.length) {
|
|
496
|
+
for (const hook of bindHooks) {
|
|
497
|
+
try {
|
|
498
|
+
hook({ combo, raw, options: opts, plugin, id: binding.id });
|
|
499
|
+
} catch (err) {
|
|
500
|
+
console.error("[hotkey-router] onBind hook threw:", err);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
const off = () => removeById(binding.id);
|
|
505
|
+
off.id = binding.id;
|
|
506
|
+
off.hotkey = raw;
|
|
507
|
+
return off;
|
|
508
|
+
}
|
|
509
|
+
function onBind(hook) {
|
|
510
|
+
if (typeof hook !== "function") {
|
|
511
|
+
throw new TypeError("hotkeys.onBind(hook) expects a function");
|
|
512
|
+
}
|
|
513
|
+
bindHooks.push(hook);
|
|
514
|
+
return () => {
|
|
515
|
+
bindHooks = bindHooks.filter((h) => h !== hook);
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
function unbind(hotkeyStr, handler) {
|
|
519
|
+
const { combo, type } = parseHotkey(hotkeyStr);
|
|
520
|
+
const comboStr = comboKeyFromParts(combo);
|
|
521
|
+
const typeMap = registry.get(comboStr);
|
|
522
|
+
if (!typeMap) return;
|
|
523
|
+
const arr = typeMap.get(type);
|
|
524
|
+
if (!arr?.length) return;
|
|
525
|
+
if (!handler) {
|
|
526
|
+
for (const b of arr) bindingIndex.delete(b.id);
|
|
527
|
+
typeMap.delete(type);
|
|
528
|
+
} else {
|
|
529
|
+
const keep = [];
|
|
530
|
+
for (const b of arr) {
|
|
531
|
+
if (b.handler === handler) bindingIndex.delete(b.id);
|
|
532
|
+
else keep.push(b);
|
|
533
|
+
}
|
|
534
|
+
if (keep.length) typeMap.set(type, keep);
|
|
535
|
+
else typeMap.delete(type);
|
|
536
|
+
}
|
|
537
|
+
if (!typeMap.size) registry.delete(comboStr);
|
|
538
|
+
}
|
|
539
|
+
function registerPlugin(name, hotkeyMap) {
|
|
540
|
+
if (plugins.has(name)) throw new Error(`Plugin "${name}" already registered`);
|
|
541
|
+
const entries = Object.entries(hotkeyMap);
|
|
542
|
+
for (const [hotkeyStr] of entries) parseHotkey(hotkeyStr);
|
|
543
|
+
const ids = [];
|
|
544
|
+
for (const [hotkeyStr, fnOrTuple] of entries) {
|
|
545
|
+
const [fn, options] = Array.isArray(fnOrTuple) ? fnOrTuple : [fnOrTuple, {}];
|
|
546
|
+
const off = bind(hotkeyStr, fn, name, options);
|
|
547
|
+
ids.push(off.id);
|
|
548
|
+
}
|
|
549
|
+
plugins.set(name, ids);
|
|
550
|
+
return () => unregisterPlugin(name);
|
|
551
|
+
}
|
|
552
|
+
function unregisterPlugin(name) {
|
|
553
|
+
const ids = plugins.get(name);
|
|
554
|
+
if (!ids) return;
|
|
555
|
+
for (const id of ids) removeById(id);
|
|
556
|
+
plugins.delete(name);
|
|
557
|
+
}
|
|
558
|
+
function pause() {
|
|
559
|
+
paused = true;
|
|
560
|
+
}
|
|
561
|
+
function resume() {
|
|
562
|
+
paused = false;
|
|
563
|
+
}
|
|
564
|
+
function ignoreInput(value = true) {
|
|
565
|
+
focusMode = inputMode(value);
|
|
566
|
+
}
|
|
567
|
+
function setTarget(target2) {
|
|
568
|
+
defaultTarget = target2;
|
|
569
|
+
}
|
|
570
|
+
function eligibleBindings(e, type, phase) {
|
|
571
|
+
const guards = {
|
|
572
|
+
altGraphChar: isAltGraphCharacter(e),
|
|
573
|
+
composing: isComposingEvent(e),
|
|
574
|
+
focus: focusMode && focusKind(e, focusMode),
|
|
575
|
+
chord: isChordEvent(e)
|
|
576
|
+
};
|
|
577
|
+
const candidates = [];
|
|
578
|
+
const seen = /* @__PURE__ */ new Set();
|
|
579
|
+
for (const lookupKey of [comboKeyFromEvent(e), codeKeyFromEvent(e)]) {
|
|
580
|
+
if (!lookupKey || seen.has(lookupKey)) continue;
|
|
581
|
+
seen.add(lookupKey);
|
|
582
|
+
collectEligible(candidates, lookupKey, e, type, phase, guards);
|
|
583
|
+
}
|
|
584
|
+
if (!candidates.length) {
|
|
585
|
+
const latinKey = latinKeyFromEvent(e);
|
|
586
|
+
if (latinKey && !seen.has(latinKey)) collectEligible(candidates, latinKey, e, type, phase, guards);
|
|
587
|
+
}
|
|
588
|
+
return candidates;
|
|
589
|
+
}
|
|
590
|
+
function collectEligible(candidates, lookupKey, e, type, phase, { altGraphChar, composing, focus, chord }) {
|
|
591
|
+
const bindings = registry.get(lookupKey)?.get(type);
|
|
592
|
+
if (!bindings?.length) return;
|
|
593
|
+
for (const b of bindings) {
|
|
594
|
+
const o = b.options;
|
|
595
|
+
if (phase === "capture" && !o.capture) continue;
|
|
596
|
+
if (phase === "bubble" && o.capture) continue;
|
|
597
|
+
if (altGraphChar && !o.altGraph) continue;
|
|
598
|
+
if (composing && !o.composing) continue;
|
|
599
|
+
if (focus && !(focus === "control" && chord) && !o.allowIn?.(e)) continue;
|
|
600
|
+
if (type === "keydown" && o.repeat === false && e.repeat) continue;
|
|
601
|
+
if (o.when && o.when(e) === false) continue;
|
|
602
|
+
candidates.push(b);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
function fire(winner, e) {
|
|
606
|
+
const o = winner.options;
|
|
607
|
+
if (o.preventDefault) e.preventDefault();
|
|
608
|
+
if (o.stopImmediatePropagation) e.stopImmediatePropagation();
|
|
609
|
+
else if (o.stopPropagation) e.stopPropagation();
|
|
610
|
+
try {
|
|
611
|
+
winner.handler(e);
|
|
612
|
+
} finally {
|
|
613
|
+
if (o.once) removeById(winner.id);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
function handleEvent(type, phase = "all") {
|
|
617
|
+
return (e) => {
|
|
618
|
+
if (paused) return;
|
|
619
|
+
const candidates = eligibleBindings(e, type, phase);
|
|
620
|
+
if (candidates.length) fire(pickWinner(candidates), e);
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
let stopDown = null;
|
|
624
|
+
let stopUp = null;
|
|
625
|
+
let stopCaptureDown = null;
|
|
626
|
+
let stopCaptureUp = null;
|
|
627
|
+
function init({ target: target2 = defaultTarget, capture: capture2 = defaultCapture } = {}) {
|
|
628
|
+
if (!target2) throw new Error("hotkeys.init() requires a target (e.g. window)");
|
|
629
|
+
if (stopDown || stopUp) destroy();
|
|
630
|
+
if (capture2) {
|
|
631
|
+
stopDown = on(target2, "keydown", handleEvent("keydown"), { capture: true });
|
|
632
|
+
stopUp = on(target2, "keyup", handleEvent("keyup"), { capture: true });
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
stopDown = on(target2, "keydown", handleEvent("keydown", "bubble"), { capture: false });
|
|
636
|
+
stopUp = on(target2, "keyup", handleEvent("keyup", "bubble"), { capture: false });
|
|
637
|
+
stopCaptureDown = on(target2, "keydown", handleEvent("keydown", "capture"), { capture: true });
|
|
638
|
+
stopCaptureUp = on(target2, "keyup", handleEvent("keyup", "capture"), { capture: true });
|
|
639
|
+
}
|
|
640
|
+
function destroy() {
|
|
641
|
+
stopDown?.();
|
|
642
|
+
stopUp?.();
|
|
643
|
+
stopCaptureDown?.();
|
|
644
|
+
stopCaptureUp?.();
|
|
645
|
+
stopDown = null;
|
|
646
|
+
stopUp = null;
|
|
647
|
+
stopCaptureDown = null;
|
|
648
|
+
stopCaptureUp = null;
|
|
649
|
+
registry.clear();
|
|
650
|
+
plugins.clear();
|
|
651
|
+
bindingIndex.clear();
|
|
652
|
+
}
|
|
653
|
+
function trigger(hotkeyStr, { type: forcedType, target: target2 = null } = {}) {
|
|
654
|
+
const { combo, type } = parseHotkey(hotkeyStr);
|
|
655
|
+
const actualType = forcedType || type;
|
|
656
|
+
if (paused) return false;
|
|
657
|
+
const eventKey = combo.bareModifier ? BARE_MOD_TO_KEY[combo.bareModifier] : combo.key ?? keyForCode(combo.code);
|
|
658
|
+
const eventCode = combo.bareModifier ? null : combo.code ?? codeForKey(combo.key);
|
|
659
|
+
let altKey = !!combo.alt;
|
|
660
|
+
let ctrlKey = !!combo.ctrl;
|
|
661
|
+
let shiftKey = !!combo.shift;
|
|
662
|
+
let metaKey = !!combo.meta;
|
|
663
|
+
if (combo.bareModifier && actualType === "keyup") {
|
|
664
|
+
if (combo.bareModifier === "alt") altKey = false;
|
|
665
|
+
else if (combo.bareModifier === "ctrl") ctrlKey = false;
|
|
666
|
+
else if (combo.bareModifier === "shift") shiftKey = false;
|
|
667
|
+
else if (combo.bareModifier === "meta") metaKey = false;
|
|
668
|
+
}
|
|
669
|
+
const e = {
|
|
670
|
+
type: actualType,
|
|
671
|
+
key: eventKey,
|
|
672
|
+
code: eventCode,
|
|
673
|
+
ctrlKey,
|
|
674
|
+
shiftKey,
|
|
675
|
+
altKey,
|
|
676
|
+
metaKey,
|
|
677
|
+
repeat: false,
|
|
678
|
+
isComposing: false,
|
|
679
|
+
getModifierState() {
|
|
680
|
+
return false;
|
|
681
|
+
},
|
|
682
|
+
preventDefault() {
|
|
683
|
+
},
|
|
684
|
+
stopPropagation() {
|
|
685
|
+
},
|
|
686
|
+
stopImmediatePropagation() {
|
|
687
|
+
},
|
|
688
|
+
target: target2
|
|
689
|
+
};
|
|
690
|
+
const candidates = eligibleBindings(e, actualType, "all");
|
|
691
|
+
if (!candidates.length) return false;
|
|
692
|
+
fire(pickWinner(candidates), e);
|
|
693
|
+
return true;
|
|
694
|
+
}
|
|
695
|
+
return {
|
|
696
|
+
// lifecycle
|
|
697
|
+
init,
|
|
698
|
+
destroy,
|
|
699
|
+
setTarget,
|
|
700
|
+
// bindings
|
|
701
|
+
bind,
|
|
702
|
+
unbind,
|
|
703
|
+
registerPlugin,
|
|
704
|
+
unregisterPlugin,
|
|
705
|
+
// extension hooks
|
|
706
|
+
onBind,
|
|
707
|
+
// control
|
|
708
|
+
pause,
|
|
709
|
+
resume,
|
|
710
|
+
ignoreInput,
|
|
711
|
+
// testing / automation
|
|
712
|
+
trigger,
|
|
713
|
+
// helpers
|
|
714
|
+
parseHotkey,
|
|
715
|
+
comboFromEvent,
|
|
716
|
+
detectPlatform
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
// hotkey-router.js
|
|
721
|
+
var hotkeys = createRouter();
|
|
722
|
+
if (typeof window !== "undefined") hotkeys.init();
|
|
723
|
+
var hotkey_router_default = hotkeys;
|
|
669
724
|
//# sourceMappingURL=hotkey-router.cjs.map
|