Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

keybindingMatching.ts

159 lines4.7 KB

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;
17​
18export interface ShortcutModifierStateLike {
19 metaKey: boolean;
20 ctrlKey: boolean;
21 shiftKey: boolean;
22 altKey: boolean;
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);
78​
79export function normalizeEventKey(key: string): string {
80 const normalized = key.toLowerCase();
81 if (normalized === "esc") return "escape";
82 return normalized;
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;
104​
105export function resolveEventKeys(
106 event: ShortcutEventLike,
107 keyboardLayoutMap?: ShortcutKeyboardLayoutMap,
108 allowDottedIFallback = false,
109): Set<string> {
110 const layoutKey = normalizeEventKey(event.key);
111 const keys = new Set([layoutKey]);
112 if (allowDottedIFallback && layoutKey === "i\u0307") keys.add("i");
113 const letterCode = event.code?.match(/^Key([A-Z])$/)?.[1];
114 const shortcutKey = shortcutKeyFromEvent(event, keyboardLayoutMap);
115 if (letterCode && !/^[a-z]$/.test(shortcutKey)) {
116 keys.add(letterCode.toLowerCase());
117 }
118 keys.add(shortcutKey);
119 // Layout letters supplement, rather than replace, physical punctuation shortcuts.
120 keys.add(shortcutKeyFromEvent(event));
121 return keys;
123​
124export function matchesKeybindingShortcutModifiers(
125 event: ShortcutModifierStateLike,
126 shortcut: KeybindingShortcut,
127 platform: string,
128): boolean {
129 const useMetaForMod = isMacPlatform(platform);
130 const expectedMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
131 const expectedCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
132 return (
133 event.metaKey === expectedMeta &&
134 event.ctrlKey === expectedCtrl &&
135 event.shiftKey === shortcut.shiftKey &&
136 event.altKey === shortcut.altKey
137 );
139​
140export function matchesKeybindingShortcut(
141 event: ShortcutEventLike,
142 shortcut: KeybindingShortcut,
143 platform: string,
144 keyboardLayoutMap?: ShortcutKeyboardLayoutMap,
145 allowDottedIFallback = true,
146): boolean {
147 if (
148 !isMacPlatform(platform) &&
149 event.getModifierState?.("AltGraph") &&
150 !/^[a-z0-9]$/i.test(event.key)
151 )
152 return false;
153 if (!matchesKeybindingShortcutModifiers(event, shortcut, platform)) return false;
154 return resolveEventKeys(
155 event,
156 isMacPlatform(platform) && event.altKey ? keyboardLayoutMap : undefined,
157 allowDottedIFallback,
158 ).has(shortcut.key);