@kubohiroya/turbowarp-plus-editor-project-folder 0.5.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.
@@ -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
- void session.save().then(result => report(messageFor(locale)[result]), error => fail(locale, error)).finally(renderReload);
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,6 @@
1
- import {assetKey, assertFolderUnchanged, FolderError, parseProject, projectChanges, projectFile, readFolder, readFolderUpdate} from './source.js';
2
- import type {Asset, FolderSource, FolderUpdate, Project, Storage} 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
+ import type {Asset, FolderSource, FolderUpdate, JsonObject, Project, Storage} from './source.js';
3
4
  import {applyBlockReload, planBlockReload} from './block-reload.js';
4
5
  import type {ReloadTarget} from './block-reload.js';
5
6
  import {applyScriptReload, planScriptReload} from './script-reload.js';
@@ -16,7 +17,7 @@ export interface FolderVM {
16
17
  on(event: 'PROJECT_CHANGED', listener: () => void): void;
17
18
  off(event: 'PROJECT_CHANGED', listener: () => void): void;
18
19
  quit(): void;
19
- loadProject(project: string): Promise<unknown>;
20
+ loadProject(project: string, options?: {replaceLegacyExtensions: boolean}): Promise<unknown>;
20
21
  toJSON(target?: string, options?: {allowOptimization: boolean}): string;
21
22
  renderer?: {draw(): void};
22
23
  }
@@ -73,7 +74,7 @@ type Edits = {kind: 'values'; changes: ReturnType<typeof planBlockReload>} |
73
74
  {kind: 'scripts'; changes: ReturnType<typeof planScriptReload>};
74
75
  type PendingUpdate = {update: FolderUpdate; edits: Edits | null; stable: boolean};
75
76
  type Pending = PendingUpdate | 'structure' | 'waiting';
76
- type Session = {source: FolderSource; baseline: Project; stage: object; pending: Pending | null};
77
+ type Session = {source: FolderSource; baseline: Project; ids: string[] | undefined; stage: object; pending: Pending | null};
77
78
  /**
78
79
  * `ready`: the change can be applied in place: values and positions, or with `deserializeBlocks`, whole scripts. `full`: the change needs the whole project reloaded.
79
80
  * `waiting`: an external write looks incomplete and is checked again. `dirty`, `running` and `busy` hold the change.
@@ -82,7 +83,10 @@ export type ReloadState = 'ready' | 'full' | 'waiting' | 'dirty' | 'running' | '
82
83
  /** What the reload command does for the pending change, regardless of whether it may run now. */
83
84
  export type ReloadKind = 'partial' | 'full' | null;
84
85
  export type OpenResult = 'opened' | 'cancelled';
85
- export type SaveResult = 'saved' | 'unchanged';
86
+ type SaveResult = {status: 'saved' | 'unchanged'; sourceRemoved: string[]};
87
+ type SaveDirectory = {directory: FileSystemDirectoryHandle; files: Map<string, Uint8Array>; prefix: string};
88
+
89
+ type VMAsset = {assetId: string; dataFormat: string; data?: Uint8Array};
86
90
 
