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

keybindingMatching.ts

106 lines2.9 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​
49export function isMacPlatform(platform: string): boolean {
50 return /mac|iphone|ipad|ipod/i.test(platform);
52​
53export function normalizeEventKey(key: string): string {
54 const normalized = key.toLowerCase();
55 if (normalized === "esc") return "escape";
56 return normalized;
58​
59export function shortcutKeyFromEvent(event: Pick<ShortcutEventLike, "key" | "code">): string {
60 const layoutKey = normalizeEventKey(event.key);
61 if (/^[a-z]$/.test(layoutKey)) return layoutKey;
62 const physicalKey = event.code ? EVENT_CODE_SHORTCUT_KEYS[event.code] : undefined;
63 return physicalKey ?? layoutKey;
65​
66export function resolveEventKeys(event: ShortcutEventLike): Set<string> {
67 const layoutKey = normalizeEventKey(event.key);
68 const keys = new Set([layoutKey]);
69 const letterCode = event.code?.match(/^Key([A-Z])$/)?.[1];
70 if (letterCode && !/^[a-z]$/.test(layoutKey)) {
71 keys.add(letterCode.toLowerCase());
72 }
73 keys.add(shortcutKeyFromEvent(event));
74 return keys;
76​
77export function matchesKeybindingShortcutModifiers(
78 event: ShortcutModifierStateLike,
79 shortcut: KeybindingShortcut,
80 platform: string,
81): boolean {
82 const useMetaForMod = isMacPlatform(platform);
83 const expectedMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
84 const expectedCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
85 return (
86 event.metaKey === expectedMeta &&
87 event.ctrlKey === expectedCtrl &&
88 event.shiftKey === shortcut.shiftKey &&
89 event.altKey === shortcut.altKey
90 );
92​
93export function matchesKeybindingShortcut(
94 event: ShortcutEventLike,
95 shortcut: KeybindingShortcut,
96 platform: string,
97): boolean {
98 if (
99 !isMacPlatform(platform) &&
100 event.getModifierState?.("AltGraph") &&
101 !/^[a-z0-9]$/i.test(event.key)
102 )
103 return false;
104 if (!matchesKeybindingShortcutModifiers(event, shortcut, platform)) return false;
105 return resolveEventKeys(event).has(shortcut.key);