dsh-remote-vs 0.21.0 → 0.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/lib/client.js +202 -20
- package/lib/types/client/XTerm.d.ts +2 -0
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/vsc/terminal-clipboard.d.ts +94 -0
- package/package.json +1 -1
- package/src/client/TerminalView.tsx +1 -0
- package/src/client/XTerm.tsx +112 -21
- package/src/client/i18n.ts +13 -2
- package/src/client/vsc/terminal-clipboard.ts +170 -0
package/README.en.md
CHANGED
|
@@ -220,7 +220,7 @@ dsh_remote_vs/
|
|
|
220
220
|
├── vendor/dsh-remote-helpers/ # helper seeds (shipped, seeded into cache at startup, see below)
|
|
221
221
|
├── docs/ # local-only working docs (only docs/media/ screenshots ship)
|
|
222
222
|
│ ├── MAINTENANCE.md # local-only (.gitignore), not shipped: upgrade impact, checklist, pitfalls, release
|
|
223
|
-
│ ├── VERSION-HISTORY.md # local-only: per-release change log (0.7.1 → 0.21.
|
|
223
|
+
│ ├── VERSION-HISTORY.md # local-only: per-release change log (0.7.1 → 0.21.1)
|
|
224
224
|
│ └── GATES.md # local-only: local gate inventory and measured assertion counts
|
|
225
225
|
├── locale/ # bilingual plugin metadata
|
|
226
226
|
└── lib/ # build output (**must be committed**; only .map is ignored; restart after link install)
|
package/README.md
CHANGED
|
@@ -205,7 +205,7 @@ dsh_remote_vs/
|
|
|
205
205
|
├── vendor/dsh-remote-helpers/ # helper 种子(随包发布,启动铺缓存,见下)
|
|
206
206
|
├── docs/ # 本地工作文档(不推送 GitHub;只有 docs/media/ 的截图随仓库公开)
|
|
207
207
|
│ ├── MAINTENANCE.md # 本地私有(.gitignore,不入库):升级影响面/检查清单/踩坑/发布流程
|
|
208
|
-
│ ├── VERSION-HISTORY.md # 本地私有:逐版本变更纪要(0.7.1 → 0.21.
|
|
208
|
+
│ ├── VERSION-HISTORY.md # 本地私有:逐版本变更纪要(0.7.1 → 0.21.1)
|
|
209
209
|
│ └── GATES.md # 本地私有:本地门禁清单与实跑条数
|
|
210
210
|
├── locale/ # 插件元信息双语
|
|
211
211
|
└── lib/ # 构建产物(**必须入库**,只有 .map 忽略;link 安装后重启即生效)
|
package/lib/client.js
CHANGED
|
@@ -489,6 +489,9 @@ window.__ModuleLoader__.load({
|
|
|
489
489
|
"term.empty": "还没有终端 —— 选好主机后点右上角 + 新建一个。",
|
|
490
490
|
"term.close": "关闭终端",
|
|
491
491
|
"term.closeTitle": "关闭这个终端",
|
|
492
|
+
"term.copy": "复制",
|
|
493
|
+
"term.paste": "粘贴",
|
|
494
|
+
"term.selectAll": "全选",
|
|
492
495
|
"term.noHosts": "还没有主机,请先去「设置 → 远程主机」配置。",
|
|
493
496
|
"tab.git": "Git",
|
|
494
497
|
"git.hint": "请选择远端路径为 git 仓库的工作区。",
|
|
@@ -1091,6 +1094,9 @@ window.__ModuleLoader__.load({
|
|
|
1091
1094
|
"term.empty": "No terminal yet — pick a host, then press + to open one.",
|
|
1092
1095
|
"term.close": "Close terminal",
|
|
1093
1096
|
"term.closeTitle": "Close this terminal",
|
|
1097
|
+
"term.copy": "Copy",
|
|
1098
|
+
"term.paste": "Paste",
|
|
1099
|
+
"term.selectAll": "Select all",
|
|
1094
1100
|
"term.noHosts": "No hosts yet. Configure one in Settings → Remote Hosts first.",
|
|
1095
1101
|
"tab.git": "Git",
|
|
1096
1102
|
"git.hint": "Pick a workspace whose remote path is a git repository.",
|
|
@@ -41584,7 +41590,7 @@ Integer Float Double Long Int32 Int64 Uint8 uint32_t int32_t uint64_t int64_t si
|
|
|
41584
41590
|
const primary = host.querySelectorAll("[class*=\"rvs-ck-\"]").length;
|
|
41585
41591
|
const fallback = host.querySelectorAll("span[class^=\"rvs-t-\"]").length;
|
|
41586
41592
|
const count = primary > 0 ? primary : fallback;
|
|
41587
|
-
console.info(`[dsh-remote-vs] build 1112-
|
|
41593
|
+
console.info(`[dsh-remote-vs] build 1112-1650 · ${source} · colored tokens: ${count} (${propsRef.current.path})`);
|
|
41588
41594
|
propsRef.current.onTokens?.(count);
|
|
41589
41595
|
};
|
|
41590
41596
|
const armFallback = () => {
|
|
@@ -48808,8 +48814,8 @@ Integer Float Double Long Int32 Int64 Uint8 uint32_t int32_t uint64_t int64_t si
|
|
|
48808
48814
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(ConnectionStatusChip, { compact: true }),
|
|
48809
48815
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
48810
48816
|
className: "rvs-build",
|
|
48811
|
-
title: `dsh-remote-vs build 1112-
|
|
48812
|
-
children: "1112-
|
|
48817
|
+
title: `dsh-remote-vs build 1112-1650`,
|
|
48818
|
+
children: "1112-1650"
|
|
48813
48819
|
}),
|
|
48814
48820
|
tokenCount !== void 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
|
|
48815
48821
|
className: "rvs-build",
|
|
@@ -72989,9 +72995,96 @@ Integer Float Double Long Int32 Int64 Uint8 uint32_t int32_t uint64_t int64_t si
|
|
|
72989
72995
|
})()));
|
|
72990
72996
|
}));
|
|
72991
72997
|
//#endregion
|
|
72992
|
-
//#region src/client/
|
|
72998
|
+
//#region src/client/vsc/terminal-clipboard.ts
|
|
72993
72999
|
var import_xterm = require_xterm();
|
|
72994
73000
|
var import_addon_fit = require_addon_fit();
|
|
73001
|
+
/** True when the key is the letter (layout-aware `code` is not required: xterm gives `key`). */
|
|
73002
|
+
function isLetter(input, letter) {
|
|
73003
|
+
return input.key.toLowerCase() === letter;
|
|
73004
|
+
}
|
|
73005
|
+
/**
|
|
73006
|
+
* Map one keydown to a clipboard chord.
|
|
73007
|
+
*
|
|
73008
|
+
* Conventions: Ctrl+Shift+C / Ctrl+Insert / Cmd+C copy, Ctrl+Shift+V / Cmd+V /
|
|
73009
|
+
* Shift+Insert paste. Plain Ctrl+V is left to the browser first (`paste-native`),
|
|
73010
|
+
* and plain Ctrl+C always stays SIGINT.
|
|
73011
|
+
*/
|
|
73012
|
+
function clipboardChordFor(input) {
|
|
73013
|
+
const ctrl = input.ctrl, shift = input.shift, meta = input.meta;
|
|
73014
|
+
if (input.alt) return void 0;
|
|
73015
|
+
if (!meta && ctrl && shift && isLetter(input, "c")) return "copy";
|
|
73016
|
+
if (!meta && ctrl && !shift && input.key === "Insert") return "copy";
|
|
73017
|
+
if (meta && !ctrl && !shift && isLetter(input, "c")) return "copy";
|
|
73018
|
+
if (!meta && ctrl && shift && isLetter(input, "v")) return "paste";
|
|
73019
|
+
if (!meta && !ctrl && shift && input.key === "Insert") return "paste";
|
|
73020
|
+
if (meta && !ctrl && !shift && isLetter(input, "v")) return "paste";
|
|
73021
|
+
if (!meta && ctrl && !shift && isLetter(input, "v")) return "paste-native";
|
|
73022
|
+
if (!meta && ctrl && !shift && isLetter(input, "c")) return "copy-interrupt";
|
|
73023
|
+
}
|
|
73024
|
+
/**
|
|
73025
|
+
* Watches one Ctrl+V press: the browser's native paste wins when its event arrives,
|
|
73026
|
+
* otherwise the Clipboard API inserts the text after {@link PASTE_FALLBACK_MS}.
|
|
73027
|
+
*
|
|
73028
|
+
* The watcher is a single slot: arming again (a held/repeated key) replaces the
|
|
73029
|
+
* pending window instead of stacking pastes.
|
|
73030
|
+
*/
|
|
73031
|
+
function createPasteFallback(deps) {
|
|
73032
|
+
const schedule = deps.schedule ?? ((run, ms) => {
|
|
73033
|
+
const timer = setTimeout(run, ms);
|
|
73034
|
+
timer.unref?.();
|
|
73035
|
+
return timer;
|
|
73036
|
+
});
|
|
73037
|
+
let pending;
|
|
73038
|
+
return {
|
|
73039
|
+
arm: () => {
|
|
73040
|
+
if (pending !== void 0) pending.cancelled = true;
|
|
73041
|
+
const token = { cancelled: false };
|
|
73042
|
+
pending = token;
|
|
73043
|
+
schedule(() => {
|
|
73044
|
+
if (token.cancelled) return;
|
|
73045
|
+
token.cancelled = true;
|
|
73046
|
+
if (pending === token) pending = void 0;
|
|
73047
|
+
deps.read().then((text) => {
|
|
73048
|
+
if (text === void 0) {
|
|
73049
|
+
deps.onDenied();
|
|
73050
|
+
return;
|
|
73051
|
+
}
|
|
73052
|
+
deps.paste(text);
|
|
73053
|
+
});
|
|
73054
|
+
}, 180);
|
|
73055
|
+
},
|
|
73056
|
+
observed: () => {
|
|
73057
|
+
if (pending !== void 0) pending.cancelled = true;
|
|
73058
|
+
pending = void 0;
|
|
73059
|
+
}
|
|
73060
|
+
};
|
|
73061
|
+
}
|
|
73062
|
+
/** Menu entry state for the right-click menu. */
|
|
73063
|
+
function terminalMenuState(input) {
|
|
73064
|
+
return { copyDisabled: !input.hasSelection };
|
|
73065
|
+
}
|
|
73066
|
+
/** Start/end markers a terminal wraps around a bracketed paste. */
|
|
73067
|
+
const PASTE_START = "\x1B[200~";
|
|
73068
|
+
const PASTE_END = "\x1B[201~";
|
|
73069
|
+
/**
|
|
73070
|
+
* Remove ONE bracketed-paste wrapper from an input chunk.
|
|
73071
|
+
*
|
|
73072
|
+
* xterm wraps `Terminal.paste()`/native pastes while the remote application has
|
|
73073
|
+
* bracketed paste enabled. When that mode is stale (an app that enabled it was
|
|
73074
|
+
* killed, or the shell never enabled it) the wrapper is displayed literally as
|
|
73075
|
+
* `^[[200~...^[[201~` and the text is not executed — a field screenshot showed
|
|
73076
|
+
* exactly that. The host-side input path strips one wrapper before forwarding,
|
|
73077
|
+
* so the PTY only ever receives the user's text. Apps that expect the wrapper
|
|
73078
|
+
* still accept raw text, like a classic terminal.
|
|
73079
|
+
*/
|
|
73080
|
+
function stripBracketedPasteMarkers(data) {
|
|
73081
|
+
if (data.length < 6) return data;
|
|
73082
|
+
if (!data.startsWith(PASTE_START)) return data;
|
|
73083
|
+
if (!data.endsWith(PASTE_END)) return data;
|
|
73084
|
+
return data.slice(6, data.length - 6);
|
|
73085
|
+
}
|
|
73086
|
+
//#endregion
|
|
73087
|
+
//#region src/client/XTerm.tsx
|
|
72995
73088
|
function send(ws, frame) {
|
|
72996
73089
|
if (ws.readyState === WebSocket.OPEN) try {
|
|
72997
73090
|
ws.send(JSON.stringify(frame));
|
|
@@ -73075,6 +73168,11 @@ Integer Float Double Long Int32 Int64 Uint8 uint32_t int32_t uint64_t int64_t si
|
|
|
73075
73168
|
const propsRef = (0, react.useRef)(props);
|
|
73076
73169
|
propsRef.current = props;
|
|
73077
73170
|
const liveRef = (0, react.useRef)(void 0);
|
|
73171
|
+
const menu = useContextMenu();
|
|
73172
|
+
const openMenuRef = (0, react.useRef)(menu.open);
|
|
73173
|
+
openMenuRef.current = menu.open;
|
|
73174
|
+
const tRef = (0, react.useRef)(props.t);
|
|
73175
|
+
tRef.current = props.t;
|
|
73078
73176
|
(0, react.useEffect)(() => {
|
|
73079
73177
|
if (props.visible === false) return;
|
|
73080
73178
|
liveRef.current?.refit();
|
|
@@ -73134,7 +73232,9 @@ Integer Float Double Long Int32 Int64 Uint8 uint32_t int32_t uint64_t int64_t si
|
|
|
73134
73232
|
* characters — far under that cap either way, and far above a keystroke.
|
|
73135
73233
|
*/
|
|
73136
73234
|
const INPUT_CHUNK_CHARS = 64 * 1024;
|
|
73137
|
-
const sendInput = (
|
|
73235
|
+
const sendInput = (rawData) => {
|
|
73236
|
+
const data = stripBracketedPasteMarkers(rawData);
|
|
73237
|
+
if (data === "") return;
|
|
73138
73238
|
if (data.length <= INPUT_CHUNK_CHARS) {
|
|
73139
73239
|
send(ws, {
|
|
73140
73240
|
t: "in",
|
|
@@ -73221,27 +73321,102 @@ Integer Float Double Long Int32 Int64 Uint8 uint32_t int32_t uint64_t int64_t si
|
|
|
73221
73321
|
term.paste(text);
|
|
73222
73322
|
});
|
|
73223
73323
|
};
|
|
73324
|
+
const pasteFallback = createPasteFallback({
|
|
73325
|
+
read: readClipboard,
|
|
73326
|
+
paste: (text) => {
|
|
73327
|
+
term.paste(text);
|
|
73328
|
+
},
|
|
73329
|
+
onDenied: () => {
|
|
73330
|
+
term.write("\r\n[dsh-remote] clipboard read denied by the browser\r\n");
|
|
73331
|
+
}
|
|
73332
|
+
});
|
|
73333
|
+
/**
|
|
73334
|
+
* All paste input converges here: one insertion per paste event.
|
|
73335
|
+
*
|
|
73336
|
+
* Capture phase + stopPropagation keeps xterm's own textarea handler from
|
|
73337
|
+
* inserting a second copy, and preventDefault stops the browser's remaining
|
|
73338
|
+
* default. The watcher is cancelled so the Clipboard-API fallback (for hosts
|
|
73339
|
+
* that never deliver this event) does not fire on top of it.
|
|
73340
|
+
*/
|
|
73341
|
+
/**
|
|
73342
|
+
* Explicit paste chords (Ctrl+Shift+V / Cmd+V / Shift+Insert) insert through
|
|
73343
|
+
* the Clipboard API themselves. Some hosts still deliver the browser's native
|
|
73344
|
+
* paste afterwards even when the keydown was preventDefault-ed (observed with
|
|
73345
|
+
* Ctrl+Shift+V paste-as-plain-text), which pasted twice. This window swallows
|
|
73346
|
+
* that native delivery; plain Ctrl+V keeps the native path below.
|
|
73347
|
+
*/
|
|
73348
|
+
let suppressNativePasteUntil = 0;
|
|
73349
|
+
const onPasteEvent = (e) => {
|
|
73350
|
+
e.preventDefault();
|
|
73351
|
+
e.stopPropagation();
|
|
73352
|
+
if (Date.now() < suppressNativePasteUntil) {
|
|
73353
|
+
suppressNativePasteUntil = 0;
|
|
73354
|
+
return;
|
|
73355
|
+
}
|
|
73356
|
+
const text = e.clipboardData?.getData("text/plain") ?? "";
|
|
73357
|
+
pasteFallback.observed();
|
|
73358
|
+
if (text !== "") term.paste(text);
|
|
73359
|
+
};
|
|
73360
|
+
host.addEventListener("paste", onPasteEvent, true);
|
|
73224
73361
|
term.attachCustomKeyEventHandler((e) => {
|
|
73225
|
-
const
|
|
73226
|
-
|
|
73227
|
-
|
|
73228
|
-
|
|
73229
|
-
|
|
73230
|
-
|
|
73231
|
-
|
|
73232
|
-
|
|
73233
|
-
|
|
73362
|
+
const chord = clipboardChordFor({
|
|
73363
|
+
key: e.key,
|
|
73364
|
+
ctrl: e.ctrlKey,
|
|
73365
|
+
shift: e.shiftKey,
|
|
73366
|
+
alt: e.altKey,
|
|
73367
|
+
meta: e.metaKey
|
|
73368
|
+
});
|
|
73369
|
+
if (chord === void 0) return true;
|
|
73370
|
+
if (chord === "copy-interrupt") {
|
|
73371
|
+
if (!term.hasSelection()) return true;
|
|
73372
|
+
e.preventDefault();
|
|
73373
|
+
doCopy(false);
|
|
73374
|
+
return false;
|
|
73234
73375
|
}
|
|
73235
|
-
if (
|
|
73376
|
+
if (chord === "paste") {
|
|
73377
|
+
e.preventDefault();
|
|
73378
|
+
suppressNativePasteUntil = Date.now() + 600;
|
|
73236
73379
|
doPaste();
|
|
73237
73380
|
return false;
|
|
73238
73381
|
}
|
|
73239
|
-
|
|
73382
|
+
if (chord === "paste-native") {
|
|
73383
|
+
pasteFallback.arm();
|
|
73384
|
+
return true;
|
|
73385
|
+
}
|
|
73386
|
+
if (!term.hasSelection()) return true;
|
|
73387
|
+
e.preventDefault();
|
|
73388
|
+
doCopy(false);
|
|
73389
|
+
return false;
|
|
73240
73390
|
});
|
|
73241
73391
|
const onContextMenu = (e) => {
|
|
73242
73392
|
e.preventDefault();
|
|
73243
|
-
|
|
73244
|
-
|
|
73393
|
+
const hasSelection = term.hasSelection();
|
|
73394
|
+
const entries = [
|
|
73395
|
+
{
|
|
73396
|
+
label: tRef.current("term.copy"),
|
|
73397
|
+
disabled: terminalMenuState({ hasSelection }).copyDisabled,
|
|
73398
|
+
onSelect: () => {
|
|
73399
|
+
doCopy(false);
|
|
73400
|
+
term.focus();
|
|
73401
|
+
}
|
|
73402
|
+
},
|
|
73403
|
+
{
|
|
73404
|
+
label: tRef.current("term.paste"),
|
|
73405
|
+
onSelect: () => {
|
|
73406
|
+
doPaste();
|
|
73407
|
+
term.focus();
|
|
73408
|
+
}
|
|
73409
|
+
},
|
|
73410
|
+
{
|
|
73411
|
+
label: tRef.current("term.selectAll"),
|
|
73412
|
+
separated: true,
|
|
73413
|
+
onSelect: () => {
|
|
73414
|
+
term.selectAll();
|
|
73415
|
+
term.focus();
|
|
73416
|
+
}
|
|
73417
|
+
}
|
|
73418
|
+
];
|
|
73419
|
+
openMenuRef.current(e.clientX, e.clientY, entries);
|
|
73245
73420
|
};
|
|
73246
73421
|
host.addEventListener("contextmenu", onContextMenu);
|
|
73247
73422
|
term.focus();
|
|
@@ -73250,6 +73425,7 @@ Integer Float Double Long Int32 Int64 Uint8 uint32_t int32_t uint64_t int64_t si
|
|
|
73250
73425
|
unflip();
|
|
73251
73426
|
window.clearTimeout(timer);
|
|
73252
73427
|
window.removeEventListener("resize", onWindowResize);
|
|
73428
|
+
host.removeEventListener("paste", onPasteEvent, true);
|
|
73253
73429
|
host.removeEventListener("contextmenu", onContextMenu);
|
|
73254
73430
|
resizeObserver.disconnect();
|
|
73255
73431
|
try {
|
|
@@ -73258,13 +73434,18 @@ Integer Float Double Long Int32 Int64 Uint8 uint32_t int32_t uint64_t int64_t si
|
|
|
73258
73434
|
term.dispose();
|
|
73259
73435
|
};
|
|
73260
73436
|
}, []);
|
|
73261
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
73437
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
73262
73438
|
ref: hostRef,
|
|
73263
73439
|
style: {
|
|
73264
73440
|
height: "100%",
|
|
73265
73441
|
minHeight: 200
|
|
73266
73442
|
}
|
|
73267
|
-
})
|
|
73443
|
+
}), menu.menu !== void 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ContextMenu, {
|
|
73444
|
+
x: menu.menu.x,
|
|
73445
|
+
y: menu.menu.y,
|
|
73446
|
+
entries: menu.menu.entries,
|
|
73447
|
+
onClose: menu.close
|
|
73448
|
+
})] });
|
|
73268
73449
|
}
|
|
73269
73450
|
//#endregion
|
|
73270
73451
|
//#region src/client/TerminalView.tsx
|
|
@@ -73646,6 +73827,7 @@ Integer Float Double Long Int32 Int64 Uint8 uint32_t int32_t uint64_t int64_t si
|
|
|
73646
73827
|
rows: 24
|
|
73647
73828
|
}),
|
|
73648
73829
|
visible: props.visible && tab.id === active?.id,
|
|
73830
|
+
t,
|
|
73649
73831
|
...tab.initialCommand === void 0 ? {} : { initialInput: tab.initialCommand },
|
|
73650
73832
|
onReady: (id) => patchTab(tab.id, {
|
|
73651
73833
|
session: id,
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* them here keeps the check meaningful in both directions: every entry must
|
|
12
12
|
* exist in both dictionaries, and nothing else may be unreferenced.
|
|
13
13
|
*/
|
|
14
|
-
export declare const DYNAMIC_KEYS: readonly ["common.loading", "editor.saving", "editor.unsaved", "editor.saved", "scm.hunkStaged", "scm.hunkUnstaged", "scm.hunkReverted", "history.details", "history.content", "history.diff", "downloads.running", "downloads.indeterminate", "downloads.sizeUnknown", "downloads.handedOff", "term.disconnected", "term.closed", "connection.ok", "connection.error", "connection.idle", "connection.actionFailed", "doctor.state.ok", "doctor.state.missing", "doctor.state.unknown", "doctor.item.os", "doctor.item.libc", "doctor.item.git", "doctor.item.rg", "doctor.item.tar", "doctor.item.setsid", "doctor.item.disk", "doctor.item.nproc", "doctor.item.memory", "doctor.item.helpers", "doctor.item.sshd"];
|
|
14
|
+
export declare const DYNAMIC_KEYS: readonly ["common.loading", "editor.saving", "editor.unsaved", "editor.saved", "scm.hunkStaged", "scm.hunkUnstaged", "scm.hunkReverted", "history.details", "history.content", "history.diff", "downloads.running", "downloads.indeterminate", "downloads.sizeUnknown", "downloads.handedOff", "term.disconnected", "term.closed", "term.copy", "term.paste", "term.selectAll", "connection.ok", "connection.error", "connection.idle", "connection.actionFailed", "doctor.state.ok", "doctor.state.missing", "doctor.state.unknown", "doctor.item.os", "doctor.item.libc", "doctor.item.git", "doctor.item.rg", "doctor.item.tar", "doctor.item.setsid", "doctor.item.disk", "doctor.item.nproc", "doctor.item.memory", "doctor.item.helpers", "doctor.item.sshd"];
|
|
15
15
|
export type Lang = 'zh' | 'en';
|
|
16
16
|
export declare function detectLang(): Lang;
|
|
17
17
|
export declare function makeT(lang: Lang): (key: string) => string;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal clipboard chords and the paste fallback, as testable logic.
|
|
3
|
+
*
|
|
4
|
+
* Why this module exists
|
|
5
|
+
* ----------------------
|
|
6
|
+
* The terminal's copy/paste handling used to be an inline `attachCustomKeyEventHandler`
|
|
7
|
+
* with three problems the UI tests could not see:
|
|
8
|
+
*
|
|
9
|
+
* 1. **Double paste on Ctrl+Shift+V.** The handler pasted through
|
|
10
|
+
* `navigator.clipboard.readText()` and returned `false`, but returning false only
|
|
11
|
+
* stops xterm's own handling — the browser still performed its default paste
|
|
12
|
+
* (Chrome maps Ctrl+Shift+V to "paste as plain text"), so both paths inserted the
|
|
13
|
+
* text. Handled chords must call `preventDefault()` themselves.
|
|
14
|
+
* 2. **Ctrl+V depended entirely on the browser's native paste.** When the host page
|
|
15
|
+
* or a wrapper swallows the paste event (desktop shells, non-standard clipboard
|
|
16
|
+
* permissions), the terminal silently received nothing. It now falls back to the
|
|
17
|
+
* Clipboard API only when no paste event arrived shortly after the key press.
|
|
18
|
+
* 3. **Right-click copied/pasted immediately with no visible affordance.** It is now
|
|
19
|
+
* a small menu (copy / paste / select all) so the action is discoverable and the
|
|
20
|
+
* copy item can be disabled when nothing is selected.
|
|
21
|
+
*
|
|
22
|
+
* The chord table and the fallback watcher are pure so a gate can pin all of it.
|
|
23
|
+
*/
|
|
24
|
+
/** What one key press asks the terminal to do. */
|
|
25
|
+
export type TerminalClipboardChord =
|
|
26
|
+
/** Copy the current selection, then clear it (menu action). */
|
|
27
|
+
'copy'
|
|
28
|
+
/** Paste immediately through the Clipboard API. */
|
|
29
|
+
| 'paste'
|
|
30
|
+
/** Let the browser's native paste run; fall back to the Clipboard API when no paste event arrives. */
|
|
31
|
+
| 'paste-native'
|
|
32
|
+
/** Plain Ctrl+C: copy when a selection exists, otherwise stay SIGINT. */
|
|
33
|
+
| 'copy-interrupt';
|
|
34
|
+
/** The keyboard state the chord table reads. */
|
|
35
|
+
export interface TerminalChordInput {
|
|
36
|
+
key: string;
|
|
37
|
+
ctrl: boolean;
|
|
38
|
+
shift: boolean;
|
|
39
|
+
alt: boolean;
|
|
40
|
+
meta: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Map one keydown to a clipboard chord.
|
|
44
|
+
*
|
|
45
|
+
* Conventions: Ctrl+Shift+C / Ctrl+Insert / Cmd+C copy, Ctrl+Shift+V / Cmd+V /
|
|
46
|
+
* Shift+Insert paste. Plain Ctrl+V is left to the browser first (`paste-native`),
|
|
47
|
+
* and plain Ctrl+C always stays SIGINT.
|
|
48
|
+
*/
|
|
49
|
+
export declare function clipboardChordFor(input: TerminalChordInput): TerminalClipboardChord | undefined;
|
|
50
|
+
/** Copy and paste chords take the key over; the native-paste chord must not. */
|
|
51
|
+
export declare function chordTakesKey(chord: TerminalClipboardChord): boolean;
|
|
52
|
+
/** Dependencies of {@link createPasteFallback}. */
|
|
53
|
+
export interface PasteFallbackDeps {
|
|
54
|
+
/** Clipboard read; `undefined` when the browser denies it. */
|
|
55
|
+
read: () => Promise<string | undefined>;
|
|
56
|
+
/** Insert text into the terminal. */
|
|
57
|
+
paste: (text: string) => void;
|
|
58
|
+
/** Report a denied clipboard read (shown in the terminal). */
|
|
59
|
+
onDenied: () => void;
|
|
60
|
+
/** Timer injection (tests use a fake clock). */
|
|
61
|
+
schedule?: (run: () => void, ms: number) => unknown;
|
|
62
|
+
}
|
|
63
|
+
/** How long to wait for a native paste event before falling back to the API. */
|
|
64
|
+
export declare const PASTE_FALLBACK_MS = 180;
|
|
65
|
+
/**
|
|
66
|
+
* Watches one Ctrl+V press: the browser's native paste wins when its event arrives,
|
|
67
|
+
* otherwise the Clipboard API inserts the text after {@link PASTE_FALLBACK_MS}.
|
|
68
|
+
*
|
|
69
|
+
* The watcher is a single slot: arming again (a held/repeated key) replaces the
|
|
70
|
+
* pending window instead of stacking pastes.
|
|
71
|
+
*/
|
|
72
|
+
export declare function createPasteFallback(deps: PasteFallbackDeps): {
|
|
73
|
+
arm: () => void;
|
|
74
|
+
/** The terminal saw a native paste for this window: cancel the fallback. */
|
|
75
|
+
observed: () => void;
|
|
76
|
+
};
|
|
77
|
+
/** Menu entry state for the right-click menu. */
|
|
78
|
+
export declare function terminalMenuState(input: {
|
|
79
|
+
hasSelection: boolean;
|
|
80
|
+
}): {
|
|
81
|
+
copyDisabled: boolean;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Remove ONE bracketed-paste wrapper from an input chunk.
|
|
85
|
+
*
|
|
86
|
+
* xterm wraps `Terminal.paste()`/native pastes while the remote application has
|
|
87
|
+
* bracketed paste enabled. When that mode is stale (an app that enabled it was
|
|
88
|
+
* killed, or the shell never enabled it) the wrapper is displayed literally as
|
|
89
|
+
* `^[[200~...^[[201~` and the text is not executed — a field screenshot showed
|
|
90
|
+
* exactly that. The host-side input path strips one wrapper before forwarding,
|
|
91
|
+
* so the PTY only ever receives the user's text. Apps that expect the wrapper
|
|
92
|
+
* still accept raw text, like a classic terminal.
|
|
93
|
+
*/
|
|
94
|
+
export declare function stripBracketedPasteMarkers(data: string): string;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-remote-vs",
|
|
3
3
|
"description": "VSCode-style remote development for DSH: SSH host manager (key/password), helper deploy pipeline with version check, remote file explorer + editor + terminal in dsh-better-sidebar.",
|
|
4
|
-
"version": "0.21.
|
|
4
|
+
"version": "0.21.1",
|
|
5
5
|
"icon": "icon.svg",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"dsh",
|
|
@@ -480,6 +480,7 @@ export function TerminalView(props: {
|
|
|
480
480
|
rows: 24,
|
|
481
481
|
})}
|
|
482
482
|
visible={props.visible && tab.id === active?.id}
|
|
483
|
+
t={t}
|
|
483
484
|
{...(tab.initialCommand === undefined ? {} : { initialInput: tab.initialCommand })}
|
|
484
485
|
onReady={(id) => patchTab(tab.id, { session: id, connected: true, note: undefined })}
|
|
485
486
|
onExit={(code, message) => {
|
package/src/client/XTerm.tsx
CHANGED
|
@@ -10,6 +10,13 @@ import type { ITheme } from '@xterm/xterm'
|
|
|
10
10
|
import { FitAddon } from '@xterm/addon-fit'
|
|
11
11
|
import type { TerminalClientFrame, TerminalServerFrame } from '../protocol.ts'
|
|
12
12
|
import { isDarkScheme, subscribeColorScheme } from './theme.ts'
|
|
13
|
+
import { ContextMenu, useContextMenu, type MenuEntry } from './vsc/kit.tsx'
|
|
14
|
+
import {
|
|
15
|
+
clipboardChordFor,
|
|
16
|
+
createPasteFallback,
|
|
17
|
+
stripBracketedPasteMarkers,
|
|
18
|
+
terminalMenuState,
|
|
19
|
+
} from './vsc/terminal-clipboard.ts'
|
|
13
20
|
|
|
14
21
|
function send(ws: WebSocket, frame: TerminalClientFrame): void {
|
|
15
22
|
if (ws.readyState === WebSocket.OPEN) {
|
|
@@ -112,11 +119,20 @@ export function XTerm(props: {
|
|
|
112
119
|
* which correctly re-runs the line.
|
|
113
120
|
*/
|
|
114
121
|
initialInput?: string
|
|
122
|
+
/** Translator for the right-click menu (copy / paste / select all). */
|
|
123
|
+
t: (key: string) => string
|
|
115
124
|
}): ReactNode {
|
|
116
125
|
const hostRef = useRef<HTMLDivElement>(null)
|
|
117
126
|
const propsRef = useRef(props)
|
|
118
127
|
propsRef.current = props
|
|
119
128
|
const liveRef = useRef<{ refit: () => void } | undefined>(undefined)
|
|
129
|
+
// The context menu lives at component level (portal); the mount-once effect
|
|
130
|
+
// reaches it through refs, the same pattern the explorer row uses.
|
|
131
|
+
const menu = useContextMenu()
|
|
132
|
+
const openMenuRef = useRef(menu.open)
|
|
133
|
+
openMenuRef.current = menu.open
|
|
134
|
+
const tRef = useRef(props.t)
|
|
135
|
+
tRef.current = props.t
|
|
120
136
|
|
|
121
137
|
// Shown again after being hidden: refit and push the size.
|
|
122
138
|
useEffect(() => {
|
|
@@ -187,7 +203,9 @@ export function XTerm(props: {
|
|
|
187
203
|
* characters — far under that cap either way, and far above a keystroke.
|
|
188
204
|
*/
|
|
189
205
|
const INPUT_CHUNK_CHARS = 64 * 1024
|
|
190
|
-
const sendInput = (
|
|
206
|
+
const sendInput = (rawData: string): void => {
|
|
207
|
+
const data = stripBracketedPasteMarkers(rawData)
|
|
208
|
+
if (data === '') return
|
|
191
209
|
if (data.length <= INPUT_CHUNK_CHARS) {
|
|
192
210
|
send(ws, { t: 'in', d: data })
|
|
193
211
|
return
|
|
@@ -253,9 +271,13 @@ export function XTerm(props: {
|
|
|
253
271
|
// Late layout (tab switch): refit shortly after mount too.
|
|
254
272
|
const timer = window.setTimeout(pushSize, 300)
|
|
255
273
|
|
|
256
|
-
//
|
|
257
|
-
// Ctrl+Shift+C / Cmd+C / Ctrl+Insert =
|
|
258
|
-
// Shift+Insert
|
|
274
|
+
// Clipboard (VS Code / Windows Terminal conventions; plain Ctrl+C stays SIGINT):
|
|
275
|
+
// copy = Ctrl+Shift+C / Cmd+C / Ctrl+Insert, paste = Ctrl+Shift+V / Cmd+V /
|
|
276
|
+
// Shift+Insert. Handled chords call preventDefault() THEMSELVES: returning
|
|
277
|
+
// false only stops xterm, and the browser would then run its own default
|
|
278
|
+
// (Chrome maps Ctrl+Shift+V to paste-as-plain-text, which double-pasted).
|
|
279
|
+
// Plain Ctrl+V is left to the browser first; when no paste event arrives the
|
|
280
|
+
// Clipboard API fallback fires (some shells swallow the native paste).
|
|
259
281
|
const doCopy = (clear: boolean): void => {
|
|
260
282
|
const selection = term.getSelection()
|
|
261
283
|
if (selection === '') return
|
|
@@ -273,30 +295,91 @@ export function XTerm(props: {
|
|
|
273
295
|
term.paste(text)
|
|
274
296
|
})
|
|
275
297
|
}
|
|
298
|
+
const pasteFallback = createPasteFallback({
|
|
299
|
+
read: readClipboard,
|
|
300
|
+
paste: (text) => { term.paste(text) },
|
|
301
|
+
onDenied: () => { term.write('\r\n[dsh-remote] clipboard read denied by the browser\r\n') },
|
|
302
|
+
})
|
|
303
|
+
/**
|
|
304
|
+
* All paste input converges here: one insertion per paste event.
|
|
305
|
+
*
|
|
306
|
+
* Capture phase + stopPropagation keeps xterm's own textarea handler from
|
|
307
|
+
* inserting a second copy, and preventDefault stops the browser's remaining
|
|
308
|
+
* default. The watcher is cancelled so the Clipboard-API fallback (for hosts
|
|
309
|
+
* that never deliver this event) does not fire on top of it.
|
|
310
|
+
*/
|
|
311
|
+
/**
|
|
312
|
+
* Explicit paste chords (Ctrl+Shift+V / Cmd+V / Shift+Insert) insert through
|
|
313
|
+
* the Clipboard API themselves. Some hosts still deliver the browser's native
|
|
314
|
+
* paste afterwards even when the keydown was preventDefault-ed (observed with
|
|
315
|
+
* Ctrl+Shift+V paste-as-plain-text), which pasted twice. This window swallows
|
|
316
|
+
* that native delivery; plain Ctrl+V keeps the native path below.
|
|
317
|
+
*/
|
|
318
|
+
let suppressNativePasteUntil = 0
|
|
319
|
+
const onPasteEvent = (e: ClipboardEvent): void => {
|
|
320
|
+
e.preventDefault()
|
|
321
|
+
e.stopPropagation()
|
|
322
|
+
if (Date.now() < suppressNativePasteUntil) {
|
|
323
|
+
// The explicit chord already inserted this paste through the API.
|
|
324
|
+
suppressNativePasteUntil = 0
|
|
325
|
+
return
|
|
326
|
+
}
|
|
327
|
+
const text = e.clipboardData?.getData('text/plain') ?? ''
|
|
328
|
+
pasteFallback.observed()
|
|
329
|
+
if (text !== '') term.paste(text)
|
|
330
|
+
}
|
|
331
|
+
host.addEventListener('paste', onPasteEvent, true)
|
|
276
332
|
term.attachCustomKeyEventHandler((e) => {
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
return true
|
|
333
|
+
const chord = clipboardChordFor({
|
|
334
|
+
key: e.key,
|
|
335
|
+
ctrl: e.ctrlKey,
|
|
336
|
+
shift: e.shiftKey,
|
|
337
|
+
alt: e.altKey,
|
|
338
|
+
meta: e.metaKey,
|
|
339
|
+
})
|
|
340
|
+
if (chord === undefined) return true
|
|
341
|
+
if (chord === 'copy-interrupt') {
|
|
342
|
+
// Ctrl+C: with a selection the convention is COPY (never SIGINT); without
|
|
343
|
+
// one the key falls through so the remote shell interrupts as usual.
|
|
344
|
+
if (!term.hasSelection()) return true
|
|
345
|
+
e.preventDefault()
|
|
346
|
+
doCopy(false)
|
|
347
|
+
return false
|
|
289
348
|
}
|
|
290
|
-
if (
|
|
349
|
+
if (chord === 'paste') {
|
|
350
|
+
// Explicit paste chord: insert ONCE through the Clipboard API and swallow
|
|
351
|
+
// the browser's native delivery for this gesture (some hosts ignore the
|
|
352
|
+
// keydown preventDefault and paste again).
|
|
353
|
+
e.preventDefault()
|
|
354
|
+
suppressNativePasteUntil = Date.now() + 600
|
|
291
355
|
doPaste()
|
|
292
356
|
return false
|
|
293
357
|
}
|
|
294
|
-
|
|
358
|
+
if (chord === 'paste-native') {
|
|
359
|
+
// Plain Ctrl+V: the browser's native paste is the primary path; when the
|
|
360
|
+
// host swallows it, the watcher falls back to the Clipboard API.
|
|
361
|
+
pasteFallback.arm()
|
|
362
|
+
return true
|
|
363
|
+
}
|
|
364
|
+
// Copy: with nothing selected the key belongs to the terminal/browser.
|
|
365
|
+
if (!term.hasSelection()) return true
|
|
366
|
+
e.preventDefault()
|
|
367
|
+
doCopy(false)
|
|
368
|
+
return false
|
|
295
369
|
})
|
|
296
370
|
const onContextMenu = (e: MouseEvent): void => {
|
|
297
371
|
e.preventDefault()
|
|
298
|
-
|
|
299
|
-
|
|
372
|
+
const hasSelection = term.hasSelection()
|
|
373
|
+
const entries: MenuEntry[] = [
|
|
374
|
+
{
|
|
375
|
+
label: tRef.current('term.copy'),
|
|
376
|
+
disabled: terminalMenuState({ hasSelection }).copyDisabled,
|
|
377
|
+
onSelect: () => { doCopy(false); term.focus() },
|
|
378
|
+
},
|
|
379
|
+
{ label: tRef.current('term.paste'), onSelect: () => { doPaste(); term.focus() } },
|
|
380
|
+
{ label: tRef.current('term.selectAll'), separated: true, onSelect: () => { term.selectAll(); term.focus() } },
|
|
381
|
+
]
|
|
382
|
+
openMenuRef.current(e.clientX, e.clientY, entries)
|
|
300
383
|
}
|
|
301
384
|
host.addEventListener('contextmenu', onContextMenu)
|
|
302
385
|
|
|
@@ -306,6 +389,7 @@ export function XTerm(props: {
|
|
|
306
389
|
unflip()
|
|
307
390
|
window.clearTimeout(timer)
|
|
308
391
|
window.removeEventListener('resize', onWindowResize)
|
|
392
|
+
host.removeEventListener('paste', onPasteEvent, true)
|
|
309
393
|
host.removeEventListener('contextmenu', onContextMenu)
|
|
310
394
|
resizeObserver.disconnect()
|
|
311
395
|
try {
|
|
@@ -319,5 +403,12 @@ export function XTerm(props: {
|
|
|
319
403
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
320
404
|
}, [])
|
|
321
405
|
|
|
322
|
-
return
|
|
406
|
+
return (
|
|
407
|
+
<>
|
|
408
|
+
<div ref={hostRef} style={{ height: '100%', minHeight: 200 }} />
|
|
409
|
+
{menu.menu !== undefined && (
|
|
410
|
+
<ContextMenu x={menu.menu.x} y={menu.menu.y} entries={menu.menu.entries} onClose={menu.close} />
|
|
411
|
+
)}
|
|
412
|
+
</>
|
|
413
|
+
)
|
|
323
414
|
}
|
package/src/client/i18n.ts
CHANGED
|
@@ -40,6 +40,11 @@ export const DYNAMIC_KEYS = [
|
|
|
40
40
|
// exit), which stores the key and renders it later.
|
|
41
41
|
'term.disconnected',
|
|
42
42
|
'term.closed',
|
|
43
|
+
// Reached through tRef.current(...) in the XTerm right-click menu (built
|
|
44
|
+
// inside a mount-once effect: literal keys, but not a t() call site).
|
|
45
|
+
'term.copy',
|
|
46
|
+
'term.paste',
|
|
47
|
+
'term.selectAll',
|
|
43
48
|
// Labels chosen through connectionLabelKey in connection.ts (a state → key
|
|
44
49
|
// mapping, so the call site is (connectionLabelKey(state))).
|
|
45
50
|
'connection.ok',
|
|
@@ -202,7 +207,10 @@ const zh: Record<string, string> = {
|
|
|
202
207
|
'term.new': '新建终端',
|
|
203
208
|
'term.empty': '还没有终端 —— 选好主机后点右上角 + 新建一个。',
|
|
204
209
|
'term.close': '关闭终端',
|
|
205
|
-
'term.closeTitle': '关闭这个终端',
|
|
210
|
+
'term.closeTitle': '关闭这个终端',
|
|
211
|
+
'term.copy': '复制',
|
|
212
|
+
'term.paste': '粘贴',
|
|
213
|
+
'term.selectAll': '全选',
|
|
206
214
|
'term.noHosts': '还没有主机,请先去「设置 → 远程主机」配置。',
|
|
207
215
|
'tab.git': 'Git',
|
|
208
216
|
'git.hint': '请选择远端路径为 git 仓库的工作区。',
|
|
@@ -817,7 +825,10 @@ const en: Record<string, string> = {
|
|
|
817
825
|
'term.new': 'New terminal',
|
|
818
826
|
'term.empty': 'No terminal yet — pick a host, then press + to open one.',
|
|
819
827
|
'term.close': 'Close terminal',
|
|
820
|
-
'term.closeTitle': 'Close this terminal',
|
|
828
|
+
'term.closeTitle': 'Close this terminal',
|
|
829
|
+
'term.copy': 'Copy',
|
|
830
|
+
'term.paste': 'Paste',
|
|
831
|
+
'term.selectAll': 'Select all',
|
|
821
832
|
'term.noHosts': 'No hosts yet. Configure one in Settings → Remote Hosts first.',
|
|
822
833
|
'tab.git': 'Git',
|
|
823
834
|
'git.hint': 'Pick a workspace whose remote path is a git repository.',
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal clipboard chords and the paste fallback, as testable logic.
|
|
3
|
+
*
|
|
4
|
+
* Why this module exists
|
|
5
|
+
* ----------------------
|
|
6
|
+
* The terminal's copy/paste handling used to be an inline `attachCustomKeyEventHandler`
|
|
7
|
+
* with three problems the UI tests could not see:
|
|
8
|
+
*
|
|
9
|
+
* 1. **Double paste on Ctrl+Shift+V.** The handler pasted through
|
|
10
|
+
* `navigator.clipboard.readText()` and returned `false`, but returning false only
|
|
11
|
+
* stops xterm's own handling — the browser still performed its default paste
|
|
12
|
+
* (Chrome maps Ctrl+Shift+V to "paste as plain text"), so both paths inserted the
|
|
13
|
+
* text. Handled chords must call `preventDefault()` themselves.
|
|
14
|
+
* 2. **Ctrl+V depended entirely on the browser's native paste.** When the host page
|
|
15
|
+
* or a wrapper swallows the paste event (desktop shells, non-standard clipboard
|
|
16
|
+
* permissions), the terminal silently received nothing. It now falls back to the
|
|
17
|
+
* Clipboard API only when no paste event arrived shortly after the key press.
|
|
18
|
+
* 3. **Right-click copied/pasted immediately with no visible affordance.** It is now
|
|
19
|
+
* a small menu (copy / paste / select all) so the action is discoverable and the
|
|
20
|
+
* copy item can be disabled when nothing is selected.
|
|
21
|
+
*
|
|
22
|
+
* The chord table and the fallback watcher are pure so a gate can pin all of it.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** What one key press asks the terminal to do. */
|
|
26
|
+
export type TerminalClipboardChord =
|
|
27
|
+
/** Copy the current selection, then clear it (menu action). */
|
|
28
|
+
| 'copy'
|
|
29
|
+
/** Paste immediately through the Clipboard API. */
|
|
30
|
+
| 'paste'
|
|
31
|
+
/** Let the browser's native paste run; fall back to the Clipboard API when no paste event arrives. */
|
|
32
|
+
| 'paste-native'
|
|
33
|
+
/** Plain Ctrl+C: copy when a selection exists, otherwise stay SIGINT. */
|
|
34
|
+
| 'copy-interrupt'
|
|
35
|
+
|
|
36
|
+
/** The keyboard state the chord table reads. */
|
|
37
|
+
export interface TerminalChordInput {
|
|
38
|
+
key: string
|
|
39
|
+
ctrl: boolean
|
|
40
|
+
shift: boolean
|
|
41
|
+
alt: boolean
|
|
42
|
+
meta: boolean
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** True when the key is the letter (layout-aware `code` is not required: xterm gives `key`). */
|
|
46
|
+
function isLetter(input: TerminalChordInput, letter: string): boolean {
|
|
47
|
+
return input.key.toLowerCase() === letter
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Map one keydown to a clipboard chord.
|
|
52
|
+
*
|
|
53
|
+
* Conventions: Ctrl+Shift+C / Ctrl+Insert / Cmd+C copy, Ctrl+Shift+V / Cmd+V /
|
|
54
|
+
* Shift+Insert paste. Plain Ctrl+V is left to the browser first (`paste-native`),
|
|
55
|
+
* and plain Ctrl+C always stays SIGINT.
|
|
56
|
+
*/
|
|
57
|
+
export function clipboardChordFor(input: TerminalChordInput): TerminalClipboardChord | undefined {
|
|
58
|
+
const ctrl = input.ctrl,
|
|
59
|
+
shift = input.shift,
|
|
60
|
+
meta = input.meta,
|
|
61
|
+
alt = input.alt
|
|
62
|
+
if (alt) return undefined
|
|
63
|
+
if (!meta && ctrl && shift && isLetter(input, 'c')) return 'copy'
|
|
64
|
+
if (!meta && ctrl && !shift && input.key === 'Insert') return 'copy'
|
|
65
|
+
if (meta && !ctrl && !shift && isLetter(input, 'c')) return 'copy'
|
|
66
|
+
if (!meta && ctrl && shift && isLetter(input, 'v')) return 'paste'
|
|
67
|
+
if (!meta && !ctrl && shift && input.key === 'Insert') return 'paste'
|
|
68
|
+
if (meta && !ctrl && !shift && isLetter(input, 'v')) return 'paste'
|
|
69
|
+
if (!meta && ctrl && !shift && isLetter(input, 'v')) return 'paste-native'
|
|
70
|
+
// Plain Ctrl+C only means COPY when there is a selection; without one it must
|
|
71
|
+
// stay SIGINT. The chord therefore only says "look at the selection".
|
|
72
|
+
if (!meta && ctrl && !shift && isLetter(input, 'c')) return 'copy-interrupt'
|
|
73
|
+
return undefined
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Copy and paste chords take the key over; the native-paste chord must not. */
|
|
77
|
+
export function chordTakesKey(chord: TerminalClipboardChord): boolean {
|
|
78
|
+
return chord !== 'paste-native'
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Dependencies of {@link createPasteFallback}. */
|
|
82
|
+
export interface PasteFallbackDeps {
|
|
83
|
+
/** Clipboard read; `undefined` when the browser denies it. */
|
|
84
|
+
read: () => Promise<string | undefined>
|
|
85
|
+
/** Insert text into the terminal. */
|
|
86
|
+
paste: (text: string) => void
|
|
87
|
+
/** Report a denied clipboard read (shown in the terminal). */
|
|
88
|
+
onDenied: () => void
|
|
89
|
+
/** Timer injection (tests use a fake clock). */
|
|
90
|
+
schedule?: (run: () => void, ms: number) => unknown
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** How long to wait for a native paste event before falling back to the API. */
|
|
94
|
+
export const PASTE_FALLBACK_MS = 180
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Watches one Ctrl+V press: the browser's native paste wins when its event arrives,
|
|
98
|
+
* otherwise the Clipboard API inserts the text after {@link PASTE_FALLBACK_MS}.
|
|
99
|
+
*
|
|
100
|
+
* The watcher is a single slot: arming again (a held/repeated key) replaces the
|
|
101
|
+
* pending window instead of stacking pastes.
|
|
102
|
+
*/
|
|
103
|
+
export function createPasteFallback(deps: PasteFallbackDeps): {
|
|
104
|
+
arm: () => void
|
|
105
|
+
/** The terminal saw a native paste for this window: cancel the fallback. */
|
|
106
|
+
observed: () => void
|
|
107
|
+
} {
|
|
108
|
+
const schedule = deps.schedule
|
|
109
|
+
?? ((run, ms) => {
|
|
110
|
+
const timer = setTimeout(run, ms)
|
|
111
|
+
// Never keep a Host-alike or a page alive for a clipboard probe.
|
|
112
|
+
;(timer as unknown as { unref?: () => void }).unref?.()
|
|
113
|
+
return timer
|
|
114
|
+
})
|
|
115
|
+
let pending: { cancelled: boolean } | undefined
|
|
116
|
+
return {
|
|
117
|
+
arm: () => {
|
|
118
|
+
if (pending !== undefined) pending.cancelled = true
|
|
119
|
+
const token = { cancelled: false }
|
|
120
|
+
pending = token
|
|
121
|
+
schedule(() => {
|
|
122
|
+
if (token.cancelled) return
|
|
123
|
+
token.cancelled = true
|
|
124
|
+
if (pending === token) pending = undefined
|
|
125
|
+
void deps.read().then((text) => {
|
|
126
|
+
if (text === undefined) {
|
|
127
|
+
deps.onDenied()
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
deps.paste(text)
|
|
131
|
+
})
|
|
132
|
+
}, PASTE_FALLBACK_MS)
|
|
133
|
+
},
|
|
134
|
+
observed: () => {
|
|
135
|
+
if (pending !== undefined) pending.cancelled = true
|
|
136
|
+
pending = undefined
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Menu entry state for the right-click menu. */
|
|
142
|
+
export function terminalMenuState(input: { hasSelection: boolean }): {
|
|
143
|
+
copyDisabled: boolean
|
|
144
|
+
} {
|
|
145
|
+
return { copyDisabled: !input.hasSelection }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// ------------------------------------------------- bracketed-paste sanitising
|
|
149
|
+
|
|
150
|
+
/** Start/end markers a terminal wraps around a bracketed paste. */
|
|
151
|
+
const PASTE_START = '\u001b[200~'
|
|
152
|
+
const PASTE_END = '\u001b[201~'
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Remove ONE bracketed-paste wrapper from an input chunk.
|
|
156
|
+
*
|
|
157
|
+
* xterm wraps `Terminal.paste()`/native pastes while the remote application has
|
|
158
|
+
* bracketed paste enabled. When that mode is stale (an app that enabled it was
|
|
159
|
+
* killed, or the shell never enabled it) the wrapper is displayed literally as
|
|
160
|
+
* `^[[200~...^[[201~` and the text is not executed — a field screenshot showed
|
|
161
|
+
* exactly that. The host-side input path strips one wrapper before forwarding,
|
|
162
|
+
* so the PTY only ever receives the user's text. Apps that expect the wrapper
|
|
163
|
+
* still accept raw text, like a classic terminal.
|
|
164
|
+
*/
|
|
165
|
+
export function stripBracketedPasteMarkers(data: string): string {
|
|
166
|
+
if (data.length < PASTE_START.length) return data
|
|
167
|
+
if (!data.startsWith(PASTE_START)) return data
|
|
168
|
+
if (!data.endsWith(PASTE_END)) return data
|
|
169
|
+
return data.slice(PASTE_START.length, data.length - PASTE_END.length)
|
|
170
|
+
}
|