| ... | ... | @@ -4,6 +4,7 @@ import { |
| 4 | 4 | type KeybindingWhenNode, |
| 5 | 5 | type ResolvedKeybindingsConfig, |
| 6 | 6 | } from "@t3tools/contracts"; |
| 7 | import { isMacPlatform } from "./lib/utils"; |
| 7 | 8 | |
| 8 | 9 | export interface ShortcutEventLike { |
| 9 | 10 | key: string; |
| ... | ... | @@ -19,17 +20,11 @@ export interface ShortcutMatchContext { |
| 19 | 20 | [key: string]: boolean; |
| 20 | 21 | } |
| 21 | 22 | |
| 22 | | export type ResolvedKeybindings = ResolvedKeybindingsConfig; |
| 23 | | |
| 24 | 23 | interface ShortcutMatchOptions { |
| 25 | 24 | platform?: string; |
| 26 | 25 | context?: Partial<ShortcutMatchContext>; |
| 27 | 26 | } |
| 28 | 27 | |
| 29 | | function isMacPlatform(platform: string): boolean { |
| 30 | | return /mac|iphone|ipad|ipod/i.test(platform); |
| 31 | | } |
| 32 | | |
| 33 | 28 | function normalizeEventKey(key: string): string { |
| 34 | 29 | const normalized = key.toLowerCase(); |
| 35 | 30 | if (normalized === "esc") return "escape"; |
| ... | ... | @@ -83,7 +78,7 @@ function evaluateWhenNode(node: KeybindingWhenNode, context: ShortcutMatchContex |
| 83 | 78 | } |
| 84 | 79 | |
| 85 | 80 | function matchesWhenClause( |
| 86 | | whenAst: ResolvedKeybindings[number]["whenAst"], |
| 81 | whenAst: ResolvedKeybindingsConfig[number]["whenAst"], |
| 87 | 82 | context: ShortcutMatchContext, |
| 88 | 83 | ): boolean { |
| 89 | 84 | if (!whenAst) return true; |
| ... | ... | @@ -92,7 +87,7 @@ function matchesWhenClause( |
| 92 | 87 | |
| 93 | 88 | function matchesCommandShortcut( |
| 94 | 89 | event: ShortcutEventLike, |
| 95 | | keybindings: ResolvedKeybindings, |
| 90 | keybindings: ResolvedKeybindingsConfig, |
| 96 | 91 | command: KeybindingCommand, |
| 97 | 92 | options?: ShortcutMatchOptions, |
| 98 | 93 | ): boolean { |
| ... | ... | @@ -101,7 +96,7 @@ function matchesCommandShortcut( |
| 101 | 96 | |
| 102 | 97 | function resolveShortcutCommand( |
| 103 | 98 | event: ShortcutEventLike, |
| 104 | | keybindings: ResolvedKeybindings, |
| 99 | keybindings: ResolvedKeybindingsConfig, |
| 105 | 100 | options?: ShortcutMatchOptions, |
| 106 | 101 | ): KeybindingCommand | null { |
| 107 | 102 | const platform = resolvePlatform(options); |
| ... | ... | @@ -128,7 +123,10 @@ function formatShortcutKeyLabel(key: string): string { |
| 128 | 123 | return key.slice(0, 1).toUpperCase() + key.slice(1); |
| 129 | 124 | } |
| 130 | 125 | |
| 131 | | export function formatShortcutLabel(shortcut: KeybindingShortcut, platform = navigator.platform): string { |
| 126 | export function formatShortcutLabel( |
| 127 | shortcut: KeybindingShortcut, |
| 128 | platform = navigator.platform, |
| 129 | ): string { |
| 132 | 130 | const keyLabel = formatShortcutKeyLabel(shortcut.key); |
| 133 | 131 | const useMetaForMod = isMacPlatform(platform); |
| 134 | 132 | const showMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod); |
| ... | ... | @@ -150,7 +148,7 @@ export function formatShortcutLabel(shortcut: KeybindingShortcut, platform = nav |
| 150 | 148 | } |
| 151 | 149 | |
| 152 | 150 | export function shortcutLabelForCommand( |
| 153 | | keybindings: ResolvedKeybindings, |
| 151 | keybindings: ResolvedKeybindingsConfig, |
| 154 | 152 | command: KeybindingCommand, |
| 155 | 153 | platform = navigator.platform, |
| 156 | 154 | ): string | null { |
| ... | ... | @@ -164,7 +162,7 @@ export function shortcutLabelForCommand( |
| 164 | 162 | |
| 165 | 163 | export function isTerminalToggleShortcut( |
| 166 | 164 | event: ShortcutEventLike, |
| 167 | | keybindings: ResolvedKeybindings, |
| 165 | keybindings: ResolvedKeybindingsConfig, |
| 168 | 166 | options?: ShortcutMatchOptions, |
| 169 | 167 | ): boolean { |
| 170 | 168 | return matchesCommandShortcut(event, keybindings, "terminal.toggle", options); |
| ... | ... | @@ -172,7 +170,7 @@ export function isTerminalToggleShortcut( |
| 172 | 170 | |
| 173 | 171 | export function isTerminalSplitShortcut( |
| 174 | 172 | event: ShortcutEventLike, |
| 175 | | keybindings: ResolvedKeybindings, |
| 173 | keybindings: ResolvedKeybindingsConfig, |
| 176 | 174 | options?: ShortcutMatchOptions, |
| 177 | 175 | ): boolean { |
| 178 | 176 | return matchesCommandShortcut(event, keybindings, "terminal.split", options); |
| ... | ... | @@ -180,7 +178,7 @@ export function isTerminalSplitShortcut( |
| 180 | 178 | |
| 181 | 179 | export function isTerminalNewShortcut( |
| 182 | 180 | event: ShortcutEventLike, |
| 183 | | keybindings: ResolvedKeybindings, |
| 181 | keybindings: ResolvedKeybindingsConfig, |
| 184 | 182 | options?: ShortcutMatchOptions, |
| 185 | 183 | ): boolean { |
| 186 | 184 | return matchesCommandShortcut(event, keybindings, "terminal.new", options); |
| ... | ... | @@ -188,7 +186,7 @@ export function isTerminalNewShortcut( |
| 188 | 186 | |
| 189 | 187 | export function isChatNewShortcut( |
| 190 | 188 | event: ShortcutEventLike, |
| 191 | | keybindings: ResolvedKeybindings, |
| 189 | keybindings: ResolvedKeybindingsConfig, |
| 192 | 190 | options?: ShortcutMatchOptions, |
| 193 | 191 | ): boolean { |
| 194 | 192 | return matchesCommandShortcut(event, keybindings, "chat.new", options); |
| ... | ... | @@ -196,7 +194,7 @@ export function isChatNewShortcut( |
| 196 | 194 | |
| 197 | 195 | export function isOpenFavoriteEditorShortcut( |
| 198 | 196 | event: ShortcutEventLike, |
| 199 | | keybindings: ResolvedKeybindings, |
| 197 | keybindings: ResolvedKeybindingsConfig, |
| 200 | 198 | options?: ShortcutMatchOptions, |
| 201 | 199 | ): boolean { |
| 202 | 200 | return matchesCommandShortcut(event, keybindings, "editor.openFavorite", options); |