mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(app): paste into unfocused composer and guard terminal Command chords
This commit is contained in:
parent
9a7bfb063c
commit
daefe2cf49
5 changed files with 239 additions and 7 deletions
|
|
@ -8,15 +8,40 @@ const workspace = { name: "TerminalInput", pty: {} } satisfies WorkspaceInput
|
|||
|
||||
test.use({ viewport: { width: 1440, height: 900 } })
|
||||
|
||||
test("clears the terminal line with Command+Delete", async ({ page }) => {
|
||||
test("clears the terminal line with Command+Delete and suppresses printable Command chords", async ({ page }) => {
|
||||
const { pty } = await openSession(page, workspace)
|
||||
const terminal = page.locator('[data-component="terminal"]')
|
||||
await page.keyboard.press("Control+Backquote")
|
||||
await expect(terminal.locator("textarea")).toHaveCount(1)
|
||||
await expect.poll(() => pty.sockets.length).toBe(1)
|
||||
|
||||
await page.keyboard.press("Meta+k")
|
||||
await page.keyboard.press("Meta+a")
|
||||
await page.keyboard.press("Meta+v")
|
||||
await terminal.locator("textarea").evaluate((textarea) => {
|
||||
textarea.dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
key: "v",
|
||||
code: "Period",
|
||||
metaKey: true,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
)
|
||||
const clipboardData = new DataTransfer()
|
||||
clipboardData.setData("text/plain", "echo pasted")
|
||||
textarea.dispatchEvent(
|
||||
new ClipboardEvent("paste", {
|
||||
clipboardData,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
)
|
||||
})
|
||||
await expect.poll(() => pty.sockets[0]!.input.join("")).toBe("echo pasted")
|
||||
|
||||
await page.keyboard.press("Meta+Backspace")
|
||||
await expect.poll(() => pty.sockets[0]!.input.join("")).toBe("\x15")
|
||||
await expect.poll(() => pty.sockets[0]!.input.join("")).toBe("echo pasted\x15")
|
||||
})
|
||||
|
||||
test("reveals the terminal after its first output and hides the native caret", async ({ page }) => {
|
||||
|
|
@ -50,10 +75,31 @@ test("routes typing to the composer unless the open terminal is focused", async
|
|||
await expect(terminal).toHaveCSS("opacity", "1")
|
||||
await page.evaluate(() => {
|
||||
if (document.activeElement instanceof HTMLElement) document.activeElement.blur()
|
||||
document.dispatchEvent(new KeyboardEvent("keydown", { key: "Meta", metaKey: true, bubbles: true, cancelable: true }))
|
||||
document.dispatchEvent(new KeyboardEvent("keyup", { key: "Meta", metaKey: false, bubbles: true, cancelable: true }))
|
||||
})
|
||||
await page.keyboard.type("v")
|
||||
await expect(editor).not.toBeFocused()
|
||||
await expect(editor).toHaveText("")
|
||||
|
||||
await page.evaluate(() => {
|
||||
const clipboardData = new DataTransfer()
|
||||
clipboardData.setData("text/plain", "pasted ")
|
||||
document.body.dispatchEvent(
|
||||
new ClipboardEvent("paste", {
|
||||
clipboardData,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
)
|
||||
})
|
||||
await expect(editor).toBeFocused()
|
||||
await expect(editor).toHaveText("pasted ")
|
||||
await page.keyboard.type("v")
|
||||
await expect(editor).toHaveText("pasted ")
|
||||
await page.keyboard.type("a")
|
||||
await expect(editor).toBeFocused()
|
||||
await expect(editor).toHaveText("a")
|
||||
await expect(editor).toHaveText("pasted a")
|
||||
})
|
||||
|
||||
for (const mount of ["cached", "explicit"] as const) {
|
||||
|
|
|
|||
|
|
@ -34,6 +34,8 @@ import { createSessionRequestModel } from "../requests/model"
|
|||
import type { Region } from "@/runtime/extension/panels"
|
||||
import { SessionLocationMissing } from "./location-missing"
|
||||
|
||||
const metaChordGraceMs = 100
|
||||
|
||||
export function createActiveSessionRegion(input: {
|
||||
session: SessionModel
|
||||
screen: SessionScreenLayout
|
||||
|
|
@ -54,6 +56,8 @@ export function createActiveSessionRegion(input: {
|
|||
const state = createSessionRequestModel()
|
||||
const controls = createComposerControls()
|
||||
let promptRef: HTMLDivElement | undefined
|
||||
let lastMetaAt = 0
|
||||
let lastPasteFocusAt = 0
|
||||
|
||||
createEffect(
|
||||
on(
|
||||
|
|
@ -134,7 +138,23 @@ export function createActiveSessionRegion(input: {
|
|||
}
|
||||
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.key === "Meta" || event.metaKey) lastMetaAt = Date.now()
|
||||
|
||||
if (event.defaultPrevented) return
|
||||
|
||||
if (
|
||||
!event.ctrlKey &&
|
||||
!event.metaKey &&
|
||||
!event.altKey &&
|
||||
event.key.toLowerCase() === "v" &&
|
||||
Date.now() - lastPasteFocusAt < metaChordGraceMs
|
||||
) {
|
||||
lastPasteFocusAt = 0
|
||||
event.preventDefault()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const path = event.composedPath()
|
||||
const target = path.find((item): item is HTMLElement => item instanceof HTMLElement)
|
||||
const active = activeElement()
|
||||
|
|
@ -174,14 +194,76 @@ export function createActiveSessionRegion(input: {
|
|||
return
|
||||
}
|
||||
|
||||
if (event.key.length !== 1 || event.key === "Unidentified" || event.ctrlKey || event.metaKey) return
|
||||
if (
|
||||
event.key.length !== 1 ||
|
||||
event.key === "Unidentified" ||
|
||||
event.ctrlKey ||
|
||||
event.metaKey ||
|
||||
Date.now() - lastMetaAt < metaChordGraceMs
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
if (state.blocked() || input.session.data.isChild() || !promptRef) return
|
||||
promptRef.focus()
|
||||
setCursorPosition(promptRef, prompt.cursor() ?? promptLength(prompt.current()))
|
||||
}
|
||||
|
||||
onMount(() => makeEventListener(document, "keydown", handleKeyDown))
|
||||
const handleKeyUp = (event: KeyboardEvent) => {
|
||||
if (event.key === "Meta" || event.metaKey) lastMetaAt = Date.now()
|
||||
}
|
||||
|
||||
const handlePaste = (event: ClipboardEvent) => {
|
||||
if (event.defaultPrevented) return
|
||||
const path = event.composedPath()
|
||||
const target = path.find((item): item is HTMLElement => item instanceof HTMLElement)
|
||||
const active = activeElement()
|
||||
|
||||
if (
|
||||
path.some((item) => item instanceof HTMLElement && item.closest("[data-prevent-autofocus]") !== null) ||
|
||||
editable(target) ||
|
||||
(active && (active.closest("[data-prevent-autofocus]") || editable(active))) ||
|
||||
dialog.active
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
if (
|
||||
state.blocked() ||
|
||||
input.session.data.isChild() ||
|
||||
!promptRef ||
|
||||
!promptRef.isConnected ||
|
||||
!promptRef.isContentEditable
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
const now = Date.now()
|
||||
lastMetaAt = now
|
||||
lastPasteFocusAt = now
|
||||
promptRef.focus()
|
||||
setCursorPosition(promptRef, prompt.cursor() ?? promptLength(prompt.current()))
|
||||
|
||||
const forwarded = new ClipboardEvent("paste", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
clipboardData: event.clipboardData instanceof DataTransfer ? event.clipboardData : undefined,
|
||||
})
|
||||
|
||||
if (forwarded.clipboardData !== event.clipboardData) {
|
||||
Object.defineProperty(forwarded, "clipboardData", { value: event.clipboardData })
|
||||
}
|
||||
|
||||
promptRef.dispatchEvent(forwarded)
|
||||
|
||||
if (forwarded.defaultPrevented) event.preventDefault()
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
makeEventListener(document, "keydown", handleKeyDown)
|
||||
makeEventListener(document, "keyup", handleKeyUp)
|
||||
makeEventListener(document, "paste", handlePaste)
|
||||
})
|
||||
|
||||
const revert = createSessionRevert({
|
||||
session: input.session,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
import { describe, expect, test } from "bun:test"
|
||||
import { consumeTerminalMetaKey, terminalKeyInput } from "./terminal-key-event"
|
||||
|
||||
function keyEvent(init: KeyboardEventInit) {
|
||||
return new KeyboardEvent("keydown", { cancelable: true, ...init })
|
||||
}
|
||||
|
||||
describe("terminalKeyEvent", () => {
|
||||
test("maps unmodified Command+Backspace to line kill", () => {
|
||||
expect(terminalKeyInput(keyEvent({ key: "Backspace", code: "Backspace", metaKey: true }))).toBe("\x15")
|
||||
expect(terminalKeyInput(keyEvent({ key: "Backspace", code: "Backspace", metaKey: true, shiftKey: true }))).toBeUndefined()
|
||||
expect(terminalKeyInput(keyEvent({ key: "Backspace", code: "Backspace", ctrlKey: true }))).toBeUndefined()
|
||||
})
|
||||
|
||||
test("consumes printable Command chords without blocking paste or copy across layouts", () => {
|
||||
const qwertyPaste = keyEvent({ key: "v", code: "KeyV", metaKey: true })
|
||||
expect(consumeTerminalMetaKey(qwertyPaste)).toBe(false)
|
||||
|
||||
const dvorakPaste = keyEvent({ key: "v", code: "Period", metaKey: true })
|
||||
expect(consumeTerminalMetaKey(dvorakPaste)).toBe(true)
|
||||
dvorakPaste.preventDefault()
|
||||
expect(dvorakPaste.defaultPrevented).toBe(false)
|
||||
|
||||
const dvorakPeriodOnKeyV = keyEvent({ key: ".", code: "KeyV", metaKey: true })
|
||||
expect(consumeTerminalMetaKey(dvorakPeriodOnKeyV)).toBe(true)
|
||||
dvorakPeriodOnKeyV.preventDefault()
|
||||
expect(dvorakPeriodOnKeyV.defaultPrevented).toBe(true)
|
||||
|
||||
const cmdK = keyEvent({ key: "k", code: "KeyK", metaKey: true })
|
||||
expect(consumeTerminalMetaKey(cmdK)).toBe(true)
|
||||
|
||||
let copied = 0
|
||||
const dvorakCopyWithSelection = keyEvent({ key: "c", code: "KeyI", metaKey: true })
|
||||
expect(
|
||||
consumeTerminalMetaKey(dvorakCopyWithSelection, {
|
||||
copy: () => {
|
||||
copied += 1
|
||||
|
||||
return true
|
||||
},
|
||||
}),
|
||||
).toBe(true)
|
||||
expect(copied).toBe(1)
|
||||
|
||||
const dvorakCopyWithoutSelection = keyEvent({ key: "c", code: "KeyI", metaKey: true })
|
||||
expect(consumeTerminalMetaKey(dvorakCopyWithoutSelection, { copy: () => false })).toBe(true)
|
||||
dvorakCopyWithoutSelection.preventDefault()
|
||||
expect(dvorakCopyWithoutSelection.defaultPrevented).toBe(false)
|
||||
|
||||
const cmdLeft = keyEvent({ key: "ArrowLeft", code: "ArrowLeft", metaKey: true })
|
||||
expect(consumeTerminalMetaKey(cmdLeft)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
|
@ -5,3 +5,46 @@ export function terminalKeyInput(event: KeyboardEvent) {
|
|||
|
||||
return "\x15"
|
||||
}
|
||||
|
||||
export function consumeTerminalMetaKey(
|
||||
event: KeyboardEvent,
|
||||
options?: {
|
||||
copy?: () => boolean
|
||||
},
|
||||
) {
|
||||
if (!event.metaKey || event.ctrlKey || event.altKey) return false
|
||||
|
||||
const key = event.key.toLowerCase()
|
||||
|
||||
// ghostty-web only exempts physical KeyV from KeyEncoder.encode; on non-QWERTY
|
||||
// layouts where "v" lives on another physical key, let the browser paste fire
|
||||
// without encoding a literal "v" to the PTY.
|
||||
if (key === "v") {
|
||||
if (event.code === "KeyV") return false
|
||||
|
||||
allowBrowserDefault(event)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
if (!event.shiftKey && key === "c") {
|
||||
if (options?.copy?.()) return true
|
||||
|
||||
if (event.code === "KeyC") return false
|
||||
|
||||
allowBrowserDefault(event)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// ghostty-web passes ASCII `utf8` to KeyEncoder.encode, which ignores Mods.SUPER
|
||||
// in standard VT100 mode and emits the raw character to the PTY.
|
||||
return event.key.length === 1
|
||||
}
|
||||
|
||||
function allowBrowserDefault(event: KeyboardEvent) {
|
||||
Object.defineProperty(event, "preventDefault", {
|
||||
value: () => {},
|
||||
configurable: true,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import type { TerminalModel } from "./model"
|
|||
import type { LocalPTY } from "./state"
|
||||
import { SerializeAddon } from "./serialize"
|
||||
import { disposeIfDisposable, getHoveredLinkText, setOptionIfSupported } from "./runtime-adapters"
|
||||
import { terminalKeyInput } from "./terminal-key-event"
|
||||
import { consumeTerminalMetaKey, terminalKeyInput } from "./terminal-key-event"
|
||||
import { terminalWriter } from "./writer"
|
||||
|
||||
// Serialization on unmount is a synchronous O(rows x cols) walk on the main thread and the
|
||||
|
|
@ -481,7 +481,15 @@ export const Terminal = (props: TerminalProps) => {
|
|||
}
|
||||
|
||||
// allow for toggle terminal keybinds in parent
|
||||
return keybinds.matches("terminal.toggle", event)
|
||||
if (keybinds.matches("terminal.toggle", event)) return true
|
||||
|
||||
return consumeTerminalMetaKey(event, {
|
||||
copy: () => {
|
||||
if (!t.hasSelection()) return false
|
||||
|
||||
return document.execCommand("copy") || t.copySelection()
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
const fit = new mod.FitAddon()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue