hotkey-router 0.3.1 → 0.3.3
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 +129 -11
- package/dist/auto.js +5 -1031
- package/dist/auto.js.map +4 -4
- package/dist/auto.min.js +2 -2
- package/dist/format.js +394 -0
- package/dist/format.js.map +7 -0
- package/dist/hotkey-router.cjs +270 -127
- package/dist/hotkey-router.cjs.map +4 -4
- package/dist/hotkey-router.js +269 -128
- package/dist/hotkey-router.js.map +4 -4
- package/dist/hotkey-router.min.js +2 -2
- package/dist/reservations.js +128 -62
- package/dist/reservations.js.map +4 -4
- package/dist/sites.js +195 -80
- package/dist/sites.js.map +4 -4
- package/dist/types/format.d.ts +128 -0
- package/dist/types/hotkey-router.d.ts +19 -20
- package/dist/types/parse.d.ts +125 -0
- package/dist/types/platform.d.ts +23 -0
- package/dist/types/reservations.d.ts +11 -6
- package/dist/types/sites.d.ts +6 -1
- package/dist/types/validate.d.ts +2 -2
- package/dist/validate.js +202 -8
- package/dist/validate.js.map +3 -3
- package/package.json +12 -2
|
@@ -13,6 +13,7 @@ declare namespace _default {
|
|
|
13
13
|
export { trigger };
|
|
14
14
|
export { parseHotkey };
|
|
15
15
|
export { comboFromEvent };
|
|
16
|
+
export { detectPlatform };
|
|
16
17
|
}
|
|
17
18
|
export default _default;
|
|
18
19
|
/**
|
|
@@ -37,6 +38,7 @@ export type BindOptions = {
|
|
|
37
38
|
capture?: boolean;
|
|
38
39
|
altGraph?: boolean;
|
|
39
40
|
composing?: boolean;
|
|
41
|
+
warnOnReserved?: boolean;
|
|
40
42
|
};
|
|
41
43
|
/**
|
|
42
44
|
* Return value of {@link bind}: callable to unbind, plus `id` (the unique
|
|
@@ -155,30 +157,22 @@ declare function resume(): void;
|
|
|
155
157
|
*/
|
|
156
158
|
declare function ignoreInput(value?: boolean): void;
|
|
157
159
|
/**
|
|
158
|
-
* Programmatically fire
|
|
159
|
-
* automation; not a typical app-runtime path.
|
|
160
|
-
*
|
|
160
|
+
* Programmatically fire the binding a real keypress of `hotkeyStr` would
|
|
161
|
+
* fire. Used by tests and automation; not a typical app-runtime path. The
|
|
162
|
+
* synthetic event carries both `key` and `code` (US layout), so key-based
|
|
163
|
+
* and `code:` bindings both match, and the same guards apply as for a real
|
|
164
|
+
* event: `when`, `ignoreInput`/`allowIn` (pass `target` to fire "from" an
|
|
165
|
+
* element), priority, `once`, and `pause()`. Returns `true` if a handler ran.
|
|
161
166
|
*
|
|
162
167
|
* @param {string} hotkeyStr
|
|
163
|
-
* @param {{ type?: 'keydown' | 'keyup' }} [opts]
|
|
168
|
+
* @param {{ type?: 'keydown' | 'keyup', target?: EventTarget | null }} [opts]
|
|
164
169
|
* @returns {boolean}
|
|
165
170
|
*/
|
|
166
|
-
declare function trigger(hotkeyStr: string, { type: forcedType }?: {
|
|
171
|
+
declare function trigger(hotkeyStr: string, { type: forcedType, target }?: {
|
|
167
172
|
type?: "keydown" | "keyup";
|
|
173
|
+
target?: EventTarget | null;
|
|
168
174
|
}): boolean;
|
|
169
|
-
|
|
170
|
-
combo: {
|
|
171
|
-
ctrl: boolean;
|
|
172
|
-
shift: boolean;
|
|
173
|
-
alt: boolean;
|
|
174
|
-
meta: boolean;
|
|
175
|
-
key: any;
|
|
176
|
-
code: any;
|
|
177
|
-
bareModifier: any;
|
|
178
|
-
};
|
|
179
|
-
type: string;
|
|
180
|
-
raw: any;
|
|
181
|
-
};
|
|
175
|
+
import { parseHotkey } from './parse.js';
|
|
182
176
|
/**
|
|
183
177
|
* The hotkey string for a keyboard event, for a "press your keys" recorder.
|
|
184
178
|
* Physical by default (`ctrl+shift+code:Period`), so the binding survives
|
|
@@ -186,11 +180,16 @@ export function parseHotkey(hotkeyStr: any): {
|
|
|
186
180
|
* macOS, Ctrl elsewhere) is written as `mod`. Returns null for a bare
|
|
187
181
|
* modifier press (the user hasn't pressed the base key yet).
|
|
188
182
|
*
|
|
183
|
+
* `platform` (default: this machine) decides which modifier `mod` stands for.
|
|
184
|
+
*
|
|
189
185
|
* @param {KeyboardEvent} e
|
|
190
|
-
* @param {{ physical?: boolean, useMod?: boolean }} [opts]
|
|
186
|
+
* @param {{ physical?: boolean, useMod?: boolean, platform?: 'mac' | 'windows' | 'linux' | 'chromeos' | null }} [opts]
|
|
191
187
|
* @returns {string | null}
|
|
192
188
|
*/
|
|
193
|
-
export function comboFromEvent(e: KeyboardEvent, { physical, useMod }?: {
|
|
189
|
+
export function comboFromEvent(e: KeyboardEvent, { physical, useMod, platform }?: {
|
|
194
190
|
physical?: boolean;
|
|
195
191
|
useMod?: boolean;
|
|
192
|
+
platform?: "mac" | "windows" | "linux" | "chromeos" | null;
|
|
196
193
|
}): string | null;
|
|
194
|
+
import { detectPlatform } from './platform.js';
|
|
195
|
+
export { parseHotkey, detectPlatform };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
export function normalizeKey(key: any): any;
|
|
2
|
+
/**
|
|
3
|
+
* Lenient parse: never throws. `error` holds the message `parseHotkey` would
|
|
4
|
+
* throw; the combo is filled in as far as the string allows.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} hotkeyStr
|
|
7
|
+
* @param {{ platform?: string | null }} [opts]
|
|
8
|
+
*/
|
|
9
|
+
export function readHotkey(hotkeyStr: string, { platform }?: {
|
|
10
|
+
platform?: string | null;
|
|
11
|
+
}): {
|
|
12
|
+
combo: {
|
|
13
|
+
ctrl: boolean;
|
|
14
|
+
shift: boolean;
|
|
15
|
+
alt: boolean;
|
|
16
|
+
meta: boolean;
|
|
17
|
+
key: any;
|
|
18
|
+
code: any;
|
|
19
|
+
bareModifier: any;
|
|
20
|
+
};
|
|
21
|
+
type: string;
|
|
22
|
+
raw: string;
|
|
23
|
+
error: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Parse a hotkey string into modifier flags plus a `key` (normalized
|
|
27
|
+
* `KeyboardEvent.key`), a `code` (`KeyboardEvent.code`) or a bare modifier.
|
|
28
|
+
* `mod` resolves for `opts.platform` (default: this machine). Throws on
|
|
29
|
+
* strings the router can't bind.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} hotkeyStr
|
|
32
|
+
* @param {{ platform?: 'mac' | 'windows' | 'linux' | 'chromeos' | null }} [opts]
|
|
33
|
+
* @returns {{
|
|
34
|
+
* combo: { ctrl: boolean, shift: boolean, alt: boolean, meta: boolean, key: string | null, code: string | null, bareModifier: 'ctrl' | 'shift' | 'alt' | 'meta' | null },
|
|
35
|
+
* type: 'keydown' | 'keyup',
|
|
36
|
+
* raw: string
|
|
37
|
+
* }}
|
|
38
|
+
*/
|
|
39
|
+
export function parseHotkey(hotkeyStr: string, opts?: {
|
|
40
|
+
platform?: "mac" | "windows" | "linux" | "chromeos" | null;
|
|
41
|
+
}): {
|
|
42
|
+
combo: {
|
|
43
|
+
ctrl: boolean;
|
|
44
|
+
shift: boolean;
|
|
45
|
+
alt: boolean;
|
|
46
|
+
meta: boolean;
|
|
47
|
+
key: string | null;
|
|
48
|
+
code: string | null;
|
|
49
|
+
bareModifier: "ctrl" | "shift" | "alt" | "meta" | null;
|
|
50
|
+
};
|
|
51
|
+
type: "keydown" | "keyup";
|
|
52
|
+
raw: string;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* For a key-based combo with Shift and one of `, . / ; ' [ ] \ - = \``, the
|
|
56
|
+
* message explaining it never fires on US layouts and the `code:` spelling
|
|
57
|
+
* to bind instead (written the way the input was). Null for anything else,
|
|
58
|
+
* including strings that do not parse.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} hotkeyStr
|
|
61
|
+
* @returns {{ message: string, suggestion: string } | null}
|
|
62
|
+
*/
|
|
63
|
+
export function shiftedPunctuationHint(hotkeyStr: string): {
|
|
64
|
+
message: string;
|
|
65
|
+
suggestion: string;
|
|
66
|
+
} | null;
|
|
67
|
+
/** `KeyA` -> `a`, `Digit1`/`Numpad1` -> `1`, `ArrowLeft` -> `arrowleft`. */
|
|
68
|
+
export function codeToBase(code: any): any;
|
|
69
|
+
/** `'.'` -> `period`, `'Esc'` -> `escape`, `'K'` -> `k`. */
|
|
70
|
+
export function keyToBase(key: any): any;
|
|
71
|
+
/**
|
|
72
|
+
* The canonical lookup key for a parsed combo: modifiers in the order ctrl,
|
|
73
|
+
* meta, alt, shift, then the base name (`ctrl+shift+period`). Null for a bare
|
|
74
|
+
* modifier or no key.
|
|
75
|
+
*
|
|
76
|
+
* @param {{ ctrl?: boolean, meta?: boolean, alt?: boolean, shift?: boolean, key?: string | null, code?: string | null, bareModifier?: string | null } | null} combo
|
|
77
|
+
* @returns {string | null}
|
|
78
|
+
*/
|
|
79
|
+
export function lookupKey(combo: {
|
|
80
|
+
ctrl?: boolean;
|
|
81
|
+
meta?: boolean;
|
|
82
|
+
alt?: boolean;
|
|
83
|
+
shift?: boolean;
|
|
84
|
+
key?: string | null;
|
|
85
|
+
code?: string | null;
|
|
86
|
+
bareModifier?: string | null;
|
|
87
|
+
} | null): string | null;
|
|
88
|
+
export const keyAliases: Readonly<{
|
|
89
|
+
esc: "escape";
|
|
90
|
+
escape: "escape";
|
|
91
|
+
space: " ";
|
|
92
|
+
spacebar: " ";
|
|
93
|
+
enter: "enter";
|
|
94
|
+
return: "enter";
|
|
95
|
+
del: "delete";
|
|
96
|
+
delete: "delete";
|
|
97
|
+
backspace: "backspace";
|
|
98
|
+
tab: "tab";
|
|
99
|
+
up: "arrowup";
|
|
100
|
+
down: "arrowdown";
|
|
101
|
+
left: "arrowleft";
|
|
102
|
+
right: "arrowright";
|
|
103
|
+
home: "home";
|
|
104
|
+
end: "end";
|
|
105
|
+
pageup: "pageup";
|
|
106
|
+
pagedown: "pagedown";
|
|
107
|
+
pgup: "pageup";
|
|
108
|
+
pgdn: "pagedown";
|
|
109
|
+
cmd: "meta";
|
|
110
|
+
command: "meta";
|
|
111
|
+
win: "meta";
|
|
112
|
+
meta: "meta";
|
|
113
|
+
control: "ctrl";
|
|
114
|
+
ctrl: "ctrl";
|
|
115
|
+
option: "alt";
|
|
116
|
+
opt: "alt";
|
|
117
|
+
alt: "alt";
|
|
118
|
+
shift: "shift";
|
|
119
|
+
'\u2318': "meta";
|
|
120
|
+
'\u2303': "ctrl";
|
|
121
|
+
'\u2325': "alt";
|
|
122
|
+
'\u21E7': "shift";
|
|
123
|
+
mod: "mod";
|
|
124
|
+
plus: "+";
|
|
125
|
+
}>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The platform a navigator reports: 'mac', 'windows' or 'linux' (ChromeOS
|
|
3
|
+
* counts as 'linux' unless `{ chromeos: true }`). Null when it can't tell,
|
|
4
|
+
* or outside a browser.
|
|
5
|
+
*
|
|
6
|
+
* @param {Navigator | null} [nav]
|
|
7
|
+
* @param {{ chromeos?: boolean }} [opts]
|
|
8
|
+
* @returns {Platform | null}
|
|
9
|
+
*/
|
|
10
|
+
export function detectPlatform(nav?: Navigator | null, { chromeos }?: {
|
|
11
|
+
chromeos?: boolean;
|
|
12
|
+
}): Platform | null;
|
|
13
|
+
/**
|
|
14
|
+
* @param {Navigator | null} [nav]
|
|
15
|
+
* @returns {Browser | null}
|
|
16
|
+
*/
|
|
17
|
+
export function detectBrowser(nav?: Navigator | null): Browser | null;
|
|
18
|
+
/** 'mac' -> 'macOS'. Unknown names come back as given. @param {string} platform @returns {string} */
|
|
19
|
+
export function platformLabel(platform: string): string;
|
|
20
|
+
/** 'chrome' -> 'Chrome'. Unknown names come back as given. @param {string} browser @returns {string} */
|
|
21
|
+
export function browserLabel(browser: string): string;
|
|
22
|
+
export type Platform = "mac" | "windows" | "linux" | "chromeos";
|
|
23
|
+
export type Browser = "chrome" | "firefox" | "safari" | "edge";
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
export function detectPlatform(nav?: Navigator): "mac" | "windows" | "linux";
|
|
2
|
-
export function detectBrowser(nav?: Navigator): "edge" | "firefox" | "chrome" | "safari";
|
|
3
|
-
export function lookupKeyFromCombo(combo: any): string;
|
|
4
1
|
/**
|
|
5
2
|
* @typedef {'mac' | 'windows' | 'linux'} Platform
|
|
6
3
|
* @typedef {'chrome' | 'firefox' | 'safari' | 'edge'} Browser
|
|
7
|
-
* @typedef {'hard' | '
|
|
4
|
+
* @typedef {'hard' | 'os' | 'menu-activation' | 'find-bar-only' | 'compose' | 'system-text' | 'devtools-open'} Severity
|
|
5
|
+
* hard tiers (never reach the page): hard, os, menu-activation; the rest are soft
|
|
8
6
|
*
|
|
9
7
|
* @typedef {{
|
|
10
8
|
* source: 'browser' | 'system',
|
|
@@ -64,12 +62,15 @@ export function installReservationWarnings(hotkeys: object, opts?: {
|
|
|
64
62
|
browser?: Browser;
|
|
65
63
|
console?: Console;
|
|
66
64
|
}): () => void;
|
|
65
|
+
export const lookupKeyFromCombo: typeof lookupKey;
|
|
67
66
|
export const SCHEMA_VERSION: any;
|
|
68
67
|
export const DATA_GENERATED: any;
|
|
69
|
-
export { HOTKEY_DATA };
|
|
70
68
|
export type Platform = "mac" | "windows" | "linux";
|
|
71
69
|
export type Browser = "chrome" | "firefox" | "safari" | "edge";
|
|
72
|
-
|
|
70
|
+
/**
|
|
71
|
+
* hard tiers (never reach the page): hard, os, menu-activation; the rest are soft
|
|
72
|
+
*/
|
|
73
|
+
export type Severity = "hard" | "os" | "menu-activation" | "find-bar-only" | "compose" | "system-text" | "devtools-open";
|
|
73
74
|
export type Reservation = {
|
|
74
75
|
source: "browser" | "system";
|
|
75
76
|
platform: Platform;
|
|
@@ -78,3 +79,7 @@ export type Reservation = {
|
|
|
78
79
|
action: string;
|
|
79
80
|
severity: Severity;
|
|
80
81
|
};
|
|
82
|
+
import { detectPlatform } from './platform.js';
|
|
83
|
+
import { detectBrowser } from './platform.js';
|
|
84
|
+
import { lookupKey } from './parse.js';
|
|
85
|
+
export { detectPlatform, detectBrowser, HOTKEY_DATA };
|
package/dist/types/sites.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/** @typedef {'mac' | 'windows' | 'linux' | 'chromeos'} SitePlatform */
|
|
2
2
|
/**
|
|
3
|
+
* `detectPlatform(nav, { chromeos: true })` from the shared detector: the
|
|
4
|
+
* site table has a ChromeOS column.
|
|
5
|
+
*
|
|
3
6
|
* @param {Navigator | null} [nav]
|
|
4
7
|
* @returns {SitePlatform | null}
|
|
5
8
|
*/
|
|
@@ -7,7 +10,9 @@ export function detectSitePlatform(nav?: Navigator | null): SitePlatform | null;
|
|
|
7
10
|
/**
|
|
8
11
|
* Parse a hotkey string (`mod+shift+code:Period`, `ctrl+alt+,`) into modifier
|
|
9
12
|
* flags and a canonical base key name (`period`), resolving `mod` for the
|
|
10
|
-
* platform.
|
|
13
|
+
* platform. Same parser as the router (`opt`, `^k`, `ctrl++`), but lenient:
|
|
14
|
+
* a string the router would refuse still yields what it can, and `base` is
|
|
15
|
+
* null when there is no key. Shared with validate.js.
|
|
11
16
|
*
|
|
12
17
|
* @param {string} str
|
|
13
18
|
* @param {SitePlatform} platform
|
package/dist/types/validate.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @typedef {{
|
|
3
3
|
* code: 'invalid' | 'no-key' | 'no-modifier' | 'alt-only' | 'needs-primary' |
|
|
4
4
|
* 'too-few-modifiers' | 'taken' | 'reserved' | 'reserved-soft' |
|
|
5
|
-
* 'altgr' | 'site-conflict',
|
|
5
|
+
* 'altgr' | 'site-conflict' | 'shifted-punctuation',
|
|
6
6
|
* severity: 'error' | 'warning',
|
|
7
7
|
* message: string,
|
|
8
8
|
* platforms?: string[],
|
|
@@ -34,7 +34,7 @@ export function validateCombo(combo: string, { platforms, browsers, requirePrima
|
|
|
34
34
|
lookupKeys: Record<string, string>;
|
|
35
35
|
};
|
|
36
36
|
export type ComboReason = {
|
|
37
|
-
code: "invalid" | "no-key" | "no-modifier" | "alt-only" | "needs-primary" | "too-few-modifiers" | "taken" | "reserved" | "reserved-soft" | "altgr" | "site-conflict";
|
|
37
|
+
code: "invalid" | "no-key" | "no-modifier" | "alt-only" | "needs-primary" | "too-few-modifiers" | "taken" | "reserved" | "reserved-soft" | "altgr" | "site-conflict" | "shifted-punctuation";
|
|
38
38
|
severity: "error" | "warning";
|
|
39
39
|
message: string;
|
|
40
40
|
platforms?: string[];
|
package/dist/validate.js
CHANGED
|
@@ -1,13 +1,200 @@
|
|
|
1
|
-
// hotkey-router v0.3.
|
|
1
|
+
// hotkey-router v0.3.3 | SEE LICENSE IN LICENSE
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
// validate.js
|
|
5
5
|
import { lookupReservation, severityIsHard } from "./reservations.js";
|
|
6
6
|
import { parseComboString, listSites } from "./sites.js";
|
|
7
|
+
import { modifierLabel, listModifiers, platformLabel, browserLabel } from "./format.js";
|
|
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 keyAliases[k] ?? k;
|
|
90
|
+
}
|
|
91
|
+
function isModifierToken(token) {
|
|
92
|
+
return modifierTokens.has(normalizeKey(token));
|
|
93
|
+
}
|
|
94
|
+
var CODE_TOKEN_RE = /(^|\+)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) {
|
|
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
|
+
for (const part of parts) {
|
|
147
|
+
if (part === "__code__") {
|
|
148
|
+
combo.code = codeValue;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const actual = keyAliases[part] ?? part;
|
|
152
|
+
if (modifierTokens.has(actual)) setMod(actual);
|
|
153
|
+
else combo.key = normalizeKey(actual);
|
|
154
|
+
}
|
|
155
|
+
if (!combo.key && !combo.code && impliedPlusKey) combo.key = "+";
|
|
156
|
+
if (!combo.key && !combo.code) {
|
|
157
|
+
const modCount = (combo.ctrl ? 1 : 0) + (combo.shift ? 1 : 0) + (combo.alt ? 1 : 0) + (combo.meta ? 1 : 0);
|
|
158
|
+
if (modCount === 1) {
|
|
159
|
+
combo.bareModifier = combo.ctrl ? "ctrl" : combo.shift ? "shift" : combo.alt ? "alt" : "meta";
|
|
160
|
+
} else if (modCount > 1) {
|
|
161
|
+
fail('multi-modifier bare bindings not supported. Add a base key (e.g. "ctrl+alt+x").');
|
|
162
|
+
} else {
|
|
163
|
+
error = error || `Invalid hotkey "${hotkeyStr}" (missing base key)`;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return { combo, type: isUp ? "keyup" : "keydown", raw: hotkeyStr, error };
|
|
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
|
+
// validate.js
|
|
7
196
|
var ALL_PLATFORMS = ["mac", "windows", "linux"];
|
|
8
197
|
var ALL_BROWSERS = ["chrome", "firefox", "edge", "safari"];
|
|
9
|
-
var PLATFORM_LABEL = { mac: "macOS", windows: "Windows", linux: "Linux" };
|
|
10
|
-
var BROWSER_LABEL = { chrome: "Chrome", firefox: "Firefox", edge: "Edge", safari: "Safari" };
|
|
11
198
|
var SITE_COL = { windows: 0, mac: 1, linux: 0 };
|
|
12
199
|
function validateCombo(combo, {
|
|
13
200
|
platforms = ALL_PLATFORMS,
|
|
@@ -27,9 +214,16 @@ function validateCombo(combo, {
|
|
|
27
214
|
}
|
|
28
215
|
reasons.push(reason);
|
|
29
216
|
};
|
|
217
|
+
const allMac = platforms.every((p) => p === "mac");
|
|
218
|
+
const noMac = !platforms.includes("mac");
|
|
219
|
+
const anyModifier = allMac ? listModifiers(["meta", "alt", "ctrl", "shift"], { platform: "mac" }) : noMac ? listModifiers(["ctrl", "alt", "shift"], { platform: "windows" }) : `${["ctrl", "alt", "shift"].map((m) => modifierLabel(m, { platform: "windows" })).join(", ")} or ${modifierLabel("meta", { platform: "mac" })}`;
|
|
30
220
|
if (typeof combo !== "string" || !combo.trim()) {
|
|
31
221
|
return { ok: false, reasons: [{ code: "invalid", severity: "error", message: "No keys were recorded." }], lookupKeys };
|
|
32
222
|
}
|
|
223
|
+
const shifted = shiftedPunctuationHint(combo);
|
|
224
|
+
if (shifted) {
|
|
225
|
+
add({ code: "shifted-punctuation", severity: "error", message: shifted.message, details: { suggestion: shifted.suggestion } });
|
|
226
|
+
}
|
|
33
227
|
for (const platform of platforms) {
|
|
34
228
|
let parsed;
|
|
35
229
|
try {
|
|
@@ -49,14 +243,14 @@ function validateCombo(combo, {
|
|
|
49
243
|
const key = [...mods, parsed.base].join("+");
|
|
50
244
|
lookupKeys[platform] = key;
|
|
51
245
|
if (mods.length === 0) {
|
|
52
|
-
add({ code: "no-modifier", severity: "error", message:
|
|
246
|
+
add({ code: "no-modifier", severity: "error", message: `Use at least one modifier (${anyModifier}), or the key would fire while typing.`, platforms: [platform] });
|
|
53
247
|
}
|
|
54
248
|
if (platform !== "mac" && mods.length === 1 && parsed.alt) {
|
|
55
|
-
add({ code: "alt-only", severity: "error", message: "
|
|
249
|
+
add({ code: "alt-only", severity: "error", message: `${modifierLabel("alt", { platform })} on its own moves focus to the browser menu when released. Add ${listModifiers(["ctrl", "shift"], { platform })}.`, platforms: [platform] });
|
|
56
250
|
}
|
|
57
251
|
const primary = platform === "mac" ? parsed.meta : parsed.ctrl;
|
|
58
252
|
if (requirePrimary && !primary) {
|
|
59
|
-
add({ code: "needs-primary", severity: "error", message: `Include ${
|
|
253
|
+
add({ code: "needs-primary", severity: "error", message: `Include ${modifierLabel("mod", { platform })}.`, platforms: [platform] });
|
|
60
254
|
}
|
|
61
255
|
if (mods.length > 0 && mods.length < minModifiers) {
|
|
62
256
|
add({ code: "too-few-modifiers", severity: "error", message: `Use at least ${minModifiers} modifiers together.`, platforms: [platform] });
|
|
@@ -77,7 +271,7 @@ function validateCombo(combo, {
|
|
|
77
271
|
if (platform !== "mac" && browser === "safari") continue;
|
|
78
272
|
const hit = lookupReservation(shape, { platform, browser });
|
|
79
273
|
if (!hit) continue;
|
|
80
|
-
const where = hit.source === "system" ?
|
|
274
|
+
const where = hit.source === "system" ? platformLabel(platform) : `${browserLabel(browser)} on ${platformLabel(platform)}`;
|
|
81
275
|
if (severityIsHard(hit.severity)) {
|
|
82
276
|
add({ code: "reserved", severity: "error", message: `${where} uses these keys (${hit.action}), so a page never sees them.`, platforms: [platform], details: { browser, action: hit.action } });
|
|
83
277
|
} else {
|
|
@@ -85,7 +279,7 @@ function validateCombo(combo, {
|
|
|
85
279
|
}
|
|
86
280
|
}
|
|
87
281
|
if (platform === "windows" && parsed.ctrl && parsed.alt && !parsed.meta) {
|
|
88
|
-
add({ code: "altgr", severity: "warning", message:
|
|
282
|
+
add({ code: "altgr", severity: "warning", message: `On some keyboard layouts ${modifierLabel("ctrl", { platform })}+${modifierLabel("alt", { platform })} acts as AltGr and types characters; those keystrokes are skipped.`, platforms: [platform] });
|
|
89
283
|
}
|
|
90
284
|
if (sites) {
|
|
91
285
|
const col = SITE_COL[platform];
|