The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { |
| 2 | type KeybindingCommand, |
| 3 | type KeybindingShortcut, |
| 4 | type KeybindingWhenNode, |
| 5 | type ResolvedKeybindingRule, |
| 6 | MODEL_PICKER_JUMP_KEYBINDING_COMMANDS, |
| 7 | type ResolvedKeybindingsConfig, |
| 8 | THREAD_JUMP_KEYBINDING_COMMANDS, |
| 9 | type ModelPickerJumpKeybindingCommand, |
| 10 | type ThreadJumpKeybindingCommand, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import { |
| 13 | isMacPlatform, |
| 14 | matchesKeybindingShortcut, |
| 15 | matchesKeybindingShortcutModifiers, |
| 16 | normalizeEventKey, |
| 17 | resolveEventKeys, |
| 18 | type ShortcutEventLike as SharedShortcutEventLike, |
| 19 | type ShortcutModifierStateLike, |
| 20 | } from "@t3tools/shared/keybindings"; |
| 21 | import { isElectron } from "./env"; |
| 22 | import { isEditableFocused } from "./lib/editableFocus"; |
| 23 | import { projectScriptIdFromCommand } from "./projectScripts"; |
| 24 | |
| 25 | export type { ShortcutModifierStateLike } from "@t3tools/shared/keybindings"; |
| 26 | export { shortcutKeyFromEvent } from "@t3tools/shared/keybindings"; |
| 27 | |
| 28 | export interface ShortcutEventLike extends SharedShortcutEventLike { |
| 29 | target?: EventTarget | null; |
| 30 | } |
| 31 | |
| 32 | export interface ShortcutMatchContext { |
| 33 | terminalFocus: boolean; |
| 34 | terminalOpen: boolean; |
| 35 | previewFocus: boolean; |
| 36 | previewOpen: boolean; |
| 37 | isWeb: boolean; |
| 38 | isDesktop: boolean; |
| 39 | editableFocus?: boolean; |
| 40 | [key: string]: boolean; |
| 41 | } |
| 42 | |
| 43 | interface ShortcutMatchOptions { |
| 44 | platform?: string; |
| 45 | context?: Partial<ShortcutMatchContext>; |
| 46 | } |
| 47 | |
| 48 | interface ResolvedShortcutLabelOptions extends ShortcutMatchOptions { |
| 49 | platform?: string; |
| 50 | } |
| 51 | |
| 52 | const TERMINAL_WORD_BACKWARD = "\u001bb"; |
| 53 | const TERMINAL_WORD_FORWARD = "\u001bf"; |
| 54 | const TERMINAL_LINE_START = "\u0001"; |
| 55 | const TERMINAL_LINE_END = "\u0005"; |
| 56 | const TERMINAL_DELETE_TO_LINE_START = "\u0015"; |
| 57 | function resolvePlatform(options: ShortcutMatchOptions | undefined): string { |
| 58 | return options?.platform ?? navigator.platform; |
| 59 | } |
| 60 | |
| 61 | function resolveContext(options: ShortcutMatchOptions | undefined): ShortcutMatchContext { |
| 62 | return { |
| 63 | terminalFocus: false, |
| 64 | terminalOpen: false, |
| 65 | previewFocus: false, |
| 66 | previewOpen: false, |
| 67 | isWeb: !isElectron, |
| 68 | isDesktop: isElectron, |
| 69 | editableFocus: false, |
| 70 | ...options?.context, |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | function evaluateWhenNode(node: KeybindingWhenNode, context: ShortcutMatchContext): boolean { |
| 75 | switch (node.type) { |
| 76 | case "identifier": |
| 77 | if (node.name === "true") return true; |
| 78 | if (node.name === "false") return false; |
| 79 | return Boolean(context[node.name]); |
| 80 | case "not": |
| 81 | return !evaluateWhenNode(node.node, context); |
| 82 | case "and": |
| 83 | return evaluateWhenNode(node.left, context) && evaluateWhenNode(node.right, context); |
| 84 | case "or": |
| 85 | return evaluateWhenNode(node.left, context) || evaluateWhenNode(node.right, context); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | function matchesWhenClause( |
| 90 | whenAst: KeybindingWhenNode | undefined, |
| 91 | context: ShortcutMatchContext, |
| 92 | ): boolean { |
| 93 | if (!whenAst) return true; |
| 94 | return evaluateWhenNode(whenAst, context); |
| 95 | } |
| 96 | |
| 97 | export function shortcutConflictKey( |
| 98 | shortcut: KeybindingShortcut, |
| 99 | platform = navigator.platform, |
| 100 | ): string { |
| 101 | const useMetaForMod = isMacPlatform(platform); |
| 102 | const metaKey = shortcut.metaKey || (shortcut.modKey && useMetaForMod); |
| 103 | const ctrlKey = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod); |
| 104 | return [ |
| 105 | shortcut.key, |
| 106 | metaKey ? "meta" : "", |
| 107 | ctrlKey ? "ctrl" : "", |
| 108 | shortcut.shiftKey ? "shift" : "", |
| 109 | shortcut.altKey ? "alt" : "", |
| 110 | ].join("|"); |
| 111 | } |
| 112 | |
| 113 | export function shortcutForContext( |
| 114 | binding: ResolvedKeybindingRule, |
| 115 | context: Partial<ShortcutMatchContext>, |
| 116 | ) { |
| 117 | const shortcut = binding.shortcut; |
| 118 | // Resolve persisted Mod+Enter against send preference without rewriting saved shortcuts. |
| 119 | if ( |
| 120 | context.composerRequiresModifier && |
| 121 | (binding.command === "composer.sendAlternate" || |
| 122 | binding.command === "composer.sendBackground") && |
| 123 | shortcut.key === "enter" && |
| 124 | shortcut.modKey && |
| 125 | !shortcut.metaKey && |
| 126 | !shortcut.ctrlKey && |
| 127 | !shortcut.altKey && |
| 128 | !shortcut.shiftKey |
| 129 | ) { |
| 130 | return { ...shortcut, shiftKey: true }; |
| 131 | } |
| 132 | return shortcut; |
| 133 | } |
| 134 | |
| 135 | export function effectiveShortcutsForCommand( |
| 136 | keybindings: ResolvedKeybindingsConfig, |
| 137 | command: KeybindingCommand, |
| 138 | options?: ShortcutMatchOptions, |
| 139 | ): KeybindingShortcut[] { |
| 140 | const platform = resolvePlatform(options); |
| 141 | const context = resolveContext(options); |
| 142 | const claimedShortcuts = new Set<string>(); |
| 143 | const effective: KeybindingShortcut[] = []; |
| 144 | |
| 145 | for (let index = keybindings.length - 1; index >= 0; index -= 1) { |
| 146 | const binding = keybindings[index]; |
| 147 | if (!binding) continue; |
| 148 | if (!matchesWhenClause(binding.whenAst, context)) continue; |
| 149 | |
| 150 | const shortcut = shortcutForContext(binding, context); |
| 151 | const conflictKey = shortcutConflictKey(shortcut, platform); |
| 152 | if (claimedShortcuts.has(conflictKey)) { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | claimedShortcuts.add(conflictKey); |
| 157 | if (binding.command === command) { |
| 158 | effective.push(shortcut); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return effective; |
| 163 | } |
| 164 | |
| 165 | function findEffectiveShortcutForCommand( |
| 166 | keybindings: ResolvedKeybindingsConfig, |
| 167 | command: KeybindingCommand, |
| 168 | options?: ShortcutMatchOptions, |
| 169 | ): KeybindingShortcut | null { |
| 170 | return effectiveShortcutsForCommand(keybindings, command, options)[0] ?? null; |
| 171 | } |
| 172 | |
| 173 | function matchesCommandShortcut( |
| 174 | event: ShortcutEventLike, |
| 175 | keybindings: ResolvedKeybindingsConfig, |
| 176 | command: KeybindingCommand, |
| 177 | options?: ShortcutMatchOptions, |
| 178 | ): boolean { |
| 179 | return resolveShortcutCommand(event, keybindings, options) === command; |
| 180 | } |
| 181 | |
| 182 | export function resolveShortcutCommand( |
| 183 | event: ShortcutEventLike, |
| 184 | keybindings: ResolvedKeybindingsConfig, |
| 185 | options?: ShortcutMatchOptions, |
| 186 | ): KeybindingCommand | null { |
| 187 | const platform = resolvePlatform(options); |
| 188 | const context = resolveContext(options); |
| 189 | if (options?.context?.editableFocus === undefined && typeof document !== "undefined") { |
| 190 | context.editableFocus = isEditableFocused(event.target ?? document.activeElement); |
| 191 | } |
| 192 | |
| 193 | for (let index = keybindings.length - 1; index >= 0; index -= 1) { |
| 194 | const binding = keybindings[index]; |
| 195 | if (!binding) continue; |
| 196 | if (!matchesWhenClause(binding.whenAst, context)) continue; |
| 197 | if (!matchesKeybindingShortcut(event, shortcutForContext(binding, context), platform)) continue; |
| 198 | return binding.command; |
| 199 | } |
| 200 | return null; |
| 201 | } |
| 202 | |
| 203 | export function resolveChatShortcutCommand( |
| 204 | event: ShortcutEventLike, |
| 205 | primaryKeybindings: ResolvedKeybindingsConfig, |
| 206 | activeKeybindings: ResolvedKeybindingsConfig, |
| 207 | options?: ShortcutMatchOptions, |
| 208 | ): KeybindingCommand | null { |
| 209 | const primaryCommand = resolveShortcutCommand(event, primaryKeybindings, options); |
| 210 | if (primaryCommand !== null && projectScriptIdFromCommand(primaryCommand) === null) { |
| 211 | return primaryCommand; |
| 212 | } |
| 213 | |
| 214 | const activeCommand = resolveShortcutCommand(event, activeKeybindings, options); |
| 215 | return activeCommand !== null && projectScriptIdFromCommand(activeCommand) !== null |
| 216 | ? activeCommand |
| 217 | : null; |
| 218 | } |
| 219 | |
| 220 | export function formatShortcutKeyLabel(key: string): string { |
| 221 | if (key === " ") return "Space"; |
| 222 | if (key.length === 1) return key.toUpperCase(); |
| 223 | if (key === "escape") return "Esc"; |
| 224 | if (key === "arrowup") return "Up"; |
| 225 | if (key === "arrowdown") return "Down"; |
| 226 | if (key === "arrowleft") return "Left"; |
| 227 | if (key === "arrowright") return "Right"; |
| 228 | if (key === "pageup") return "PageUp"; |
| 229 | if (key === "pagedown") return "PageDown"; |
| 230 | return key.slice(0, 1).toUpperCase() + key.slice(1); |
| 231 | } |
| 232 | |
| 233 | export function formatShortcutLabel( |
| 234 | shortcut: KeybindingShortcut, |
| 235 | platform = navigator.platform, |
| 236 | ): string { |
| 237 | const keyLabel = formatShortcutKeyLabel(shortcut.key); |
| 238 | const useMetaForMod = isMacPlatform(platform); |
| 239 | const showMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod); |
| 240 | const showCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod); |
| 241 | const showAlt = shortcut.altKey; |
| 242 | const showShift = shortcut.shiftKey; |
| 243 | |
| 244 | if (useMetaForMod) { |
| 245 | return `${showCtrl ? "\u2303" : ""}${showAlt ? "\u2325" : ""}${showShift ? "\u21e7" : ""}${showMeta ? "\u2318" : ""}${keyLabel}`; |
| 246 | } |
| 247 | |
| 248 | const parts: string[] = []; |
| 249 | if (showCtrl) parts.push("Ctrl"); |
| 250 | if (showAlt) parts.push("Alt"); |
| 251 | if (showShift) parts.push("Shift"); |
| 252 | if (showMeta) parts.push("Meta"); |
| 253 | parts.push(keyLabel); |
| 254 | return parts.join("+"); |
| 255 | } |
| 256 | |
| 257 | export function shortcutLabelForCommand( |
| 258 | keybindings: ResolvedKeybindingsConfig, |
| 259 | command: KeybindingCommand | null, |
| 260 | options?: string | ResolvedShortcutLabelOptions, |
| 261 | ): string | null { |
| 262 | if (command === null) return null; |
| 263 | const resolvedOptions = |
| 264 | typeof options === "string" |
| 265 | ? ({ platform: options } satisfies ResolvedShortcutLabelOptions) |
| 266 | : options; |
| 267 | const platform = resolvePlatform(resolvedOptions); |
| 268 | const shortcut = findEffectiveShortcutForCommand(keybindings, command, resolvedOptions); |
| 269 | return shortcut ? formatShortcutLabel(shortcut, platform) : null; |
| 270 | } |
| 271 | |
| 272 | export function threadJumpCommandForIndex(index: number): ThreadJumpKeybindingCommand | null { |
| 273 | return THREAD_JUMP_KEYBINDING_COMMANDS[index] ?? null; |
| 274 | } |
| 275 | |
| 276 | export function threadJumpIndexFromCommand(command: string): number | null { |
| 277 | const index = THREAD_JUMP_KEYBINDING_COMMANDS.indexOf(command as ThreadJumpKeybindingCommand); |
| 278 | return index === -1 ? null : index; |
| 279 | } |
| 280 | |
| 281 | export function threadTraversalDirectionFromCommand( |
| 282 | command: string | null, |
| 283 | ): "previous" | "next" | null { |
| 284 | if (command === "thread.previous") return "previous"; |
| 285 | if (command === "thread.next") return "next"; |
| 286 | return null; |
| 287 | } |
| 288 | |
| 289 | export function isThreadNavigationShortcut( |
| 290 | event: ShortcutEventLike, |
| 291 | keybindings: ResolvedKeybindingsConfig, |
| 292 | options?: ShortcutMatchOptions, |
| 293 | ): boolean { |
| 294 | const command = resolveShortcutCommand(event, keybindings, options); |
| 295 | return ( |
| 296 | threadTraversalDirectionFromCommand(command) !== null || |
| 297 | threadJumpIndexFromCommand(command ?? "") !== null |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | export function shouldShowThreadJumpHintsForModifiers( |
| 302 | modifiers: ShortcutModifierStateLike, |
| 303 | keybindings: ResolvedKeybindingsConfig, |
| 304 | options?: ShortcutMatchOptions, |
| 305 | ): boolean { |
| 306 | // Holding a modifier is routine in a shell (Ctrl+C, Ctrl+R), so don't flash hints over it. |
| 307 | if (resolveContext(options).terminalFocus) { |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | const platform = resolvePlatform(options); |
| 312 | |
| 313 | for (const command of THREAD_JUMP_KEYBINDING_COMMANDS) { |
| 314 | const shortcut = findEffectiveShortcutForCommand(keybindings, command, options); |
| 315 | if (!shortcut) continue; |
| 316 | if (matchesKeybindingShortcutModifiers(modifiers, shortcut, platform)) { |
| 317 | return true; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | export function modelPickerJumpCommandForIndex( |
| 325 | index: number, |
| 326 | ): ModelPickerJumpKeybindingCommand | null { |
| 327 | return MODEL_PICKER_JUMP_KEYBINDING_COMMANDS[index] ?? null; |
| 328 | } |
| 329 | |
| 330 | export function modelPickerJumpIndexFromCommand(command: string): number | null { |
| 331 | const index = MODEL_PICKER_JUMP_KEYBINDING_COMMANDS.indexOf( |
| 332 | command as ModelPickerJumpKeybindingCommand, |
| 333 | ); |
| 334 | return index === -1 ? null : index; |
| 335 | } |
| 336 | |
| 337 | export function isTerminalToggleShortcut( |
| 338 | event: ShortcutEventLike, |
| 339 | keybindings: ResolvedKeybindingsConfig, |
| 340 | options?: ShortcutMatchOptions, |
| 341 | ): boolean { |
| 342 | return matchesCommandShortcut(event, keybindings, "terminal.toggle", options); |
| 343 | } |
| 344 | |
| 345 | export function isTerminalSplitShortcut( |
| 346 | event: ShortcutEventLike, |
| 347 | keybindings: ResolvedKeybindingsConfig, |
| 348 | options?: ShortcutMatchOptions, |
| 349 | ): boolean { |
| 350 | return matchesCommandShortcut(event, keybindings, "terminal.split", options); |
| 351 | } |
| 352 | |
| 353 | export function isTerminalSplitVerticalShortcut( |
| 354 | event: ShortcutEventLike, |
| 355 | keybindings: ResolvedKeybindingsConfig, |
| 356 | options?: ShortcutMatchOptions, |
| 357 | ): boolean { |
| 358 | return matchesCommandShortcut(event, keybindings, "terminal.splitVertical", options); |
| 359 | } |
| 360 | |
| 361 | export function isTerminalNewShortcut( |
| 362 | event: ShortcutEventLike, |
| 363 | keybindings: ResolvedKeybindingsConfig, |
| 364 | options?: ShortcutMatchOptions, |
| 365 | ): boolean { |
| 366 | return matchesCommandShortcut(event, keybindings, "terminal.new", options); |
| 367 | } |
| 368 | |
| 369 | export function isTerminalCloseShortcut( |
| 370 | event: ShortcutEventLike, |
| 371 | keybindings: ResolvedKeybindingsConfig, |
| 372 | options?: ShortcutMatchOptions, |
| 373 | ): boolean { |
| 374 | return matchesCommandShortcut(event, keybindings, "terminal.close", options); |
| 375 | } |
| 376 | |
| 377 | export function isDiffToggleShortcut( |
| 378 | event: ShortcutEventLike, |
| 379 | keybindings: ResolvedKeybindingsConfig, |
| 380 | options?: ShortcutMatchOptions, |
| 381 | ): boolean { |
| 382 | return matchesCommandShortcut(event, keybindings, "diff.toggle", options); |
| 383 | } |
| 384 | |
| 385 | export function isSidebarToggleShortcut( |
| 386 | event: ShortcutEventLike, |
| 387 | keybindings: ResolvedKeybindingsConfig, |
| 388 | options?: ShortcutMatchOptions, |
| 389 | ): boolean { |
| 390 | // A focused terminal owns Ctrl chords, such as tmux's Ctrl+B prefix; Cmd never reaches it. |
| 391 | if (event.ctrlKey && options?.context?.terminalFocus) return false; |
| 392 | return matchesCommandShortcut(event, keybindings, "sidebar.toggle", options); |
| 393 | } |
| 394 | |
| 395 | export function isOpenFavoriteEditorShortcut( |
| 396 | event: ShortcutEventLike, |
| 397 | keybindings: ResolvedKeybindingsConfig, |
| 398 | options?: ShortcutMatchOptions, |
| 399 | ): boolean { |
| 400 | return ( |
| 401 | event.repeat !== true && |
| 402 | matchesCommandShortcut(event, keybindings, "editor.openFavorite", options) |
| 403 | ); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Whether the keypress should bold text in the rich-text composer: Mod+B |
| 408 | * without extra modifiers, with text selected. Tiptap binds the same chord, so |
| 409 | * app shortcuts captured ahead of the editor yield only in that case. With no |
| 410 | * selection an app binding on the chord wins (the sidebar toggle by default). |
| 411 | */ |
| 412 | export function isRichTextBoldShortcut( |
| 413 | event: ShortcutEventLike, |
| 414 | selection: Pick<Selection, "isCollapsed"> | null, |
| 415 | ): boolean { |
| 416 | if (event.type !== undefined && event.type !== "keydown") { |
| 417 | return false; |
| 418 | } |
| 419 | return ( |
| 420 | selection !== null && |
| 421 | !selection.isCollapsed && |
| 422 | resolveEventKeys(event).has("b") && |
| 423 | (event.metaKey || event.ctrlKey) && |
| 424 | !event.altKey && |
| 425 | !event.shiftKey |
| 426 | ); |
| 427 | } |
| 428 | |
| 429 | /** Mod+B, Mod+I and Mod+E, the mark chords the rich-text composer binds. */ |
| 430 | export function isRichTextFormattingShortcut(event: ShortcutEventLike): boolean { |
| 431 | if (!(event.metaKey || event.ctrlKey) || event.altKey || event.shiftKey) return false; |
| 432 | const keys = resolveEventKeys(event); |
| 433 | return keys.has("b") || keys.has("i") || keys.has("e"); |
| 434 | } |
| 435 | |
| 436 | export function isTerminalClearShortcut( |
| 437 | event: ShortcutEventLike, |
| 438 | platform = navigator.platform, |
| 439 | ): boolean { |
| 440 | if (event.type !== undefined && event.type !== "keydown") { |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | const key = event.key.toLowerCase(); |
| 445 | |
| 446 | if (key === "l" && event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) { |
| 447 | return true; |
| 448 | } |
| 449 | |
| 450 | return ( |
| 451 | isMacPlatform(platform) && |
| 452 | key === "k" && |
| 453 | event.metaKey && |
| 454 | !event.ctrlKey && |
| 455 | !event.altKey && |
| 456 | !event.shiftKey |
| 457 | ); |
| 458 | } |
| 459 | |
| 460 | export function terminalDeleteShortcutData( |
| 461 | event: ShortcutEventLike, |
| 462 | platform = navigator.platform, |
| 463 | ): string | null { |
| 464 | if (event.type !== undefined && event.type !== "keydown") { |
| 465 | return null; |
| 466 | } |
| 467 | |
| 468 | if (!isMacPlatform(platform)) { |
| 469 | return null; |
| 470 | } |
| 471 | |
| 472 | const key = normalizeEventKey(event.key); |
| 473 | if (key !== "backspace") { |
| 474 | return null; |
| 475 | } |
| 476 | |
| 477 | return event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey |
| 478 | ? TERMINAL_DELETE_TO_LINE_START |
| 479 | : null; |
| 480 | } |
| 481 | |
| 482 | export function terminalNavigationShortcutData( |
| 483 | event: ShortcutEventLike, |
| 484 | platform = navigator.platform, |
| 485 | ): string | null { |
| 486 | if (event.type !== undefined && event.type !== "keydown") { |
| 487 | return null; |
| 488 | } |
| 489 | |
| 490 | if (event.shiftKey) return null; |
| 491 | |
| 492 | const key = normalizeEventKey(event.key); |
| 493 | if (key !== "arrowleft" && key !== "arrowright") { |
| 494 | return null; |
| 495 | } |
| 496 | |
| 497 | const moveWord = key === "arrowleft" ? TERMINAL_WORD_BACKWARD : TERMINAL_WORD_FORWARD; |
| 498 | const moveLine = key === "arrowleft" ? TERMINAL_LINE_START : TERMINAL_LINE_END; |
| 499 | |
| 500 | if (isMacPlatform(platform)) { |
| 501 | if (event.altKey && !event.metaKey && !event.ctrlKey) { |
| 502 | return moveWord; |
| 503 | } |
| 504 | if (event.metaKey && !event.altKey && !event.ctrlKey) { |
| 505 | return moveLine; |
| 506 | } |
| 507 | return null; |
| 508 | } |
| 509 | |
| 510 | if (event.altKey && !event.metaKey && !event.ctrlKey) { |
| 511 | return moveWord; |
| 512 | } |
| 513 | |
| 514 | return null; |
| 515 | } |