hotkey-router 0.1.0 → 0.2.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 +113 -4
- package/data/browser-hotkeys.json +349 -0
- package/dist/auto.js +967 -0
- package/dist/auto.js.map +7 -0
- package/dist/auto.min.js +3 -0
- package/dist/hotkey-router.cjs +40 -17
- package/dist/hotkey-router.cjs.map +2 -2
- package/dist/hotkey-router.js +40 -17
- package/dist/hotkey-router.js.map +2 -2
- package/dist/hotkey-router.min.js +2 -2
- package/dist/reservations.js +528 -0
- package/dist/reservations.js.map +7 -0
- package/package.json +14 -4
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../hotkey-router.js"],
|
|
4
|
-
"sourcesContent": ["// hotkey-router.js\n// Hotkey Router \u2014 plugin-first keyboard shortcut router (tiny + predictable)\n//\n// Goals:\n// - O(1) dispatch by combo string lookup\n// - Deterministic routing: highest priority wins; ties -> most recently bound wins\n// - Safe plugin cleanup (never deletes other plugins\u2019 bindings)\n// - Modern browser target (KeyboardEvent.key + addEventListener)\n\n// --- tiny event helper ---\nfunction on(target, type, fn, options) {\n target.addEventListener(type, fn, options)\n return () => target.removeEventListener(type, fn, options)\n}\n\n// --- key alias map ---\nconst isMac =\n typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform)\n\n\nconst modifierTokens = new Set(['ctrl', 'shift', 'alt', 'meta'])\n\n// Map from KeyboardEvent.key (which IS the modifier name on bare-modifier events)\n// to the canonical token used in combo strings. Used to detect bare-modifier\n// keydown/keyup events at dispatch time.\nconst MODIFIER_KEY_TOKEN = Object.freeze({\n Alt: 'alt',\n AltGraph: 'alt',\n Control: 'ctrl',\n Shift: 'shift',\n Meta: 'meta',\n OS: 'meta',\n})\n\nconst ahkPrefixMap = new Map([\n ['^', 'ctrl'], // Ctrl\n ['!', 'alt'], // Alt\n ['+', 'shift'], // Shift\n ['#', 'meta'], // Win / Command\n])\n\n\nconst keyAliases = Object.freeze({\n // common\n esc: 'escape',\n escape: 'escape',\n space: ' ',\n spacebar: ' ',\n enter: 'enter',\n return: 'enter',\n del: 'delete',\n delete: 'delete',\n backspace: 'backspace',\n tab: 'tab',\n\n // arrows\n up: 'arrowup',\n down: 'arrowdown',\n left: 'arrowleft',\n right: 'arrowright',\n\n // navigation\n home: 'home',\n end: 'end',\n pageup: 'pageup',\n pagedown: 'pagedown',\n pgup: 'pageup',\n pgdn: 'pagedown',\n\n // modifiers (words)\n cmd: 'meta',\n command: 'meta',\n win: 'meta',\n meta: 'meta',\n control: 'ctrl',\n ctrl: 'ctrl',\n option: 'alt',\n alt: 'alt',\n shift: 'shift',\n\n // modifiers (symbols)\n '\u2318': 'meta',\n '\u2303': 'ctrl',\n '\u2325': 'alt',\n '\u21E7': 'shift',\n\n // cross-platform \"mod\"\n mod: isMac ? 'meta' : 'ctrl',\n\n // plus key ergonomics\n plus: '+',\n})\n\n// --- config ---\nlet ignoreEditable = true\nlet defaultTarget = typeof window !== 'undefined' ? window : null\n\n// --- state ---\n// comboStr -> Map<type, Array<Binding>>\nconst registry = new Map()\n// pluginName -> Array<bindingId>\nconst plugins = new Map()\n// bindingId -> { comboStr, type }\nconst bindingIndex = new Map()\n\nlet paused = false\nlet nextId = 1\n\n// --- helpers ---\nfunction normalizeKey(key) {\n const raw = String(key ?? '').trim()\n const k = raw.toLowerCase()\n return keyAliases[k] ?? (raw === ' ' ? ' ' : k)\n}\n\nfunction comboKeyFromParts({ ctrl, shift, alt, meta, key, code, bareModifier }) {\n // Bare-modifier bindings get a sentinel namespace so they can't collide with\n // regular chord combo strings (`'alt+x'` vs `'__bare__:alt'`).\n if (bareModifier) return `__bare__:${bareModifier}`\n\n let s = ''\n if (ctrl) s += 'ctrl+'\n if (shift) s += 'shift+'\n if (alt) s += 'alt+'\n if (meta) s += 'meta+'\n\n // Code-based combos use a `code:KeyX` suffix instead of the key character.\n if (code) return s + 'code:' + code\n return s ? s + key : key\n}\n\nfunction comboKeyFromEvent(e) {\n // If the event's `key` is itself a modifier name, this is a bare-modifier\n // press/release. Match it via the bare-modifier sentinel regardless of which\n // other modifier flags are set (loose match \u2014 Alt fires `alt` even when held\n // alongside Ctrl).\n const bareMod = MODIFIER_KEY_TOKEN[e.key]\n if (bareMod) return `__bare__:${bareMod}`\n\n return comboKeyFromParts({\n ctrl: e.ctrlKey,\n shift: e.shiftKey,\n alt: e.altKey,\n meta: e.metaKey,\n key: normalizeKey(e.key),\n })\n}\n\n// Code-based lookup string for an event. Independent of `comboKeyFromEvent`\n// because consumers can register either key-based OR code-based bindings (or\n// both) for the same physical key combo, and we look up under whichever they\n// chose.\nfunction codeKeyFromEvent(e) {\n if (!e.code) return null\n\n let s = ''\n if (e.ctrlKey) s += 'ctrl+'\n if (e.shiftKey) s += 'shift+'\n if (e.altKey) s += 'alt+'\n if (e.metaKey) s += 'meta+'\n return s + 'code:' + e.code\n}\n\n\nfunction isModifierToken(token) {\n const t = normalizeKey(token)\n return modifierTokens.has(t)\n}\n\n\n// Matches `code:KeyX` (case-sensitive value). Used to extract code tokens BEFORE\n// the rest of the parser lowercases everything, since KeyboardEvent.code values\n// are camelCase (KeyA, Digit1, ArrowLeft) and must be preserved.\nconst CODE_TOKEN_RE = /(^|\\+)code:([A-Za-z][A-Za-z0-9_]*)/\n\nfunction parseHotkey(hotkeyStr) {\n const raw = String(hotkeyStr || '').trim()\n\n // Detect ' up' suffix case-insensitively so 'Alt UP' works the same as 'alt up'.\n const isUp = /\\s+up$/i.test(raw)\n const baseRaw = isUp ? raw.replace(/\\s+up$/i, '').trim() : raw\n\n // --- AHK-style prefix modifiers ---\n // Examples:\n // ^k => ctrl+k\n // !k => alt+k\n // +k => shift+k\n // #k => meta+k\n // ^!k => ctrl+alt+k\n // !code:KeyX => alt+code:KeyX\n //\n // We strip these BEFORE both the code-token extraction AND the lowercase\n // pass, because the AHK prefix chars (^ ! + #) are case-stable, and the\n // remainder may contain a case-sensitive `code:KeyX` token that must survive.\n const combo = {\n ctrl: false, shift: false, alt: false, meta: false,\n key: null, code: null, bareModifier: null,\n }\n\n let stripped = baseRaw\n while (stripped.length) {\n const mod = ahkPrefixMap.get(stripped[0]) ?? null\n if (!mod) break\n combo[mod] = true\n stripped = stripped.slice(1).trimStart()\n }\n\n // Pull out a code:XXX token before lowercasing; preserve the camelCase value.\n // Reject multiple code: tokens to avoid silent loss of intent.\n const codeMatches = stripped.match(/code:[A-Za-z][A-Za-z0-9_]*/g)\n if (codeMatches && codeMatches.length > 1) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": multiple code: tokens not supported (got ${codeMatches.length}).`\n )\n }\n let codeValue = null\n const scrubbed = stripped.replace(CODE_TOKEN_RE, (_, sep, val) => {\n codeValue = val\n return sep + '__code__'\n })\n\n let cleanStr = scrubbed.toLowerCase()\n\n // Allow plus key:\n // - \"ctrl++\" => ctrl + \"+\"\n // - \"++\" => shift + \"+\" (AHK '+' prefix sets shift, remaining '+' becomes the base key)\n // `split('+')` produces empties; we keep that signal.\n const rawParts = cleanStr.split('+').map((p) => p.trim())\n const parts = rawParts.filter(Boolean)\n const impliedPlusKey = rawParts.some((p) => p === '') && cleanStr.includes('++')\n\n // Enforce: modifiers must come before the base key.\n // Example: \"ctrl+k\" \u2705 | \"k+ctrl\" \u274C\n // We ignore the special \"+ key\" case (ctrl++) since it has no explicit base token.\n if (!impliedPlusKey) {\n let seenBase = false\n for (const part of parts) {\n if (isModifierToken(part)) {\n if (seenBase) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": modifiers must come before the base key (e.g. \"ctrl+k\", not \"k+ctrl\")`\n )\n }\n } else {\n if (!seenBase) seenBase = true\n }\n }\n }\n\n // Continue parsing the remaining tokens (words/symbols/aliases)\n for (const part of parts) {\n if (part === '__code__') {\n combo.code = codeValue\n continue\n }\n const actual = keyAliases[part] ?? part\n if (actual === 'ctrl') combo.ctrl = true\n else if (actual === 'shift') combo.shift = true\n else if (actual === 'alt') combo.alt = true\n else if (actual === 'meta') combo.meta = true\n else combo.key = normalizeKey(actual)\n }\n\n if (!combo.key && !combo.code && impliedPlusKey) combo.key = '+'\n\n // If no base key/code is set, this might be a bare-modifier binding ('alt' / 'ctrl').\n // Allow exactly ONE modifier with no other tokens. Multi-modifier bare bindings\n // (e.g. 'ctrl+alt') are intentionally rejected \u2014 add a base key for those.\n if (!combo.key && !combo.code) {\n const modCount =\n (combo.ctrl ? 1 : 0) + (combo.shift ? 1 : 0) +\n (combo.alt ? 1 : 0) + (combo.meta ? 1 : 0)\n\n if (modCount === 1) {\n combo.bareModifier =\n combo.ctrl ? 'ctrl' :\n combo.shift ? 'shift' :\n combo.alt ? 'alt' : 'meta'\n } else if (modCount > 1) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": multi-modifier bare bindings not supported. Add a base key (e.g. \"ctrl+alt+x\").`\n )\n } else {\n throw new Error(`Invalid hotkey \"${hotkeyStr}\" (missing base key)`)\n }\n }\n\n return { combo, type: isUp ? 'keyup' : 'keydown', raw: hotkeyStr }\n}\n\n\nfunction isFromInputTarget(e) {\n const t = e?.target\n if (!t) return false\n const tag = String(t.tagName || '').toUpperCase()\n return (\n t.isContentEditable ||\n tag === 'INPUT' ||\n tag === 'TEXTAREA' ||\n tag === 'SELECT' ||\n t.getAttribute?.('role') === 'textbox'\n )\n}\n\n\n// --- binding model ---\n// options:\n// preventDefault, stopPropagation, stopImmediatePropagation\n// repeat (default false on keydown), once\n// when(e) -> boolean (gate)\n// allowIn(e) -> boolean (override ignoreInput)\n// priority (higher wins)\nfunction normalizeOptions(type, options) {\n const o = options ? { ...options } : {}\n if (type === 'keydown' && o.repeat == null) o.repeat = false\n if (o.priority == null) o.priority = 0\n return o\n}\n\n\nfunction ensureSlot(comboStr, type) {\n let typeMap = registry.get(comboStr)\n if (!typeMap) {\n typeMap = new Map()\n registry.set(comboStr, typeMap)\n }\n let arr = typeMap.get(type)\n if (!arr) {\n arr = []\n typeMap.set(type, arr)\n }\n return arr\n}\n\n\nfunction removeById(id) {\n const idx = bindingIndex.get(id)\n if (!idx) return false\n\n const { comboStr, type } = idx\n const typeMap = registry.get(comboStr)\n const arr = typeMap?.get(type)\n if (!arr?.length) {\n bindingIndex.delete(id)\n return false\n }\n\n const next = arr.filter((b) => b.id !== id)\n if (next.length) typeMap.set(type, next)\n else typeMap.delete(type)\n\n if (!typeMap.size) registry.delete(comboStr)\n bindingIndex.delete(id)\n return true\n}\n\n\n// --- public API: bind / unbind ---\n// bind returns an off() function; off.id is exposed for plugin bookkeeping.\nfunction bind(hotkeyStr, handler, plugin = null, options = {}) {\n const { combo, type, raw } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const opts = normalizeOptions(type, options)\n\n const binding = {\n id: nextId++,\n comboStr,\n type,\n combo,\n handler,\n plugin,\n raw,\n options: opts,\n }\n\n ensureSlot(comboStr, type).push(binding)\n bindingIndex.set(binding.id, { comboStr, type })\n\n const off = () => removeById(binding.id)\n off.id = binding.id\n off.hotkey = raw\n return off\n}\n\n\n// unbind('ctrl+k') -> remove all for that combo/type\n// unbind('ctrl+k', fn) -> remove only that handler\nfunction unbind(hotkeyStr, handler) {\n const { combo, type } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const typeMap = registry.get(comboStr)\n if (!typeMap) return\n\n const arr = typeMap.get(type)\n if (!arr?.length) return\n\n if (!handler) {\n for (const b of arr) bindingIndex.delete(b.id)\n typeMap.delete(type)\n } else {\n const keep = []\n for (const b of arr) {\n if (b.handler === handler) bindingIndex.delete(b.id)\n else keep.push(b)\n }\n if (keep.length) typeMap.set(type, keep)\n else typeMap.delete(type)\n }\n\n if (!typeMap.size) registry.delete(comboStr)\n}\n\n\n// --- plugin system ---\n// registerPlugin returns unregister function.\nfunction registerPlugin(name, hotkeyMap) {\n if (plugins.has(name)) throw new Error(`Plugin \"${name}\" already registered`)\n const ids = []\n\n for (const [hotkeyStr, fnOrTuple] of Object.entries(hotkeyMap)) {\n const [fn, options] = Array.isArray(fnOrTuple) ? fnOrTuple : [fnOrTuple, {}]\n const off = bind(hotkeyStr, fn, name, options)\n ids.push(off.id)\n }\n\n plugins.set(name, ids)\n return () => unregisterPlugin(name)\n}\n\nfunction unregisterPlugin(name) {\n const ids = plugins.get(name)\n if (!ids) return\n for (const id of ids) removeById(id)\n plugins.delete(name)\n}\n\n// --- control ---\nfunction pause() {\n paused = true\n}\n\nfunction resume() {\n paused = false\n}\n\nfunction ignoreInput(value = true) {\n ignoreEditable = !!value\n}\n\n// allow changing the default target (iframes/tests).\n// we intentionally do not auto-rebind to keep the core tiny.\nfunction setTarget(target) {\n defaultTarget = target\n}\n\n\n// --- dispatch (O(1) lookup) ---\nfunction pickWinner(bindings) {\n // Highest priority wins; ties -> newest wins (largest id).\n let best = null\n for (const b of bindings) {\n if (!best) best = b\n else if (b.options.priority > best.options.priority) best = b\n else if (b.options.priority === best.options.priority && b.id > best.id) best = b\n }\n return best\n}\n\n\nfunction handleEvent(type) {\n return (e) => {\n if (paused) return\n\n const fromInput = ignoreEditable && isFromInputTarget(e)\n const comboStr = comboKeyFromEvent(e)\n const codeStr = codeKeyFromEvent(e)\n\n // Look up under both lookup keys: key-based (`alt+x`) and code-based\n // (`alt+code:KeyX`). Same physical keypress, two valid binding shapes.\n // Merge candidates and let the existing priority/recency arbiter pick.\n const seen = new Set()\n const candidates = []\n for (const lookupKey of [comboStr, codeStr]) {\n if (!lookupKey || seen.has(lookupKey)) continue\n seen.add(lookupKey)\n\n const bindings = registry.get(lookupKey)?.get(type)\n if (!bindings?.length) continue\n\n for (const b of bindings) {\n const o = b.options\n if (fromInput && !o.allowIn?.(e)) continue\n if (type === 'keydown' && o.repeat === false && e.repeat) continue\n if (o.when && o.when(e) === false) continue\n candidates.push(b)\n }\n }\n if (!candidates.length) return\n\n const winner = pickWinner(candidates)\n const o = winner.options\n\n if (o.preventDefault) e.preventDefault()\n if (o.stopImmediatePropagation) e.stopImmediatePropagation()\n else if (o.stopPropagation) e.stopPropagation()\n\n winner.handler(e)\n\n if (o.once) removeById(winner.id)\n }\n}\n\n\n// --- listeners ---\nlet stopDown = null\nlet stopUp = null\n\nfunction init({ target = defaultTarget, capture = false } = {}) {\n if (!target) throw new Error('hotkeys.init() requires a target (e.g. window)')\n if (stopDown || stopUp) destroy()\n\n stopDown = on(target, 'keydown', handleEvent('keydown'), { capture })\n stopUp = on(target, 'keyup', handleEvent('keyup'), { capture })\n}\n\n\nfunction destroy() {\n stopDown?.()\n stopUp?.()\n stopDown = null\n stopUp = null\n registry.clear()\n plugins.clear()\n bindingIndex.clear()\n}\n\n\n// --- programmatic trigger (tests / automation) ---\n//\n// Reverse map: 'alt' -> 'Alt' (the KeyboardEvent.key value for that bare modifier).\n// Used to populate the synthetic event's `.key` field so consumer handlers that\n// inspect `e.key` see the same value they'd get from a real event.\nconst BARE_MOD_TO_KEY = {\n ctrl: 'Control',\n shift: 'Shift',\n alt: 'Alt',\n meta: 'Meta',\n}\n\nfunction trigger(hotkeyStr, { type: forcedType } = {}) {\n const { combo, type } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const actualType = forcedType || type\n\n const bindings = registry.get(comboStr)?.get(actualType)\n if (!bindings?.length) return false\n\n // Fill `.key` from bareModifier when the binding is bare-modifier-only;\n // otherwise use combo.key. `.code` is set from combo.code for code-based bindings.\n const eventKey = combo.bareModifier\n ? BARE_MOD_TO_KEY[combo.bareModifier]\n : combo.key\n\n // For bare-modifier keyup, real KeyboardEvents have the corresponding\n // modifier flag FALSE (the modifier just released). Mirror that so\n // automation handlers that inspect e.altKey / e.ctrlKey / etc. see the\n // same value they'd see from a real event.\n let altKey = !!combo.alt\n let ctrlKey = !!combo.ctrl\n let shiftKey = !!combo.shift\n let metaKey = !!combo.meta\n if (combo.bareModifier && actualType === 'keyup') {\n if (combo.bareModifier === 'alt') altKey = false\n else if (combo.bareModifier === 'ctrl') ctrlKey = false\n else if (combo.bareModifier === 'shift') shiftKey = false\n else if (combo.bareModifier === 'meta') metaKey = false\n }\n\n const e = {\n type: actualType,\n key: eventKey,\n code: combo.code || null,\n ctrlKey, shiftKey, altKey, metaKey,\n repeat: false,\n preventDefault() { },\n stopPropagation() { },\n stopImmediatePropagation() { },\n target: null,\n }\n\n const winner = pickWinner(\n bindings.filter((b) => (b.options.when ? b.options.when(e) !== false : true))\n )\n if (!winner) return false\n\n winner.handler(e)\n if (winner.options.once) removeById(winner.id)\n return true\n}\n\n\n// Auto-init on window by default (browser only)\nif (defaultTarget) init()\n\nexport default {\n // lifecycle\n init,\n destroy,\n setTarget,\n\n // bindings\n bind,\n unbind,\n registerPlugin,\n unregisterPlugin,\n\n // control\n pause,\n resume,\n ignoreInput,\n\n // testing / automation\n trigger,\n}"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["// hotkey-router.js\n// Hotkey Router \u2014 plugin-first keyboard shortcut router (tiny + predictable)\n//\n// Goals:\n// - O(1) dispatch by combo string lookup\n// - Deterministic routing: highest priority wins; ties -> most recently bound wins\n// - Safe plugin cleanup (never deletes other plugins\u2019 bindings)\n// - Modern browser target (KeyboardEvent.key + addEventListener)\n//\n// This core ships ZERO reservation data \u2014 opt in via `hotkey-router/auto`\n// or by calling `installReservationWarnings(hotkeys)` from the separate\n// `hotkey-router/reservations` module. Bundlers tree-shake the data file\n// out entirely when neither is imported.\n\n// --- tiny event helper ---\nfunction on(target, type, fn, options) {\n target.addEventListener(type, fn, options)\n return () => target.removeEventListener(type, fn, options)\n}\n\n// --- key alias map ---\nconst isMac =\n typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform)\n\n\nconst modifierTokens = new Set(['ctrl', 'shift', 'alt', 'meta'])\n\n// Map from KeyboardEvent.key (which IS the modifier name on bare-modifier events)\n// to the canonical token used in combo strings. Used to detect bare-modifier\n// keydown/keyup events at dispatch time.\nconst MODIFIER_KEY_TOKEN = Object.freeze({\n Alt: 'alt',\n AltGraph: 'alt',\n Control: 'ctrl',\n Shift: 'shift',\n Meta: 'meta',\n OS: 'meta',\n})\n\nconst ahkPrefixMap = new Map([\n ['^', 'ctrl'], // Ctrl\n ['!', 'alt'], // Alt\n ['+', 'shift'], // Shift\n ['#', 'meta'], // Win / Command\n])\n\n\nconst keyAliases = Object.freeze({\n // common\n esc: 'escape',\n escape: 'escape',\n space: ' ',\n spacebar: ' ',\n enter: 'enter',\n return: 'enter',\n del: 'delete',\n delete: 'delete',\n backspace: 'backspace',\n tab: 'tab',\n\n // arrows\n up: 'arrowup',\n down: 'arrowdown',\n left: 'arrowleft',\n right: 'arrowright',\n\n // navigation\n home: 'home',\n end: 'end',\n pageup: 'pageup',\n pagedown: 'pagedown',\n pgup: 'pageup',\n pgdn: 'pagedown',\n\n // modifiers (words)\n cmd: 'meta',\n command: 'meta',\n win: 'meta',\n meta: 'meta',\n control: 'ctrl',\n ctrl: 'ctrl',\n option: 'alt',\n alt: 'alt',\n shift: 'shift',\n\n // modifiers (symbols)\n '\u2318': 'meta',\n '\u2303': 'ctrl',\n '\u2325': 'alt',\n '\u21E7': 'shift',\n\n // cross-platform \"mod\"\n mod: isMac ? 'meta' : 'ctrl',\n\n // plus key ergonomics\n plus: '+',\n})\n\n// --- config ---\nlet ignoreEditable = true\nlet defaultTarget = typeof window !== 'undefined' ? window : null\n\n// Bind hooks: subscribers called after each successful bind. Used by the\n// optional reservations module to emit conflict warnings without coupling\n// the core router to the (~5 KB) reservation table.\nlet bindHooks = []\n\n// --- state ---\n// comboStr -> Map<type, Array<Binding>>\nconst registry = new Map()\n// pluginName -> Array<bindingId>\nconst plugins = new Map()\n// bindingId -> { comboStr, type }\nconst bindingIndex = new Map()\n\nlet paused = false\nlet nextId = 1\n\n// --- helpers ---\nfunction normalizeKey(key) {\n const raw = String(key ?? '').trim()\n const k = raw.toLowerCase()\n return keyAliases[k] ?? (raw === ' ' ? ' ' : k)\n}\n\nfunction comboKeyFromParts({ ctrl, shift, alt, meta, key, code, bareModifier }) {\n // Bare-modifier bindings get a sentinel namespace so they can't collide with\n // regular chord combo strings (`'alt+x'` vs `'__bare__:alt'`).\n if (bareModifier) return `__bare__:${bareModifier}`\n\n let s = ''\n if (ctrl) s += 'ctrl+'\n if (shift) s += 'shift+'\n if (alt) s += 'alt+'\n if (meta) s += 'meta+'\n\n // Code-based combos use a `code:KeyX` suffix instead of the key character.\n if (code) return s + 'code:' + code\n return s ? s + key : key\n}\n\nfunction comboKeyFromEvent(e) {\n // If the event's `key` is itself a modifier name, this is a bare-modifier\n // press/release. Match it via the bare-modifier sentinel regardless of which\n // other modifier flags are set (loose match \u2014 Alt fires `alt` even when held\n // alongside Ctrl).\n const bareMod = MODIFIER_KEY_TOKEN[e.key]\n if (bareMod) return `__bare__:${bareMod}`\n\n return comboKeyFromParts({\n ctrl: e.ctrlKey,\n shift: e.shiftKey,\n alt: e.altKey,\n meta: e.metaKey,\n key: normalizeKey(e.key),\n })\n}\n\n// Code-based lookup string for an event. Independent of `comboKeyFromEvent`\n// because consumers can register either key-based OR code-based bindings (or\n// both) for the same physical key combo, and we look up under whichever they\n// chose.\nfunction codeKeyFromEvent(e) {\n if (!e.code) return null\n\n let s = ''\n if (e.ctrlKey) s += 'ctrl+'\n if (e.shiftKey) s += 'shift+'\n if (e.altKey) s += 'alt+'\n if (e.metaKey) s += 'meta+'\n return s + 'code:' + e.code\n}\n\n\nfunction isModifierToken(token) {\n const t = normalizeKey(token)\n return modifierTokens.has(t)\n}\n\n\n// Matches `code:KeyX` (case-sensitive value). Used to extract code tokens BEFORE\n// the rest of the parser lowercases everything, since KeyboardEvent.code values\n// are camelCase (KeyA, Digit1, ArrowLeft) and must be preserved.\nconst CODE_TOKEN_RE = /(^|\\+)code:([A-Za-z][A-Za-z0-9_]*)/\n\nfunction parseHotkey(hotkeyStr) {\n const raw = String(hotkeyStr || '').trim()\n\n // Detect ' up' suffix case-insensitively so 'Alt UP' works the same as 'alt up'.\n const isUp = /\\s+up$/i.test(raw)\n const baseRaw = isUp ? raw.replace(/\\s+up$/i, '').trim() : raw\n\n // --- AHK-style prefix modifiers ---\n // Examples:\n // ^k => ctrl+k\n // !k => alt+k\n // +k => shift+k\n // #k => meta+k\n // ^!k => ctrl+alt+k\n // !code:KeyX => alt+code:KeyX\n //\n // We strip these BEFORE both the code-token extraction AND the lowercase\n // pass, because the AHK prefix chars (^ ! + #) are case-stable, and the\n // remainder may contain a case-sensitive `code:KeyX` token that must survive.\n const combo = {\n ctrl: false, shift: false, alt: false, meta: false,\n key: null, code: null, bareModifier: null,\n }\n\n let stripped = baseRaw\n while (stripped.length) {\n const mod = ahkPrefixMap.get(stripped[0]) ?? null\n if (!mod) break\n combo[mod] = true\n stripped = stripped.slice(1).trimStart()\n }\n\n // Pull out a code:XXX token before lowercasing; preserve the camelCase value.\n // Reject multiple code: tokens to avoid silent loss of intent.\n const codeMatches = stripped.match(/code:[A-Za-z][A-Za-z0-9_]*/g)\n if (codeMatches && codeMatches.length > 1) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": multiple code: tokens not supported (got ${codeMatches.length}).`\n )\n }\n let codeValue = null\n const scrubbed = stripped.replace(CODE_TOKEN_RE, (_, sep, val) => {\n codeValue = val\n return sep + '__code__'\n })\n\n let cleanStr = scrubbed.toLowerCase()\n\n // Allow plus key:\n // - \"ctrl++\" => ctrl + \"+\"\n // - \"++\" => shift + \"+\" (AHK '+' prefix sets shift, remaining '+' becomes the base key)\n // `split('+')` produces empties; we keep that signal.\n const rawParts = cleanStr.split('+').map((p) => p.trim())\n const parts = rawParts.filter(Boolean)\n const impliedPlusKey = rawParts.some((p) => p === '') && cleanStr.includes('++')\n\n // Enforce: modifiers must come before the base key.\n // Example: \"ctrl+k\" \u2705 | \"k+ctrl\" \u274C\n // We ignore the special \"+ key\" case (ctrl++) since it has no explicit base token.\n if (!impliedPlusKey) {\n let seenBase = false\n for (const part of parts) {\n if (isModifierToken(part)) {\n if (seenBase) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": modifiers must come before the base key (e.g. \"ctrl+k\", not \"k+ctrl\")`\n )\n }\n } else {\n if (!seenBase) seenBase = true\n }\n }\n }\n\n // Continue parsing the remaining tokens (words/symbols/aliases)\n for (const part of parts) {\n if (part === '__code__') {\n combo.code = codeValue\n continue\n }\n const actual = keyAliases[part] ?? part\n if (actual === 'ctrl') combo.ctrl = true\n else if (actual === 'shift') combo.shift = true\n else if (actual === 'alt') combo.alt = true\n else if (actual === 'meta') combo.meta = true\n else combo.key = normalizeKey(actual)\n }\n\n if (!combo.key && !combo.code && impliedPlusKey) combo.key = '+'\n\n // If no base key/code is set, this might be a bare-modifier binding ('alt' / 'ctrl').\n // Allow exactly ONE modifier with no other tokens. Multi-modifier bare bindings\n // (e.g. 'ctrl+alt') are intentionally rejected \u2014 add a base key for those.\n if (!combo.key && !combo.code) {\n const modCount =\n (combo.ctrl ? 1 : 0) + (combo.shift ? 1 : 0) +\n (combo.alt ? 1 : 0) + (combo.meta ? 1 : 0)\n\n if (modCount === 1) {\n combo.bareModifier =\n combo.ctrl ? 'ctrl' :\n combo.shift ? 'shift' :\n combo.alt ? 'alt' : 'meta'\n } else if (modCount > 1) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": multi-modifier bare bindings not supported. Add a base key (e.g. \"ctrl+alt+x\").`\n )\n } else {\n throw new Error(`Invalid hotkey \"${hotkeyStr}\" (missing base key)`)\n }\n }\n\n return { combo, type: isUp ? 'keyup' : 'keydown', raw: hotkeyStr }\n}\n\n\nfunction isFromInputTarget(e) {\n const t = e?.target\n if (!t) return false\n const tag = String(t.tagName || '').toUpperCase()\n return (\n t.isContentEditable ||\n tag === 'INPUT' ||\n tag === 'TEXTAREA' ||\n tag === 'SELECT' ||\n t.getAttribute?.('role') === 'textbox'\n )\n}\n\n\n// --- binding model ---\n// options:\n// preventDefault, stopPropagation, stopImmediatePropagation\n// repeat (default false on keydown), once\n// when(e) -> boolean (gate)\n// allowIn(e) -> boolean (override ignoreInput)\n// priority (higher wins)\nfunction normalizeOptions(type, options) {\n const o = options ? { ...options } : {}\n if (type === 'keydown' && o.repeat == null) o.repeat = false\n if (o.priority == null) o.priority = 0\n return o\n}\n\n\nfunction ensureSlot(comboStr, type) {\n let typeMap = registry.get(comboStr)\n if (!typeMap) {\n typeMap = new Map()\n registry.set(comboStr, typeMap)\n }\n let arr = typeMap.get(type)\n if (!arr) {\n arr = []\n typeMap.set(type, arr)\n }\n return arr\n}\n\n\nfunction removeById(id) {\n const idx = bindingIndex.get(id)\n if (!idx) return false\n\n const { comboStr, type } = idx\n const typeMap = registry.get(comboStr)\n const arr = typeMap?.get(type)\n if (!arr?.length) {\n bindingIndex.delete(id)\n return false\n }\n\n const next = arr.filter((b) => b.id !== id)\n if (next.length) typeMap.set(type, next)\n else typeMap.delete(type)\n\n if (!typeMap.size) registry.delete(comboStr)\n bindingIndex.delete(id)\n return true\n}\n\n\n// --- public API: bind / unbind ---\n// bind returns an off() function; off.id is exposed for plugin bookkeeping.\nfunction bind(hotkeyStr, handler, plugin = null, options = {}) {\n const { combo, type, raw } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const opts = normalizeOptions(type, options)\n\n const binding = {\n id: nextId++,\n comboStr,\n type,\n combo,\n handler,\n plugin,\n raw,\n options: opts,\n }\n\n ensureSlot(comboStr, type).push(binding)\n bindingIndex.set(binding.id, { comboStr, type })\n\n // Notify bind subscribers. Hook errors must never break the bind itself.\n if (bindHooks.length) {\n for (const hook of bindHooks) {\n try {\n hook({ combo, raw, options: opts, plugin, id: binding.id })\n } catch (err) {\n // eslint-disable-next-line no-console\n console.error('[hotkey-router] onBind hook threw:', err)\n }\n }\n }\n\n const off = () => removeById(binding.id)\n off.id = binding.id\n off.hotkey = raw\n return off\n}\n\n// Subscribe to bind events. The hook receives\n// { combo, raw, options, plugin, id }\n// after the binding is registered. Returns an unsubscribe function. Used by\n// extensions like `installReservationWarnings` from `hotkey-router/reservations`.\nfunction onBind(hook) {\n if (typeof hook !== 'function') {\n throw new TypeError('hotkeys.onBind(hook) expects a function')\n }\n bindHooks.push(hook)\n return () => {\n bindHooks = bindHooks.filter((h) => h !== hook)\n }\n}\n\n\n// unbind('ctrl+k') -> remove all for that combo/type\n// unbind('ctrl+k', fn) -> remove only that handler\nfunction unbind(hotkeyStr, handler) {\n const { combo, type } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const typeMap = registry.get(comboStr)\n if (!typeMap) return\n\n const arr = typeMap.get(type)\n if (!arr?.length) return\n\n if (!handler) {\n for (const b of arr) bindingIndex.delete(b.id)\n typeMap.delete(type)\n } else {\n const keep = []\n for (const b of arr) {\n if (b.handler === handler) bindingIndex.delete(b.id)\n else keep.push(b)\n }\n if (keep.length) typeMap.set(type, keep)\n else typeMap.delete(type)\n }\n\n if (!typeMap.size) registry.delete(comboStr)\n}\n\n\n// --- plugin system ---\n// registerPlugin returns unregister function.\nfunction registerPlugin(name, hotkeyMap) {\n if (plugins.has(name)) throw new Error(`Plugin \"${name}\" already registered`)\n const ids = []\n\n for (const [hotkeyStr, fnOrTuple] of Object.entries(hotkeyMap)) {\n const [fn, options] = Array.isArray(fnOrTuple) ? fnOrTuple : [fnOrTuple, {}]\n const off = bind(hotkeyStr, fn, name, options)\n ids.push(off.id)\n }\n\n plugins.set(name, ids)\n return () => unregisterPlugin(name)\n}\n\nfunction unregisterPlugin(name) {\n const ids = plugins.get(name)\n if (!ids) return\n for (const id of ids) removeById(id)\n plugins.delete(name)\n}\n\n// --- control ---\nfunction pause() {\n paused = true\n}\n\nfunction resume() {\n paused = false\n}\n\nfunction ignoreInput(value = true) {\n ignoreEditable = !!value\n}\n\n// allow changing the default target (iframes/tests).\n// we intentionally do not auto-rebind to keep the core tiny.\nfunction setTarget(target) {\n defaultTarget = target\n}\n\n\n// --- dispatch (O(1) lookup) ---\nfunction pickWinner(bindings) {\n // Highest priority wins; ties -> newest wins (largest id).\n let best = null\n for (const b of bindings) {\n if (!best) best = b\n else if (b.options.priority > best.options.priority) best = b\n else if (b.options.priority === best.options.priority && b.id > best.id) best = b\n }\n return best\n}\n\n\nfunction handleEvent(type) {\n return (e) => {\n if (paused) return\n\n const fromInput = ignoreEditable && isFromInputTarget(e)\n const comboStr = comboKeyFromEvent(e)\n const codeStr = codeKeyFromEvent(e)\n\n // Look up under both lookup keys: key-based (`alt+x`) and code-based\n // (`alt+code:KeyX`). Same physical keypress, two valid binding shapes.\n // Merge candidates and let the existing priority/recency arbiter pick.\n const seen = new Set()\n const candidates = []\n for (const lookupKey of [comboStr, codeStr]) {\n if (!lookupKey || seen.has(lookupKey)) continue\n seen.add(lookupKey)\n\n const bindings = registry.get(lookupKey)?.get(type)\n if (!bindings?.length) continue\n\n for (const b of bindings) {\n const o = b.options\n if (fromInput && !o.allowIn?.(e)) continue\n if (type === 'keydown' && o.repeat === false && e.repeat) continue\n if (o.when && o.when(e) === false) continue\n candidates.push(b)\n }\n }\n if (!candidates.length) return\n\n const winner = pickWinner(candidates)\n const o = winner.options\n\n if (o.preventDefault) e.preventDefault()\n if (o.stopImmediatePropagation) e.stopImmediatePropagation()\n else if (o.stopPropagation) e.stopPropagation()\n\n winner.handler(e)\n\n if (o.once) removeById(winner.id)\n }\n}\n\n\n// --- listeners ---\nlet stopDown = null\nlet stopUp = null\n\nfunction init({ target = defaultTarget, capture = false } = {}) {\n if (!target) throw new Error('hotkeys.init() requires a target (e.g. window)')\n if (stopDown || stopUp) destroy()\n\n stopDown = on(target, 'keydown', handleEvent('keydown'), { capture })\n stopUp = on(target, 'keyup', handleEvent('keyup'), { capture })\n}\n\n\nfunction destroy() {\n stopDown?.()\n stopUp?.()\n stopDown = null\n stopUp = null\n registry.clear()\n plugins.clear()\n bindingIndex.clear()\n // Note: bindHooks deliberately survive destroy(). Consumer extensions\n // (reservations, future telemetry, etc.) install once at startup and\n // shouldn't have to re-attach when the router restarts. Tests that need\n // a clean slate should use the uninstall fn returned by their installer.\n}\n\n\n// --- programmatic trigger (tests / automation) ---\n//\n// Reverse map: 'alt' -> 'Alt' (the KeyboardEvent.key value for that bare modifier).\n// Used to populate the synthetic event's `.key` field so consumer handlers that\n// inspect `e.key` see the same value they'd get from a real event.\nconst BARE_MOD_TO_KEY = {\n ctrl: 'Control',\n shift: 'Shift',\n alt: 'Alt',\n meta: 'Meta',\n}\n\nfunction trigger(hotkeyStr, { type: forcedType } = {}) {\n const { combo, type } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const actualType = forcedType || type\n\n const bindings = registry.get(comboStr)?.get(actualType)\n if (!bindings?.length) return false\n\n // Fill `.key` from bareModifier when the binding is bare-modifier-only;\n // otherwise use combo.key. `.code` is set from combo.code for code-based bindings.\n const eventKey = combo.bareModifier\n ? BARE_MOD_TO_KEY[combo.bareModifier]\n : combo.key\n\n // For bare-modifier keyup, real KeyboardEvents have the corresponding\n // modifier flag FALSE (the modifier just released). Mirror that so\n // automation handlers that inspect e.altKey / e.ctrlKey / etc. see the\n // same value they'd see from a real event.\n let altKey = !!combo.alt\n let ctrlKey = !!combo.ctrl\n let shiftKey = !!combo.shift\n let metaKey = !!combo.meta\n if (combo.bareModifier && actualType === 'keyup') {\n if (combo.bareModifier === 'alt') altKey = false\n else if (combo.bareModifier === 'ctrl') ctrlKey = false\n else if (combo.bareModifier === 'shift') shiftKey = false\n else if (combo.bareModifier === 'meta') metaKey = false\n }\n\n const e = {\n type: actualType,\n key: eventKey,\n code: combo.code || null,\n ctrlKey, shiftKey, altKey, metaKey,\n repeat: false,\n preventDefault() { },\n stopPropagation() { },\n stopImmediatePropagation() { },\n target: null,\n }\n\n const winner = pickWinner(\n bindings.filter((b) => (b.options.when ? b.options.when(e) !== false : true))\n )\n if (!winner) return false\n\n winner.handler(e)\n if (winner.options.once) removeById(winner.id)\n return true\n}\n\n\n// Auto-init on window by default (browser only)\nif (defaultTarget) init()\n\nexport default {\n // lifecycle\n init,\n destroy,\n setTarget,\n\n // bindings\n bind,\n unbind,\n registerPlugin,\n unregisterPlugin,\n\n // extension hooks\n onBind,\n\n // control\n pause,\n resume,\n ignoreInput,\n\n // testing / automation\n trigger,\n}"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAeA,SAAS,GAAG,QAAQ,MAAM,IAAI,SAAS;AACrC,SAAO,iBAAiB,MAAM,IAAI,OAAO;AACzC,SAAO,MAAM,OAAO,oBAAoB,MAAM,IAAI,OAAO;AAC3D;AAGA,IAAM,QACJ,OAAO,cAAc,eAAe,uBAAuB,KAAK,UAAU,QAAQ;AAGpF,IAAM,iBAAiB,oBAAI,IAAI,CAAC,QAAQ,SAAS,OAAO,MAAM,CAAC;AAK/D,IAAM,qBAAqB,OAAO,OAAO;AAAA,EACvC,KAAK;AAAA,EACL,UAAU;AAAA,EACV,SAAS;AAAA,EACT,OAAO;AAAA,EACP,MAAM;AAAA,EACN,IAAI;AACN,CAAC;AAED,IAAM,eAAe,oBAAI,IAAI;AAAA,EAC3B,CAAC,KAAK,MAAM;AAAA;AAAA,EACZ,CAAC,KAAK,KAAK;AAAA;AAAA,EACX,CAAC,KAAK,OAAO;AAAA;AAAA,EACb,CAAC,KAAK,MAAM;AAAA;AACd,CAAC;AAGD,IAAM,aAAa,OAAO,OAAO;AAAA;AAAA,EAE/B,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,UAAU;AAAA,EACV,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,WAAW;AAAA,EACX,KAAK;AAAA;AAAA,EAGL,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA;AAAA,EAGP,MAAM;AAAA,EACN,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,UAAU;AAAA,EACV,MAAM;AAAA,EACN,MAAM;AAAA;AAAA,EAGN,KAAK;AAAA,EACL,SAAS;AAAA,EACT,KAAK;AA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A,EACpD;AAEA,QAAM,IAAI;AAAA,IACR,MAAM;AAAA,IACN,KAAK;AAAA,IACL,MAAM,MAAM,QAAQ;AAAA,IACpB;AAAA,IAAS;AAAA,IAAU;AAAA,IAAQ;AAAA,IAC3B,QAAQ;AAAA,IACR,iBAAiB;AAAA,IAAE;AAAA,IACnB,kBAAkB;AAAA,IAAE;AAAA,IACpB,2BAA2B;AAAA,IAAE;AAAA,IAC7B,QAAQ;AAAA,EACV;AAEA,QAAM,SAAS;AAAA,IACb,SAAS,OAAO,CAAC,MAAO,EAAE,QAAQ,OAAO,EAAE,QAAQ,KAAK,CAAC,MAAM,QAAQ,IAAK;AAAA,EAC9E;AACA,MAAI,CAAC,OAAQ,QAAO;AAEpB,SAAO,QAAQ,CAAC;AAChB,MAAI,OAAO,QAAQ,KAAM,YAAW,OAAO,EAAE;AAC7C,SAAO;AACT;AAIA,IAAI,cAAe,MAAK;AAExB,IAAO,wBAAQ;AAAA;AAAA,EAEb;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA;AACF;",
|
|
6
6
|
"names": ["o"]
|
|
7
7
|
}
|
package/dist/hotkey-router.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
// hotkey-router v0.
|
|
1
|
+
// hotkey-router v0.2.0 | SEE LICENSE IN LICENSE
|
|
2
2
|
|
|
3
|
+
|
|
4
|
+
// hotkey-router.js
|
|
3
5
|
function on(target, type, fn, options) {
|
|
4
6
|
target.addEventListener(type, fn, options);
|
|
5
7
|
return () => target.removeEventListener(type, fn, options);
|
|
6
8
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
var isMac = typeof navigator !== "undefined" && /Mac|iPhone|iPad|iPod/.test(navigator.platform);
|
|
10
|
+
var modifierTokens = /* @__PURE__ */ new Set(["ctrl", "shift", "alt", "meta"]);
|
|
11
|
+
var MODIFIER_KEY_TOKEN = Object.freeze({
|
|
10
12
|
Alt: "alt",
|
|
11
13
|
AltGraph: "alt",
|
|
12
14
|
Control: "ctrl",
|
|
@@ -14,7 +16,7 @@ const MODIFIER_KEY_TOKEN = Object.freeze({
|
|
|
14
16
|
Meta: "meta",
|
|
15
17
|
OS: "meta"
|
|
16
18
|
});
|
|
17
|
-
|
|
19
|
+
var ahkPrefixMap = /* @__PURE__ */ new Map([
|
|
18
20
|
["^", "ctrl"],
|
|
19
21
|
// Ctrl
|
|
20
22
|
["!", "alt"],
|
|
@@ -24,7 +26,7 @@ const ahkPrefixMap = /* @__PURE__ */ new Map([
|
|
|
24
26
|
["#", "meta"]
|
|
25
27
|
// Win / Command
|
|
26
28
|
]);
|
|
27
|
-
|
|
29
|
+
var keyAliases = Object.freeze({
|
|
28
30
|
// common
|
|
29
31
|
esc: "escape",
|
|
30
32
|
escape: "escape",
|
|
@@ -68,13 +70,14 @@ const keyAliases = Object.freeze({
|
|
|
68
70
|
// plus key ergonomics
|
|
69
71
|
plus: "+"
|
|
70
72
|
});
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
73
|
+
var ignoreEditable = true;
|
|
74
|
+
var defaultTarget = typeof window !== "undefined" ? window : null;
|
|
75
|
+
var bindHooks = [];
|
|
76
|
+
var registry = /* @__PURE__ */ new Map();
|
|
77
|
+
var plugins = /* @__PURE__ */ new Map();
|
|
78
|
+
var bindingIndex = /* @__PURE__ */ new Map();
|
|
79
|
+
var paused = false;
|
|
80
|
+
var nextId = 1;
|
|
78
81
|
function normalizeKey(key) {
|
|
79
82
|
const raw = String(key ?? "").trim();
|
|
80
83
|
const k = raw.toLowerCase();
|
|
@@ -114,7 +117,7 @@ function isModifierToken(token) {
|
|
|
114
117
|
const t = normalizeKey(token);
|
|
115
118
|
return modifierTokens.has(t);
|
|
116
119
|
}
|
|
117
|
-
|
|
120
|
+
var CODE_TOKEN_RE = /(^|\+)code:([A-Za-z][A-Za-z0-9_]*)/;
|
|
118
121
|
function parseHotkey(hotkeyStr) {
|
|
119
122
|
const raw = String(hotkeyStr || "").trim();
|
|
120
123
|
const isUp = /\s+up$/i.test(raw);
|
|
@@ -249,11 +252,29 @@ function bind(hotkeyStr, handler, plugin = null, options = {}) {
|
|
|
249
252
|
};
|
|
250
253
|
ensureSlot(comboStr, type).push(binding);
|
|
251
254
|
bindingIndex.set(binding.id, { comboStr, type });
|
|
255
|
+
if (bindHooks.length) {
|
|
256
|
+
for (const hook of bindHooks) {
|
|
257
|
+
try {
|
|
258
|
+
hook({ combo, raw, options: opts, plugin, id: binding.id });
|
|
259
|
+
} catch (err) {
|
|
260
|
+
console.error("[hotkey-router] onBind hook threw:", err);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
252
264
|
const off = () => removeById(binding.id);
|
|
253
265
|
off.id = binding.id;
|
|
254
266
|
off.hotkey = raw;
|
|
255
267
|
return off;
|
|
256
268
|
}
|
|
269
|
+
function onBind(hook) {
|
|
270
|
+
if (typeof hook !== "function") {
|
|
271
|
+
throw new TypeError("hotkeys.onBind(hook) expects a function");
|
|
272
|
+
}
|
|
273
|
+
bindHooks.push(hook);
|
|
274
|
+
return () => {
|
|
275
|
+
bindHooks = bindHooks.filter((h) => h !== hook);
|
|
276
|
+
};
|
|
277
|
+
}
|
|
257
278
|
function unbind(hotkeyStr, handler) {
|
|
258
279
|
const { combo, type } = parseHotkey(hotkeyStr);
|
|
259
280
|
const comboStr = comboKeyFromParts(combo);
|
|
@@ -344,8 +365,8 @@ function handleEvent(type) {
|
|
|
344
365
|
if (o.once) removeById(winner.id);
|
|
345
366
|
};
|
|
346
367
|
}
|
|
347
|
-
|
|
348
|
-
|
|
368
|
+
var stopDown = null;
|
|
369
|
+
var stopUp = null;
|
|
349
370
|
function init({ target = defaultTarget, capture = false } = {}) {
|
|
350
371
|
if (!target) throw new Error("hotkeys.init() requires a target (e.g. window)");
|
|
351
372
|
if (stopDown || stopUp) destroy();
|
|
@@ -361,7 +382,7 @@ function destroy() {
|
|
|
361
382
|
plugins.clear();
|
|
362
383
|
bindingIndex.clear();
|
|
363
384
|
}
|
|
364
|
-
|
|
385
|
+
var BARE_MOD_TO_KEY = {
|
|
365
386
|
ctrl: "Control",
|
|
366
387
|
shift: "Shift",
|
|
367
388
|
alt: "Alt",
|
|
@@ -420,6 +441,8 @@ var hotkey_router_default = {
|
|
|
420
441
|
unbind,
|
|
421
442
|
registerPlugin,
|
|
422
443
|
unregisterPlugin,
|
|
444
|
+
// extension hooks
|
|
445
|
+
onBind,
|
|
423
446
|
// control
|
|
424
447
|
pause,
|
|
425
448
|
resume,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../hotkey-router.js"],
|
|
4
|
-
"sourcesContent": ["// hotkey-router.js\n// Hotkey Router \u2014 plugin-first keyboard shortcut router (tiny + predictable)\n//\n// Goals:\n// - O(1) dispatch by combo string lookup\n// - Deterministic routing: highest priority wins; ties -> most recently bound wins\n// - Safe plugin cleanup (never deletes other plugins\u2019 bindings)\n// - Modern browser target (KeyboardEvent.key + addEventListener)\n\n// --- tiny event helper ---\nfunction on(target, type, fn, options) {\n target.addEventListener(type, fn, options)\n return () => target.removeEventListener(type, fn, options)\n}\n\n// --- key alias map ---\nconst isMac =\n typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform)\n\n\nconst modifierTokens = new Set(['ctrl', 'shift', 'alt', 'meta'])\n\n// Map from KeyboardEvent.key (which IS the modifier name on bare-modifier events)\n// to the canonical token used in combo strings. Used to detect bare-modifier\n// keydown/keyup events at dispatch time.\nconst MODIFIER_KEY_TOKEN = Object.freeze({\n Alt: 'alt',\n AltGraph: 'alt',\n Control: 'ctrl',\n Shift: 'shift',\n Meta: 'meta',\n OS: 'meta',\n})\n\nconst ahkPrefixMap = new Map([\n ['^', 'ctrl'], // Ctrl\n ['!', 'alt'], // Alt\n ['+', 'shift'], // Shift\n ['#', 'meta'], // Win / Command\n])\n\n\nconst keyAliases = Object.freeze({\n // common\n esc: 'escape',\n escape: 'escape',\n space: ' ',\n spacebar: ' ',\n enter: 'enter',\n return: 'enter',\n del: 'delete',\n delete: 'delete',\n backspace: 'backspace',\n tab: 'tab',\n\n // arrows\n up: 'arrowup',\n down: 'arrowdown',\n left: 'arrowleft',\n right: 'arrowright',\n\n // navigation\n home: 'home',\n end: 'end',\n pageup: 'pageup',\n pagedown: 'pagedown',\n pgup: 'pageup',\n pgdn: 'pagedown',\n\n // modifiers (words)\n cmd: 'meta',\n command: 'meta',\n win: 'meta',\n meta: 'meta',\n control: 'ctrl',\n ctrl: 'ctrl',\n option: 'alt',\n alt: 'alt',\n shift: 'shift',\n\n // modifiers (symbols)\n '\u2318': 'meta',\n '\u2303': 'ctrl',\n '\u2325': 'alt',\n '\u21E7': 'shift',\n\n // cross-platform \"mod\"\n mod: isMac ? 'meta' : 'ctrl',\n\n // plus key ergonomics\n plus: '+',\n})\n\n// --- config ---\nlet ignoreEditable = true\nlet defaultTarget = typeof window !== 'undefined' ? window : null\n\n// --- state ---\n// comboStr -> Map<type, Array<Binding>>\nconst registry = new Map()\n// pluginName -> Array<bindingId>\nconst plugins = new Map()\n// bindingId -> { comboStr, type }\nconst bindingIndex = new Map()\n\nlet paused = false\nlet nextId = 1\n\n// --- helpers ---\nfunction normalizeKey(key) {\n const raw = String(key ?? '').trim()\n const k = raw.toLowerCase()\n return keyAliases[k] ?? (raw === ' ' ? ' ' : k)\n}\n\nfunction comboKeyFromParts({ ctrl, shift, alt, meta, key, code, bareModifier }) {\n // Bare-modifier bindings get a sentinel namespace so they can't collide with\n // regular chord combo strings (`'alt+x'` vs `'__bare__:alt'`).\n if (bareModifier) return `__bare__:${bareModifier}`\n\n let s = ''\n if (ctrl) s += 'ctrl+'\n if (shift) s += 'shift+'\n if (alt) s += 'alt+'\n if (meta) s += 'meta+'\n\n // Code-based combos use a `code:KeyX` suffix instead of the key character.\n if (code) return s + 'code:' + code\n return s ? s + key : key\n}\n\nfunction comboKeyFromEvent(e) {\n // If the event's `key` is itself a modifier name, this is a bare-modifier\n // press/release. Match it via the bare-modifier sentinel regardless of which\n // other modifier flags are set (loose match \u2014 Alt fires `alt` even when held\n // alongside Ctrl).\n const bareMod = MODIFIER_KEY_TOKEN[e.key]\n if (bareMod) return `__bare__:${bareMod}`\n\n return comboKeyFromParts({\n ctrl: e.ctrlKey,\n shift: e.shiftKey,\n alt: e.altKey,\n meta: e.metaKey,\n key: normalizeKey(e.key),\n })\n}\n\n// Code-based lookup string for an event. Independent of `comboKeyFromEvent`\n// because consumers can register either key-based OR code-based bindings (or\n// both) for the same physical key combo, and we look up under whichever they\n// chose.\nfunction codeKeyFromEvent(e) {\n if (!e.code) return null\n\n let s = ''\n if (e.ctrlKey) s += 'ctrl+'\n if (e.shiftKey) s += 'shift+'\n if (e.altKey) s += 'alt+'\n if (e.metaKey) s += 'meta+'\n return s + 'code:' + e.code\n}\n\n\nfunction isModifierToken(token) {\n const t = normalizeKey(token)\n return modifierTokens.has(t)\n}\n\n\n// Matches `code:KeyX` (case-sensitive value). Used to extract code tokens BEFORE\n// the rest of the parser lowercases everything, since KeyboardEvent.code values\n// are camelCase (KeyA, Digit1, ArrowLeft) and must be preserved.\nconst CODE_TOKEN_RE = /(^|\\+)code:([A-Za-z][A-Za-z0-9_]*)/\n\nfunction parseHotkey(hotkeyStr) {\n const raw = String(hotkeyStr || '').trim()\n\n // Detect ' up' suffix case-insensitively so 'Alt UP' works the same as 'alt up'.\n const isUp = /\\s+up$/i.test(raw)\n const baseRaw = isUp ? raw.replace(/\\s+up$/i, '').trim() : raw\n\n // --- AHK-style prefix modifiers ---\n // Examples:\n // ^k => ctrl+k\n // !k => alt+k\n // +k => shift+k\n // #k => meta+k\n // ^!k => ctrl+alt+k\n // !code:KeyX => alt+code:KeyX\n //\n // We strip these BEFORE both the code-token extraction AND the lowercase\n // pass, because the AHK prefix chars (^ ! + #) are case-stable, and the\n // remainder may contain a case-sensitive `code:KeyX` token that must survive.\n const combo = {\n ctrl: false, shift: false, alt: false, meta: false,\n key: null, code: null, bareModifier: null,\n }\n\n let stripped = baseRaw\n while (stripped.length) {\n const mod = ahkPrefixMap.get(stripped[0]) ?? null\n if (!mod) break\n combo[mod] = true\n stripped = stripped.slice(1).trimStart()\n }\n\n // Pull out a code:XXX token before lowercasing; preserve the camelCase value.\n // Reject multiple code: tokens to avoid silent loss of intent.\n const codeMatches = stripped.match(/code:[A-Za-z][A-Za-z0-9_]*/g)\n if (codeMatches && codeMatches.length > 1) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": multiple code: tokens not supported (got ${codeMatches.length}).`\n )\n }\n let codeValue = null\n const scrubbed = stripped.replace(CODE_TOKEN_RE, (_, sep, val) => {\n codeValue = val\n return sep + '__code__'\n })\n\n let cleanStr = scrubbed.toLowerCase()\n\n // Allow plus key:\n // - \"ctrl++\" => ctrl + \"+\"\n // - \"++\" => shift + \"+\" (AHK '+' prefix sets shift, remaining '+' becomes the base key)\n // `split('+')` produces empties; we keep that signal.\n const rawParts = cleanStr.split('+').map((p) => p.trim())\n const parts = rawParts.filter(Boolean)\n const impliedPlusKey = rawParts.some((p) => p === '') && cleanStr.includes('++')\n\n // Enforce: modifiers must come before the base key.\n // Example: \"ctrl+k\" \u2705 | \"k+ctrl\" \u274C\n // We ignore the special \"+ key\" case (ctrl++) since it has no explicit base token.\n if (!impliedPlusKey) {\n let seenBase = false\n for (const part of parts) {\n if (isModifierToken(part)) {\n if (seenBase) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": modifiers must come before the base key (e.g. \"ctrl+k\", not \"k+ctrl\")`\n )\n }\n } else {\n if (!seenBase) seenBase = true\n }\n }\n }\n\n // Continue parsing the remaining tokens (words/symbols/aliases)\n for (const part of parts) {\n if (part === '__code__') {\n combo.code = codeValue\n continue\n }\n const actual = keyAliases[part] ?? part\n if (actual === 'ctrl') combo.ctrl = true\n else if (actual === 'shift') combo.shift = true\n else if (actual === 'alt') combo.alt = true\n else if (actual === 'meta') combo.meta = true\n else combo.key = normalizeKey(actual)\n }\n\n if (!combo.key && !combo.code && impliedPlusKey) combo.key = '+'\n\n // If no base key/code is set, this might be a bare-modifier binding ('alt' / 'ctrl').\n // Allow exactly ONE modifier with no other tokens. Multi-modifier bare bindings\n // (e.g. 'ctrl+alt') are intentionally rejected \u2014 add a base key for those.\n if (!combo.key && !combo.code) {\n const modCount =\n (combo.ctrl ? 1 : 0) + (combo.shift ? 1 : 0) +\n (combo.alt ? 1 : 0) + (combo.meta ? 1 : 0)\n\n if (modCount === 1) {\n combo.bareModifier =\n combo.ctrl ? 'ctrl' :\n combo.shift ? 'shift' :\n combo.alt ? 'alt' : 'meta'\n } else if (modCount > 1) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": multi-modifier bare bindings not supported. Add a base key (e.g. \"ctrl+alt+x\").`\n )\n } else {\n throw new Error(`Invalid hotkey \"${hotkeyStr}\" (missing base key)`)\n }\n }\n\n return { combo, type: isUp ? 'keyup' : 'keydown', raw: hotkeyStr }\n}\n\n\nfunction isFromInputTarget(e) {\n const t = e?.target\n if (!t) return false\n const tag = String(t.tagName || '').toUpperCase()\n return (\n t.isContentEditable ||\n tag === 'INPUT' ||\n tag === 'TEXTAREA' ||\n tag === 'SELECT' ||\n t.getAttribute?.('role') === 'textbox'\n )\n}\n\n\n// --- binding model ---\n// options:\n// preventDefault, stopPropagation, stopImmediatePropagation\n// repeat (default false on keydown), once\n// when(e) -> boolean (gate)\n// allowIn(e) -> boolean (override ignoreInput)\n// priority (higher wins)\nfunction normalizeOptions(type, options) {\n const o = options ? { ...options } : {}\n if (type === 'keydown' && o.repeat == null) o.repeat = false\n if (o.priority == null) o.priority = 0\n return o\n}\n\n\nfunction ensureSlot(comboStr, type) {\n let typeMap = registry.get(comboStr)\n if (!typeMap) {\n typeMap = new Map()\n registry.set(comboStr, typeMap)\n }\n let arr = typeMap.get(type)\n if (!arr) {\n arr = []\n typeMap.set(type, arr)\n }\n return arr\n}\n\n\nfunction removeById(id) {\n const idx = bindingIndex.get(id)\n if (!idx) return false\n\n const { comboStr, type } = idx\n const typeMap = registry.get(comboStr)\n const arr = typeMap?.get(type)\n if (!arr?.length) {\n bindingIndex.delete(id)\n return false\n }\n\n const next = arr.filter((b) => b.id !== id)\n if (next.length) typeMap.set(type, next)\n else typeMap.delete(type)\n\n if (!typeMap.size) registry.delete(comboStr)\n bindingIndex.delete(id)\n return true\n}\n\n\n// --- public API: bind / unbind ---\n// bind returns an off() function; off.id is exposed for plugin bookkeeping.\nfunction bind(hotkeyStr, handler, plugin = null, options = {}) {\n const { combo, type, raw } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const opts = normalizeOptions(type, options)\n\n const binding = {\n id: nextId++,\n comboStr,\n type,\n combo,\n handler,\n plugin,\n raw,\n options: opts,\n }\n\n ensureSlot(comboStr, type).push(binding)\n bindingIndex.set(binding.id, { comboStr, type })\n\n const off = () => removeById(binding.id)\n off.id = binding.id\n off.hotkey = raw\n return off\n}\n\n\n// unbind('ctrl+k') -> remove all for that combo/type\n// unbind('ctrl+k', fn) -> remove only that handler\nfunction unbind(hotkeyStr, handler) {\n const { combo, type } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const typeMap = registry.get(comboStr)\n if (!typeMap) return\n\n const arr = typeMap.get(type)\n if (!arr?.length) return\n\n if (!handler) {\n for (const b of arr) bindingIndex.delete(b.id)\n typeMap.delete(type)\n } else {\n const keep = []\n for (const b of arr) {\n if (b.handler === handler) bindingIndex.delete(b.id)\n else keep.push(b)\n }\n if (keep.length) typeMap.set(type, keep)\n else typeMap.delete(type)\n }\n\n if (!typeMap.size) registry.delete(comboStr)\n}\n\n\n// --- plugin system ---\n// registerPlugin returns unregister function.\nfunction registerPlugin(name, hotkeyMap) {\n if (plugins.has(name)) throw new Error(`Plugin \"${name}\" already registered`)\n const ids = []\n\n for (const [hotkeyStr, fnOrTuple] of Object.entries(hotkeyMap)) {\n const [fn, options] = Array.isArray(fnOrTuple) ? fnOrTuple : [fnOrTuple, {}]\n const off = bind(hotkeyStr, fn, name, options)\n ids.push(off.id)\n }\n\n plugins.set(name, ids)\n return () => unregisterPlugin(name)\n}\n\nfunction unregisterPlugin(name) {\n const ids = plugins.get(name)\n if (!ids) return\n for (const id of ids) removeById(id)\n plugins.delete(name)\n}\n\n// --- control ---\nfunction pause() {\n paused = true\n}\n\nfunction resume() {\n paused = false\n}\n\nfunction ignoreInput(value = true) {\n ignoreEditable = !!value\n}\n\n// allow changing the default target (iframes/tests).\n// we intentionally do not auto-rebind to keep the core tiny.\nfunction setTarget(target) {\n defaultTarget = target\n}\n\n\n// --- dispatch (O(1) lookup) ---\nfunction pickWinner(bindings) {\n // Highest priority wins; ties -> newest wins (largest id).\n let best = null\n for (const b of bindings) {\n if (!best) best = b\n else if (b.options.priority > best.options.priority) best = b\n else if (b.options.priority === best.options.priority && b.id > best.id) best = b\n }\n return best\n}\n\n\nfunction handleEvent(type) {\n return (e) => {\n if (paused) return\n\n const fromInput = ignoreEditable && isFromInputTarget(e)\n const comboStr = comboKeyFromEvent(e)\n const codeStr = codeKeyFromEvent(e)\n\n // Look up under both lookup keys: key-based (`alt+x`) and code-based\n // (`alt+code:KeyX`). Same physical keypress, two valid binding shapes.\n // Merge candidates and let the existing priority/recency arbiter pick.\n const seen = new Set()\n const candidates = []\n for (const lookupKey of [comboStr, codeStr]) {\n if (!lookupKey || seen.has(lookupKey)) continue\n seen.add(lookupKey)\n\n const bindings = registry.get(lookupKey)?.get(type)\n if (!bindings?.length) continue\n\n for (const b of bindings) {\n const o = b.options\n if (fromInput && !o.allowIn?.(e)) continue\n if (type === 'keydown' && o.repeat === false && e.repeat) continue\n if (o.when && o.when(e) === false) continue\n candidates.push(b)\n }\n }\n if (!candidates.length) return\n\n const winner = pickWinner(candidates)\n const o = winner.options\n\n if (o.preventDefault) e.preventDefault()\n if (o.stopImmediatePropagation) e.stopImmediatePropagation()\n else if (o.stopPropagation) e.stopPropagation()\n\n winner.handler(e)\n\n if (o.once) removeById(winner.id)\n }\n}\n\n\n// --- listeners ---\nlet stopDown = null\nlet stopUp = null\n\nfunction init({ target = defaultTarget, capture = false } = {}) {\n if (!target) throw new Error('hotkeys.init() requires a target (e.g. window)')\n if (stopDown || stopUp) destroy()\n\n stopDown = on(target, 'keydown', handleEvent('keydown'), { capture })\n stopUp = on(target, 'keyup', handleEvent('keyup'), { capture })\n}\n\n\nfunction destroy() {\n stopDown?.()\n stopUp?.()\n stopDown = null\n stopUp = null\n registry.clear()\n plugins.clear()\n bindingIndex.clear()\n}\n\n\n// --- programmatic trigger (tests / automation) ---\n//\n// Reverse map: 'alt' -> 'Alt' (the KeyboardEvent.key value for that bare modifier).\n// Used to populate the synthetic event's `.key` field so consumer handlers that\n// inspect `e.key` see the same value they'd get from a real event.\nconst BARE_MOD_TO_KEY = {\n ctrl: 'Control',\n shift: 'Shift',\n alt: 'Alt',\n meta: 'Meta',\n}\n\nfunction trigger(hotkeyStr, { type: forcedType } = {}) {\n const { combo, type } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const actualType = forcedType || type\n\n const bindings = registry.get(comboStr)?.get(actualType)\n if (!bindings?.length) return false\n\n // Fill `.key` from bareModifier when the binding is bare-modifier-only;\n // otherwise use combo.key. `.code` is set from combo.code for code-based bindings.\n const eventKey = combo.bareModifier\n ? BARE_MOD_TO_KEY[combo.bareModifier]\n : combo.key\n\n // For bare-modifier keyup, real KeyboardEvents have the corresponding\n // modifier flag FALSE (the modifier just released). Mirror that so\n // automation handlers that inspect e.altKey / e.ctrlKey / etc. see the\n // same value they'd see from a real event.\n let altKey = !!combo.alt\n let ctrlKey = !!combo.ctrl\n let shiftKey = !!combo.shift\n let metaKey = !!combo.meta\n if (combo.bareModifier && actualType === 'keyup') {\n if (combo.bareModifier === 'alt') altKey = false\n else if (combo.bareModifier === 'ctrl') ctrlKey = false\n else if (combo.bareModifier === 'shift') shiftKey = false\n else if (combo.bareModifier === 'meta') metaKey = false\n }\n\n const e = {\n type: actualType,\n key: eventKey,\n code: combo.code || null,\n ctrlKey, shiftKey, altKey, metaKey,\n repeat: false,\n preventDefault() { },\n stopPropagation() { },\n stopImmediatePropagation() { },\n target: null,\n }\n\n const winner = pickWinner(\n bindings.filter((b) => (b.options.when ? b.options.when(e) !== false : true))\n )\n if (!winner) return false\n\n winner.handler(e)\n if (winner.options.once) removeById(winner.id)\n return true\n}\n\n\n// Auto-init on window by default (browser only)\nif (defaultTarget) init()\n\nexport default {\n // lifecycle\n init,\n destroy,\n setTarget,\n\n // bindings\n bind,\n unbind,\n registerPlugin,\n unregisterPlugin,\n\n // control\n pause,\n resume,\n ignoreInput,\n\n // testing / automation\n trigger,\n}"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["// hotkey-router.js\n// Hotkey Router \u2014 plugin-first keyboard shortcut router (tiny + predictable)\n//\n// Goals:\n// - O(1) dispatch by combo string lookup\n// - Deterministic routing: highest priority wins; ties -> most recently bound wins\n// - Safe plugin cleanup (never deletes other plugins\u2019 bindings)\n// - Modern browser target (KeyboardEvent.key + addEventListener)\n//\n// This core ships ZERO reservation data \u2014 opt in via `hotkey-router/auto`\n// or by calling `installReservationWarnings(hotkeys)` from the separate\n// `hotkey-router/reservations` module. Bundlers tree-shake the data file\n// out entirely when neither is imported.\n\n// --- tiny event helper ---\nfunction on(target, type, fn, options) {\n target.addEventListener(type, fn, options)\n return () => target.removeEventListener(type, fn, options)\n}\n\n// --- key alias map ---\nconst isMac =\n typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform)\n\n\nconst modifierTokens = new Set(['ctrl', 'shift', 'alt', 'meta'])\n\n// Map from KeyboardEvent.key (which IS the modifier name on bare-modifier events)\n// to the canonical token used in combo strings. Used to detect bare-modifier\n// keydown/keyup events at dispatch time.\nconst MODIFIER_KEY_TOKEN = Object.freeze({\n Alt: 'alt',\n AltGraph: 'alt',\n Control: 'ctrl',\n Shift: 'shift',\n Meta: 'meta',\n OS: 'meta',\n})\n\nconst ahkPrefixMap = new Map([\n ['^', 'ctrl'], // Ctrl\n ['!', 'alt'], // Alt\n ['+', 'shift'], // Shift\n ['#', 'meta'], // Win / Command\n])\n\n\nconst keyAliases = Object.freeze({\n // common\n esc: 'escape',\n escape: 'escape',\n space: ' ',\n spacebar: ' ',\n enter: 'enter',\n return: 'enter',\n del: 'delete',\n delete: 'delete',\n backspace: 'backspace',\n tab: 'tab',\n\n // arrows\n up: 'arrowup',\n down: 'arrowdown',\n left: 'arrowleft',\n right: 'arrowright',\n\n // navigation\n home: 'home',\n end: 'end',\n pageup: 'pageup',\n pagedown: 'pagedown',\n pgup: 'pageup',\n pgdn: 'pagedown',\n\n // modifiers (words)\n cmd: 'meta',\n command: 'meta',\n win: 'meta',\n meta: 'meta',\n control: 'ctrl',\n ctrl: 'ctrl',\n option: 'alt',\n alt: 'alt',\n shift: 'shift',\n\n // modifiers (symbols)\n '\u2318': 'meta',\n '\u2303': 'ctrl',\n '\u2325': 'alt',\n '\u21E7': 'shift',\n\n // cross-platform \"mod\"\n mod: isMac ? 'meta' : 'ctrl',\n\n // plus key ergonomics\n plus: '+',\n})\n\n// --- config ---\nlet ignoreEditable = true\nlet defaultTarget = typeof window !== 'undefined' ? window : null\n\n// Bind hooks: subscribers called after each successful bind. Used by the\n// optional reservations module to emit conflict warnings without coupling\n// the core router to the (~5 KB) reservation table.\nlet bindHooks = []\n\n// --- state ---\n// comboStr -> Map<type, Array<Binding>>\nconst registry = new Map()\n// pluginName -> Array<bindingId>\nconst plugins = new Map()\n// bindingId -> { comboStr, type }\nconst bindingIndex = new Map()\n\nlet paused = false\nlet nextId = 1\n\n// --- helpers ---\nfunction normalizeKey(key) {\n const raw = String(key ?? '').trim()\n const k = raw.toLowerCase()\n return keyAliases[k] ?? (raw === ' ' ? ' ' : k)\n}\n\nfunction comboKeyFromParts({ ctrl, shift, alt, meta, key, code, bareModifier }) {\n // Bare-modifier bindings get a sentinel namespace so they can't collide with\n // regular chord combo strings (`'alt+x'` vs `'__bare__:alt'`).\n if (bareModifier) return `__bare__:${bareModifier}`\n\n let s = ''\n if (ctrl) s += 'ctrl+'\n if (shift) s += 'shift+'\n if (alt) s += 'alt+'\n if (meta) s += 'meta+'\n\n // Code-based combos use a `code:KeyX` suffix instead of the key character.\n if (code) return s + 'code:' + code\n return s ? s + key : key\n}\n\nfunction comboKeyFromEvent(e) {\n // If the event's `key` is itself a modifier name, this is a bare-modifier\n // press/release. Match it via the bare-modifier sentinel regardless of which\n // other modifier flags are set (loose match \u2014 Alt fires `alt` even when held\n // alongside Ctrl).\n const bareMod = MODIFIER_KEY_TOKEN[e.key]\n if (bareMod) return `__bare__:${bareMod}`\n\n return comboKeyFromParts({\n ctrl: e.ctrlKey,\n shift: e.shiftKey,\n alt: e.altKey,\n meta: e.metaKey,\n key: normalizeKey(e.key),\n })\n}\n\n// Code-based lookup string for an event. Independent of `comboKeyFromEvent`\n// because consumers can register either key-based OR code-based bindings (or\n// both) for the same physical key combo, and we look up under whichever they\n// chose.\nfunction codeKeyFromEvent(e) {\n if (!e.code) return null\n\n let s = ''\n if (e.ctrlKey) s += 'ctrl+'\n if (e.shiftKey) s += 'shift+'\n if (e.altKey) s += 'alt+'\n if (e.metaKey) s += 'meta+'\n return s + 'code:' + e.code\n}\n\n\nfunction isModifierToken(token) {\n const t = normalizeKey(token)\n return modifierTokens.has(t)\n}\n\n\n// Matches `code:KeyX` (case-sensitive value). Used to extract code tokens BEFORE\n// the rest of the parser lowercases everything, since KeyboardEvent.code values\n// are camelCase (KeyA, Digit1, ArrowLeft) and must be preserved.\nconst CODE_TOKEN_RE = /(^|\\+)code:([A-Za-z][A-Za-z0-9_]*)/\n\nfunction parseHotkey(hotkeyStr) {\n const raw = String(hotkeyStr || '').trim()\n\n // Detect ' up' suffix case-insensitively so 'Alt UP' works the same as 'alt up'.\n const isUp = /\\s+up$/i.test(raw)\n const baseRaw = isUp ? raw.replace(/\\s+up$/i, '').trim() : raw\n\n // --- AHK-style prefix modifiers ---\n // Examples:\n // ^k => ctrl+k\n // !k => alt+k\n // +k => shift+k\n // #k => meta+k\n // ^!k => ctrl+alt+k\n // !code:KeyX => alt+code:KeyX\n //\n // We strip these BEFORE both the code-token extraction AND the lowercase\n // pass, because the AHK prefix chars (^ ! + #) are case-stable, and the\n // remainder may contain a case-sensitive `code:KeyX` token that must survive.\n const combo = {\n ctrl: false, shift: false, alt: false, meta: false,\n key: null, code: null, bareModifier: null,\n }\n\n let stripped = baseRaw\n while (stripped.length) {\n const mod = ahkPrefixMap.get(stripped[0]) ?? null\n if (!mod) break\n combo[mod] = true\n stripped = stripped.slice(1).trimStart()\n }\n\n // Pull out a code:XXX token before lowercasing; preserve the camelCase value.\n // Reject multiple code: tokens to avoid silent loss of intent.\n const codeMatches = stripped.match(/code:[A-Za-z][A-Za-z0-9_]*/g)\n if (codeMatches && codeMatches.length > 1) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": multiple code: tokens not supported (got ${codeMatches.length}).`\n )\n }\n let codeValue = null\n const scrubbed = stripped.replace(CODE_TOKEN_RE, (_, sep, val) => {\n codeValue = val\n return sep + '__code__'\n })\n\n let cleanStr = scrubbed.toLowerCase()\n\n // Allow plus key:\n // - \"ctrl++\" => ctrl + \"+\"\n // - \"++\" => shift + \"+\" (AHK '+' prefix sets shift, remaining '+' becomes the base key)\n // `split('+')` produces empties; we keep that signal.\n const rawParts = cleanStr.split('+').map((p) => p.trim())\n const parts = rawParts.filter(Boolean)\n const impliedPlusKey = rawParts.some((p) => p === '') && cleanStr.includes('++')\n\n // Enforce: modifiers must come before the base key.\n // Example: \"ctrl+k\" \u2705 | \"k+ctrl\" \u274C\n // We ignore the special \"+ key\" case (ctrl++) since it has no explicit base token.\n if (!impliedPlusKey) {\n let seenBase = false\n for (const part of parts) {\n if (isModifierToken(part)) {\n if (seenBase) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": modifiers must come before the base key (e.g. \"ctrl+k\", not \"k+ctrl\")`\n )\n }\n } else {\n if (!seenBase) seenBase = true\n }\n }\n }\n\n // Continue parsing the remaining tokens (words/symbols/aliases)\n for (const part of parts) {\n if (part === '__code__') {\n combo.code = codeValue\n continue\n }\n const actual = keyAliases[part] ?? part\n if (actual === 'ctrl') combo.ctrl = true\n else if (actual === 'shift') combo.shift = true\n else if (actual === 'alt') combo.alt = true\n else if (actual === 'meta') combo.meta = true\n else combo.key = normalizeKey(actual)\n }\n\n if (!combo.key && !combo.code && impliedPlusKey) combo.key = '+'\n\n // If no base key/code is set, this might be a bare-modifier binding ('alt' / 'ctrl').\n // Allow exactly ONE modifier with no other tokens. Multi-modifier bare bindings\n // (e.g. 'ctrl+alt') are intentionally rejected \u2014 add a base key for those.\n if (!combo.key && !combo.code) {\n const modCount =\n (combo.ctrl ? 1 : 0) + (combo.shift ? 1 : 0) +\n (combo.alt ? 1 : 0) + (combo.meta ? 1 : 0)\n\n if (modCount === 1) {\n combo.bareModifier =\n combo.ctrl ? 'ctrl' :\n combo.shift ? 'shift' :\n combo.alt ? 'alt' : 'meta'\n } else if (modCount > 1) {\n throw new Error(\n `Invalid hotkey \"${hotkeyStr}\": multi-modifier bare bindings not supported. Add a base key (e.g. \"ctrl+alt+x\").`\n )\n } else {\n throw new Error(`Invalid hotkey \"${hotkeyStr}\" (missing base key)`)\n }\n }\n\n return { combo, type: isUp ? 'keyup' : 'keydown', raw: hotkeyStr }\n}\n\n\nfunction isFromInputTarget(e) {\n const t = e?.target\n if (!t) return false\n const tag = String(t.tagName || '').toUpperCase()\n return (\n t.isContentEditable ||\n tag === 'INPUT' ||\n tag === 'TEXTAREA' ||\n tag === 'SELECT' ||\n t.getAttribute?.('role') === 'textbox'\n )\n}\n\n\n// --- binding model ---\n// options:\n// preventDefault, stopPropagation, stopImmediatePropagation\n// repeat (default false on keydown), once\n// when(e) -> boolean (gate)\n// allowIn(e) -> boolean (override ignoreInput)\n// priority (higher wins)\nfunction normalizeOptions(type, options) {\n const o = options ? { ...options } : {}\n if (type === 'keydown' && o.repeat == null) o.repeat = false\n if (o.priority == null) o.priority = 0\n return o\n}\n\n\nfunction ensureSlot(comboStr, type) {\n let typeMap = registry.get(comboStr)\n if (!typeMap) {\n typeMap = new Map()\n registry.set(comboStr, typeMap)\n }\n let arr = typeMap.get(type)\n if (!arr) {\n arr = []\n typeMap.set(type, arr)\n }\n return arr\n}\n\n\nfunction removeById(id) {\n const idx = bindingIndex.get(id)\n if (!idx) return false\n\n const { comboStr, type } = idx\n const typeMap = registry.get(comboStr)\n const arr = typeMap?.get(type)\n if (!arr?.length) {\n bindingIndex.delete(id)\n return false\n }\n\n const next = arr.filter((b) => b.id !== id)\n if (next.length) typeMap.set(type, next)\n else typeMap.delete(type)\n\n if (!typeMap.size) registry.delete(comboStr)\n bindingIndex.delete(id)\n return true\n}\n\n\n// --- public API: bind / unbind ---\n// bind returns an off() function; off.id is exposed for plugin bookkeeping.\nfunction bind(hotkeyStr, handler, plugin = null, options = {}) {\n const { combo, type, raw } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const opts = normalizeOptions(type, options)\n\n const binding = {\n id: nextId++,\n comboStr,\n type,\n combo,\n handler,\n plugin,\n raw,\n options: opts,\n }\n\n ensureSlot(comboStr, type).push(binding)\n bindingIndex.set(binding.id, { comboStr, type })\n\n // Notify bind subscribers. Hook errors must never break the bind itself.\n if (bindHooks.length) {\n for (const hook of bindHooks) {\n try {\n hook({ combo, raw, options: opts, plugin, id: binding.id })\n } catch (err) {\n // eslint-disable-next-line no-console\n console.error('[hotkey-router] onBind hook threw:', err)\n }\n }\n }\n\n const off = () => removeById(binding.id)\n off.id = binding.id\n off.hotkey = raw\n return off\n}\n\n// Subscribe to bind events. The hook receives\n// { combo, raw, options, plugin, id }\n// after the binding is registered. Returns an unsubscribe function. Used by\n// extensions like `installReservationWarnings` from `hotkey-router/reservations`.\nfunction onBind(hook) {\n if (typeof hook !== 'function') {\n throw new TypeError('hotkeys.onBind(hook) expects a function')\n }\n bindHooks.push(hook)\n return () => {\n bindHooks = bindHooks.filter((h) => h !== hook)\n }\n}\n\n\n// unbind('ctrl+k') -> remove all for that combo/type\n// unbind('ctrl+k', fn) -> remove only that handler\nfunction unbind(hotkeyStr, handler) {\n const { combo, type } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const typeMap = registry.get(comboStr)\n if (!typeMap) return\n\n const arr = typeMap.get(type)\n if (!arr?.length) return\n\n if (!handler) {\n for (const b of arr) bindingIndex.delete(b.id)\n typeMap.delete(type)\n } else {\n const keep = []\n for (const b of arr) {\n if (b.handler === handler) bindingIndex.delete(b.id)\n else keep.push(b)\n }\n if (keep.length) typeMap.set(type, keep)\n else typeMap.delete(type)\n }\n\n if (!typeMap.size) registry.delete(comboStr)\n}\n\n\n// --- plugin system ---\n// registerPlugin returns unregister function.\nfunction registerPlugin(name, hotkeyMap) {\n if (plugins.has(name)) throw new Error(`Plugin \"${name}\" already registered`)\n const ids = []\n\n for (const [hotkeyStr, fnOrTuple] of Object.entries(hotkeyMap)) {\n const [fn, options] = Array.isArray(fnOrTuple) ? fnOrTuple : [fnOrTuple, {}]\n const off = bind(hotkeyStr, fn, name, options)\n ids.push(off.id)\n }\n\n plugins.set(name, ids)\n return () => unregisterPlugin(name)\n}\n\nfunction unregisterPlugin(name) {\n const ids = plugins.get(name)\n if (!ids) return\n for (const id of ids) removeById(id)\n plugins.delete(name)\n}\n\n// --- control ---\nfunction pause() {\n paused = true\n}\n\nfunction resume() {\n paused = false\n}\n\nfunction ignoreInput(value = true) {\n ignoreEditable = !!value\n}\n\n// allow changing the default target (iframes/tests).\n// we intentionally do not auto-rebind to keep the core tiny.\nfunction setTarget(target) {\n defaultTarget = target\n}\n\n\n// --- dispatch (O(1) lookup) ---\nfunction pickWinner(bindings) {\n // Highest priority wins; ties -> newest wins (largest id).\n let best = null\n for (const b of bindings) {\n if (!best) best = b\n else if (b.options.priority > best.options.priority) best = b\n else if (b.options.priority === best.options.priority && b.id > best.id) best = b\n }\n return best\n}\n\n\nfunction handleEvent(type) {\n return (e) => {\n if (paused) return\n\n const fromInput = ignoreEditable && isFromInputTarget(e)\n const comboStr = comboKeyFromEvent(e)\n const codeStr = codeKeyFromEvent(e)\n\n // Look up under both lookup keys: key-based (`alt+x`) and code-based\n // (`alt+code:KeyX`). Same physical keypress, two valid binding shapes.\n // Merge candidates and let the existing priority/recency arbiter pick.\n const seen = new Set()\n const candidates = []\n for (const lookupKey of [comboStr, codeStr]) {\n if (!lookupKey || seen.has(lookupKey)) continue\n seen.add(lookupKey)\n\n const bindings = registry.get(lookupKey)?.get(type)\n if (!bindings?.length) continue\n\n for (const b of bindings) {\n const o = b.options\n if (fromInput && !o.allowIn?.(e)) continue\n if (type === 'keydown' && o.repeat === false && e.repeat) continue\n if (o.when && o.when(e) === false) continue\n candidates.push(b)\n }\n }\n if (!candidates.length) return\n\n const winner = pickWinner(candidates)\n const o = winner.options\n\n if (o.preventDefault) e.preventDefault()\n if (o.stopImmediatePropagation) e.stopImmediatePropagation()\n else if (o.stopPropagation) e.stopPropagation()\n\n winner.handler(e)\n\n if (o.once) removeById(winner.id)\n }\n}\n\n\n// --- listeners ---\nlet stopDown = null\nlet stopUp = null\n\nfunction init({ target = defaultTarget, capture = false } = {}) {\n if (!target) throw new Error('hotkeys.init() requires a target (e.g. window)')\n if (stopDown || stopUp) destroy()\n\n stopDown = on(target, 'keydown', handleEvent('keydown'), { capture })\n stopUp = on(target, 'keyup', handleEvent('keyup'), { capture })\n}\n\n\nfunction destroy() {\n stopDown?.()\n stopUp?.()\n stopDown = null\n stopUp = null\n registry.clear()\n plugins.clear()\n bindingIndex.clear()\n // Note: bindHooks deliberately survive destroy(). Consumer extensions\n // (reservations, future telemetry, etc.) install once at startup and\n // shouldn't have to re-attach when the router restarts. Tests that need\n // a clean slate should use the uninstall fn returned by their installer.\n}\n\n\n// --- programmatic trigger (tests / automation) ---\n//\n// Reverse map: 'alt' -> 'Alt' (the KeyboardEvent.key value for that bare modifier).\n// Used to populate the synthetic event's `.key` field so consumer handlers that\n// inspect `e.key` see the same value they'd get from a real event.\nconst BARE_MOD_TO_KEY = {\n ctrl: 'Control',\n shift: 'Shift',\n alt: 'Alt',\n meta: 'Meta',\n}\n\nfunction trigger(hotkeyStr, { type: forcedType } = {}) {\n const { combo, type } = parseHotkey(hotkeyStr)\n const comboStr = comboKeyFromParts(combo)\n const actualType = forcedType || type\n\n const bindings = registry.get(comboStr)?.get(actualType)\n if (!bindings?.length) return false\n\n // Fill `.key` from bareModifier when the binding is bare-modifier-only;\n // otherwise use combo.key. `.code` is set from combo.code for code-based bindings.\n const eventKey = combo.bareModifier\n ? BARE_MOD_TO_KEY[combo.bareModifier]\n : combo.key\n\n // For bare-modifier keyup, real KeyboardEvents have the corresponding\n // modifier flag FALSE (the modifier just released). Mirror that so\n // automation handlers that inspect e.altKey / e.ctrlKey / etc. see the\n // same value they'd see from a real event.\n let altKey = !!combo.alt\n let ctrlKey = !!combo.ctrl\n let shiftKey = !!combo.shift\n let metaKey = !!combo.meta\n if (combo.bareModifier && actualType === 'keyup') {\n if (combo.bareModifier === 'alt') altKey = false\n else if (combo.bareModifier === 'ctrl') ctrlKey = false\n else if (combo.bareModifier === 'shift') shiftKey = false\n else if (combo.bareModifier === 'meta') metaKey = false\n }\n\n const e = {\n type: actualType,\n key: eventKey,\n code: combo.code || null,\n ctrlKey, shiftKey, altKey, metaKey,\n repeat: false,\n preventDefault() { },\n stopPropagation() { },\n stopImmediatePropagation() { },\n target: null,\n }\n\n const winner = pickWinner(\n bindings.filter((b) => (b.options.when ? b.options.when(e) !== false : true))\n )\n if (!winner) return false\n\n winner.handler(e)\n if (winner.options.once) removeById(winner.id)\n return true\n}\n\n\n// Auto-init on window by default (browser only)\nif (defaultTarget) init()\n\nexport default {\n // lifecycle\n init,\n destroy,\n setTarget,\n\n // bindings\n bind,\n unbind,\n registerPlugin,\n unregisterPlugin,\n\n // extension hooks\n onBind,\n\n // control\n pause,\n resume,\n ignoreInput,\n\n // testing / automation\n trigger,\n}"],
|
|
5
|
+
"mappings": ";;;;AAeA,SAAS,GAAG,QAAQ,MAAM,IAAI,SAAS;AACrC,SAAO,iBAAiB,MAAM,IAAI,OAAO;AACzC,SAAO,MAAM,OAAO,oBAAoB,MAAM,IAAI,OAAO;AAC3D;AAGA,IAAM,QACJ,OAAO,cAAc,eAAe,uBAAuB,KAAK,UAAU,QAAQ;AAGpF,IAAM,iBAAiB,oBAAI,IAAI,CAAC,QAAQ,SAAS,OAAO,MAAM,CAAC;AAK/D,IAAM,qBAAqB,OAAO,OAAO;AAAA,EACvC,KAAK;AAAA,EACL,UAAU;AAAA,EACV,SAAS;AAAA,EACT,OAAO;AAAA,EACP,MAAM;AAAA,EACN,IAAI;AACN,CAAC;AAED,IAAM,eAAe,oBAAI,IAAI;AAAA,EAC3B,CAAC,KAAK,MAAM;AAAA;AAAA,EACZ,CAAC,KAAK,KAAK;AAAA;AAAA,EACX,CAAC,KAAK,OAAO;AAAA;AAAA,EACb,CAAC,KAAK,MAAM;AAAA;AACd,CAAC;AAGD,IAAM,aAAa,OAAO,OAAO;AAAA;AAAA,EAE/B,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,UAAU;AAAA,EACV,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,WAAW;AAAA,EACX,KAAK;AAAA;AAAA,EAGL,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA;AAAA,EAGP,MAAM;AAAA,EACN,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,UAAU;AAAA,EACV,MAAM;AAAA,EACN,MAAM;AAAA;AAAA,EAGN,KAAK;AAAA,EACL,S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BAAiB,OAAQ,WAAU;AAAA,EACpD;AAEA,QAAM,IAAI;AAAA,IACR,MAAM;AAAA,IACN,KAAK;AAAA,IACL,MAAM,MAAM,QAAQ;AAAA,IACpB;AAAA,IAAS;AAAA,IAAU;AAAA,IAAQ;AAAA,IAC3B,QAAQ;AAAA,IACR,iBAAiB;AAAA,IAAE;AAAA,IACnB,kBAAkB;AAAA,IAAE;AAAA,IACpB,2BAA2B;AAAA,IAAE;AAAA,IAC7B,QAAQ;AAAA,EACV;AAEA,QAAM,SAAS;AAAA,IACb,SAAS,OAAO,CAAC,MAAO,EAAE,QAAQ,OAAO,EAAE,QAAQ,KAAK,CAAC,MAAM,QAAQ,IAAK;AAAA,EAC9E;AACA,MAAI,CAAC,OAAQ,QAAO;AAEpB,SAAO,QAAQ,CAAC;AAChB,MAAI,OAAO,QAAQ,KAAM,YAAW,OAAO,EAAE;AAC7C,SAAO;AACT;AAIA,IAAI,cAAe,MAAK;AAExB,IAAO,wBAAQ;AAAA;AAAA,EAEb;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA;AACF;",
|
|
6
6
|
"names": ["o"]
|
|
7
7
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
// hotkey-router v0.
|
|
1
|
+
// hotkey-router v0.2.0 | SEE LICENSE IN LICENSE
|
|
2
2
|
|
|
3
|
-
function
|
|
3
|
+
function P(e,t,n,s){return e.addEventListener(t,n,s),()=>e.removeEventListener(t,n,s)}var D=typeof navigator<"u"&&/Mac|iPhone|iPad|iPod/.test(navigator.platform),F=new Set(["ctrl","shift","alt","meta"]),L=Object.freeze({Alt:"alt",AltGraph:"alt",Control:"ctrl",Shift:"shift",Meta:"meta",OS:"meta"}),R=new Map([["^","ctrl"],["!","alt"],["+","shift"],["#","meta"]]),T=Object.freeze({esc:"escape",escape:"escape",space:" ",spacebar:" ",enter:"enter",return:"enter",del:"delete",delete:"delete",backspace:"backspace",tab:"tab",up:"arrowup",down:"arrowdown",left:"arrowleft",right:"arrowright",home:"home",end:"end",pageup:"pageup",pagedown:"pagedown",pgup:"pageup",pgdn:"pagedown",cmd:"meta",command:"meta",win:"meta",meta:"meta",control:"ctrl",ctrl:"ctrl",option:"alt",alt:"alt",shift:"shift","\u2318":"meta","\u2303":"ctrl","\u2325":"alt","\u21E7":"shift",mod:D?"meta":"ctrl",plus:"+"}),S=!0,_=typeof window<"u"?window:null,y=[],g=new Map,w=new Map,m=new Map,K=!1,N=1;function v(e){let t=String(e??"").trim(),n=t.toLowerCase();return T[n]??(t===" "?" ":n)}function E({ctrl:e,shift:t,alt:n,meta:s,key:o,code:r,bareModifier:l}){if(l)return`__bare__:${l}`;let i="";return e&&(i+="ctrl+"),t&&(i+="shift+"),n&&(i+="alt+"),s&&(i+="meta+"),r?i+"code:"+r:i?i+o:o}function U(e){let t=L[e.key];return t?`__bare__:${t}`:E({ctrl:e.ctrlKey,shift:e.shiftKey,alt:e.altKey,meta:e.metaKey,key:v(e.key)})}function Z(e){if(!e.code)return null;let t="";return e.ctrlKey&&(t+="ctrl+"),e.shiftKey&&(t+="shift+"),e.altKey&&(t+="alt+"),e.metaKey&&(t+="meta+"),t+"code:"+e.code}function j(e){let t=v(e);return F.has(t)}var H=/(^|\+)code:([A-Za-z][A-Za-z0-9_]*)/;function I(e){let t=String(e||"").trim(),n=/\s+up$/i.test(t),s=n?t.replace(/\s+up$/i,"").trim():t,o={ctrl:!1,shift:!1,alt:!1,meta:!1,key:null,code:null,bareModifier:null},r=s;for(;r.length;){let a=R.get(r[0])??null;if(!a)break;o[a]=!0,r=r.slice(1).trimStart()}let l=r.match(/code:[A-Za-z][A-Za-z0-9_]*/g);if(l&&l.length>1)throw new Error(`Invalid hotkey "${e}": multiple code: tokens not supported (got ${l.length}).`);let i=null,f=r.replace(H,(a,p,B)=>(i=B,p+"__code__")).toLowerCase(),u=f.split("+").map(a=>a.trim()),h=u.filter(Boolean),d=u.some(a=>a==="")&&f.includes("++");if(!d){let a=!1;for(let p of h)if(j(p)){if(a)throw new Error(`Invalid hotkey "${e}": modifiers must come before the base key (e.g. "ctrl+k", not "k+ctrl")`)}else a||(a=!0)}for(let a of h){if(a==="__code__"){o.code=i;continue}let p=T[a]??a;p==="ctrl"?o.ctrl=!0:p==="shift"?o.shift=!0:p==="alt"?o.alt=!0:p==="meta"?o.meta=!0:o.key=v(p)}if(!o.key&&!o.code&&d&&(o.key="+"),!o.key&&!o.code){let a=(o.ctrl?1:0)+(o.shift?1:0)+(o.alt?1:0)+(o.meta?1:0);if(a===1)o.bareModifier=o.ctrl?"ctrl":o.shift?"shift":o.alt?"alt":"meta";else throw a>1?new Error(`Invalid hotkey "${e}": multi-modifier bare bindings not supported. Add a base key (e.g. "ctrl+alt+x").`):new Error(`Invalid hotkey "${e}" (missing base key)`)}return{combo:o,type:n?"keyup":"keydown",raw:e}}function Y(e){let t=e?.target;if(!t)return!1;let n=String(t.tagName||"").toUpperCase();return t.isContentEditable||n==="INPUT"||n==="TEXTAREA"||n==="SELECT"||t.getAttribute?.("role")==="textbox"}function q(e,t){let n=t?{...t}:{};return e==="keydown"&&n.repeat==null&&(n.repeat=!1),n.priority==null&&(n.priority=0),n}function G(e,t){let n=g.get(e);n||(n=new Map,g.set(e,n));let s=n.get(t);return s||(s=[],n.set(t,s)),s}function M(e){let t=m.get(e);if(!t)return!1;let{comboStr:n,type:s}=t,o=g.get(n),r=o?.get(s);if(!r?.length)return m.delete(e),!1;let l=r.filter(i=>i.id!==e);return l.length?o.set(s,l):o.delete(s),o.size||g.delete(n),m.delete(e),!0}function O(e,t,n=null,s={}){let{combo:o,type:r,raw:l}=I(e),i=E(o),c=q(r,s),f={id:N++,comboStr:i,type:r,combo:o,handler:t,plugin:n,raw:l,options:c};if(G(i,r).push(f),m.set(f.id,{comboStr:i,type:r}),y.length)for(let h of y)try{h({combo:o,raw:l,options:c,plugin:n,id:f.id})}catch(d){console.error("[hotkey-router] onBind hook threw:",d)}let u=()=>M(f.id);return u.id=f.id,u.hotkey=l,u}function V(e){if(typeof e!="function")throw new TypeError("hotkeys.onBind(hook) expects a function");return y.push(e),()=>{y=y.filter(t=>t!==e)}}function W(e,t){let{combo:n,type:s}=I(e),o=E(n),r=g.get(o);if(!r)return;let l=r.get(s);if(l?.length){if(t){let i=[];for(let c of l)c.handler===t?m.delete(c.id):i.push(c);i.length?r.set(s,i):r.delete(s)}else{for(let i of l)m.delete(i.id);r.delete(s)}r.size||g.delete(o)}}function X(e,t){if(w.has(e))throw new Error(`Plugin "${e}" already registered`);let n=[];for(let[s,o]of Object.entries(t)){let[r,l]=Array.isArray(o)?o:[o,{}],i=O(s,r,e,l);n.push(i.id)}return w.set(e,n),()=>x(e)}function x(e){let t=w.get(e);if(t){for(let n of t)M(n);w.delete(e)}}function J(){K=!0}function Q(){K=!1}function ee(e=!0){S=!!e}function te(e){_=e}function z(e){let t=null;for(let n of e)t?(n.options.priority>t.options.priority||n.options.priority===t.options.priority&&n.id>t.id)&&(t=n):t=n;return t}function A(e){return t=>{if(K)return;let n=S&&Y(t),s=U(t),o=Z(t),r=new Set,l=[];for(let f of[s,o]){if(!f||r.has(f))continue;r.add(f);let u=g.get(f)?.get(e);if(u?.length)for(let h of u){let d=h.options;n&&!d.allowIn?.(t)||e==="keydown"&&d.repeat===!1&&t.repeat||d.when&&d.when(t)===!1||l.push(h)}}if(!l.length)return;let i=z(l),c=i.options;c.preventDefault&&t.preventDefault(),c.stopImmediatePropagation?t.stopImmediatePropagation():c.stopPropagation&&t.stopPropagation(),i.handler(t),c.once&&M(i.id)}}var b=null,k=null;function $({target:e=_,capture:t=!1}={}){if(!e)throw new Error("hotkeys.init() requires a target (e.g. window)");(b||k)&&C(),b=P(e,"keydown",A("keydown"),{capture:t}),k=P(e,"keyup",A("keyup"),{capture:t})}function C(){b?.(),k?.(),b=null,k=null,g.clear(),w.clear(),m.clear()}var ne={ctrl:"Control",shift:"Shift",alt:"Alt",meta:"Meta"};function oe(e,{type:t}={}){let{combo:n,type:s}=I(e),o=E(n),r=t||s,l=g.get(o)?.get(r);if(!l?.length)return!1;let i=n.bareModifier?ne[n.bareModifier]:n.key,c=!!n.alt,f=!!n.ctrl,u=!!n.shift,h=!!n.meta;n.bareModifier&&r==="keyup"&&(n.bareModifier==="alt"?c=!1:n.bareModifier==="ctrl"?f=!1:n.bareModifier==="shift"?u=!1:n.bareModifier==="meta"&&(h=!1));let d={type:r,key:i,code:n.code||null,ctrlKey:f,shiftKey:u,altKey:c,metaKey:h,repeat:!1,preventDefault(){},stopPropagation(){},stopImmediatePropagation(){},target:null},a=z(l.filter(p=>p.options.when?p.options.when(d)!==!1:!0));return a?(a.handler(d),a.options.once&&M(a.id),!0):!1}_&&$();var re={init:$,destroy:C,setTarget:te,bind:O,unbind:W,registerPlugin:X,unregisterPlugin:x,onBind:V,pause:J,resume:Q,ignoreInput:ee,trigger:oe};export{re as default};
|