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