2021-01-27 19:47:55 +01:00
|
|
|
import { fileOpen, fileSave } from "browser-fs-access";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { cleanAppStateForExport } from "../appState";
|
|
|
|
import { MIME_TYPES } from "../constants";
|
|
|
|
import { clearElementsForExport } from "../element";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
import { AppState } from "../types";
|
|
|
|
import { loadFromBlob } from "./blob";
|
2020-07-27 15:29:19 +03:00
|
|
|
import { Library } from "./library";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const serializeAsJSON = (
|
2020-03-07 10:20:38 -05:00
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
2020-05-20 16:21:37 +03:00
|
|
|
): string =>
|
|
|
|
JSON.stringify(
|
2020-03-07 10:20:38 -05:00
|
|
|
{
|
|
|
|
type: "excalidraw",
|
2020-05-27 15:14:50 +02:00
|
|
|
version: 2,
|
2020-03-07 10:20:38 -05:00
|
|
|
source: window.location.origin,
|
2020-11-11 15:55:22 +01:00
|
|
|
elements: clearElementsForExport(elements),
|
2020-03-07 10:20:38 -05:00
|
|
|
appState: cleanAppStateForExport(appState),
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
2,
|
|
|
|
);
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const saveAsJSON = async (
|
2020-03-07 10:20:38 -05:00
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2020-03-07 10:20:38 -05:00
|
|
|
const serialized = serializeAsJSON(elements, appState);
|
2020-06-12 18:35:04 +02:00
|
|
|
const blob = new Blob([serialized], {
|
|
|
|
type: "application/json",
|
|
|
|
});
|
2020-10-19 10:53:37 +02:00
|
|
|
|
|
|
|
const fileHandle = await fileSave(
|
2020-06-15 12:23:23 +02:00
|
|
|
blob,
|
|
|
|
{
|
2020-12-01 14:00:13 +01:00
|
|
|
fileName: appState.name,
|
2020-06-15 12:23:23 +02:00
|
|
|
description: "Excalidraw file",
|
2020-09-10 12:00:18 +02:00
|
|
|
extensions: [".excalidraw"],
|
2020-06-15 12:23:23 +02:00
|
|
|
},
|
2020-10-19 10:53:37 +02:00
|
|
|
appState.fileHandle,
|
2020-06-15 12:23:23 +02:00
|
|
|
);
|
2020-10-19 10:53:37 +02:00
|
|
|
return { fileHandle };
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-06-12 18:35:04 +02:00
|
|
|
|
2020-10-13 13:46:52 +02:00
|
|
|
export const loadFromJSON = async (localAppState: AppState) => {
|
2020-03-07 10:20:38 -05:00
|
|
|
const blob = await fileOpen({
|
|
|
|
description: "Excalidraw files",
|
2020-10-13 14:47:07 +02:00
|
|
|
extensions: [".json", ".excalidraw", ".png", ".svg"],
|
|
|
|
mimeTypes: ["application/json", "image/png", "image/svg+xml"],
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-10-13 13:46:52 +02:00
|
|
|
return loadFromBlob(blob, localAppState);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-07-27 15:29:19 +03:00
|
|
|
|
2020-10-01 10:12:43 -07:00
|
|
|
export const isValidLibrary = (json: any) => {
|
|
|
|
return (
|
|
|
|
typeof json === "object" &&
|
|
|
|
json &&
|
|
|
|
json.type === "excalidrawlib" &&
|
|
|
|
json.version === 1
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-07-27 15:29:19 +03:00
|
|
|
export const saveLibraryAsJSON = async () => {
|
2020-10-30 21:01:41 +01:00
|
|
|
const library = await Library.loadLibrary();
|
2020-07-27 15:29:19 +03:00
|
|
|
const serialized = JSON.stringify(
|
|
|
|
{
|
|
|
|
type: "excalidrawlib",
|
|
|
|
version: 1,
|
|
|
|
library,
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
2,
|
|
|
|
);
|
2020-09-03 22:46:42 +03:00
|
|
|
const fileName = "library.excalidrawlib";
|
2020-07-27 15:29:19 +03:00
|
|
|
const blob = new Blob([serialized], {
|
2020-10-13 14:47:07 +02:00
|
|
|
type: MIME_TYPES.excalidrawlib,
|
2020-07-27 15:29:19 +03:00
|
|
|
});
|
|
|
|
await fileSave(blob, {
|
|
|
|
fileName,
|
|
|
|
description: "Excalidraw library file",
|
2020-09-10 12:00:18 +02:00
|
|
|
extensions: [".excalidrawlib"],
|
2020-07-27 15:29:19 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const importLibraryFromJSON = async () => {
|
|
|
|
const blob = await fileOpen({
|
|
|
|
description: "Excalidraw library files",
|
2020-09-10 12:00:18 +02:00
|
|
|
extensions: [".json", ".excalidrawlib"],
|
2020-07-27 15:29:19 +03:00
|
|
|
mimeTypes: ["application/json"],
|
|
|
|
});
|
|
|
|
Library.importLibrary(blob);
|
|
|
|
};
|