hotkey-router 0.3.1 → 0.3.2
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 +75 -8
- package/dist/auto.js +265 -195
- 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 +204 -125
- package/dist/hotkey-router.cjs.map +4 -4
- package/dist/hotkey-router.js +203 -126
- 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 +112 -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/validate.js +10 -8
- package/dist/validate.js.map +2 -2
- package/package.json +5 -1
|
@@ -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,112 @@
|
|
|
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
|
+
/** `KeyA` -> `a`, `Digit1`/`Numpad1` -> `1`, `ArrowLeft` -> `arrowleft`. */
|
|
55
|
+
export function codeToBase(code: any): any;
|
|
56
|
+
/** `'.'` -> `period`, `'Esc'` -> `escape`, `'K'` -> `k`. */
|
|
57
|
+
export function keyToBase(key: any): any;
|
|
58
|
+
/**
|
|
59
|
+
* The canonical lookup key for a parsed combo: modifiers in the order ctrl,
|
|
60
|
+
* meta, alt, shift, then the base name (`ctrl+shift+period`). Null for a bare
|
|
61
|
+
* modifier or no key.
|
|
62
|
+
*
|
|
63
|
+
* @param {{ ctrl?: boolean, meta?: boolean, alt?: boolean, shift?: boolean, key?: string | null, code?: string | null, bareModifier?: string | null } | null} combo
|
|
64
|
+
* @returns {string | null}
|
|
65
|
+
*/
|
|
66
|
+
export function lookupKey(combo: {
|
|
67
|
+
ctrl?: boolean;
|
|
68
|
+
meta?: boolean;
|
|
69
|
+
alt?: boolean;
|
|
70
|
+
shift?: boolean;
|
|
71
|
+
key?: string | null;
|
|
72
|
+
code?: string | null;
|
|
73
|
+
bareModifier?: string | null;
|
|
74
|
+
} | null): string | null;
|
|
75
|
+
export const keyAliases: Readonly<{
|
|
76
|
+
esc: "escape";
|
|
77
|
+
escape: "escape";
|
|
78
|
+
space: " ";
|
|
79
|
+
spacebar: " ";
|
|
80
|
+
enter: "enter";
|
|
81
|
+
return: "enter";
|
|
82
|
+
del: "delete";
|
|
83
|
+
delete: "delete";
|
|
84
|
+
backspace: "backspace";
|
|
85
|
+
tab: "tab";
|
|
86
|
+
up: "arrowup";
|
|
87
|
+
down: "arrowdown";
|
|
88
|
+
left: "arrowleft";
|
|
89
|
+
right: "arrowright";
|
|
90
|
+
home: "home";
|
|
91
|
+
end: "end";
|
|
92
|
+
pageup: "pageup";
|
|
93
|
+
pagedown: "pagedown";
|
|
94
|
+
pgup: "pageup";
|
|
95
|
+
pgdn: "pagedown";
|
|
96
|
+
cmd: "meta";
|
|
97
|
+
command: "meta";
|
|
98
|
+
win: "meta";
|
|
99
|
+
meta: "meta";
|
|
100
|
+
control: "ctrl";
|
|
101
|
+
ctrl: "ctrl";
|
|
102
|
+
option: "alt";
|
|
103
|
+
opt: "alt";
|
|
104
|
+
alt: "alt";
|
|
105
|
+
shift: "shift";
|
|
106
|
+
'\u2318': "meta";
|
|
107
|
+
'\u2303': "ctrl";
|
|
108
|
+
'\u2325': "alt";
|
|
109
|
+
'\u21E7': "shift";
|
|
110
|
+
mod: "mod";
|
|
111
|
+
plus: "+";
|
|
112
|
+
}>;
|
|
@@ -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/validate.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
// hotkey-router v0.3.
|
|
1
|
+
// hotkey-router v0.3.2 | 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";
|
|
7
8
|
var ALL_PLATFORMS = ["mac", "windows", "linux"];
|
|
8
9
|
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
10
|
var SITE_COL = { windows: 0, mac: 1, linux: 0 };
|
|
12
11
|
function validateCombo(combo, {
|
|
13
12
|
platforms = ALL_PLATFORMS,
|
|
@@ -27,6 +26,9 @@ function validateCombo(combo, {
|
|
|
27
26
|
}
|
|
28
27
|
reasons.push(reason);
|
|
29
28
|
};
|
|
29
|
+
const allMac = platforms.every((p) => p === "mac");
|
|
30
|
+
const noMac = !platforms.includes("mac");
|
|
31
|
+
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
32
|
if (typeof combo !== "string" || !combo.trim()) {
|
|
31
33
|
return { ok: false, reasons: [{ code: "invalid", severity: "error", message: "No keys were recorded." }], lookupKeys };
|
|
32
34
|
}
|
|
@@ -49,14 +51,14 @@ function validateCombo(combo, {
|
|
|
49
51
|
const key = [...mods, parsed.base].join("+");
|
|
50
52
|
lookupKeys[platform] = key;
|
|
51
53
|
if (mods.length === 0) {
|
|
52
|
-
add({ code: "no-modifier", severity: "error", message:
|
|
54
|
+
add({ code: "no-modifier", severity: "error", message: `Use at least one modifier (${anyModifier}), or the key would fire while typing.`, platforms: [platform] });
|
|
53
55
|
}
|
|
54
56
|
if (platform !== "mac" && mods.length === 1 && parsed.alt) {
|
|
55
|
-
add({ code: "alt-only", severity: "error", message: "
|
|
57
|
+
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
58
|
}
|
|
57
59
|
const primary = platform === "mac" ? parsed.meta : parsed.ctrl;
|
|
58
60
|
if (requirePrimary && !primary) {
|
|
59
|
-
add({ code: "needs-primary", severity: "error", message: `Include ${
|
|
61
|
+
add({ code: "needs-primary", severity: "error", message: `Include ${modifierLabel("mod", { platform })}.`, platforms: [platform] });
|
|
60
62
|
}
|
|
61
63
|
if (mods.length > 0 && mods.length < minModifiers) {
|
|
62
64
|
add({ code: "too-few-modifiers", severity: "error", message: `Use at least ${minModifiers} modifiers together.`, platforms: [platform] });
|
|
@@ -77,7 +79,7 @@ function validateCombo(combo, {
|
|
|
77
79
|
if (platform !== "mac" && browser === "safari") continue;
|
|
78
80
|
const hit = lookupReservation(shape, { platform, browser });
|
|
79
81
|
if (!hit) continue;
|
|
80
|
-
const where = hit.source === "system" ?
|
|
82
|
+
const where = hit.source === "system" ? platformLabel(platform) : `${browserLabel(browser)} on ${platformLabel(platform)}`;
|
|
81
83
|
if (severityIsHard(hit.severity)) {
|
|
82
84
|
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
85
|
} else {
|
|
@@ -85,7 +87,7 @@ function validateCombo(combo, {
|
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
89
|
if (platform === "windows" && parsed.ctrl && parsed.alt && !parsed.meta) {
|
|
88
|
-
add({ code: "altgr", severity: "warning", message:
|
|
90
|
+
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
91
|
}
|
|
90
92
|
if (sites) {
|
|
91
93
|
const col = SITE_COL[platform];
|
package/dist/validate.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../validate.js"],
|
|
4
|
-
"sourcesContent": ["// validate.js\r\n// Check a hotkey before an app saves it, e.g. from a \"press your keys\"\r\n// recorder. Returns plain-language reasons the app can show as they are.\r\n//\r\n// import { comboFromEvent } from 'hotkey-router'\r\n// import { validateCombo } from 'hotkey-router/validate'\r\n//\r\n// const combo = comboFromEvent(e, { useMod: true }) // 'mod+shift+code:Period'\r\n// const { ok, reasons } = validateCombo(combo, { requirePrimary: true, minModifiers: 2 })\r\n//\r\n// Errors make `ok` false (the combo would not work, or breaks a rule the\r\n// app asked for). Warnings leave `ok` true (it works, with a caveat worth\r\n// telling the user: AltGr on some layouts, sites that use the same keys).\r\n//\r\n// Data: data/browser-hotkeys.json (keys the browser or OS take before the\r\n// page sees them) and data/site-hotkeys.runtime.json (keys popular sites use).\r\n\r\nimport { lookupReservation, severityIsHard } from './reservations.js'\r\nimport { parseComboString, listSites } from './sites.js'\r\n\r\nconst ALL_PLATFORMS = ['mac', 'windows', 'linux']\r\nconst ALL_BROWSERS = ['chrome', 'firefox', 'edge', 'safari']\r\
|
|
5
|
-
"mappings": ";;;;AAiBA,SAAS,mBAAmB,sBAAsB;AAClD,SAAS,kBAAkB,iBAAiB;
|
|
4
|
+
"sourcesContent": ["// validate.js\r\n// Check a hotkey before an app saves it, e.g. from a \"press your keys\"\r\n// recorder. Returns plain-language reasons the app can show as they are.\r\n//\r\n// import { comboFromEvent } from 'hotkey-router'\r\n// import { validateCombo } from 'hotkey-router/validate'\r\n//\r\n// const combo = comboFromEvent(e, { useMod: true }) // 'mod+shift+code:Period'\r\n// const { ok, reasons } = validateCombo(combo, { requirePrimary: true, minModifiers: 2 })\r\n//\r\n// Errors make `ok` false (the combo would not work, or breaks a rule the\r\n// app asked for). Warnings leave `ok` true (it works, with a caveat worth\r\n// telling the user: AltGr on some layouts, sites that use the same keys).\r\n//\r\n// Data: data/browser-hotkeys.json (keys the browser or OS take before the\r\n// page sees them) and data/site-hotkeys.runtime.json (keys popular sites use).\r\n\r\nimport { lookupReservation, severityIsHard } from './reservations.js'\r\nimport { parseComboString, listSites } from './sites.js'\r\nimport { modifierLabel, listModifiers, platformLabel, browserLabel } from './format.js'\r\n\r\nconst ALL_PLATFORMS = ['mac', 'windows', 'linux']\r\nconst ALL_BROWSERS = ['chrome', 'firefox', 'edge', 'safari']\r\n// Runtime site rows: [windows, mac, chromeos, action, verified]\r\nconst SITE_COL = { windows: 0, mac: 1, linux: 0 }\r\n\r\n/**\r\n * @typedef {{\r\n * code: 'invalid' | 'no-key' | 'no-modifier' | 'alt-only' | 'needs-primary' |\r\n * 'too-few-modifiers' | 'taken' | 'reserved' | 'reserved-soft' |\r\n * 'altgr' | 'site-conflict',\r\n * severity: 'error' | 'warning',\r\n * message: string,\r\n * platforms?: string[],\r\n * details?: object\r\n * }} ComboReason\r\n */\r\n\r\n/**\r\n * @param {string} combo hotkey string, e.g. `mod+shift+code:Period`\r\n * @param {{\r\n * platforms?: Array<'mac' | 'windows' | 'linux'>,\r\n * browsers?: Array<'chrome' | 'firefox' | 'edge' | 'safari'>,\r\n * requirePrimary?: boolean, // must include Ctrl (Cmd on macOS)\r\n * minModifiers?: number, // default 1\r\n * taken?: string[], // combos the app already uses\r\n * sites?: boolean, // warn about sites that use the keys (default true)\r\n * }} [opts]\r\n * @returns {{ ok: boolean, reasons: ComboReason[], lookupKeys: Record<string, string> }}\r\n */\r\nexport function validateCombo(combo, {\r\n platforms = ALL_PLATFORMS,\r\n browsers = ALL_BROWSERS,\r\n requirePrimary = false,\r\n minModifiers = 1,\r\n taken = [],\r\n sites = true,\r\n} = {}) {\r\n /** @type {ComboReason[]} */\r\n const reasons = []\r\n const lookupKeys = {}\r\n const add = (reason) => {\r\n const same = reasons.find((r) => r.code === reason.code && r.message === reason.message)\r\n if (same) {\r\n for (const p of reason.platforms || []) if (!same.platforms?.includes(p)) same.platforms = [...(same.platforms || []), p]\r\n return\r\n }\r\n reasons.push(reason)\r\n }\r\n\r\n // Messages that cover several platforms name the modifiers once: in the\r\n // platform's own words when every platform agrees (all macOS, or none),\r\n // otherwise PC words plus Cmd.\r\n const allMac = platforms.every((p) => p === 'mac')\r\n const noMac = !platforms.includes('mac')\r\n const anyModifier = allMac\r\n ? listModifiers(['meta', 'alt', 'ctrl', 'shift'], { platform: 'mac' })\r\n : noMac\r\n ? listModifiers(['ctrl', 'alt', 'shift'], { platform: 'windows' })\r\n : `${['ctrl', 'alt', 'shift'].map((m) => modifierLabel(m, { platform: 'windows' })).join(', ')} or ${modifierLabel('meta', { platform: 'mac' })}`\r\n\r\n if (typeof combo !== 'string' || !combo.trim()) {\r\n return { ok: false, reasons: [{ code: 'invalid', severity: 'error', message: 'No keys were recorded.' }], lookupKeys }\r\n }\r\n\r\n for (const platform of platforms) {\r\n let parsed\r\n try {\r\n parsed = parseComboString(combo, platform)\r\n } catch {\r\n parsed = null\r\n }\r\n if (!parsed) {\r\n add({ code: 'invalid', severity: 'error', message: 'These keys could not be read.', platforms: [platform] })\r\n continue\r\n }\r\n if (!parsed.base) {\r\n add({ code: 'no-key', severity: 'error', message: 'Add a key to the modifiers, for example a letter or punctuation key.', platforms: [platform] })\r\n continue\r\n }\r\n const mods = ['ctrl', 'meta', 'alt', 'shift'].filter((m) => parsed[m])\r\n const key = [...mods, parsed.base].join('+')\r\n lookupKeys[platform] = key\r\n\r\n if (mods.length === 0) {\r\n add({ code: 'no-modifier', severity: 'error', message: `Use at least one modifier (${anyModifier}), or the key would fire while typing.`, platforms: [platform] })\r\n }\r\n if (platform !== 'mac' && mods.length === 1 && parsed.alt) {\r\n 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] })\r\n }\r\n const primary = platform === 'mac' ? parsed.meta : parsed.ctrl\r\n if (requirePrimary && !primary) {\r\n add({ code: 'needs-primary', severity: 'error', message: `Include ${modifierLabel('mod', { platform })}.`, platforms: [platform] })\r\n }\r\n if (mods.length > 0 && mods.length < minModifiers) {\r\n add({ code: 'too-few-modifiers', severity: 'error', message: `Use at least ${minModifiers} modifiers together.`, platforms: [platform] })\r\n }\r\n\r\n for (const other of taken) {\r\n let otherKey = null\r\n try {\r\n const o = parseComboString(other, platform)\r\n if (o?.base) otherKey = [...['ctrl', 'meta', 'alt', 'shift'].filter((m) => o[m]), o.base].join('+')\r\n } catch { /* ignore unreadable entries */ }\r\n if (otherKey === key) {\r\n add({ code: 'taken', severity: 'error', message: 'These keys are already used for something else here.', platforms: [platform], details: { combo: other } })\r\n }\r\n }\r\n\r\n const shape = { ctrl: parsed.ctrl, meta: parsed.meta, alt: parsed.alt, shift: parsed.shift, key: parsed.base, code: null, bareModifier: null }\r\n for (const browser of browsers) {\r\n if (platform !== 'mac' && browser === 'safari') continue\r\n const hit = lookupReservation(shape, { platform, browser })\r\n if (!hit) continue\r\n const where = hit.source === 'system' ? platformLabel(platform) : `${browserLabel(browser)} on ${platformLabel(platform)}`\r\n if (severityIsHard(hit.severity)) {\r\n 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 } })\r\n } else {\r\n add({ code: 'reserved-soft', severity: 'warning', message: `${where} uses these keys in some situations (${hit.action}).`, platforms: [platform], details: { browser, action: hit.action, severity: hit.severity } })\r\n }\r\n }\r\n\r\n if (platform === 'windows' && parsed.ctrl && parsed.alt && !parsed.meta) {\r\n 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] })\r\n }\r\n\r\n if (sites) {\r\n const col = SITE_COL[platform]\r\n const names = []\r\n for (const site of listSites()) {\r\n if (site.shortcuts.some((row) => row[col] === key)) names.push(site.name)\r\n }\r\n if (names.length) {\r\n add({ code: 'site-conflict', severity: 'warning', message: `Also used by ${names.join(', ')}.`, platforms: [platform], details: { sites: names } })\r\n }\r\n }\r\n }\r\n\r\n return { ok: !reasons.some((r) => r.severity === 'error'), reasons, lookupKeys }\r\n}\r\n"],
|
|
5
|
+
"mappings": ";;;;AAiBA,SAAS,mBAAmB,sBAAsB;AAClD,SAAS,kBAAkB,iBAAiB;AAC5C,SAAS,eAAe,eAAe,eAAe,oBAAoB;AAE1E,IAAM,gBAAgB,CAAC,OAAO,WAAW,OAAO;AAChD,IAAM,eAAe,CAAC,UAAU,WAAW,QAAQ,QAAQ;AAE3D,IAAM,WAAW,EAAE,SAAS,GAAG,KAAK,GAAG,OAAO,EAAE;AA0BzC,SAAS,cAAc,OAAO;AAAA,EACnC,YAAY;AAAA,EACZ,WAAW;AAAA,EACX,iBAAiB;AAAA,EACjB,eAAe;AAAA,EACf,QAAQ,CAAC;AAAA,EACT,QAAQ;AACV,IAAI,CAAC,GAAG;AAEN,QAAM,UAAU,CAAC;AACjB,QAAM,aAAa,CAAC;AACpB,QAAM,MAAM,CAAC,WAAW;AACtB,UAAM,OAAO,QAAQ,KAAK,CAAC,MAAM,EAAE,SAAS,OAAO,QAAQ,EAAE,YAAY,OAAO,OAAO;AACvF,QAAI,MAAM;AACR,iBAA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YAAM,MAAM,SAAS,QAAQ;AAC7B,YAAM,QAAQ,CAAC;AACf,iBAAW,QAAQ,UAAU,GAAG;AAC9B,YAAI,KAAK,UAAU,KAAK,CAAC,QAAQ,IAAI,GAAG,MAAM,GAAG,EAAG,OAAM,KAAK,KAAK,IAAI;AAAA,MAC1E;AACA,UAAI,MAAM,QAAQ;AAChB,YAAI,EAAE,MAAM,iBAAiB,UAAU,WAAW,SAAS,gBAAgB,MAAM,KAAK,IAAI,CAAC,KAAK,WAAW,CAAC,QAAQ,GAAG,SAAS,EAAE,OAAO,MAAM,EAAE,CAAC;AAAA,MACpJ;AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,IAAI,CAAC,QAAQ,KAAK,CAAC,MAAM,EAAE,aAAa,OAAO,GAAG,SAAS,WAAW;AACjF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hotkey-router",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "A tiny, deterministic keyboard routing engine for modern web apps.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "WATT3D <WATT3D@protonmail.com>",
|
|
@@ -37,6 +37,10 @@
|
|
|
37
37
|
"types": "./dist/types/validate.d.ts",
|
|
38
38
|
"import": "./dist/validate.js"
|
|
39
39
|
},
|
|
40
|
+
"./format": {
|
|
41
|
+
"types": "./dist/types/format.d.ts",
|
|
42
|
+
"import": "./dist/format.js"
|
|
43
|
+
},
|
|
40
44
|
"./data/browser-hotkeys.json": "./data/browser-hotkeys.json",
|
|
41
45
|
"./data/site-hotkeys.json": "./data/site-hotkeys.json",
|
|
42
46
|
"./data/site-hotkeys.runtime.json": "./data/site-hotkeys.runtime.json"
|