87
91
  export function createFolderSession(vm: FolderVM, host: FolderHost) {
88
92
  let current: Session | null = null;
@@ -91,6 +95,48 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
91
95
  let revision = 0;
92
96
  let scanning: Promise<void> | null = null;
93
97
  const snapshot = () => parseProject(vm.toJSON(undefined, {allowOptimization: false}));
98
+ type RuntimeTarget = {id?: string; isStage: boolean; isOriginal: boolean; getName(): string; sprite?: {
99
+ costumes?: readonly {asset?: VMAsset}[]; sounds?: readonly {asset?: VMAsset}[];
100
+ }};
101
+ const runtimeTargets = () => (vm.runtime.targets ?? []) as readonly RuntimeTarget[];
102
+ // Runtime ids in the order of the project's targets, so a renamed sprite is still the same sprite.
103
+ const identify = (project: Project): string[] | undefined => {
104
+ const ids = new Map<string, string>();
105
+ for (const target of runtimeTargets()) {
106
+ if (target.isOriginal && target.id) ids.set(target.isStage ? 'stage' : `sprite:${target.getName()}`, target.id);
107
+ }
108
+ const result = project.targets.map(target => ids.get(target.isStage ? 'stage' : `sprite:${target.name}`));
109
+ return result.every((id): id is string => id !== undefined) ? result : undefined;
110
+ };
111
+ const identity = (session: Session, edited: Project) => {
112
+ const after = identify(edited);
113
+ return session.ids && after ? {before: session.ids, after} : undefined;
114
+ };
115
+ // Writes a whole file and, for a file the folder already had, checks the folder before the write takes effect.
116
+ // A new file is empty until then, so it is checked by the writes that follow it instead.
117
+ const writeChecked = async (session: Session, handle: FileSystemFileHandle, data: Uint8Array, existing: boolean) => {
118
+ const options = (existing ? {mode: 'exclusive'} : {}) as FileSystemCreateWritableOptions;
119
+ const writable = await handle.createWritable(options);
120
+ try {
121
+ await writable.write(data as Uint8Array<ArrayBuffer>);
122
+ if (existing) await assertFolderUnchanged(session.source);
123
+ active(session);
124
+ await writable.close();
125
+ } catch (error) {
126
+ await writable.abort().catch(() => {});
127
+ throw error;
128
+ }
129
+ };
130
+ // The bytes of an asset the Editor holds, such as a costume added since the folder was opened.
131
+ const assetData = (name: string) => {
132
+ for (const target of runtimeTargets()) {
133
+ for (const entry of [...target.sprite?.costumes ?? [], ...target.sprite?.sounds ?? []]) {
134
+ const asset = entry.asset;
135
+ if (asset && `${asset.assetId}.${asset.dataFormat}` === name && asset.data) return asset.data;
136
+ }
137
+ }
138
+ return null;
139
+ };
94
140
  const changed = () => {revision++;};
95
141
  const loaded = () => {current = null;};
96
142
  vm.runtime.on('PROJECT_LOADED', loaded);
@@ -128,14 +174,15 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
128
174
  let stage: object | null;
129
175
  try {
130
176
  vm.quit();
131
- await vm.loadProject(source.projectText);
177
+ await vm.loadProject(source.loadText, {replaceLegacyExtensions: true});
132
178
  stage = vm.runtime.getTargetForStage();
133
179
  } finally {
134
180
  if (reader.assets === assets) reader.assets = null;
135
181
  }
136
182
  if (disposed || !stage || vm.runtime.getTargetForStage() !== stage) return 'cancelled';
137
183
  host.onOpened(directory.name);
138
- current = {source, stage, baseline: snapshot(), pending: null};
184
+ const baseline = snapshot();
185
+ current = {source, stage, baseline, ids: identify(baseline), pending: null};
139
186
  vm.renderer?.draw();
140
187
  return 'opened';
141
188
  } finally {
@@ -212,8 +259,9 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
212
259
  const base = decoder.decode(session.source.files.get(projectFile));
213
260
  let editor: string | null;
214
261
  try {
215
- const result = projectChanges(session.source.project, session.baseline, snapshot());
216
- editor = result ? `${JSON.stringify(result, null, 2)}\n` : base;
262
+ const edited = snapshot();
263
+ const result = projectChanges(session.source.project, session.baseline, edited, identity(session, edited));
264
+ editor = result ? `${JSON.stringify(planExtensionSave(session.source, result).project, null, 2)}\n` : base;
217
265
  } catch (error) {
218
266
  if (!(error instanceof FolderError)) throw error;
219
267
  editor = null;
@@ -262,6 +310,7 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
262
310
  throw error;
263
311
  }
264
312
  session.baseline = snapshot();
313
+ session.ids = identify(session.baseline);
265
314
  session.source.project = pending.update.project;
266
315
  session.source.files.set(projectFile, pending.update.bytes);
267
316
  session.source.versions.set(projectFile, pending.update.version);
@@ -304,33 +353,120 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
304
353
  try {
305
354
  const saveRevision = revision;
306
355
  const edited = snapshot();
307
- const result = projectChanges(session.source.project, session.baseline, edited);
356
+ const ids = identify(edited);
357
+ const result = projectChanges(session.source.project, session.baseline, edited, identity(session, edited));
308
358
  await assertFolderUnchanged(session.source);
309
359
  active(session);
310
- if (!result) {
360
+ const {source} = session;
361
+ const extensionSave = planExtensionSave(source, result ?? source.project);
362
+ const needed = projectAssets(extensionSave.project);
363
+ const added = new Map<string, Uint8Array>();
364
+ for (const [name, kind] of needed) {
365
+ if (source.bytes.has(name)) continue;
366
+ const data = assetData(name);
367
+ if (!data) return unsupported(`The data of ${name} is not in the Editor`);
368
+ const [id, format] = name.split('.');
369
+ const asset = vm.runtime.storage.createAsset(assetTypeFor(vm.runtime.storage, kind, format), format, data.slice(), null, true);
370
+ if (asset.assetId !== id) return unsupported(`Asset hash mismatch: ${name}`);
371
+ added.set(name, data);
372
+ }
373
+ const removed = [...source.bytes.keys()].filter(name => !needed.has(name));
374
+ if (!result && !removed.length && !extensionSave.removed.length) {
311
375
  if (revision === saveRevision) host.onSaved();
312
- return 'unchanged';
376
+ return {status: 'unchanged', sourceRemoved: []};
313
377
  }
314
- const text = `${JSON.stringify(result, null, 2)}\n`;
315
- const file = await session.source.directory.getFileHandle(projectFile);
316
- const options = {mode: 'exclusive'} as FileSystemCreateWritableOptions;
317
- const writable = await file.createWritable(options);
378
+ const encoder = new TextEncoder();
379
+ let manifest: Uint8Array | null = null;
380
+ if (added.size || removed.length) {
381
+ const value = JSON.parse(new TextDecoder().decode(source.files.get(manifestFile))) as JsonObject;
382
+ const entries = (value.archiveEntries as string[]).filter(name => !removed.includes(name));
383
+ value.archiveEntries = [...entries, ...added.keys()];
384
+ manifest = encoder.encode(`${JSON.stringify(value, null, 2)}\n`);
385
+ }
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');
399
+ }
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.
318
437
  try {
319
- await writable.write(text);
320
- await assertFolderUnchanged(session.source);
321
- active(session);
322
- await writable.close();
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`));
443
+ }
444
+ if (result) await write(root, projectFile, encoder.encode(`${JSON.stringify(extensionSave.project, null, 2)}\n`));
323
445
  } catch (error) {
324
- await writable.abort().catch(() => {});
446
+ for (const restore of undo.reverse()) await restore().catch(() => {});
325
447
  throw error;
326
448
  }
327
- session.source.project = result;
328
- session.source.files.set(projectFile, new TextEncoder().encode(text));
329
- session.source.versions.delete(projectFile);
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);
463
+ }
464
+ }
330
465
  session.baseline = edited;
466
+ session.ids = ids;
331
467
  session.pending = null;
332
468
  if (current === session && revision === saveRevision) host.onSaved();
333
- return 'saved';
469
+ return {status: 'saved', sourceRemoved: extensionSave.sourceRemoved};
334
470
  } finally {
335
471
  busy = false;
336
472
  }