UNPKG

@tanstack/hotkeys

Version:

Type-safe, framework-agnostic keyboard hotkey management for the browser

69 lines (67 loc) • 3.15 kB
import { keysEqual, ownValue } from "./_keys.js"; import { MODIFIER_ALIASES } from "./constants.js"; // src/_chord.ts /** * Resolves a modifier token or alias case-insensitively: `ctrl` → `Control`, * `cmd` → `Meta`, `mod` → `Mod`. Returns undefined for anything else. */ function modifierAlias(token) { return ownValue(MODIFIER_ALIASES, token.toLowerCase()); } /** Splits modifier tokens while retaining a trailing literal plus: `Control++` → `['Control', '+']`. */ function splitChordParts(hotkey) { if (!hotkey.endsWith("+")) return hotkey.split("+"); const parts = hotkey.slice(0, -1).split("+"); if (parts.at(-1) === "") parts.pop(); parts.push("+"); return parts; } /** The code inside a physical token: `[KeyS]` → `KeyS`. Undefined for anything else. */ function physicalCodeOf(token) { return token.startsWith("[") && token.endsWith("]") && isCodeName(token.slice(1, -1)) ? token.slice(1, -1) : void 0; } /** Whether a string has the shape of a `KeyboardEvent.code`, such as `KeyS`. */ function isCodeName(code) { return /^[A-Za-z][A-Za-z0-9]*$/.test(code); } /** Whether a `KeyboardEvent.code` is a modifier key, such as `ShiftLeft`. */ function isModifierCode(code) { return /^(Control|Shift|Alt|Meta)(Left|Right)$/.test(code); } /** Rejects reserved physical-token syntax in logical object fields while allowing Unicode characters. */ function assertLogicalKey(key) { if (/^\[.*\]$/s.test(key.trim())) throw new Error(`[hotkeys] Put '${key}' in the code field, not key`); } /** * Whether a key is written like a key the library knows: one character, * F1-F24, or a capitalized name such as `Escape`. Key names are * case-sensitive, so `escape` and `f1` don't qualify. */ function isKeyNameShape(key) { return /^(?:.|F(?:[1-9]|1\d|2[0-4])|[A-Z][a-z][A-Za-z\d]*)$/su.test(key); } /** * Whether a parsed chord has a key that isn't a modifier. A chord without one * registers nothing: an empty key would match key-less events, and a * modifier-only chord isn't supported. */ function chordHasKey(chord) { return chord.code !== void 0 ? !!chord.code && !isModifierCode(chord.code) : !!chord.key && !modifierAlias(chord.key); } /** * Whether two parsed chords have the same identity: equal modifiers, and the * same physical code or (case-insensitively) the same logical key. A code and * a key never match, even when one keyboard event could produce both. */ function parsedChordsEqual(a, b) { return a.ctrl === b.ctrl && a.alt === b.alt && a.shift === b.shift && a.meta === b.meta && (a.code !== void 0 || b.code !== void 0 ? a.code === b.code : keysEqual(a.key, b.key)); } /** Whether two parsed step lists have the same length and equal chords. */ function parsedStepsEqual(a, b) { return a.length === b.length && a.every((step, index) => parsedChordsEqual(step, b[index])); } /** The steps of a hotkey: a sequence's chords, or a chord as one step. */ function hotkeySteps(hotkey) { return Array.isArray(hotkey) ? hotkey : [hotkey]; } export { assertLogicalKey, chordHasKey, hotkeySteps, isCodeName, isKeyNameShape, isModifierCode, modifierAlias, parsedChordsEqual, parsedStepsEqual, physicalCodeOf, splitChordParts };