hotkey-router 0.5.2 → 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/README.md +32 -27
- package/dist/auto.js +3 -3
- package/dist/auto.js.map +2 -2
- package/dist/auto.min.js +2 -2
- package/dist/core.js +17 -321
- package/dist/core.js.map +3 -3
- package/dist/dev.js +56 -0
- package/dist/dev.js.map +7 -0
- package/dist/format.js +4 -194
- package/dist/format.js.map +3 -3
- package/dist/hotkey-router.cjs +14 -145
- package/dist/hotkey-router.cjs.map +2 -2
- package/dist/hotkey-router.js +1 -1
- package/dist/hotkey-router.js.map +2 -2
- package/dist/hotkey-router.min.js +2 -2
- package/dist/lookup.js +9 -0
- package/dist/lookup.js.map +7 -0
- package/dist/parse.js +207 -0
- package/dist/parse.js.map +7 -0
- package/dist/platform.js +42 -0
- package/dist/platform.js.map +7 -0
- package/dist/reservations.js +6 -666
- package/dist/reservations.js.map +3 -3
- package/dist/shifted-punctuation.js +34 -0
- package/dist/shifted-punctuation.js.map +7 -0
- package/dist/sites.js +3 -227
- package/dist/sites.js.map +3 -3
- package/dist/testing.js +104 -0
- package/dist/testing.js.map +7 -0
- package/dist/types/core.d.ts +3 -9
- package/dist/types/dev.d.ts +48 -0
- package/dist/types/parse.d.ts +0 -13
- package/dist/types/reservations.d.ts +4 -44
- package/dist/types/shifted-punctuation.d.ts +17 -0
- package/dist/types/testing.d.ts +17 -0
- package/dist/validate.js +7 -203
- package/dist/validate.js.map +3 -3
- package/package.json +9 -1
package/dist/core.js
CHANGED
|
@@ -1,210 +1,10 @@
|
|
|
1
|
-
// hotkey-router v0.
|
|
1
|
+
// hotkey-router v0.6.0 | SEE LICENSE IN LICENSE
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
// lookup.js
|
|
5
|
-
function ownValue(table, key) {
|
|
6
|
-
return table != null && Object.prototype.hasOwnProperty.call(table, key) ? table[key] : void 0;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
// platform.js
|
|
10
|
-
function fromPlatformString(s) {
|
|
11
|
-
if (!s) return null;
|
|
12
|
-
if (/Chrome ?OS|CrOS/i.test(s)) return "chromeos";
|
|
13
|
-
if (/Mac|iPhone|iPad|iPod/i.test(s)) return "mac";
|
|
14
|
-
if (/Win/i.test(s)) return "windows";
|
|
15
|
-
if (/Linux|X11/i.test(s)) return "linux";
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
function detectPlatform(nav = typeof navigator !== "undefined" ? navigator : null, { chromeos = false } = {}) {
|
|
19
|
-
if (!nav) return null;
|
|
20
|
-
const ua = nav.userAgent || "";
|
|
21
|
-
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);
|
|
22
|
-
if (found === "chromeos" && !chromeos) return "linux";
|
|
23
|
-
return found;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// parse.js
|
|
27
|
-
var DEFAULT_PLATFORM = detectPlatform();
|
|
28
|
-
var modifierTokens = /* @__PURE__ */ new Set(["ctrl", "shift", "alt", "meta", "mod"]);
|
|
29
|
-
var ahkPrefixMap = /* @__PURE__ */ new Map([
|
|
30
|
-
["^", "ctrl"],
|
|
31
|
-
// Ctrl
|
|
32
|
-
["!", "alt"],
|
|
33
|
-
// Alt
|
|
34
|
-
["+", "shift"],
|
|
35
|
-
// Shift
|
|
36
|
-
["#", "meta"]
|
|
37
|
-
// Win / Command
|
|
38
|
-
]);
|
|
39
|
-
var keyAliases = Object.freeze({
|
|
40
|
-
// common
|
|
41
|
-
esc: "escape",
|
|
42
|
-
escape: "escape",
|
|
43
|
-
space: " ",
|
|
44
|
-
spacebar: " ",
|
|
45
|
-
enter: "enter",
|
|
46
|
-
return: "enter",
|
|
47
|
-
del: "delete",
|
|
48
|
-
delete: "delete",
|
|
49
|
-
backspace: "backspace",
|
|
50
|
-
tab: "tab",
|
|
51
|
-
// arrows
|
|
52
|
-
up: "arrowup",
|
|
53
|
-
down: "arrowdown",
|
|
54
|
-
left: "arrowleft",
|
|
55
|
-
right: "arrowright",
|
|
56
|
-
// navigation
|
|
57
|
-
home: "home",
|
|
58
|
-
end: "end",
|
|
59
|
-
pageup: "pageup",
|
|
60
|
-
pagedown: "pagedown",
|
|
61
|
-
pgup: "pageup",
|
|
62
|
-
pgdn: "pagedown",
|
|
63
|
-
// modifiers (words)
|
|
64
|
-
cmd: "meta",
|
|
65
|
-
command: "meta",
|
|
66
|
-
win: "meta",
|
|
67
|
-
meta: "meta",
|
|
68
|
-
control: "ctrl",
|
|
69
|
-
ctrl: "ctrl",
|
|
70
|
-
option: "alt",
|
|
71
|
-
opt: "alt",
|
|
72
|
-
alt: "alt",
|
|
73
|
-
shift: "shift",
|
|
74
|
-
// modifiers (symbols)
|
|
75
|
-
"\u2318": "meta",
|
|
76
|
-
"\u2303": "ctrl",
|
|
77
|
-
"\u2325": "alt",
|
|
78
|
-
"\u21E7": "shift",
|
|
79
|
-
// cross-platform "mod", resolved per platform while parsing
|
|
80
|
-
mod: "mod",
|
|
81
|
-
// plus key ergonomics
|
|
82
|
-
plus: "+"
|
|
83
|
-
});
|
|
84
|
-
function normalizeKey(key) {
|
|
85
|
-
const original = String(key ?? "");
|
|
86
|
-
if (original === " ") return " ";
|
|
87
|
-
const trimmed = original.trim();
|
|
88
|
-
const k = trimmed.toLowerCase();
|
|
89
|
-
return ownValue(keyAliases, k) ?? k;
|
|
90
|
-
}
|
|
91
|
-
function isModifierToken(token) {
|
|
92
|
-
return modifierTokens.has(normalizeKey(token));
|
|
93
|
-
}
|
|
94
|
-
var CODE_TOKEN_RE = /(^|\+)\s*code:([A-Za-z][A-Za-z0-9_]*)/;
|
|
95
|
-
function readHotkey(hotkeyStr, { platform = DEFAULT_PLATFORM } = {}) {
|
|
96
|
-
const raw = String(hotkeyStr || "").trim();
|
|
97
|
-
let error = null;
|
|
98
|
-
const fail = (msg) => {
|
|
99
|
-
error = error || `Invalid hotkey "${hotkeyStr}": ${msg}`;
|
|
100
|
-
};
|
|
101
|
-
const isUp = /\s+up$/i.test(raw);
|
|
102
|
-
const baseRaw = isUp ? raw.replace(/\s+up$/i, "").trim() : raw;
|
|
103
|
-
const combo = {
|
|
104
|
-
ctrl: false,
|
|
105
|
-
shift: false,
|
|
106
|
-
alt: false,
|
|
107
|
-
meta: false,
|
|
108
|
-
key: null,
|
|
109
|
-
code: null,
|
|
110
|
-
bareModifier: null
|
|
111
|
-
};
|
|
112
|
-
const setMod = (mod) => {
|
|
113
|
-
if (mod === "mod") combo[platform === "mac" ? "meta" : "ctrl"] = true;
|
|
114
|
-
else combo[mod] = true;
|
|
115
|
-
};
|
|
116
|
-
let stripped = baseRaw;
|
|
117
|
-
while (stripped.length > 1) {
|
|
118
|
-
const mod = ahkPrefixMap.get(stripped[0]) ?? null;
|
|
119
|
-
if (!mod) break;
|
|
120
|
-
setMod(mod);
|
|
121
|
-
stripped = stripped.slice(1).trimStart();
|
|
122
|
-
}
|
|
123
|
-
const codeMatches = stripped.match(/code:[A-Za-z][A-Za-z0-9_]*/g);
|
|
124
|
-
if (codeMatches && codeMatches.length > 1) {
|
|
125
|
-
fail(`multiple code: tokens not supported (got ${codeMatches.length}).`);
|
|
126
|
-
}
|
|
127
|
-
let codeValue = null;
|
|
128
|
-
const scrubbed = stripped.replace(CODE_TOKEN_RE, (_, sep, val) => {
|
|
129
|
-
codeValue = val;
|
|
130
|
-
return sep + "__code__";
|
|
131
|
-
});
|
|
132
|
-
const cleanStr = scrubbed.toLowerCase();
|
|
133
|
-
const rawParts = cleanStr.split("+").map((p) => p.trim());
|
|
134
|
-
const parts = rawParts.filter(Boolean);
|
|
135
|
-
const impliedPlusKey = rawParts.some((p) => p === "") && cleanStr.includes("++");
|
|
136
|
-
if (!impliedPlusKey) {
|
|
137
|
-
let seenBase = false;
|
|
138
|
-
for (const part of parts) {
|
|
139
|
-
if (isModifierToken(part)) {
|
|
140
|
-
if (seenBase) fail('modifiers must come before the base key (e.g. "ctrl+k", not "k+ctrl")');
|
|
141
|
-
} else {
|
|
142
|
-
seenBase = true;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
let bases = 0;
|
|
147
|
-
for (const part of parts) {
|
|
148
|
-
if (part === "__code__") {
|
|
149
|
-
combo.code = codeValue;
|
|
150
|
-
bases++;
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
const actual = ownValue(keyAliases, part) ?? part;
|
|
154
|
-
if (modifierTokens.has(actual)) setMod(actual);
|
|
155
|
-
else {
|
|
156
|
-
combo.key = normalizeKey(actual);
|
|
157
|
-
bases++;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
if (bases > 1) fail('use one base key (e.g. "ctrl+k"), or one code: token');
|
|
161
|
-
if (!combo.key && !combo.code && (impliedPlusKey || stripped === "+")) combo.key = "+";
|
|
162
|
-
if (!combo.key && !combo.code) {
|
|
163
|
-
const modCount = (combo.ctrl ? 1 : 0) + (combo.shift ? 1 : 0) + (combo.alt ? 1 : 0) + (combo.meta ? 1 : 0);
|
|
164
|
-
if (modCount === 1) {
|
|
165
|
-
combo.bareModifier = combo.ctrl ? "ctrl" : combo.shift ? "shift" : combo.alt ? "alt" : "meta";
|
|
166
|
-
} else if (modCount > 1) {
|
|
167
|
-
fail('multi-modifier bare bindings not supported. Add a base key (e.g. "ctrl+alt+x").');
|
|
168
|
-
} else {
|
|
169
|
-
error = error || `Invalid hotkey "${hotkeyStr}" (missing base key)`;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return { combo, type: isUp ? "keyup" : "keydown", raw: hotkeyStr, error };
|
|
173
|
-
}
|
|
174
|
-
function parseHotkey(hotkeyStr, opts) {
|
|
175
|
-
const { combo, type, raw, error } = readHotkey(hotkeyStr, opts);
|
|
176
|
-
if (error) throw new Error(error);
|
|
177
|
-
return { combo, type, raw };
|
|
178
|
-
}
|
|
179
|
-
var SHIFTED_PUNCT = {
|
|
180
|
-
",": ["<", "Comma"],
|
|
181
|
-
".": [">", "Period"],
|
|
182
|
-
"/": ["?", "Slash"],
|
|
183
|
-
";": [":", "Semicolon"],
|
|
184
|
-
"'": ['"', "Quote"],
|
|
185
|
-
"[": ["{", "BracketLeft"],
|
|
186
|
-
"]": ["}", "BracketRight"],
|
|
187
|
-
"\\": ["|", "Backslash"],
|
|
188
|
-
"-": ["_", "Minus"],
|
|
189
|
-
"=": ["+", "Equal"],
|
|
190
|
-
"`": ["~", "Backquote"]
|
|
191
|
-
};
|
|
192
|
-
for (let d = 0; d < 10; d++) SHIFTED_PUNCT[d] = [")!@#$%^&*("[d], "Digit" + d];
|
|
193
|
-
function shiftedPunctuationHint(hotkeyStr) {
|
|
194
|
-
const { combo, type, error } = readHotkey(hotkeyStr);
|
|
195
|
-
if (error || !combo.shift || combo.code) return null;
|
|
196
|
-
const shifted = ownValue(SHIFTED_PUNCT, combo.key);
|
|
197
|
-
if (!shifted) return null;
|
|
198
|
-
const [typed, code] = shifted;
|
|
199
|
-
const base = String(hotkeyStr).trim().replace(/\s+up$/i, "");
|
|
200
|
-
const suggestion = base.slice(0, -1) + "code:" + code + (type === "keyup" ? " up" : "");
|
|
201
|
-
return {
|
|
202
|
-
message: `"${hotkeyStr}" never fires on US layouts: Shift turns "${combo.key}" into "${typed}". Bind "${suggestion}" instead.`,
|
|
203
|
-
suggestion
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
|
|
207
4
|
// core.js
|
|
5
|
+
import { ownValue } from "./lookup.js";
|
|
6
|
+
import { parseHotkey, normalizeKey } from "./parse.js";
|
|
7
|
+
import { detectPlatform } from "./platform.js";
|
|
208
8
|
function on(target, type, fn, options) {
|
|
209
9
|
target.addEventListener(type, fn, options);
|
|
210
10
|
return () => target.removeEventListener(type, fn, options);
|
|
@@ -218,7 +18,6 @@ var MODIFIER_KEY_TOKEN = Object.freeze({
|
|
|
218
18
|
Meta: "meta",
|
|
219
19
|
OS: "meta"
|
|
220
20
|
});
|
|
221
|
-
var warnedShifted = /* @__PURE__ */ new Set();
|
|
222
21
|
function comboKeyFromParts({ ctrl, shift, alt, meta, key, code, bareModifier }) {
|
|
223
22
|
if (bareModifier) return `__bare__:${bareModifier}`;
|
|
224
23
|
let s = "";
|
|
@@ -351,62 +150,6 @@ function unshiftedKeyFromEvent(e) {
|
|
|
351
150
|
key: normalizeKey(e.key)
|
|
352
151
|
});
|
|
353
152
|
}
|
|
354
|
-
var BARE_MOD_TO_KEY = {
|
|
355
|
-
ctrl: "Control",
|
|
356
|
-
shift: "Shift",
|
|
357
|
-
alt: "Alt",
|
|
358
|
-
meta: "Meta"
|
|
359
|
-
};
|
|
360
|
-
var CODE_TO_KEY = {
|
|
361
|
-
Period: ".",
|
|
362
|
-
Comma: ",",
|
|
363
|
-
Semicolon: ";",
|
|
364
|
-
Quote: "'",
|
|
365
|
-
Slash: "/",
|
|
366
|
-
Backslash: "\\",
|
|
367
|
-
BracketLeft: "[",
|
|
368
|
-
BracketRight: "]",
|
|
369
|
-
Minus: "-",
|
|
370
|
-
Equal: "=",
|
|
371
|
-
Backquote: "`",
|
|
372
|
-
Space: " ",
|
|
373
|
-
Enter: "Enter",
|
|
374
|
-
NumpadEnter: "Enter",
|
|
375
|
-
Tab: "Tab",
|
|
376
|
-
Escape: "Escape",
|
|
377
|
-
Backspace: "Backspace",
|
|
378
|
-
Delete: "Delete",
|
|
379
|
-
Insert: "Insert",
|
|
380
|
-
Home: "Home",
|
|
381
|
-
End: "End",
|
|
382
|
-
PageUp: "PageUp",
|
|
383
|
-
PageDown: "PageDown",
|
|
384
|
-
ArrowUp: "ArrowUp",
|
|
385
|
-
ArrowDown: "ArrowDown",
|
|
386
|
-
ArrowLeft: "ArrowLeft",
|
|
387
|
-
ArrowRight: "ArrowRight"
|
|
388
|
-
};
|
|
389
|
-
var KEY_TO_CODE = {};
|
|
390
|
-
var _a;
|
|
391
|
-
for (const [code, key] of Object.entries(CODE_TO_KEY)) {
|
|
392
|
-
KEY_TO_CODE[_a = normalizeKey(key)] ?? (KEY_TO_CODE[_a] = code);
|
|
393
|
-
}
|
|
394
|
-
function keyForCode(code) {
|
|
395
|
-
if (!code) return null;
|
|
396
|
-
const letter = /^Key([A-Z])$/.exec(code);
|
|
397
|
-
if (letter) return letter[1].toLowerCase();
|
|
398
|
-
const digit = /^(?:Digit|Numpad)(\d)$/.exec(code);
|
|
399
|
-
if (digit) return digit[1];
|
|
400
|
-
if (/^F\d{1,2}$/.test(code)) return code;
|
|
401
|
-
return ownValue(CODE_TO_KEY, code) ?? null;
|
|
402
|
-
}
|
|
403
|
-
function codeForKey(key) {
|
|
404
|
-
if (!key) return null;
|
|
405
|
-
if (/^[a-z]$/.test(key)) return "Key" + key.toUpperCase();
|
|
406
|
-
if (/^\d$/.test(key)) return "Digit" + key;
|
|
407
|
-
if (/^f\d{1,2}$/.test(key)) return key.toUpperCase();
|
|
408
|
-
return ownValue(KEY_TO_CODE, key) ?? null;
|
|
409
|
-
}
|
|
410
153
|
function comboFromEvent(e, { physical = true, useMod = false, platform = PLATFORM } = {}) {
|
|
411
154
|
if (!e || ownValue(MODIFIER_KEY_TOKEN, e.key)) return null;
|
|
412
155
|
const parts = [];
|
|
@@ -485,18 +228,10 @@ function createRouter({
|
|
|
485
228
|
};
|
|
486
229
|
ensureSlot(comboStr, type).push(binding);
|
|
487
230
|
bindingIndex.set(binding.id, { comboStr, type });
|
|
488
|
-
const warnKey = `${comboStr} ${type}`;
|
|
489
|
-
if (!warnedShifted.has(warnKey)) {
|
|
490
|
-
const hint = shiftedPunctuationHint(hotkeyStr);
|
|
491
|
-
if (hint) {
|
|
492
|
-
warnedShifted.add(warnKey);
|
|
493
|
-
console.warn(`[hotkey-router] ${hint.message}`);
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
231
|
if (bindHooks.length) {
|
|
497
232
|
for (const hook of bindHooks) {
|
|
498
233
|
try {
|
|
499
|
-
hook({ combo, raw, options: opts, plugin, id: binding.id });
|
|
234
|
+
hook({ combo, type, raw, options: opts, plugin, id: binding.id });
|
|
500
235
|
} catch (err) {
|
|
501
236
|
console.error("[hotkey-router] onBind hook threw:", err);
|
|
502
237
|
}
|
|
@@ -620,13 +355,21 @@ function createRouter({
|
|
|
620
355
|
if (o.once) removeById(winner.id);
|
|
621
356
|
winner.handler(e);
|
|
622
357
|
}
|
|
358
|
+
function routeEvent(e, type, phase) {
|
|
359
|
+
if (paused) return false;
|
|
360
|
+
const candidates = eligibleBindings(e, type, phase);
|
|
361
|
+
if (!candidates.length) return false;
|
|
362
|
+
fire(pickWinner(candidates), e);
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
623
365
|
function handleEvent(type, phase = "all") {
|
|
624
366
|
return (e) => {
|
|
625
|
-
|
|
626
|
-
const candidates = eligibleBindings(e, type, phase);
|
|
627
|
-
if (candidates.length) fire(pickWinner(candidates), e);
|
|
367
|
+
routeEvent(e, type, phase);
|
|
628
368
|
};
|
|
629
369
|
}
|
|
370
|
+
function dispatch(e, type) {
|
|
371
|
+
return routeEvent(e, type, "all");
|
|
372
|
+
}
|
|
630
373
|
let stopDown = null;
|
|
631
374
|
let stopUp = null;
|
|
632
375
|
let stopCaptureDown = null;
|
|
@@ -657,48 +400,6 @@ function createRouter({
|
|
|
657
400
|
plugins.clear();
|
|
658
401
|
bindingIndex.clear();
|
|
659
402
|
}
|
|
660
|
-
function trigger(hotkeyStr, { type: forcedType, target: target2 = null } = {}) {
|
|
661
|
-
const { combo, type } = parseHotkey(hotkeyStr);
|
|
662
|
-
const actualType = forcedType || type;
|
|
663
|
-
if (paused) return false;
|
|
664
|
-
const eventKey = combo.bareModifier ? ownValue(BARE_MOD_TO_KEY, combo.bareModifier) : combo.key ?? keyForCode(combo.code);
|
|
665
|
-
const eventCode = combo.bareModifier ? null : combo.code ?? codeForKey(combo.key);
|
|
666
|
-
let altKey = !!combo.alt;
|
|
667
|
-
let ctrlKey = !!combo.ctrl;
|
|
668
|
-
let shiftKey = !!combo.shift;
|
|
669
|
-
let metaKey = !!combo.meta;
|
|
670
|
-
if (combo.bareModifier && actualType === "keyup") {
|
|
671
|
-
if (combo.bareModifier === "alt") altKey = false;
|
|
672
|
-
else if (combo.bareModifier === "ctrl") ctrlKey = false;
|
|
673
|
-
else if (combo.bareModifier === "shift") shiftKey = false;
|
|
674
|
-
else if (combo.bareModifier === "meta") metaKey = false;
|
|
675
|
-
}
|
|
676
|
-
const e = {
|
|
677
|
-
type: actualType,
|
|
678
|
-
key: eventKey,
|
|
679
|
-
code: eventCode,
|
|
680
|
-
ctrlKey,
|
|
681
|
-
shiftKey,
|
|
682
|
-
altKey,
|
|
683
|
-
metaKey,
|
|
684
|
-
repeat: false,
|
|
685
|
-
isComposing: false,
|
|
686
|
-
getModifierState() {
|
|
687
|
-
return false;
|
|
688
|
-
},
|
|
689
|
-
preventDefault() {
|
|
690
|
-
},
|
|
691
|
-
stopPropagation() {
|
|
692
|
-
},
|
|
693
|
-
stopImmediatePropagation() {
|
|
694
|
-
},
|
|
695
|
-
target: target2
|
|
696
|
-
};
|
|
697
|
-
const candidates = eligibleBindings(e, actualType, "all");
|
|
698
|
-
if (!candidates.length) return false;
|
|
699
|
-
fire(pickWinner(candidates), e);
|
|
700
|
-
return true;
|
|
701
|
-
}
|
|
702
403
|
return {
|
|
703
404
|
// lifecycle
|
|
704
405
|
init,
|
|
@@ -715,12 +416,7 @@ function createRouter({
|
|
|
715
416
|
pause,
|
|
716
417
|
resume,
|
|
717
418
|
ignoreInput,
|
|
718
|
-
|
|
719
|
-
trigger,
|
|
720
|
-
// helpers
|
|
721
|
-
parseHotkey,
|
|
722
|
-
comboFromEvent,
|
|
723
|
-
detectPlatform
|
|
419
|
+
dispatch
|
|
724
420
|
};
|
|
725
421
|
}
|
|
726
422
|
export {
|