@kubohiroya/turbowarp-plus-editor-project-folder 0.6.0 → 0.8.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 +24 -2
- package/README.md +18 -2
- package/dist/THIRD_PARTY_NOTICES.md +5 -1
- package/dist/explorer.d.ts +52 -0
- package/dist/explorer.js +1059 -0
- package/dist/folder-registry.d.ts +13 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +390 -621
- package/dist/session.d.ts +11 -3
- package/dist/shared-DlmcdO1_.js +458 -0
- package/dist/source.d.ts +34 -3
- package/package.json +24 -1
- package/src/explorer.tsx +410 -0
- package/src/folder-registry.ts +45 -0
- package/src/index.ts +23 -2
- package/src/session.ts +115 -68
- package/src/source.ts +235 -50
- package/tsconfig.json +2 -1
- package/vite.config.ts +12 -2
package/src/session.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {assetKey, assetTypeFor, assertFolderUnchanged, fileVersion, FolderError, manifestFile, parseProject,
|
|
2
|
-
projectFile, readFolder, readFolderUpdate, unsupported} from './source.js';
|
|
3
|
-
import type {Asset, FolderSource, FolderUpdate, JsonObject, 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, AssetKind, 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';
|
|
6
6
|
import {applyScriptReload, planScriptReload} from './script-reload.js';
|
|
@@ -17,8 +17,9 @@ 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
|
+
assets?: readonly (Asset | null | undefined)[];
|
|
22
23
|
renderer?: {draw(): void};
|
|
23
24
|
}
|
|
24
25
|
|
|
@@ -83,12 +84,21 @@ export type ReloadState = 'ready' | 'full' | 'waiting' | 'dirty' | 'running' | '
|
|
|
83
84
|
/** What the reload command does for the pending change, regardless of whether it may run now. */
|
|
84
85
|
export type ReloadKind = 'partial' | 'full' | null;
|
|
85
86
|
export type OpenResult = 'opened' | 'cancelled';
|
|
86
|
-
|
|
87
|
+
type SaveResult = {status: 'saved' | 'unchanged'; sourceRemoved: string[]};
|
|
88
|
+
type SaveDirectory = {directory: FileSystemDirectoryHandle; files: Map<string, Uint8Array>; prefix: string};
|
|
87
89
|
|
|
88
90
|
type VMAsset = {assetId: string; dataFormat: string; data?: Uint8Array};
|
|
89
91
|
|
|
90
92
|
export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
91
93
|
let current: Session | null = null;
|
|
94
|
+
// Told when a folder opens or closes, and after a save, so a view of the folder can read it again.
|
|
95
|
+
const listeners = new Set<() => void>();
|
|
96
|
+
const notify = () => {for (const listener of [...listeners]) listener();};
|
|
97
|
+
const setCurrent = (session: Session | null) => {
|
|
98
|
+
if (current === session) return;
|
|
99
|
+
current = session;
|
|
100
|
+
notify();
|
|
101
|
+
};
|
|
92
102
|
let busy = false;
|
|
93
103
|
let disposed = false;
|
|
94
104
|
let revision = 0;
|
|
@@ -112,7 +122,7 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
112
122
|
return session.ids && after ? {before: session.ids, after} : undefined;
|
|
113
123
|
};
|
|
114
124
|
// Writes a whole file and, for a file the folder already had, checks the folder before the write takes effect.
|
|
115
|
-
// A new
|
|
125
|
+
// A new file is empty until then, so it is checked by the writes that follow it instead.
|
|
116
126
|
const writeChecked = async (session: Session, handle: FileSystemFileHandle, data: Uint8Array, existing: boolean) => {
|
|
117
127
|
const options = (existing ? {mode: 'exclusive'} : {}) as FileSystemCreateWritableOptions;
|
|
118
128
|
const writable = await handle.createWritable(options);
|
|
@@ -126,13 +136,11 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
126
136
|
throw error;
|
|
127
137
|
}
|
|
128
138
|
};
|
|
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
139
|
// The bytes of an asset the Editor holds, such as a costume added since the folder was opened.
|
|
135
|
-
const assetData = (name: string) => {
|
|
140
|
+
const assetData = (name: string, kind: AssetKind) => {
|
|
141
|
+
if (kind === 'fonts') {
|
|
142
|
+
return vm.assets?.find(asset => asset && `${asset.assetId}.${asset.dataFormat}` === name)?.data;
|
|
143
|
+
}
|
|
136
144
|
for (const target of runtimeTargets()) {
|
|
137
145
|
for (const entry of [...target.sprite?.costumes ?? [], ...target.sprite?.sounds ?? []]) {
|
|
138
146
|
const asset = entry.asset;
|
|
@@ -142,7 +150,7 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
142
150
|
return null;
|
|
143
151
|
};
|
|
144
152
|
const changed = () => {revision++;};
|
|
145
|
-
const loaded = () =>
|
|
153
|
+
const loaded = () => setCurrent(null);
|
|
146
154
|
vm.runtime.on('PROJECT_LOADED', loaded);
|
|
147
155
|
vm.on('PROJECT_CHANGED', changed);
|
|
148
156
|
const active = (session: Session) => {
|
|
@@ -152,7 +160,8 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
152
160
|
};
|
|
153
161
|
const dispose = () => {
|
|
154
162
|
disposed = true;
|
|
155
|
-
|
|
163
|
+
setCurrent(null);
|
|
164
|
+
listeners.clear();
|
|
156
165
|
vm.runtime.off('PROJECT_LOADED', loaded);
|
|
157
166
|
vm.off('PROJECT_CHANGED', changed);
|
|
158
167
|
};
|
|
@@ -171,14 +180,14 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
171
180
|
if (disposed || vm.runtime.getTargetForStage() !== priorStage) return 'cancelled';
|
|
172
181
|
if (host.isProjectDirty() && !confirmReplace()) return 'cancelled';
|
|
173
182
|
host.beforeLoad();
|
|
174
|
-
|
|
183
|
+
setCurrent(null);
|
|
175
184
|
const reader = readerFor(storage);
|
|
176
185
|
const assets = source.storageAssets;
|
|
177
186
|
reader.assets = assets;
|
|
178
187
|
let stage: object | null;
|
|
179
188
|
try {
|
|
180
189
|
vm.quit();
|
|
181
|
-
await vm.loadProject(source.
|
|
190
|
+
await vm.loadProject(source.loadText, {replaceLegacyExtensions: true});
|
|
182
191
|
stage = vm.runtime.getTargetForStage();
|
|
183
192
|
} finally {
|
|
184
193
|
if (reader.assets === assets) reader.assets = null;
|
|
@@ -186,7 +195,7 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
186
195
|
if (disposed || !stage || vm.runtime.getTargetForStage() !== stage) return 'cancelled';
|
|
187
196
|
host.onOpened(directory.name);
|
|
188
197
|
const baseline = snapshot();
|
|
189
|
-
|
|
198
|
+
setCurrent({source, stage, baseline, ids: identify(baseline), pending: null});
|
|
190
199
|
vm.renderer?.draw();
|
|
191
200
|
return 'opened';
|
|
192
201
|
} finally {
|
|
@@ -231,7 +240,12 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
231
240
|
};
|
|
232
241
|
const canApply = () => !host.isProjectDirty() && !host.blockReload?.isRunning();
|
|
233
242
|
return {
|
|
234
|
-
detach() {
|
|
243
|
+
detach() {setCurrent(null);},
|
|
244
|
+
/** Calls `listener` when a folder opens or closes, and after a save. Returns the function that stops it. */
|
|
245
|
+
subscribe(listener: () => void) {
|
|
246
|
+
listeners.add(listener);
|
|
247
|
+
return () => {listeners.delete(listener);};
|
|
248
|
+
},
|
|
235
249
|
get canSave() {return current !== null;},
|
|
236
250
|
get busy() {return busy;},
|
|
237
251
|
get reloadState() {return reloadState();},
|
|
@@ -265,7 +279,7 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
265
279
|
try {
|
|
266
280
|
const edited = snapshot();
|
|
267
281
|
const result = projectChanges(session.source.project, session.baseline, edited, identity(session, edited));
|
|
268
|
-
editor = result ? `${JSON.stringify(result, null, 2)}\n` : base;
|
|
282
|
+
editor = result ? `${JSON.stringify(planExtensionSave(session.source, result).project, null, 2)}\n` : base;
|
|
269
283
|
} catch (error) {
|
|
270
284
|
if (!(error instanceof FolderError)) throw error;
|
|
271
285
|
editor = null;
|
|
@@ -361,18 +375,13 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
361
375
|
const result = projectChanges(session.source.project, session.baseline, edited, identity(session, edited));
|
|
362
376
|
await assertFolderUnchanged(session.source);
|
|
363
377
|
active(session);
|
|
364
|
-
if (!result) {
|
|
365
|
-
if (revision === saveRevision) host.onSaved();
|
|
366
|
-
return 'unchanged';
|
|
367
|
-
}
|
|
368
378
|
const {source} = session;
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
const needed = projectAssets(result);
|
|
379
|
+
const extensionSave = planExtensionSave(source, result ?? source.project);
|
|
380
|
+
const needed = projectAssets(extensionSave.project);
|
|
372
381
|
const added = new Map<string, Uint8Array>();
|
|
373
382
|
for (const [name, kind] of needed) {
|
|
374
383
|
if (source.bytes.has(name)) continue;
|
|
375
|
-
const data = assetData(name);
|
|
384
|
+
const data = assetData(name, kind);
|
|
376
385
|
if (!data) return unsupported(`The data of ${name} is not in the Editor`);
|
|
377
386
|
const [id, format] = name.split('.');
|
|
378
387
|
const asset = vm.runtime.storage.createAsset(assetTypeFor(vm.runtime.storage, kind, format), format, data.slice(), null, true);
|
|
@@ -380,6 +389,11 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
380
389
|
added.set(name, data);
|
|
381
390
|
}
|
|
382
391
|
const removed = [...source.bytes.keys()].filter(name => !needed.has(name));
|
|
392
|
+
if (!result && !removed.length && !extensionSave.removed.length) {
|
|
393
|
+
if (revision === saveRevision) host.onSaved();
|
|
394
|
+
notify();
|
|
395
|
+
return {status: 'unchanged', sourceRemoved: []};
|
|
396
|
+
}
|
|
383
397
|
const encoder = new TextEncoder();
|
|
384
398
|
let manifest: Uint8Array | null = null;
|
|
385
399
|
if (added.size || removed.length) {
|
|
@@ -388,58 +402,91 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
388
402
|
value.archiveEntries = [...entries, ...added.keys()];
|
|
389
403
|
manifest = encoder.encode(`${JSON.stringify(value, null, 2)}\n`);
|
|
390
404
|
}
|
|
391
|
-
const
|
|
392
|
-
const
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
405
|
+
const root = {directory: source.directory, files: source.files, prefix: ''};
|
|
406
|
+
const assets = {directory: await source.directory.getDirectoryHandle('assets'), files: source.bytes, prefix: 'assets/'};
|
|
407
|
+
const extensions = {directory: await source.directory.getDirectoryHandle('extensions'),
|
|
408
|
+
files: source.extensionBytes, prefix: 'extensions/'};
|
|
409
|
+
const undo: (() => Promise<void>)[] = [];
|
|
410
|
+
const write = async ({directory, files, prefix}: SaveDirectory, name: string, data: Uint8Array) => {
|
|
411
|
+
const previous = files.get(name);
|
|
412
|
+
if (!previous) {
|
|
413
|
+
const existing = await directory.getFileHandle(name).catch(error => {
|
|
414
|
+
if (error instanceof DOMException && error.name === 'NotFoundError') return null;
|
|
415
|
+
throw error;
|
|
416
|
+
});
|
|
417
|
+
if (existing) throw new FolderError('conflict');
|
|
403
418
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
419
|
+
const handle = await directory.getFileHandle(name, previous ? undefined : {create: true});
|
|
420
|
+
const key = prefix + name;
|
|
421
|
+
const previousVersion = source.versions.get(key);
|
|
422
|
+
let committed = false;
|
|
423
|
+
undo.push(async () => {
|
|
424
|
+
let version = previousVersion;
|
|
425
|
+
try {
|
|
426
|
+
if (!committed && previous) return;
|
|
427
|
+
const bytes = new Uint8Array(await (await handle.getFile()).arrayBuffer());
|
|
428
|
+
if (!sameBytes(bytes, committed ? data : new Uint8Array())) return;
|
|
429
|
+
if (previous) {
|
|
430
|
+
const writable = await handle.createWritable();
|
|
431
|
+
try {
|
|
432
|
+
await writable.write(previous as Uint8Array<ArrayBuffer>);
|
|
433
|
+
await writable.close();
|
|
434
|
+
} catch (error) {
|
|
435
|
+
await writable.abort().catch(() => {});
|
|
436
|
+
throw error;
|
|
437
|
+
}
|
|
438
|
+
version = fileVersion(await handle.getFile());
|
|
439
|
+
} else {
|
|
440
|
+
await directory.removeEntry(name);
|
|
441
|
+
}
|
|
442
|
+
} finally {
|
|
443
|
+
// If restoration failed, keep the old expectations so the next save reports a conflict.
|
|
444
|
+
if (previous) files.set(name, previous);
|
|
445
|
+
else files.delete(name);
|
|
446
|
+
if (version === undefined) source.versions.delete(key);
|
|
447
|
+
else source.versions.set(key, version);
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
await writeChecked(session, handle, data, previous !== undefined);
|
|
451
|
+
committed = true;
|
|
452
|
+
files.set(name, data);
|
|
453
|
+
source.versions.set(key, fileVersion(await handle.getFile()));
|
|
454
|
+
};
|
|
455
|
+
// Write the files that will be referenced before publishing the new manifests and project.
|
|
456
|
+
try {
|
|
457
|
+
for (const [name, data] of added) await write(assets, name, data);
|
|
458
|
+
for (const [name, data] of extensionSave.writes) await write(extensions, name, data);
|
|
459
|
+
if (manifest) await write(root, manifestFile, manifest);
|
|
460
|
+
if (extensionSave.manifestChanged) {
|
|
461
|
+
await write(root, extensionsFile, encoder.encode(`${JSON.stringify(extensionSave.embedded, null, 2)}\n`));
|
|
410
462
|
}
|
|
411
|
-
|
|
463
|
+
if (result) await write(root, projectFile, encoder.encode(`${JSON.stringify(extensionSave.project, null, 2)}\n`));
|
|
412
464
|
} 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
|
-
}
|
|
465
|
+
for (const restore of undo.reverse()) await restore().catch(() => {});
|
|
424
466
|
throw error;
|
|
425
467
|
}
|
|
426
|
-
source.project =
|
|
427
|
-
source.
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
468
|
+
source.project = extensionSave.project;
|
|
469
|
+
source.embedded = extensionSave.embedded;
|
|
470
|
+
for (const [{directory, files, prefix}, names] of [[assets, removed], [extensions, extensionSave.removed]] as const) {
|
|
471
|
+
for (const name of names) {
|
|
472
|
+
try {
|
|
473
|
+
const file = await (await directory.getFileHandle(name)).getFile();
|
|
474
|
+
if (!sameBytes(new Uint8Array(await file.arrayBuffer()), files.get(name)!)) continue;
|
|
475
|
+
await directory.removeEntry(name);
|
|
476
|
+
} catch (error) {
|
|
477
|
+
// The next save retries cleanup.
|
|
478
|
+
if (!(error instanceof DOMException && error.name === 'NotFoundError')) continue;
|
|
479
|
+
}
|
|
480
|
+
files.delete(name);
|
|
481
|
+
source.versions.delete(prefix + name);
|
|
436
482
|
}
|
|
437
483
|
}
|
|
438
484
|
session.baseline = edited;
|
|
439
485
|
session.ids = ids;
|
|
440
486
|
session.pending = null;
|
|
441
487
|
if (current === session && revision === saveRevision) host.onSaved();
|
|
442
|
-
|
|
488
|
+
notify();
|
|
489
|
+
return {status: 'saved', sourceRemoved: extensionSave.sourceRemoved};
|
|
443
490
|
} finally {
|
|
444
491
|
busy = false;
|
|
445
492
|
}
|