@celljs/ta-workspace 4.6.1 → 4.8.0
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.md +5 -5
- package/README.zh-cn.md +29 -0
- package/package.json +3 -3
- package/web/index.js +1955 -637
- package/web-src/browser-agent.ts +440 -0
- package/web-src/browser-common.tsx +278 -0
- package/web-src/browser-import.tsx +126 -0
- package/web-src/browser-webview.tsx +270 -0
- package/web-src/browser.tsx +37 -197
- package/web-src/explorer.tsx +241 -3
- package/web-src/i18n.ts +77 -2
- package/web-src/index.tsx +7 -1
- package/web-src/style.ts +35 -6
- package/web-src/terminal.tsx +53 -134
- package/web-src/util/browser-snapshot.spec.ts +96 -0
- package/web-src/util/browser-snapshot.ts +225 -0
- package/web-src/util/upload.spec.ts +66 -0
- package/web-src/util/upload.ts +123 -0
- package/web-src/util/workspace.spec.ts +42 -13
- package/web-src/util/workspace.ts +65 -0
- package/web-src/util/zip.spec.ts +60 -0
- package/web-src/util/zip.ts +137 -0
- package/web-src/workspace-client.ts +103 -1
- package/web-src/util/ansi.spec.ts +0 -66
- package/web-src/util/ansi.ts +0 -438
- package/web-src/util/keys.ts +0 -56
package/web-src/i18n.ts
CHANGED
|
@@ -11,6 +11,22 @@ const ZH = {
|
|
|
11
11
|
copied: '已复制',
|
|
12
12
|
findInFolder: '在文件夹中查找…',
|
|
13
13
|
revealInExplorer: '在资源管理器中显示',
|
|
14
|
+
revealInFinder: '在 Finder 中显示',
|
|
15
|
+
revealInFileExplorer: '在文件资源管理器中显示',
|
|
16
|
+
openContainingFolder: '打开所在的文件夹',
|
|
17
|
+
revealFailed: '无法在文件管理器中显示:{message}',
|
|
18
|
+
uploadFiles: '上传文件…',
|
|
19
|
+
uploadFolder: '上传文件夹…',
|
|
20
|
+
download: '下载',
|
|
21
|
+
uploading: '正在上传 {name}',
|
|
22
|
+
uploaded: '已上传 {count} 个文件',
|
|
23
|
+
uploadFailed: '上传失败:{message}',
|
|
24
|
+
replaceTitle: '已存在 {names},要替换吗?',
|
|
25
|
+
replaceMessage: '同名的文件会被上传的文件覆盖。',
|
|
26
|
+
packing: '正在打包 {name}',
|
|
27
|
+
downloadFailed: '下载失败:{message}',
|
|
28
|
+
moveExists: '目标文件夹中已有“{name}”。',
|
|
29
|
+
moveFailed: '移动失败:{message}',
|
|
14
30
|
nameEmpty: '必须提供文件或文件夹名称。',
|
|
15
31
|
nameSlash: '文件或文件夹名称不能以斜杠开头。',
|
|
16
32
|
nameInvalid: '名称 {name} 不能用作文件或文件夹名称,请换一个。',
|
|
@@ -131,7 +147,28 @@ const ZH = {
|
|
|
131
147
|
browserHint: '输入网址或搜索,也可以打开本机的开发服务器。',
|
|
132
148
|
home: '起始页',
|
|
133
149
|
removeRecent: '从最近访问中移除',
|
|
134
|
-
frameHint: '
|
|
150
|
+
frameHint: '有的网站不允许被嵌入,页面空白时请在新窗口打开;Ta 桌面版没有这个限制。',
|
|
151
|
+
stop: '停止',
|
|
152
|
+
openBrowser: '在默认浏览器中打开',
|
|
153
|
+
loadFailed: '无法打开这个网页',
|
|
154
|
+
pageCrashed: '这个网页崩溃了',
|
|
155
|
+
retry: '重试',
|
|
156
|
+
agentDriving: 'Ta 正在操作',
|
|
157
|
+
importSignIns: '导入浏览器的登录状态',
|
|
158
|
+
importTitle: '导入登录状态',
|
|
159
|
+
importHint: '把所选浏览器的 Cookie 复制到这里,你和 Agent 都能直接使用已登录的网站。不导入密码和浏览记录。',
|
|
160
|
+
importFrom: '从哪个浏览器导入',
|
|
161
|
+
noProfiles: '没有找到 Chrome、Edge、Brave、Arc、Vivaldi 或 Chromium 的个人资料。',
|
|
162
|
+
importNow: '导入…',
|
|
163
|
+
importing: '导入中…',
|
|
164
|
+
imported: '已导入 {count} 个 Cookie',
|
|
165
|
+
importSkipped: '{count} 个无法读取,已跳过。',
|
|
166
|
+
importAppBound: '{count} 个受 Chrome 应用绑定加密保护,只能在网站上重新登录。',
|
|
167
|
+
clearBrowserData: '清除内置浏览器数据',
|
|
168
|
+
clearBrowserDataDetail: '删除内置浏览器的全部 Cookie、网站存储和缓存,网站都需要重新登录。',
|
|
169
|
+
clear: '清除',
|
|
170
|
+
cleared: '已清除内置浏览器数据',
|
|
171
|
+
done: '完成',
|
|
135
172
|
binary: '二进制文件,不显示内容({size})。',
|
|
136
173
|
truncated: '文件较大,只显示前 1 MB,不能编辑。',
|
|
137
174
|
line: '第 {line} 行',
|
|
@@ -163,6 +200,22 @@ const EN: Record<Key, string> = {
|
|
|
163
200
|
copied: 'Copied',
|
|
164
201
|
findInFolder: 'Find in Folder…',
|
|
165
202
|
revealInExplorer: 'Reveal in Explorer',
|
|
203
|
+
revealInFinder: 'Reveal in Finder',
|
|
204
|
+
revealInFileExplorer: 'Reveal in File Explorer',
|
|
205
|
+
openContainingFolder: 'Open Containing Folder',
|
|
206
|
+
revealFailed: 'Could not show it in the file manager: {message}',
|
|
207
|
+
uploadFiles: 'Upload Files…',
|
|
208
|
+
uploadFolder: 'Upload Folder…',
|
|
209
|
+
download: 'Download',
|
|
210
|
+
uploading: 'Uploading {name}',
|
|
211
|
+
uploaded: 'Uploaded {count} file(s)',
|
|
212
|
+
uploadFailed: 'Could not upload: {message}',
|
|
213
|
+
replaceTitle: '{names} already exists. Replace it?',
|
|
214
|
+
replaceMessage: 'Files of the same name are overwritten by the uploaded ones.',
|
|
215
|
+
packing: 'Packing {name}',
|
|
216
|
+
downloadFailed: 'Could not download: {message}',
|
|
217
|
+
moveExists: '{name} already exists in the folder.',
|
|
218
|
+
moveFailed: 'Could not move: {message}',
|
|
166
219
|
nameEmpty: 'A file or folder name must be provided.',
|
|
167
220
|
nameSlash: 'A file or folder name cannot start with a slash.',
|
|
168
221
|
nameInvalid: 'The name {name} is not valid as a file or folder name. Please choose a different name.',
|
|
@@ -283,7 +336,29 @@ const EN: Record<Key, string> = {
|
|
|
283
336
|
browserHint: 'Enter an address or search, or open a dev server on this machine.',
|
|
284
337
|
home: 'Start page',
|
|
285
338
|
removeRecent: 'Remove from recent',
|
|
286
|
-
frameHint: 'Some sites refuse to be embedded; if the page stays blank, open it in a new window.',
|
|
339
|
+
frameHint: 'Some sites refuse to be embedded; if the page stays blank, open it in a new window. Ta Desktop has no such limit.',
|
|
340
|
+
stop: 'Stop',
|
|
341
|
+
openBrowser: 'Open in the default browser',
|
|
342
|
+
loadFailed: 'This page did not open',
|
|
343
|
+
pageCrashed: 'This page crashed',
|
|
344
|
+
retry: 'Retry',
|
|
345
|
+
agentDriving: 'Ta is working here',
|
|
346
|
+
importSignIns: 'Import sign-ins from your browser',
|
|
347
|
+
importTitle: 'Import sign-ins',
|
|
348
|
+
importHint:
|
|
349
|
+
'Copies the cookies of the browser you pick here, so you and the agent use the sites you are signed in to. Passwords and history are not imported.',
|
|
350
|
+
importFrom: 'Import from',
|
|
351
|
+
noProfiles: 'No profile of Chrome, Edge, Brave, Arc, Vivaldi or Chromium was found.',
|
|
352
|
+
importNow: 'Import…',
|
|
353
|
+
importing: 'Importing…',
|
|
354
|
+
imported: '{count} cookies imported',
|
|
355
|
+
importSkipped: '{count} could not be read and were skipped.',
|
|
356
|
+
importAppBound: "{count} are under Chrome's app-bound encryption: sign in again on those sites.",
|
|
357
|
+
clearBrowserData: 'Clear browser data',
|
|
358
|
+
clearBrowserDataDetail: 'Deletes every cookie, site storage and the cache of the browser of Ta: every site needs a sign-in again.',
|
|
359
|
+
clear: 'Clear',
|
|
360
|
+
cleared: 'Browser data cleared',
|
|
361
|
+
done: 'Done',
|
|
287
362
|
binary: 'A binary file; its content is not shown ({size}).',
|
|
288
363
|
truncated: 'A large file: only the first 1 MB is shown, read-only.',
|
|
289
364
|
line: 'Line {line}',
|
package/web-src/index.tsx
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { WebExtensionAPI, WebLink } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
2
|
-
import { BrowserView
|
|
2
|
+
import { BrowserView } from './browser';
|
|
3
|
+
import { AgentBrowser } from './browser-agent';
|
|
4
|
+
import { ShowPage } from './browser-common';
|
|
3
5
|
import { OpenFiles, openFile } from './editors';
|
|
4
6
|
import { ExplorerView, ShowDirectory } from './explorer';
|
|
5
7
|
import { bindHost, FilesChanged, Host, PACKAGE } from './host';
|
|
@@ -55,6 +57,10 @@ export default function activate(pi: WebExtensionAPI): () => void {
|
|
|
55
57
|
pi.on('tool_execution_end', () => FilesChanged.notify());
|
|
56
58
|
pi.on('agent_end', () => FilesChanged.notify());
|
|
57
59
|
pi.registerOpener(link => openLink(pi, link));
|
|
60
|
+
// The agent works the browser in Ta Desktop only, where the app gives the page the DevTools protocol.
|
|
61
|
+
if (pi.ui.debugWebview) {
|
|
62
|
+
pi.registerTool(AgentBrowser.TOOL);
|
|
63
|
+
}
|
|
58
64
|
const stopTerminals = pi.onTerminalData((terminalId, data, exitCode) => Terminals.receive(terminalId, data, exitCode));
|
|
59
65
|
// The files open when the page closed open again, behind what is shown.
|
|
60
66
|
void OpenFiles.restore();
|
package/web-src/style.ts
CHANGED
|
@@ -30,6 +30,12 @@ export const STYLE = `
|
|
|
30
30
|
.taw-guides { position: absolute; inset: 0; pointer-events: none; }
|
|
31
31
|
.taw-guide { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--ta-border); opacity: 0; transition: opacity 120ms; }
|
|
32
32
|
.taw-tree:hover .taw-guide { opacity: 1; }
|
|
33
|
+
.taw-tree-row.taw-drop, .taw-tree-row.taw-drop:hover { background: color-mix(in srgb, var(--ta-border-focus) 16%, transparent); }
|
|
34
|
+
.taw-tree.taw-drop-root { box-shadow: inset 0 0 0 1px var(--ta-border-focus); background: color-mix(in srgb, var(--ta-border-focus) 8%, transparent); }
|
|
35
|
+
.taw-transfer { position: relative; flex: none; display: flex; align-items: center; gap: 8px; height: 28px; padding: 0 12px; border-top: 1px solid var(--ta-border-muted); color: var(--ta-secondary); font-size: 12px; overflow: hidden; }
|
|
36
|
+
.taw-transfer > span:first-child { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
37
|
+
.taw-transfer .taw-detail { flex: none; }
|
|
38
|
+
.taw-transfer-bar { position: absolute; left: 0; bottom: 0; height: 2px; background: var(--ta-border-focus); transition: width 0.2s ease; }
|
|
33
39
|
.taw-editing { cursor: default; background: none !important; }
|
|
34
40
|
.taw-name-input { position: relative; flex: 1; min-width: 0; display: flex; }
|
|
35
41
|
.taw-input.taw-inline { height: 22px; padding: 0 4px; border-radius: 4px; font-size: 13px; }
|
|
@@ -184,11 +190,35 @@ export const STYLE = `
|
|
|
184
190
|
.taw-text { padding-right: 16px; }
|
|
185
191
|
.taw-hint { flex: none; padding: 4px 12px; border-bottom: 1px solid var(--ta-border-muted); background: var(--ta-raised); color: var(--ta-secondary); font-size: 12px; }
|
|
186
192
|
.taw-frame { flex: 1; width: 100%; min-height: 0; border: 0; background: #fff; }
|
|
187
|
-
.taw-
|
|
188
|
-
.taw-
|
|
189
|
-
.taw-
|
|
190
|
-
.taw-
|
|
191
|
-
|
|
193
|
+
.taw-browser-stage { position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; }
|
|
194
|
+
.taw-webview { flex: 1; width: 100%; min-height: 0; border: 0; background: #fff; }
|
|
195
|
+
.taw-browser-driven { display: inline-flex; align-items: center; flex: none; gap: 6px; height: 22px; margin-right: 2px; padding: 0 9px 0 8px; border-radius: 11px; background: color-mix(in srgb, var(--ta-green) 14%, transparent); color: var(--ta-green); font-size: 11.5px; white-space: nowrap; }
|
|
196
|
+
.taw-browser-driven-dot { width: 6px; height: 6px; border-radius: 3px; background: currentColor; animation: taw-pulse 1.2s ease-in-out infinite; }
|
|
197
|
+
@keyframes taw-pulse { 50% { opacity: 0.35; } }
|
|
198
|
+
.taw-browser-import { display: inline-flex; align-items: center; gap: 6px; margin-top: 18px; padding: 0; border: 0; background: none; color: var(--ta-secondary); font: inherit; font-size: 12.5px; cursor: pointer; }
|
|
199
|
+
.taw-browser-import:hover { color: var(--ta-text); text-decoration: underline; text-underline-offset: 3px; }
|
|
200
|
+
.taw-import { display: flex; flex-direction: column; gap: 12px; width: min(420px, 80vw); font-size: 13px; color: var(--ta-text); }
|
|
201
|
+
.taw-import-hint { margin: 0; color: var(--ta-secondary); line-height: 1.6; }
|
|
202
|
+
.taw-import-error { margin: 0; color: var(--ta-red); line-height: 1.5; overflow-wrap: anywhere; }
|
|
203
|
+
.taw-import-profiles { display: flex; flex-direction: column; gap: 2px; max-height: 260px; overflow: auto; }
|
|
204
|
+
.taw-import-profile { display: flex; align-items: center; gap: 10px; height: 38px; padding: 0 10px; border: 1px solid transparent; border-radius: 8px; cursor: pointer; }
|
|
205
|
+
.taw-import-profile:hover { background: var(--ta-hover); }
|
|
206
|
+
.taw-import-profile.taw-on { border-color: var(--ta-border-focus); background: var(--ta-hover); }
|
|
207
|
+
.taw-import-profile input { position: absolute; opacity: 0; pointer-events: none; }
|
|
208
|
+
.taw-import-browser { flex: none; font-weight: 500; }
|
|
209
|
+
.taw-import-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ta-muted); }
|
|
210
|
+
.taw-import-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 4px; }
|
|
211
|
+
.taw-import-result { display: flex; flex-direction: column; align-items: center; text-align: center; padding: 8px 0 0; }
|
|
212
|
+
.taw-import-result .taw-import-actions { justify-content: center; margin-top: 18px; }
|
|
213
|
+
.taw-button.taw-primary { border-color: var(--ta-brand); background: var(--ta-brand); color: var(--ta-brand-text); font-weight: 500; }
|
|
214
|
+
.taw-button.taw-primary:hover:not(:disabled) { background: var(--ta-brand); opacity: 0.88; }
|
|
215
|
+
.taw-button:disabled { opacity: 0.45; cursor: default; }
|
|
216
|
+
.taw-link { padding: 0; border: 0; background: none; color: var(--ta-muted); font: inherit; font-size: 12.5px; cursor: pointer; }
|
|
217
|
+
.taw-link:hover:not(:disabled) { color: var(--ta-text); text-decoration: underline; text-underline-offset: 3px; }
|
|
218
|
+
.taw-address-favicon { width: 14px; height: 14px; border-radius: 3px; object-fit: contain; }
|
|
219
|
+
.taw-browser-failure { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px; background: var(--ta-main); text-align: center; }
|
|
220
|
+
.taw-browser-failure .taw-browser-sub { max-width: 380px; overflow-wrap: anywhere; }
|
|
221
|
+
.taw-terminal { position: relative; flex: 1; min-height: 0; overflow: hidden; background: var(--ta-code); }
|
|
192
222
|
@media (pointer: coarse) {
|
|
193
223
|
.taw-row { height: 36px; }
|
|
194
224
|
.taw-action { width: 32px; height: 32px; }
|
|
@@ -212,7 +242,6 @@ export const STYLE = `
|
|
|
212
242
|
.taw-terminal-tab > button:first-child { display: flex; align-items: center; gap: 6px; min-width: 0; height: 24px; padding: 0 2px 0 8px; border: 0; background: none; color: inherit; font: inherit; font-size: 12px; cursor: pointer; }
|
|
213
243
|
.taw-terminal-tab > button:first-child > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
214
244
|
.taw-terminal-tab > .taw-action { width: 20px; height: 20px; }
|
|
215
|
-
.taw-terminal-input { position: absolute; left: 0; bottom: 0; width: 1px; height: 1px; padding: 0; border: 0; opacity: 0; resize: none; }
|
|
216
245
|
`;
|
|
217
246
|
|
|
218
247
|
/** Adds the styles to the page; the function returned takes them away. */
|
package/web-src/terminal.tsx
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import type { TaTerminalInfo } from '@celljs/ta-protocol';
|
|
2
|
+
import type { WebTerminal } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
2
3
|
import { Action, Empty, Glyph, type MenuItem, useContextMenu } from './common';
|
|
3
4
|
import { Host, pi } from './host';
|
|
4
5
|
import { t } from './i18n';
|
|
5
6
|
import { React } from './react';
|
|
6
|
-
import { AnsiScreen, AnsiUtil } from './util/ansi';
|
|
7
|
-
import { TerminalKeys } from './util/keys';
|
|
8
7
|
import { WorkspaceUtil } from './util/workspace';
|
|
9
8
|
|
|
10
|
-
const LINE_HEIGHT = 18;
|
|
11
|
-
const SCROLLBACK = 1000;
|
|
12
9
|
const RESIZE_MS = 120;
|
|
10
|
+
/** A shell opens at this size, and its screen with it, until its view fits it. */
|
|
11
|
+
const OPEN_COLS = 100;
|
|
12
|
+
const OPEN_ROWS = 30;
|
|
13
13
|
|
|
14
14
|
/** `pty` is false for a shell on pipes (the server has no `script`). */
|
|
15
15
|
type TerminalInfo = TaTerminalInfo;
|
|
16
16
|
|
|
17
17
|
interface Session {
|
|
18
18
|
info: TerminalInfo;
|
|
19
|
-
|
|
19
|
+
terminal: WebTerminal;
|
|
20
20
|
exitCode?: number;
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -42,7 +42,7 @@ export namespace Terminals {
|
|
|
42
42
|
return () => listeners.delete(listener);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
/**
|
|
45
|
+
/** The list of terminals or how one ended changed: views read it again once a frame. */
|
|
46
46
|
function changed(): void {
|
|
47
47
|
list = [...sessions.values()];
|
|
48
48
|
if (!frame) {
|
|
@@ -58,8 +58,22 @@ export namespace Terminals {
|
|
|
58
58
|
export function add(info: TerminalInfo): Session {
|
|
59
59
|
let session = sessions.get(info.terminalId);
|
|
60
60
|
if (!session) {
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
const terminal = pi.ui.createTerminal({ cols: OPEN_COLS, rows: OPEN_ROWS, cwd: info.cwd || undefined });
|
|
62
|
+
const created: Session = { info, terminal };
|
|
63
|
+
const id = info.terminalId;
|
|
64
|
+
terminal.onData(data => {
|
|
65
|
+
if (created.exitCode === undefined) {
|
|
66
|
+
void pi.terminals.writeTerminal(id, data).catch(() => undefined);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
// The shell hears the last size, once the view stopped changing.
|
|
70
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
71
|
+
terminal.onResize(({ cols, rows }) => {
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
timer = setTimeout(() => void pi.terminals.resizeTerminal(id, cols, rows).catch(() => undefined), RESIZE_MS);
|
|
74
|
+
});
|
|
75
|
+
session = created;
|
|
76
|
+
sessions.set(id, session);
|
|
63
77
|
changed();
|
|
64
78
|
}
|
|
65
79
|
return session;
|
|
@@ -70,17 +84,20 @@ export namespace Terminals {
|
|
|
70
84
|
return;
|
|
71
85
|
}
|
|
72
86
|
const session = sessions.get(terminalId) ?? add({ terminalId, cwd: '', shell: '', pty: true });
|
|
73
|
-
session.
|
|
87
|
+
session.terminal.write(data);
|
|
74
88
|
// JSON has no undefined: a terminal that goes on sends null.
|
|
75
89
|
if (exitCode !== undefined && exitCode !== null) {
|
|
76
90
|
session.exitCode = exitCode;
|
|
77
|
-
|
|
91
|
+
// Nothing more is typed there: the cursor goes.
|
|
92
|
+
session.terminal.write(`\r\n${t('terminalEnded', { code: exitCode })}\r\n\x1b[?25l`);
|
|
93
|
+
changed();
|
|
78
94
|
}
|
|
79
|
-
changed();
|
|
80
95
|
}
|
|
81
96
|
|
|
82
97
|
export function remove(terminalId: string): void {
|
|
83
|
-
|
|
98
|
+
const session = sessions.get(terminalId);
|
|
99
|
+
if (session && sessions.delete(terminalId)) {
|
|
100
|
+
session.terminal.dispose();
|
|
84
101
|
closed.add(terminalId);
|
|
85
102
|
changed();
|
|
86
103
|
}
|
|
@@ -129,7 +146,7 @@ export function TerminalView() {
|
|
|
129
146
|
opening.current = true;
|
|
130
147
|
try {
|
|
131
148
|
setError(undefined);
|
|
132
|
-
setShown(Terminals.add(await pi.terminals.openTerminal({ cwd, cols:
|
|
149
|
+
setShown(Terminals.add(await pi.terminals.openTerminal({ cwd, cols: OPEN_COLS, rows: OPEN_ROWS })).info.terminalId);
|
|
133
150
|
} catch (failure) {
|
|
134
151
|
setError(Host.message(failure));
|
|
135
152
|
} finally {
|
|
@@ -204,147 +221,49 @@ export function TerminalView() {
|
|
|
204
221
|
);
|
|
205
222
|
}
|
|
206
223
|
|
|
207
|
-
/** The
|
|
208
|
-
function measure(element: HTMLElement): { width: number; height: number } {
|
|
209
|
-
const probe = document.createElement('span');
|
|
210
|
-
probe.textContent = 'MMMMMMMMMM';
|
|
211
|
-
probe.style.visibility = 'hidden';
|
|
212
|
-
probe.style.position = 'absolute';
|
|
213
|
-
element.appendChild(probe);
|
|
214
|
-
const width = probe.getBoundingClientRect().width / 10 || 8;
|
|
215
|
-
probe.remove();
|
|
216
|
-
return { width, height: LINE_HEIGHT };
|
|
217
|
-
}
|
|
218
|
-
|
|
224
|
+
/** The screen of a shell: its terminal, moved into this view while it shows. */
|
|
219
225
|
function TerminalEditor({ terminalId }: { terminalId: string }) {
|
|
220
226
|
useTerminals();
|
|
221
227
|
const session = Terminals.get(terminalId);
|
|
222
228
|
const box = React.useRef<HTMLDivElement>(null);
|
|
223
|
-
const
|
|
224
|
-
const composing = React.useRef(false);
|
|
225
|
-
const pinned = React.useRef(true);
|
|
226
|
-
const write = React.useCallback((data: string) => void pi.terminals.writeTerminal(terminalId, data).catch(() => undefined), [terminalId]);
|
|
227
|
-
|
|
229
|
+
const terminal = session?.terminal;
|
|
228
230
|
React.useEffect(() => {
|
|
229
231
|
const element = box.current;
|
|
230
|
-
if (!element || !
|
|
232
|
+
if (!element || !terminal) {
|
|
231
233
|
return;
|
|
232
234
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if (!element.clientWidth || !element.clientHeight) {
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
const cell = measure(element);
|
|
241
|
-
const cols = Math.max(20, Math.floor((element.clientWidth - 16) / cell.width));
|
|
242
|
-
const rows = Math.max(5, Math.floor((element.clientHeight - 16) / cell.height));
|
|
243
|
-
if (`${cols}x${rows}` === last) {
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
last = `${cols}x${rows}`;
|
|
247
|
-
session.screen.resize(cols, rows);
|
|
248
|
-
clearTimeout(timer);
|
|
249
|
-
timer = setTimeout(() => void pi.terminals.resizeTerminal(terminalId, cols, rows).catch(() => undefined), RESIZE_MS);
|
|
250
|
-
});
|
|
251
|
-
observer.observe(element);
|
|
252
|
-
return () => {
|
|
253
|
-
observer.disconnect();
|
|
254
|
-
clearTimeout(timer);
|
|
255
|
-
};
|
|
256
|
-
}, [session, terminalId]);
|
|
257
|
-
|
|
258
|
-
// The keys go to the terminal once it shows.
|
|
259
|
-
React.useEffect(() => input.current?.focus({ preventScroll: true }), []);
|
|
260
|
-
|
|
261
|
-
// Stays at the bottom while it is there.
|
|
262
|
-
React.useLayoutEffect(() => {
|
|
263
|
-
const element = box.current;
|
|
264
|
-
if (element && pinned.current) {
|
|
265
|
-
element.scrollTop = element.scrollHeight;
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
|
|
235
|
+
const detach = terminal.attach(element);
|
|
236
|
+
terminal.focus();
|
|
237
|
+
return detach;
|
|
238
|
+
}, [terminal]);
|
|
269
239
|
if (!session) {
|
|
270
240
|
return <Empty>{t('noTerminals')}</Empty>;
|
|
271
241
|
}
|
|
272
|
-
const lines = session.screen.spans();
|
|
273
|
-
const cursor = session.screen.cursor();
|
|
274
|
-
const ended = session.exitCode !== undefined;
|
|
275
|
-
const onKeyDown = (event: React.KeyboardEvent) => {
|
|
276
|
-
if (composing.current || ended) {
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
const sequence = TerminalKeys.sequence(event);
|
|
280
|
-
if (sequence !== undefined) {
|
|
281
|
-
event.preventDefault();
|
|
282
|
-
write(sequence);
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
const flush = () => {
|
|
286
|
-
const element = input.current;
|
|
287
|
-
if (element?.value && !composing.current) {
|
|
288
|
-
if (!ended) {
|
|
289
|
-
write(element.value);
|
|
290
|
-
}
|
|
291
|
-
element.value = '';
|
|
292
|
-
}
|
|
293
|
-
};
|
|
294
242
|
return (
|
|
295
243
|
<div className="taw-editor">
|
|
296
244
|
{session.info.pty === false && <div className="taw-hint">{t('pipeMode')}</div>}
|
|
297
|
-
{/* biome-ignore lint/a11y/noStaticElementInteractions:
|
|
245
|
+
{/* biome-ignore lint/a11y/noStaticElementInteractions: dropping files types their paths, as they could be typed */}
|
|
298
246
|
<div
|
|
299
247
|
className="taw-terminal"
|
|
300
248
|
ref={box}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
249
|
+
onDragOver={event => {
|
|
250
|
+
if (pi.ui.fileDrag.has(event.dataTransfer)) {
|
|
251
|
+
event.preventDefault();
|
|
252
|
+
event.dataTransfer.dropEffect = 'copy';
|
|
253
|
+
}
|
|
305
254
|
}}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
255
|
+
onDrop={event => {
|
|
256
|
+
const files = pi.ui.fileDrag.get(event.dataTransfer);
|
|
257
|
+
if (!files || session.exitCode !== undefined) {
|
|
258
|
+
return;
|
|
309
259
|
}
|
|
260
|
+
event.preventDefault();
|
|
261
|
+
const windows = /(^|[\\/])(cmd|powershell|pwsh)(\.exe)?$/i.test(session.info.shell);
|
|
262
|
+
const words = pi.ui.fileDrag.paths(files).map(path => WorkspaceUtil.shellQuote(path, windows));
|
|
263
|
+
void pi.terminals.writeTerminal(terminalId, `${words.join(' ')} `).catch(() => undefined);
|
|
264
|
+
terminal?.focus();
|
|
310
265
|
}}
|
|
311
|
-
|
|
312
|
-
<pre className="taw-screen">
|
|
313
|
-
{lines.map((spans, row) => (
|
|
314
|
-
<div key={row} className="taw-screen-line">
|
|
315
|
-
{spans.map((span, index) => (
|
|
316
|
-
<span key={index} style={AnsiUtil.css(span.style)}>
|
|
317
|
-
{span.text}
|
|
318
|
-
</span>
|
|
319
|
-
))}
|
|
320
|
-
{row === cursor.row && !ended && <span className="taw-cursor" style={{ left: `${cursor.col}ch` }} />}
|
|
321
|
-
{'\n'}
|
|
322
|
-
</div>
|
|
323
|
-
))}
|
|
324
|
-
</pre>
|
|
325
|
-
<textarea
|
|
326
|
-
ref={input}
|
|
327
|
-
className="taw-terminal-input"
|
|
328
|
-
aria-label={t('terminal')}
|
|
329
|
-
autoCapitalize="off"
|
|
330
|
-
autoComplete="off"
|
|
331
|
-
autoCorrect="off"
|
|
332
|
-
spellCheck={false}
|
|
333
|
-
onKeyDown={onKeyDown}
|
|
334
|
-
onInput={flush}
|
|
335
|
-
onCompositionStart={() => (composing.current = true)}
|
|
336
|
-
onCompositionEnd={() => {
|
|
337
|
-
composing.current = false;
|
|
338
|
-
flush();
|
|
339
|
-
}}
|
|
340
|
-
onPaste={event => {
|
|
341
|
-
event.preventDefault();
|
|
342
|
-
if (!ended) {
|
|
343
|
-
write(TerminalKeys.paste(event.clipboardData.getData('text')));
|
|
344
|
-
}
|
|
345
|
-
}}
|
|
346
|
-
/>
|
|
347
|
-
</div>
|
|
266
|
+
/>
|
|
348
267
|
</div>
|
|
349
268
|
);
|
|
350
269
|
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { type AXNode, BrowserSnapshot } from './browser-snapshot';
|
|
3
|
+
|
|
4
|
+
const node = (nodeId: string, role: string, name: string, extra: Partial<AXNode> = {}): AXNode => ({
|
|
5
|
+
nodeId,
|
|
6
|
+
role: { value: role },
|
|
7
|
+
name: { value: name },
|
|
8
|
+
...extra,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('BrowserSnapshot', () => {
|
|
12
|
+
it('shows a page as indented lines, with refs on what one acts on', () => {
|
|
13
|
+
const nodes: AXNode[] = [
|
|
14
|
+
node('1', 'RootWebArea', 'Sign in', { childIds: ['2'] }),
|
|
15
|
+
node('2', 'generic', '', { parentId: '1', childIds: ['3', '4', '6', '7', '9'] }),
|
|
16
|
+
node('3', 'heading', 'Welcome back', { parentId: '2', childIds: ['3t'], properties: [{ name: 'level', value: { value: 1 } }] }),
|
|
17
|
+
node('3t', 'StaticText', 'Welcome back', { parentId: '3' }),
|
|
18
|
+
node('4', 'textbox', 'Email', {
|
|
19
|
+
parentId: '2',
|
|
20
|
+
backendDOMNodeId: 41,
|
|
21
|
+
childIds: ['5'],
|
|
22
|
+
value: { value: 'a@b.dev' },
|
|
23
|
+
properties: [
|
|
24
|
+
{ name: 'focused', value: { value: true } },
|
|
25
|
+
{ name: 'required', value: { value: true } },
|
|
26
|
+
],
|
|
27
|
+
}),
|
|
28
|
+
node('5', 'generic', '', { parentId: '4', backendDOMNodeId: 50, properties: [{ name: 'editable', value: { value: 'plaintext' } }] }),
|
|
29
|
+
node('6', 'checkbox', 'Remember me', { parentId: '2', backendDOMNodeId: 60, properties: [{ name: 'checked', value: { value: 'mixed' } }] }),
|
|
30
|
+
node('7', 'button', 'More', { parentId: '2', backendDOMNodeId: 70, properties: [{ name: 'expanded', value: { value: false } }] }),
|
|
31
|
+
node('9', 'paragraph', '', { parentId: '2', childIds: ['10', '11'] }),
|
|
32
|
+
node('10', 'StaticText', 'No account?', { parentId: '9' }),
|
|
33
|
+
node('11', 'link', 'Sign up', { parentId: '9', backendDOMNodeId: 110, ignored: false, childIds: ['12'] }),
|
|
34
|
+
node('12', 'StaticText', 'Sign up', { parentId: '11' }),
|
|
35
|
+
];
|
|
36
|
+
expect(BrowserSnapshot.render(nodes)).toBe(
|
|
37
|
+
[
|
|
38
|
+
'RootWebArea "Sign in"',
|
|
39
|
+
' heading "Welcome back" level=1',
|
|
40
|
+
' [41] textbox "Email" (focused, required) value="a@b.dev"',
|
|
41
|
+
' [60] checkbox "Remember me" (checked=mixed)',
|
|
42
|
+
' [70] button "More" (collapsed)',
|
|
43
|
+
' "No account?"',
|
|
44
|
+
' [110] link "Sign up"',
|
|
45
|
+
].join('\n'),
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('gives a ref to an editable element of its own, and leaves out labels without a name', () => {
|
|
50
|
+
const nodes: AXNode[] = [
|
|
51
|
+
node('1', 'LabelText', '', { childIds: ['2', '3'] }),
|
|
52
|
+
node('2', 'StaticText', 'Notes', { parentId: '1' }),
|
|
53
|
+
node('3', 'generic', '', { parentId: '1', backendDOMNodeId: 7, properties: [{ name: 'editable', value: { value: 'richtext' } }] }),
|
|
54
|
+
];
|
|
55
|
+
expect(BrowserSnapshot.render(nodes)).toBe('"Notes"\n[7] generic');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('skips what is ignored and says how much it left out', () => {
|
|
59
|
+
const nodes: AXNode[] = [
|
|
60
|
+
node('1', 'generic', '', { childIds: ['2', '3', '4'] }),
|
|
61
|
+
node('2', 'none', '', { parentId: '1', childIds: ['5'] }),
|
|
62
|
+
node('3', 'button', 'Hidden', { parentId: '1', ignored: true, backendDOMNodeId: 3 }),
|
|
63
|
+
node('4', 'StaticText', 'one', { parentId: '1' }),
|
|
64
|
+
node('5', 'StaticText', 'two', { parentId: '2' }),
|
|
65
|
+
];
|
|
66
|
+
expect(BrowserSnapshot.render(nodes, 1)).toBe('"two"\n… 1 more lines: scroll, or evaluate to read a part of the page.');
|
|
67
|
+
expect(BrowserSnapshot.render([])).toBe('(the page shows nothing to read yet)');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('cuts long names', () => {
|
|
71
|
+
const line = BrowserSnapshot.render([node('1', 'heading', 'x'.repeat(300))]);
|
|
72
|
+
expect(line.length).toBeLessThan(140);
|
|
73
|
+
expect(line.endsWith('…"')).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('finds the middle of a quad', () => {
|
|
77
|
+
expect(BrowserSnapshot.center([10, 20, 110, 20, 110, 60, 10, 60])).toEqual({ x: 60, y: 40 });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('reads keys and shortcuts as key events', () => {
|
|
81
|
+
expect(BrowserSnapshot.key('Enter')).toEqual({ key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, modifiers: 0, text: '\r' });
|
|
82
|
+
expect(BrowserSnapshot.key('a')).toEqual({ key: 'a', code: 'KeyA', windowsVirtualKeyCode: 65, modifiers: 0, text: 'a' });
|
|
83
|
+
expect(BrowserSnapshot.key('Shift+a')).toEqual({ key: 'A', code: 'KeyA', windowsVirtualKeyCode: 65, modifiers: 8, text: 'A' });
|
|
84
|
+
expect(BrowserSnapshot.key('Control+A')).toEqual({ key: 'a', code: 'KeyA', windowsVirtualKeyCode: 65, modifiers: 2 });
|
|
85
|
+
expect(BrowserSnapshot.key('Meta+Shift+ArrowLeft')).toEqual({ key: 'ArrowLeft', code: 'ArrowLeft', windowsVirtualKeyCode: 37, modifiers: 12 });
|
|
86
|
+
expect(BrowserSnapshot.key('cmd+enter')).toEqual({ key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, modifiers: 4 });
|
|
87
|
+
expect(BrowserSnapshot.key('Space')).toMatchObject({ key: ' ', text: ' ' });
|
|
88
|
+
expect(BrowserSnapshot.key('F5')).toEqual({ key: 'F5', code: 'F5', windowsVirtualKeyCode: 116, modifiers: 0 });
|
|
89
|
+
expect(BrowserSnapshot.key('7')).toMatchObject({ code: 'Digit7', windowsVirtualKeyCode: 55, text: '7' });
|
|
90
|
+
expect(BrowserSnapshot.key('+')).toMatchObject({ key: '+', text: '+' });
|
|
91
|
+
expect(BrowserSnapshot.key('Control++')).toMatchObject({ key: '+', modifiers: 2 });
|
|
92
|
+
expect(BrowserSnapshot.key('Hyper+a')).toBeUndefined();
|
|
93
|
+
expect(BrowserSnapshot.key('Hello')).toBeUndefined();
|
|
94
|
+
expect(BrowserSnapshot.key('')).toBeUndefined();
|
|
95
|
+
});
|
|
96
|
+
});
|