@kubohiroya/turbowarp-plus-editor-project-folder 0.9.2 → 0.10.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.ja.md +2 -1
- package/README.md +2 -1
- package/dist/explorer-state.d.ts +22 -0
- package/dist/explorer.d.ts +5 -0
- package/dist/explorer.js +283 -74
- package/dist/file-preview.d.ts +14 -4
- package/dist/index.d.ts +6 -1
- package/dist/index.js +99 -12
- package/dist/session.d.ts +18 -0
- package/dist/{shared-Dco5rGKE.js → shared-teFlHQ2X.js} +15 -4
- package/dist/{shared-Dz9dzxQF.js → shared-zlHgCKDG.js} +33 -1
- package/dist/text-file-editor.d.ts +5 -0
- package/package.json +1 -1
- package/src/explorer-files.ts +2 -1
- package/src/explorer-menu.tsx +1 -1
- package/src/explorer-state.ts +50 -0
- package/src/explorer.tsx +191 -43
- package/src/file-preview.ts +22 -4
- package/src/index.ts +6 -1
- package/src/session.ts +112 -10
- package/src/text-file-editor.ts +16 -3
package/src/explorer.tsx
CHANGED
|
@@ -5,6 +5,8 @@ import {useTree} from '@headless-tree/react/react17';
|
|
|
5
5
|
import {FolderError, isObject, manifestFile, projectFile} from './source.js';
|
|
6
6
|
import {projectFolderOverwriteWarning} from './create-folder.js';
|
|
7
7
|
import {followProjectFolder} from './folder-registry.js';
|
|
8
|
+
import {hiddenEntry, readExplorerState, subscribeExplorerState, writeExplorerState} from './explorer-state.js';
|
|
9
|
+
import type {ExplorerTreeState} from './explorer-state.js';
|
|
8
10
|
import type {ExplorerMutation, FollowedFolder} from './folder-registry.js';
|
|
9
11
|
import type {Json, Project} from './source.js';
|
|
10
12
|
import {canMoveExplorerEntry, copyExplorerFile, createExplorerDirectory, deleteExplorerEntry, ExplorerFileError,
|
|
@@ -39,6 +41,8 @@ export type ProjectFolderExplorerProps = {
|
|
|
39
41
|
unsavedPaths?: ReadonlySet<string>;
|
|
40
42
|
/** Changes after the folder contents have been saved or reloaded. */
|
|
41
43
|
refreshKey?: number;
|
|
44
|
+
/** Case-insensitive search by file name or relative path. */
|
|
45
|
+
query?: string;
|
|
42
46
|
/** Called when a file is opened with Enter, Space or a click. */
|
|
43
47
|
onOpen?(entry: ExplorerEntry): void;
|
|
44
48
|
/** The tree's accessible name. Defaults to "Project folder". */
|
|
@@ -51,15 +55,16 @@ export type ProjectFolderExplorerProps = {
|
|
|
51
55
|
|
|
52
56
|
const messages = {
|
|
53
57
|
en: {
|
|
54
|
-
label: 'Project folder', loading: 'Loading…', empty: 'This folder is empty.',
|
|
58
|
+
label: 'Project folder', loading: 'Loading…', empty: 'This folder is empty.', noMatches: 'No matching files.',
|
|
59
|
+
results: 'Matching files',
|
|
55
60
|
noFolder: 'Open a project folder to show its files here.', open: 'Open Project Folder…',
|
|
56
61
|
create: 'Create Project Folder', creating: 'Creating project folder…',
|
|
57
62
|
createFailed: 'Could not create the project folder.',
|
|
58
63
|
unavailable: 'File tabs are unavailable in this editor layout.',
|
|
59
64
|
failed: 'This folder could not be read.', denied: 'Permission to read this folder was not granted.',
|
|
60
65
|
separator: ', ',
|
|
61
|
-
marks: {added: '
|
|
62
|
-
unsaved: 'Unsaved changes', staged: '
|
|
66
|
+
marks: {added: 'New file', modified: 'Saved, not committed', deleted: 'Deleted', untracked: 'Untracked'},
|
|
67
|
+
unsaved: 'Unsaved changes', staged: 'Added to Git index',
|
|
63
68
|
revisions: {committed: 'Committed', pushed: 'Pushed', merged: 'Pull request merged'},
|
|
64
69
|
project: 'Project data', manifest: 'Folder manifest', extensions: 'Embedded extensions',
|
|
65
70
|
assetsDirectory: 'Costume and sound files', extensionsDirectory: 'Extension files',
|
|
@@ -69,15 +74,16 @@ const messages = {
|
|
|
69
74
|
stageSound: (name: string) => `Stage · sound ${name}`
|
|
70
75
|
},
|
|
71
76
|
ja: {
|
|
72
|
-
label: '作品フォルダ', loading: '読み込んでいます…', empty: 'このフォルダは空です。',
|
|
77
|
+
label: '作品フォルダ', loading: '読み込んでいます…', empty: 'このフォルダは空です。', noMatches: '該当するファイルはありません。',
|
|
78
|
+
results: '検索結果',
|
|
73
79
|
noFolder: 'プロジェクトフォルダを開くと、ここにファイルを表示します。', open: 'プロジェクトフォルダを開く…',
|
|
74
80
|
create: 'プロジェクトフォルダを作成', creating: 'プロジェクトフォルダを作成しています…',
|
|
75
81
|
createFailed: 'プロジェクトフォルダを作成できませんでした。',
|
|
76
82
|
unavailable: 'このエディターのレイアウトではファイルをタブで開けません。',
|
|
77
83
|
failed: 'このフォルダを読み込めません。', denied: 'このフォルダを読む許可がありません。',
|
|
78
84
|
separator: '、',
|
|
79
|
-
marks: {added: '
|
|
80
|
-
unsaved: '未保存の変更あり', staged: '
|
|
85
|
+
marks: {added: '新しいファイル', modified: '保存済・未コミットの変更', deleted: '削除', untracked: '未追跡'},
|
|
86
|
+
unsaved: '未保存の変更あり', staged: 'Git のインデックスに追加済み',
|
|
81
87
|
revisions: {committed: 'コミット済み', pushed: 'プッシュ済み', merged: 'PR マージ済み'},
|
|
82
88
|
project: '作品データ', manifest: '作品フォルダの目録', extensions: '埋め込み拡張の一覧',
|
|
83
89
|
assetsDirectory: 'コスチュームと音のファイル', extensionsDirectory: '拡張のファイル',
|
|
@@ -136,7 +142,7 @@ export function describeProjectFolder(project: Project, locale?: string, {manife
|
|
|
136
142
|
return descriptions;
|
|
137
143
|
}
|
|
138
144
|
|
|
139
|
-
type ExplorerNode = {name: string; path: string; kind: 'file' | 'directory' | 'error'; message?: string};
|
|
145
|
+
type ExplorerNode = {name: string; path: string; kind: 'file' | 'directory' | 'error'; deleted?: boolean; message?: string};
|
|
140
146
|
type Message = typeof messages.en;
|
|
141
147
|
const rootId = '/';
|
|
142
148
|
// '\0' cannot appear in a file name, so the error row never collides with a real entry.
|
|
@@ -154,6 +160,10 @@ const css = `.${prefix}{font:inherit;color:inherit}
|
|
|
154
160
|
.${prefix}-item{display:grid;grid-template-columns:1em 20px minmax(0,1fr);align-items:center;gap:.2em .4em;
|
|
155
161
|
width:100%;box-sizing:border-box;margin:0;border:0;border-radius:3px;
|
|
156
162
|
padding-block:.15em;padding-inline-end:.4em;background:none;color:inherit;font:inherit;text-align:start;cursor:pointer;white-space:nowrap}
|
|
163
|
+
.${prefix}-search-result{display:block;width:100%;text-align:start;color:inherit;font:inherit;background:transparent;
|
|
164
|
+
border:0;border-radius:3px;padding:.35em .5em;overflow-wrap:anywhere;cursor:pointer}
|
|
165
|
+
.${prefix}-search-result:hover{background:rgba(128,128,128,.12)}
|
|
166
|
+
.${prefix}-search-result:focus-visible{outline:2px solid currentColor;outline-offset:-2px}
|
|
157
167
|
.${prefix}-item:hover{background:rgba(128,128,128,.12)}
|
|
158
168
|
.${prefix}-item[aria-selected="true"]{background:rgba(128,128,128,.24)}
|
|
159
169
|
.${prefix}-item:focus-visible{outline:2px solid currentColor;outline-offset:-2px}
|
|
@@ -165,12 +175,14 @@ background:repeating-conic-gradient(rgba(128,128,128,.22) 0 25%,transparent 0 50
|
|
|
165
175
|
.${prefix}-label{grid-column:3;display:flex;align-items:center;gap:.4em;min-width:0}
|
|
166
176
|
.${prefix}-filename{display:inline-flex;min-width:0}
|
|
167
177
|
.${prefix}-name{overflow:hidden;text-overflow:ellipsis}
|
|
168
|
-
.${prefix}-
|
|
169
|
-
.${prefix}-item[data-
|
|
170
|
-
.${prefix}-
|
|
171
|
-
.${prefix}-
|
|
172
|
-
.${prefix}-
|
|
173
|
-
.${prefix}-
|
|
178
|
+
.${prefix}-marker{flex:none;white-space:pre}
|
|
179
|
+
.${prefix}-item[data-staged] .${prefix}-name, .${prefix}-search-result[data-staged] .${prefix}-name,
|
|
180
|
+
.${prefix}-name[data-git-change="modified"], .${prefix}-name[data-git-change="added"]{font-weight:700}
|
|
181
|
+
.${prefix}-name[data-revision="committed"]{color:#174b9d;color:light-dark(#174b9d,#91c1ff)}
|
|
182
|
+
.${prefix}-name[data-revision="pushed"]{color:#67369a;color:light-dark(#67369a,#dfafff)}
|
|
183
|
+
.${prefix}-name[data-revision="merged"]{color:#17603e;color:light-dark(#17603e,#85e3ad)}
|
|
184
|
+
.${prefix}-name[data-git-change="deleted"]{text-decoration:line-through}
|
|
185
|
+
.${prefix}-item[aria-disabled="true"], .${prefix}-search-result:disabled{cursor:default}
|
|
174
186
|
.${prefix}-description{grid-column:3;min-width:0;padding:.2em .4em;border-radius:3px;background:rgba(128,128,128,.12);
|
|
175
187
|
color:inherit;color:color-mix(in srgb,currentColor 78%,transparent);font-size:.75em;line-height:1.4;
|
|
176
188
|
white-space:normal;overflow-wrap:anywhere}
|
|
@@ -264,6 +276,100 @@ function Icon({kind, path, readFile}: {kind: IconKind; path: string; readFile?:
|
|
|
264
276
|
);
|
|
265
277
|
}
|
|
266
278
|
|
|
279
|
+
function FileStatusMarkers({unsaved, change}: {unsaved?: boolean; change?: ExplorerMark}) {
|
|
280
|
+
return <>
|
|
281
|
+
{unsaved && <span className={`${prefix}-marker`} data-tw-plus-unsaved-marker aria-hidden="true">{' *'}</span>}
|
|
282
|
+
{change === 'untracked' && <span className={`${prefix}-marker`} aria-hidden="true">{' ✨'}</span>}
|
|
283
|
+
</>;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function fileStatusLabels(message: ReturnType<typeof messagesFor>, unsaved: boolean | undefined,
|
|
287
|
+
status: ExplorerFileStatus | undefined, change: ExplorerMark | undefined) {
|
|
288
|
+
return [unsaved && message.unsaved, status?.staged && message.staged,
|
|
289
|
+
status?.revision && message.revisions[status.revision], change && message.marks[change]];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function deletedPaths(fileStatuses: ProjectFolderExplorerProps['fileStatuses'], marks: ProjectFolderExplorerProps['marks']) {
|
|
293
|
+
const paths = new Set<string>();
|
|
294
|
+
for (const [path, mark] of marks ?? []) if (mark === 'deleted') paths.add(path);
|
|
295
|
+
for (const [path, status] of fileStatuses ?? []) {
|
|
296
|
+
if (status.change === 'deleted') paths.add(path);
|
|
297
|
+
else paths.delete(path);
|
|
298
|
+
}
|
|
299
|
+
return paths;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function deletedChildren(paths: ReadonlySet<string>) {
|
|
303
|
+
const folders = new Map<string, Map<string, ExplorerNode>>();
|
|
304
|
+
for (const path of paths) {
|
|
305
|
+
const parts = path.split('/');
|
|
306
|
+
let parent = rootId;
|
|
307
|
+
for (const [index, name] of parts.entries()) {
|
|
308
|
+
const child = parent === rootId ? name : parent + '/' + name;
|
|
309
|
+
const kind = index === parts.length - 1 ? 'file' : 'directory';
|
|
310
|
+
let children = folders.get(parent);
|
|
311
|
+
if (!children) folders.set(parent, children = new Map());
|
|
312
|
+
children.set(child, {name, path: child, kind, deleted: true});
|
|
313
|
+
parent = child;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return folders;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function FileSearch({directory, query, locale, refreshKey, marks, fileStatuses, unsavedPaths, onOpen}:
|
|
320
|
+
ProjectFolderExplorerProps & {query: string}) {
|
|
321
|
+
const message = messagesFor(locale);
|
|
322
|
+
const [index, setIndex] = React.useState<{files: ExplorerEntry[]; failed: boolean}>();
|
|
323
|
+
React.useEffect(() => {
|
|
324
|
+
let current = true;
|
|
325
|
+
setIndex(undefined);
|
|
326
|
+
const files: ExplorerEntry[] = [];
|
|
327
|
+
let failed = false;
|
|
328
|
+
const walk = async (folder: FileSystemDirectoryHandle, base: string) => {
|
|
329
|
+
try {
|
|
330
|
+
for await (const entry of (folder as IterableDirectory).values()) {
|
|
331
|
+
if (!current) return;
|
|
332
|
+
if (hiddenEntry(entry.name)) continue;
|
|
333
|
+
const path = base + entry.name;
|
|
334
|
+
if (entry.kind === 'directory') await walk(entry as FileSystemDirectoryHandle, path + '/');
|
|
335
|
+
else files.push({path, name: entry.name, kind: 'file'});
|
|
336
|
+
}
|
|
337
|
+
} catch {
|
|
338
|
+
failed = true;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
void walk(directory, '').then(() => {
|
|
342
|
+
if (current) setIndex({files, failed});
|
|
343
|
+
});
|
|
344
|
+
return () => {current = false;};
|
|
345
|
+
}, [directory, refreshKey]);
|
|
346
|
+
|
|
347
|
+
const paths = deletedPaths(fileStatuses, marks);
|
|
348
|
+
const files = new Map(index?.files.map(entry => [entry.path, entry]));
|
|
349
|
+
for (const path of paths) {
|
|
350
|
+
if (!files.has(path)) files.set(path, {name: path.split('/').pop()!, path, kind: 'file'});
|
|
351
|
+
}
|
|
352
|
+
const matches = [...files.values()].filter(entry => entry.path.toLowerCase().includes(query))
|
|
353
|
+
.sort((a, b) => a.path.localeCompare(b.path, locale, {numeric: true}));
|
|
354
|
+
return <div className={prefix} role="group" aria-label={message.results}>
|
|
355
|
+
{matches?.map(entry => {
|
|
356
|
+
const unsaved = unsavedPaths?.has(entry.path);
|
|
357
|
+
const status = fileStatuses?.get(entry.path);
|
|
358
|
+
const change = status?.change ?? marks?.get(entry.path);
|
|
359
|
+
const accessibleName = [entry.path, ...fileStatusLabels(message, unsaved, status, change)]
|
|
360
|
+
.filter(Boolean).join(message.separator);
|
|
361
|
+
return <button key={entry.path} type="button" className={`${prefix}-search-result`}
|
|
362
|
+
aria-label={accessibleName} title={accessibleName} data-staged={status?.staged ? '' : undefined}
|
|
363
|
+
data-revision={status?.revision} disabled={paths.has(entry.path)} onClick={() => onOpen?.(entry)}>
|
|
364
|
+
<span className={`${prefix}-name`} data-git-change={change} data-revision={status?.revision}>{entry.path}</span>
|
|
365
|
+
<FileStatusMarkers unsaved={unsaved} change={change}/>
|
|
366
|
+
</button>;
|
|
367
|
+
})}
|
|
368
|
+
{!index ? <p role="status">{message.loading}</p> : index.failed ? <p role="status">{message.failed}</p> :
|
|
369
|
+
!matches?.length && <p role="status">{message.noMatches}</p>}
|
|
370
|
+
</div>;
|
|
371
|
+
}
|
|
372
|
+
|
|
267
373
|
function Explorer({directory, locale, descriptions, marks, fileStatuses, unsavedPaths, refreshKey,
|
|
268
374
|
onOpen, label, mutate, git, isCurrent}: ProjectFolderExplorerProps) {
|
|
269
375
|
const message = messagesFor(locale);
|
|
@@ -303,15 +409,30 @@ function Explorer({directory, locale, descriptions, marks, fileStatuses, unsaved
|
|
|
303
409
|
}
|
|
304
410
|
return handle.getFile();
|
|
305
411
|
};
|
|
412
|
+
const deleted = React.useMemo(() => deletedChildren(deletedPaths(fileStatuses, marks)), [fileStatuses, marks]);
|
|
413
|
+
const [remembered] = React.useState(() => readExplorerState(directory));
|
|
414
|
+
const remember = (change: Partial<ExplorerTreeState>) => {
|
|
415
|
+
const previous = readExplorerState(directory) ?? {expanded: []};
|
|
416
|
+
const next = {...previous, ...change};
|
|
417
|
+
if (!next.selected) delete next.selected;
|
|
418
|
+
writeExplorerState(directory, next);
|
|
419
|
+
};
|
|
420
|
+
const compareNodes = (a: ExplorerNode, b: ExplorerNode) =>
|
|
421
|
+
(a.kind === 'directory' ? 0 : 1) - (b.kind === 'directory' ? 0 : 1) ||
|
|
422
|
+
collator.compare(a.name, b.name) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
306
423
|
const tree = useTree<ExplorerNode>({
|
|
307
424
|
rootItemId: rootId,
|
|
308
|
-
|
|
425
|
+
// The tree comes back as it was left, or as the folder's screen state saved it.
|
|
426
|
+
initialState: {expandedItems: [...remembered?.expanded ?? []],
|
|
427
|
+
selectedItems: remembered?.selected ? [remembered.selected] : [], focusedItem: remembered?.selected ?? null},
|
|
428
|
+
setExpandedItems: expanded => remember({expanded: expanded as string[]}),
|
|
429
|
+
setSelectedItems: selected => remember({selected: (selected as string[])[0]}),
|
|
309
430
|
getItemName: item => item.getItemData()?.name ?? '',
|
|
310
431
|
isItemFolder: item => item.getItemData()?.kind === 'directory',
|
|
311
432
|
createLoadingItemData: () => ({name: '', path: '', kind: 'file'}),
|
|
312
433
|
onPrimaryAction: item => {
|
|
313
434
|
const data = item.getItemData();
|
|
314
|
-
if (data?.kind === 'file') onOpen?.({path: data.path, name: data.name, kind: 'file'});
|
|
435
|
+
if (data?.kind === 'file' && !data.deleted && (fileStatuses?.get(data.path)?.change ?? marks?.get(data.path)) !== 'deleted') onOpen?.({path: data.path, name: data.name, kind: 'file'});
|
|
315
436
|
},
|
|
316
437
|
onLoadedChildren: id => {if (id === rootId) setRootLoaded(true);},
|
|
317
438
|
dataLoader: {
|
|
@@ -329,24 +450,25 @@ function Explorer({directory, locale, descriptions, marks, fileStatuses, unsaved
|
|
|
329
450
|
}
|
|
330
451
|
},
|
|
331
452
|
getChildrenWithData: async id => {
|
|
453
|
+
const base = id === rootId ? '' : `${id}/`;
|
|
454
|
+
const missingChildren = deleted.get(id);
|
|
455
|
+
const nodes = new Map(missingChildren);
|
|
332
456
|
try {
|
|
333
457
|
const handle = await directoryAt(id);
|
|
334
|
-
const base = id === rootId ? '' : `${id}/`;
|
|
335
|
-
const nodes: ExplorerNode[] = [];
|
|
336
458
|
for await (const entry of (handle as IterableDirectory).values()) {
|
|
337
|
-
if (entry.name
|
|
459
|
+
if (hiddenEntry(entry.name)) continue;
|
|
338
460
|
const path = base + entry.name;
|
|
339
461
|
if (entry.kind === 'directory') handles.set(path, entry as FileSystemDirectoryHandle);
|
|
340
462
|
else files.set(path, entry as FileSystemFileHandle);
|
|
341
|
-
nodes.
|
|
463
|
+
nodes.set(path, {name: entry.name, path, kind: entry.kind});
|
|
342
464
|
}
|
|
343
|
-
loadedFolders.add(id);
|
|
344
|
-
nodes.sort((a, b) => (a.kind === 'directory' ? 0 : 1) - (b.kind === 'directory' ? 0 : 1) ||
|
|
345
|
-
collator.compare(a.name, b.name) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
346
|
-
return nodes.map(data => ({id: data.path, data}));
|
|
347
465
|
} catch (error) {
|
|
348
|
-
|
|
466
|
+
if (!missingChildren || (error as DOMException).name !== 'NotFoundError') {
|
|
467
|
+
return [{id: errorId(id), data: {name: failure(error, message), path: '', kind: 'error'}}];
|
|
468
|
+
}
|
|
349
469
|
}
|
|
470
|
+
loadedFolders.add(id);
|
|
471
|
+
return [...nodes.values()].sort(compareNodes).map(data => ({id: data.path, data}));
|
|
350
472
|
}
|
|
351
473
|
},
|
|
352
474
|
features: [asyncDataLoaderFeature, selectionFeature, hotkeysCoreFeature]
|
|
@@ -357,12 +479,25 @@ function Explorer({directory, locale, descriptions, marks, fileStatuses, unsaved
|
|
|
357
479
|
tree.setMounted(true);
|
|
358
480
|
return () => tree.setMounted(false);
|
|
359
481
|
}, [tree]);
|
|
360
|
-
|
|
482
|
+
// A project opened again with this folder shows the tree its screen state saved.
|
|
483
|
+
React.useEffect(() => subscribeExplorerState(directory, state => {
|
|
484
|
+
tree.applySubStateUpdate('expandedItems', [...state.expanded]);
|
|
485
|
+
tree.applySubStateUpdate('selectedItems', state.selected ? [state.selected] : []);
|
|
486
|
+
tree.applySubStateUpdate('focusedItem', state.selected ?? null);
|
|
487
|
+
tree.rebuildTree();
|
|
488
|
+
}), [tree, directory]);
|
|
489
|
+
const lastRefresh = React.useRef({refreshKey, deleted});
|
|
361
490
|
React.useEffect(() => {
|
|
362
|
-
|
|
363
|
-
lastRefresh.current = refreshKey;
|
|
364
|
-
|
|
365
|
-
|
|
491
|
+
const previous = lastRefresh.current;
|
|
492
|
+
lastRefresh.current = {refreshKey, deleted};
|
|
493
|
+
const affected = [...loadedFolders].filter(id => {
|
|
494
|
+
if (previous.refreshKey !== refreshKey) return true;
|
|
495
|
+
const before = previous.deleted.get(id);
|
|
496
|
+
const after = deleted.get(id);
|
|
497
|
+
return before?.size !== after?.size || [...after ?? []].some(([path, node]) => before?.get(path)?.kind !== node.kind);
|
|
498
|
+
});
|
|
499
|
+
void Promise.all(affected.map(id => tree.getItemInstance(id).invalidateChildrenIds(true)));
|
|
500
|
+
}, [tree, loadedFolders, refreshKey, deleted]);
|
|
366
501
|
|
|
367
502
|
const errorText = (error: unknown) => {
|
|
368
503
|
if (error instanceof ExplorerFileError) return actions[error.code];
|
|
@@ -555,27 +690,27 @@ function Explorer({directory, locale, descriptions, marks, fileStatuses, unsaved
|
|
|
555
690
|
};
|
|
556
691
|
const renderItem = (item: ItemInstance<ExplorerNode>) => {
|
|
557
692
|
const data = item.getItemData();
|
|
558
|
-
const entry: ExplorerEntry | undefined = data?.path && data.kind !== 'error' ?
|
|
559
|
-
{path: data.path, name: data.name, kind: data.kind} : undefined;
|
|
560
693
|
const itemProps = item.getProps();
|
|
561
694
|
const level = item.getItemMeta().level;
|
|
562
695
|
const folder = item.isFolder();
|
|
563
696
|
const expanded = folder && item.isExpanded();
|
|
564
697
|
const status = data?.kind === 'file' ? fileStatuses?.get(data.path) : undefined;
|
|
565
698
|
const unsaved = data?.kind === 'file' && unsavedPaths?.has(data.path);
|
|
566
|
-
const
|
|
567
|
-
const
|
|
699
|
+
const change = data?.deleted ? 'deleted' : status?.change ?? (data?.kind === 'file' ? marks?.get(data.path) : undefined);
|
|
700
|
+
const entry: ExplorerEntry | undefined = data?.path && data.kind !== 'error' && change !== 'deleted' ?
|
|
701
|
+
{path: data.path, name: data.name, kind: data.kind} : undefined;
|
|
568
702
|
const description = data && data.kind !== 'error' ? descriptions?.get(data.path) : undefined;
|
|
569
703
|
const icon = !data || data.kind === 'error' ? undefined :
|
|
570
704
|
folder ? (expanded ? 'folder-open' : 'folder') : iconFor(data.name);
|
|
571
|
-
const accessibleName = [item.getItemName(),
|
|
572
|
-
|
|
705
|
+
const accessibleName = [item.getItemName(), ...fileStatusLabels(message, unsaved, status, change), description]
|
|
706
|
+
.filter(Boolean).join(message.separator);
|
|
573
707
|
const moveDestination = entry?.kind === 'directory' && move?.destinations.has(entry.path);
|
|
574
708
|
const moveActive = moveDestination && drag === entry.path;
|
|
575
709
|
const rows = [
|
|
576
710
|
<button {...itemProps} key={item.getId()} type="button" aria-label={accessibleName}
|
|
577
711
|
className={`${prefix}-item${data?.kind === 'error' ? ` ${prefix}-error` : ''}`} style={indent(level)}
|
|
578
712
|
data-path={data?.path} data-staged={status?.staged ? '' : undefined} data-revision={status?.revision}
|
|
713
|
+
aria-disabled={change === 'deleted' && !folder ? true : undefined}
|
|
579
714
|
title={accessibleName || undefined}
|
|
580
715
|
data-drop-directory={entry?.kind === 'directory' ? entry.path : entry ? parentPath(entry.path) : undefined}
|
|
581
716
|
data-drop-active={entry?.kind === 'directory' && drag === entry.path ? '' : undefined}
|
|
@@ -592,13 +727,12 @@ function Explorer({directory, locale, descriptions, marks, fileStatuses, unsaved
|
|
|
592
727
|
else itemProps.onKeyDown?.(event);
|
|
593
728
|
} : itemProps.onKeyDown}>
|
|
594
729
|
<span className={`${prefix}-caret`} aria-hidden="true">{folder ? (expanded ? '▾' : '▸') : ''}</span>
|
|
595
|
-
{icon ? <Icon kind={icon} path={data!.path} readFile={icon === 'image' ? readFile : undefined} /> : null}
|
|
730
|
+
{icon ? <Icon kind={icon} path={data!.path} readFile={icon === 'image' && change !== 'deleted' ? readFile : undefined} /> : null}
|
|
596
731
|
<span className={`${prefix}-label`}>
|
|
597
732
|
<span className={`${prefix}-filename`}>
|
|
598
|
-
<span className={`${prefix}-name`}>{item.getItemName()}</span>
|
|
599
|
-
|
|
733
|
+
<span className={`${prefix}-name`} data-git-change={change} data-revision={status?.revision}>{item.getItemName()}</span>
|
|
734
|
+
<FileStatusMarkers unsaved={unsaved} change={change}/>
|
|
600
735
|
</span>
|
|
601
|
-
{markLabel ? <span className={`${prefix}-mark`} aria-hidden="true">{markLabel}</span> : null}
|
|
602
736
|
</span>
|
|
603
737
|
{description ? <span className={`${prefix}-description`} aria-hidden="true">{description}</span> : null}
|
|
604
738
|
</button>
|
|
@@ -677,7 +811,11 @@ const keyFor = (directory: FileSystemDirectoryHandle) => {
|
|
|
677
811
|
*/
|
|
678
812
|
export function ProjectFolderExplorer(props: ProjectFolderExplorerProps) {
|
|
679
813
|
// A new directory starts a new tree, so cached entries and expanded folders never leak between folders.
|
|
680
|
-
|
|
814
|
+
const query = props.query?.trim().toLowerCase();
|
|
815
|
+
return <React.Fragment key={keyFor(props.directory)}>
|
|
816
|
+
<div hidden={Boolean(query)}><Explorer {...props} /></div>
|
|
817
|
+
{query && <FileSearch {...props} query={query} />}
|
|
818
|
+
</React.Fragment>;
|
|
681
819
|
}
|
|
682
820
|
|
|
683
821
|
/** Follows marks for the files under `directory`, by path relative to it. Returns the function that stops it. */
|
|
@@ -692,6 +830,9 @@ type FolderViewProps = {
|
|
|
692
830
|
createFolder?(): Promise<unknown>;
|
|
693
831
|
marks?: FollowMarks;
|
|
694
832
|
fileStatuses?: FollowFileStatuses;
|
|
833
|
+
/** Reports unsaved text edits in open file tabs for the given folder. */
|
|
834
|
+
followFileEdits?(directory: FileSystemDirectoryHandle, update: (paths: readonly string[]) => void): () => void;
|
|
835
|
+
query?: string;
|
|
695
836
|
git?: FollowExplorerGit;
|
|
696
837
|
/** Opens a transient file tab in the host workspace. */
|
|
697
838
|
openFile?(request: FilePreviewRequest): boolean;
|
|
@@ -704,7 +845,7 @@ const readJson = async (directory: FileSystemDirectoryHandle, name: string): Pro
|
|
|
704
845
|
JSON.parse(await (await (await directory.getFileHandle(name)).getFile()).text());
|
|
705
846
|
|
|
706
847
|
function FolderView({vm, locale = 'en', marks: followMarks, fileStatuses: followStatuses, git: followGit,
|
|
707
|
-
openFile, readBaseline, createFolder}: FolderViewProps) {
|
|
848
|
+
openFile, readBaseline, query, followFileEdits, createFolder}: FolderViewProps) {
|
|
708
849
|
const message = messagesFor(locale);
|
|
709
850
|
const [creating, setCreating] = React.useState(false);
|
|
710
851
|
const [createError, setCreateError] = React.useState('');
|
|
@@ -764,6 +905,11 @@ function FolderView({vm, locale = 'en', marks: followMarks, fileStatuses: follow
|
|
|
764
905
|
const stop = folder.subscribeEdits(schedule);
|
|
765
906
|
return () => {stop(); clearTimeout(timer);};
|
|
766
907
|
}, [folder, directory, version]);
|
|
908
|
+
const [fileEdits, setFileEdits] = React.useState<{directory: FileSystemDirectoryHandle; paths: readonly string[]}>();
|
|
909
|
+
React.useEffect(() => {
|
|
910
|
+
if (!directory || !followFileEdits) return;
|
|
911
|
+
return followFileEdits(directory, paths => setFileEdits({directory, paths}));
|
|
912
|
+
}, [directory, followFileEdits]);
|
|
767
913
|
const [git, setGit] = React.useState<{directory: FileSystemDirectoryHandle; actions?: ExplorerGit}>();
|
|
768
914
|
React.useEffect(() => {
|
|
769
915
|
if (!directory || !followGit) return;
|
|
@@ -784,10 +930,12 @@ function FolderView({vm, locale = 'en', marks: followMarks, fileStatuses: follow
|
|
|
784
930
|
</>}
|
|
785
931
|
</div>;
|
|
786
932
|
}
|
|
933
|
+
const unsavedPaths = new Set(unsaved?.directory === directory ? unsaved.paths : undefined);
|
|
934
|
+
if (fileEdits?.directory === directory) for (const path of fileEdits.paths) unsavedPaths.add(path);
|
|
787
935
|
return <>
|
|
788
936
|
<ProjectFolderExplorer directory={directory} locale={locale} descriptions={descriptions} marks={marks}
|
|
789
|
-
refreshKey={version} fileStatuses={statuses?.directory === directory ? statuses.paths : undefined}
|
|
790
|
-
unsavedPaths={
|
|
937
|
+
refreshKey={version} query={query} fileStatuses={statuses?.directory === directory ? statuses.paths : undefined}
|
|
938
|
+
unsavedPaths={unsavedPaths}
|
|
791
939
|
mutate={operation => folder.mutate(directory, operation)} isCurrent={() => folder.directory === directory}
|
|
792
940
|
git={git?.directory === directory ? git.actions : undefined}
|
|
793
941
|
onOpen={openFile && (entry => {
|
package/src/file-preview.ts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
|
+
interface FilePreviewSession {
|
|
2
|
+
requestClose(): void;
|
|
3
|
+
dispose(): void;
|
|
4
|
+
save?: () => Promise<void>;
|
|
5
|
+
readonly fileEdit?: {
|
|
6
|
+
directory: FileSystemDirectoryHandle;
|
|
7
|
+
path: string;
|
|
8
|
+
isDirty(): boolean;
|
|
9
|
+
readUnsaved(): Uint8Array | undefined;
|
|
10
|
+
save(): Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
1
14
|
export interface FilePreviewRequest {
|
|
2
15
|
reference: {providerId: string; documentId: string};
|
|
3
16
|
filename: string;
|
|
4
17
|
title: string;
|
|
5
18
|
transient: true;
|
|
6
|
-
mount(element: HTMLElement, close: () => void
|
|
19
|
+
mount(element: HTMLElement, close: () => void, onSaveAvailable?: () => void): FilePreviewSession;
|
|
7
20
|
}
|
|
8
21
|
|
|
9
22
|
export type FileBaseline = {branch: string | undefined; head: string | null; bytes: Uint8Array | null};
|
|
@@ -54,7 +67,7 @@ export function createFilePreview(directory: FileSystemDirectoryHandle, document
|
|
|
54
67
|
filename: name,
|
|
55
68
|
title: name,
|
|
56
69
|
transient: true,
|
|
57
|
-
mount(element, close) {
|
|
70
|
+
mount(element, close, onSaveAvailable) {
|
|
58
71
|
const message = locale.toLowerCase().startsWith('ja') ? messages.ja : messages.en;
|
|
59
72
|
const {ownerDocument: document} = element;
|
|
60
73
|
const panel = document.createElement('div');
|
|
@@ -69,7 +82,7 @@ export function createFilePreview(directory: FileSystemDirectoryHandle, document
|
|
|
69
82
|
let disposed = false;
|
|
70
83
|
let url: string | undefined;
|
|
71
84
|
let media: HTMLMediaElement | undefined;
|
|
72
|
-
let textSession: ReturnType<
|
|
85
|
+
let textSession: ReturnType<typeof import('./text-file-editor.js').mountTextFile> | undefined;
|
|
73
86
|
const readHandle = () => fileAt(directory, path);
|
|
74
87
|
|
|
75
88
|
const show = (node: HTMLElement) => status.replaceWith(node);
|
|
@@ -114,12 +127,17 @@ export function createFilePreview(directory: FileSystemDirectoryHandle, document
|
|
|
114
127
|
}
|
|
115
128
|
textSession = mountTextFile(panel, {
|
|
116
129
|
path, extension, content, locale, limit: textLimit, readHandle, close,
|
|
117
|
-
readBaseline: readBaseline && (() => readBaseline(directory, path, textLimit))
|
|
130
|
+
readBaseline: readBaseline && (() => readBaseline(directory, path, textLimit)),
|
|
131
|
+
hostSaveShortcut: Boolean(onSaveAvailable), onDirtyChange: onSaveAvailable
|
|
118
132
|
});
|
|
119
133
|
status.remove();
|
|
134
|
+
onSaveAvailable?.();
|
|
120
135
|
};
|
|
121
136
|
void load().catch(() => {if (!disposed) status.textContent = message.failed;});
|
|
122
137
|
return {
|
|
138
|
+
get save() {return textSession?.save;},
|
|
139
|
+
get fileEdit() {return textSession && {directory, path, isDirty: textSession.isDirty,
|
|
140
|
+
readUnsaved: textSession.readUnsaved, save: textSession.save};},
|
|
123
141
|
requestClose() {if (textSession) textSession.requestClose(); else close();},
|
|
124
142
|
dispose() {
|
|
125
143
|
disposed = true;
|
package/src/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {isProjectFolderEmpty} from './create-folder.js';
|
|
|
4
4
|
import {registerFolder} from './folder-registry.js';
|
|
5
5
|
import type {FolderHost, FolderVM, PickerWindow} from './session.js';
|
|
6
6
|
|
|
7
|
-
export type {Comparison, ComparisonRequest, ComparisonSection, FolderHost, FolderVM, OpenResult} from './session.js';
|
|
7
|
+
export type {Comparison, ComparisonRequest, ComparisonSection, FolderHost, FolderScreenState, FolderVM, OpenResult} from './session.js';
|
|
8
8
|
export type {ReloadTarget} from './block-reload.js';
|
|
9
9
|
export {followProjectFolder} from './folder-registry.js';
|
|
10
10
|
export type {FollowedFolder} from './folder-registry.js';
|
|
@@ -262,6 +262,9 @@ export function mountProjectFolder({vm, document, host, locale = 'en', autoReloa
|
|
|
262
262
|
return {
|
|
263
263
|
openDirectory: (...args: Parameters<typeof session.openDirectory>) =>
|
|
264
264
|
session.openDirectory(...args).finally(renderReload),
|
|
265
|
+
unsavedPaths: () => session.unsavedPaths(),
|
|
266
|
+
readUnsavedFile: (path: string) => session.readUnsavedFile(path),
|
|
267
|
+
subscribeEdits: (listener: () => void) => session.subscribeEdits(listener),
|
|
265
268
|
save: () => session.save().finally(renderReload),
|
|
266
269
|
createFromSnapshot({prepareFiles, collectEdits, directory, current = () => true}: {
|
|
267
270
|
prepareFiles: () => Promise<ReadonlyMap<string, Uint8Array>>;
|
|
@@ -330,3 +333,5 @@ export function renderProjectFolderMenu(createElement: MenuElement, {
|
|
|
330
333
|
};
|
|
331
334
|
return createElement(MenuSection, null, item('open'), entry.session.canSave ? item('save') : null);
|
|
332
335
|
}
|
|
336
|
+
export {parseExplorerState, readExplorerState, subscribeExplorerState, writeExplorerState} from './explorer-state.js';
|
|
337
|
+
export type {ExplorerTreeState} from './explorer-state.js';
|