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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | 1x 1x 1x 443x 443x 443x 443x 442x 442x 1x 1x 1x 455x 455x 455x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 312x 312x 312x 312x 312x 312x 312x 312x 312x 312x 312x 312x 312x 312x 502x 233x 232x 233x 233x 269x 271x 502x 312x 312x 312x 1x 1x 1x 354x 354x 354x 59x 59x 354x 46x 46x 1x 1x 1x 628x 628x 628x 1x 1x 1x 25x 25x 25x 25x 25x 25x 1x 1x 1x 2x 8x 8x 8x 8x 2x 1x 1x 1x 2x 2x 2x 1x 1x 1x 95x 95x 95x 95x 95x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 95x 95x 95x 95x 90x 95x 95x | import type { KeyBinding, ParsedBinding } from './types';
/**
* Detects whether the current platform is macOS. Used to expand the `mod`
* token and to select the `mac` branch of a platform-aware `KeyBinding`.
* Falls back to `navigator.platform` for broad compatibility; the newer
* `userAgentData` API is preferred when available.
* @returns `true` on macOS, `false` otherwise.
* @example
* ```ts
* isMac(); // true on macOS
* ```
* @source
*/
export function isMac(): boolean {
// `userAgentData` is a not-yet-standardized browser API absent from the DOM lib types;
// widen the trusted `navigator` global to read it safely.
const uaData = (navigator as Navigator & { userAgentData?: { platform?: string } }).userAgentData;
if (uaData?.platform) return /mac/i.test(uaData.platform);
return /mac/i.test(navigator.platform);
}
/**
* Resolves a platform-aware `KeyBinding` down to a single string for the
* current platform. Pass-through when already a string.
* @param binding - Shared binding string, or a `{ mac, other }` object.
* @returns The platform-specific binding string.
* @example
* ```ts
* resolveBinding("mod+s"); // => "mod+s"
* resolveBinding({ mac: "meta+s", other: "ctrl+shift+s" }); // => "meta+s" on macOS
* ```
* @source
*/
export function resolveBinding(binding: KeyBinding): string {
if (typeof binding === 'string') return binding;
return isMac() ? binding.mac : binding.other;
}
type ModifierFlag = 'meta' | 'ctrl' | 'alt' | 'shift';
const MOD_ALIASES: Record<string, ModifierFlag> = {
meta: 'meta',
cmd: 'meta',
command: 'meta',
ctrl: 'ctrl',
control: 'ctrl',
alt: 'alt',
option: 'alt',
shift: 'shift',
};
/**
* Maps friendly key aliases used in sequential bindings to the lowercased
* `KeyboardEvent.key` value they should match against. Anything not listed
* matches its own lowercased token (e.g. `a` → `"a"`).
*/
const KEY_ALIASES: Record<string, string> = {
up: 'arrowup',
uparrow: 'arrowup',
down: 'arrowdown',
downarrow: 'arrowdown',
left: 'arrowleft',
leftarrow: 'arrowleft',
right: 'arrowright',
rightarrow: 'arrowright',
esc: 'escape',
enter: 'enter',
return: 'enter',
space: ' ',
spacebar: ' ',
};
/** Display symbols for sequence keys that read better than the raw name. */
const SEQUENCE_SYMBOLS: Record<string, string> = {
arrowup: '↑',
arrowdown: '↓',
arrowleft: '←',
arrowright: '→',
escape: 'Esc',
enter: 'Enter',
' ': 'Space',
};
/**
* Parses a key-binding string such as `"mod+shift+r"` into a `ParsedBinding`.
* The final `+`-separated token is treated as the key; earlier tokens are
* modifiers. The `mod` token expands to `meta` on macOS and `ctrl` elsewhere.
* @param binding - The key-binding string. Tokens are case-insensitive.
* @returns The parsed binding, ready for matching against a `KeyboardEvent`.
* @example
* ```ts
* parseBinding("mod+shift+r");
* // => on macOS: { meta: true, ctrl: false, alt: false, shift: true, key: "r" }
* // => on other: { meta: false, ctrl: true, alt: false, shift: true, key: "r" }
* ```
* @source
*/
export function parseBinding(binding: string): ParsedBinding {
const tokens = binding
.split('+')
.map((t) => t.trim().toLowerCase())
.filter(Boolean);
const parsed: ParsedBinding = {
meta: false,
ctrl: false,
alt: false,
shift: false,
key: '',
};
const mac = isMac();
const last = tokens.pop() ?? '';
for (const token of tokens) {
if (token === 'mod') {
if (mac) parsed.meta = true;
else parsed.ctrl = true;
continue;
}
const flag = MOD_ALIASES[token];
if (flag) parsed[flag] = true;
}
parsed.key = normalizeKey(last);
return parsed;
}
/**
* Checks whether a `KeyboardEvent` matches a `ParsedBinding`. Matching is
* strict on modifiers — any modifier that is not required in the binding
* must not be held down, so `ctrl+s` will not also fire for `ctrl+shift+s`.
* Key comparison is case-insensitive via `event.key`.
* @param event - The incoming keyboard event.
* @param binding - Parsed binding returned from {@link parseBinding}.
* @returns `true` if the event matches the binding.
* @example
* ```ts
* const b = parseBinding("mod+shift+r");
* matches(event, b); // true when ⌘⇧R (mac) or Ctrl+Shift+R (other)
* ```
* @source
*/
export function matches(event: KeyboardEvent, binding: ParsedBinding): boolean {
if (event.metaKey !== binding.meta) return false;
if (event.ctrlKey !== binding.ctrl) return false;
if (event.altKey !== binding.alt) return false;
// `?` is produced by Shift+/ — event.key is already "?", so don't require shift
// separately. For other keys, enforce the shift flag strictly.
if (binding.key !== '?' && event.shiftKey !== binding.shift) return false;
return normalizeKey(event.key) === binding.key;
}
/**
* Normalizes a single binding token (e.g. `"UpArrow"`, `"Esc"`, `"A"`) to the
* lowercased `KeyboardEvent.key` value it should match. Unknown tokens map to
* their own lowercased form.
* @param token - A raw key token from a binding string.
* @returns The normalized key, comparable against a lowercased `event.key`.
* @example
* ```ts
* normalizeKey("UpArrow"); // "arrowup"
* normalizeKey("Esc"); // "escape"
* normalizeKey("A"); // "a"
* ```
* @source
*/
export function normalizeKey(token: string): string {
const lower = token.trim().toLowerCase();
return KEY_ALIASES[lower] ?? lower;
}
/**
* Parses a sequential binding string such as `"up+up+down+down+left+right"`
* into an ordered array of normalized keys. Unlike {@link parseBinding}, every
* token is a key in the sequence — there are no modifiers.
* @param binding - The sequence binding string. Tokens are case-insensitive.
* @returns The ordered, normalized keys the user must press in turn.
* @example
* ```ts
* parseSequence("up+up+down+down+left+right");
* // => ["arrowup", "arrowup", "arrowdown", "arrowdown", "arrowleft", "arrowright"]
* ```
* @source
*/
export function parseSequence(binding: string): string[] {
return binding
.split('+')
.map((t) => t.trim())
.filter(Boolean)
.map(normalizeKey);
}
/**
* Formats a sequential binding into per-step display labels for the help
* modal — arrows become `↑ ↓ ← →`, named keys are title-cased, and single
* characters are upper-cased.
* @param binding - The sequence binding string (same shape as in config).
* @returns Labels in press order, e.g. `["↑", "↑", "↓", "↓", "←", "→"]`.
* @example
* ```ts
* formatSequenceTokens("up+up+down+down+left+right"); // ["↑","↑","↓","↓","←","→"]
* ```
* @source
*/
export function formatSequenceTokens(binding: string): string[] {
return parseSequence(binding).map((key) => {
const symbol = SEQUENCE_SYMBOLS[key];
if (symbol) return symbol;
return key.length === 1 ? key.toUpperCase() : key.charAt(0).toUpperCase() + key.slice(1);
});
}
/**
* Formats a key-binding string into a human-readable label for display in
* the help modal. Uses platform-appropriate symbols (⌘ ⌥ ⌃ ⇧) on macOS
* and spelled-out names (Ctrl, Alt, Shift) elsewhere.
* @param binding - The key-binding source (same shape as in config).
* @returns A pretty label such as `"⌘⇧R"` or `"Ctrl+Shift+R"`.
* @example
* ```ts
* formatBinding("mod+shift+r"); // "⌘⇧R" on macOS, "Ctrl+Shift+R" on other
* formatBinding("shift+?"); // "⇧?" on macOS, "Shift+?" on other
* ```
* @source
*/
export function formatBinding(binding: KeyBinding): string {
const tokens = formatBindingTokens(binding);
return isMac() ? tokens.join('') : tokens.join('+');
}
/**
* Like {@link formatBinding}, but returns the individual key / modifier
* labels as an array instead of a pre-joined string. Useful when each token
* should render as its own UI element (e.g. chip per key in the help modal)
* so layout can flex or wrap.
* @param binding - The key-binding source (same shape as in config).
* @returns Array of labels in press order, e.g. `["⌘", "⇧", "R"]` or
* `["Ctrl", "Shift", "R"]`.
* @example
* ```ts
* formatBindingTokens("mod+shift+r"); // ["⌘", "⇧", "R"] on macOS
* formatBindingTokens("shift+?"); // ["⇧", "?"]
* ```
* @source
*/
export function formatBindingTokens(binding: KeyBinding): string[] {
const mac = isMac();
const resolved = resolveBinding(binding);
const tokens = resolved.split('+').map((t) => t.trim().toLowerCase());
const last = tokens.pop() ?? '';
const symbols: Record<string, string> = mac
? {
mod: '⌘',
meta: '⌘',
cmd: '⌘',
command: '⌘',
ctrl: '⌃',
control: '⌃',
alt: '⌥',
option: '⌥',
shift: '⇧',
}
: {
mod: 'Ctrl',
meta: 'Meta',
cmd: 'Cmd',
command: 'Cmd',
ctrl: 'Ctrl',
control: 'Ctrl',
alt: 'Alt',
option: 'Alt',
shift: 'Shift',
};
const modifiers = tokens.map((t) => symbols[t] ?? t);
const normalizedKey = normalizeKey(last);
const keyLabel =
SEQUENCE_SYMBOLS[normalizedKey] ??
(last.length === 1 ? last.toUpperCase() : last.charAt(0).toUpperCase() + last.slice(1));
return [...modifiers, keyLabel];
}
|