@kubohiroya/turbowarp-plus-editor-project-folder 0.6.0 → 0.7.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 +20 -2
- package/README.md +14 -2
- package/dist/THIRD_PARTY_NOTICES.md +5 -1
- package/dist/explorer.d.ts +37 -0
- package/dist/explorer.js +1008 -0
- package/dist/index.js +360 -614
- package/dist/session.d.ts +7 -2
- package/dist/shared-DLcytVOx.js +445 -0
- package/dist/source.d.ts +30 -1
- package/package.json +24 -1
- package/src/explorer.tsx +349 -0
- package/src/index.ts +7 -1
- package/src/session.ts +87 -60
- package/src/source.ts +212 -23
- package/tsconfig.json +2 -1
- package/vite.config.ts +12 -2
package/src/explorer.tsx
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import {asyncDataLoaderFeature, hotkeysCoreFeature, selectionFeature} from '@headless-tree/core';
|
|
3
|
+
import type {ItemInstance} from '@headless-tree/core';
|
|
4
|
+
import {useTree} from '@headless-tree/react/react17';
|
|
5
|
+
import {isObject, manifestFile, projectFile} from './source.js';
|
|
6
|
+
import type {Json, Project} from './source.js';
|
|
7
|
+
|
|
8
|
+
export type ExplorerMark = 'added' | 'modified' | 'deleted' | 'untracked';
|
|
9
|
+
export type ExplorerEntry = {path: string; name: string; kind: 'file' | 'directory'};
|
|
10
|
+
export type ProjectFolderExplorerProps = {
|
|
11
|
+
/** The folder to show: a project folder or a repository root. It is only read. */
|
|
12
|
+
directory: FileSystemDirectoryHandle;
|
|
13
|
+
/** 'ja…' shows Japanese text; anything else shows English. */
|
|
14
|
+
locale?: string;
|
|
15
|
+
/** Extra text for an entry, by path relative to `directory`, such as `assets/<md5>.svg`. */
|
|
16
|
+
descriptions?: ReadonlyMap<string, string>;
|
|
17
|
+
/** Git status for an entry, by path relative to `directory`. */
|
|
18
|
+
marks?: ReadonlyMap<string, ExplorerMark>;
|
|
19
|
+
/** Called when a file is opened with Enter, Space or a click. */
|
|
20
|
+
onOpen?(entry: ExplorerEntry): void;
|
|
21
|
+
/** The tree's accessible name. Defaults to "Project folder". */
|
|
22
|
+
label?: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const messages = {
|
|
26
|
+
en: {
|
|
27
|
+
label: 'Project folder', loading: 'Loading…', empty: 'This folder is empty.',
|
|
28
|
+
failed: 'This folder could not be read.', denied: 'Permission to read this folder was not granted.',
|
|
29
|
+
separator: ', ',
|
|
30
|
+
marks: {added: 'Added', modified: 'Modified', deleted: 'Deleted', untracked: 'Untracked'},
|
|
31
|
+
project: 'Project data', manifest: 'Folder manifest', extensions: 'Embedded extensions',
|
|
32
|
+
assetsDirectory: 'Costume and sound files', extensionsDirectory: 'Extension files',
|
|
33
|
+
costume: (sprite: string, name: string) => `${sprite} · costume ${name}`,
|
|
34
|
+
backdrop: (name: string) => `Stage · backdrop ${name}`,
|
|
35
|
+
sound: (sprite: string, name: string) => `${sprite} · sound ${name}`,
|
|
36
|
+
stageSound: (name: string) => `Stage · sound ${name}`
|
|
37
|
+
},
|
|
38
|
+
ja: {
|
|
39
|
+
label: '作品フォルダ', loading: '読み込んでいます…', empty: 'このフォルダは空です。',
|
|
40
|
+
failed: 'このフォルダを読み込めません。', denied: 'このフォルダを読む許可がありません。',
|
|
41
|
+
separator: '、',
|
|
42
|
+
marks: {added: '追加', modified: '変更', deleted: '削除', untracked: '未追跡'},
|
|
43
|
+
project: '作品データ', manifest: '作品フォルダの目録', extensions: '埋め込み拡張の一覧',
|
|
44
|
+
assetsDirectory: 'コスチュームと音のファイル', extensionsDirectory: '拡張のファイル',
|
|
45
|
+
costume: (sprite: string, name: string) => `${sprite} のコスチューム ${name}`,
|
|
46
|
+
backdrop: (name: string) => `ステージの背景 ${name}`,
|
|
47
|
+
sound: (sprite: string, name: string) => `${sprite} の音 ${name}`,
|
|
48
|
+
stageSound: (name: string) => `ステージの音 ${name}`
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const messagesFor = (locale = 'en') => locale.toLowerCase().startsWith('ja') ? messages.ja : messages.en;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Describes the files of a project folder for `ProjectFolderExplorer`'s `descriptions`: the project, manifest and
|
|
55
|
+
* extension files, and each asset file with the costumes and sounds that use it. `folder` is the project folder's
|
|
56
|
+
* path inside the explorer's root ('' when the root is the project folder). Malformed entries are skipped.
|
|
57
|
+
*/
|
|
58
|
+
export function describeProjectFolder(project: Project, locale?: string, {manifest, folder = ''}: {
|
|
59
|
+
manifest?: Json; folder?: string;
|
|
60
|
+
} = {}): Map<string, string> {
|
|
61
|
+
const message = messagesFor(locale);
|
|
62
|
+
const prefix = folder.replace(/^\/+|\/+$/gu, '');
|
|
63
|
+
const at = (name: string) => prefix ? `${prefix}/${name}` : name;
|
|
64
|
+
const assetsDirectory = isObject(manifest) && typeof manifest.assetsDirectory === 'string' && manifest.assetsDirectory ?
|
|
65
|
+
manifest.assetsDirectory.replace(/^\/+|\/+$/gu, '') : 'assets';
|
|
66
|
+
const extensions = isObject(manifest) && typeof manifest.embeddedExtensions === 'string' ?
|
|
67
|
+
manifest.embeddedExtensions : 'embedded-extensions.json';
|
|
68
|
+
const descriptions = new Map<string, string>([
|
|
69
|
+
[at(projectFile), message.project],
|
|
70
|
+
[at(manifestFile), message.manifest],
|
|
71
|
+
[at(extensions), message.extensions],
|
|
72
|
+
[at(assetsDirectory), message.assetsDirectory],
|
|
73
|
+
[at('extensions'), message.extensionsDirectory]
|
|
74
|
+
]);
|
|
75
|
+
const users = new Map<string, string[]>();
|
|
76
|
+
for (const target of Array.isArray(project.targets) ? project.targets : []) {
|
|
77
|
+
if (!isObject(target)) continue;
|
|
78
|
+
const sprite = typeof target.name === 'string' ? target.name : '';
|
|
79
|
+
for (const kind of ['costumes', 'sounds'] as const) {
|
|
80
|
+
const entries = target[kind];
|
|
81
|
+
if (!Array.isArray(entries)) continue;
|
|
82
|
+
for (const entry of entries) {
|
|
83
|
+
if (!isObject(entry) || typeof entry.assetId !== 'string' || typeof entry.dataFormat !== 'string') continue;
|
|
84
|
+
const file = typeof entry.md5ext === 'string' ? entry.md5ext : `${entry.assetId}.${entry.dataFormat}`;
|
|
85
|
+
const name = typeof entry.name === 'string' ? entry.name : '';
|
|
86
|
+
const text = kind === 'costumes' ?
|
|
87
|
+
(target.isStage ? message.backdrop(name) : message.costume(sprite, name)) :
|
|
88
|
+
(target.isStage ? message.stageSound(name) : message.sound(sprite, name));
|
|
89
|
+
const path = at(`${assetsDirectory}/${file}`);
|
|
90
|
+
const list = users.get(path) ?? [];
|
|
91
|
+
if (!list.includes(text)) list.push(text);
|
|
92
|
+
users.set(path, list);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
for (const [path, list] of users) descriptions.set(path, list.join(message.separator));
|
|
97
|
+
return descriptions;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
type Node = {name: string; path: string; kind: 'file' | 'directory' | 'error'; message?: string};
|
|
101
|
+
type Message = typeof messages.en;
|
|
102
|
+
const rootId = '/';
|
|
103
|
+
// '\0' cannot appear in a file name, so the error row never collides with a real entry.
|
|
104
|
+
const errorId = (parent: string) => `${parent}\0error`;
|
|
105
|
+
type IterableDirectory = FileSystemDirectoryHandle & {values(): AsyncIterable<FileSystemHandle>};
|
|
106
|
+
|
|
107
|
+
const failure = (error: unknown, message: Message) =>
|
|
108
|
+
error instanceof DOMException && (error.name === 'NotAllowedError' || error.name === 'SecurityError') ?
|
|
109
|
+
message.denied : message.failed;
|
|
110
|
+
|
|
111
|
+
const prefix = 'twpf-explorer';
|
|
112
|
+
// Colors come from the host's text color, so the tree follows light and dark themes.
|
|
113
|
+
const css = `.${prefix}{font:inherit;color:inherit}
|
|
114
|
+
.${prefix}-item{display:flex;align-items:center;gap:.4em;width:100%;box-sizing:border-box;margin:0;border:0;border-radius:3px;
|
|
115
|
+
padding-block:.15em;padding-inline-end:.4em;background:none;color:inherit;font:inherit;text-align:start;cursor:pointer;white-space:nowrap}
|
|
116
|
+
.${prefix}-item:hover{background:rgba(128,128,128,.12)}
|
|
117
|
+
.${prefix}-item[aria-selected="true"]{background:rgba(128,128,128,.24)}
|
|
118
|
+
.${prefix}-item:focus-visible{outline:2px solid currentColor;outline-offset:-2px}
|
|
119
|
+
.${prefix}-caret{display:inline-block;width:1em;flex:none;text-align:center}
|
|
120
|
+
.${prefix}-icon{display:inline-flex;align-items:center;justify-content:center;width:20px;height:20px;flex:none}
|
|
121
|
+
.${prefix}-icon img{width:20px;height:20px;object-fit:contain;border-radius:2px;
|
|
122
|
+
background:repeating-conic-gradient(rgba(128,128,128,.22) 0 25%,transparent 0 50%) 0 0/8px 8px}
|
|
123
|
+
.${prefix}-icon img[hidden]{display:none}
|
|
124
|
+
.${prefix}-name{overflow:hidden;text-overflow:ellipsis}
|
|
125
|
+
.${prefix}-mark{flex:none;font-size:.85em;padding:0 .3em;border:1px solid currentColor;border-radius:3px;opacity:.85}
|
|
126
|
+
.${prefix}-description{overflow:hidden;text-overflow:ellipsis;opacity:.7;font-size:.9em}
|
|
127
|
+
.${prefix}-status{opacity:.7;font-style:italic;padding-block:.15em}`;
|
|
128
|
+
|
|
129
|
+
const indent = (level: number) => ({paddingInlineStart: `${0.25 + level * 1.1}em`});
|
|
130
|
+
|
|
131
|
+
type IconKind = 'folder' | 'folder-open' | 'project' | 'json' | 'image' | 'sound' | 'code' | 'text' | 'file';
|
|
132
|
+
const page = 'M3.5 1.5h6l3 3v10h-9z';
|
|
133
|
+
const corner = 'M9.5 1.5v3h3';
|
|
134
|
+
// 16px outlines drawn with currentColor, so they follow the host's text color.
|
|
135
|
+
const icons: Record<IconKind, string[]> = {
|
|
136
|
+
'folder': ['M1.5 3.5h4l1.5 1.5h7.5v8h-13z'],
|
|
137
|
+
'folder-open': ['M1.5 13V3.5h4l1.5 1.5h6v2', 'M1.5 13l2-6h11l-2 6z'],
|
|
138
|
+
'project': ['M8 1.5l6 3v7l-6 3-6-3v-7z', 'M2 4.5l6 3 6-3', 'M8 7.5v7'],
|
|
139
|
+
'json': [page, corner, 'M7 7c-1 0-1 .6-1 1.4s-.3 1.1-.8 1.1c.5 0 .8.4.8 1.1S6 12 7 12',
|
|
140
|
+
'M9 7c1 0 1 .6 1 1.4s.3 1.1.8 1.1c-.5 0-.8.4-.8 1.1S10 12 9 12'],
|
|
141
|
+
'image': ['M1.5 2.5h13v11h-13z', 'M1.5 11l4-4 3 3 2-2 4 4', 'M12.2 5.5a1.2 1.2 0 1 1-2.4 0a1.2 1.2 0 1 1 2.4 0'],
|
|
142
|
+
'sound': ['M2 6h3l3.5-3v10L5 10H2z', 'M10.5 5.5a3.5 3.5 0 0 1 0 5', 'M12.5 3.5a6.5 6.5 0 0 1 0 9'],
|
|
143
|
+
'code': ['M5.5 4.5L2 8l3.5 3.5', 'M10.5 4.5L14 8l-3.5 3.5', 'M9 3l-2 10'],
|
|
144
|
+
'text': [page, corner, 'M5.5 7.5h5M5.5 9.5h5M5.5 11.5h3'],
|
|
145
|
+
'file': [page, corner]
|
|
146
|
+
};
|
|
147
|
+
const extensionIcons: Record<string, IconKind> = {
|
|
148
|
+
json: 'json', svg: 'image', png: 'image', jpg: 'image', jpeg: 'image', gif: 'image', bmp: 'image', webp: 'image',
|
|
149
|
+
wav: 'sound', mp3: 'sound', ogg: 'sound', js: 'code', mjs: 'code', ts: 'code', md: 'text', txt: 'text'
|
|
150
|
+
};
|
|
151
|
+
const iconFor = (name: string): IconKind => name === projectFile || name === manifestFile ? 'project' :
|
|
152
|
+
extensionIcons[name.slice(name.lastIndexOf('.') + 1).toLowerCase()] ?? 'file';
|
|
153
|
+
// Larger images are left as icons: decoding them for a 20px preview costs more than it shows.
|
|
154
|
+
const thumbnailLimit = 2 * 1024 * 1024;
|
|
155
|
+
type Thumbnail = 'waiting' | 'loading' | 'loaded' | 'skipped' | 'failed';
|
|
156
|
+
|
|
157
|
+
function Icon({kind, path, readFile}: {kind: IconKind; path: string; readFile?: (path: string) => Promise<File>}) {
|
|
158
|
+
const ref = React.useRef<HTMLSpanElement>(null);
|
|
159
|
+
const [state, setState] = React.useState<Thumbnail>('waiting');
|
|
160
|
+
const [url, setUrl] = React.useState<string>();
|
|
161
|
+
// A row keeps its path for its lifetime (rows are keyed by path, and a new directory remounts the tree).
|
|
162
|
+
React.useEffect(() => {
|
|
163
|
+
if (!readFile) return undefined;
|
|
164
|
+
let live = true;
|
|
165
|
+
let created: string | undefined;
|
|
166
|
+
const load = async () => {
|
|
167
|
+
setState('loading');
|
|
168
|
+
try {
|
|
169
|
+
const file = await readFile(path);
|
|
170
|
+
if (!live) return;
|
|
171
|
+
if (file.size > thumbnailLimit) return setState('skipped');
|
|
172
|
+
created = URL.createObjectURL(file);
|
|
173
|
+
setUrl(created);
|
|
174
|
+
} catch {
|
|
175
|
+
if (live) setState('failed');
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
// Only rows that are shown read their file, so a large assets folder does not read every image at once.
|
|
179
|
+
let observer: IntersectionObserver | undefined;
|
|
180
|
+
if (typeof IntersectionObserver === 'function' && ref.current) {
|
|
181
|
+
observer = new IntersectionObserver(entries => {
|
|
182
|
+
if (!entries.some(entry => entry.isIntersecting)) return;
|
|
183
|
+
observer?.disconnect();
|
|
184
|
+
void load();
|
|
185
|
+
});
|
|
186
|
+
observer.observe(ref.current);
|
|
187
|
+
} else {
|
|
188
|
+
void load();
|
|
189
|
+
}
|
|
190
|
+
return () => {
|
|
191
|
+
live = false;
|
|
192
|
+
observer?.disconnect();
|
|
193
|
+
if (created) URL.revokeObjectURL(created);
|
|
194
|
+
};
|
|
195
|
+
}, [path]);
|
|
196
|
+
return (
|
|
197
|
+
<span ref={ref} className={`${prefix}-icon`} aria-hidden="true" data-icon={kind}
|
|
198
|
+
data-thumbnail={readFile ? state : undefined}>
|
|
199
|
+
{state === 'loaded' ? null : (
|
|
200
|
+
<svg viewBox="0 0 16 16" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.2"
|
|
201
|
+
strokeLinecap="round" strokeLinejoin="round" focusable="false">
|
|
202
|
+
{icons[kind].map(d => <path key={d} d={d} />)}
|
|
203
|
+
</svg>
|
|
204
|
+
)}
|
|
205
|
+
{/* An <img> never runs an SVG's scripts, unlike markup inserted into the page. */}
|
|
206
|
+
{url && state !== 'failed' ? (
|
|
207
|
+
<img src={url} alt="" decoding="async" hidden={state !== 'loaded'}
|
|
208
|
+
onLoad={() => setState('loaded')} onError={() => setState('failed')} />
|
|
209
|
+
) : null}
|
|
210
|
+
</span>
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function Explorer({directory, locale, descriptions, marks, onOpen, label}: ProjectFolderExplorerProps) {
|
|
215
|
+
const message = messagesFor(locale);
|
|
216
|
+
const [handles] = React.useState(() => new Map<string, FileSystemDirectoryHandle>([[rootId, directory]]));
|
|
217
|
+
const [files] = React.useState(() => new Map<string, FileSystemFileHandle>());
|
|
218
|
+
const [rootLoaded, setRootLoaded] = React.useState(false);
|
|
219
|
+
const collator = React.useMemo(() => new Intl.Collator(locale, {numeric: true, sensitivity: 'base'}), [locale]);
|
|
220
|
+
|
|
221
|
+
const directoryAt = async (id: string) => {
|
|
222
|
+
let handle = handles.get(id);
|
|
223
|
+
if (handle) return handle;
|
|
224
|
+
handle = directory;
|
|
225
|
+
for (const name of id.split('/')) handle = await handle.getDirectoryHandle(name);
|
|
226
|
+
handles.set(id, handle);
|
|
227
|
+
return handle;
|
|
228
|
+
};
|
|
229
|
+
const readFile = async (path: string) => {
|
|
230
|
+
let handle = files.get(path);
|
|
231
|
+
if (!handle) {
|
|
232
|
+
const parts = path.split('/');
|
|
233
|
+
const name = parts.pop()!;
|
|
234
|
+
handle = await (parts.length ? await directoryAt(parts.join('/')) : directory).getFileHandle(name);
|
|
235
|
+
}
|
|
236
|
+
return handle.getFile();
|
|
237
|
+
};
|
|
238
|
+
const tree = useTree<Node>({
|
|
239
|
+
rootItemId: rootId,
|
|
240
|
+
initialState: {expandedItems: [], selectedItems: [], focusedItem: null},
|
|
241
|
+
getItemName: item => item.getItemData()?.name ?? '',
|
|
242
|
+
isItemFolder: item => item.getItemData()?.kind === 'directory',
|
|
243
|
+
createLoadingItemData: () => ({name: '', path: '', kind: 'file'}),
|
|
244
|
+
onPrimaryAction: item => {
|
|
245
|
+
const data = item.getItemData();
|
|
246
|
+
if (data?.kind === 'file') onOpen?.({path: data.path, name: data.name, kind: 'file'});
|
|
247
|
+
},
|
|
248
|
+
onLoadedChildren: id => {if (id === rootId) setRootLoaded(true);},
|
|
249
|
+
dataLoader: {
|
|
250
|
+
getItem: async id => {
|
|
251
|
+
if (id === rootId) return {name: directory.name, path: '', kind: 'directory'};
|
|
252
|
+
const parts = id.split('/');
|
|
253
|
+
const name = parts.pop()!;
|
|
254
|
+
const parent = parts.length ? await directoryAt(parts.join('/')) : directory;
|
|
255
|
+
try {
|
|
256
|
+
await parent.getDirectoryHandle(name);
|
|
257
|
+
return {name, path: id, kind: 'directory'};
|
|
258
|
+
} catch {
|
|
259
|
+
await parent.getFileHandle(name);
|
|
260
|
+
return {name, path: id, kind: 'file'};
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
getChildrenWithData: async id => {
|
|
264
|
+
try {
|
|
265
|
+
const handle = await directoryAt(id);
|
|
266
|
+
const base = id === rootId ? '' : `${id}/`;
|
|
267
|
+
const nodes: Node[] = [];
|
|
268
|
+
for await (const entry of (handle as IterableDirectory).values()) {
|
|
269
|
+
if (entry.kind === 'directory' && entry.name === '.git') continue;
|
|
270
|
+
const path = base + entry.name;
|
|
271
|
+
if (entry.kind === 'directory') handles.set(path, entry as FileSystemDirectoryHandle);
|
|
272
|
+
else files.set(path, entry as FileSystemFileHandle);
|
|
273
|
+
nodes.push({name: entry.name, path, kind: entry.kind});
|
|
274
|
+
}
|
|
275
|
+
nodes.sort((a, b) => (a.kind === 'directory' ? 0 : 1) - (b.kind === 'directory' ? 0 : 1) ||
|
|
276
|
+
collator.compare(a.name, b.name) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
277
|
+
return nodes.map(data => ({id: data.path, data}));
|
|
278
|
+
} catch (error) {
|
|
279
|
+
return [{id: errorId(id), data: {name: failure(error, message), path: '', kind: 'error'}}];
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
features: [asyncDataLoaderFeature, selectionFeature, hotkeysCoreFeature]
|
|
284
|
+
});
|
|
285
|
+
// The React 17 hook of Headless Tree 1.8.0 never marks the tree as mounted, unlike its React 18 hook, so every state
|
|
286
|
+
// update and rebuild would wait forever. Marking it here also runs the rebuild that the hook queued on mount.
|
|
287
|
+
React.useEffect(() => {
|
|
288
|
+
tree.setMounted(true);
|
|
289
|
+
return () => tree.setMounted(false);
|
|
290
|
+
}, [tree]);
|
|
291
|
+
|
|
292
|
+
const renderItem = (item: ItemInstance<Node>) => {
|
|
293
|
+
const data = item.getItemData();
|
|
294
|
+
const level = item.getItemMeta().level;
|
|
295
|
+
const folder = item.isFolder();
|
|
296
|
+
const expanded = folder && item.isExpanded();
|
|
297
|
+
const mark = data && data.kind !== 'error' ? marks?.get(data.path) : undefined;
|
|
298
|
+
const markLabel = mark ? message.marks[mark] : '';
|
|
299
|
+
const description = data && data.kind !== 'error' ? descriptions?.get(data.path) : undefined;
|
|
300
|
+
const icon = !data || data.kind === 'error' ? undefined :
|
|
301
|
+
folder ? (expanded ? 'folder-open' : 'folder') : iconFor(data.name);
|
|
302
|
+
const accessibleName = [item.getItemName(), markLabel, description].filter(Boolean).join(message.separator);
|
|
303
|
+
const rows = [
|
|
304
|
+
<button {...item.getProps()} key={item.getId()} type="button" aria-label={accessibleName}
|
|
305
|
+
className={`${prefix}-item${data?.kind === 'error' ? ` ${prefix}-error` : ''}`} style={indent(level)}
|
|
306
|
+
data-path={data?.path}>
|
|
307
|
+
<span className={`${prefix}-caret`} aria-hidden="true">{folder ? (expanded ? '▾' : '▸') : ''}</span>
|
|
308
|
+
{icon ? <Icon kind={icon} path={data!.path} readFile={icon === 'image' ? readFile : undefined} /> : null}
|
|
309
|
+
<span className={`${prefix}-name`}>{item.getItemName()}</span>
|
|
310
|
+
{markLabel ? <span className={`${prefix}-mark`} aria-hidden="true">{markLabel}</span> : null}
|
|
311
|
+
{description ? <span className={`${prefix}-description`} aria-hidden="true">{description}</span> : null}
|
|
312
|
+
</button>
|
|
313
|
+
];
|
|
314
|
+
if (expanded && item.isLoading()) {
|
|
315
|
+
rows.push(<div key={`${item.getId()}\0loading`} className={`${prefix}-status`} role="status"
|
|
316
|
+
style={indent(level + 1)}>{message.loading}</div>);
|
|
317
|
+
}
|
|
318
|
+
return rows;
|
|
319
|
+
};
|
|
320
|
+
const items = tree.getItems();
|
|
321
|
+
return (
|
|
322
|
+
<div className={prefix}>
|
|
323
|
+
<style>{css}</style>
|
|
324
|
+
<div {...tree.getContainerProps(label ?? message.label)}>
|
|
325
|
+
{items.map(renderItem)}
|
|
326
|
+
</div>
|
|
327
|
+
{items.length ? null : (
|
|
328
|
+
<div className={`${prefix}-status`} role="status">{rootLoaded ? message.empty : message.loading}</div>
|
|
329
|
+
)}
|
|
330
|
+
</div>
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
let nextKey = 0;
|
|
335
|
+
const keys = new WeakMap<FileSystemDirectoryHandle, number>();
|
|
336
|
+
const keyFor = (directory: FileSystemDirectoryHandle) => {
|
|
337
|
+
if (!keys.has(directory)) keys.set(directory, nextKey++);
|
|
338
|
+
return keys.get(directory)!;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* A read-only tree of a folder for the Editor's side panel, built on Headless Tree. Folders load when expanded, folders
|
|
343
|
+
* sort before files, and `.git` is hidden. Keyboard: arrow keys, Home and End move and expand; Enter or Space opens a
|
|
344
|
+
* file. Needs React 16.14–18 from the host.
|
|
345
|
+
*/
|
|
346
|
+
export function ProjectFolderExplorer(props: ProjectFolderExplorerProps) {
|
|
347
|
+
// A new directory starts a new tree, so cached entries and expanded folders never leak between folders.
|
|
348
|
+
return <Explorer key={keyFor(props.directory)} {...props} />;
|
|
349
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ const messages = {
|
|
|
11
11
|
open: 'Open Project Folder…', save: 'Save to Project Folder',
|
|
12
12
|
replace: 'Replace the open project and discard its unsaved changes?',
|
|
13
13
|
opened: 'Project folder opened.', saved: 'Project folder saved.', unchanged: 'No changes to save.',
|
|
14
|
+
sourceRemoved: (ids: string[]) => `Removed source metadata for replaced extensions: ${ids.join(', ')}. Use sb3-toolchain to fetch the managed sources again if you need to keep their provenance.`,
|
|
14
15
|
conflict: 'The project folder changed outside the editor or is no longer open. Your edits are still in the editor.',
|
|
15
16
|
unsupported: 'This project or edit cannot be saved to a project folder yet.',
|
|
16
17
|
failed: 'Could not access the project folder.',
|
|
@@ -31,6 +32,7 @@ const messages = {
|
|
|
31
32
|
open: 'プロジェクトフォルダを開く…', save: 'プロジェクトフォルダに保存',
|
|
32
33
|
replace: '未保存の変更を破棄して、別の作品を開きますか?',
|
|
33
34
|
opened: 'プロジェクトフォルダを開きました。', saved: 'プロジェクトフォルダに保存しました。', unchanged: '保存する変更はありません。',
|
|
35
|
+
sourceRemoved: (ids: string[]) => `差し替えた拡張機能(${ids.join('、')})の出どころ情報を削除しました。出どころを保つには、sb3-toolchain で拡張機能を取得し直してください。`,
|
|
34
36
|
conflict: '外部でフォルダが変更されたか、作品が切り替わりました。編集中の変更はエディターに残っています。',
|
|
35
37
|
unsupported: 'この作品または編集は、まだプロジェクトフォルダに保存できません。',
|
|
36
38
|
failed: 'プロジェクトフォルダにアクセスできませんでした。',
|
|
@@ -229,7 +231,11 @@ export function mountProjectFolder({vm, document, host, locale = 'en', autoReloa
|
|
|
229
231
|
},
|
|
230
232
|
save(locale) {
|
|
231
233
|
if (!session.busy) {
|
|
232
|
-
|
|
234
|
+
const message = messageFor(locale);
|
|
235
|
+
void session.save().then(({status, sourceRemoved}) => {
|
|
236
|
+
const detail = sourceRemoved.length ? ` ${message.sourceRemoved(sourceRemoved)}` : '';
|
|
237
|
+
report(message[status] + detail);
|
|
238
|
+
}, error => fail(locale, error)).finally(renderReload);
|
|
233
239
|
renderReload();
|
|
234
240
|
}
|
|
235
241
|
}
|
package/src/session.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {assetKey, assetTypeFor, assertFolderUnchanged, fileVersion, FolderError, manifestFile, parseProject,
|
|
2
|
-
projectFile, readFolder, readFolderUpdate, unsupported} from './source.js';
|
|
1
|
+
import {assetKey, assetTypeFor, assertFolderUnchanged, extensionsFile, fileVersion, FolderError, manifestFile, parseProject,
|
|
2
|
+
planExtensionSave, projectAssets, projectChanges, projectFile, readFolder, readFolderUpdate, sameBytes, unsupported} from './source.js';
|
|
3
3
|
import type {Asset, FolderSource, FolderUpdate, JsonObject, Project, Storage} from './source.js';
|
|
4
4
|
import {applyBlockReload, planBlockReload} from './block-reload.js';
|
|
5
5
|
import type {ReloadTarget} from './block-reload.js';
|
|
@@ -17,7 +17,7 @@ export interface FolderVM {
|
|
|
17
17
|
on(event: 'PROJECT_CHANGED', listener: () => void): void;
|
|
18
18
|
off(event: 'PROJECT_CHANGED', listener: () => void): void;
|
|
19
19
|
quit(): void;
|
|
20
|
-
loadProject(project: string): Promise<unknown>;
|
|
20
|
+
loadProject(project: string, options?: {replaceLegacyExtensions: boolean}): Promise<unknown>;
|
|
21
21
|
toJSON(target?: string, options?: {allowOptimization: boolean}): string;
|
|
22
22
|
renderer?: {draw(): void};
|
|
23
23
|
}
|
|
@@ -83,7 +83,8 @@ export type ReloadState = 'ready' | 'full' | 'waiting' | 'dirty' | 'running' | '
|
|
|
83
83
|
/** What the reload command does for the pending change, regardless of whether it may run now. */
|
|
84
84
|
export type ReloadKind = 'partial' | 'full' | null;
|
|
85
85
|
export type OpenResult = 'opened' | 'cancelled';
|
|
86
|
-
|
|
86
|
+
type SaveResult = {status: 'saved' | 'unchanged'; sourceRemoved: string[]};
|
|
87
|
+
type SaveDirectory = {directory: FileSystemDirectoryHandle; files: Map<string, Uint8Array>; prefix: string};
|
|
87
88
|
|
|
88
89
|
type VMAsset = {assetId: string; dataFormat: string; data?: Uint8Array};
|
|
89
90
|
|
|
@@ -112,7 +113,7 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
112
113
|
return session.ids && after ? {before: session.ids, after} : undefined;
|
|
113
114
|
};
|
|
114
115
|
// Writes a whole file and, for a file the folder already had, checks the folder before the write takes effect.
|
|
115
|
-
// A new
|
|
116
|
+
// A new file is empty until then, so it is checked by the writes that follow it instead.
|
|
116
117
|
const writeChecked = async (session: Session, handle: FileSystemFileHandle, data: Uint8Array, existing: boolean) => {
|
|
117
118
|
const options = (existing ? {mode: 'exclusive'} : {}) as FileSystemCreateWritableOptions;
|
|
118
119
|
const writable = await handle.createWritable(options);
|
|
@@ -126,11 +127,6 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
126
127
|
throw error;
|
|
127
128
|
}
|
|
128
129
|
};
|
|
129
|
-
const rewrite = async (directory: FileSystemDirectoryHandle, name: string, data: Uint8Array) => {
|
|
130
|
-
const writable = await (await directory.getFileHandle(name)).createWritable();
|
|
131
|
-
await writable.write(data as Uint8Array<ArrayBuffer>);
|
|
132
|
-
await writable.close();
|
|
133
|
-
};
|
|
134
130
|
// The bytes of an asset the Editor holds, such as a costume added since the folder was opened.
|
|
135
131
|
const assetData = (name: string) => {
|
|
136
132
|
for (const target of runtimeTargets()) {
|
|
@@ -178,7 +174,7 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
178
174
|
let stage: object | null;
|
|
179
175
|
try {
|
|
180
176
|
vm.quit();
|
|
181
|
-
await vm.loadProject(source.
|
|
177
|
+
await vm.loadProject(source.loadText, {replaceLegacyExtensions: true});
|
|
182
178
|
stage = vm.runtime.getTargetForStage();
|
|
183
179
|
} finally {
|
|
184
180
|
if (reader.assets === assets) reader.assets = null;
|
|
@@ -265,7 +261,7 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
265
261
|
try {
|
|
266
262
|
const edited = snapshot();
|
|
267
263
|
const result = projectChanges(session.source.project, session.baseline, edited, identity(session, edited));
|
|
268
|
-
editor = result ? `${JSON.stringify(result, null, 2)}\n` : base;
|
|
264
|
+
editor = result ? `${JSON.stringify(planExtensionSave(session.source, result).project, null, 2)}\n` : base;
|
|
269
265
|
} catch (error) {
|
|
270
266
|
if (!(error instanceof FolderError)) throw error;
|
|
271
267
|
editor = null;
|
|
@@ -361,14 +357,9 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
361
357
|
const result = projectChanges(session.source.project, session.baseline, edited, identity(session, edited));
|
|
362
358
|
await assertFolderUnchanged(session.source);
|
|
363
359
|
active(session);
|
|
364
|
-
if (!result) {
|
|
365
|
-
if (revision === saveRevision) host.onSaved();
|
|
366
|
-
return 'unchanged';
|
|
367
|
-
}
|
|
368
360
|
const {source} = session;
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
const needed = projectAssets(result);
|
|
361
|
+
const extensionSave = planExtensionSave(source, result ?? source.project);
|
|
362
|
+
const needed = projectAssets(extensionSave.project);
|
|
372
363
|
const added = new Map<string, Uint8Array>();
|
|
373
364
|
for (const [name, kind] of needed) {
|
|
374
365
|
if (source.bytes.has(name)) continue;
|
|
@@ -380,6 +371,10 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
380
371
|
added.set(name, data);
|
|
381
372
|
}
|
|
382
373
|
const removed = [...source.bytes.keys()].filter(name => !needed.has(name));
|
|
374
|
+
if (!result && !removed.length && !extensionSave.removed.length) {
|
|
375
|
+
if (revision === saveRevision) host.onSaved();
|
|
376
|
+
return {status: 'unchanged', sourceRemoved: []};
|
|
377
|
+
}
|
|
383
378
|
const encoder = new TextEncoder();
|
|
384
379
|
let manifest: Uint8Array | null = null;
|
|
385
380
|
if (added.size || removed.length) {
|
|
@@ -388,58 +383,90 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
388
383
|
value.archiveEntries = [...entries, ...added.keys()];
|
|
389
384
|
manifest = encoder.encode(`${JSON.stringify(value, null, 2)}\n`);
|
|
390
385
|
}
|
|
391
|
-
const
|
|
392
|
-
const
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
386
|
+
const root = {directory: source.directory, files: source.files, prefix: ''};
|
|
387
|
+
const assets = {directory: await source.directory.getDirectoryHandle('assets'), files: source.bytes, prefix: 'assets/'};
|
|
388
|
+
const extensions = {directory: await source.directory.getDirectoryHandle('extensions'),
|
|
389
|
+
files: source.extensionBytes, prefix: 'extensions/'};
|
|
390
|
+
const undo: (() => Promise<void>)[] = [];
|
|
391
|
+
const write = async ({directory, files, prefix}: SaveDirectory, name: string, data: Uint8Array) => {
|
|
392
|
+
const previous = files.get(name);
|
|
393
|
+
if (!previous) {
|
|
394
|
+
const existing = await directory.getFileHandle(name).catch(error => {
|
|
395
|
+
if (error instanceof DOMException && error.name === 'NotFoundError') return null;
|
|
396
|
+
throw error;
|
|
397
|
+
});
|
|
398
|
+
if (existing) throw new FolderError('conflict');
|
|
403
399
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
400
|
+
const handle = await directory.getFileHandle(name, previous ? undefined : {create: true});
|
|
401
|
+
const key = prefix + name;
|
|
402
|
+
const previousVersion = source.versions.get(key);
|
|
403
|
+
let committed = false;
|
|
404
|
+
undo.push(async () => {
|
|
405
|
+
let version = previousVersion;
|
|
406
|
+
try {
|
|
407
|
+
if (!committed && previous) return;
|
|
408
|
+
const bytes = new Uint8Array(await (await handle.getFile()).arrayBuffer());
|
|
409
|
+
if (!sameBytes(bytes, committed ? data : new Uint8Array())) return;
|
|
410
|
+
if (previous) {
|
|
411
|
+
const writable = await handle.createWritable();
|
|
412
|
+
try {
|
|
413
|
+
await writable.write(previous as Uint8Array<ArrayBuffer>);
|
|
414
|
+
await writable.close();
|
|
415
|
+
} catch (error) {
|
|
416
|
+
await writable.abort().catch(() => {});
|
|
417
|
+
throw error;
|
|
418
|
+
}
|
|
419
|
+
version = fileVersion(await handle.getFile());
|
|
420
|
+
} else {
|
|
421
|
+
await directory.removeEntry(name);
|
|
422
|
+
}
|
|
423
|
+
} finally {
|
|
424
|
+
// If restoration failed, keep the old expectations so the next save reports a conflict.
|
|
425
|
+
if (previous) files.set(name, previous);
|
|
426
|
+
else files.delete(name);
|
|
427
|
+
if (version === undefined) source.versions.delete(key);
|
|
428
|
+
else source.versions.set(key, version);
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
await writeChecked(session, handle, data, previous !== undefined);
|
|
432
|
+
committed = true;
|
|
433
|
+
files.set(name, data);
|
|
434
|
+
source.versions.set(key, fileVersion(await handle.getFile()));
|
|
435
|
+
};
|
|
436
|
+
// Write the files that will be referenced before publishing the new manifests and project.
|
|
437
|
+
try {
|
|
438
|
+
for (const [name, data] of added) await write(assets, name, data);
|
|
439
|
+
for (const [name, data] of extensionSave.writes) await write(extensions, name, data);
|
|
440
|
+
if (manifest) await write(root, manifestFile, manifest);
|
|
441
|
+
if (extensionSave.manifestChanged) {
|
|
442
|
+
await write(root, extensionsFile, encoder.encode(`${JSON.stringify(extensionSave.embedded, null, 2)}\n`));
|
|
410
443
|
}
|
|
411
|
-
|
|
444
|
+
if (result) await write(root, projectFile, encoder.encode(`${JSON.stringify(extensionSave.project, null, 2)}\n`));
|
|
412
445
|
} catch (error) {
|
|
413
|
-
|
|
414
|
-
if (manifestWritten) {
|
|
415
|
-
await rewrite(source.directory, manifestFile, previousManifest).catch(() => {});
|
|
416
|
-
source.files.set(manifestFile, previousManifest);
|
|
417
|
-
source.versions.delete(manifestFile);
|
|
418
|
-
}
|
|
419
|
-
for (const name of written) {
|
|
420
|
-
await assets.removeEntry(name).catch(() => {});
|
|
421
|
-
source.bytes.delete(name);
|
|
422
|
-
source.versions.delete(`assets/${name}`);
|
|
423
|
-
}
|
|
446
|
+
for (const restore of undo.reverse()) await restore().catch(() => {});
|
|
424
447
|
throw error;
|
|
425
448
|
}
|
|
426
|
-
source.project =
|
|
427
|
-
source.
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
449
|
+
source.project = extensionSave.project;
|
|
450
|
+
source.embedded = extensionSave.embedded;
|
|
451
|
+
for (const [{directory, files, prefix}, names] of [[assets, removed], [extensions, extensionSave.removed]] as const) {
|
|
452
|
+
for (const name of names) {
|
|
453
|
+
try {
|
|
454
|
+
const file = await (await directory.getFileHandle(name)).getFile();
|
|
455
|
+
if (!sameBytes(new Uint8Array(await file.arrayBuffer()), files.get(name)!)) continue;
|
|
456
|
+
await directory.removeEntry(name);
|
|
457
|
+
} catch (error) {
|
|
458
|
+
// The next save retries cleanup.
|
|
459
|
+
if (!(error instanceof DOMException && error.name === 'NotFoundError')) continue;
|
|
460
|
+
}
|
|
461
|
+
files.delete(name);
|
|
462
|
+
source.versions.delete(prefix + name);
|
|
436
463
|
}
|
|
437
464
|
}
|
|
438
465
|
session.baseline = edited;
|
|
439
466
|
session.ids = ids;
|
|
440
467
|
session.pending = null;
|
|
441
468
|
if (current === session && revision === saveRevision) host.onSaved();
|
|
442
|
-
return 'saved';
|
|
469
|
+
return {status: 'saved', sourceRemoved: extensionSave.sourceRemoved};
|
|
443
470
|
} finally {
|
|
444
471
|
busy = false;
|
|
445
472
|
}
|