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

keybindings.ts

294 lines8.2 KB

1import {
2 type KeybindingCommand,
3 type KeybindingShortcut,
4 type KeybindingWhenNode,
5 type ResolvedKeybindingsConfig,
6} from "@t3tools/contracts";
7import { isMacPlatform } from "./lib/utils";
8​
9export interface ShortcutEventLike {
10 type?: string;
11 key: string;
12 metaKey: boolean;
13 ctrlKey: boolean;
14 shiftKey: boolean;
15 altKey: boolean;
17​
18export interface ShortcutMatchContext {
19 terminalFocus: boolean;
20 terminalOpen: boolean;
21 [key: string]: boolean;
23​
24interface ShortcutMatchOptions {
25 platform?: string;
26 context?: Partial<ShortcutMatchContext>;
28​
29const TERMINAL_WORD_BACKWARD = "\u001bb";
30const TERMINAL_WORD_FORWARD = "\u001bf";
31const TERMINAL_LINE_START = "\u0001";
32const TERMINAL_LINE_END = "\u0005";
33​
34function normalizeEventKey(key: string): string {
35 const normalized = key.toLowerCase();
36 if (normalized === "esc") return "escape";
37 return normalized;
39​
40function matchesShortcut(
41 event: ShortcutEventLike,
42 shortcut: KeybindingShortcut,
43 platform = navigator.platform,
44): boolean {
45 const key = normalizeEventKey(event.key);
46 if (key !== shortcut.key) return false;
47​
48 const useMetaForMod = isMacPlatform(platform);
49 const expectedMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
50 const expectedCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
51 return (
52 event.metaKey === expectedMeta &&
53 event.ctrlKey === expectedCtrl &&
54 event.shiftKey === shortcut.shiftKey &&
55 event.altKey === shortcut.altKey
56 );
58​
59function resolvePlatform(options: ShortcutMatchOptions | undefined): string {
60 return options?.platform ?? navigator.platform;
62​
63function resolveContext(options: ShortcutMatchOptions | undefined): ShortcutMatchContext {
64 return {
65 terminalFocus: false,
66 terminalOpen: false,
67 ...options?.context,
68 };
70​
71function evaluateWhenNode(node: KeybindingWhenNode, context: ShortcutMatchContext): boolean {
72 switch (node.type) {
73 case "identifier":
74 if (node.name === "true") return true;
75 if (node.name === "false") return false;
76 return Boolean(context[node.name]);
77 case "not":
78 return !evaluateWhenNode(node.node, context);
79 case "and":
80 return evaluateWhenNode(node.left, context) && evaluateWhenNode(node.right, context);
81 case "or":
82 return evaluateWhenNode(node.left, context) || evaluateWhenNode(node.right, context);
83 }
85​
86function matchesWhenClause(
87 whenAst: KeybindingWhenNode | undefined,
88 context: ShortcutMatchContext,
89): boolean {
90 if (!whenAst) return true;
91 return evaluateWhenNode(whenAst, context);
93​
94function matchesCommandShortcut(
95 event: ShortcutEventLike,
96 keybindings: ResolvedKeybindingsConfig,
97 command: KeybindingCommand,
98 options?: ShortcutMatchOptions,
99): boolean {
100 return resolveShortcutCommand(event, keybindings, options) === command;
102​
103export function resolveShortcutCommand(
104 event: ShortcutEventLike,
105 keybindings: ResolvedKeybindingsConfig,
106 options?: ShortcutMatchOptions,
107): string | null {
108 const platform = resolvePlatform(options);
109 const context = resolveContext(options);
110​
111 for (let index = keybindings.length - 1; index >= 0; index -= 1) {
112 const binding = keybindings[index];
113 if (!binding) continue;
114 if (!matchesWhenClause(binding.whenAst, context)) continue;
115 if (!matchesShortcut(event, binding.shortcut, platform)) continue;
116 return binding.command;
117 }
118 return null;
120​
121function formatShortcutKeyLabel(key: string): string {
122 if (key === " ") return "Space";
123 if (key.length === 1) return key.toUpperCase();
124 if (key === "escape") return "Esc";
125 if (key === "arrowup") return "Up";
126 if (key === "arrowdown") return "Down";
127 if (key === "arrowleft") return "Left";
128 if (key === "arrowright") return "Right";
129 return key.slice(0, 1).toUpperCase() + key.slice(1);
131​
132export function formatShortcutLabel(
133 shortcut: KeybindingShortcut,
134 platform = navigator.platform,
135): string {
136 const keyLabel = formatShortcutKeyLabel(shortcut.key);
137 const useMetaForMod = isMacPlatform(platform);
138 const showMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
139 const showCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
140 const showAlt = shortcut.altKey;
141 const showShift = shortcut.shiftKey;
142​
143 if (useMetaForMod) {
144 return `${showCtrl ? "\u2303" : ""}${showAlt ? "\u2325" : ""}${showShift ? "\u21e7" : ""}${showMeta ? "\u2318" : ""}${keyLabel}`;
145 }
146​
147 const parts: string[] = [];
148 if (showCtrl) parts.push("Ctrl");
149 if (showAlt) parts.push("Alt");
150 if (showShift) parts.push("Shift");
151 if (showMeta) parts.push("Meta");
152 parts.push(keyLabel);
153 return parts.join("+");
155​
156export function shortcutLabelForCommand(
157 keybindings: ResolvedKeybindingsConfig,
158 command: KeybindingCommand,
159 platform = navigator.platform,
160): string | null {
161 for (let index = keybindings.length - 1; index >= 0; index -= 1) {
162 const binding = keybindings[index];
163 if (!binding || binding.command !== command) continue;
164 return formatShortcutLabel(binding.shortcut, platform);
165 }
166 return null;
168​
169export function isTerminalToggleShortcut(
170 event: ShortcutEventLike,
171 keybindings: ResolvedKeybindingsConfig,
172 options?: ShortcutMatchOptions,
173): boolean {
174 return matchesCommandShortcut(event, keybindings, "terminal.toggle", options);
176​
177export function isTerminalSplitShortcut(
178 event: ShortcutEventLike,
179 keybindings: ResolvedKeybindingsConfig,
180 options?: ShortcutMatchOptions,
181): boolean {
182 return matchesCommandShortcut(event, keybindings, "terminal.split", options);
184​
185export function isTerminalNewShortcut(
186 event: ShortcutEventLike,
187 keybindings: ResolvedKeybindingsConfig,
188 options?: ShortcutMatchOptions,
189): boolean {
190 return matchesCommandShortcut(event, keybindings, "terminal.new", options);
192​
193export function isTerminalCloseShortcut(
194 event: ShortcutEventLike,
195 keybindings: ResolvedKeybindingsConfig,
196 options?: ShortcutMatchOptions,
197): boolean {
198 return matchesCommandShortcut(event, keybindings, "terminal.close", options);
200​
201export function isDiffToggleShortcut(
202 event: ShortcutEventLike,
203 keybindings: ResolvedKeybindingsConfig,
204 options?: ShortcutMatchOptions,
205): boolean {
206 return matchesCommandShortcut(event, keybindings, "diff.toggle", options);
208​
209export function isChatNewShortcut(
210 event: ShortcutEventLike,
211 keybindings: ResolvedKeybindingsConfig,
212 options?: ShortcutMatchOptions,
213): boolean {
214 return matchesCommandShortcut(event, keybindings, "chat.new", options);
216​
217export function isChatNewLocalShortcut(
218 event: ShortcutEventLike,
219 keybindings: ResolvedKeybindingsConfig,
220 options?: ShortcutMatchOptions,
221): boolean {
222 return matchesCommandShortcut(event, keybindings, "chat.newLocal", options);
224​
225export function isOpenFavoriteEditorShortcut(
226 event: ShortcutEventLike,
227 keybindings: ResolvedKeybindingsConfig,
228 options?: ShortcutMatchOptions,
229): boolean {
230 return matchesCommandShortcut(event, keybindings, "editor.openFavorite", options);
232​
233export function isTerminalClearShortcut(
234 event: ShortcutEventLike,
235 platform = navigator.platform,
236): boolean {
237 if (event.type !== undefined && event.type !== "keydown") {
238 return false;
239 }
240​
241 const key = event.key.toLowerCase();
242​
243 if (key === "l" && event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
244 return true;
245 }
246​
247 return (
248 isMacPlatform(platform) &&
249 key === "k" &&
250 event.metaKey &&
251 !event.ctrlKey &&
252 !event.altKey &&
253 !event.shiftKey
254 );
256​
257export function terminalNavigationShortcutData(
258 event: ShortcutEventLike,
259 platform = navigator.platform,
260): string | null {
261 if (event.type !== undefined && event.type !== "keydown") {
262 return null;
263 }
264​
265 if (event.shiftKey) return null;
266​
267 const key = normalizeEventKey(event.key);
268 if (key !== "arrowleft" && key !== "arrowright") {
269 return null;
270 }
271​
272 const moveWord = key === "arrowleft" ? TERMINAL_WORD_BACKWARD : TERMINAL_WORD_FORWARD;
273 const moveLine = key === "arrowleft" ? TERMINAL_LINE_START : TERMINAL_LINE_END;
274​
275 if (isMacPlatform(platform)) {
276 if (event.altKey && !event.metaKey && !event.ctrlKey) {
277 return moveWord;
278 }
279 if (event.metaKey && !event.altKey && !event.ctrlKey) {
280 return moveLine;
281 }
282 return null;
283 }
284​
285 if (event.ctrlKey && !event.metaKey && !event.altKey) {
286 return moveWord;
287 }
288​
289 if (event.altKey && !event.metaKey && !event.ctrlKey) {
290 return moveWord;
291 }
292​
293 return null;