excalidraw/src/data/json.ts
dependabot-preview[bot] 722c498abe
Bump prettier from 1.19.1 to 2.0.1 (#1060)
* Bump prettier from 1.19.1 to 2.0.1

Bumps [prettier](https://github.com/prettier/prettier) from 1.19.1 to 2.0.1.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/master/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/1.19.1...2.0.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Update formatting

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Panayiotis Lipiridis <lipiridis@gmail.com>
2020-03-23 13:05:07 +02:00

49 lines
1.2 KiB
TypeScript

import { ExcalidrawElement } from "../element/types";
import { AppState } from "../types";
import { cleanAppStateForExport } from "../appState";
import { fileOpen, fileSave } from "browser-nativefs";
import { loadFromBlob } from "./blob";
export function serializeAsJSON(
elements: readonly ExcalidrawElement[],
appState: AppState,
): string {
return JSON.stringify(
{
type: "excalidraw",
version: 1,
source: window.location.origin,
elements: elements.filter((element) => !element.isDeleted),
appState: cleanAppStateForExport(appState),
},
null,
2,
);
}
export async function saveAsJSON(
elements: readonly ExcalidrawElement[],
appState: AppState,
) {
const serialized = serializeAsJSON(elements, appState);
const name = `${appState.name}.excalidraw`;
await fileSave(
new Blob([serialized], { type: "application/json" }),
{
fileName: name,
description: "Excalidraw file",
},
(window as any).handle,
);
}
export async function loadFromJSON() {
const blob = await fileOpen({
description: "Excalidraw files",
extensions: ["json", "excalidraw"],
mimeTypes: ["application/json"],
});
return loadFromBlob(blob);
}