@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.
package/src/source.ts CHANGED
@@ -4,8 +4,9 @@ export type Project = JsonObject & {targets: Target[]};
4
4
  type Target = JsonObject & {isStage: boolean; name: string};
5
5
 
6
6
  export const projectFile = 'project.source.json';
7
- const manifestFile = 'sb3-source.json';
8
- const extensionsFile = 'embedded-extensions.json';
7
+ export const manifestFile = 'sb3-source.json';
8
+ export const extensionsFile = 'embedded-extensions.json';
9
+ const embeddedReference = 'embedded-extension:';
9
10
 
10
11
  export class FolderError extends Error {
11
12
  code: 'conflict' | 'unsupported';
@@ -57,11 +58,17 @@ type FolderFiles = {
57
58
  files: Map<string, Uint8Array>;
58
59
  directory: FileSystemDirectoryHandle;
59
60
  bytes: Map<string, Uint8Array>;
61
+ extensionBytes: Map<string, Uint8Array>;
62
+ };
63
+ export type FolderSource = FolderFiles & {
64
+ project: Project;
65
+ loadText: string;
66
+ storageAssets: Map<string, Asset>;
67
+ embedded: ExtensionManifest;
60
68
  };
61
- export type FolderSource = FolderFiles & {project: Project; projectText: string; storageAssets: Map<string, Asset>};
62
69
 
63
70
  const getFile = async (dir: FileSystemDirectoryHandle, name: string) => (await dir.getFileHandle(name)).getFile();
64
- const fileVersion = (file: File) => `${file.size}:${file.lastModified}`;
71
+ export const fileVersion = (file: File) => `${file.size}:${file.lastModified}`;
65
72
  const fileBytes = async (file: File) => new Uint8Array(await file.arrayBuffer());
66
73
  const readBytes = async (dir: FileSystemDirectoryHandle, name: string) => fileBytes(await getFile(dir, name));
67
74
  const readNames = async (dir: FileSystemDirectoryHandle) => {
@@ -78,8 +85,181 @@ const sameNames = (actual: Set<string>, expected: Iterable<string>) => {
78
85
  return actual.size === expectedNames.size && [...actual].every(name => expectedNames.has(name));
79
86
  };
80
87
 
88
+ export const assetTypeFor = (storage: Storage, kind: 'costumes' | 'sounds', format: string) => kind === 'sounds' ?
89
+ storage.AssetType.Sound : format === 'svg' ? storage.AssetType.ImageVector : storage.AssetType.ImageBitmap;
81
90
  export const assetKey = (type: AssetType, id: string, format: string) => `${type.name}/${id}.${format}`;
82
91
 
92
+ type DataUrlMetadata = {mediaType: string; parameters: string[]; encoding: 'base64' | 'percent'};
93
+ type EmbeddedExtension = JsonObject & DataUrlMetadata & {id: string; path: string};
94
+ type ExtensionBundle = JsonObject & {id: string; members: string[]};
95
+ type ExtensionManifest = JsonObject & {extensions: EmbeddedExtension[]; extensionBundles?: ExtensionBundle[]};
96
+
97
+ const extensionPath = (id: string) => {
98
+ if (!/^[A-Za-z0-9._-]+$/u.test(id)) return unsupported(`Embedded extension ID cannot be used as a filename: ${id}`);
99
+ return `extensions/${id}.js`;
100
+ };
101
+ const validUrlPart = (part: Json | undefined): part is string => typeof part === 'string' && part.length > 0 && !/[;,\r\n]/u.test(part);
102
+ const extensionURLs = (project: Project) => {
103
+ const urls = project.extensionURLs === undefined ? {} : project.extensionURLs;
104
+ if (!isObject(urls) || Object.values(urls).some(url => typeof url !== 'string')) return unsupported('Invalid extensionURLs');
105
+ return urls as Record<string, string>;
106
+ };
107
+ const apiManifestName = (entry: EmbeddedExtension): string | undefined => {
108
+ if (entry.source === undefined) return;
109
+ if (!isObject(entry.source)) return unsupported(`Invalid source for embedded extension: ${entry.id}`);
110
+ const api = entry.source.apiManifest;
111
+ if (api === undefined) return;
112
+ const name = `${entry.id}.manifest.json`;
113
+ if (!isObject(api) || api.path !== `extensions/${name}`) return unsupported(`Invalid API manifest path: ${entry.id}`);
114
+ return name;
115
+ };
116
+ const extensionNames = (entries: EmbeddedExtension[]) => entries.flatMap(entry => {
117
+ const script = `${entry.id}.js`;
118
+ const api = apiManifestName(entry);
119
+ return api === undefined ? [script] : [script, api];
120
+ });
121
+
122
+ function validateExtensions(project: Project, value: JsonObject): ExtensionManifest {
123
+ if (value.formatVersion !== 1 || !Array.isArray(value.extensions)) return unsupported('Invalid embedded extension manifest');
124
+ const urls = extensionURLs(project);
125
+ const ids = new Set<string>();
126
+ for (const entry of value.extensions) {
127
+ if (!isObject(entry) || typeof entry.id !== 'string' || ids.has(entry.id) ||
128
+ entry.path !== extensionPath(entry.id) || !validUrlPart(entry.mediaType) ||
129
+ !Array.isArray(entry.parameters) || !entry.parameters.every(validUrlPart) ||
130
+ (entry.encoding !== 'base64' && entry.encoding !== 'percent') ||
131
+ !Object.hasOwn(urls, entry.id) || urls[entry.id] !== embeddedReference + entry.path) {
132
+ return unsupported('Invalid embedded extension entry or project reference');
133
+ }
134
+ ids.add(entry.id);
135
+ }
136
+ for (const [id, url] of Object.entries(urls)) {
137
+ if (url.startsWith('data:') || (url.startsWith(embeddedReference) && !ids.has(id))) {
138
+ return unsupported(`Invalid embedded extension reference: ${id}`);
139
+ }
140
+ }
141
+ if (value.extensionBundles !== undefined) {
142
+ if (!Array.isArray(value.extensionBundles)) return unsupported('Invalid extensionBundles');
143
+ const bundleIds = new Set<string>();
144
+ const members = new Set<string>();
145
+ for (const bundle of value.extensionBundles) {
146
+ if (!isObject(bundle) || typeof bundle.id !== 'string' || !/^[a-z0-9]+$/u.test(bundle.id) ||
147
+ ids.has(bundle.id) || bundleIds.has(bundle.id) || typeof bundle.name !== 'string' ||
148
+ !bundle.name.trim() || /[\r\n]/u.test(bundle.name) ||
149
+ !Array.isArray(bundle.members) || bundle.members.length < 2) {
150
+ return unsupported('Invalid extension bundle');
151
+ }
152
+ bundleIds.add(bundle.id);
153
+ for (const member of bundle.members) {
154
+ if (typeof member !== 'string' || !/^[a-z0-9]+$/u.test(member) || !ids.has(member) || members.has(member)) {
155
+ return unsupported(`Invalid member of extension bundle: ${bundle.id}`);
156
+ }
157
+ members.add(member);
158
+ }
159
+ }
160
+ }
161
+ return value as ExtensionManifest;
162
+ }
163
+
164
+ function encodeDataUrl(entry: EmbeddedExtension, bytes: Uint8Array): string {
165
+ const metadata = [entry.mediaType, ...entry.parameters].join(';');
166
+ if (entry.encoding === 'percent') {
167
+ const payload = Array.from(bytes, byte => `%${byte.toString(16).padStart(2, '0').toUpperCase()}`).join('');
168
+ return `data:${metadata},${payload}`;
169
+ }
170
+ let binary = '';
171
+ for (let offset = 0; offset < bytes.length; offset += 8192) {
172
+ binary += String.fromCharCode(...bytes.subarray(offset, offset + 8192));
173
+ }
174
+ return `data:${metadata};base64,${btoa(binary)}`;
175
+ }
176
+
177
+ function decodeDataUrl(id: string, url: string): {metadata: DataUrlMetadata; bytes: Uint8Array} {
178
+ const comma = url.indexOf(',');
179
+ if (comma === -1) return unsupported(`Invalid data URL for embedded extension: ${id}`);
180
+ const [type, ...parameters] = url.slice('data:'.length, comma).split(';');
181
+ const mediaType = type || 'text/plain';
182
+ const base64 = parameters.indexOf('base64');
183
+ if (base64 !== -1) parameters.splice(base64, 1);
184
+ const encoding = base64 === -1 ? 'percent' : 'base64';
185
+ if (!validUrlPart(mediaType) || !parameters.every(validUrlPart) || parameters.includes('base64')) {
186
+ return unsupported(`Invalid data URL metadata for embedded extension: ${id}`);
187
+ }
188
+ const payload = url.slice(comma + 1);
189
+ try {
190
+ if (encoding === 'percent') {
191
+ return {metadata: {mediaType, parameters, encoding}, bytes: new TextEncoder().encode(decodeURIComponent(payload))};
192
+ }
193
+ if (!/^[A-Za-z0-9+/]*={0,2}$/u.test(payload)) throw new Error('Invalid base64');
194
+ const binary = atob(payload.replace(/=+$/u, ''));
195
+ return {metadata: {mediaType, parameters, encoding}, bytes: Uint8Array.from(binary, char => char.charCodeAt(0))};
196
+ } catch {
197
+ return unsupported(`Invalid data URL payload for embedded extension: ${id}`);
198
+ }
199
+ }
200
+
201
+ function hasSandboxedNoticeRequirement(bundle: ExtensionBundle, entry: EmbeddedExtension, bytes: Uint8Array): boolean {
202
+ let requirement;
203
+ if (bundle.buildMode === 'module') {
204
+ const source = entry.source;
205
+ if (isObject(source) && isObject(source.module)) requirement = source.module.unsandboxed;
206
+ } else {
207
+ const script = new TextDecoder().decode(bytes);
208
+ requirement = /^\/\/ Unsandboxed: (.+)\r?$/mu.exec(script)?.[1].trim();
209
+ }
210
+ return requirement === 'required' || requirement === 'optional';
211
+ }
212
+
213
+ export function planExtensionSave(source: FolderSource, project: Project) {
214
+ const urls = {...extensionURLs(project)};
215
+ const entries = new Map(source.embedded.extensions.map(entry => [entry.id, entry]));
216
+ const writes = new Map<string, Uint8Array>();
217
+ const sourceRemoved: string[] = [];
218
+ for (const [id, entry] of entries) {
219
+ if (!Object.hasOwn(urls, id) || (!urls[id].startsWith('data:') && urls[id] !== embeddedReference + entry.path)) entries.delete(id);
220
+ }
221
+ for (const [id, url] of Object.entries(urls)) {
222
+ if (!url.startsWith('data:')) continue;
223
+ const path = extensionPath(id);
224
+ const {metadata, bytes} = decodeDataUrl(id, url);
225
+ const name = `${id}.js`;
226
+ const previous = entries.get(id);
227
+ urls[id] = embeddedReference + path;
228
+ if (!previous || !sameBytes(source.extensionBytes.get(name)!, bytes)) {
229
+ const entry: EmbeddedExtension = {...previous, id, path, ...metadata};
230
+ if (entry.source !== undefined) {
231
+ delete entry.source;
232
+ sourceRemoved.push(id);
233
+ }
234
+ entries.set(id, entry);
235
+ writes.set(name, bytes);
236
+ }
237
+ }
238
+ const embedded: ExtensionManifest = {...source.embedded, extensions: [...entries.values()]};
239
+ if (embedded.extensionBundles) {
240
+ embedded.extensionBundles = embedded.extensionBundles.flatMap(bundle => {
241
+ const members = bundle.members.filter(id => entries.has(id) &&
242
+ // Module bundles need a managed npm module for each member.
243
+ !(bundle.buildMode === 'module' && sourceRemoved.includes(id)));
244
+ if (members.length < 2) return [];
245
+ const updated: ExtensionBundle = {...bundle, members};
246
+ const changed = members.length !== bundle.members.length || members.some(id => writes.has(`${id}.js`));
247
+ if (updated.sandboxedNotice !== undefined && changed && !members.some(id => {
248
+ const name = `${id}.js`;
249
+ return hasSandboxedNoticeRequirement(updated, entries.get(id)!, writes.get(name) ?? source.extensionBytes.get(name)!);
250
+ })) {
251
+ delete updated.sandboxedNotice;
252
+ }
253
+ return [updated];
254
+ });
255
+ }
256
+ const result = project.extensionURLs === undefined ? project : {...project, extensionURLs: urls};
257
+ validateExtensions(result, embedded);
258
+ const names = new Set(extensionNames(embedded.extensions));
259
+ return {project: result, embedded, manifestChanged: !equal(embedded, source.embedded), writes, sourceRemoved,
260
+ removed: [...source.extensionBytes.keys()].filter(name => !names.has(name))};
261
+ }
262
+
83
263
  export async function readFolder(directory: FileSystemDirectoryHandle, storage: Storage): Promise<FolderSource> {
84
264
  const versions = new Map<string, string>();
85
265
  const readEntry = async (dir: FileSystemDirectoryHandle, name: string, prefix = '') => {
@@ -98,12 +278,8 @@ export async function readFolder(directory: FileSystemDirectoryHandle, storage:
98
278
  manifest.assetsDirectory !== 'assets' || !Array.isArray(manifest.archiveEntries)) {
99
279
  return unsupported('Expected an sb3-toolchain v1 folder');
100
280
  }
101
- if (embedded.formatVersion !== 1 || !Array.isArray(embedded.extensions) || embedded.extensions.length ||
102
- (embedded.extensionBundles !== undefined && (!Array.isArray(embedded.extensionBundles) || embedded.extensionBundles.length)) ||
103
- project.turboWarpPlus !== undefined || project.customFonts !== undefined ||
104
- (project.extensionURLs !== undefined && (!isObject(project.extensionURLs) || Object.keys(project.extensionURLs).length))) {
105
- return unsupported('Custom extensions and fonts are not supported in project folders');
106
- }
281
+ if (customContent(project)) return unsupported('Plus extension manifests and custom fonts are not supported in project folders');
282
+ const extensionManifest = validateExtensions(project, embedded);
107
283
  const expected = new Set(['project.json']);
108
284
  const references = new Map<string, {id: string; format: string; type: AssetType; name: string}>();
109
285
  for (const target of project.targets) {
@@ -130,20 +306,27 @@ export async function readFolder(directory: FileSystemDirectoryHandle, storage:
130
306
  }
131
307
  expected.delete('project.json');
132
308
  const [assets, extensions] = await Promise.all([directory.getDirectoryHandle('assets'), directory.getDirectoryHandle('extensions')]);
133
- if (!sameNames(await readNames(assets), expected) || (await readNames(extensions)).size) {
309
+ const scripts = extensionNames(extensionManifest.extensions);
310
+ if (!sameNames(await readNames(assets), expected) || !sameNames(await readNames(extensions), scripts)) {
134
311
  return unsupported('Unexpected or missing source files');
135
312
  }
136
313
  const bytes = new Map(await Promise.all([...expected].map(name => readEntry(assets, name, 'assets/'))));
314
+ const extensionBytes = new Map(await Promise.all(scripts.map(name => readEntry(extensions, name, 'extensions/'))));
315
+ const loadText = extensionManifest.extensions.length ? JSON.stringify({...project, extensionURLs: {
316
+ ...extensionURLs(project),
317
+ ...Object.fromEntries(extensionManifest.extensions.map(entry =>
318
+ [entry.id, encodeDataUrl(entry, extensionBytes.get(`${entry.id}.js`)!)]))
319
+ }}) : projectText;
137
320
  const storageAssets = new Map<string, Asset>();
138
321
  for (const [key, {id, format, type, name}] of references) {
139
322
  const asset = storage.createAsset(type, format, bytes.get(name)!.slice(), null, true);
140
323
  if (asset.assetId !== id) return unsupported(`Asset hash mismatch: ${name}`);
141
324
  storageAssets.set(key, asset);
142
325
  }
143
- return {versions, files, directory, bytes, project, projectText, storageAssets};
326
+ return {versions, files, directory, bytes, extensionBytes, project, loadText, storageAssets, embedded: extensionManifest};
144
327
  }
145
328
 
146
- const sameBytes = (a: Uint8Array, b: Uint8Array) => a.length === b.length && a.every((value, index) => value === b[index]);
329
+ export const sameBytes = (a: Uint8Array, b: Uint8Array) => a.length === b.length && a.every((value, index) => value === b[index]);
147
330
 
148
331
  export type FolderUpdate = {version: string; bytes: Uint8Array; project: Project};
149
332
  export type DiskChange = FolderUpdate | 'structure' | 'waiting' | null;
@@ -157,10 +340,11 @@ export async function readFolderUpdate(source: FolderSource, previous: FolderUpd
157
340
  directory.getDirectoryHandle('assets'), directory.getDirectoryHandle('extensions'), getFile(directory, projectFile)
158
341
  ]);
159
342
  const [assetNames, extensionNames] = await Promise.all([readNames(assets), readNames(extensions)]);
160
- if (!sameNames(assetNames, source.bytes.keys()) || extensionNames.size) return 'structure';
343
+ if (!sameNames(assetNames, source.bytes.keys()) || !sameNames(extensionNames, source.extensionBytes.keys())) return 'structure';
161
344
  const unchanged = await Promise.all([
162
345
  ...[manifestFile, extensionsFile].map(async name => fileVersion(await getFile(directory, name)) === versions.get(name)),
163
- ...[...assetNames].map(async name => fileVersion(await getFile(assets, name)) === versions.get(`assets/${name}`))
346
+ ...[...assetNames].map(async name => fileVersion(await getFile(assets, name)) === versions.get(`assets/${name}`)),
347
+ ...[...extensionNames].map(async name => fileVersion(await getFile(extensions, name)) === versions.get(`extensions/${name}`))
164
348
  ]);
165
349
  if (unchanged.includes(false)) return 'structure';
166
350
  const version = fileVersion(project);
@@ -172,7 +356,9 @@ export async function readFolderUpdate(source: FolderSource, previous: FolderUpd
172
356
  versions.set(projectFile, version);
173
357
  return null;
174
358
  }
175
- return {version, bytes, project: parseProject(new TextDecoder('utf-8', {fatal: true}).decode(bytes))};
359
+ const updated = parseProject(new TextDecoder('utf-8', {fatal: true}).decode(bytes));
360
+ if (!equal(source.project.extensionURLs, updated.extensionURLs)) return 'structure';
361
+ return {version, bytes, project: updated};
176
362
  } catch (error) {
177
363
  // Well-formed JSON outside the partial scope needs a whole reload. Anything else may be a write in
178
364
  // progress, such as truncated JSON or a file replaced between the probe and the read, and is retried.
@@ -184,12 +370,12 @@ export async function assertFolderUnchanged(source: FolderSource) {
184
370
  try {
185
371
  const assets = await source.directory.getDirectoryHandle('assets');
186
372
  const extensions = await source.directory.getDirectoryHandle('extensions');
187
- if (!sameNames(await readNames(assets), source.bytes.keys()) || (await readNames(extensions)).size) throw new FolderError('conflict');
188
- for (const [name, bytes] of source.files) {
189
- if (!sameBytes(await readBytes(source.directory, name), bytes)) throw new FolderError('conflict');
190
- }
191
- for (const [name, bytes] of source.bytes) {
192
- if (!sameBytes(await readBytes(assets, name), bytes)) throw new FolderError('conflict');
373
+ if (!sameNames(await readNames(assets), source.bytes.keys()) ||
374
+ !sameNames(await readNames(extensions), source.extensionBytes.keys())) throw new FolderError('conflict');
375
+ for (const [directory, files] of [[source.directory, source.files], [assets, source.bytes], [extensions, source.extensionBytes]] as const) {
376
+ for (const [name, bytes] of files) {
377
+ if (!sameBytes(await readBytes(directory, name), bytes)) throw new FolderError('conflict');
378
+ }
193
379
  }
194
380
  } catch (error) {
195
381
  if (error instanceof FolderError || (error instanceof DOMException && ['NotFoundError', 'TypeMismatchError'].includes(error.name))) {
@@ -235,56 +421,77 @@ const applyChanges = (source: Json | undefined, before: Json, after: Json): Json
235
421
  return result;
236
422
  };
237
423
 
238
- const assetNames = (project: Project) => {
239
- const names = new Set<string>();
424
+ /** The asset files a project refers to, by file name, with the kind of list that refers to each. */
425
+ export const projectAssets = (project: Project) => {
426
+ const names = new Map<string, 'costumes' | 'sounds'>();
240
427
  for (const target of project.targets) {
241
428
  for (const kind of ['costumes', 'sounds'] as const) {
242
- const entries = target[kind];
243
- if (!Array.isArray(entries)) continue;
244
- for (const entry of entries) if (isObject(entry) && typeof entry.md5ext === 'string') names.add(entry.md5ext);
429
+ const entries = target[kind] ?? [];
430
+ if (!Array.isArray(entries)) return unsupported('Invalid asset list');
431
+ for (const entry of entries) {
432
+ if (!isObject(entry) || typeof entry.assetId !== 'string' || !/^[a-f0-9]{32}$/u.test(entry.assetId) ||
433
+ typeof entry.dataFormat !== 'string' || entry.md5ext !== `${entry.assetId}.${entry.dataFormat}`) {
434
+ return unsupported('Invalid asset reference');
435
+ }
436
+ const supported = kind === 'sounds' ? /^(wav|mp3)$/u.test(entry.dataFormat) :
437
+ /^(svg|png|bmp|jpg|jpeg|gif)$/u.test(entry.dataFormat);
438
+ if (!supported) return unsupported(`Unsupported asset reference: ${entry.md5ext}`);
439
+ names.set(entry.md5ext, kind);
440
+ }
245
441
  }
246
442
  }
247
443
  return names;
248
444
  };
249
- const customContent = (project: Project) => project.turboWarpPlus !== undefined || project.customFonts !== undefined ||
250
- (project.extensionURLs !== undefined && (!isObject(project.extensionURLs) || Object.keys(project.extensionURLs).length > 0));
445
+ const customContent = (project: Project) => project.turboWarpPlus !== undefined || project.customFonts !== undefined;
446
+
447
+ /** Runtime ids of the targets, in the order of the project's `targets`, so a renamed sprite stays the same sprite. */
448
+ export type TargetIdentity = {before: readonly string[]; after: readonly string[]};
251
449
 
252
450
  /**
253
451
  * Applies the edits between `before` and `after` (both from the VM) to the project read from the folder, keeping the
254
- * fields and order the VM does not know. Saves change only the project file: the stage and sprites keep their names,
255
- * and the costumes and sounds keep using the asset files already in the folder. Returns `null` without edits.
452
+ * fields and order the VM does not know. Sprites are matched by `identity` (by name without it): a sprite with the
453
+ * same id keeps its folder entry under a new name, and added or removed sprites are added or removed. The asset files
454
+ * the result needs are the caller's to write. Returns `null` without edits.
256
455
  */
257
- export function projectChanges(source: Project, before: Project, after: Project): Project | null {
258
- const keys = (project: Project) => project.targets.map(targetKey);
259
- const beforeKeys = keys(before), afterKeys = keys(after);
260
- if (beforeKeys.length !== afterKeys.length || !afterKeys.every(key => beforeKeys.includes(key))) {
261
- return unsupported('Adding, removing or renaming sprites is not saved to a project folder yet');
456
+ export function projectChanges(source: Project, before: Project, after: Project, identity?: TargetIdentity): Project | null {
457
+ const beforeIds = identity?.before ?? before.targets.map(targetKey);
458
+ const afterIds = identity?.after ?? after.targets.map(targetKey);
459
+ if (beforeIds.length !== before.targets.length || afterIds.length !== after.targets.length) {
460
+ return unsupported('The targets could not be identified');
262
461
  }
263
- const used = assetNames(after), known = assetNames(before);
264
- if (used.size !== known.size || [...used].some(name => !known.has(name))) {
265
- return unsupported('Adding or removing costumes and sounds is not saved to a project folder yet');
266
- }
267
- if (customContent(after)) return unsupported('Custom extensions and fonts are not saved to a project folder');
268
- if (equal(before, after)) return null;
462
+ projectAssets(after);
463
+ if (customContent(after)) return unsupported('Plus extension manifests and custom fonts are not saved to a project folder');
464
+ if (equal(before, after) && equal([...beforeIds], [...afterIds])) return null;
269
465
  const {targets: sourceTargets, ...sourceOther} = source;
270
466
  const {targets: beforeTargets, ...beforeOther} = before;
271
467
  const {targets: afterTargets, ...afterOther} = after;
272
468
  const originals = new Map(sourceTargets.map(target => [targetKey(target), target]));
273
- const previous = new Map(beforeTargets.map(target => [targetKey(target), target]));
274
- const targets = afterTargets.map(target => {
275
- const key = targetKey(target);
276
- const original = originals.get(key);
469
+ const previous = new Map(beforeTargets.map((target, index) => [beforeIds[index], target]));
470
+ const targets = afterTargets.map((target, index) => {
471
+ const prior = previous.get(afterIds[index]);
472
+ if (!prior) return target;
473
+ const original = originals.get(targetKey(prior));
277
474
  if (!original) return unsupported('The original target could not be identified');
278
- return applyChanges(original, previous.get(key)!, target) as Target;
475
+ return applyChanges(original, prior, target) as Target;
279
476
  });
280
- // The folder keeps its own target order unless the Editor reordered the sprites.
281
- const order = equal(beforeKeys, afterKeys) ? sourceTargets.map(targetKey) : afterKeys;
282
- const byKey = new Map(targets.map(target => [targetKey(target), target]));
477
+ // The folder keeps its own target order unless the Editor added, removed or reordered sprites.
478
+ let ordered = targets;
479
+ if (equal([...beforeIds], [...afterIds])) {
480
+ const position = new Map(sourceTargets.map((target, index) => [targetKey(target), index]));
481
+ const place = (index: number) => position.get(targetKey(beforeTargets[index])) ?? index;
482
+ ordered = targets.map((target, index) => ({target, at: place(index)})).sort((a, b) => a.at - b.at).map(entry => entry.target);
483
+ }
283
484
  const other = applyChanges(sourceOther, beforeOther, afterOther) as JsonObject;
485
+ if (!equal(before.extensionURLs, after.extensionURLs)) {
486
+ // The VM only serializes the URLs it knows. Apply its edits to those entries.
487
+ const urls = applyChanges(source.extensionURLs, extensionURLs(before), extensionURLs(after)) as JsonObject;
488
+ if (after.extensionURLs === undefined && !Object.keys(urls).length) delete other.extensionURLs;
489
+ else other.extensionURLs = urls;
490
+ }
284
491
  // Keep the folder's key order, with the targets where they were.
285
492
  const result: JsonObject = {};
286
493
  for (const key of Object.keys(source)) {
287
- if (key === 'targets') result.targets = order.map(key => byKey.get(key)!);
494
+ if (key === 'targets') result.targets = ordered;
288
495
  else if (Object.hasOwn(other, key)) result[key] = other[key];
289
496
  }
290
497
  for (const key of Object.keys(other)) if (!Object.hasOwn(result, key)) result[key] = other[key];
package/tsconfig.json CHANGED
@@ -5,6 +5,7 @@
5
5
  "module": "NodeNext",
6
6
  "moduleResolution": "NodeNext",
7
7
  "types": [],
8
+ "jsx": "react",
8
9
  "strict": true,
9
10
  "verbatimModuleSyntax": true,
10
11
  "declaration": true,
@@ -12,5 +13,5 @@
12
13
  "rootDir": "src",
13
14
  "outDir": "dist"
14
15
  },
15
- "include": ["src/**/*.ts"]
16
+ "include": ["src/**/*.ts", "src/**/*.tsx"]
16
17
  }
package/vite.config.ts CHANGED
@@ -1,10 +1,20 @@
1
- import {defineConfig} from 'vite';
1
+ import {defineConfig, esmExternalRequirePlugin} from 'vite';
2
2
 
3
3
  export default defineConfig({
4
4
  build: {
5
5
  // The pinned Scratch GUI still parses the library with Webpack 4.
6
6
  target: 'es2017',
7
7
  license: {fileName: 'THIRD_PARTY_NOTICES.md'},
8
- lib: {entry: 'src/index.ts', formats: ['es'], fileName: () => 'index.js'}
8
+ lib: {
9
+ entry: {index: 'src/index.ts', explorer: 'src/explorer.tsx'},
10
+ formats: ['es'],
11
+ fileName: (_format, name) => `${name}.js`
12
+ },
13
+ rolldownOptions: {
14
+ // The explorer renders with the host's React; Headless Tree is bundled.
15
+ plugins: [esmExternalRequirePlugin({external: ['react', 'react-dom']})],
16
+ // Code both entries use; it never imports React.
17
+ output: {chunkFileNames: 'shared-[hash].js'}
18
+ }
9
19
  }
10
20
  });