diff options
| author | Adam <[email protected]> | 2026-02-12 09:49:14 -0600 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-02-12 09:49:14 -0600 |
| commit | ff4414bb152acfddb5c0eb073c38bedc1df4ae14 (patch) | |
| tree | 78381c67d21ef6f089647f6b19e7aa2976840dbc /packages/app/src/context/command.tsx | |
| parent | 56ad2db02055955f926fda0e4a89055b22ead6f9 (diff) | |
| download | opencode-ff4414bb152acfddb5c0eb073c38bedc1df4ae14.tar.gz opencode-ff4414bb152acfddb5c0eb073c38bedc1df4ae14.zip | |
chore: refactor packages/app files (#13236)
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: Frank <[email protected]>
Diffstat (limited to 'packages/app/src/context/command.tsx')
| -rw-r--r-- | packages/app/src/context/command.tsx | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/packages/app/src/context/command.tsx b/packages/app/src/context/command.tsx index e6a16fd4b..b286364c6 100644 --- a/packages/app/src/context/command.tsx +++ b/packages/app/src/context/command.tsx @@ -11,6 +11,7 @@ const IS_MAC = typeof navigator === "object" && /(Mac|iPod|iPhone|iPad)/.test(na const PALETTE_ID = "command.palette" const DEFAULT_PALETTE_KEYBIND = "mod+shift+p" const SUGGESTED_PREFIX = "suggested." +const EDITABLE_KEYBIND_IDS = new Set(["terminal.toggle", "terminal.new"]) function actionId(id: string) { if (!id.startsWith(SUGGESTED_PREFIX)) return id @@ -33,6 +34,11 @@ function signatureFromEvent(event: KeyboardEvent) { return signature(normalizeKey(event.key), event.ctrlKey, event.metaKey, event.shiftKey, event.altKey) } +function isAllowedEditableKeybind(id: string | undefined) { + if (!id) return false + return EDITABLE_KEYBIND_IDS.has(actionId(id)) +} + export type KeybindConfig = string export interface Keybind { @@ -56,6 +62,8 @@ export interface CommandOption { onHighlight?: () => (() => void) | void } +type CommandSource = "palette" | "keybind" | "slash" + export type CommandCatalogItem = { title: string description?: string @@ -169,6 +177,14 @@ export function formatKeybind(config: string): string { return IS_MAC ? parts.join("") : parts.join("+") } +function isEditableTarget(target: EventTarget | null) { + if (!(target instanceof HTMLElement)) return false + if (target.isContentEditable) return true + if (target.closest("[contenteditable='true']")) return true + if (target.closest("input, textarea, select")) return true + return false +} + export const { use: useCommand, provider: CommandProvider } = createSimpleContext({ name: "Command", init: () => { @@ -275,13 +291,18 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex return map }) - const run = (id: string, source?: "palette" | "keybind" | "slash") => { + const optionMap = createMemo(() => { + const map = new Map<string, CommandOption>() for (const option of options()) { - if (option.id === id || option.id === "suggested." + id) { - option.onSelect?.(source) - return - } + map.set(option.id, option) + map.set(actionId(option.id), option) } + return map + }) + + const run = (id: string, source?: CommandSource) => { + const option = optionMap().get(id) + option?.onSelect?.(source) } const showPalette = () => { @@ -292,14 +313,17 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex if (suspended() || dialog.active) return const sig = signatureFromEvent(event) + const isPalette = palette().has(sig) + const option = keymap().get(sig) + + if (isEditableTarget(event.target) && !isPalette && !isAllowedEditableKeybind(option?.id)) return - if (palette().has(sig)) { + if (isPalette) { event.preventDefault() showPalette() return } - const option = keymap().get(sig) if (!option) return event.preventDefault() option.onSelect?.("keybind") @@ -332,7 +356,7 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex return { register, - trigger(id: string, source?: "palette" | "keybind" | "slash") { + trigger(id: string, source?: CommandSource) { run(id, source) }, keybind(id: string) { @@ -351,7 +375,7 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex }, show: showPalette, keybinds(enabled: boolean) { - setStore("suspendCount", (count) => count + (enabled ? -1 : 1)) + setStore("suspendCount", (count) => Math.max(0, count + (enabled ? -1 : 1))) }, suspended, get catalog() { |
