Refactor save and load functionality in App.tsx
This commit is contained in:
@@ -1653,9 +1653,9 @@ export function App({ store, initialStatusMessage }: AppProps) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasPrimaryModifier && event.code === "KeyS" && !event.shiftKey) {
|
if (hasPrimaryModifier && event.code === "KeyS") {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
handleSaveDraft();
|
void handleSaveProject();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4435,40 +4435,33 @@ export function App({ store, initialStatusMessage }: AppProps) {
|
|||||||
scheduleDraftCommit(applyChange);
|
scheduleDraftCommit(applyChange);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSaveDraft = () => {
|
const handleSaveProject = async () => {
|
||||||
const result = store.saveDraft();
|
|
||||||
setStatusMessage(result.message);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleLoadDraft = () => {
|
|
||||||
const result = store.loadDraft();
|
|
||||||
setStatusMessage(result.message);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleExportJson = () => {
|
|
||||||
try {
|
try {
|
||||||
const exportedJson = store.exportDocumentJson();
|
if (!projectAssetStorageReady && projectAssetList.length > 0) {
|
||||||
const blob = new Blob([exportedJson], { type: "application/json" });
|
throw new Error("Project save failed: project asset storage is still initializing for this asset-backed scene.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const projectBytes = await saveProjectPackage(editorState.document, projectAssetStorage);
|
||||||
|
const blob = new Blob([projectBytes], { type: "application/zip" });
|
||||||
const objectUrl = URL.createObjectURL(blob);
|
const objectUrl = URL.createObjectURL(blob);
|
||||||
const anchor = document.createElement("a");
|
const anchor = document.createElement("a");
|
||||||
|
|
||||||
anchor.href = objectUrl;
|
anchor.href = objectUrl;
|
||||||
anchor.download = `${editorState.document.name.replace(/\s+/g, "-").toLowerCase() || "scene"}.json`;
|
anchor.download = createProjectDownloadName(editorState.document.name);
|
||||||
anchor.click();
|
anchor.click();
|
||||||
URL.revokeObjectURL(objectUrl);
|
URL.revokeObjectURL(objectUrl);
|
||||||
|
|
||||||
setStatusMessage("Scene document exported as JSON.");
|
setStatusMessage(`Saved project ${anchor.download}.`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = getErrorMessage(error);
|
setStatusMessage(getErrorMessage(error));
|
||||||
setStatusMessage(message);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleImportJsonButtonClick = () => {
|
const handleLoadProjectButtonClick = () => {
|
||||||
importInputRef.current?.click();
|
importProjectInputRef.current?.click();
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleImportJsonChange = async (event: ChangeEvent<HTMLInputElement>) => {
|
const handleLoadProjectChange = async (event: ChangeEvent<HTMLInputElement>) => {
|
||||||
const input = event.currentTarget;
|
const input = event.currentTarget;
|
||||||
const file = input.files?.[0];
|
const file = input.files?.[0];
|
||||||
|
|
||||||
@@ -4477,9 +4470,10 @@ export function App({ store, initialStatusMessage }: AppProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const source = await file.text();
|
const projectBytes = new Uint8Array(await file.arrayBuffer());
|
||||||
store.importDocumentJson(source);
|
const nextDocument = await loadProjectPackage(projectBytes, projectAssetStorage);
|
||||||
setStatusMessage(`Imported ${file.name}.`);
|
store.replaceDocument(nextDocument);
|
||||||
|
setStatusMessage(`Loaded project ${file.name}.`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setStatusMessage(getErrorMessage(error));
|
setStatusMessage(getErrorMessage(error));
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user