Open History to see the last commit
1import type { KeybindingShortcut } from "@t3tools/contracts";
2
3// Matching only: the desktop preview guest bundles this file without the rest of the
4// keybindings module, so it stays free of parsing, defaults, and contracts runtime code.
5
6export interface ShortcutEventLike {
7 getModifierState?: (key: "AltGraph") => boolean;
8 type?: string;
9 code?: string;
10 key: string;
11 repeat?: boolean;
12 metaKey: boolean;
13 ctrlKey: boolean;
14 shiftKey: boolean;
15 altKey: boolean;
16}
17
18export interface ShortcutModifierStateLike {
19 metaKey: boolean;
20 ctrlKey: boolean;
21 shiftKey: boolean;
22 altKey: boolean;
23}
24
25export const EVENT_CODE_SHORTCUT_KEYS: Readonly<Record<string, string>> = {
26 Backquote: "`",
27 Backslash: "\\",
28 BracketLeft: "[",
29 BracketRight: "]",
30 Comma: ",",
31 Digit0: "0",
32 Digit1: "1",
33 Digit2: "2",
34 Digit3: "3",
35 Digit4: "4",
36 Digit5: "5",
37 Digit6: "6",
38 Digit7: "7",
39 Digit8: "8",
40 Digit9: "9",
41 Equal: "=",
42 Minus: "-",
43 Period: ".",
44 Quote: "'",
45 Semicolon: ";",
46 Slash: "/",
47};
48
49// macOS US layout Option+letter glyphs (Option+P types "π"). Dead keys (E, I, N, U) are absent.
50const US_MAC_OPTION_GLYPH_LETTERS: ReadonlyMap<string, string> = new Map([
51 ["å", "a"],
52 ["∫", "b"],
53 ["ç", "c"],
54 ["∂", "d"],
55 ["ƒ", "f"],
56 ["©", "g"],
57 ["˙", "h"],
58 ["∆", "j"],
59 ["˚", "k"],
60 ["¬", "l"],
61 ["µ", "m"],
62 ["ø", "o"],
63 ["π", "p"],
64 ["œ", "q"],
65 ["®", "r"],
66 ["ß", "s"],
67 ["†", "t"],
68 ["√", "v"],
69 ["∑", "w"],
70 ["≈", "x"],
71 ["¥", "y"],
72 ["Ω", "z"],
73]);
74
75export function isMacPlatform(platform: string): boolean {
76 return /mac|iphone|ipad|ipod/i.test(platform);
77}
78
79export function normalizeEventKey(key: string): string {
80 const normalized = key.toLowerCase();
81 if (normalized === "esc") return "escape";
82 return normalized;
83}
84
85export type ShortcutKeyboardLayoutMap = Pick<ReadonlyMap<string, string>, "get">;
86
87export function shortcutKeyFromEvent(
88 event: Pick<ShortcutEventLike, "key" | "code" | "shiftKey" | "altKey">,
89 keyboardLayoutMap?: ShortcutKeyboardLayoutMap,
90): string {
91 const layoutKey = normalizeEventKey(event.key);
92 if (/^[a-z]$/.test(layoutKey)) return layoutKey;
93 const mappedKey = event.code ? keyboardLayoutMap?.get(event.code)?.toLowerCase() : undefined;
94 if (mappedKey && /^[a-z]$/.test(mappedKey)) return mappedKey;
95 // Only the US layout's glyph at its own key: other layouts and AltGr keep their character.
96 const usOptionLetter = event.altKey ? US_MAC_OPTION_GLYPH_LETTERS.get(event.key) : undefined;
97 if (usOptionLetter && event.code === `Key${usOptionLetter.toUpperCase()}`) return usOptionLetter;
98 // An unshifted "+" is a dedicated zoom key (German, Spanish, Nordic); Czech types it on Digit1.
99 const isZoomPlus = layoutKey === "+" && !event.shiftKey && !event.altKey;
100 if (isZoomPlus && !event.code?.startsWith("Digit")) return layoutKey;
101 const physicalKey = event.code ? EVENT_CODE_SHORTCUT_KEYS[event.code] : undefined;
102 return physicalKey ?? layoutKey;
103}
104
105export function resolveEventKeys(
106 event: ShortcutEventLike,
107 keyboardLayoutMap?: ShortcutKeyboardLayoutMap,
108): Set<string> {
109 const layoutKey = normalizeEventKey(event.key);
110 const keys = new Set([layoutKey]);
111 const letterCode = event.code?.match(/^Key([A-Z])$/)?.[1];
112 const shortcutKey = shortcutKeyFromEvent(event, keyboardLayoutMap);
113 if (letterCode && !/^[a-z]$/.test(shortcutKey)) {
114 keys.add(letterCode.toLowerCase());
115 }
116 keys.add(shortcutKey);
117 // Layout letters supplement, rather than replace, physical punctuation shortcuts.
118 keys.add(shortcutKeyFromEvent(event));
119 return keys;
120}
121
122export function matchesKeybindingShortcutModifiers(
123 event: ShortcutModifierStateLike,
124 shortcut: KeybindingShortcut,
125 platform: string,
126): boolean {
127 const useMetaForMod = isMacPlatform(platform);
128 const expectedMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
129 const expectedCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
130 return (
131 event.metaKey === expectedMeta &&
132 event.ctrlKey === expectedCtrl &&
133 event.shiftKey === shortcut.shiftKey &&
134 event.altKey === shortcut.altKey
135 );
136}
137
138export function matchesKeybindingShortcut(
139 event: ShortcutEventLike,
140 shortcut: KeybindingShortcut,
141 platform: string,
142 keyboardLayoutMap?: ShortcutKeyboardLayoutMap,
143): boolean {
144 if (
145 !isMacPlatform(platform) &&
146 event.getModifierState?.("AltGraph") &&
147 !/^[a-z0-9]$/i.test(event.key)
148 )
149 return false;
150 if (!matchesKeybindingShortcutModifiers(event, shortcut, platform)) return false;
151 return resolveEventKeys(
152 event,
153 isMacPlatform(platform) && event.altKey ? keyboardLayoutMap : undefined,
154 ).has(shortcut.key);
155}