Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 1x 2x 28x 1x 27x 28x 28x 2x 2x 2x 52x 52x 52x 77x 77x 4x 2x 73x 52x 60x 60x 52x 52x 75x 75x 24x 11x 2x 9x 13x 24x 51x 52x 31x 31x 39x 39x 31x 33x 34x 41x 1x 40x 33x 7x 7x 4x 3x | import type { ParsedChord, ParsedHotkey } from "./types";
/**
* Detects whether the current runtime is a browser with DOM access.
* Returns `false` during server-side rendering.
*/
export function isHotkeysSupported(): boolean {
return typeof window !== "undefined" && typeof document !== "undefined";
}
const APPLE_PLATFORM_RE = /Mac|iPhone|iPod|iPad/i;
/**
* Detects macOS/iOS so the `"mod"` alias can resolve to the Command (Meta) key
* on Apple platforms and to Control everywhere else. SSR-safe (returns `false`
* when `navigator` is unavailable).
*/
export function isMacPlatform(): boolean {
if (typeof navigator === "undefined") {
return false;
}
// `navigator.platform` is deprecated but remains the most reliable
// synchronous signal; prefer `userAgentData.platform` when present and fall
// back to the user-agent string.
const platform =
(navigator as Navigator & { userAgentData?: { platform?: string } })
.userAgentData?.platform ||
navigator.platform ||
"";
const userAgent = navigator.userAgent || "";
return APPLE_PLATFORM_RE.test(platform) || APPLE_PLATFORM_RE.test(userAgent);
}
/**
* `event.key` values that represent modifier keys. A modifier-only `keydown`
* neither starts a combo nor breaks an in-progress sequence.
*/
export const MODIFIER_KEY_NAMES = new Set(["Control", "Shift", "Alt", "Meta"]);
/**
* Maps modifier aliases to their canonical modifier name. `"mod"` is resolved
* against the current platform at parse time.
*/
const MODIFIER_TOKENS: Record<
string,
"ctrl" | "shift" | "alt" | "meta" | "mod"
> = {
ctrl: "ctrl",
control: "ctrl",
ctl: "ctrl",
shift: "shift",
alt: "alt",
option: "alt",
opt: "alt",
meta: "meta",
cmd: "meta",
command: "meta",
win: "meta",
windows: "meta",
super: "meta",
mod: "mod",
};
/**
* Friendly key aliases mapped to the lower-cased canonical `event.key` value
* they should compare against.
*/
const KEY_ALIASES: Record<string, string> = {
esc: "escape",
escape: "escape",
space: " ",
spacebar: " ",
up: "arrowup",
down: "arrowdown",
left: "arrowleft",
right: "arrowright",
arrowup: "arrowup",
arrowdown: "arrowdown",
arrowleft: "arrowleft",
arrowright: "arrowright",
enter: "enter",
return: "enter",
del: "delete",
delete: "delete",
ins: "insert",
tab: "tab",
backspace: "backspace",
plus: "+",
};
/**
* Splits a combo on `+` while treating a literal `+` key correctly
* (`"ctrl++"` → `["ctrl", "+"]`, `"+"` → `["+"]`).
*/
function splitCombo(combo: string): string[] {
const parts = combo.split("+");
const tokens: string[] = [];
for (const part of parts) {
const token = part.trim();
if (token === "") {
if (tokens[tokens.length - 1] !== "+") {
tokens.push("+");
}
} else {
tokens.push(token);
}
}
return tokens;
}
/**
* Normalizes a non-modifier key token to its canonical, lower-cased comparison
* form (resolving friendly aliases such as `esc` → `escape`).
*/
export function normalizeKeyToken(token: string): string {
const lower = token.toLowerCase();
return lower in KEY_ALIASES ? KEY_ALIASES[lower] : lower;
}
/**
* Parses a single combo such as `"mod+shift+k"` into a {@link ParsedChord}.
* The last non-modifier token wins as the key.
*
* @param combo - The combo string (no spaces).
* @param mac - Whether `"mod"` should resolve to `meta` (Apple) instead of `ctrl`.
*/
export function parseCombo(combo: string, mac: boolean): ParsedChord {
const chord: ParsedChord = {
ctrl: false,
shift: false,
alt: false,
meta: false,
key: "",
};
for (const token of splitCombo(combo)) {
const modifier = MODIFIER_TOKENS[token.toLowerCase()];
if (modifier) {
if (modifier === "mod") {
if (mac) {
chord.meta = true;
} else {
chord.ctrl = true;
}
} else {
chord[modifier] = true;
}
continue;
}
chord.key = normalizeKeyToken(token);
}
return chord;
}
/**
* Parses a full hotkey string into a {@link ParsedHotkey}. Space-separated
* combos become an ordered sequence; a single combo yields a length-1 sequence.
*
* @param hotkey - The hotkey string, e.g. `"mod+k"` or `"g i"`.
* @param options - `mac` overrides `"mod"` resolution; defaults to platform detection.
*
* @example
* ```ts
* parseHotkey("mod+k", { mac: false });
* // { raw: "mod+k", chords: [{ ctrl: true, shift: false, alt: false, meta: false, key: "k" }] }
*
* parseHotkey("g i");
* // { raw: "g i", chords: [{ ...key: "g" }, { ...key: "i" }] }
* ```
*/
export function parseHotkey(
hotkey: string,
options: { mac?: boolean } = {}
): ParsedHotkey {
const mac = options.mac ?? isMacPlatform();
const chords = hotkey
.trim()
.split(/\s+/)
.filter((combo) => combo.length > 0)
.map((combo) => parseCombo(combo, mac));
return { raw: hotkey, chords };
}
/**
* Derives a {@link ParsedChord} from a live keyboard event, capturing its
* modifier state and normalized key.
*/
export function eventToChord(event: KeyboardEvent): ParsedChord {
return {
ctrl: event.ctrlKey,
shift: event.shiftKey,
alt: event.altKey,
meta: event.metaKey,
key: (event.key || "").toLowerCase(),
};
}
/**
* Returns `true` when an event-derived chord satisfies a parsed combo. Modifier
* state must match **exactly** (e.g. plain `"a"` does not match `Ctrl+A`).
*/
export function chordMatches(eventChord: ParsedChord, combo: ParsedChord): boolean {
return (
eventChord.ctrl === combo.ctrl &&
eventChord.shift === combo.shift &&
eventChord.alt === combo.alt &&
eventChord.meta === combo.meta &&
combo.key !== "" &&
eventChord.key === combo.key
);
}
/**
* Returns `true` if the event originated from an editable element — an
* `<input>`, `<textarea>`, `<select>`, or `contenteditable` node. Used to
* suppress hotkeys while the user is typing.
*/
export function isEditableTarget(target: EventTarget | null): boolean {
if (target === null || typeof HTMLElement === "undefined") {
return false;
}
if (!(target instanceof HTMLElement)) {
return false;
}
const tagName = target.tagName;
if (tagName === "INPUT" || tagName === "TEXTAREA" || tagName === "SELECT") {
return true;
}
// `isContentEditable` can be `undefined` in some environments (e.g. jsdom);
// coerce to a strict boolean.
return target.isContentEditable === true;
}
|