Import and export library from/to a file (#1940)

Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
Mohammed Salman 2020-07-27 15:29:19 +03:00 committed by GitHub
parent 7eff6893c5
commit ee8fa6aaad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 199 additions and 39 deletions

View file

@ -4,6 +4,8 @@ import { cleanAppStateForExport } from "../appState";
import { fileOpen, fileSave } from "browser-nativefs";
import { loadFromBlob } from "./blob";
import { loadLibrary } from "./localStorage";
import { Library } from "./library";
export const serializeAsJSON = (
elements: readonly ExcalidrawElement[],
@ -50,3 +52,34 @@ export const loadFromJSON = async (appState: AppState) => {
});
return loadFromBlob(blob, appState);
};
export const saveLibraryAsJSON = async () => {
const library = await loadLibrary();
const serialized = JSON.stringify(
{
type: "excalidrawlib",
version: 1,
library,
},
null,
2,
);
const fileName = `library.excalidrawlib`;
const blob = new Blob([serialized], {
type: "application/vnd.excalidrawlib+json",
});
await fileSave(blob, {
fileName,
description: "Excalidraw library file",
extensions: ["excalidrawlib"],
});
};
export const importLibraryFromJSON = async () => {
const blob = await fileOpen({
description: "Excalidraw library files",
extensions: ["json", "excalidrawlib"],
mimeTypes: ["application/json"],
});
Library.importLibrary(blob);
};