Implement Save without re-prompt and Save as (#1709)

* Implement Save without re-prompt and Save as
Fixes #1668

* Add save-as icon

* Make .excalidraw the default extension

* Only show save as button on supporting browsers
This commit is contained in:
Thomas Steiner 2020-06-12 18:35:04 +02:00 committed by GitHub
parent 0ed6a96b6a
commit 5d3867d8ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 74 additions and 26 deletions

View file

@ -24,28 +24,38 @@ export const serializeAsJSON = (
export const saveAsJSON = async (
elements: readonly ExcalidrawElement[],
appState: AppState,
fileHandle: any,
) => {
const serialized = serializeAsJSON(elements, appState);
const name = `${appState.name}.excalidraw`;
await fileSave(
new Blob([serialized], {
type: /\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent)
? "application/json"
: "application/vnd.excalidraw+json",
}),
{
fileName: name,
description: "Excalidraw file",
},
(window as any).handle,
);
const blob = new Blob([serialized], {
type: "application/json",
});
// Either "Save as" or non-supporting browser
if (!fileHandle) {
const name = `${appState.name}.excalidraw`;
const handle = await fileSave(
blob,
{
fileName: name,
description: "Excalidraw file",
extensions: ["excalidraw"],
},
fileHandle,
);
(window as any).handle = handle;
return;
}
// "Save"
const writable = await fileHandle.createWritable();
await writable.write(blob);
await writable.close();
};
export const loadFromJSON = async () => {
const blob = await fileOpen({
description: "Excalidraw files",
extensions: ["json", "excalidraw"],
mimeTypes: ["application/json", "application/vnd.excalidraw+json"],
mimeTypes: ["application/json"],
});
return loadFromBlob(blob);
};