@maxbill/dsh-file-explorer 1.1.2 → 1.2.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 +21 -3
- package/README.md +21 -3
- package/lib/client.js +566 -13
- package/lib/index.js +303 -2
- package/package.json +7 -1
package/README.en.md
CHANGED
|
@@ -207,10 +207,12 @@ Do those four things once and you know the whole plugin.
|
|
|
207
207
|
- Writes go through DSH's `ctx.fs` (atomic write + version guard): if the file changed on disk after you opened it, the host returns **409** and the editor offers *reload* or *overwrite* — never a silent overwrite.
|
|
208
208
|
- Binary extensions (zip / images / media …) have no edit button, so bytes that cannot round-trip are never written back.
|
|
209
209
|
|
|
210
|
-
#### 7.
|
|
210
|
+
#### 7. Very few dependencies, still no build step
|
|
211
211
|
|
|
212
212
|
- The host side is plain ESM (`lib/index.js`); the browser side is a hand-written `window.__ModuleLoader__` factory module (`lib/client.js`). **There is no bundling step** — change it, reload the page, done.
|
|
213
|
-
-
|
|
213
|
+
- The only third-party front-end dependency is the terminal renderer **@xterm/xterm**; the host hands it to the page on demand instead of bundling it.
|
|
214
|
+
- The terminal can also use **node-pty** (an `optionalDependencies` entry with prebuilt macOS / Windows binaries, nothing to compile): when it loads, it provides the real pty; otherwise the `python3` bridge is used, and plain pipes are the last resort.
|
|
215
|
+
- XLSX / DOCX / ZIP parsing happens in the host on Node's standard library; the terminal gets a pty from the machine's own `/usr/bin/python3`, so nothing native is compiled.
|
|
214
216
|
|
|
215
217
|
---
|
|
216
218
|
|
|
@@ -303,6 +305,16 @@ Do those four things once and you know the whole plugin.
|
|
|
303
305
|
| Folders, extra | **Add as workspace** |
|
|
304
306
|
| Empty space | New folder…, new file…, paste, reveal current folder in file manager, refresh |
|
|
305
307
|
|
|
308
|
+
#### 4.9 Terminal
|
|
309
|
+
|
|
310
|
+
- **Where**: a Terminal button right after Sort in the toolbar, and Open in terminal on a folder context menu, which starts a shell in that directory.
|
|
311
|
+
- **Layout**: it docks between the file list and the status bar; drag its top grip to change the height, capped at half the window, and the height is remembered.
|
|
312
|
+
- **Tabs**: the header is a tab strip — one tab per shell, named after its folder (the full path is in the tooltip), and **double-clicking a tab name renames it**. The × closes that terminal and + opens another one; clicking a tab switches to it, and the shells you switch away from keep running.
|
|
313
|
+
- **What it runs**: a real pty — **node-pty** when it is installed (macOS, Linux and Windows ConPTY), otherwise a `pty.fork` bridge through the host's `python3`. Colours and full-screen programs such as `vim` and `top` work, and dragging the dock passes the new size to the shell. Output is polled every 120 ms; keystrokes are batched every 12 ms.
|
|
314
|
+
- **Look**: 13 px font with a 1.5 line height, and the palette follows the app between light and dark; there are no extra settings.
|
|
315
|
+
- **Sessions**: only the close button ends the shell. Switching to a conversation and back reattaches the same shell and keeps what was on screen, and a terminal nobody has looked at for half an hour is reclaimed.
|
|
316
|
+
- **Limits**: desktop only (the button and menu item are hidden over a remote connection), at most three sessions.
|
|
317
|
+
|
|
306
318
|
---
|
|
307
319
|
|
|
308
320
|
### 5. Previewable file types
|
|
@@ -337,6 +349,7 @@ Do those four things once and you know the whole plugin.
|
|
|
337
349
|
| Move to trash | ✅ `~/.Trash` | ✅ Recycle Bin via PowerShell, falling back to `~/.Trash` | ✅ freedesktop (`.trashinfo`, restorable) |
|
|
338
350
|
| Address bar paths | ✅ POSIX | ✅ drive letters and UNC | ✅ POSIX |
|
|
339
351
|
| Upload a whole folder | ✅ | ✅ | ✅ `webkitdirectory` (Chrome / Edge / Firefox; Safari degrades to picking files) |
|
|
352
|
+
| Terminal | ✅ real pty (node-pty / python3) | ✅ ConPTY (with node-pty; pipes otherwise) | ✅ real pty (node-pty / python3) |
|
|
340
353
|
|
|
341
354
|
> ⚠️ **Windows and Linux have not been verified end to end on real machines.** Both branches follow the platform conventions (Explorer's `/select,`, the PowerShell recycle-bin call, freedesktop's `files` + `info` layout) and are covered by “platform plan” test cases in `npm test` that check command and path construction — but nobody has clicked through them on real hardware.
|
|
342
355
|
|
|
@@ -393,6 +406,11 @@ The decision is made from the request's origin: a loopback Host counts as local;
|
|
|
393
406
|
- **Search** — 4000 entries, 200 hits, 4 seconds
|
|
394
407
|
- **Upload** — 512 MB
|
|
395
408
|
|
|
409
|
+
**Terminal**
|
|
410
|
+
|
|
411
|
+
- The terminal runs commands on the host machine, exactly like a shell there. It is only offered when the browser reached us over loopback; a remote browser gets 403.
|
|
412
|
+
- Closing the terminal or the panel ends the process, and at most three sessions are kept.
|
|
413
|
+
|
|
396
414
|
**Git**
|
|
397
415
|
|
|
398
416
|
- Read-only `git status --porcelain` with `GIT_OPTIONAL_LOCKS=0`; the index is never written and the working tree is never touched.
|
|
@@ -468,7 +486,7 @@ There is no build step: `lib/index.js` is plain ESM and `lib/client.js` is a han
|
|
|
468
486
|
| File | Purpose |
|
|
469
487
|
|:--|:--|
|
|
470
488
|
| `lib/index.js` | Host side: registers the 25 `/api/file-explorer` routes on DSH's `connection` service, with every path `realpath`-ed into `$HOME` |
|
|
471
|
-
| `lib/client.js` | Browser side: registers the sidebar icon and the `main` panel;
|
|
489
|
+
| `lib/client.js` | Browser side: registers the sidebar icon and the `main` panel; needs only the host's `react` plus a lazily loaded xterm |
|
|
472
490
|
| `cordis.patch.yml` | Bundle patch that inserts the Loader entry `dsh-file-explorer` |
|
|
473
491
|
| `assets/icons/` | 12 ready-to-use two-tone SVGs (`node scripts/export-icons.mjs` exports them from `SHAPES`) |
|
|
474
492
|
| `test/host-api.test.mjs` | Host API tests (including the three-platform command plans) |
|
package/README.md
CHANGED
|
@@ -206,10 +206,12 @@ dsh plugin --profile desktop add @maxbill/dsh-file-explorer
|
|
|
206
206
|
- 写入走 DSH 的 `ctx.fs`(原子写 + 版本守卫):文件被外部改过时宿主返回 **409**,编辑器给「重新加载 / 覆盖保存」二选一,不静默覆盖。
|
|
207
207
|
- 二进制扩展名(zip / 图片 / 音视频等)不给编辑入口,避免把无法往返的字节写回去。
|
|
208
208
|
|
|
209
|
-
#### 7.
|
|
209
|
+
#### 7. 依赖很少、没有构建步骤
|
|
210
210
|
|
|
211
211
|
- 宿主端是普通 ESM(`lib/index.js`),浏览器端是手写的 `window.__ModuleLoader__` 工厂模块(`lib/client.js`),**没有打包步骤**,改完重载页面即生效。
|
|
212
|
-
-
|
|
212
|
+
- 前端唯一的第三方依赖是终端渲染器 **@xterm/xterm**:宿主按需从 `node_modules` 交给页面,不打进 client 模块。
|
|
213
|
+
- 终端另外可选装 **node-pty**(`optionalDependencies`,带 macOS / Windows 预编译,无需编译):装上就用它开真 PTY;没装或加载失败时回退到 `python3` 中继,再不行退到管道。
|
|
214
|
+
- XLSX / DOCX / ZIP 的解析都在宿主用 Node 标准库完成;终端用宿主自带的 `/usr/bin/python3` 建 PTY,不需要编译原生模块。
|
|
213
215
|
|
|
214
216
|
---
|
|
215
217
|
|
|
@@ -302,6 +304,16 @@ dsh plugin --profile desktop add @maxbill/dsh-file-explorer
|
|
|
302
304
|
| 文件夹额外 | **添加为工作区** |
|
|
303
305
|
| 空白处 | 新建文件夹…、新建文件…、粘贴、在访达中显示当前文件夹、刷新 |
|
|
304
306
|
|
|
307
|
+
#### 4.9 终端
|
|
308
|
+
|
|
309
|
+
- **入口**:工具栏「排序」之后的「终端」按钮;文件夹右键「在终端打开」,直接在该目录起一个 shell。
|
|
310
|
+
- **位置**:停在文件区与状态栏之间,拖动顶部把手可调高度,**最高不超过窗口一半**,高度记在浏览器里。
|
|
311
|
+
- **页签**:标题栏就是标签栏 —— 每个终端一个标签(名字取目录名,完整路径在悬停提示里),**双击标签名可以改名**;点 × 关掉这个终端、点 + 再开一个;标签之间点一下就切换,切走的终端在后台继续跑。
|
|
312
|
+
- **能力**:真 PTY —— 优先用 `node-pty`(macOS / Linux / Windows 的 ConPTY),没装时用 `python3` 的 `pty.fork` 中继,都可跑彩色输出与 `vim` / `top` 这类全屏程序,拖动高度会同步窗口大小;输出按 120 ms 轮询拉取,输入按 12 ms 合并发送。
|
|
313
|
+
- **外观**:字号 13、行高 1.5,配色跟随界面深浅自动切换,不提供额外设置。
|
|
314
|
+
- **会话**:点 × 关闭终端才结束 shell;切到会话再切回来会接回同一个 shell,屏幕内容保留;半小时无人查看的终端自动回收。
|
|
315
|
+
- **限制**:仅桌面端可用(远程访问时按钮与菜单项隐藏);同时最多保留 3 个会话。
|
|
316
|
+
|
|
305
317
|
---
|
|
306
318
|
|
|
307
319
|
### 五、可预览的文件类型
|
|
@@ -336,6 +348,7 @@ dsh plugin --profile desktop add @maxbill/dsh-file-explorer
|
|
|
336
348
|
| 移到废纸篓 | ✅ `~/.Trash` | ✅ 系统回收站(PowerShell,失败退回 `~/.Trash`) | ✅ freedesktop(`.trashinfo`,可还原) |
|
|
337
349
|
| 地址栏路径 | ✅ POSIX | ✅ 盘符与 UNC | ✅ POSIX |
|
|
338
350
|
| 上传整个文件夹 | ✅ | ✅ | ✅ `webkitdirectory`(Chrome / Edge / Firefox;Safari 退化为选文件) |
|
|
351
|
+
| 终端 | ✅ 真 PTY(node-pty / python3) | ✅ ConPTY(装 node-pty 后;否则管道) | ✅ 真 PTY(node-pty / python3) |
|
|
339
352
|
|
|
340
353
|
> ⚠️ **Windows 与 Linux 尚未实机完整验收**。两条分支按各自官方约定实现(Explorer 的 `/select,`、回收站的 `SendToRecycleBin`、freedesktop 的 `files`+`info` 目录结构),并在 `npm test` 里用「平台计划」用例模拟验证命令与路径拼接,但没有在真机点过一遍。
|
|
341
354
|
|
|
@@ -392,6 +405,11 @@ dsh plugin --profile desktop add @maxbill/dsh-file-explorer
|
|
|
392
405
|
- **搜索** —— 4000 个目录项、200 条命中、4 秒
|
|
393
406
|
- **上传** —— 512 MB
|
|
394
407
|
|
|
408
|
+
**终端**
|
|
409
|
+
|
|
410
|
+
- 终端执行的是宿主机器上的命令,等同在宿主上开一个 shell;只在浏览器来自本机(回环地址)时提供,远程访问一律 403。
|
|
411
|
+
- 关闭终端或面板时结束对应进程;同时最多保留 3 个会话。
|
|
412
|
+
|
|
395
413
|
**Git**
|
|
396
414
|
|
|
397
415
|
- 只读 `git status --porcelain`,设 `GIT_OPTIONAL_LOCKS=0`,绝不写索引、不改工作区。
|
|
@@ -467,7 +485,7 @@ npm test # 宿主端 HTTP 接口测试,在 $HOME 下的临时目录里
|
|
|
467
485
|
| 文件 | 作用 |
|
|
468
486
|
|:--|:--|
|
|
469
487
|
| `lib/index.js` | Host 端:把 `/api/file-explorer` 的 25 条路由注册到 DSH `connection` 服务,路径统一 `realpath` 收敛在 `$HOME` |
|
|
470
|
-
| `lib/client.js` | 浏览器端:注册左侧栏图标与 `main`
|
|
488
|
+
| `lib/client.js` | 浏览器端:注册左侧栏图标与 `main` 面板;只依赖宿主提供的 `react` 与按需加载的 xterm |
|
|
471
489
|
| `cordis.patch.yml` | Bundle patch,把 Loader 条目 `dsh-file-explorer` 插入 |
|
|
472
490
|
| `assets/icons/` | 12 个可直接取用的双色 SVG(`node scripts/export-icons.mjs` 从 `SHAPES` 导出) |
|
|
473
491
|
| `test/host-api.test.mjs` | 宿主接口测试(含三平台命令计划) |
|
package/lib/client.js
CHANGED
|
@@ -14,6 +14,327 @@ window.__ModuleLoader__.load({
|
|
|
14
14
|
var useRef = React.useRef;
|
|
15
15
|
var useState = React.useState;
|
|
16
16
|
|
|
17
|
+
/** xterm is loaded from the host once, on first use. */
|
|
18
|
+
var xtermPromise = null;
|
|
19
|
+
function loadXterm() {
|
|
20
|
+
if (xtermPromise) return xtermPromise;
|
|
21
|
+
xtermPromise = new Promise(function (resolve, reject) {
|
|
22
|
+
if (window.Terminal) { resolve(window.Terminal); return; }
|
|
23
|
+
var css = document.createElement("link");
|
|
24
|
+
css.rel = "stylesheet";
|
|
25
|
+
css.href = API + "/vendor?name=xterm.css";
|
|
26
|
+
document.head.appendChild(css);
|
|
27
|
+
var script = document.createElement("script");
|
|
28
|
+
script.src = API + "/vendor?name=xterm.js";
|
|
29
|
+
script.onload = function () {
|
|
30
|
+
if (window.Terminal) resolve(window.Terminal);
|
|
31
|
+
else reject(new Error("xterm did not register"));
|
|
32
|
+
};
|
|
33
|
+
script.onerror = function () { reject(new Error("xterm failed to load")); };
|
|
34
|
+
document.head.appendChild(script);
|
|
35
|
+
});
|
|
36
|
+
return xtermPromise;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
/** Terminal metrics: one fixed size, the theme follows the app. */
|
|
41
|
+
var TERM_FONT_SIZE = 13;
|
|
42
|
+
var TERM_LINE_HEIGHT = 1.5;
|
|
43
|
+
var TERM_THEMES = {
|
|
44
|
+
light: { background: "#ffffff", foreground: "#1b1b1b", cursor: "#3b6cf6", selectionBackground: "rgba(59, 108, 246, .22)" },
|
|
45
|
+
dark: { background: "#1f1f1f", foreground: "#e6edf3", cursor: "#7aa2ff", selectionBackground: "rgba(122, 162, 255, .3)" },
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** True when the page is in a dark theme. */
|
|
49
|
+
function termIsDark() {
|
|
50
|
+
return (document.documentElement.getAttribute("data-ds-theme-source") || "") === "dark"
|
|
51
|
+
|| !!(window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The xterm theme for the page theme. */
|
|
55
|
+
function termTheme() {
|
|
56
|
+
return termIsDark() ? TERM_THEMES.dark : TERM_THEMES.light;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Character width for the terminal font, measured with a canvas. */
|
|
60
|
+
function termCharWidth() {
|
|
61
|
+
var canvas = document.createElement("canvas");
|
|
62
|
+
var context = canvas.getContext("2d");
|
|
63
|
+
context.font = TERM_FONT_SIZE + "px ui-monospace, SFMono-Regular, Menlo, monospace";
|
|
64
|
+
return context.measureText("MMMMMMMMMM").width / 10;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The cell size xterm actually renders with. Its internal cell height is
|
|
69
|
+
* taller than fontSize * lineHeight, so it is read from the renderer and
|
|
70
|
+
* only estimated when that is unavailable.
|
|
71
|
+
* @param {object} terminal - xterm instance
|
|
72
|
+
* @returns {object} width and height in css pixels
|
|
73
|
+
*/
|
|
74
|
+
function termCell(terminal) {
|
|
75
|
+
try {
|
|
76
|
+
var dims = terminal && terminal._core && terminal._core._renderService && terminal._core._renderService.dimensions;
|
|
77
|
+
var cell = dims && dims.css && dims.css.cell;
|
|
78
|
+
if (cell && cell.width > 0 && cell.height > 0) return { width: cell.width, height: cell.height };
|
|
79
|
+
} catch (error) {}
|
|
80
|
+
return { width: termCharWidth(), height: TERM_FONT_SIZE * TERM_LINE_HEIGHT * 1.15 };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** How many cells fit in the dock; the vertical padding is derived after. */
|
|
84
|
+
function termGrid(host, terminal) {
|
|
85
|
+
var cell = termCell(terminal);
|
|
86
|
+
var style = window.getComputedStyle(host);
|
|
87
|
+
var padX = (parseFloat(style.paddingLeft) || 0) + (parseFloat(style.paddingRight) || 0);
|
|
88
|
+
return {
|
|
89
|
+
cols: Math.max(20, Math.floor((host.clientWidth - padX - 2) / cell.width)),
|
|
90
|
+
rows: Math.max(4, Math.floor((host.clientHeight - 2) / cell.height)),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Split the leftover height evenly above and below the grid. */
|
|
95
|
+
function termCenter(host, terminal) {
|
|
96
|
+
var cell = termCell(terminal);
|
|
97
|
+
var used = terminal.rows * cell.height;
|
|
98
|
+
var pad = Math.max(0, (host.clientHeight - used) / 2);
|
|
99
|
+
host.style.paddingTop = pad + "px";
|
|
100
|
+
host.style.paddingBottom = pad + "px";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The docked terminal: a tab per shell, one xterm instance for the active
|
|
105
|
+
* tab, polled output.
|
|
106
|
+
* @param {object} props - cwd, height, handlers
|
|
107
|
+
* @returns {object} element
|
|
108
|
+
*/
|
|
109
|
+
function TerminalDock(props) {
|
|
110
|
+
var hostRef = useRef(null);
|
|
111
|
+
var termRef = useRef(null);
|
|
112
|
+
var [revision, setRevision] = useState(function () {
|
|
113
|
+
if (termTabs.length === 0) termEnsureTab(props.cwd);
|
|
114
|
+
else if (!termActiveKey) termActiveKey = termTabs[0].key;
|
|
115
|
+
return 0;
|
|
116
|
+
});
|
|
117
|
+
var [editingKey, setEditingKey] = useState(null);
|
|
118
|
+
var [draftName, setDraftName] = useState("");
|
|
119
|
+
var height = props.height;
|
|
120
|
+
var active = null;
|
|
121
|
+
for (var i = 0; i < termTabs.length; i += 1) {
|
|
122
|
+
if (termTabs[i].key === termActiveKey) active = termTabs[i];
|
|
123
|
+
}
|
|
124
|
+
var bump = function () { setRevision(function (count) { return count + 1; }); };
|
|
125
|
+
|
|
126
|
+
/** Rename a tab; an empty name falls back to the folder. */
|
|
127
|
+
var commitTabName = function (tab) {
|
|
128
|
+
var next = draftName.trim();
|
|
129
|
+
tab.name = next.length > 0 ? next : null;
|
|
130
|
+
setEditingKey(null);
|
|
131
|
+
bump();
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// the xterm instance is created once and reused for every tab, so
|
|
135
|
+
// switching tabs never tears the terminal down and flashes the panel
|
|
136
|
+
useEffect(function () {
|
|
137
|
+
var disposed = false;
|
|
138
|
+
var terminal = null;
|
|
139
|
+
loadXterm().then(function (Terminal) {
|
|
140
|
+
if (disposed) return;
|
|
141
|
+
var host = hostRef.current;
|
|
142
|
+
if (!host) return;
|
|
143
|
+
terminal = new Terminal({
|
|
144
|
+
fontSize: TERM_FONT_SIZE,
|
|
145
|
+
lineHeight: TERM_LINE_HEIGHT,
|
|
146
|
+
fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace",
|
|
147
|
+
cursorBlink: true,
|
|
148
|
+
scrollback: 4000,
|
|
149
|
+
allowProposedApi: true,
|
|
150
|
+
theme: termTheme(),
|
|
151
|
+
});
|
|
152
|
+
terminal.open(host);
|
|
153
|
+
termRef.current = terminal;
|
|
154
|
+
var grid = termGrid(host, terminal);
|
|
155
|
+
terminal.resize(grid.cols, grid.rows);
|
|
156
|
+
termCenter(host, terminal);
|
|
157
|
+
setRevision(function (count) { return count + 1; });
|
|
158
|
+
});
|
|
159
|
+
return function () {
|
|
160
|
+
disposed = true;
|
|
161
|
+
if (terminal) terminal.dispose();
|
|
162
|
+
termRef.current = null;
|
|
163
|
+
};
|
|
164
|
+
}, []);
|
|
165
|
+
|
|
166
|
+
// bind the reused terminal to the active tab: replay its screen, then
|
|
167
|
+
// forward keys and poll its output
|
|
168
|
+
useEffect(function () {
|
|
169
|
+
var terminal = termRef.current;
|
|
170
|
+
var host = hostRef.current;
|
|
171
|
+
if (!terminal || !host || !active) return undefined;
|
|
172
|
+
var stopped = false;
|
|
173
|
+
var timer = null;
|
|
174
|
+
var inputTimer = null;
|
|
175
|
+
var pending = "";
|
|
176
|
+
var dataSub = null;
|
|
177
|
+
|
|
178
|
+
terminal.reset();
|
|
179
|
+
if (active.log) terminal.write(active.log);
|
|
180
|
+
|
|
181
|
+
var flush = function () {
|
|
182
|
+
if (!pending || !active.id) return;
|
|
183
|
+
var data = pending;
|
|
184
|
+
pending = "";
|
|
185
|
+
fetch(API + "/term", {
|
|
186
|
+
method: "POST",
|
|
187
|
+
headers: { "content-type": "application/json" },
|
|
188
|
+
body: JSON.stringify({ action: "input", id: active.id, data: data }),
|
|
189
|
+
}).catch(function () {});
|
|
190
|
+
};
|
|
191
|
+
dataSub = terminal.onData(function (data) {
|
|
192
|
+
if (!active.id) return;
|
|
193
|
+
pending += data;
|
|
194
|
+
if (inputTimer) return;
|
|
195
|
+
inputTimer = setTimeout(function () { inputTimer = null; flush(); }, 12);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
var tick = function () {
|
|
199
|
+
if (stopped) return;
|
|
200
|
+
fetch(API + "/term-poll?id=" + encodeURIComponent(active.id) + "&since=" + active.since)
|
|
201
|
+
.then(function (response) { return response.json(); })
|
|
202
|
+
.then(function (payload) {
|
|
203
|
+
if (stopped) return;
|
|
204
|
+
if (payload.gone) { active.id = null; return; }
|
|
205
|
+
if (payload.data) {
|
|
206
|
+
terminal.write(payload.data);
|
|
207
|
+
active.since = payload.since;
|
|
208
|
+
active.log = (active.log + payload.data).slice(-TERM_LOG_LIMIT);
|
|
209
|
+
}
|
|
210
|
+
if (payload.exited) { active.exited = true; return; }
|
|
211
|
+
timer = setTimeout(tick, 120);
|
|
212
|
+
})
|
|
213
|
+
.catch(function () { if (!stopped) timer = setTimeout(tick, 800); });
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
if (active.id) {
|
|
217
|
+
tick();
|
|
218
|
+
} else {
|
|
219
|
+
var grid = termGrid(host, terminal);
|
|
220
|
+
fetch(API + "/term", {
|
|
221
|
+
method: "POST",
|
|
222
|
+
headers: { "content-type": "application/json" },
|
|
223
|
+
body: JSON.stringify({ action: "open", cwd: active.cwd, cols: grid.cols, rows: grid.rows }),
|
|
224
|
+
}).then(function (response) { return response.json(); }).then(function (payload) {
|
|
225
|
+
if (stopped) {
|
|
226
|
+
if (payload && payload.id) fetch(API + "/term", { method: "POST", body: JSON.stringify({ action: "close", id: payload.id }) }).catch(function () {});
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (!payload || !payload.id) return;
|
|
230
|
+
active.id = payload.id;
|
|
231
|
+
active.pty = !!payload.pty;
|
|
232
|
+
active.since = 0;
|
|
233
|
+
active.log = "";
|
|
234
|
+
active.exited = false;
|
|
235
|
+
tick();
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return function () {
|
|
240
|
+
stopped = true;
|
|
241
|
+
if (timer) { clearTimeout(timer); timer = null; }
|
|
242
|
+
if (inputTimer) { clearTimeout(inputTimer); inputTimer = null; }
|
|
243
|
+
if (dataSub) dataSub.dispose();
|
|
244
|
+
};
|
|
245
|
+
}, [revision]);
|
|
246
|
+
|
|
247
|
+
// a folder opened from the panel gets its own tab
|
|
248
|
+
useEffect(function () {
|
|
249
|
+
if (!props.cwd) return;
|
|
250
|
+
var before = termActiveKey;
|
|
251
|
+
termEnsureTab(props.cwd);
|
|
252
|
+
if (before !== termActiveKey) bump();
|
|
253
|
+
}, [props.cwd]);
|
|
254
|
+
|
|
255
|
+
// refit the active tab when the dock is dragged, and tell the shell
|
|
256
|
+
useEffect(function () {
|
|
257
|
+
var terminal = termRef.current;
|
|
258
|
+
var host = hostRef.current;
|
|
259
|
+
if (!terminal || !host || !active) return;
|
|
260
|
+
var grid = termGrid(host, terminal);
|
|
261
|
+
terminal.resize(grid.cols, grid.rows);
|
|
262
|
+
termCenter(host, terminal);
|
|
263
|
+
if (active.id) {
|
|
264
|
+
fetch(API + "/term", {
|
|
265
|
+
method: "POST",
|
|
266
|
+
headers: { "content-type": "application/json" },
|
|
267
|
+
body: JSON.stringify({ action: "resize", id: active.id, cols: grid.cols, rows: grid.rows }),
|
|
268
|
+
}).catch(function () {});
|
|
269
|
+
}
|
|
270
|
+
}, [height]);
|
|
271
|
+
|
|
272
|
+
var palette = termTheme();
|
|
273
|
+
return h("div", { className: "dfx-term", style: { height: height + "px", background: palette.background, color: palette.foreground } },
|
|
274
|
+
h("div", { className: "dfx-term-grip", onPointerDown: props.onGrip, title: t("拖动调整高度") }),
|
|
275
|
+
h("div", { className: "dfx-term-tabs" },
|
|
276
|
+
termTabs.map(function (tab) {
|
|
277
|
+
return h("div", {
|
|
278
|
+
key: tab.key,
|
|
279
|
+
className: "dfx-term-tab" + (tab.key === termActiveKey ? " dfx-term-tab--active" : ""),
|
|
280
|
+
title: tab.cwd,
|
|
281
|
+
onClick: function () { if (termActiveKey !== tab.key) { termActiveKey = tab.key; bump(); } },
|
|
282
|
+
},
|
|
283
|
+
glyph("terminal", 13),
|
|
284
|
+
tab.key === editingKey
|
|
285
|
+
? h("input", {
|
|
286
|
+
className: "dfx-term-tabinput",
|
|
287
|
+
value: draftName,
|
|
288
|
+
autoFocus: true,
|
|
289
|
+
spellCheck: false,
|
|
290
|
+
onClick: function (event) { event.stopPropagation(); },
|
|
291
|
+
onChange: function (event) { setDraftName(event.target.value); },
|
|
292
|
+
onBlur: function () { commitTabName(tab); },
|
|
293
|
+
onKeyDown: function (event) {
|
|
294
|
+
if (event.key === "Enter") { event.preventDefault(); commitTabName(tab); }
|
|
295
|
+
else if (event.key === "Escape") { event.preventDefault(); setEditingKey(null); }
|
|
296
|
+
},
|
|
297
|
+
})
|
|
298
|
+
: h("span", {
|
|
299
|
+
className: "dfx-term-tabname",
|
|
300
|
+
title: t("双击重命名"),
|
|
301
|
+
onDoubleClick: function (event) {
|
|
302
|
+
event.stopPropagation();
|
|
303
|
+
setDraftName(tab.name || termTabName(tab.cwd));
|
|
304
|
+
setEditingKey(tab.key);
|
|
305
|
+
},
|
|
306
|
+
}, tab.name || termTabName(tab.cwd)),
|
|
307
|
+
h("button", {
|
|
308
|
+
type: "button",
|
|
309
|
+
className: "dfx-term-tabclose",
|
|
310
|
+
title: t("关闭终端"),
|
|
311
|
+
"aria-label": t("关闭终端"),
|
|
312
|
+
onClick: function (event) {
|
|
313
|
+
event.stopPropagation();
|
|
314
|
+
termCloseTab(tab);
|
|
315
|
+
if (termTabs.length === 0) { props.onClose(); return; }
|
|
316
|
+
bump();
|
|
317
|
+
},
|
|
318
|
+
}, glyph("close", 12)));
|
|
319
|
+
}),
|
|
320
|
+
h("button", {
|
|
321
|
+
type: "button",
|
|
322
|
+
className: "dfx-term-new",
|
|
323
|
+
title: t("新建终端"),
|
|
324
|
+
"aria-label": t("新建终端"),
|
|
325
|
+
onClick: function () { termNewTab(active ? active.cwd : props.cwd); bump(); },
|
|
326
|
+
}, glyph("plus", 14)),
|
|
327
|
+
h("span", { className: "dfx-term-spacer" }),
|
|
328
|
+
h("button", {
|
|
329
|
+
type: "button",
|
|
330
|
+
className: "dfx-term-hide",
|
|
331
|
+
title: t("隐藏终端"),
|
|
332
|
+
"aria-label": t("隐藏终端"),
|
|
333
|
+
onClick: props.onClose,
|
|
334
|
+
}, glyph("chevronDown", 15))),
|
|
335
|
+
h("div", { className: "dfx-term-body", ref: hostRef }));
|
|
336
|
+
}
|
|
337
|
+
|
|
17
338
|
/** Identity shared by the sidebar entry and the main panel it opens. */
|
|
18
339
|
var PANEL_ID = "dsh-file-explorer";
|
|
19
340
|
/** Host route prefix served by lib/index.js. */
|
|
@@ -21,7 +342,59 @@ window.__ModuleLoader__.load({
|
|
|
21
342
|
var STYLE_ID = "dsh-file-explorer-style";
|
|
22
343
|
|
|
23
344
|
/** Remembered navigation, clipboard and view so switching panels does not lose them. */
|
|
24
|
-
var remembered = { path: null, clipboard: null, view: null };
|
|
345
|
+
var remembered = { path: null, clipboard: null, view: null, termOpen: false, termCwd: null };
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Terminal tabs, kept outside the panel so switching to a conversation and
|
|
349
|
+
* back reattaches the same shells instead of starting new ones.
|
|
350
|
+
*/
|
|
351
|
+
var termTabs = [];
|
|
352
|
+
var termActiveKey = null;
|
|
353
|
+
var termTabSeq = 0;
|
|
354
|
+
var TERM_LOG_LIMIT = 200 * 1024;
|
|
355
|
+
|
|
356
|
+
/** The folder name a tab shows. */
|
|
357
|
+
function termTabName(path) {
|
|
358
|
+
var clean = String(path || "").replace(/[\\/]+$/, "");
|
|
359
|
+
var parts = clean.split(/[\\/]/);
|
|
360
|
+
return parts[parts.length - 1] || clean || "终端";
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** The tab for a folder, appended (and activated) when it is missing. */
|
|
364
|
+
function termEnsureTab(cwd) {
|
|
365
|
+
for (var i = 0; i < termTabs.length; i += 1) {
|
|
366
|
+
if (termTabs[i].cwd === cwd) { termActiveKey = termTabs[i].key; return termTabs[i]; }
|
|
367
|
+
}
|
|
368
|
+
var tab = { key: "tt" + (termTabSeq += 1), id: null, cwd: cwd, name: null, since: 0, log: "", pty: false, exited: false };
|
|
369
|
+
termTabs.push(tab);
|
|
370
|
+
termActiveKey = tab.key;
|
|
371
|
+
return tab;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Append a fresh tab in a folder, even when one is already open there. */
|
|
375
|
+
function termNewTab(cwd) {
|
|
376
|
+
var tab = { key: "tt" + (termTabSeq += 1), id: null, cwd: cwd, name: null, since: 0, log: "", pty: false, exited: false };
|
|
377
|
+
termTabs.push(tab);
|
|
378
|
+
termActiveKey = tab.key;
|
|
379
|
+
return tab;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** Close one tab and its shell. */
|
|
383
|
+
function termCloseTab(tab) {
|
|
384
|
+
if (tab.id) {
|
|
385
|
+
fetch(API + "/term", {
|
|
386
|
+
method: "POST",
|
|
387
|
+
headers: { "content-type": "application/json" },
|
|
388
|
+
body: JSON.stringify({ action: "close", id: tab.id }),
|
|
389
|
+
}).catch(function () {});
|
|
390
|
+
}
|
|
391
|
+
var index = termTabs.indexOf(tab);
|
|
392
|
+
if (index >= 0) termTabs.splice(index, 1);
|
|
393
|
+
if (termActiveKey === tab.key) {
|
|
394
|
+
var next = termTabs[Math.min(index, termTabs.length - 1)];
|
|
395
|
+
termActiveKey = next ? next.key : null;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
25
398
|
|
|
26
399
|
/** DSH workspace service (from ctx.workspaces), filled in by apply(). */
|
|
27
400
|
var workspacesRef = { current: null };
|
|
@@ -180,6 +553,13 @@ window.__ModuleLoader__.load({
|
|
|
180
553
|
"引用到输入框": "Mention in the composer",
|
|
181
554
|
"[dsh-file-explorer] 引用到输入框失败:": "Could not reference the file in the composer:",
|
|
182
555
|
"添加为工作区": "Add as workspace",
|
|
556
|
+
"终端": "Terminal",
|
|
557
|
+
"新建终端": "New terminal",
|
|
558
|
+
"隐藏终端": "Hide terminal",
|
|
559
|
+
"双击重命名": "Double-click to rename",
|
|
560
|
+
"在终端打开": "Open in terminal",
|
|
561
|
+
"关闭终端": "Close terminal",
|
|
562
|
+
"在终端中打开当前目录": "Open this folder in a terminal",
|
|
183
563
|
"粘贴": "Paste",
|
|
184
564
|
"在访达中显示当前文件夹": "Reveal this folder in Finder",
|
|
185
565
|
"刷新": "Refresh",
|
|
@@ -645,6 +1025,64 @@ window.__ModuleLoader__.load({
|
|
|
645
1025
|
.dfx-tool:disabled { opacity: .38; cursor: default; }
|
|
646
1026
|
.dfx-tool--open { background: rgba(127, 127, 127, .16); color: var(--dsw-alias-label-primary, #1b1b1b); }
|
|
647
1027
|
.dfx-tool-label { line-height: 1; }
|
|
1028
|
+
/* ── terminal dock ─────────────────────────────────────────────────── */
|
|
1029
|
+
.dfx-term {
|
|
1030
|
+
position: relative; display: flex; flex-direction: column; flex: 0 0 auto;
|
|
1031
|
+
border-top: 1px solid var(--dfx-rule, rgba(127, 127, 127, .13));
|
|
1032
|
+
background: var(--dsw-alias-bg-layer-1, #fff);
|
|
1033
|
+
}
|
|
1034
|
+
.dfx-term-grip {
|
|
1035
|
+
position: absolute; top: -4px; left: 0; right: 0; height: 8px; cursor: row-resize;
|
|
1036
|
+
}
|
|
1037
|
+
.dfx-term-grip::after {
|
|
1038
|
+
content: ""; position: absolute; left: 50%; top: 2px; width: 34px; height: 3px; margin-left: -17px;
|
|
1039
|
+
border-radius: 999px; background: var(--dsw-alias-border-l2, rgba(0, 0, 0, .16));
|
|
1040
|
+
}
|
|
1041
|
+
.dfx-term-tabs {
|
|
1042
|
+
display: flex; align-items: center; gap: 6px; height: 40px; padding: 0 10px; flex: 0 0 auto;
|
|
1043
|
+
border-bottom: 1px solid var(--dfx-rule, rgba(127, 127, 127, .13));
|
|
1044
|
+
background: rgba(127, 127, 127, .04);
|
|
1045
|
+
}
|
|
1046
|
+
.dfx-term-tab {
|
|
1047
|
+
display: flex; align-items: center; gap: 8px; height: 30px; min-width: 150px; max-width: 320px; padding: 0 5px 0 12px;
|
|
1048
|
+
border-radius: 8px; border: 1px solid transparent;
|
|
1049
|
+
font-size: 12px; color: inherit; opacity: .55; cursor: pointer; user-select: none;
|
|
1050
|
+
}
|
|
1051
|
+
.dfx-term-tab:hover { opacity: .8; background: rgba(127, 127, 127, .08); }
|
|
1052
|
+
.dfx-term-tab--active { opacity: .95; background: var(--dsw-alias-bg-layer-1, #fff); border-color: var(--dfx-rule, rgba(127, 127, 127, .18)); }
|
|
1053
|
+
.dfx-term-tabname { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
1054
|
+
.dfx-term-tabinput {
|
|
1055
|
+
width: 132px; height: 21px; padding: 0 5px; border-radius: 5px; outline: none;
|
|
1056
|
+
border: 1px solid var(--dfx-brand, #3b6cf6); background: transparent; color: inherit;
|
|
1057
|
+
font: inherit; font-size: 12px;
|
|
1058
|
+
}
|
|
1059
|
+
.dfx-term-tabclose {
|
|
1060
|
+
width: 18px; height: 18px; padding: 0; display: inline-flex; align-items: center; justify-content: center;
|
|
1061
|
+
border: 0; border-radius: 4px; background: transparent; color: inherit; opacity: .45; cursor: pointer;
|
|
1062
|
+
}
|
|
1063
|
+
.dfx-term-tabclose:hover { opacity: .9; background: rgba(127, 127, 127, .2); }
|
|
1064
|
+
.dfx-term-tab--active .dfx-term-tabclose { opacity: .6; }
|
|
1065
|
+
.dfx-term-hide {
|
|
1066
|
+
width: 26px; height: 26px; padding: 0; display: inline-flex; align-items: center; justify-content: center;
|
|
1067
|
+
border: 0; border-radius: 6px; background: transparent; color: inherit; opacity: .55; cursor: pointer;
|
|
1068
|
+
}
|
|
1069
|
+
.dfx-term-hide:hover { opacity: .9; background: rgba(127, 127, 127, .14); }
|
|
1070
|
+
.dfx-term-new {
|
|
1071
|
+
width: 26px; height: 26px; padding: 0; display: inline-flex; align-items: center; justify-content: center;
|
|
1072
|
+
border: 0; border-radius: 6px; background: transparent; color: inherit; opacity: .55; cursor: pointer;
|
|
1073
|
+
}
|
|
1074
|
+
.dfx-term-new:hover { opacity: .9; background: rgba(127, 127, 127, .14); }
|
|
1075
|
+
.dfx-term-spacer { flex: 1 1 auto; }
|
|
1076
|
+
.dfx-term-body { flex: 1 1 auto; min-height: 0; padding: 11px 10px 12px; overflow: hidden; }
|
|
1077
|
+
.dfx-term-body .xterm { height: 100%; }
|
|
1078
|
+
.dfx-term .dfx-navbtn { color: inherit; opacity: .7; }
|
|
1079
|
+
.dfx-term .dfx-navbtn:hover { opacity: 1; }
|
|
1080
|
+
.dfx-term .dfx-navbtn--active { opacity: 1; }
|
|
1081
|
+
.dfx-term .xterm-viewport {
|
|
1082
|
+
background: transparent !important;
|
|
1083
|
+
overflow-y: auto !important;
|
|
1084
|
+
}
|
|
1085
|
+
.dfx-term .xterm-scrollbar { display: none !important; }
|
|
648
1086
|
.dfx-root svg { display: block; flex: 0 0 auto; }
|
|
649
1087
|
.dfx-rows--in, .dfx-head--in, .dfx-opt-sample--in { animation: dfx-nav-in 340ms cubic-bezier(.25, .8, .25, 1) both; }
|
|
650
1088
|
.dfx-rows--out, .dfx-head--out, .dfx-opt-sample--out { animation: dfx-nav-out 340ms cubic-bezier(.25, .8, .25, 1) both; }
|
|
@@ -1592,6 +2030,8 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
1592
2030
|
infoBorn: ["M6.2 5.8h11.6a1.8 1.8 0 0 1 1.8 1.8v10.6a1.8 1.8 0 0 1-1.8 1.8H6.2a1.8 1.8 0 0 1-1.8-1.8V7.6a1.8 1.8 0 0 1 1.8-1.8z", "M4.4 10.2h15.2", "M8.2 3.8v3.6", "M15.8 3.8v3.6"],
|
|
1593
2031
|
infoTime: ["M12 19.5a7.5 7.5 0 1 0 0-15 7.5 7.5 0 0 0 0 15z", "M12 8.2V12l2.9 1.8"],
|
|
1594
2032
|
infoAcl: ["M5.2 10.6h13.6v9.2H5.2z", "M8.4 10.6V8.2a3.6 3.6 0 0 1 7.2 0v2.4"],
|
|
2033
|
+
// A prompt chevron beside a cursor: the terminal.
|
|
2034
|
+
terminal: ["M4.6 6.4l5.2 5.6-5.2 5.6", "M12.4 17.6h7"],
|
|
1595
2035
|
viewInvert: ["M6.4 9.4h10.2l-2.9-2.9", "M17.6 14.6H7.4l2.9 2.9"],
|
|
1596
2036
|
viewGrid: ["M4 4.5h6v6H4z", "M14 4.5h6v6h-6z", "M4 13.5h6v6H4z", "M14 13.5h6v6h-6z"],
|
|
1597
2037
|
info: ["M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18z", "M12 11v5.5", "M12 7.6h.01"],
|
|
@@ -2713,6 +3153,20 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
2713
3153
|
});
|
|
2714
3154
|
}
|
|
2715
3155
|
|
|
3156
|
+
/** Whether two listings show the same entries (name, kind, size, mtime). */
|
|
3157
|
+
function sameEntries(left, right) {
|
|
3158
|
+
var a = left || [];
|
|
3159
|
+
var b = right || [];
|
|
3160
|
+
if (a.length !== b.length) return false;
|
|
3161
|
+
for (var i = 0; i < a.length; i += 1) {
|
|
3162
|
+
if (a[i].name !== b[i].name) return false;
|
|
3163
|
+
if (!!a[i].dir !== !!b[i].dir) return false;
|
|
3164
|
+
if (a[i].size !== b[i].size) return false;
|
|
3165
|
+
if (a[i].mtime !== b[i].mtime) return false;
|
|
3166
|
+
}
|
|
3167
|
+
return true;
|
|
3168
|
+
}
|
|
3169
|
+
|
|
2716
3170
|
function FileExplorerPanel() {
|
|
2717
3171
|
// Resolve the language before anything renders, so every t() call in this
|
|
2718
3172
|
// pass uses it.
|
|
@@ -2770,6 +3224,8 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
2770
3224
|
var [dropHot, setDropHot] = useState(false);
|
|
2771
3225
|
// The right pane starts hidden; the toolbar toggle or 选项 brings it back.
|
|
2772
3226
|
var [paneState, setPaneState] = useState(readStoredPane);
|
|
3227
|
+
/** The entry the right pane is showing; right-click does not replace it. */
|
|
3228
|
+
var [paneEntry, setPaneEntry] = useState(null);
|
|
2773
3229
|
var paneVisible = paneState.visible;
|
|
2774
3230
|
var paneMode = paneState.mode;
|
|
2775
3231
|
/**
|
|
@@ -2783,7 +3239,18 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
2783
3239
|
storePane(next.visible, next.mode);
|
|
2784
3240
|
setPaneState(next);
|
|
2785
3241
|
}
|
|
2786
|
-
function setPaneVisible(next) {
|
|
3242
|
+
function setPaneVisible(next) {
|
|
3243
|
+
setPaneBoth(next, paneMode);
|
|
3244
|
+
if (!next) return;
|
|
3245
|
+
var entry = selected || (rows && rows[0]) || null;
|
|
3246
|
+
if (!entry) return;
|
|
3247
|
+
if (!selected) {
|
|
3248
|
+
setSelected(entry);
|
|
3249
|
+
setSelectedPaths([entry.path]);
|
|
3250
|
+
setAnchorPath(entry.path);
|
|
3251
|
+
}
|
|
3252
|
+
fillPane(entry);
|
|
3253
|
+
}
|
|
2787
3254
|
function setPaneMode(next) { setPaneBoth(paneVisible, next); }
|
|
2788
3255
|
var [addressDraft, setAddressDraft] = useState(null);
|
|
2789
3256
|
var addressRef = useRef(null);
|
|
@@ -2807,6 +3274,50 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
2807
3274
|
var [gitMap, setGitMap] = useState(null);
|
|
2808
3275
|
/** True when the browser runs on another machine than the host. */
|
|
2809
3276
|
var [remoteMode, setRemoteMode] = useState(false);
|
|
3277
|
+
var [termOpen, setTermOpen] = useState(function () { return remembered.termOpen === true; });
|
|
3278
|
+
var [termCwd, setTermCwd] = useState(function () { return remembered.termCwd || ""; });
|
|
3279
|
+
var [termHeight, setTermHeight] = useState(function () {
|
|
3280
|
+
var saved = null;
|
|
3281
|
+
try { saved = Number(window.localStorage.getItem("dfx.termHeight")); } catch (error) { saved = null; }
|
|
3282
|
+
var maxHeight = Math.max(220, Math.floor((window.innerHeight || 800) / 2));
|
|
3283
|
+
return saved >= 180 ? Math.min(saved, maxHeight) : 260;
|
|
3284
|
+
});
|
|
3285
|
+
|
|
3286
|
+
/** Open the docked terminal at a folder. */
|
|
3287
|
+
function openTerminalAt(target) {
|
|
3288
|
+
var folder = target || (listing ? listing.path : remembered.path);
|
|
3289
|
+
if (!folder) return;
|
|
3290
|
+
remembered.termCwd = folder;
|
|
3291
|
+
remembered.termOpen = true;
|
|
3292
|
+
setTermCwd(folder);
|
|
3293
|
+
setTermOpen(true);
|
|
3294
|
+
}
|
|
3295
|
+
|
|
3296
|
+
/** Hide the dock (its shell is closed by the dock itself). */
|
|
3297
|
+
function hideTerminal() {
|
|
3298
|
+
remembered.termOpen = false;
|
|
3299
|
+
setTermOpen(false);
|
|
3300
|
+
}
|
|
3301
|
+
|
|
3302
|
+
/** Drag the top edge to grow or shrink the dock, capped at half the window. */
|
|
3303
|
+
function startTermDrag(event) {
|
|
3304
|
+
if (!event || event.button !== 0) return;
|
|
3305
|
+
event.preventDefault();
|
|
3306
|
+
var startY = event.clientY;
|
|
3307
|
+
var startHeight = termHeight;
|
|
3308
|
+
var maxHeight = Math.max(220, Math.floor((window.innerHeight || 800) / 2));
|
|
3309
|
+
var move = function (moveEvent) {
|
|
3310
|
+
var next = Math.max(180, Math.min(maxHeight, startHeight - (moveEvent.clientY - startY)));
|
|
3311
|
+
setTermHeight(next);
|
|
3312
|
+
try { window.localStorage.setItem("dfx.termHeight", String(next)); } catch (error) {}
|
|
3313
|
+
};
|
|
3314
|
+
var up = function () {
|
|
3315
|
+
window.removeEventListener("pointermove", move);
|
|
3316
|
+
window.removeEventListener("pointerup", up);
|
|
3317
|
+
};
|
|
3318
|
+
window.addEventListener("pointermove", move);
|
|
3319
|
+
window.addEventListener("pointerup", up);
|
|
3320
|
+
}
|
|
2810
3321
|
/** Editor state when a text file is being edited in the pane. */
|
|
2811
3322
|
var [editor, setEditor] = useState(null);
|
|
2812
3323
|
var [busy, setBusy] = useState(false);
|
|
@@ -2844,17 +3355,24 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
2844
3355
|
setView(next);
|
|
2845
3356
|
}, []);
|
|
2846
3357
|
|
|
2847
|
-
var load = useCallback(function (target, focusPath, direction) {
|
|
3358
|
+
var load = useCallback(function (target, focusPath, direction, silent) {
|
|
2848
3359
|
var seq = requestSeq.current + 1;
|
|
2849
3360
|
requestSeq.current = seq;
|
|
2850
|
-
if (direction) setNavMotion({ dir: direction, seq: navSeqRef.current + 1 });
|
|
3361
|
+
if (direction && !silent) setNavMotion({ dir: direction, seq: navSeqRef.current + 1 });
|
|
2851
3362
|
navSeqRef.current += 1;
|
|
2852
|
-
setStatus("loading");
|
|
3363
|
+
if (!silent) setStatus("loading");
|
|
2853
3364
|
setError(null);
|
|
2854
3365
|
setMenu(null);
|
|
2855
3366
|
apiGet("/list", target ? { path: target } : {}).then(function (result) {
|
|
2856
3367
|
if (requestSeq.current !== seq) return;
|
|
2857
3368
|
if (!result.ok) throw new Error(t((result.body && result.body.message) || "无法读取目录"));
|
|
3369
|
+
if (silent) {
|
|
3370
|
+
var previous = listingRef.current;
|
|
3371
|
+
if (previous && previous.path === result.body.path && sameEntries(previous.entries, result.body.entries)) {
|
|
3372
|
+
setStatus("ready");
|
|
3373
|
+
return;
|
|
3374
|
+
}
|
|
3375
|
+
}
|
|
2858
3376
|
setListing(result.body);
|
|
2859
3377
|
remembered.path = result.body.path;
|
|
2860
3378
|
setStatus("ready");
|
|
@@ -2871,6 +3389,7 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
2871
3389
|
setSelected(focus);
|
|
2872
3390
|
setSelectedPaths(focus ? [focus.path] : []);
|
|
2873
3391
|
setAnchorPath(focus ? focus.path : null);
|
|
3392
|
+
setPaneEntry(focus || null);
|
|
2874
3393
|
if (focus && focus.dir) detailsRef.current(focus);
|
|
2875
3394
|
else if (focus) previewRef.current(focus);
|
|
2876
3395
|
else { setPreview(null); setPreviewStatus("idle"); }
|
|
@@ -3005,12 +3524,14 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
3005
3524
|
/** Open the details pane for one entry and remember the choice. */
|
|
3006
3525
|
var openInfo = useCallback(function (entry) {
|
|
3007
3526
|
if (!entry) return;
|
|
3527
|
+
setPaneEntry(entry);
|
|
3008
3528
|
setPaneBoth(true, "info");
|
|
3009
3529
|
openDetails(entry);
|
|
3010
3530
|
}, [openDetails]);
|
|
3011
3531
|
|
|
3012
3532
|
/** Fill the right pane for an entry according to the current mode. */
|
|
3013
3533
|
var fillPane = useCallback(function (entry) {
|
|
3534
|
+
setPaneEntry(entry || null);
|
|
3014
3535
|
if (!entry) {
|
|
3015
3536
|
setPreview(null);
|
|
3016
3537
|
setPreviewStatus("idle");
|
|
@@ -3067,6 +3588,7 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
3067
3588
|
|
|
3068
3589
|
/** Drop the whole selection. */
|
|
3069
3590
|
function clearSelection() {
|
|
3591
|
+
setPaneEntry(null);
|
|
3070
3592
|
setSelectedPaths([]);
|
|
3071
3593
|
setAnchorPath(null);
|
|
3072
3594
|
setSelected(null);
|
|
@@ -3226,7 +3748,7 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
3226
3748
|
var live = listingRef.current;
|
|
3227
3749
|
if (listingChanged && loadRef.current && live && live.path === current.path) {
|
|
3228
3750
|
var keep = selectionRef.current ? selectionRef.current.path : null;
|
|
3229
|
-
loadRef.current(current.path, keep, "neutral");
|
|
3751
|
+
loadRef.current(current.path, keep, "neutral", true);
|
|
3230
3752
|
}
|
|
3231
3753
|
}, function () { watchRef.current.busy = false; });
|
|
3232
3754
|
}, 1600);
|
|
@@ -3682,6 +4204,7 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
3682
4204
|
if (id === "attach") { attachToSession(entry); return; }
|
|
3683
4205
|
if (id === "quote") { quoteToSession(entry); return; }
|
|
3684
4206
|
if (id === "addWorkspace") { askAddWorkspace(entry); return; }
|
|
4207
|
+
if (id === "openTerm") { openTerminalAt(entry.dir ? entry.path : parentOf(entry.path)); return; }
|
|
3685
4208
|
if (id === "trash") { askDeleteSelection(); return; }
|
|
3686
4209
|
}
|
|
3687
4210
|
|
|
@@ -3934,6 +4457,7 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
3934
4457
|
entry.dir ? null : { id: "quote", label: t("引用到输入框"), icon: "at" },
|
|
3935
4458
|
entry.dir && workspacesRef.current ? { separator: true } : null,
|
|
3936
4459
|
entry.dir && workspacesRef.current ? { id: "addWorkspace", label: t("添加为工作区"), icon: "briefcase" } : null,
|
|
4460
|
+
remoteMode ? null : { id: "openTerm", label: t("在终端打开"), icon: "terminal" },
|
|
3937
4461
|
remoteMode ? null : { separator: true },
|
|
3938
4462
|
remoteMode ? null : { id: "trash", label: t("移到废纸篓"), icon: "trash", danger: true },
|
|
3939
4463
|
];
|
|
@@ -4072,11 +4596,12 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
4072
4596
|
function openRowMenu(event, entry) {
|
|
4073
4597
|
event.preventDefault();
|
|
4074
4598
|
event.stopPropagation();
|
|
4599
|
+
// Right-click only selects and opens the menu: it must not pull the
|
|
4600
|
+
// entry into the preview pane, which stays on whatever it showed.
|
|
4075
4601
|
if (selectedPaths.indexOf(entry.path) === -1) {
|
|
4076
4602
|
setSelected(entry);
|
|
4077
4603
|
setSelectedPaths([entry.path]);
|
|
4078
4604
|
setAnchorPath(entry.path);
|
|
4079
|
-
fillPane(entry);
|
|
4080
4605
|
}
|
|
4081
4606
|
setMenu({ x: event.clientX, y: event.clientY, entry: entry });
|
|
4082
4607
|
if (rowsRef.current && rowsRef.current.focus) rowsRef.current.focus({ preventScroll: true });
|
|
@@ -4820,7 +5345,15 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
4820
5345
|
};
|
|
4821
5346
|
|
|
4822
5347
|
previewPathRef.current = preview && preview.entry && !preview.entry.dir ? preview.entry.path : null;
|
|
4823
|
-
|
|
5348
|
+
// With the pane switched on, show the first entry until the user picks
|
|
5349
|
+
// one: the pane then follows clicks, never a right-click.
|
|
5350
|
+
useEffect(function () {
|
|
5351
|
+
if (!paneVisible || paneEntry) return;
|
|
5352
|
+
if (rows && rows.length > 0) fillPane(rows[0]);
|
|
5353
|
+
}, [paneVisible, paneEntry, rows && rows.length]);
|
|
5354
|
+
|
|
5355
|
+
var paneShown = paneEntry;
|
|
5356
|
+
var showInfoPane = paneVisible && !!paneShown && (paneShown.dir || paneMode === "info");
|
|
4824
5357
|
var previewPane = null;
|
|
4825
5358
|
if (!paneVisible) {
|
|
4826
5359
|
previewPane = null;
|
|
@@ -4834,9 +5367,9 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
4834
5367
|
infoInner = infoRows(details.body);
|
|
4835
5368
|
}
|
|
4836
5369
|
previewPane = h("div", { className: "dfx-preview" },
|
|
4837
|
-
paneHeader(
|
|
5370
|
+
paneHeader(paneShown),
|
|
4838
5371
|
h("div", { className: "dfx-preview-body" }, infoInner),
|
|
4839
|
-
paneFooter(
|
|
5372
|
+
paneFooter(paneShown));
|
|
4840
5373
|
} else if (preview || previewStatus === "loading") {
|
|
4841
5374
|
var meta = null;
|
|
4842
5375
|
var inner = null;
|
|
@@ -4987,12 +5520,12 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
4987
5520
|
inner = h("div", { className: "dfx-empty" }, t("此格式不支持内嵌预览"));
|
|
4988
5521
|
}
|
|
4989
5522
|
}
|
|
4990
|
-
|
|
5523
|
+
|
|
4991
5524
|
previewPane = h("div", { className: "dfx-preview" },
|
|
4992
|
-
paneHeader(
|
|
5525
|
+
paneHeader(paneShown),
|
|
4993
5526
|
meta,
|
|
4994
5527
|
h("div", { className: "dfx-preview-body" }, inner),
|
|
4995
|
-
paneFooter(
|
|
5528
|
+
paneFooter(paneShown));
|
|
4996
5529
|
}
|
|
4997
5530
|
|
|
4998
5531
|
var menuNode = null;
|
|
@@ -5175,6 +5708,7 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
5175
5708
|
h("span", { className: "dfx-dropzone-hint" }, t("文件作为附件,文件夹作为引用")))
|
|
5176
5709
|
: null;
|
|
5177
5710
|
|
|
5711
|
+
|
|
5178
5712
|
return h("div", {
|
|
5179
5713
|
className: "dfx-root",
|
|
5180
5714
|
ref: rootRef,
|
|
@@ -5353,6 +5887,18 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
5353
5887
|
title: t("排序方式"),
|
|
5354
5888
|
onClick: function (event) { openPopup("sort", event); },
|
|
5355
5889
|
}, glyph("sortIcon", 14), h("span", { className: "dfx-tool-label" }, t("排序")), glyph("chevronDown", 12)),
|
|
5890
|
+
remoteMode ? null : h("span", { className: "dfx-toolbar-sep" }),
|
|
5891
|
+
remoteMode ? null : h("button", {
|
|
5892
|
+
type: "button",
|
|
5893
|
+
className: "dfx-tool" + (termOpen ? " dfx-tool--open" : ""),
|
|
5894
|
+
title: t("在终端中打开当前目录"),
|
|
5895
|
+
"aria-pressed": termOpen ? "true" : "false",
|
|
5896
|
+
onClick: function () {
|
|
5897
|
+
if (termOpen) { hideTerminal(); return; }
|
|
5898
|
+
openTerminalAt();
|
|
5899
|
+
},
|
|
5900
|
+
}, glyph("terminal", 14), h("span", { className: "dfx-tool-label" }, t("终端"))),
|
|
5901
|
+
remoteMode ? null : h("span", { className: "dfx-toolbar-sep" }),
|
|
5356
5902
|
h("button", {
|
|
5357
5903
|
type: "button",
|
|
5358
5904
|
className: "dfx-tool" + (popup && popup.kind === "more" ? " dfx-tool--open" : ""),
|
|
@@ -5446,6 +5992,13 @@ html[data-ds-theme-source="dark"] .dfx-json-code { --dsw-json-guide: rgba(255, 2
|
|
|
5446
5992
|
previewPane),
|
|
5447
5993
|
dropStrip,
|
|
5448
5994
|
quickLookNode,
|
|
5995
|
+
termOpen ? h(TerminalDock, {
|
|
5996
|
+
key: "term",
|
|
5997
|
+
cwd: termCwd || (listing ? listing.path : ""),
|
|
5998
|
+
height: termHeight,
|
|
5999
|
+
onClose: hideTerminal,
|
|
6000
|
+
onGrip: startTermDrag,
|
|
6001
|
+
}) : null,
|
|
5449
6002
|
h("div", { className: "dfx-foot" },
|
|
5450
6003
|
h("span", null, tf("{n} 项", { n: rows.length })),
|
|
5451
6004
|
listing && listing.truncated ? h(Fragment, null,
|
package/lib/index.js
CHANGED
|
@@ -24,9 +24,10 @@
|
|
|
24
24
|
* POST /transfer JSON {sources, target, mode: copy|move}
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
|
-
import { execFile } from 'node:child_process'
|
|
27
|
+
import { execFile, spawn } from 'node:child_process'
|
|
28
28
|
import { once } from 'node:events'
|
|
29
|
-
import { createWriteStream } from 'node:fs'
|
|
29
|
+
import { chmodSync, createWriteStream, existsSync } from 'node:fs'
|
|
30
|
+
import { createRequire } from 'node:module'
|
|
30
31
|
import { access, cp, lstat, mkdir, readFile, readdir, realpath, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
31
32
|
import { homedir, platform } from 'node:os'
|
|
32
33
|
import { pathToFileURL } from 'node:url'
|
|
@@ -1029,6 +1030,9 @@ const ROUTES = [
|
|
|
1029
1030
|
{ suffix: '/git', methods: ['GET'] },
|
|
1030
1031
|
{ suffix: '/watch', methods: ['GET'] },
|
|
1031
1032
|
{ suffix: '/search', methods: ['GET'] },
|
|
1033
|
+
{ suffix: '/term', methods: ['POST'] },
|
|
1034
|
+
{ suffix: '/term-poll', methods: ['GET'] },
|
|
1035
|
+
{ suffix: '/vendor', methods: ['GET'] },
|
|
1032
1036
|
]
|
|
1033
1037
|
|
|
1034
1038
|
/**
|
|
@@ -1114,6 +1118,289 @@ async function sendRaw(res, target) {
|
|
|
1114
1118
|
res.end(data)
|
|
1115
1119
|
}
|
|
1116
1120
|
|
|
1121
|
+
/** A browser reaching us over loopback counts as local. */
|
|
1122
|
+
function isLoopback(req) {
|
|
1123
|
+
const hostHeader = String((req.headers && req.headers.host) || "").toLowerCase()
|
|
1124
|
+
const hostName = hostHeader.replace(/:\d+$/, "")
|
|
1125
|
+
return hostName === "127.0.0.1" || hostName === "localhost" || hostName === "[::1]" || hostName === "0.0.0.0" || hostName === ""
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/** Live terminal sessions, keyed by id. */
|
|
1129
|
+
const TERM_SESSIONS = new Map()
|
|
1130
|
+
let termSeq = 0
|
|
1131
|
+
const TERM_KEEP_BYTES = 300 * 1024
|
|
1132
|
+
|
|
1133
|
+
/** The shell to open, honouring $SHELL. */
|
|
1134
|
+
function termShell() {
|
|
1135
|
+
if (process.env.SHELL) return process.env.SHELL
|
|
1136
|
+
if (platform() === "win32") return process.env.COMSPEC || "cmd.exe"
|
|
1137
|
+
return "/bin/bash"
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* Python bridge that runs the shell behind a real pty. macOS script(1) needs
|
|
1142
|
+
* its own stdin to be a terminal, which it is not inside the DSH host, so a
|
|
1143
|
+
* small pty.fork relay is used instead. Resize travels over fd 3.
|
|
1144
|
+
*/
|
|
1145
|
+
const TERM_PY = [
|
|
1146
|
+
"import os, pty, sys, select, fcntl, termios, struct",
|
|
1147
|
+
"shell, rows, cols = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])",
|
|
1148
|
+
"pid, fd = pty.fork()",
|
|
1149
|
+
"if pid == 0:",
|
|
1150
|
+
" os.execvpe(shell, [shell, \"-l\"], os.environ)",
|
|
1151
|
+
"def setsize(r, c):",
|
|
1152
|
+
" try:",
|
|
1153
|
+
" fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack(\"HHHH\", r, c, 0, 0))",
|
|
1154
|
+
" except OSError:",
|
|
1155
|
+
" pass",
|
|
1156
|
+
"setsize(rows, cols)",
|
|
1157
|
+
"while True:",
|
|
1158
|
+
" r, _, _ = select.select([fd, 0, 3], [], [])",
|
|
1159
|
+
" if fd in r:",
|
|
1160
|
+
" try:",
|
|
1161
|
+
" data = os.read(fd, 65536)",
|
|
1162
|
+
" except OSError:",
|
|
1163
|
+
" break",
|
|
1164
|
+
" if not data:",
|
|
1165
|
+
" break",
|
|
1166
|
+
" os.write(1, data)",
|
|
1167
|
+
" if 0 in r:",
|
|
1168
|
+
" data = os.read(0, 65536)",
|
|
1169
|
+
" if not data:",
|
|
1170
|
+
" break",
|
|
1171
|
+
" os.write(fd, data)",
|
|
1172
|
+
" if 3 in r:",
|
|
1173
|
+
" line = os.read(3, 64).decode(\"utf8\", \"ignore\").strip()",
|
|
1174
|
+
" parts = line.split()",
|
|
1175
|
+
" if len(parts) == 2:",
|
|
1176
|
+
" setsize(int(parts[0]), int(parts[1]))",
|
|
1177
|
+
].join(String.fromCharCode(10))
|
|
1178
|
+
|
|
1179
|
+
/** Locate a python3 that can import pty. */
|
|
1180
|
+
function termPython() {
|
|
1181
|
+
return process.env.DSH_PYTHON || "/usr/bin/python3"
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
/** node-pty loaded on first use; null once it is known to be missing. */
|
|
1185
|
+
let termPtyModule = null
|
|
1186
|
+
let termPtyTried = false
|
|
1187
|
+
|
|
1188
|
+
/**
|
|
1189
|
+
* Load the optional pty backend. node-pty ships prebuilt binaries for
|
|
1190
|
+
* macOS and Windows, so nothing is compiled; the macOS spawn helper loses
|
|
1191
|
+
* its exec bit through some package managers, so it is repaired here.
|
|
1192
|
+
* @returns {object|null} the node-pty module, or null when unavailable.
|
|
1193
|
+
*/
|
|
1194
|
+
function loadNodePty() {
|
|
1195
|
+
if (termPtyTried) return termPtyModule
|
|
1196
|
+
termPtyTried = true
|
|
1197
|
+
try {
|
|
1198
|
+
const require = createRequire(import.meta.url)
|
|
1199
|
+
const loaded = require("node-pty")
|
|
1200
|
+
try {
|
|
1201
|
+
const pkg = require.resolve("node-pty/package.json")
|
|
1202
|
+
const root = dirname(pkg)
|
|
1203
|
+
for (const rel of ["prebuilds/" + process.platform + "-" + process.arch + "/spawn-helper", "build/Release/spawn-helper"]) {
|
|
1204
|
+
const helper = join(root, rel)
|
|
1205
|
+
if (existsSync(helper)) {
|
|
1206
|
+
try { chmodSync(helper, 0o755) } catch {}
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
} catch {}
|
|
1210
|
+
termPtyModule = loaded
|
|
1211
|
+
} catch {
|
|
1212
|
+
termPtyModule = null
|
|
1213
|
+
}
|
|
1214
|
+
return termPtyModule
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
/**
|
|
1218
|
+
* Start a shell under node-pty.
|
|
1219
|
+
* @param {string} shell - login shell
|
|
1220
|
+
* @param {number} cols - columns
|
|
1221
|
+
* @param {number} rows - rows
|
|
1222
|
+
* @param {string} cwd - working directory
|
|
1223
|
+
* @returns {object|null} pty handle or null
|
|
1224
|
+
*/
|
|
1225
|
+
function termSpawnNodePty(shell, cols, rows, cwd) {
|
|
1226
|
+
const module = loadNodePty()
|
|
1227
|
+
if (!module) return null
|
|
1228
|
+
const args = platform() === "win32" ? [] : ["-l"]
|
|
1229
|
+
return module.spawn(shell, args, {
|
|
1230
|
+
name: "xterm-256color",
|
|
1231
|
+
cols,
|
|
1232
|
+
rows,
|
|
1233
|
+
cwd,
|
|
1234
|
+
env: { ...process.env, TERM: "xterm-256color", COLUMNS: String(cols), LINES: String(rows) },
|
|
1235
|
+
})
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
/**
|
|
1239
|
+
* The pty bridge when python3 is available, otherwise the plain shell.
|
|
1240
|
+
* @param {string} shell - login shell
|
|
1241
|
+
* @param {number} rows - initial rows
|
|
1242
|
+
* @param {number} cols - initial cols
|
|
1243
|
+
* @returns {object} spawn arguments
|
|
1244
|
+
*/
|
|
1245
|
+
function termCommand(shell, rows, cols) {
|
|
1246
|
+
if (platform() !== "win32") {
|
|
1247
|
+
return { command: termPython(), args: ["-c", TERM_PY, shell, String(rows), String(cols)], pty: true }
|
|
1248
|
+
}
|
|
1249
|
+
return { command: shell, args: [], pty: false }
|
|
1250
|
+
}
|
|
1251
|
+
/** Append output to a session, dropping the oldest bytes past the cap. */
|
|
1252
|
+
function termPush(session, text) {
|
|
1253
|
+
const clean = String(text)
|
|
1254
|
+
if (!clean) return
|
|
1255
|
+
session.chunks.push({ seq: session.nextSeq++, text: clean })
|
|
1256
|
+
session.bytes += clean.length
|
|
1257
|
+
while (session.bytes > TERM_KEEP_BYTES && session.chunks.length > 1) {
|
|
1258
|
+
const dropped = session.chunks.shift()
|
|
1259
|
+
session.bytes -= dropped.text.length
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
/** Close shells nobody has looked at for half an hour. */
|
|
1264
|
+
const TERM_IDLE_MS = 30 * 60 * 1000
|
|
1265
|
+
const TERM_SWEEP = setInterval(() => {
|
|
1266
|
+
const now = Date.now()
|
|
1267
|
+
for (const [id, session] of TERM_SESSIONS) {
|
|
1268
|
+
if (session.exited || now - session.lastSeen > TERM_IDLE_MS) termClose(id)
|
|
1269
|
+
}
|
|
1270
|
+
}, 60 * 1000)
|
|
1271
|
+
if (typeof TERM_SWEEP.unref === "function") TERM_SWEEP.unref()
|
|
1272
|
+
|
|
1273
|
+
/** Close one session and its process group. */
|
|
1274
|
+
function termClose(id) {
|
|
1275
|
+
const session = TERM_SESSIONS.get(id)
|
|
1276
|
+
if (!session) return false
|
|
1277
|
+
try { session.kill() } catch {}
|
|
1278
|
+
TERM_SESSIONS.delete(id)
|
|
1279
|
+
return true
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
/**
|
|
1283
|
+
* Open a shell at a directory and remember its output. node-pty is used when
|
|
1284
|
+
* it loaded (real pty on macOS, Linux and Windows), with the python bridge
|
|
1285
|
+
* and finally plain pipes as fallbacks.
|
|
1286
|
+
*/
|
|
1287
|
+
async function termOpen(body) {
|
|
1288
|
+
const cwd = await safePath(body.cwd)
|
|
1289
|
+
const info = await stat(cwd)
|
|
1290
|
+
if (!info.isDirectory()) throw new RouteError(400, "cwd is not a folder")
|
|
1291
|
+
const cols = Math.max(20, Math.min(400, Number(body.cols) || 80))
|
|
1292
|
+
const rows = Math.max(5, Math.min(200, Number(body.rows) || 24))
|
|
1293
|
+
const shell = termShell()
|
|
1294
|
+
const id = "t" + (termSeq += 1) + "-" + Date.now().toString(36)
|
|
1295
|
+
const session = {
|
|
1296
|
+
id, chunks: [], nextSeq: 0, bytes: 0, exited: false, code: null,
|
|
1297
|
+
cwd, pty: false, engine: "pipe", cols, rows, lastSeen: Date.now(),
|
|
1298
|
+
write: () => {}, resize: () => {}, kill: () => {},
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
let handle = null
|
|
1302
|
+
try { handle = termSpawnNodePty(shell, cols, rows, cwd) } catch { handle = null }
|
|
1303
|
+
if (handle) {
|
|
1304
|
+
session.engine = "node-pty"
|
|
1305
|
+
session.pty = true
|
|
1306
|
+
handle.onData((data) => termPush(session, data))
|
|
1307
|
+
handle.onExit(({ exitCode }) => {
|
|
1308
|
+
session.exited = true
|
|
1309
|
+
session.code = exitCode
|
|
1310
|
+
termPush(session, "\r\n[进程已退出 " + (exitCode === null ? "" : exitCode) + "]\r\n")
|
|
1311
|
+
})
|
|
1312
|
+
session.write = (data) => { try { handle.write(data) } catch {} }
|
|
1313
|
+
session.resize = (c, r) => { try { handle.resize(c, r) } catch {} }
|
|
1314
|
+
session.kill = () => { try { handle.kill() } catch {} }
|
|
1315
|
+
} else {
|
|
1316
|
+
const spec = termCommand(shell, rows, cols)
|
|
1317
|
+
const proc = spawn(spec.command, spec.args, {
|
|
1318
|
+
cwd,
|
|
1319
|
+
env: { ...process.env, TERM: "xterm-256color", COLUMNS: String(cols), LINES: String(rows) },
|
|
1320
|
+
stdio: ["pipe", "pipe", "pipe", "pipe"],
|
|
1321
|
+
})
|
|
1322
|
+
session.pty = spec.pty
|
|
1323
|
+
session.engine = spec.pty ? "python" : "pipe"
|
|
1324
|
+
const control = spec.pty ? proc.stdio[3] : null
|
|
1325
|
+
proc.stdout.setEncoding("utf8")
|
|
1326
|
+
proc.stderr.setEncoding("utf8")
|
|
1327
|
+
proc.stdout.on("data", (chunk) => termPush(session, chunk))
|
|
1328
|
+
proc.stderr.on("data", (chunk) => termPush(session, chunk))
|
|
1329
|
+
proc.on("exit", (code) => {
|
|
1330
|
+
session.exited = true
|
|
1331
|
+
session.code = code
|
|
1332
|
+
termPush(session, "\r\n[进程已退出 " + (code === null ? "" : code) + "]\r\n")
|
|
1333
|
+
})
|
|
1334
|
+
proc.on("error", (error) => termPush(session, "\r\n[无法启动终端: " + (error && error.message) + "]\r\n"))
|
|
1335
|
+
session.write = (data) => { try { proc.stdin.write(data) } catch {} }
|
|
1336
|
+
session.resize = (c, r) => {
|
|
1337
|
+
if (control) { try { control.write(String(r) + " " + String(c) + "\n") } catch {} }
|
|
1338
|
+
}
|
|
1339
|
+
session.kill = () => {
|
|
1340
|
+
try { proc.kill("SIGHUP") } catch {}
|
|
1341
|
+
try { proc.kill("SIGKILL") } catch {}
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
TERM_SESSIONS.set(session.id, session)
|
|
1346
|
+
if (TERM_SESSIONS.size > 3) {
|
|
1347
|
+
const oldest = TERM_SESSIONS.keys().next().value
|
|
1348
|
+
if (oldest !== session.id) termClose(oldest)
|
|
1349
|
+
}
|
|
1350
|
+
return { id: session.id, pty: session.pty, engine: session.engine, shell, cwd }
|
|
1351
|
+
}
|
|
1352
|
+
/** One POST on /term: open, input, resize or close. */
|
|
1353
|
+
async function handleTerm(req) {
|
|
1354
|
+
const raw = await readBody(req)
|
|
1355
|
+
let body = {}
|
|
1356
|
+
try { body = raw.length > 0 ? JSON.parse(raw.toString("utf8")) : {} } catch { throw new RouteError(400, "malformed JSON") }
|
|
1357
|
+
const action = String(body.action || "")
|
|
1358
|
+
if (action === "open") return termOpen(body)
|
|
1359
|
+
const session = TERM_SESSIONS.get(String(body.id || ""))
|
|
1360
|
+
if (!session) throw new RouteError(404, "terminal is gone")
|
|
1361
|
+
if (action === "input") {
|
|
1362
|
+
session.write(String(body.data || ""))
|
|
1363
|
+
return { ok: true }
|
|
1364
|
+
}
|
|
1365
|
+
if (action === "resize") {
|
|
1366
|
+
session.cols = Number(body.cols) || null
|
|
1367
|
+
session.rows = Number(body.rows) || null
|
|
1368
|
+
session.resize(session.cols || 80, session.rows || 24)
|
|
1369
|
+
return { ok: true, pty: session.pty }
|
|
1370
|
+
}
|
|
1371
|
+
if (action === "close") return { ok: termClose(session.id) }
|
|
1372
|
+
throw new RouteError(400, "unknown action")
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
/** Output that arrived after the given sequence number. */
|
|
1376
|
+
function pollTerm(id, since) {
|
|
1377
|
+
const session = TERM_SESSIONS.get(id)
|
|
1378
|
+
if (!session) return { gone: true, data: "", since, exited: true }
|
|
1379
|
+
session.lastSeen = Date.now()
|
|
1380
|
+
const parts = []
|
|
1381
|
+
for (const chunk of session.chunks) {
|
|
1382
|
+
if (chunk.seq >= since) parts.push(chunk.text)
|
|
1383
|
+
}
|
|
1384
|
+
return { gone: false, data: parts.join(""), since: session.nextSeq, exited: session.exited, code: session.code }
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
/** Serve the terminal renderer from our own dependency. */
|
|
1388
|
+
async function sendVendor(res, name) {
|
|
1389
|
+
const allowed = { "xterm.js": "lib/xterm.js", "xterm.css": "css/xterm.css" }
|
|
1390
|
+
const rel = allowed[name]
|
|
1391
|
+
if (!rel) throw new RouteError(404, "unknown asset")
|
|
1392
|
+
const file = new URL("../node_modules/@xterm/xterm/" + rel, import.meta.url)
|
|
1393
|
+
let data
|
|
1394
|
+
try {
|
|
1395
|
+
data = await readFile(file)
|
|
1396
|
+
} catch {
|
|
1397
|
+
throw new RouteError(404, "xterm is not installed")
|
|
1398
|
+
}
|
|
1399
|
+
const type = name.endsWith(".css") ? "text/css; charset=utf-8" : "text/javascript; charset=utf-8"
|
|
1400
|
+
res.writeHead(200, { "Content-Type": type, "Content-Length": data.length, "Cache-Control": "no-store" })
|
|
1401
|
+
res.end(data)
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1117
1404
|
function mimeOf(target) {
|
|
1118
1405
|
const ext = extname(target).toLowerCase()
|
|
1119
1406
|
const table = {
|
|
@@ -1285,6 +1572,20 @@ async function handleRequest(req, res) {
|
|
|
1285
1572
|
))
|
|
1286
1573
|
return
|
|
1287
1574
|
}
|
|
1575
|
+
if (route === '/term' && method === 'POST') {
|
|
1576
|
+
if (!isLoopback(req)) throw new RouteError(403, 'terminal is desktop-only')
|
|
1577
|
+
send(res, 200, await handleTerm(req))
|
|
1578
|
+
return
|
|
1579
|
+
}
|
|
1580
|
+
if (route === '/term-poll' && method === 'GET') {
|
|
1581
|
+
if (!isLoopback(req)) throw new RouteError(403, 'terminal is desktop-only')
|
|
1582
|
+
send(res, 200, pollTerm(url.searchParams.get('id') || '', Number(url.searchParams.get('since') || 0)))
|
|
1583
|
+
return
|
|
1584
|
+
}
|
|
1585
|
+
if (route === '/vendor' && method === 'GET') {
|
|
1586
|
+
await sendVendor(res, url.searchParams.get('name') || '')
|
|
1587
|
+
return
|
|
1588
|
+
}
|
|
1288
1589
|
if (route === '/download' && method === 'GET') {
|
|
1289
1590
|
await sendAttachment(res, await safePath(path))
|
|
1290
1591
|
return
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maxbill/dsh-file-explorer",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "文件管理器 — 浏览、预览并直接用起来:文件夹一键变成工作区,文档一键加进当前对话 · A file manager for DeepSeek Harness that turns folders into workspaces and documents into conversation attachments",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -63,5 +63,11 @@
|
|
|
63
63
|
"client": {
|
|
64
64
|
"platform": "web"
|
|
65
65
|
}
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"@xterm/xterm": "^5.5.0"
|
|
69
|
+
},
|
|
70
|
+
"optionalDependencies": {
|
|
71
|
+
"node-pty": "^1.1.0"
|
|
66
72
|
}
|
|
67
73
|
}
|