@celljs/ta-workspace 4.8.0 → 4.9.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.md +2 -2
- package/README.zh-cn.md +2 -2
- package/package.json +13 -4
- package/web/index.js +916 -295
- package/web-src/browser-agent.ts +0 -440
- package/web-src/browser-common.tsx +0 -278
- package/web-src/browser-import.tsx +0 -126
- package/web-src/browser-webview.tsx +0 -270
- package/web-src/browser.tsx +0 -80
- package/web-src/common.tsx +0 -296
- package/web-src/editors.tsx +0 -471
- package/web-src/explorer.tsx +0 -717
- package/web-src/file-icon.tsx +0 -21
- package/web-src/host.ts +0 -121
- package/web-src/i18n.ts +0 -395
- package/web-src/index.tsx +0 -71
- package/web-src/open-files.tsx +0 -80
- package/web-src/preview.tsx +0 -132
- package/web-src/react.ts +0 -8
- package/web-src/scm.tsx +0 -388
- package/web-src/search.tsx +0 -485
- package/web-src/style.ts +0 -254
- package/web-src/terminal.tsx +0 -269
- package/web-src/util/browser-snapshot.spec.ts +0 -96
- package/web-src/util/browser-snapshot.ts +0 -225
- package/web-src/util/file-icon.spec.ts +0 -35
- package/web-src/util/file-icon.ts +0 -23
- package/web-src/util/git.ts +0 -203
- package/web-src/util/preview.spec.ts +0 -48
- package/web-src/util/preview.ts +0 -90
- package/web-src/util/search.spec.ts +0 -164
- package/web-src/util/search.ts +0 -274
- package/web-src/util/seti-font.ts +0 -6
- package/web-src/util/seti.ts +0 -761
- package/web-src/util/upload.spec.ts +0 -66
- package/web-src/util/upload.ts +0 -123
- package/web-src/util/workspace.spec.ts +0 -220
- package/web-src/util/workspace.ts +0 -396
- package/web-src/util/zip.spec.ts +0 -60
- package/web-src/util/zip.ts +0 -137
- package/web-src/workspace-client.ts +0 -221
package/web-src/explorer.tsx
DELETED
|
@@ -1,717 +0,0 @@
|
|
|
1
|
-
import { type TaDirEntry, TaLocation } from '@celljs/ta-protocol';
|
|
2
|
-
import { Action, copyText, Empty, Glyph, type MenuItem, NameInput, useContextMenu, useKept, ViewHeader } from './common';
|
|
3
|
-
import { closeEditorsOf, fileId, OpenFiles, openFile } from './editors';
|
|
4
|
-
import { FileIcon } from './file-icon';
|
|
5
|
-
import { Channel, FilesChanged, Host, MAC, PACKAGE, pi, useFilesChanged, useLoad } from './host';
|
|
6
|
-
import { gitLabel, t } from './i18n';
|
|
7
|
-
import { React } from './react';
|
|
8
|
-
import { SearchRequests } from './search';
|
|
9
|
-
import { type GitDecorations, GitUtil } from './util/git';
|
|
10
|
-
import { type UploadBatch, UploadUtil } from './util/upload';
|
|
11
|
-
import { WorkspaceUtil } from './util/workspace';
|
|
12
|
-
import { WorkspaceClient } from './workspace-client';
|
|
13
|
-
|
|
14
|
-
interface Tree {
|
|
15
|
-
children: Map<string, TaDirEntry[]>;
|
|
16
|
-
expanded: Set<string>;
|
|
17
|
-
error?: string;
|
|
18
|
-
/** The directories open before are open again: from now on, what is open is kept. */
|
|
19
|
-
restored?: boolean;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** A name being typed in the tree: a new file or folder in `parent`, or another name for `path`. */
|
|
23
|
-
type Editing = { kind: 'create'; parent: string; directory: boolean } | { kind: 'rename'; path: string };
|
|
24
|
-
|
|
25
|
-
const trees = new Map<string, Tree>();
|
|
26
|
-
/** The directory shown, by the directory of the conversation: that one, unless another was picked for it. */
|
|
27
|
-
const roots = new Map<string, string>();
|
|
28
|
-
const INDENT = 12;
|
|
29
|
-
/** The directories open, kept for each directory of a conversation (pi.storage.directory). */
|
|
30
|
-
const EXPANDED_KEY = 'explorer.expanded';
|
|
31
|
-
const NO_DECORATIONS: GitDecorations = { files: new Map(), folders: new Map(), ignored: [] };
|
|
32
|
-
/** How long a dragged item hovers over a closed folder before it opens, as in VS Code. */
|
|
33
|
-
const DRAG_OPEN_MS = 600;
|
|
34
|
-
|
|
35
|
-
/** Files going up, or a folder being packed: what the bar at the bottom of the view shows. */
|
|
36
|
-
interface Transfer {
|
|
37
|
-
label: string;
|
|
38
|
-
done: number;
|
|
39
|
-
total: number;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** What drops on the explorer: files of the system (uploaded), or files dragged in the page (moved). */
|
|
43
|
-
function acceptsDrop(dataTransfer: DataTransfer): boolean {
|
|
44
|
-
return dataTransfer.types.includes('Files') || pi.ui.fileDrag.has(dataTransfer);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Files to show in the explorer, as `reveal in explorer` of source control or open files. */
|
|
48
|
-
export const RevealRequests = new Channel<{ cwd: string; path: string }>();
|
|
49
|
-
/** A directory to show in place of the conversation's, as a link of a reply to it. */
|
|
50
|
-
export const ShowDirectory = new Channel<string>();
|
|
51
|
-
|
|
52
|
-
/** The files of the directory of the conversation shown, which follows the conversation; another can be picked for it. */
|
|
53
|
-
export function ExplorerView() {
|
|
54
|
-
const cwd = pi.ui.useCwd();
|
|
55
|
-
const changed = useFilesChanged();
|
|
56
|
-
const active = pi.ui.useActiveEditor();
|
|
57
|
-
const [root, setRoot] = useKept(roots, cwd, () => cwd);
|
|
58
|
-
const [tree, setTree] = useKept<Tree>(trees, root, () => ({ children: new Map(), expanded: new Set() }));
|
|
59
|
-
const [selected, setSelected] = React.useState<string>();
|
|
60
|
-
const [editing, setEditing] = React.useState<Editing>();
|
|
61
|
-
const [picking, setPicking] = React.useState(false);
|
|
62
|
-
const [menu, openMenu] = useContextMenu();
|
|
63
|
-
const list = React.useRef<HTMLDivElement>(null);
|
|
64
|
-
/** The folder a drag is over ('' for the top); undefined when none is. */
|
|
65
|
-
const [dropTarget, setDropTarget] = React.useState<string>();
|
|
66
|
-
const [transfer, setTransfer] = React.useState<Transfer>();
|
|
67
|
-
const filePicker = React.useRef<HTMLInputElement>(null);
|
|
68
|
-
const folderPicker = React.useRef<HTMLInputElement>(null);
|
|
69
|
-
/** The folder the files picked go to. */
|
|
70
|
-
const pickedFolder = React.useRef('');
|
|
71
|
-
const dragOpen = React.useRef<{ path: string; timer: ReturnType<typeof setTimeout> }>(undefined);
|
|
72
|
-
const desktop = pi.ui.desktop;
|
|
73
|
-
const showsInFileManager = !!desktop && !TaLocation.isRemote(root);
|
|
74
|
-
const git = useLoad(() => WorkspaceClient.status(root, true).catch(() => undefined), [root, changed]);
|
|
75
|
-
const decorations = React.useMemo(() => (git.value?.repository ? GitUtil.decorations(git.value) : NO_DECORATIONS), [git.value]);
|
|
76
|
-
const FolderPicker = pi.ui.FolderPicker;
|
|
77
|
-
|
|
78
|
-
const load = React.useCallback(
|
|
79
|
-
async (dirs: string[]) => {
|
|
80
|
-
const read = await Promise.all(
|
|
81
|
-
dirs.map(async dir => {
|
|
82
|
-
try {
|
|
83
|
-
return [dir, await pi.workspace.listDirectory(dir, root)] as const;
|
|
84
|
-
} catch (error) {
|
|
85
|
-
return [dir, Host.message(error)] as const;
|
|
86
|
-
}
|
|
87
|
-
}),
|
|
88
|
-
);
|
|
89
|
-
setTree(previous => {
|
|
90
|
-
const children = new Map(previous.children);
|
|
91
|
-
let error: string | undefined;
|
|
92
|
-
for (const [dir, result] of read) {
|
|
93
|
-
if (typeof result === 'string') {
|
|
94
|
-
children.delete(dir);
|
|
95
|
-
error = dir ? error : result;
|
|
96
|
-
} else {
|
|
97
|
-
children.set(dir, result);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return { ...previous, children, error };
|
|
101
|
-
});
|
|
102
|
-
},
|
|
103
|
-
[root, setTree],
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
// What is open is read again when the agent changed files, or the directory changed.
|
|
107
|
-
React.useEffect(() => {
|
|
108
|
-
void load(['', ...tree.expanded]);
|
|
109
|
-
}, [load, changed]);
|
|
110
|
-
|
|
111
|
-
// The directories open when this directory was shown last, in this browser or another.
|
|
112
|
-
React.useEffect(() => {
|
|
113
|
-
if (tree.restored) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
let live = true;
|
|
117
|
-
pi.storage
|
|
118
|
-
.directory(root)
|
|
119
|
-
.get<unknown>(EXPANDED_KEY)
|
|
120
|
-
.then(
|
|
121
|
-
kept => {
|
|
122
|
-
const paths = WorkspaceUtil.paths(kept);
|
|
123
|
-
if (live) {
|
|
124
|
-
setTree(previous => ({ ...previous, restored: true, expanded: new Set([...previous.expanded, ...paths]) }));
|
|
125
|
-
void load(paths);
|
|
126
|
-
}
|
|
127
|
-
},
|
|
128
|
-
() => live && setTree(previous => ({ ...previous, restored: true })),
|
|
129
|
-
);
|
|
130
|
-
return () => {
|
|
131
|
-
live = false;
|
|
132
|
-
};
|
|
133
|
-
}, [root]);
|
|
134
|
-
|
|
135
|
-
const expand = (change: (expanded: Set<string>) => Set<string>) => {
|
|
136
|
-
const expanded = change(tree.expanded);
|
|
137
|
-
setTree(previous => ({ ...previous, expanded: change(previous.expanded) }));
|
|
138
|
-
if (tree.restored) {
|
|
139
|
-
pi.storage
|
|
140
|
-
.directory(root)
|
|
141
|
-
.set(EXPANDED_KEY, [...expanded])
|
|
142
|
-
.catch(() => undefined);
|
|
143
|
-
}
|
|
144
|
-
return expanded;
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
/** Shows a path: the folders around it open, it selected and scrolled to. */
|
|
148
|
-
const reveal = (path: string) => {
|
|
149
|
-
const expanded = expand(previous => WorkspaceUtil.reveal(previous, path));
|
|
150
|
-
void load([...expanded].filter(dir => !tree.children.has(dir) && WorkspaceUtil.within(path, dir)));
|
|
151
|
-
setSelected(path);
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
// The file shown in the workbench is the one selected, as an IDE follows the editor.
|
|
155
|
-
React.useEffect(() => {
|
|
156
|
-
const prefix = fileId(root, '');
|
|
157
|
-
if (active?.startsWith(prefix)) {
|
|
158
|
-
reveal(active.slice(prefix.length));
|
|
159
|
-
}
|
|
160
|
-
}, [active, root]);
|
|
161
|
-
|
|
162
|
-
ShowDirectory.useReceive(directory => {
|
|
163
|
-
setRoot(directory);
|
|
164
|
-
setSelected(undefined);
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
RevealRequests.useReceive(request => {
|
|
168
|
-
if (request.cwd === root || request.cwd === cwd) {
|
|
169
|
-
if (request.cwd !== root) {
|
|
170
|
-
setRoot(request.cwd);
|
|
171
|
-
}
|
|
172
|
-
reveal(request.path);
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
React.useEffect(() => {
|
|
177
|
-
if (selected) {
|
|
178
|
-
list.current?.querySelector<HTMLElement>(`[data-path="${CSS.escape(selected)}"]`)?.scrollIntoView({ block: 'nearest' });
|
|
179
|
-
}
|
|
180
|
-
}, [selected]);
|
|
181
|
-
|
|
182
|
-
const toggle = (entry: TaDirEntry) => {
|
|
183
|
-
const opening = !tree.expanded.has(entry.path);
|
|
184
|
-
expand(expanded => WorkspaceUtil.toggle(expanded, entry.path));
|
|
185
|
-
if (opening && !tree.children.has(entry.path)) {
|
|
186
|
-
void load([entry.path]);
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
const entryOf = (path: string): TaDirEntry | undefined => tree.children.get(WorkspaceUtil.dirname(path))?.find(entry => entry.path === path);
|
|
191
|
-
|
|
192
|
-
/** Where a new file goes: the folder selected, or the folder of the file selected. */
|
|
193
|
-
const targetFolder = (path = selected): string => {
|
|
194
|
-
const entry = path === undefined ? undefined : entryOf(path);
|
|
195
|
-
return !entry ? '' : entry.kind === 'directory' ? entry.path : WorkspaceUtil.dirname(entry.path);
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
const startCreate = (directory: boolean, parent = targetFolder()) => {
|
|
199
|
-
if (parent) {
|
|
200
|
-
expand(previous => WorkspaceUtil.reveal(new Set([...previous, parent]), parent));
|
|
201
|
-
if (!tree.children.has(parent)) {
|
|
202
|
-
void load([parent]);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
setEditing({ kind: 'create', parent, directory });
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
const siblings = (dir: string, except?: string) => (tree.children.get(dir) ?? []).filter(entry => entry.path !== except).map(entry => entry.name);
|
|
209
|
-
|
|
210
|
-
const validate = (dir: string, except?: string) => (name: string) => {
|
|
211
|
-
const problem = WorkspaceUtil.validateName(name, siblings(dir, except));
|
|
212
|
-
return problem && { message: t(problem.key, { name }), severity: problem.severity };
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
const finishCreate = async (parent: string, directory: boolean, name: string | undefined) => {
|
|
216
|
-
setEditing(undefined);
|
|
217
|
-
if (!name) {
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
const path = WorkspaceUtil.join(parent, name.replace(/\/+$/, ''));
|
|
221
|
-
try {
|
|
222
|
-
if (directory) {
|
|
223
|
-
await WorkspaceClient.createFolder(root, path);
|
|
224
|
-
} else {
|
|
225
|
-
await WorkspaceClient.createFile(root, path);
|
|
226
|
-
}
|
|
227
|
-
await load([parent, ...[...tree.expanded].filter(dir => WorkspaceUtil.within(dir, parent))]);
|
|
228
|
-
reveal(path);
|
|
229
|
-
if (!directory) {
|
|
230
|
-
openFile(root, path);
|
|
231
|
-
}
|
|
232
|
-
FilesChanged.notify();
|
|
233
|
-
} catch (error) {
|
|
234
|
-
pi.ui.notify(t('createFailed', { message: Host.message(error) }), 'error');
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
const finishRename = async (from: string, name: string | undefined) => {
|
|
239
|
-
setEditing(undefined);
|
|
240
|
-
if (!name) {
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
const dir = WorkspaceUtil.dirname(from);
|
|
244
|
-
const to = WorkspaceUtil.join(dir, name);
|
|
245
|
-
const reopen = OpenFiles.list().filter(file => file.kind === 'file' && file.cwd === root && WorkspaceUtil.within(file.detail, from));
|
|
246
|
-
try {
|
|
247
|
-
await WorkspaceClient.rename(root, from, to);
|
|
248
|
-
closeEditorsOf(root, from);
|
|
249
|
-
for (const file of reopen) {
|
|
250
|
-
openFile(root, to + file.detail.slice(from.length), undefined, { preserveFocus: true });
|
|
251
|
-
}
|
|
252
|
-
expand(previous => new Set([...previous].map(path => (WorkspaceUtil.within(path, from) ? to + path.slice(from.length) : path))));
|
|
253
|
-
await load([dir]);
|
|
254
|
-
setSelected(to);
|
|
255
|
-
FilesChanged.notify();
|
|
256
|
-
} catch (error) {
|
|
257
|
-
pi.ui.notify(t('renameFailed', { message: Host.message(error) }), 'error');
|
|
258
|
-
}
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
const remove = async (entry: TaDirEntry) => {
|
|
262
|
-
const confirmed = await pi.ui.confirm(t(entry.kind === 'directory' ? 'deleteFolder' : 'deleteFile', { name: entry.name }), t('irreversible'), {
|
|
263
|
-
confirmLabel: t('delete'),
|
|
264
|
-
danger: true,
|
|
265
|
-
});
|
|
266
|
-
if (!confirmed) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
try {
|
|
270
|
-
await WorkspaceClient.remove(root, entry.path);
|
|
271
|
-
closeEditorsOf(root, entry.path);
|
|
272
|
-
setSelected(previous => (previous && WorkspaceUtil.within(previous, entry.path) ? undefined : previous));
|
|
273
|
-
await load([WorkspaceUtil.dirname(entry.path)]);
|
|
274
|
-
FilesChanged.notify();
|
|
275
|
-
} catch (error) {
|
|
276
|
-
pi.ui.notify(t('deleteFailed', { message: Host.message(error) }), 'error');
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
/** Reads a folder again, and those open inside it. */
|
|
281
|
-
const reloadFolder = async (folder: string) => {
|
|
282
|
-
await load([folder, ...[...tree.expanded].filter(dir => dir !== folder && WorkspaceUtil.within(dir, folder))]);
|
|
283
|
-
FilesChanged.notify();
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
/** The names of `names` that `folder` holds already, as far as it was read. */
|
|
287
|
-
const taken = (folder: string, names: string[]) => {
|
|
288
|
-
const there = siblings(folder).map(name => name.toLowerCase());
|
|
289
|
-
return names.filter(name => there.includes(name.toLowerCase()));
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
const upload = async (folder: string, pending: Promise<UploadBatch>) => {
|
|
293
|
-
let batch: UploadBatch;
|
|
294
|
-
try {
|
|
295
|
-
batch = await pending;
|
|
296
|
-
} catch (error) {
|
|
297
|
-
pi.ui.notify(t('uploadFailed', { message: Host.message(error) }), 'error');
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
if (!batch.files.length && !batch.folders.length) {
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
const replaced = taken(folder, UploadUtil.topNames(batch));
|
|
304
|
-
if (replaced.length && !(await pi.ui.confirm(t('replaceTitle', { names: replaced.join(', ') }), t('replaceMessage'), { confirmLabel: t('replace') }))) {
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
if (folder) {
|
|
308
|
-
expand(previous => WorkspaceUtil.reveal(new Set([...previous, folder]), folder));
|
|
309
|
-
}
|
|
310
|
-
setTransfer({ label: t('uploading', { name: '' }), done: 0, total: UploadUtil.totalBytes(batch) });
|
|
311
|
-
try {
|
|
312
|
-
const count = await WorkspaceClient.upload(root, folder, batch, (done, total, name) =>
|
|
313
|
-
setTransfer({ label: t('uploading', { name }), done, total }),
|
|
314
|
-
);
|
|
315
|
-
pi.ui.notify(t('uploaded', { count }));
|
|
316
|
-
} catch (error) {
|
|
317
|
-
pi.ui.notify(t('uploadFailed', { message: Host.message(error) }), 'error');
|
|
318
|
-
} finally {
|
|
319
|
-
setTransfer(undefined);
|
|
320
|
-
await reloadFolder(folder);
|
|
321
|
-
}
|
|
322
|
-
};
|
|
323
|
-
|
|
324
|
-
const pickFiles = (folder: string, folders: boolean) => {
|
|
325
|
-
pickedFolder.current = folder;
|
|
326
|
-
(folders ? folderPicker : filePicker).current?.click();
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
const onPicked = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
330
|
-
const files = Array.from(event.target.files ?? []);
|
|
331
|
-
event.target.value = '';
|
|
332
|
-
if (files.length) {
|
|
333
|
-
void upload(pickedFolder.current, Promise.resolve(UploadUtil.fromFiles(files)));
|
|
334
|
-
}
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
const download = async (entry: Pick<TaDirEntry, 'path' | 'name' | 'kind'>) => {
|
|
338
|
-
try {
|
|
339
|
-
if (entry.kind === 'directory') {
|
|
340
|
-
setTransfer({ label: t('packing', { name: entry.name }), done: 0, total: 0 });
|
|
341
|
-
}
|
|
342
|
-
await WorkspaceClient.download(root, entry, (done, total) => setTransfer({ label: t('packing', { name: entry.name }), done, total }));
|
|
343
|
-
} catch (error) {
|
|
344
|
-
pi.ui.notify(t('downloadFailed', { message: Host.message(error) }), 'error');
|
|
345
|
-
} finally {
|
|
346
|
-
setTransfer(undefined);
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
/** Moves into `folder` what can go there, and what was open of it follows. */
|
|
351
|
-
const move = async (paths: string[], folder: string) => {
|
|
352
|
-
const moving = WorkspaceUtil.movable(paths, folder);
|
|
353
|
-
if (!moving.length) {
|
|
354
|
-
return;
|
|
355
|
-
}
|
|
356
|
-
const clash = taken(folder, moving.map(WorkspaceUtil.basename));
|
|
357
|
-
if (clash.length) {
|
|
358
|
-
pi.ui.notify(t('moveExists', { name: clash[0] }), 'error');
|
|
359
|
-
return;
|
|
360
|
-
}
|
|
361
|
-
const reopen = OpenFiles.list().filter(
|
|
362
|
-
file => file.kind === 'file' && file.cwd === root && moving.some(path => WorkspaceUtil.within(file.detail, path)),
|
|
363
|
-
);
|
|
364
|
-
try {
|
|
365
|
-
await WorkspaceClient.move(root, moving, folder);
|
|
366
|
-
for (const path of moving) {
|
|
367
|
-
closeEditorsOf(root, path);
|
|
368
|
-
}
|
|
369
|
-
for (const file of reopen) {
|
|
370
|
-
const to = WorkspaceUtil.movedTo(file.detail, moving, folder);
|
|
371
|
-
if (to) {
|
|
372
|
-
openFile(root, to, undefined, { preserveFocus: true });
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
expand(previous => new Set([...previous].map(path => WorkspaceUtil.movedTo(path, moving, folder) ?? path)));
|
|
376
|
-
if (folder) {
|
|
377
|
-
expand(previous => WorkspaceUtil.reveal(new Set([...previous, folder]), folder));
|
|
378
|
-
}
|
|
379
|
-
await load([...new Set([folder, ...moving.map(WorkspaceUtil.dirname)])]);
|
|
380
|
-
setSelected(WorkspaceUtil.movedTo(moving[0], moving, folder));
|
|
381
|
-
FilesChanged.notify();
|
|
382
|
-
} catch (error) {
|
|
383
|
-
pi.ui.notify(t('moveFailed', { message: Host.message(error) }), 'error');
|
|
384
|
-
}
|
|
385
|
-
};
|
|
386
|
-
|
|
387
|
-
const showInFileManager = (path: string) => {
|
|
388
|
-
desktop?.showItemInFolder(absolute(path)).catch(error => pi.ui.notify(t('revealFailed', { message: Host.message(error) }), 'error'));
|
|
389
|
-
};
|
|
390
|
-
|
|
391
|
-
const stopDragOpen = () => {
|
|
392
|
-
clearTimeout(dragOpen.current?.timer);
|
|
393
|
-
dragOpen.current = undefined;
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
/** A drag over `folder` (`entry` the row under it, a closed folder of which opens after a moment). */
|
|
397
|
-
const dragOver = (event: React.DragEvent, folder: string, entry?: TaDirEntry) => {
|
|
398
|
-
if (!acceptsDrop(event.dataTransfer)) {
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
event.preventDefault();
|
|
402
|
-
event.stopPropagation();
|
|
403
|
-
event.dataTransfer.dropEffect = event.dataTransfer.types.includes('Files') ? 'copy' : 'move';
|
|
404
|
-
if (dropTarget !== folder) {
|
|
405
|
-
setDropTarget(folder);
|
|
406
|
-
}
|
|
407
|
-
const closed = entry?.kind === 'directory' && !tree.expanded.has(entry.path) ? entry : undefined;
|
|
408
|
-
if (dragOpen.current?.path !== closed?.path) {
|
|
409
|
-
stopDragOpen();
|
|
410
|
-
if (closed) {
|
|
411
|
-
dragOpen.current = { path: closed.path, timer: setTimeout(() => toggle(closed), DRAG_OPEN_MS) };
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
const drop = (event: React.DragEvent, folder: string) => {
|
|
417
|
-
if (!acceptsDrop(event.dataTransfer)) {
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
event.preventDefault();
|
|
421
|
-
event.stopPropagation();
|
|
422
|
-
setDropTarget(undefined);
|
|
423
|
-
stopDragOpen();
|
|
424
|
-
if (event.dataTransfer.types.includes('Files')) {
|
|
425
|
-
// Read now: the browser empties a drop once its event returns.
|
|
426
|
-
void upload(folder, UploadUtil.fromDrop(event.dataTransfer));
|
|
427
|
-
return;
|
|
428
|
-
}
|
|
429
|
-
const dragged = pi.ui.fileDrag.get(event.dataTransfer);
|
|
430
|
-
if (dragged?.cwd === root) {
|
|
431
|
-
void move(dragged.paths, folder);
|
|
432
|
-
}
|
|
433
|
-
};
|
|
434
|
-
|
|
435
|
-
const absolute = (path: string) => (path ? `${root.replace(/\/+$/, '')}/${path}` : root);
|
|
436
|
-
|
|
437
|
-
const findInFolder = (path: string) => {
|
|
438
|
-
SearchRequests.send({ cwd: root, include: path ? `${path}/**` : '' });
|
|
439
|
-
pi.ui.showView(`${PACKAGE}/search`);
|
|
440
|
-
};
|
|
441
|
-
|
|
442
|
-
const menuFor = (entry?: TaDirEntry): MenuItem[] => {
|
|
443
|
-
const folder = entry ? (entry.kind === 'directory' ? entry.path : WorkspaceUtil.dirname(entry.path)) : '';
|
|
444
|
-
const items: MenuItem[] = [
|
|
445
|
-
{ label: t('newFile'), run: () => startCreate(false, folder) },
|
|
446
|
-
{ label: t('newFolder'), run: () => startCreate(true, folder) },
|
|
447
|
-
];
|
|
448
|
-
const fileManager: MenuItem[] =
|
|
449
|
-
desktop && showsInFileManager
|
|
450
|
-
? [{ label: t(WorkspaceUtil.fileManagerLabel(desktop.platform)), run: () => showInFileManager(entry?.path ?? '') }]
|
|
451
|
-
: [];
|
|
452
|
-
const transfers: MenuItem[] = [
|
|
453
|
-
{ label: t('uploadFiles'), run: () => pickFiles(folder, false) },
|
|
454
|
-
{ label: t('uploadFolder'), run: () => pickFiles(folder, true) },
|
|
455
|
-
{
|
|
456
|
-
label: t('download'),
|
|
457
|
-
run: () => void download(entry ?? { path: '', name: WorkspaceUtil.basename(TaLocation.path(root)) || 'workspace', kind: 'directory' }),
|
|
458
|
-
},
|
|
459
|
-
];
|
|
460
|
-
if (!entry) {
|
|
461
|
-
return [
|
|
462
|
-
...items,
|
|
463
|
-
'-',
|
|
464
|
-
...fileManager,
|
|
465
|
-
{ label: t('findInFolder'), run: () => findInFolder('') },
|
|
466
|
-
'-',
|
|
467
|
-
...transfers,
|
|
468
|
-
'-',
|
|
469
|
-
{ label: t('copyPath'), run: () => copyText(root, t('copied')) },
|
|
470
|
-
{ label: t('refresh'), run: () => void load(['', ...tree.expanded]) },
|
|
471
|
-
];
|
|
472
|
-
}
|
|
473
|
-
return [
|
|
474
|
-
...items,
|
|
475
|
-
'-',
|
|
476
|
-
...(entry.kind === 'file' ? [{ label: t('open'), run: () => openFile(root, entry.path) }] : []),
|
|
477
|
-
...fileManager,
|
|
478
|
-
...(entry.kind === 'directory' ? [{ label: t('findInFolder'), run: () => findInFolder(entry.path) }] : []),
|
|
479
|
-
'-',
|
|
480
|
-
...transfers,
|
|
481
|
-
'-',
|
|
482
|
-
{ label: t('copyPath'), keys: MAC ? '⌥⌘C' : 'Shift+Alt+C', run: () => copyText(absolute(entry.path), t('copied')) },
|
|
483
|
-
{ label: t('copyRelativePath'), run: () => copyText(entry.path, t('copied')) },
|
|
484
|
-
'-',
|
|
485
|
-
{ label: t('rename'), keys: MAC ? '↵' : 'F2', run: () => setEditing({ kind: 'rename', path: entry.path }) },
|
|
486
|
-
{ label: t('delete'), keys: MAC ? '⌘⌫' : 'Delete', run: () => void remove(entry) },
|
|
487
|
-
];
|
|
488
|
-
};
|
|
489
|
-
|
|
490
|
-
const rows = WorkspaceUtil.rows(tree.children, tree.expanded);
|
|
491
|
-
const activate = (entry: TaDirEntry, pin: boolean) => {
|
|
492
|
-
setSelected(entry.path);
|
|
493
|
-
if (entry.kind === 'directory') {
|
|
494
|
-
if (!pin) {
|
|
495
|
-
toggle(entry);
|
|
496
|
-
}
|
|
497
|
-
} else {
|
|
498
|
-
openFile(root, entry.path, undefined, { preview: !pin });
|
|
499
|
-
}
|
|
500
|
-
};
|
|
501
|
-
|
|
502
|
-
const onKey = (event: React.KeyboardEvent, entry: TaDirEntry, expanded: boolean) => {
|
|
503
|
-
const row = event.currentTarget as HTMLElement;
|
|
504
|
-
const move = (to: Element | null | undefined) => {
|
|
505
|
-
const path = (to as HTMLElement | null)?.dataset.path;
|
|
506
|
-
if (path !== undefined) {
|
|
507
|
-
setSelected(path);
|
|
508
|
-
(to as HTMLElement).focus();
|
|
509
|
-
}
|
|
510
|
-
};
|
|
511
|
-
const all = () => [...(list.current?.querySelectorAll<HTMLElement>('[data-path]') ?? [])];
|
|
512
|
-
if (event.key === 'ArrowRight' && entry.kind === 'directory') {
|
|
513
|
-
event.preventDefault();
|
|
514
|
-
if (!expanded) {
|
|
515
|
-
toggle(entry);
|
|
516
|
-
} else {
|
|
517
|
-
move(row.nextElementSibling);
|
|
518
|
-
}
|
|
519
|
-
} else if (event.key === 'ArrowLeft') {
|
|
520
|
-
event.preventDefault();
|
|
521
|
-
if (expanded) {
|
|
522
|
-
toggle(entry);
|
|
523
|
-
} else {
|
|
524
|
-
move(list.current?.querySelector(`[data-path="${CSS.escape(WorkspaceUtil.dirname(entry.path))}"]`));
|
|
525
|
-
}
|
|
526
|
-
} else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
527
|
-
event.preventDefault();
|
|
528
|
-
move(event.key === 'ArrowDown' ? row.nextElementSibling : row.previousElementSibling);
|
|
529
|
-
} else if (event.key === 'Home' || event.key === 'End') {
|
|
530
|
-
event.preventDefault();
|
|
531
|
-
const rowsShown = all();
|
|
532
|
-
move(event.key === 'Home' ? rowsShown[0] : rowsShown[rowsShown.length - 1]);
|
|
533
|
-
} else if (event.key === 'Enter' && MAC) {
|
|
534
|
-
// As Finder and VS Code on a Mac: Enter renames, Cmd+Down opens.
|
|
535
|
-
event.preventDefault();
|
|
536
|
-
setEditing({ kind: 'rename', path: entry.path });
|
|
537
|
-
} else if (event.key === 'Enter' || (event.key === 'ArrowDown' && event.metaKey) || event.key === ' ') {
|
|
538
|
-
event.preventDefault();
|
|
539
|
-
activate(entry, true);
|
|
540
|
-
} else if (event.key === 'F2') {
|
|
541
|
-
event.preventDefault();
|
|
542
|
-
setEditing({ kind: 'rename', path: entry.path });
|
|
543
|
-
} else if (event.key === 'Delete' || (event.key === 'Backspace' && event.metaKey)) {
|
|
544
|
-
event.preventDefault();
|
|
545
|
-
void remove(entry);
|
|
546
|
-
} else if (event.key === 'c' && event.altKey && (event.metaKey || event.shiftKey)) {
|
|
547
|
-
event.preventDefault();
|
|
548
|
-
copyText(absolute(entry.path), t('copied'));
|
|
549
|
-
} else if (event.key === 'ContextMenu' || (event.key === 'F10' && event.shiftKey)) {
|
|
550
|
-
openMenu(event, menuFor(entry));
|
|
551
|
-
}
|
|
552
|
-
};
|
|
553
|
-
|
|
554
|
-
const creating = editing?.kind === 'create' ? editing : undefined;
|
|
555
|
-
const createRow = (depth: number) =>
|
|
556
|
-
creating && (
|
|
557
|
-
<div key="taw-create" className="taw-row taw-tree-row taw-editing" style={{ paddingLeft: 8 + depth * INDENT }}>
|
|
558
|
-
<Guides depth={depth} />
|
|
559
|
-
<span className="taw-twisty" />
|
|
560
|
-
{creating.directory ? <Glyph name="Folder" /> : <FileIcon path="" />}
|
|
561
|
-
<NameInput initial="" validate={validate(creating.parent)} onDone={name => void finishCreate(creating.parent, creating.directory, name)} />
|
|
562
|
-
</div>
|
|
563
|
-
);
|
|
564
|
-
|
|
565
|
-
const shown: React.ReactNode[] = [];
|
|
566
|
-
if (creating && creating.parent === '') {
|
|
567
|
-
shown.push(createRow(0));
|
|
568
|
-
}
|
|
569
|
-
for (const { entry, depth, expanded, loading } of rows) {
|
|
570
|
-
const renaming = editing?.kind === 'rename' && editing.path === entry.path;
|
|
571
|
-
const letter = entry.kind === 'directory' ? decorations.folders.get(entry.path) : decorations.files.get(entry.path);
|
|
572
|
-
const ignored = !letter && decorations.ignored.length > 0 && GitUtil.ignored(decorations, entry.path);
|
|
573
|
-
const color = WorkspaceUtil.gitColor(letter);
|
|
574
|
-
shown.push(
|
|
575
|
-
<div
|
|
576
|
-
key={entry.path}
|
|
577
|
-
role="treeitem"
|
|
578
|
-
tabIndex={selected === entry.path || (!selected && shown.length === 0) ? 0 : -1}
|
|
579
|
-
data-path={entry.path}
|
|
580
|
-
aria-expanded={entry.kind === 'directory' ? expanded : undefined}
|
|
581
|
-
aria-level={depth + 1}
|
|
582
|
-
aria-selected={selected === entry.path}
|
|
583
|
-
className={`taw-row taw-tree-row ${selected === entry.path ? 'taw-selected' : ''} ${ignored ? 'taw-ignored' : ''} ${letter === 'D' ? 'taw-deleted' : ''} ${dropTarget && WorkspaceUtil.within(entry.path, dropTarget) ? 'taw-drop' : ''}`}
|
|
584
|
-
style={{ paddingLeft: 8 + depth * INDENT }}
|
|
585
|
-
title={letter ? `${entry.path} • ${gitLabel(letter)}` : entry.path}
|
|
586
|
-
onClick={() => !renaming && activate(entry, false)}
|
|
587
|
-
onDoubleClick={() => !renaming && activate(entry, true)}
|
|
588
|
-
onKeyDown={event => !renaming && onKey(event, entry, expanded)}
|
|
589
|
-
onContextMenu={event => {
|
|
590
|
-
setSelected(entry.path);
|
|
591
|
-
openMenu(event, menuFor(entry));
|
|
592
|
-
}}
|
|
593
|
-
draggable={!renaming}
|
|
594
|
-
onDragStart={event => {
|
|
595
|
-
setSelected(entry.path);
|
|
596
|
-
event.dataTransfer.effectAllowed = 'copyMove';
|
|
597
|
-
pi.ui.fileDrag.set(event.dataTransfer, { cwd: root, paths: [entry.path] });
|
|
598
|
-
}}
|
|
599
|
-
onDragOver={event => dragOver(event, entry.kind === 'directory' ? entry.path : WorkspaceUtil.dirname(entry.path), entry)}
|
|
600
|
-
onDrop={event => drop(event, entry.kind === 'directory' ? entry.path : WorkspaceUtil.dirname(entry.path))}
|
|
601
|
-
>
|
|
602
|
-
<Guides depth={depth} />
|
|
603
|
-
<span className="taw-twisty">
|
|
604
|
-
{entry.kind === 'directory' && <Glyph name={loading ? 'LoaderCircle' : expanded ? 'ChevronDown' : 'ChevronRight'} size={14} />}
|
|
605
|
-
</span>
|
|
606
|
-
{entry.kind === 'directory' ? <Glyph name={expanded ? 'FolderOpen' : 'Folder'} /> : <FileIcon path={entry.name} />}
|
|
607
|
-
{renaming ? (
|
|
608
|
-
<NameInput
|
|
609
|
-
initial={entry.name}
|
|
610
|
-
selectBase={entry.kind === 'file'}
|
|
611
|
-
validate={validate(WorkspaceUtil.dirname(entry.path), entry.path)}
|
|
612
|
-
onDone={name => void finishRename(entry.path, name)}
|
|
613
|
-
/>
|
|
614
|
-
) : (
|
|
615
|
-
<React.Fragment>
|
|
616
|
-
<span className="taw-name" style={color ? { color } : undefined}>
|
|
617
|
-
{entry.name}
|
|
618
|
-
</span>
|
|
619
|
-
{letter && (
|
|
620
|
-
<span className="taw-letter" style={{ color }}>
|
|
621
|
-
{entry.kind === 'directory' ? '•' : letter}
|
|
622
|
-
</span>
|
|
623
|
-
)}
|
|
624
|
-
</React.Fragment>
|
|
625
|
-
)}
|
|
626
|
-
</div>,
|
|
627
|
-
);
|
|
628
|
-
if (creating && creating.parent === entry.path && expanded) {
|
|
629
|
-
shown.push(createRow(depth + 1));
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
return (
|
|
634
|
-
<div className="taw-view">
|
|
635
|
-
<ViewHeader title={WorkspaceUtil.basename(root) || t('explorer')} hint={root}>
|
|
636
|
-
{root !== cwd && (
|
|
637
|
-
<Action icon="RotateCcw" label={t('followConversation', { name: WorkspaceUtil.basename(cwd) || cwd })} onClick={() => setRoot(cwd)} />
|
|
638
|
-
)}
|
|
639
|
-
<Action icon="FilePlus" label={t('newFile')} onClick={() => startCreate(false)} />
|
|
640
|
-
<Action icon="FolderPlus" label={t('newFolder')} onClick={() => startCreate(true)} />
|
|
641
|
-
<Action icon="RefreshCw" label={t('refresh')} onClick={() => void load(['', ...tree.expanded])} />
|
|
642
|
-
<Action icon="ChevronsDownUp" label={t('collapseAll')} onClick={() => expand(() => new Set())} />
|
|
643
|
-
<Action icon="FolderOpen" label={t('changeFolder')} onClick={() => setPicking(true)} />
|
|
644
|
-
</ViewHeader>
|
|
645
|
-
<div
|
|
646
|
-
ref={list}
|
|
647
|
-
className={`taw-list taw-tree ${dropTarget === '' ? 'taw-drop-root' : ''}`}
|
|
648
|
-
role="tree"
|
|
649
|
-
aria-label={t('explorer')}
|
|
650
|
-
onContextMenu={event => event.target === event.currentTarget && openMenu(event, menuFor())}
|
|
651
|
-
onClick={event => event.target === event.currentTarget && setSelected(undefined)}
|
|
652
|
-
onKeyDown={event => event.key === 'Escape' && setSelected(undefined)}
|
|
653
|
-
onDragOver={event => dragOver(event, '')}
|
|
654
|
-
onDrop={event => drop(event, '')}
|
|
655
|
-
onDragLeave={event => {
|
|
656
|
-
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) {
|
|
657
|
-
setDropTarget(undefined);
|
|
658
|
-
stopDragOpen();
|
|
659
|
-
}
|
|
660
|
-
}}
|
|
661
|
-
onDragEnd={() => {
|
|
662
|
-
setDropTarget(undefined);
|
|
663
|
-
stopDragOpen();
|
|
664
|
-
}}
|
|
665
|
-
>
|
|
666
|
-
{tree.error && <Empty tone="error">{t('failed', { message: tree.error })}</Empty>}
|
|
667
|
-
{!tree.error && !tree.children.has('') && <Empty>{t('loading')}</Empty>}
|
|
668
|
-
{!tree.error && tree.children.get('')?.length === 0 && !creating && <Empty>{t('empty')}</Empty>}
|
|
669
|
-
{shown}
|
|
670
|
-
</div>
|
|
671
|
-
{transfer && (
|
|
672
|
-
<div className="taw-transfer" role="status">
|
|
673
|
-
<span>{transfer.label}</span>
|
|
674
|
-
{transfer.total > 0 && <span className="taw-detail">{Math.floor((transfer.done / transfer.total) * 100)}%</span>}
|
|
675
|
-
<span className="taw-transfer-bar" style={{ width: transfer.total > 0 ? `${(transfer.done / transfer.total) * 100}%` : '0%' }} />
|
|
676
|
-
</div>
|
|
677
|
-
)}
|
|
678
|
-
<input ref={filePicker} type="file" multiple hidden onChange={onPicked} />
|
|
679
|
-
<input
|
|
680
|
-
ref={element => {
|
|
681
|
-
folderPicker.current = element;
|
|
682
|
-
// A folder picker: not an attribute React knows.
|
|
683
|
-
element?.setAttribute('webkitdirectory', '');
|
|
684
|
-
}}
|
|
685
|
-
type="file"
|
|
686
|
-
hidden
|
|
687
|
-
onChange={onPicked}
|
|
688
|
-
/>
|
|
689
|
-
{menu}
|
|
690
|
-
{picking && (
|
|
691
|
-
<FolderPicker
|
|
692
|
-
initial={root}
|
|
693
|
-
onClose={() => setPicking(false)}
|
|
694
|
-
onPick={path => {
|
|
695
|
-
setRoot(path);
|
|
696
|
-
setSelected(undefined);
|
|
697
|
-
setPicking(false);
|
|
698
|
-
}}
|
|
699
|
-
/>
|
|
700
|
-
)}
|
|
701
|
-
</div>
|
|
702
|
-
);
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
/** The lines of the folders a row is in, as an IDE draws them while the pointer is over the tree. */
|
|
706
|
-
function Guides({ depth }: { depth: number }) {
|
|
707
|
-
if (!depth) {
|
|
708
|
-
return null;
|
|
709
|
-
}
|
|
710
|
-
return (
|
|
711
|
-
<span className="taw-guides" aria-hidden="true">
|
|
712
|
-
{Array.from({ length: depth }, (_, index) => (
|
|
713
|
-
<span key={index} className="taw-guide" style={{ left: 15 + index * INDENT }} />
|
|
714
|
-
))}
|
|
715
|
-
</span>
|
|
716
|
-
);
|
|
717
|
-
}
|