hotkey-router 0.3.2 → 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 +157 -6
- package/dist/auto.js +5 -1101
- package/dist/auto.js.map +4 -4
- 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/format.js.map +1 -1
- package/dist/hotkey-router.cjs +385 -266
- package/dist/hotkey-router.cjs.map +4 -4
- package/dist/hotkey-router.js +6 -574
- package/dist/hotkey-router.js.map +3 -3
- package/dist/hotkey-router.min.js +2 -2
- package/dist/reservations.js +1 -1
- package/dist/reservations.js.map +2 -2
- package/dist/sites.js +1 -1
- package/dist/sites.js.map +2 -2
- package/dist/types/core.d.ts +113 -0
- package/dist/types/hotkey-router.d.ts +20 -195
- package/dist/types/parse.d.ts +13 -0
- package/dist/types/validate.d.ts +2 -2
- package/dist/validate.js +193 -1
- package/dist/validate.js.map +3 -3
- package/package.json +17 -3
package/dist/hotkey-router.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// hotkey-router v0.
|
|
1
|
+
// hotkey-router v0.4.0 | 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
|
|
@@ -192,8 +193,34 @@ function parseHotkey(hotkeyStr, opts) {
|
|
|
192
193
|
if (error) throw new Error(error);
|
|
193
194
|
return { combo, type, raw };
|
|
194
195
|
}
|
|
196
|
+
var SHIFTED_PUNCT = {
|
|
197
|
+
",": ["<", "Comma"],
|
|
198
|
+
".": [">", "Period"],
|
|
199
|
+
"/": ["?", "Slash"],
|
|
200
|
+
";": [":", "Semicolon"],
|
|
201
|
+
"'": ['"', "Quote"],
|
|
202
|
+
"[": ["{", "BracketLeft"],
|
|
203
|
+
"]": ["}", "BracketRight"],
|
|
204
|
+
"\\": ["|", "Backslash"],
|
|
205
|
+
"-": ["_", "Minus"],
|
|
206
|
+
"=": ["+", "Equal"],
|
|
207
|
+
"`": ["~", "Backquote"]
|
|
208
|
+
};
|
|
209
|
+
function shiftedPunctuationHint(hotkeyStr) {
|
|
210
|
+
const { combo, type, error } = readHotkey(hotkeyStr);
|
|
211
|
+
if (error || !combo.shift || combo.code) return null;
|
|
212
|
+
const shifted = SHIFTED_PUNCT[combo.key];
|
|
213
|
+
if (!shifted) return null;
|
|
214
|
+
const [typed, code] = shifted;
|
|
215
|
+
const base = String(hotkeyStr).trim().replace(/\s+up$/i, "");
|
|
216
|
+
const suggestion = base.slice(0, -1) + "code:" + code + (type === "keyup" ? " up" : "");
|
|
217
|
+
return {
|
|
218
|
+
message: `"${hotkeyStr}" never fires on US layouts: Shift turns "${combo.key}" into "${typed}". Bind "${suggestion}" instead.`,
|
|
219
|
+
suggestion
|
|
220
|
+
};
|
|
221
|
+
}
|
|
195
222
|
|
|
196
|
-
//
|
|
223
|
+
// core.js
|
|
197
224
|
function on(target, type, fn, options) {
|
|
198
225
|
target.addEventListener(type, fn, options);
|
|
199
226
|
return () => target.removeEventListener(type, fn, options);
|
|
@@ -207,14 +234,7 @@ var MODIFIER_KEY_TOKEN = Object.freeze({
|
|
|
207
234
|
Meta: "meta",
|
|
208
235
|
OS: "meta"
|
|
209
236
|
});
|
|
210
|
-
var
|
|
211
|
-
var defaultTarget = typeof window !== "undefined" ? window : null;
|
|
212
|
-
var bindHooks = [];
|
|
213
|
-
var registry = /* @__PURE__ */ new Map();
|
|
214
|
-
var plugins = /* @__PURE__ */ new Map();
|
|
215
|
-
var bindingIndex = /* @__PURE__ */ new Map();
|
|
216
|
-
var paused = false;
|
|
217
|
-
var nextId = 1;
|
|
237
|
+
var warnedShifted = /* @__PURE__ */ new Set();
|
|
218
238
|
function comboKeyFromParts({ ctrl, shift, alt, meta, key, code, bareModifier }) {
|
|
219
239
|
if (bareModifier) return `__bare__:${bareModifier}`;
|
|
220
240
|
let s = "";
|
|
@@ -245,136 +265,59 @@ function codeKeyFromEvent(e) {
|
|
|
245
265
|
if (e.metaKey) s += "meta+";
|
|
246
266
|
return s + "code:" + e.code;
|
|
247
267
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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;
|
|
251
290
|
const tag = String(t.tagName || "").toUpperCase();
|
|
252
|
-
|
|
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);
|
|
291
|
+
if (t.isContentEditable || tag === "TEXTAREA" || tag === "SELECT" || t.getAttribute?.("role") === "textbox") {
|
|
292
|
+
return "text";
|
|
265
293
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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;
|
|
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";
|
|
282
298
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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
|
-
if (bindHooks.length) {
|
|
307
|
-
for (const hook of bindHooks) {
|
|
308
|
-
try {
|
|
309
|
-
hook({ combo, raw, options: opts, plugin, id: binding.id });
|
|
310
|
-
} catch (err) {
|
|
311
|
-
console.error("[hotkey-router] onBind hook threw:", err);
|
|
312
|
-
}
|
|
299
|
+
if (mode === "text") {
|
|
300
|
+
for (const el of path) {
|
|
301
|
+
if (String(el.tagName || "").toUpperCase() === "BUTTON" || el.getAttribute?.("role") === "button") return "control";
|
|
313
302
|
}
|
|
314
303
|
}
|
|
315
|
-
|
|
316
|
-
off.id = binding.id;
|
|
317
|
-
off.hotkey = raw;
|
|
318
|
-
return off;
|
|
319
|
-
}
|
|
320
|
-
function onBind(hook) {
|
|
321
|
-
if (typeof hook !== "function") {
|
|
322
|
-
throw new TypeError("hotkeys.onBind(hook) expects a function");
|
|
323
|
-
}
|
|
324
|
-
bindHooks.push(hook);
|
|
325
|
-
return () => {
|
|
326
|
-
bindHooks = bindHooks.filter((h) => h !== hook);
|
|
327
|
-
};
|
|
304
|
+
return null;
|
|
328
305
|
}
|
|
329
|
-
function
|
|
330
|
-
|
|
331
|
-
const comboStr = comboKeyFromParts(combo);
|
|
332
|
-
const typeMap = registry.get(comboStr);
|
|
333
|
-
if (!typeMap) return;
|
|
334
|
-
const arr = typeMap.get(type);
|
|
335
|
-
if (!arr?.length) return;
|
|
336
|
-
if (!handler) {
|
|
337
|
-
for (const b of arr) bindingIndex.delete(b.id);
|
|
338
|
-
typeMap.delete(type);
|
|
339
|
-
} else {
|
|
340
|
-
const keep = [];
|
|
341
|
-
for (const b of arr) {
|
|
342
|
-
if (b.handler === handler) bindingIndex.delete(b.id);
|
|
343
|
-
else keep.push(b);
|
|
344
|
-
}
|
|
345
|
-
if (keep.length) typeMap.set(type, keep);
|
|
346
|
-
else typeMap.delete(type);
|
|
347
|
-
}
|
|
348
|
-
if (!typeMap.size) registry.delete(comboStr);
|
|
306
|
+
function isChordEvent(e) {
|
|
307
|
+
return Boolean((e.ctrlKey || e.altKey || e.metaKey) && !MODIFIER_KEY_TOKEN[e.key]);
|
|
349
308
|
}
|
|
350
|
-
function
|
|
351
|
-
if (
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
const [fn, options] = Array.isArray(fnOrTuple) ? fnOrTuple : [fnOrTuple, {}];
|
|
355
|
-
const off = bind(hotkeyStr, fn, name, options);
|
|
356
|
-
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}"`);
|
|
357
313
|
}
|
|
358
|
-
|
|
359
|
-
return () => unregisterPlugin(name);
|
|
360
|
-
}
|
|
361
|
-
function unregisterPlugin(name) {
|
|
362
|
-
const ids = plugins.get(name);
|
|
363
|
-
if (!ids) return;
|
|
364
|
-
for (const id of ids) removeById(id);
|
|
365
|
-
plugins.delete(name);
|
|
366
|
-
}
|
|
367
|
-
function pause() {
|
|
368
|
-
paused = true;
|
|
369
|
-
}
|
|
370
|
-
function resume() {
|
|
371
|
-
paused = false;
|
|
372
|
-
}
|
|
373
|
-
function ignoreInput(value = true) {
|
|
374
|
-
ignoreEditable = !!value;
|
|
314
|
+
return value ? "all" : null;
|
|
375
315
|
}
|
|
376
|
-
function
|
|
377
|
-
|
|
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;
|
|
378
321
|
}
|
|
379
322
|
function pickWinner(bindings) {
|
|
380
323
|
let best = null;
|
|
@@ -393,75 +336,20 @@ function isAltGraphCharacter(e) {
|
|
|
393
336
|
function isComposingEvent(e) {
|
|
394
337
|
return Boolean(e.isComposing || e.keyCode === 229);
|
|
395
338
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
const
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
if (phase === "bubble" && o.capture) continue;
|
|
411
|
-
if (altGraphChar && !o.altGraph) continue;
|
|
412
|
-
if (composing && !o.composing) continue;
|
|
413
|
-
if (fromInput && !o.allowIn?.(e)) continue;
|
|
414
|
-
if (type === "keydown" && o.repeat === false && e.repeat) continue;
|
|
415
|
-
if (o.when && o.when(e) === false) continue;
|
|
416
|
-
candidates.push(b);
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
return candidates;
|
|
420
|
-
}
|
|
421
|
-
function fire(winner, e) {
|
|
422
|
-
const o = winner.options;
|
|
423
|
-
if (o.preventDefault) e.preventDefault();
|
|
424
|
-
if (o.stopImmediatePropagation) e.stopImmediatePropagation();
|
|
425
|
-
else if (o.stopPropagation) e.stopPropagation();
|
|
426
|
-
winner.handler(e);
|
|
427
|
-
if (o.once) removeById(winner.id);
|
|
428
|
-
}
|
|
429
|
-
function handleEvent(type, phase = "all") {
|
|
430
|
-
return (e) => {
|
|
431
|
-
if (paused) return;
|
|
432
|
-
const candidates = eligibleBindings(e, type, phase);
|
|
433
|
-
if (candidates.length) fire(pickWinner(candidates), e);
|
|
434
|
-
};
|
|
435
|
-
}
|
|
436
|
-
var stopDown = null;
|
|
437
|
-
var stopUp = null;
|
|
438
|
-
var stopCaptureDown = null;
|
|
439
|
-
var stopCaptureUp = null;
|
|
440
|
-
function init({ target = defaultTarget, capture = false } = {}) {
|
|
441
|
-
if (!target) throw new Error("hotkeys.init() requires a target (e.g. window)");
|
|
442
|
-
if (stopDown || stopUp) destroy();
|
|
443
|
-
if (capture) {
|
|
444
|
-
stopDown = on(target, "keydown", handleEvent("keydown"), { capture: true });
|
|
445
|
-
stopUp = on(target, "keyup", handleEvent("keyup"), { capture: true });
|
|
446
|
-
return;
|
|
447
|
-
}
|
|
448
|
-
stopDown = on(target, "keydown", handleEvent("keydown", "bubble"), { capture: false });
|
|
449
|
-
stopUp = on(target, "keyup", handleEvent("keyup", "bubble"), { capture: false });
|
|
450
|
-
stopCaptureDown = on(target, "keydown", handleEvent("keydown", "capture"), { capture: true });
|
|
451
|
-
stopCaptureUp = on(target, "keyup", handleEvent("keyup", "capture"), { capture: true });
|
|
452
|
-
}
|
|
453
|
-
function destroy() {
|
|
454
|
-
stopDown?.();
|
|
455
|
-
stopUp?.();
|
|
456
|
-
stopCaptureDown?.();
|
|
457
|
-
stopCaptureUp?.();
|
|
458
|
-
stopDown = null;
|
|
459
|
-
stopUp = null;
|
|
460
|
-
stopCaptureDown = null;
|
|
461
|
-
stopCaptureUp = null;
|
|
462
|
-
registry.clear();
|
|
463
|
-
plugins.clear();
|
|
464
|
-
bindingIndex.clear();
|
|
339
|
+
var NON_LATIN_LETTER = /^(?!\p{Script=Latin})\p{L}$/u;
|
|
340
|
+
function latinKeyFromEvent(e) {
|
|
341
|
+
if (!(e.ctrlKey || e.metaKey) || e.altKey) return null;
|
|
342
|
+
if (isComposingEvent(e)) return null;
|
|
343
|
+
if (typeof e.getModifierState === "function" && e.getModifierState("AltGraph")) return null;
|
|
344
|
+
const letter = /^Key([A-Z])$/.exec(e.code || "");
|
|
345
|
+
if (!letter || typeof e.key !== "string" || !NON_LATIN_LETTER.test(e.key)) return null;
|
|
346
|
+
return comboKeyFromParts({
|
|
347
|
+
ctrl: e.ctrlKey,
|
|
348
|
+
shift: e.shiftKey,
|
|
349
|
+
alt: false,
|
|
350
|
+
meta: e.metaKey,
|
|
351
|
+
key: letter[1].toLowerCase()
|
|
352
|
+
});
|
|
465
353
|
}
|
|
466
354
|
var BARE_MOD_TO_KEY = {
|
|
467
355
|
ctrl: "Control",
|
|
@@ -519,48 +407,6 @@ function codeForKey(key) {
|
|
|
519
407
|
if (/^f\d{1,2}$/.test(key)) return key.toUpperCase();
|
|
520
408
|
return KEY_TO_CODE[key] ?? null;
|
|
521
409
|
}
|
|
522
|
-
function trigger(hotkeyStr, { type: forcedType, target = null } = {}) {
|
|
523
|
-
const { combo, type } = parseHotkey(hotkeyStr);
|
|
524
|
-
const actualType = forcedType || type;
|
|
525
|
-
if (paused) return false;
|
|
526
|
-
const eventKey = combo.bareModifier ? BARE_MOD_TO_KEY[combo.bareModifier] : combo.key ?? keyForCode(combo.code);
|
|
527
|
-
const eventCode = combo.bareModifier ? null : combo.code ?? codeForKey(combo.key);
|
|
528
|
-
let altKey = !!combo.alt;
|
|
529
|
-
let ctrlKey = !!combo.ctrl;
|
|
530
|
-
let shiftKey = !!combo.shift;
|
|
531
|
-
let metaKey = !!combo.meta;
|
|
532
|
-
if (combo.bareModifier && actualType === "keyup") {
|
|
533
|
-
if (combo.bareModifier === "alt") altKey = false;
|
|
534
|
-
else if (combo.bareModifier === "ctrl") ctrlKey = false;
|
|
535
|
-
else if (combo.bareModifier === "shift") shiftKey = false;
|
|
536
|
-
else if (combo.bareModifier === "meta") metaKey = false;
|
|
537
|
-
}
|
|
538
|
-
const e = {
|
|
539
|
-
type: actualType,
|
|
540
|
-
key: eventKey,
|
|
541
|
-
code: eventCode,
|
|
542
|
-
ctrlKey,
|
|
543
|
-
shiftKey,
|
|
544
|
-
altKey,
|
|
545
|
-
metaKey,
|
|
546
|
-
repeat: false,
|
|
547
|
-
isComposing: false,
|
|
548
|
-
getModifierState() {
|
|
549
|
-
return false;
|
|
550
|
-
},
|
|
551
|
-
preventDefault() {
|
|
552
|
-
},
|
|
553
|
-
stopPropagation() {
|
|
554
|
-
},
|
|
555
|
-
stopImmediatePropagation() {
|
|
556
|
-
},
|
|
557
|
-
target
|
|
558
|
-
};
|
|
559
|
-
const candidates = eligibleBindings(e, actualType, "all");
|
|
560
|
-
if (!candidates.length) return false;
|
|
561
|
-
fire(pickWinner(candidates), e);
|
|
562
|
-
return true;
|
|
563
|
-
}
|
|
564
410
|
function comboFromEvent(e, { physical = true, useMod = false, platform = PLATFORM } = {}) {
|
|
565
411
|
if (!e || MODIFIER_KEY_TOKEN[e.key]) return null;
|
|
566
412
|
const parts = [];
|
|
@@ -578,28 +424,301 @@ function comboFromEvent(e, { physical = true, useMod = false, platform = PLATFOR
|
|
|
578
424
|
}
|
|
579
425
|
return parts.join("+");
|
|
580
426
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
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;
|
|
605
724
|
//# sourceMappingURL=hotkey-router.cjs.map
|