@kubohiroya/turbowarp-plus-editor-project-folder 0.9.2 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/explorer.d.ts +5 -0
- package/dist/explorer.js +259 -68
- package/dist/file-preview.d.ts +14 -4
- package/dist/index.d.ts +3 -0
- package/dist/index.js +35 -9
- package/dist/session.d.ts +2 -0
- package/dist/{shared-Dco5rGKE.js → shared-BqC3-Bzi.js} +14 -3
- package/dist/text-file-editor.d.ts +5 -0
- package/package.json +1 -1
- package/src/explorer-menu.tsx +1 -1
- package/src/explorer.tsx +169 -41
- package/src/file-preview.ts +22 -4
- package/src/index.ts +3 -0
- package/src/session.ts +33 -10
- package/src/text-file-editor.ts +16 -3
package/src/session.ts
CHANGED
|
@@ -90,6 +90,13 @@ type SaveResult = {status: 'saved' | 'unchanged'; sourceRemoved: string[]};
|
|
|
90
90
|
type SaveDirectory = {directory: FileSystemDirectoryHandle; files: Map<string, Uint8Array>; prefix: string};
|
|
91
91
|
|
|
92
92
|
type VMAsset = {assetId: string; dataFormat: string; data?: Uint8Array};
|
|
93
|
+
const encodeJson = (value: unknown) => new TextEncoder().encode(`${JSON.stringify(value, null, 2)}\n`);
|
|
94
|
+
|
|
95
|
+
function saveManifest(source: FolderSource, added: Iterable<string>, removed: readonly string[]) {
|
|
96
|
+
const value = JSON.parse(new TextDecoder().decode(source.files.get(manifestFile))) as JsonObject;
|
|
97
|
+
const entries = (value.archiveEntries as string[]).filter(name => !removed.includes(name));
|
|
98
|
+
return encodeJson({...value, archiveEntries: [...entries, ...added]});
|
|
99
|
+
}
|
|
93
100
|
|
|
94
101
|
export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
95
102
|
let current: Session | null = null;
|
|
@@ -285,6 +292,29 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
285
292
|
}
|
|
286
293
|
return paths;
|
|
287
294
|
},
|
|
295
|
+
/** The bytes the next save would write, null for a deletion, or undefined when no preview is available. */
|
|
296
|
+
readUnsavedFile(path: string): Uint8Array | null | undefined {
|
|
297
|
+
if (!current || busy) return;
|
|
298
|
+
try {
|
|
299
|
+
const {result, extensionSave, added, removed} = planSave(current, snapshot());
|
|
300
|
+
if (path === projectFile && result) return encodeJson(extensionSave.project);
|
|
301
|
+
if (path === extensionsFile && extensionSave.manifestChanged) return encodeJson(extensionSave.embedded);
|
|
302
|
+
if (path === manifestFile && (added.length || removed.length))
|
|
303
|
+
return saveManifest(current.source, added.map(([name]) => name), removed);
|
|
304
|
+
if (path.startsWith('assets/')) {
|
|
305
|
+
const name = path.slice('assets/'.length);
|
|
306
|
+
if (removed.includes(name)) return null;
|
|
307
|
+
const kind = added.find(([asset]) => asset === name)?.[1];
|
|
308
|
+
if (kind) return assetData(name, kind) ?? undefined;
|
|
309
|
+
}
|
|
310
|
+
if (path.startsWith('extensions/')) {
|
|
311
|
+
const name = path.slice('extensions/'.length);
|
|
312
|
+
return extensionSave.removed.includes(name) ? null : extensionSave.writes.get(name);
|
|
313
|
+
}
|
|
314
|
+
} catch (error) {
|
|
315
|
+
if (!(error instanceof FolderError)) throw error;
|
|
316
|
+
}
|
|
317
|
+
},
|
|
288
318
|
get canSave() {return current !== null;},
|
|
289
319
|
get busy() {return busy;},
|
|
290
320
|
get reloadState() {return reloadState();},
|
|
@@ -463,14 +493,7 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
463
493
|
notify();
|
|
464
494
|
return {status: 'unchanged', sourceRemoved: []};
|
|
465
495
|
}
|
|
466
|
-
const
|
|
467
|
-
let manifest: Uint8Array | null = null;
|
|
468
|
-
if (added.size || removed.length) {
|
|
469
|
-
const value = JSON.parse(new TextDecoder().decode(source.files.get(manifestFile))) as JsonObject;
|
|
470
|
-
const entries = (value.archiveEntries as string[]).filter(name => !removed.includes(name));
|
|
471
|
-
value.archiveEntries = [...entries, ...added.keys()];
|
|
472
|
-
manifest = encoder.encode(`${JSON.stringify(value, null, 2)}\n`);
|
|
473
|
-
}
|
|
496
|
+
const manifest = added.size || removed.length ? saveManifest(source, added.keys(), removed) : null;
|
|
474
497
|
const root = {directory: source.directory, files: source.files, prefix: ''};
|
|
475
498
|
const assets = {directory: await source.directory.getDirectoryHandle('assets', {create: true}), files: source.bytes, prefix: 'assets/'};
|
|
476
499
|
const extensions = {directory: await source.directory.getDirectoryHandle('extensions', {create: true}),
|
|
@@ -527,9 +550,9 @@ export function createFolderSession(vm: FolderVM, host: FolderHost) {
|
|
|
527
550
|
for (const [name, data] of extensionSave.writes) await write(extensions, name, data);
|
|
528
551
|
if (manifest) await write(root, manifestFile, manifest);
|
|
529
552
|
if (extensionSave.manifestChanged) {
|
|
530
|
-
await write(root, extensionsFile,
|
|
553
|
+
await write(root, extensionsFile, encodeJson(extensionSave.embedded));
|
|
531
554
|
}
|
|
532
|
-
if (result) await write(root, projectFile,
|
|
555
|
+
if (result) await write(root, projectFile, encodeJson(extensionSave.project));
|
|
533
556
|
} catch (error) {
|
|
534
557
|
for (const restore of undo.reverse()) await restore().catch(() => {});
|
|
535
558
|
throw error;
|
package/src/text-file-editor.ts
CHANGED
|
@@ -120,6 +120,8 @@ type TextFileOptions = {
|
|
|
120
120
|
readHandle(): Promise<FileSystemFileHandle>;
|
|
121
121
|
readBaseline?(): Promise<FileBaseline | null>;
|
|
122
122
|
close(): void;
|
|
123
|
+
hostSaveShortcut?: boolean;
|
|
124
|
+
onDirtyChange?(): void;
|
|
123
125
|
};
|
|
124
126
|
|
|
125
127
|
export function mountTextFile(parent: HTMLElement, options: TextFileOptions) {
|
|
@@ -199,15 +201,21 @@ export function mountTextFile(parent: HTMLElement, options: TextFileOptions) {
|
|
|
199
201
|
common, mixed ? EditorState.lineSeparator.of('\n') : [], history(), drawSelection(), highlightSpecialChars(),
|
|
200
202
|
highlightActiveLine(), EditorView.contentAttributes.of({'aria-label': options.path}),
|
|
201
203
|
keymap.of([...defaultKeymap, ...historyKeymap]),
|
|
202
|
-
EditorView.updateListener.of(update => {if (update.docChanged) updateSaveState();})
|
|
204
|
+
EditorView.updateListener.of(update => {if (update.docChanged) updateSaveState(undefined, true);})
|
|
203
205
|
]
|
|
204
206
|
});
|
|
205
207
|
const dirty = () => !view.state.doc.eq(savedDoc);
|
|
206
208
|
const serialize = () => bom + (newline === '\n' ? view.state.doc.toString() : view.state.doc.toString().replace(/\n/gu, newline));
|
|
207
209
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
+
let wasDirty = false;
|
|
211
|
+
function updateSaveState(text = saving ? message.saving : dirty() ? message.unsaved : '', contentChanged = false) {
|
|
212
|
+
const isDirty = dirty();
|
|
213
|
+
saveButton.disabled = saving || !isDirty;
|
|
210
214
|
setStatus(status, text);
|
|
215
|
+
if (isDirty !== wasDirty || contentChanged && isDirty) {
|
|
216
|
+
wasDirty = isDirty;
|
|
217
|
+
options.onDirtyChange?.();
|
|
218
|
+
}
|
|
211
219
|
}
|
|
212
220
|
|
|
213
221
|
async function unchanged(handle: FileSystemFileHandle) {
|
|
@@ -334,6 +342,8 @@ export function mountTextFile(parent: HTMLElement, options: TextFileOptions) {
|
|
|
334
342
|
};
|
|
335
343
|
panel.addEventListener('keydown', event => {
|
|
336
344
|
if (!(event.metaKey || event.ctrlKey) || event.key.toLowerCase() !== 's') return;
|
|
345
|
+
// Let the main window's save shortcut choose the target when the host manages it.
|
|
346
|
+
if (options.hostSaveShortcut && panel.ownerDocument === document) return;
|
|
337
347
|
event.preventDefault();
|
|
338
348
|
event.stopPropagation();
|
|
339
349
|
void save();
|
|
@@ -342,6 +352,9 @@ export function mountTextFile(parent: HTMLElement, options: TextFileOptions) {
|
|
|
342
352
|
showDiff(false);
|
|
343
353
|
updateSaveState();
|
|
344
354
|
return {
|
|
355
|
+
save,
|
|
356
|
+
isDirty: dirty,
|
|
357
|
+
readUnsaved: () => !disposed && dirty() ? encoder.encode(serialize()) : undefined,
|
|
345
358
|
requestClose() {
|
|
346
359
|
if (!saving && (!dirty() || browser?.confirm(message.close))) options.close();
|
|
347
360
|
},
|