@kubohiroya/turbowarp-plus-editor-project-folder 0.8.0 → 0.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/dist/THIRD_PARTY_NOTICES.md +716 -0
- package/dist/create-folder.d.ts +8 -0
- package/dist/explorer-files.d.ts +23 -0
- package/dist/explorer-menu.d.ts +53 -0
- package/dist/explorer.d.ts +34 -4
- package/dist/explorer.js +2020 -694
- package/dist/file-preview.d.ts +24 -0
- package/dist/folder-registry.d.ts +4 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +1019 -555
- package/dist/session.d.ts +6 -0
- package/dist/shared-JdaDvk2R.js +588 -0
- package/dist/shared-kI7qYraF.js +30130 -0
- package/dist/text-file-editor.d.ts +17 -0
- package/package.json +16 -1
- package/src/create-folder.ts +99 -0
- package/src/explorer-files.ts +197 -0
- package/src/explorer-menu.tsx +135 -0
- package/src/explorer.tsx +388 -22
- package/src/file-preview.ts +134 -0
- package/src/folder-registry.ts +5 -0
- package/src/index.ts +27 -2
- package/src/session.ts +77 -12
- package/src/source.ts +16 -8
- package/src/text-file-editor.ts +356 -0
- package/vite.config.ts +2 -0
- package/dist/shared-DlmcdO1_.js +0 -458
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { FileBaseline } from './file-preview.js';
|
|
2
|
+
export declare function decodeText(bytes: ArrayBuffer | Uint8Array): string | null;
|
|
3
|
+
type TextFileOptions = {
|
|
4
|
+
path: string;
|
|
5
|
+
extension: string;
|
|
6
|
+
content: string;
|
|
7
|
+
locale: string;
|
|
8
|
+
limit: number;
|
|
9
|
+
readHandle(): Promise<FileSystemFileHandle>;
|
|
10
|
+
readBaseline?(): Promise<FileBaseline | null>;
|
|
11
|
+
close(): void;
|
|
12
|
+
};
|
|
13
|
+
export declare function mountTextFile(parent: HTMLElement, options: TextFileOptions): {
|
|
14
|
+
requestClose(): void;
|
|
15
|
+
dispose(): void;
|
|
16
|
+
};
|
|
17
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-plus-editor-project-folder",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "Read and save expanded Scratch project folders in TurboWarp Plus Editor",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -39,10 +39,25 @@
|
|
|
39
39
|
"test:browser": "npm run build && node --test test/*.browser.js"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
+
"@codemirror/commands": "6.11.1",
|
|
43
|
+
"@codemirror/lang-css": "6.3.1",
|
|
44
|
+
"@codemirror/lang-html": "6.4.12",
|
|
45
|
+
"@codemirror/lang-javascript": "6.2.5",
|
|
46
|
+
"@codemirror/lang-json": "6.0.2",
|
|
47
|
+
"@codemirror/lang-markdown": "6.5.2",
|
|
48
|
+
"@codemirror/lang-python": "6.2.1",
|
|
49
|
+
"@codemirror/lang-xml": "6.1.0",
|
|
50
|
+
"@codemirror/lang-yaml": "6.1.3",
|
|
51
|
+
"@codemirror/language": "6.13.1",
|
|
52
|
+
"@codemirror/legacy-modes": "6.5.5",
|
|
53
|
+
"@codemirror/merge": "6.12.3",
|
|
54
|
+
"@codemirror/state": "6.7.6",
|
|
55
|
+
"@codemirror/view": "6.43.15",
|
|
42
56
|
"@headless-tree/core": "1.8.0",
|
|
43
57
|
"@headless-tree/react": "1.8.0",
|
|
44
58
|
"@kubohiroya/sb3-toolchain": "0.22.0",
|
|
45
59
|
"@kubohiroya/turbowarp-plus-editor-test-runtime": "0.1.0",
|
|
60
|
+
"@lezer/highlight": "1.2.5",
|
|
46
61
|
"@playwright/test": "1.64.1",
|
|
47
62
|
"@types/react": "16.14.62",
|
|
48
63
|
"@types/react-dom": "16.9.24",
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {extensionsFile, fileVersion, FolderError, manifestFile, projectFile, readFolder, sameBytes, unsupported} from './source.js';
|
|
2
|
+
import type {Storage} from './source.js';
|
|
3
|
+
|
|
4
|
+
const manifests = new Set<string>([projectFile, manifestFile, extensionsFile]);
|
|
5
|
+
const sourcePath = /^(?:assets\/[a-f0-9]{32}\.[A-Za-z0-9]+|extensions\/[A-Za-z0-9._-]+\.(?:js|manifest\.json))$/u;
|
|
6
|
+
|
|
7
|
+
type IterableDirectory = FileSystemDirectoryHandle & {values(): AsyncIterable<FileSystemHandle>};
|
|
8
|
+
|
|
9
|
+
function missing(error: unknown) {
|
|
10
|
+
return error instanceof DOMException && error.name === 'NotFoundError';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function assertEmpty(directory: FileSystemDirectoryHandle) {
|
|
14
|
+
for await (const _entry of (directory as IterableDirectory).values()) {
|
|
15
|
+
throw new FolderError('conflict', 'Choose an empty project folder');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Create and validate a project-folder snapshot in a directory chosen by the user. */
|
|
20
|
+
export async function createProjectFolder({directory, files, storage, current = () => true}: {
|
|
21
|
+
directory: FileSystemDirectoryHandle;
|
|
22
|
+
files: ReadonlyMap<string, Uint8Array>;
|
|
23
|
+
storage: Storage;
|
|
24
|
+
current?: () => boolean;
|
|
25
|
+
}) {
|
|
26
|
+
for (const filename of manifests) {
|
|
27
|
+
if (!files.has(filename)) return unsupported(`Missing ${filename}`);
|
|
28
|
+
}
|
|
29
|
+
for (const [filename, bytes] of files) {
|
|
30
|
+
if ((!manifests.has(filename) && !sourcePath.test(filename)) || !(bytes instanceof Uint8Array)) {
|
|
31
|
+
return unsupported(`Invalid project folder entry: ${filename}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const assertCurrent = () => {
|
|
35
|
+
if (!current()) throw new FolderError('conflict', 'The project changed before the folder was saved');
|
|
36
|
+
};
|
|
37
|
+
await assertEmpty(directory);
|
|
38
|
+
const directories = new Map<string, FileSystemDirectoryHandle>();
|
|
39
|
+
const undo: (() => Promise<void>)[] = [];
|
|
40
|
+
try {
|
|
41
|
+
// Publish the project JSON last, after the files it refers to.
|
|
42
|
+
const entries = [...files].sort(([a], [b]) => Number(manifests.has(a)) - Number(manifests.has(b)) ||
|
|
43
|
+
Number(a === projectFile) - Number(b === projectFile));
|
|
44
|
+
for (const [filename, bytes] of entries) {
|
|
45
|
+
assertCurrent();
|
|
46
|
+
const [first, basename] = filename.split('/');
|
|
47
|
+
let parent = directory;
|
|
48
|
+
if (basename !== undefined) {
|
|
49
|
+
const existing = directories.get(first);
|
|
50
|
+
if (existing) parent = existing;
|
|
51
|
+
else {
|
|
52
|
+
const unexpected = await directory.getDirectoryHandle(first).catch(error => {
|
|
53
|
+
if (missing(error)) return null;
|
|
54
|
+
throw error;
|
|
55
|
+
});
|
|
56
|
+
if (unexpected) throw new FolderError('conflict');
|
|
57
|
+
parent = await directory.getDirectoryHandle(first, {create: true});
|
|
58
|
+
directories.set(first, parent);
|
|
59
|
+
undo.push(async () => {
|
|
60
|
+
const entry = await directory.getDirectoryHandle(first);
|
|
61
|
+
if (await entry.isSameEntry(parent)) await directory.removeEntry(first);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const name = basename ?? first;
|
|
66
|
+
const existing = await parent.getFileHandle(name).catch(error => {
|
|
67
|
+
if (missing(error)) return null;
|
|
68
|
+
throw error;
|
|
69
|
+
});
|
|
70
|
+
if (existing) throw new FolderError('conflict');
|
|
71
|
+
const file = await parent.getFileHandle(name, {create: true});
|
|
72
|
+
const initial = await file.getFile();
|
|
73
|
+
if (initial.size !== 0) throw new FolderError('conflict');
|
|
74
|
+
let expected: Uint8Array = new Uint8Array();
|
|
75
|
+
undo.push(async () => {
|
|
76
|
+
const entry = await parent.getFileHandle(name);
|
|
77
|
+
if (!await entry.isSameEntry(file)) return;
|
|
78
|
+
if (sameBytes(new Uint8Array(await (await entry.getFile()).arrayBuffer()), expected)) await parent.removeEntry(name);
|
|
79
|
+
});
|
|
80
|
+
const writable = await file.createWritable({mode: 'exclusive'} as FileSystemCreateWritableOptions);
|
|
81
|
+
try {
|
|
82
|
+
await writable.write(bytes as Uint8Array<ArrayBuffer>);
|
|
83
|
+
assertCurrent();
|
|
84
|
+
if (fileVersion(await file.getFile()) !== fileVersion(initial)) throw new FolderError('conflict');
|
|
85
|
+
await writable.close();
|
|
86
|
+
expected = bytes;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
await writable.abort().catch(() => {});
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const source = await readFolder(directory, storage);
|
|
93
|
+
assertCurrent();
|
|
94
|
+
return source;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
for (const revert of undo.reverse()) await revert().catch(() => {});
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
type DirectoryEntries = FileSystemDirectoryHandle & {values(): AsyncIterable<FileSystemHandle>};
|
|
2
|
+
|
|
3
|
+
export class ExplorerFileError extends Error {
|
|
4
|
+
constructor(readonly code: 'invalidName' | 'exists' | 'invalidDestination' | 'sourceChanged' | 'originalKept' | 'cleanupFailed') {
|
|
5
|
+
super(code);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function parentPath(path: string) {
|
|
10
|
+
return path.slice(0, Math.max(0, path.lastIndexOf('/')));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function validateName(name: string) {
|
|
14
|
+
if (!name.trim() || name === '.' || name === '..' || name.toLowerCase() === '.git' || /[\\/\u0000]/u.test(name)) {
|
|
15
|
+
throw new ExplorerFileError('invalidName');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function resolveExplorerDirectory(root: FileSystemDirectoryHandle, path: string) {
|
|
20
|
+
let directory = root;
|
|
21
|
+
const parts = path ? path.split('/') : [];
|
|
22
|
+
for (const part of parts) {
|
|
23
|
+
validateName(part);
|
|
24
|
+
directory = await directory.getDirectoryHandle(part);
|
|
25
|
+
}
|
|
26
|
+
if ((await root.resolve(directory))?.join('/') !== path) throw new ExplorerFileError('sourceChanged');
|
|
27
|
+
return directory;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function explorerNameAvailable(directory: FileSystemDirectoryHandle, name: string) {
|
|
31
|
+
try {
|
|
32
|
+
await directory.getFileHandle(name);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (error instanceof DOMException && error.name === 'NotFoundError') return true;
|
|
35
|
+
if (!(error instanceof DOMException) || error.name !== 'TypeMismatchError') throw error;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function assertUnused(directory: FileSystemDirectoryHandle, name: string) {
|
|
41
|
+
if (!await explorerNameAvailable(directory, name)) throw new ExplorerFileError('exists');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function createExplorerDirectory(parent: FileSystemDirectoryHandle, name: string, guard: () => void) {
|
|
45
|
+
validateName(name);
|
|
46
|
+
await assertUnused(parent, name);
|
|
47
|
+
guard();
|
|
48
|
+
await parent.getDirectoryHandle(name, {create: true});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const sameVersion = (a: File, b: File) => a.size === b.size && a.lastModified === b.lastModified;
|
|
52
|
+
|
|
53
|
+
async function writeNewFile(directory: FileSystemDirectoryHandle, name: string, file: File, guard: () => void) {
|
|
54
|
+
await assertUnused(directory, name);
|
|
55
|
+
guard();
|
|
56
|
+
const handle = await directory.getFileHandle(name, {create: true});
|
|
57
|
+
const empty = await handle.getFile();
|
|
58
|
+
if (empty.size) throw new ExplorerFileError('exists');
|
|
59
|
+
const options = {mode: 'exclusive'} as FileSystemCreateWritableOptions;
|
|
60
|
+
let stream: FileSystemWritableFileStream | undefined;
|
|
61
|
+
try {
|
|
62
|
+
stream = await handle.createWritable(options);
|
|
63
|
+
await stream.write(file);
|
|
64
|
+
if (!sameVersion(empty, await handle.getFile())) throw new ExplorerFileError('exists');
|
|
65
|
+
guard();
|
|
66
|
+
await stream.close();
|
|
67
|
+
} catch (error) {
|
|
68
|
+
await stream?.abort().catch(() => {});
|
|
69
|
+
try {
|
|
70
|
+
if (sameVersion(empty, await handle.getFile())) await directory.removeEntry(name);
|
|
71
|
+
} catch {
|
|
72
|
+
throw new ExplorerFileError('cleanupFailed');
|
|
73
|
+
}
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function copyExplorerFile(directory: FileSystemDirectoryHandle, file: File, guard: () => void) {
|
|
79
|
+
validateName(file.name);
|
|
80
|
+
await writeNewFile(directory, file.name, file, guard);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
type CopiedEntry = {verify(): Promise<void>; removeSource(): Promise<void>};
|
|
84
|
+
|
|
85
|
+
async function copyEntry(sourceParent: FileSystemDirectoryHandle, source: FileSystemHandle, parent: FileSystemDirectoryHandle,
|
|
86
|
+
name: string, guard: () => void): Promise<CopiedEntry> {
|
|
87
|
+
validateName(source.name);
|
|
88
|
+
if (source.kind === 'file') {
|
|
89
|
+
const handle = source as FileSystemFileHandle;
|
|
90
|
+
const file = await handle.getFile();
|
|
91
|
+
await writeNewFile(parent, name, file, guard);
|
|
92
|
+
const verify = async () => {
|
|
93
|
+
if (!sameVersion(file, await handle.getFile())) throw new ExplorerFileError('sourceChanged');
|
|
94
|
+
};
|
|
95
|
+
return {verify, async removeSource() {
|
|
96
|
+
await verify();
|
|
97
|
+
guard();
|
|
98
|
+
await sourceParent.removeEntry(source.name);
|
|
99
|
+
}};
|
|
100
|
+
}
|
|
101
|
+
await assertUnused(parent, name);
|
|
102
|
+
guard();
|
|
103
|
+
const destination = await parent.getDirectoryHandle(name, {create: true});
|
|
104
|
+
const directory = source as DirectoryEntries;
|
|
105
|
+
const children = new Map<string, {handle: FileSystemHandle; copy: CopiedEntry}>();
|
|
106
|
+
try {
|
|
107
|
+
for await (const handle of directory.values()) {
|
|
108
|
+
children.set(handle.name, {handle, copy: await copyEntry(directory, handle, destination, handle.name, guard)});
|
|
109
|
+
}
|
|
110
|
+
} catch (error) {
|
|
111
|
+
await removeIncompleteCopy(parent, name);
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
const verify = async () => {
|
|
115
|
+
let count = 0;
|
|
116
|
+
for await (const handle of directory.values()) {
|
|
117
|
+
const child = children.get(handle.name);
|
|
118
|
+
if (!child || !await handle.isSameEntry(child.handle)) throw new ExplorerFileError('sourceChanged');
|
|
119
|
+
await child.copy.verify();
|
|
120
|
+
count++;
|
|
121
|
+
}
|
|
122
|
+
if (count !== children.size) throw new ExplorerFileError('sourceChanged');
|
|
123
|
+
};
|
|
124
|
+
return {verify, async removeSource() {
|
|
125
|
+
// Directory iteration may omit inaccessible entries. Only remove entries that were copied.
|
|
126
|
+
for (const {copy} of children.values()) await copy.removeSource();
|
|
127
|
+
guard();
|
|
128
|
+
await sourceParent.removeEntry(source.name);
|
|
129
|
+
}};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function removeIncompleteCopy(parent: FileSystemDirectoryHandle, name: string) {
|
|
133
|
+
try {
|
|
134
|
+
await parent.removeEntry(name, {recursive: true});
|
|
135
|
+
} catch {
|
|
136
|
+
throw new ExplorerFileError('cleanupFailed');
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export async function renameExplorerEntry(parent: FileSystemDirectoryHandle, entry: {name: string; kind: FileSystemHandleKind},
|
|
141
|
+
name: string, guard: () => void) {
|
|
142
|
+
if (name === entry.name) return;
|
|
143
|
+
await relocateEntry(parent, entry, parent, name, guard);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function canMoveExplorerEntry(path: string, destination: string) {
|
|
147
|
+
return parentPath(path) !== destination && path !== destination && !destination.startsWith(`${path}/`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export async function moveExplorerEntry(root: FileSystemDirectoryHandle,
|
|
151
|
+
entry: {path: string; name: string; kind: FileSystemHandleKind}, destination: FileSystemDirectoryHandle,
|
|
152
|
+
destinationPath: string, guard: () => void) {
|
|
153
|
+
if (!canMoveExplorerEntry(entry.path, destinationPath)) throw new ExplorerFileError('invalidDestination');
|
|
154
|
+
const parent = await resolveExplorerDirectory(root, parentPath(entry.path));
|
|
155
|
+
guard();
|
|
156
|
+
await relocateEntry(parent, entry, destination, entry.name, guard);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function relocateEntry(sourceParent: FileSystemDirectoryHandle, entry: {name: string; kind: FileSystemHandleKind},
|
|
160
|
+
destination: FileSystemDirectoryHandle, name: string, guard: () => void) {
|
|
161
|
+
validateName(entry.name);
|
|
162
|
+
validateName(name);
|
|
163
|
+
const source = entry.kind === 'file' ? await sourceParent.getFileHandle(entry.name) : await sourceParent.getDirectoryHandle(entry.name);
|
|
164
|
+
const copy = await copyEntry(sourceParent, source, destination, name, guard);
|
|
165
|
+
try {
|
|
166
|
+
await copy.verify();
|
|
167
|
+
guard();
|
|
168
|
+
} catch (error) {
|
|
169
|
+
await removeIncompleteCopy(destination, name);
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
await copy.removeSource();
|
|
174
|
+
} catch {
|
|
175
|
+
// Recursive removal can fail part way. The complete copy is the recovery copy.
|
|
176
|
+
throw new ExplorerFileError('originalKept');
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export async function deleteExplorerEntry(parent: FileSystemDirectoryHandle, entry: {name: string; kind: FileSystemHandleKind},
|
|
181
|
+
guard: () => void) {
|
|
182
|
+
validateName(entry.name);
|
|
183
|
+
if (entry.kind === 'directory') {
|
|
184
|
+
const check = async (directory: DirectoryEntries): Promise<void> => {
|
|
185
|
+
for await (const child of directory.values()) {
|
|
186
|
+
guard();
|
|
187
|
+
validateName(child.name);
|
|
188
|
+
if (child.kind === 'directory') await check(child as DirectoryEntries);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
await check(await parent.getDirectoryHandle(entry.name) as DirectoryEntries);
|
|
192
|
+
} else {
|
|
193
|
+
await parent.getFileHandle(entry.name);
|
|
194
|
+
}
|
|
195
|
+
guard();
|
|
196
|
+
await parent.removeEntry(entry.name, {recursive: entry.kind === 'directory'});
|
|
197
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import {createPortal} from 'react-dom';
|
|
3
|
+
|
|
4
|
+
export const actionMessages = (locale = 'en') => locale.toLowerCase().startsWith('ja') ? {
|
|
5
|
+
actions: 'ファイルとフォルダの操作', rename: '名前を変更…', renameTitle: '名前を変更', delete: '削除…', deleteTitle: '削除',
|
|
6
|
+
createFolder: '新規フォルダを作成', folderName: 'フォルダ名', newName: '新しい名前', cancel: 'キャンセル',
|
|
7
|
+
remove: (name: string, folder: boolean) => folder ? `「${name}」とその中身を削除しますか?` : `「${name}」を削除しますか?`,
|
|
8
|
+
stage: 'Git にステージ', unstage: 'Git のステージを解除',
|
|
9
|
+
working: '処理中…', copyFailed: 'コピーできませんでした', filesOnly: 'ファイルをドロップしてください。',
|
|
10
|
+
invalidName: 'この名前は使用できません。', exists: '同じ名前のファイルまたはフォルダがあります。',
|
|
11
|
+
invalidDestination: 'このフォルダには移動できません。',
|
|
12
|
+
denied: 'このフォルダへの書き込みが許可されていません。', conflict: 'プロジェクトフォルダが変更されたか、別の操作が実行中です。',
|
|
13
|
+
sourceChanged: '操作中に対象が変更されました。', originalKept: '元の項目を削除できなかったため、コピーを残しました。',
|
|
14
|
+
cleanupFailed: '失敗したコピーを削除できませんでした。', failed: '操作を完了できませんでした。'
|
|
15
|
+
} : {
|
|
16
|
+
actions: 'File and folder actions', rename: 'Rename…', renameTitle: 'Rename', delete: 'Delete…', deleteTitle: 'Delete',
|
|
17
|
+
createFolder: 'Create New Folder', folderName: 'Folder name', newName: 'New name', cancel: 'Cancel',
|
|
18
|
+
remove: (name: string, folder: boolean) => folder ? `Delete “${name}” and its contents?` : `Delete “${name}”?`,
|
|
19
|
+
stage: 'Stage in Git', unstage: 'Unstage in Git',
|
|
20
|
+
working: 'Working…', copyFailed: 'Could not copy', filesOnly: 'Drop files here.',
|
|
21
|
+
invalidName: 'This name cannot be used.', exists: 'A file or folder with this name already exists.',
|
|
22
|
+
invalidDestination: 'This item cannot be moved to that folder.',
|
|
23
|
+
denied: 'Permission to write to this folder was not granted.', conflict: 'The project folder changed or another operation is running.',
|
|
24
|
+
sourceChanged: 'The item changed during the operation.', originalKept: 'The original could not be removed, so the copy was kept.',
|
|
25
|
+
cleanupFailed: 'The incomplete copy could not be removed.', failed: 'The operation could not be completed.'
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type ActionMessages = ReturnType<typeof actionMessages>;
|
|
29
|
+
export type MenuAnchor = {row: HTMLButtonElement; x: number; y: number};
|
|
30
|
+
export type FileAction = 'createFolder' | 'rename' | 'delete';
|
|
31
|
+
|
|
32
|
+
export const actionCss = `
|
|
33
|
+
.twpf-explorer-actions{position:fixed;z-index:2147483647;max-width:calc(100vw - 8px);padding:4px;
|
|
34
|
+
border:1px solid GrayText;border-radius:5px;background:Canvas;color:CanvasText;box-shadow:0 2px 8px #0004}
|
|
35
|
+
.twpf-explorer-actions button{display:block;width:100%;border:0;padding:.35em .65em;text-align:start;background:Canvas;color:CanvasText;
|
|
36
|
+
font:inherit;cursor:pointer;border-radius:3px}
|
|
37
|
+
.twpf-explorer-actions button:is(:hover,:focus-visible){background:Highlight;color:HighlightText}
|
|
38
|
+
.twpf-explorer-dialog{max-width:min(28em,calc(100vw - 2em));color:CanvasText;background:Canvas;
|
|
39
|
+
border:1px solid GrayText;border-radius:6px;padding:1em;overflow-wrap:anywhere}
|
|
40
|
+
.twpf-explorer-dialog::backdrop{background:#0006}
|
|
41
|
+
.twpf-explorer-dialog h2{font-size:1.1em;margin:0 0 .75em}
|
|
42
|
+
.twpf-explorer-dialog input{display:block;width:100%;box-sizing:border-box;margin-top:.3em}
|
|
43
|
+
.twpf-explorer-dialog-footer{display:flex;justify-content:flex-end;gap:.5em;margin-top:1em}
|
|
44
|
+
.twpf-explorer-drop{min-height:100%;box-sizing:border-box}
|
|
45
|
+
.twpf-explorer [data-drop-active],.twpf-explorer[data-drop-active]{outline:2px solid Highlight;outline-offset:-2px}
|
|
46
|
+
.twpf-explorer-item[data-move-unavailable]{color:GrayText;opacity:.65;filter:grayscale(1)}
|
|
47
|
+
.twpf-explorer-item[data-move-active] .twpf-explorer-name{font-weight:700}
|
|
48
|
+
.twpf-explorer-drag-image{position:fixed;top:0;left:0;z-index:2147483647;pointer-events:none;
|
|
49
|
+
display:flex;align-items:center;gap:.4em;max-width:18em;white-space:nowrap;padding:.3em .5em;
|
|
50
|
+
border:1px solid GrayText;border-radius:4px;background:Canvas;color:CanvasText;box-shadow:0 2px 8px #0004}
|
|
51
|
+
.twpf-explorer-feedback{white-space:pre-wrap;overflow-wrap:anywhere;padding:.25em .5em}
|
|
52
|
+
`;
|
|
53
|
+
|
|
54
|
+
export function ExplorerMenu({anchor, label, actions, close}: {
|
|
55
|
+
anchor: MenuAnchor; label: string; actions: readonly {label: string; run(): void}[]; close(restoreFocus: boolean): void;
|
|
56
|
+
}) {
|
|
57
|
+
const ref = React.useRef<HTMLDivElement>(null);
|
|
58
|
+
const document = anchor.row.ownerDocument;
|
|
59
|
+
React.useLayoutEffect(() => {
|
|
60
|
+
const menu = ref.current!;
|
|
61
|
+
const {width, height} = menu.getBoundingClientRect();
|
|
62
|
+
menu.style.left = `${Math.max(4, Math.min(anchor.x, document.documentElement.clientWidth - width - 4))}px`;
|
|
63
|
+
menu.style.top = `${Math.max(4, Math.min(anchor.y, document.documentElement.clientHeight - height - 4))}px`;
|
|
64
|
+
menu.querySelector('button')?.focus();
|
|
65
|
+
const outside = (event: Event) => {
|
|
66
|
+
if (!menu.contains(event.target as Node)) close(false);
|
|
67
|
+
};
|
|
68
|
+
document.addEventListener('pointerdown', outside, true);
|
|
69
|
+
document.addEventListener('focusin', outside, true);
|
|
70
|
+
return () => {
|
|
71
|
+
document.removeEventListener('pointerdown', outside, true);
|
|
72
|
+
document.removeEventListener('focusin', outside, true);
|
|
73
|
+
};
|
|
74
|
+
}, [anchor]);
|
|
75
|
+
return createPortal(<div ref={ref} role="menu" aria-label={label} className="twpf-explorer-actions"
|
|
76
|
+
onKeyDown={event => {
|
|
77
|
+
if (event.key === 'Tab') {close(true); return;}
|
|
78
|
+
if (event.key === 'Escape') {
|
|
79
|
+
event.preventDefault();
|
|
80
|
+
event.stopPropagation();
|
|
81
|
+
close(true);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const buttons = Array.from(event.currentTarget.querySelectorAll('button'));
|
|
85
|
+
const index = buttons.indexOf(document.activeElement as HTMLButtonElement);
|
|
86
|
+
const next = {ArrowDown: (index + 1) % buttons.length, ArrowUp: (index + buttons.length - 1) % buttons.length,
|
|
87
|
+
Home: 0, End: buttons.length - 1}[event.key];
|
|
88
|
+
if (next === undefined) return;
|
|
89
|
+
event.preventDefault();
|
|
90
|
+
event.stopPropagation();
|
|
91
|
+
buttons[next]?.focus();
|
|
92
|
+
}}>
|
|
93
|
+
{actions.map(action => <button key={action.label} type="button" role="menuitem" tabIndex={-1}
|
|
94
|
+
onClick={action.run}>{action.label}</button>)}
|
|
95
|
+
</div>, document.body);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function ExplorerFileDialog({anchor, name, folder, action, message, close, submit}: {
|
|
99
|
+
anchor: MenuAnchor; name: string; folder: boolean; action: FileAction; message: ActionMessages;
|
|
100
|
+
close(): void; submit(name: string): void;
|
|
101
|
+
}) {
|
|
102
|
+
const ref = React.useRef<HTMLDialogElement>(null);
|
|
103
|
+
const input = React.useRef<HTMLInputElement>(null);
|
|
104
|
+
const cancel = React.useRef<HTMLButtonElement>(null);
|
|
105
|
+
const title = action === 'createFolder' ? message.createFolder : action === 'rename' ? message.renameTitle : message.deleteTitle;
|
|
106
|
+
React.useLayoutEffect(() => {
|
|
107
|
+
const dialog = ref.current!;
|
|
108
|
+
dialog.showModal();
|
|
109
|
+
if (input.current) {input.current.focus(); input.current.select();}
|
|
110
|
+
else cancel.current?.focus();
|
|
111
|
+
return () => {
|
|
112
|
+
dialog.close();
|
|
113
|
+
if (anchor.row.isConnected) anchor.row.focus({preventScroll: true});
|
|
114
|
+
};
|
|
115
|
+
}, []);
|
|
116
|
+
return createPortal(<dialog ref={ref} aria-label={title} className="twpf-explorer-dialog" onCancel={event => {
|
|
117
|
+
event.preventDefault();
|
|
118
|
+
close();
|
|
119
|
+
}}>
|
|
120
|
+
<form onSubmit={event => {
|
|
121
|
+
event.preventDefault();
|
|
122
|
+
submit(input.current?.value ?? name);
|
|
123
|
+
}}>
|
|
124
|
+
<h2>{title}</h2>
|
|
125
|
+
{action === 'delete' ? <p>{message.remove(name, folder)}</p> :
|
|
126
|
+
<label>{action === 'rename' ? message.newName : message.folderName}
|
|
127
|
+
<input ref={input} defaultValue={action === 'rename' ? name : ''} required />
|
|
128
|
+
</label>}
|
|
129
|
+
<div className="twpf-explorer-dialog-footer">
|
|
130
|
+
<button ref={cancel} type="button" onClick={close}>{message.cancel}</button>
|
|
131
|
+
<button type="submit">{title}</button>
|
|
132
|
+
</div>
|
|
133
|
+
</form>
|
|
134
|
+
</dialog>, anchor.row.ownerDocument.body);
|
|
135
|
+
}
|