fix(terminal): support Ctrl/Cmd+C and V copy/paste (#1103)
This commit is contained in:
1 parent
d6e43a007e
commit
920d219d67
6 files changed
+179
No files matched your search
@@ -44,6 +44,8 @@
|
||||
|
||||
- 🧭 **Diff/内容视图补上「在资源管理器中定位 / 在应用中打开」出口**([#1092](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1092)):从对话 diff 卡片进入的文件视图不再是死胡同;宿主可打开本地路径时,页头在刷新/展开旁提供这两个入口,不可用的部署不显示死按钮。
|
||||
|
||||
- ⌨️ **终端支持 Ctrl/Cmd+C 与 Ctrl/Cmd+V 复制粘贴**([#1103](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1103)):有选区时 Ctrl+C 复制并清空选区,没有选区时仍把 Ctrl+C 交给 shell(SIGINT);Ctrl+V 粘贴,Ctrl+Shift+C/V 同样可用。macOS 的 Cmd+C/V 走同一条路径。
|
||||
|
||||
### v0.30.1
|
||||
|
||||
- 🎯 **设置页「侧边卡片」的导航图标不再叠成两个**:插件此前按"宿主设置导航还没有图标通道"的老假设,用 CSS 把兜底齿轮藏起来、再画自己的图标;宿主早已提供按 section id 寻址的 `settings.section.icon` 插槽,而且图标的 DOM 外面多了一层 span,老选择器失效后兜底 SVG 露了出来,与插件图标并排显示。现在改为**走宿主插槽**提供图标,删掉标记 + CSS 遮罩那一套(顺带去掉了一个监听整个 document 的 MutationObserver)。
|
||||
|
||||
@@ -44,6 +44,8 @@
|
||||
|
||||
- 🧭 **The Diff/content view now offers reveal-in-file-manager and open-in-app exits** ([#1092](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1092)): a file reached from a chat diff card is no longer a dead end. When the host can open workspace paths, the pane header shows both controls next to refresh/expand; desktop-less deployments hide them instead of offering dead buttons.
|
||||
|
||||
- ⌨️ **The terminal now supports Ctrl/Cmd+C and Ctrl/Cmd+V copy/paste** ([#1103](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1103)): Ctrl+C copies the selection (and clears it); with no selection it still reaches the shell as SIGINT. Ctrl+V pastes, Ctrl+Shift+C/V works too, and macOS Cmd+C/V follows the same path.
|
||||
|
||||
### v0.30.1
|
||||
|
||||
- 🎯 **The "Side card" settings row no longer shows two icons**: the plugin still assumed the settings navigation had no icon channel, so it hid the fallback gear with CSS and painted its own glyph. The shell has long since exposed a `settings.section.icon` slot addressed by section id, and it now wraps the nav icon in a span — the stale `> svg:first-child` rule stopped hiding the fallback, so both glyphs rendered side by side. The row now contributes its icon **through the slot**, and the marker + CSS-mask apparatus is gone (along with a document-wide MutationObserver).
|
||||
|
||||
@@ -53,6 +53,7 @@ import {
|
||||
bottomTerminalContentId, nextTerminalMeta, terminalInstanceOf, terminalRunOf, webTerminals,
|
||||
type HostTerminalEnvironment, type HostTerminalView, type HostTerminalViewState,
|
||||
} from './terminal-client.ts'
|
||||
import { terminalClipboardKey } from './terminal-clipboard.ts'
|
||||
import css from './terminal.module.css'
|
||||
|
||||
/** The props the bottom terminal view consumes from its tab descriptor. */
|
||||
@@ -228,6 +229,11 @@ function TerminalScreen({ model, state, visible, scheme, label }: TerminalScreen
|
||||
xterm.loadAddon(addon)
|
||||
xterm.open(node)
|
||||
xterm.textarea?.setAttribute('aria-label', label)
|
||||
// Ctrl/Cmd+C copies only when a selection exists (no selection stays the
|
||||
// shell's SIGINT); Ctrl/Cmd+V pastes. The policy lives in a pure helper
|
||||
// because jsdom cannot construct a real xterm.
|
||||
xterm.attachCustomKeyEventHandler((event) =>
|
||||
terminalClipboardKey(event, xterm, navigator.clipboard))
|
||||
emulator.current = xterm
|
||||
fit.current = addon
|
||||
lastRevision.current = 0
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Clipboard keys on the bottom terminal's xterm screen.
|
||||
*
|
||||
* The terminal convention this plugin follows is Windows Terminal's, not the
|
||||
* browser's: Ctrl/Cmd+C copies ONLY when there is a selection; with no
|
||||
* selection it falls through to xterm so the shell still receives SIGINT.
|
||||
* Ctrl/Cmd+V pastes, and the Ctrl+Shift+C / Ctrl+Shift+V spellings used by
|
||||
* terminal apps work too.
|
||||
*
|
||||
* Kept as a pure function over the two faces it touches so the policy is
|
||||
* testable without a real emulator (xterm needs canvas / ResizeObserver,
|
||||
* which jsdom does not provide).
|
||||
*/
|
||||
|
||||
/** The slice of xterm the handler reads and drives. */
|
||||
export interface TerminalClipboardTarget {
|
||||
hasSelection(): boolean
|
||||
getSelection(): string
|
||||
clearSelection(): void
|
||||
paste(data: string): void
|
||||
}
|
||||
|
||||
/** The slice of `navigator.clipboard` the handler uses. */
|
||||
export interface TerminalClipboardFace {
|
||||
writeText(text: string): Promise<void>
|
||||
readText(): Promise<string>
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle one xterm key event.
|
||||
*
|
||||
* @returns `false` when the key belongs to the clipboard and xterm must NOT
|
||||
* also process it; `true` to let xterm handle the key unchanged.
|
||||
*/
|
||||
export function terminalClipboardKey(
|
||||
event: KeyboardEvent,
|
||||
terminal: TerminalClipboardTarget,
|
||||
clipboard: TerminalClipboardFace | undefined,
|
||||
): boolean {
|
||||
if (event.type !== 'keydown') return true
|
||||
if (!(event.ctrlKey || event.metaKey) || event.altKey) return true
|
||||
const key = event.key.toLowerCase()
|
||||
if (key === 'c') {
|
||||
// No selection means the user wants the shell's interrupt, not a copy.
|
||||
if (!terminal.hasSelection()) return true
|
||||
const text = terminal.getSelection()
|
||||
if (text === '') return true
|
||||
if (clipboard === undefined) return true
|
||||
event.preventDefault()
|
||||
void clipboard.writeText(text).then(() => { terminal.clearSelection() }).catch(() => {})
|
||||
return false
|
||||
}
|
||||
if (key === 'v') {
|
||||
if (clipboard === undefined) return true
|
||||
event.preventDefault()
|
||||
void clipboard.readText().then((text) => {
|
||||
if (text !== '') terminal.paste(text)
|
||||
}).catch(() => {})
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* Clipboard key policy for the bottom terminal (issue #1103).
|
||||
*
|
||||
* Ctrl/Cmd+C copies ONLY when xterm has a selection; with no selection the
|
||||
* event is left to xterm, which sends SIGINT. Ctrl/Cmd+V pastes. The policy is
|
||||
* a pure function because jsdom cannot open a real xterm emulator.
|
||||
*/
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { terminalClipboardKey, type TerminalClipboardFace, type TerminalClipboardTarget } from '../src/client/terminal-clipboard.ts'
|
||||
|
||||
function key(name: string, overrides: Partial<KeyboardEvent> = {}): KeyboardEvent {
|
||||
return {
|
||||
type: 'keydown',
|
||||
key: name,
|
||||
ctrlKey: true,
|
||||
metaKey: false,
|
||||
altKey: false,
|
||||
shiftKey: false,
|
||||
preventDefault: vi.fn(),
|
||||
...overrides,
|
||||
} as unknown as KeyboardEvent
|
||||
}
|
||||
|
||||
function target(selection: string | null): TerminalClipboardTarget & { pasted: string[] } {
|
||||
const pasted: string[] = []
|
||||
return {
|
||||
hasSelection: () => selection !== null,
|
||||
getSelection: () => selection ?? '',
|
||||
clearSelection: vi.fn(),
|
||||
paste: (data: string) => { pasted.push(data) },
|
||||
pasted,
|
||||
}
|
||||
}
|
||||
|
||||
function clipboard(text = 'pasted'): TerminalClipboardFace {
|
||||
return {
|
||||
writeText: vi.fn(async () => {}),
|
||||
readText: vi.fn(async () => text),
|
||||
}
|
||||
}
|
||||
|
||||
describe('terminal clipboard keys', () => {
|
||||
it('leaves an unmodified key to xterm', () => {
|
||||
const term = target('selected')
|
||||
const clip = clipboard()
|
||||
expect(terminalClipboardKey(key('c', { ctrlKey: false }), term, clip)).toBe(true)
|
||||
expect(clip.writeText).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('copies a selection and prevents xterm from sending SIGINT', async () => {
|
||||
const term = target('one\ntwo')
|
||||
const clip = clipboard()
|
||||
const event = key('c')
|
||||
expect(terminalClipboardKey(event, term, clip)).toBe(false)
|
||||
expect(clip.writeText).toHaveBeenCalledWith('one\ntwo')
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
await Promise.resolve()
|
||||
expect(term.clearSelection).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does NOT copy when there is no selection: the shell keeps Ctrl+C', () => {
|
||||
const term = target(null)
|
||||
const clip = clipboard()
|
||||
const event = key('c')
|
||||
expect(terminalClipboardKey(event, term, clip)).toBe(true)
|
||||
expect(clip.writeText).not.toHaveBeenCalled()
|
||||
expect(event.preventDefault).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('pastes the clipboard into xterm', async () => {
|
||||
const term = target(null)
|
||||
const clip = clipboard('echo hi')
|
||||
const event = key('v')
|
||||
expect(terminalClipboardKey(event, term, clip)).toBe(false)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
await Promise.resolve()
|
||||
expect(term.pasted).toEqual(['echo hi'])
|
||||
})
|
||||
|
||||
it('accepts the Ctrl+Shift terminal spelling and Cmd on macOS', () => {
|
||||
const term = target('cmd-c')
|
||||
const clip = clipboard()
|
||||
expect(terminalClipboardKey(key('C', { shiftKey: true }), term, clip)).toBe(false)
|
||||
expect(clip.writeText).toHaveBeenCalledWith('cmd-c')
|
||||
expect(terminalClipboardKey(key('c', { ctrlKey: false, metaKey: true }), target('meta-c'), clip)).toBe(false)
|
||||
})
|
||||
|
||||
it('does not hijack AltGr / Alt-modified keys', () => {
|
||||
const term = target('selected')
|
||||
const clip = clipboard()
|
||||
expect(terminalClipboardKey(key('c', { altKey: true }), term, clip)).toBe(true)
|
||||
expect(clip.writeText).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('falls back to xterm when the clipboard API is unavailable', () => {
|
||||
expect(terminalClipboardKey(key('c'), target('selected'), undefined)).toBe(true)
|
||||
expect(terminalClipboardKey(key('v'), target(null), undefined)).toBe(true)
|
||||
})
|
||||
|
||||
it('does not paste an empty clipboard', async () => {
|
||||
const term = target(null)
|
||||
expect(terminalClipboardKey(key('v'), term, clipboard(''))).toBe(false)
|
||||
await Promise.resolve()
|
||||
expect(term.pasted).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -84,6 +84,7 @@ const xterm = vi.hoisted(() => {
|
||||
reset(): void { this.calls.reset += 1 }
|
||||
resize(cols: number, rows: number): void { this.calls.resize.push([cols, rows]) }
|
||||
focus(): void { /* focus bookkeeping is not asserted */ }
|
||||
attachCustomKeyEventHandler(): void { /* policy covered by terminal-clipboard.spec.ts */ }
|
||||
dispose(): void { this.calls.dispose += 1; this.calls.disposed = true }
|
||||
onData(handler: (data: string) => void): { dispose: () => void } {
|
||||
this.calls.dataHandlers.push(handler)
|
||||
|
||||
Reference in new issue
Block a user