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 function shortcutKeyFromEvent(
86 event: Pick<ShortcutEventLike, "key" | "code" | "shiftKey" | "altKey">,
87): string {
88 const layoutKey = normalizeEventKey(event.key);
89 if (/^[a-z]$/.test(layoutKey)) return layoutKey;
90 // Only the US layout's glyph at its own key: other layouts and AltGr keep their character.
91 const usOptionLetter = event.altKey ? US_MAC_OPTION_GLYPH_LETTERS.get(event.key) : undefined;
92 if (usOptionLetter && event.code === `Key${usOptionLetter.toUpperCase()}`) return usOptionLetter;
93 // An unshifted "+" is a dedicated zoom key (German, Spanish, Nordic); Czech types it on Digit1.
94 const isZoomPlus = layoutKey === "+" && !event.shiftKey && !event.altKey;
95 if (isZoomPlus && !event.code?.startsWith("Digit")) return layoutKey;
96 const physicalKey = event.code ? EVENT_CODE_SHORTCUT_KEYS[event.code] : undefined;
97 return physicalKey ?? layoutKey;
98}
99
100export function resolveEventKeys(event: ShortcutEventLike): Set<string> {
101 const layoutKey = normalizeEventKey(event.key);
102 const keys = new Set([layoutKey]);
103 const letterCode = event.code?.match(/^Key([A-Z])$/)?.[1];
104 if (letterCode && !/^[a-z]$/.test(layoutKey)) {
105 keys.add(letterCode.toLowerCase());
106 }
107 keys.add(shortcutKeyFromEvent(event));
108 return keys;
109}
110
111export function matchesKeybindingShortcutModifiers(
112 event: ShortcutModifierStateLike,
113 shortcut: KeybindingShortcut,
114 platform: string,
115): boolean {
116 const useMetaForMod = isMacPlatform(platform);
117 const expectedMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
118 const expectedCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
119 return (
120 event.metaKey === expectedMeta &&
121 event.ctrlKey === expectedCtrl &&
122 event.shiftKey === shortcut.shiftKey &&
123 event.altKey === shortcut.altKey
124 );
125}
126
127export function matchesKeybindingShortcut(
128 event: ShortcutEventLike,
129 shortcut: KeybindingShortcut,
130 platform: string,
131): boolean {
132 if (
133 !isMacPlatform(platform) &&
134 event.getModifierState?.("AltGraph") &&
135 !/^[a-z0-9]$/i.test(event.key)
136 )
137 return false;
138 if (!matchesKeybindingShortcutModifiers(event, shortcut, platform)) return false;
139 return resolveEventKeys(event).has(shortcut.key);
140}