fix(terminal): make Ctrl+V / Cmd+V paste work in the HTTP web client (#9900)
* fix(terminal): make Ctrl+V paste work in the HTTP web client navigator.clipboard only exists in secure contexts, so the web client served over plain HTTP (e.g. a LAN address) read an empty clipboard and Ctrl+V silently did nothing: the keydown handler preventDefault-ed the chord and suppressed the native paste event whose clipboardData is the only clipboard access available there. When the async clipboard reader is unavailable in the web client, let the chord's native paste event fire and feed its clipboardData text through the existing paste pipeline (size limits, bracketed-paste handling and error toasts included). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RTjQWLg4wy1CgmcZKmGAuN * fix: address review finding (bug-bash takeover) Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com> Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
co-authored by
Claude Fable 5
Orca
Neil
parent
1ace87c155
commit
a4f42ad427
@@ -158,6 +158,15 @@ import {
|
||||
} from '@/components/terminal-quick-commands/TerminalQuickCommandDialog'
|
||||
import { keybindingMatchesAction } from '../../../../shared/keybindings'
|
||||
import { pasteTerminalClipboard } from './terminal-clipboard-paste'
|
||||
import {
|
||||
firesNativePasteEvent,
|
||||
getClipboardEventText,
|
||||
isClipboardEventPasteRequired
|
||||
} from './terminal-clipboard-event-paste'
|
||||
import {
|
||||
assertClipboardTextWithinLimitWithYield,
|
||||
type ReadClipboardTextOptions
|
||||
} from '../../../../shared/clipboard-text'
|
||||
import { scheduleImagePasteWebglAtlasRecovery } from './terminal-webgl-atlas-recovery'
|
||||
import { restoreTerminalFitToDesktop, restoreTerminalFitsToDesktop } from './terminal-fit-restore'
|
||||
import { useVisibleTerminalTabClaim } from './use-visible-terminal-tab-claim'
|
||||
@@ -2006,7 +2015,9 @@ export default function TerminalPane({
|
||||
|
||||
const pasteFromClipboard = (
|
||||
pane: ManagedPane,
|
||||
source: Extract<TerminalPasteSource, 'keyboard' | 'paste-event'>
|
||||
source: Extract<TerminalPasteSource, 'keyboard' | 'paste-event'>,
|
||||
readClipboardText: (options?: ReadClipboardTextOptions) => Promise<string> = window.api.ui
|
||||
.readClipboardText
|
||||
): void => {
|
||||
const connectionId = getConnectionId(worktreeId) ?? null
|
||||
const runtimeEnvironmentId = getRuntimeEnvironmentIdForWorktree(
|
||||
@@ -2015,7 +2026,7 @@ export default function TerminalPane({
|
||||
)
|
||||
const activeElementAtDispatch = document.activeElement
|
||||
void pasteTerminalClipboard({
|
||||
readClipboardText: window.api.ui.readClipboardText,
|
||||
readClipboardText,
|
||||
saveClipboardImageAsTempFile: window.api.ui.saveClipboardImageAsTempFile,
|
||||
connectionId,
|
||||
runtimeEnvironmentId,
|
||||
@@ -2069,6 +2080,13 @@ export default function TerminalPane({
|
||||
}
|
||||
return
|
||||
}
|
||||
if (isClipboardEventPasteRequired() && firesNativePasteEvent(e, isMac)) {
|
||||
// Why: without navigator.clipboard the chord's native paste event is the
|
||||
// only clipboard access — let its default fire and handle it in onPaste.
|
||||
// A remapped chord (e.g. Ctrl+Y) fires no paste event, so keep consuming it
|
||||
// below instead of letting xterm encode it to the PTY as a raw control char.
|
||||
return
|
||||
}
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
const manager = managerRef.current
|
||||
@@ -2119,6 +2137,13 @@ export default function TerminalPane({
|
||||
if (!pane) {
|
||||
return
|
||||
}
|
||||
if (isClipboardEventPasteRequired()) {
|
||||
const eventText = getClipboardEventText(e)
|
||||
pasteFromClipboard(pane, 'paste-event', (options) =>
|
||||
assertClipboardTextWithinLimitWithYield(eventText, options)
|
||||
)
|
||||
return
|
||||
}
|
||||
pasteFromClipboard(pane, 'paste-event')
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
firesNativePasteEvent,
|
||||
getClipboardEventText,
|
||||
shouldUseClipboardEventPaste
|
||||
} from './terminal-clipboard-event-paste'
|
||||
|
||||
function makeKeyEvent(
|
||||
overrides: Partial<Pick<KeyboardEvent, 'key' | 'metaKey' | 'ctrlKey' | 'altKey' | 'shiftKey'>>
|
||||
): Pick<KeyboardEvent, 'key' | 'metaKey' | 'ctrlKey' | 'altKey' | 'shiftKey'> {
|
||||
return { key: '', metaKey: false, ctrlKey: false, altKey: false, shiftKey: false, ...overrides }
|
||||
}
|
||||
|
||||
function makeClipboardEvent(text: string | null): ClipboardEvent {
|
||||
return {
|
||||
clipboardData:
|
||||
text === null
|
||||
? null
|
||||
: {
|
||||
getData: (type: string) => (type === 'text/plain' ? text : '')
|
||||
}
|
||||
} as unknown as ClipboardEvent
|
||||
}
|
||||
|
||||
describe('shouldUseClipboardEventPaste', () => {
|
||||
it('requires the fallback for web clients without navigator.clipboard.readText', () => {
|
||||
expect(
|
||||
shouldUseClipboardEventPaste({ isWebClient: true, clipboardReadTextAvailable: false })
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it('keeps async clipboard reads for secure-context web clients', () => {
|
||||
expect(
|
||||
shouldUseClipboardEventPaste({ isWebClient: true, clipboardReadTextAvailable: true })
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it('never applies to the Electron renderer, which reads the clipboard over IPC', () => {
|
||||
expect(
|
||||
shouldUseClipboardEventPaste({ isWebClient: false, clipboardReadTextAvailable: false })
|
||||
).toBe(false)
|
||||
expect(
|
||||
shouldUseClipboardEventPaste({ isWebClient: false, clipboardReadTextAvailable: true })
|
||||
).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getClipboardEventText', () => {
|
||||
it('reads text/plain from the event clipboardData', () => {
|
||||
expect(getClipboardEventText(makeClipboardEvent('echo hi'))).toBe('echo hi')
|
||||
})
|
||||
|
||||
it('returns empty text when clipboardData is missing', () => {
|
||||
expect(getClipboardEventText(makeClipboardEvent(null))).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('firesNativePasteEvent', () => {
|
||||
it('recognizes the default native paste chords on macOS', () => {
|
||||
expect(firesNativePasteEvent(makeKeyEvent({ key: 'v', metaKey: true }), true)).toBe(true)
|
||||
})
|
||||
|
||||
it('recognizes the default native paste chords on Windows/Linux', () => {
|
||||
expect(firesNativePasteEvent(makeKeyEvent({ key: 'v', ctrlKey: true }), false)).toBe(true)
|
||||
// Ctrl+Shift+V is the default terminal paste and still dispatches a native event.
|
||||
expect(
|
||||
firesNativePasteEvent(makeKeyEvent({ key: 'v', ctrlKey: true, shiftKey: true }), false)
|
||||
).toBe(true)
|
||||
expect(
|
||||
firesNativePasteEvent(makeKeyEvent({ key: 'Insert', shiftKey: true }), false)
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it('does not treat a remapped non-clipboard chord as a native paste event', () => {
|
||||
// A custom terminal.paste binding like Ctrl+Y fires no native paste event and
|
||||
// must stay consumed so it is not encoded to the PTY as a control char.
|
||||
expect(firesNativePasteEvent(makeKeyEvent({ key: 'y', ctrlKey: true }), false)).toBe(false)
|
||||
expect(firesNativePasteEvent(makeKeyEvent({ key: 'd', ctrlKey: true }), false)).toBe(false)
|
||||
// Cmd+V does not fire a native paste event on Windows/Linux.
|
||||
expect(firesNativePasteEvent(makeKeyEvent({ key: 'v', metaKey: true }), false)).toBe(false)
|
||||
// Ctrl+V does not fire a native paste event on macOS (readline quote-insert).
|
||||
expect(firesNativePasteEvent(makeKeyEvent({ key: 'v', ctrlKey: true }), true)).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,46 @@
|
||||
import { isWebClientLocation } from '@/lib/web-client-location'
|
||||
|
||||
// Why: navigator.clipboard only exists in secure contexts. The web client served
|
||||
// over plain HTTP (e.g. a LAN address) can reach the clipboard only through the
|
||||
// chord's native ClipboardEvent, so Ctrl/Cmd+V must not be preventDefault-ed there.
|
||||
export function shouldUseClipboardEventPaste(args: {
|
||||
isWebClient: boolean
|
||||
clipboardReadTextAvailable: boolean
|
||||
}): boolean {
|
||||
return args.isWebClient && !args.clipboardReadTextAvailable
|
||||
}
|
||||
|
||||
export function isClipboardEventPasteRequired(): boolean {
|
||||
return shouldUseClipboardEventPaste({
|
||||
isWebClient: isWebClientLocation(),
|
||||
clipboardReadTextAvailable: typeof navigator.clipboard?.readText === 'function'
|
||||
})
|
||||
}
|
||||
|
||||
export function getClipboardEventText(event: ClipboardEvent): string {
|
||||
return event.clipboardData?.getData('text/plain') ?? ''
|
||||
}
|
||||
|
||||
type PasteChordEvent = Pick<KeyboardEvent, 'key' | 'metaKey' | 'ctrlKey' | 'altKey' | 'shiftKey'>
|
||||
|
||||
// Why: only these chords make the browser dispatch a native `paste` ClipboardEvent,
|
||||
// the sole clipboard path on insecure web. A remapped terminal.paste chord (e.g. Ctrl+Y)
|
||||
// fires no paste event, so it must still be consumed rather than encoded to the PTY as
|
||||
// raw control chars.
|
||||
export function firesNativePasteEvent(event: PasteChordEvent, isMac: boolean): boolean {
|
||||
const key = event.key.toLowerCase()
|
||||
if (isMac) {
|
||||
return key === 'v' && event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey
|
||||
}
|
||||
// Ctrl+V and Ctrl+Shift+V both dispatch a native paste event on Windows/Linux.
|
||||
if (key === 'v' && event.ctrlKey && !event.metaKey && !event.altKey) {
|
||||
return true
|
||||
}
|
||||
return (
|
||||
event.key === 'Insert' &&
|
||||
event.shiftKey &&
|
||||
!event.ctrlKey &&
|
||||
!event.metaKey &&
|
||||
!event.altKey
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user