The best way to control Claude Code, Codex and more from any computer or phone

main
ts 121 lines 4.09 KB
Raw
1 import {
2 isModifierPairShortcut,
3 snapShotModifierPairLabel,
4 snapShotShortcutModifierPair,
5 type KeybindingShortcut,
6 type SnapShotModifier,
7 type SnapShotShortcut,
8 } from "@t3tools/contracts";
9 import { parseKeybindingShortcut } from "@t3tools/shared/keybindings";
10
11 import { formatShortcutKeyLabel, formatShortcutLabel, shortcutConflictKey } from "../keybindings";
12 import { isMacPlatform, isWindowsPlatform } from "./utils";
13
14 const DESKTOP_KEY_ALIASES: Readonly<Record<string, string>> = {
15 return: "enter",
16 page_up: "pageup",
17 page_down: "pagedown",
18 left: "arrowleft",
19 right: "arrowright",
20 up: "arrowup",
21 down: "arrowdown",
22 plus: "+",
23 minus: "-",
24 };
25
26 export function parseDesktopSnapShotShortcut(label: string): KeybindingShortcut | null {
27 const text = label.trim().replace(/^Press\s+/i, "");
28 if (!text) return null;
29 const gtk = text.match(/^((?:<[^<>]+>)+)([^<>]+)$/);
30 const input = (gtk ? gtk[1]!.replace(/<([^<>]+)>/g, "$1+") + gtk[2]! : text).replace(
31 /\b(super|win|primary)\b/gi,
32 (modifier) => (modifier.toLowerCase() === "primary" ? "ctrl" : "meta"),
33 );
34 if (input !== "+" && input.endsWith("+") && !input.endsWith("++")) return null;
35 const shortcut = parseKeybindingShortcut(input);
36 if (!shortcut) return null;
37 const key = DESKTOP_KEY_ALIASES[shortcut.key] ?? shortcut.key;
38 if (
39 key !== " " &&
40 !/^[^\s<>]$/u.test(key) &&
41 !/^(?:f(?:[1-9]|1\d|2[0-4])|enter|tab|escape|backspace|delete|insert|home|end|pageup|pagedown|arrow(?:left|right|up|down)|pause|printscreen)$/i.test(
42 key,
43 )
44 ) {
45 return null;
46 }
47 return { ...shortcut, key };
48 }
49
50 export function formatSnapShotShortcutLabel(
51 shortcut: SnapShotShortcut,
52 platform = navigator.platform,
53 ): string {
54 return isModifierPairShortcut(shortcut)
55 ? snapShotModifierPairLabel(snapShotShortcutModifierPair(shortcut), isMacPlatform(platform))
56 : formatShortcutLabel(shortcut, platform);
57 }
58
59 function modifierKeyLabel(modifier: SnapShotModifier, platform: string): string {
60 if (modifier === "shift") return "⇧";
61 if (isMacPlatform(platform)) {
62 if (modifier === "meta") return "⌘";
63 if (modifier === "control") return "⌃";
64 return "⌥";
65 }
66 // oxlint-disable-next-line t3code/no-untranslated-copy -- key names stay English
67 if (modifier === "meta") return isWindowsPlatform(platform) ? "⊞" : "Super";
68 return modifier === "control" ? "Ctrl" : "Alt";
69 }
70
71 export function snapShotShortcutKeyLabels(
72 shortcut: SnapShotShortcut,
73 platform = navigator.platform,
74 ): readonly string[] {
75 if (isModifierPairShortcut(shortcut)) {
76 const label = modifierKeyLabel(snapShotShortcutModifierPair(shortcut), platform);
77 return [label, label];
78 }
79
80 const useMetaForMod = isMacPlatform(platform);
81 const modifiers: readonly [boolean, SnapShotModifier][] = [
82 [shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod), "control"],
83 [shortcut.altKey, "alt"],
84 [shortcut.shiftKey, "shift"],
85 [shortcut.metaKey || (shortcut.modKey && useMetaForMod), "meta"],
86 ];
87 return [
88 ...modifiers
89 .filter(([enabled]) => enabled)
90 .map(([, modifier]) => modifierKeyLabel(modifier, platform)),
91 formatShortcutKeyLabel(shortcut.key),
92 ];
93 }
94
95 export function sameSnapShotShortcut(
96 left: SnapShotShortcut,
97 right: SnapShotShortcut,
98 platform = navigator.platform,
99 ): boolean {
100 if (isModifierPairShortcut(left) || isModifierPairShortcut(right)) {
101 return (
102 isModifierPairShortcut(left) &&
103 isModifierPairShortcut(right) &&
104 snapShotShortcutModifierPair(left) === snapShotShortcutModifierPair(right)
105 );
106 }
107 return shortcutConflictKey(left, platform) === shortcutConflictKey(right, platform);
108 }
109
110 export function snapShotKeybindingConflict<Command extends string>(
111 shortcut: SnapShotShortcut,
112 keybindings: ReadonlyArray<{ readonly command: Command; readonly shortcut: KeybindingShortcut }>,
113 platform = navigator.platform,
114 ): Command | null {
115 if (isModifierPairShortcut(shortcut)) return null;
116 const key = shortcutConflictKey(shortcut, platform);
117 return (
118 keybindings.find((binding) => shortcutConflictKey(binding.shortcut, platform) === key)
119 ?.command ?? null
120 );
121 }