2020-01-06 20:24:54 +04:00
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
|
2020-01-15 21:08:52 -05:00
|
|
|
import { getDefaultAppState } from "../appState";
|
2020-01-07 19:04:52 +04:00
|
|
|
|
2020-01-06 20:24:54 +04:00
|
|
|
import { AppState } from "../types";
|
2020-01-09 17:37:08 +01:00
|
|
|
import { ExportType } from "./types";
|
2020-01-19 13:21:33 -08:00
|
|
|
import { getExportCanvasPreview } from "./getExportCanvasPreview";
|
2020-01-07 23:49:39 +04:00
|
|
|
import nanoid from "nanoid";
|
2020-01-22 13:55:13 +01:00
|
|
|
import { fileOpenPromise, fileSavePromise } from "browser-nativefs";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
const LOCAL_STORAGE_KEY = "excalidraw";
|
|
|
|
const LOCAL_STORAGE_KEY_STATE = "excalidraw-state";
|
2020-01-20 00:56:19 -05:00
|
|
|
const BACKEND_POST = "https://json.excalidraw.com/api/v1/post/";
|
|
|
|
const BACKEND_GET = "https://json.excalidraw.com/api/v1/";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-01-22 13:55:13 +01:00
|
|
|
let fileOpen: Function;
|
|
|
|
let fileSave: Function;
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
fileOpen = (await fileOpenPromise).default;
|
|
|
|
fileSave = (await fileSavePromise).default;
|
|
|
|
})();
|
|
|
|
|
2020-01-17 11:25:05 +01:00
|
|
|
// TODO: Defined globally, since file handles aren't yet serializable.
|
|
|
|
// Once `FileSystemFileHandle` can be serialized, make this
|
|
|
|
// part of `AppState`.
|
|
|
|
(window as any).handle = null;
|
|
|
|
|
2020-01-09 19:22:04 +04:00
|
|
|
interface DataState {
|
|
|
|
elements: readonly ExcalidrawElement[];
|
2020-01-15 21:08:52 -05:00
|
|
|
appState: AppState;
|
2020-01-09 19:22:04 +04:00
|
|
|
}
|
|
|
|
|
2020-01-20 00:56:19 -05:00
|
|
|
export function serializeAsJSON(
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-20 00:56:19 -05:00
|
|
|
appState?: AppState
|
|
|
|
): string {
|
|
|
|
return JSON.stringify({
|
2020-01-06 20:24:54 +04:00
|
|
|
version: 1,
|
|
|
|
source: window.location.origin,
|
2020-01-15 21:08:52 -05:00
|
|
|
elements: elements.map(({ shape, ...el }) => el),
|
2020-01-20 00:56:19 -05:00
|
|
|
appState: appState || getDefaultAppState()
|
2020-01-06 20:24:54 +04:00
|
|
|
});
|
2020-01-20 00:56:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function saveAsJSON(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState
|
|
|
|
) {
|
|
|
|
const serialized = serializeAsJSON(elements, appState);
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-01-17 11:25:05 +01:00
|
|
|
const name = `${appState.name}.json`;
|
2020-01-22 13:55:13 +01:00
|
|
|
await fileSave(
|
|
|
|
new Blob([serialized], { type: "application/json" }),
|
|
|
|
{
|
|
|
|
fileName: name,
|
|
|
|
description: "Excalidraw file"
|
|
|
|
},
|
|
|
|
(window as any).handle
|
|
|
|
);
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
|
|
|
|
2020-01-17 11:25:05 +01:00
|
|
|
export async function loadFromJSON() {
|
|
|
|
const updateAppState = (contents: string) => {
|
|
|
|
const defaultAppState = getDefaultAppState();
|
|
|
|
let elements = [];
|
|
|
|
let appState = defaultAppState;
|
|
|
|
try {
|
|
|
|
const data = JSON.parse(contents);
|
|
|
|
elements = data.elements || [];
|
|
|
|
appState = { ...defaultAppState, ...data.appState };
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing because elements array is already empty
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
2020-01-17 11:25:05 +01:00
|
|
|
return { elements, appState };
|
2020-01-06 20:24:54 +04:00
|
|
|
};
|
|
|
|
|
2020-01-22 13:55:13 +01:00
|
|
|
const blob = await fileOpen({
|
|
|
|
description: "Excalidraw files",
|
|
|
|
extensions: ["json"],
|
|
|
|
mimeTypes: ["application/json"]
|
|
|
|
});
|
|
|
|
if (blob.handle) {
|
|
|
|
(window as any).handle = blob.handle;
|
|
|
|
}
|
|
|
|
let contents;
|
|
|
|
if ("text" in Blob) {
|
|
|
|
contents = await blob.text();
|
|
|
|
} else {
|
|
|
|
contents = await (async () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsText(blob, "utf8");
|
|
|
|
reader.onloadend = () => {
|
|
|
|
if (reader.readyState === FileReader.DONE) {
|
|
|
|
resolve(reader.result as string);
|
2020-01-17 11:25:05 +01:00
|
|
|
}
|
2020-01-22 13:55:13 +01:00
|
|
|
};
|
2020-01-17 11:25:05 +01:00
|
|
|
});
|
2020-01-22 13:55:13 +01:00
|
|
|
})();
|
2020-01-17 11:25:05 +01:00
|
|
|
}
|
2020-01-22 13:55:13 +01:00
|
|
|
const { elements, appState } = updateAppState(contents);
|
|
|
|
return new Promise<DataState>(resolve => {
|
|
|
|
resolve(restore(elements, appState));
|
|
|
|
});
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
|
|
|
|
2020-01-20 20:47:27 +02:00
|
|
|
export async function exportToBackend(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState
|
|
|
|
) {
|
2020-01-20 00:56:19 -05:00
|
|
|
const response = await fetch(BACKEND_POST, {
|
|
|
|
method: "POST",
|
2020-01-20 20:47:27 +02:00
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: serializeAsJSON(elements, appState)
|
2020-01-20 00:56:19 -05:00
|
|
|
});
|
|
|
|
const json = await response.json();
|
2020-01-20 20:11:03 +02:00
|
|
|
if (json.id) {
|
2020-01-20 00:56:19 -05:00
|
|
|
const url = new URL(window.location.href);
|
2020-01-20 21:38:51 +02:00
|
|
|
url.searchParams.append("id", json.id);
|
2020-01-20 00:56:19 -05:00
|
|
|
|
|
|
|
await navigator.clipboard.writeText(url.toString());
|
2020-01-20 20:47:27 +02:00
|
|
|
window.alert(`Copied to clipboard: ${url.toString()}`);
|
2020-01-20 00:56:19 -05:00
|
|
|
} else {
|
|
|
|
window.alert("Couldn't create shareable link");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 20:11:03 +02:00
|
|
|
export async function importFromBackend(id: string | null) {
|
2020-01-20 00:56:19 -05:00
|
|
|
let elements: readonly ExcalidrawElement[] = [];
|
|
|
|
let appState: AppState = getDefaultAppState();
|
2020-01-20 20:11:03 +02:00
|
|
|
const response = await fetch(`${BACKEND_GET}${id}.json`).then(data =>
|
2020-01-20 00:56:19 -05:00
|
|
|
data.clone().json()
|
|
|
|
);
|
|
|
|
if (response != null) {
|
|
|
|
try {
|
|
|
|
elements = response.elements || elements;
|
|
|
|
appState = response.appState || appState;
|
|
|
|
} catch (error) {
|
|
|
|
window.alert("Importing from backend failed");
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return restore(elements, appState);
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:25:05 +01:00
|
|
|
export async function exportCanvas(
|
2020-01-09 17:37:08 +01:00
|
|
|
type: ExportType,
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-06 20:24:54 +04:00
|
|
|
canvas: HTMLCanvasElement,
|
|
|
|
{
|
|
|
|
exportBackground,
|
|
|
|
exportPadding = 10,
|
|
|
|
viewBackgroundColor,
|
2020-01-17 15:43:24 +01:00
|
|
|
name,
|
|
|
|
scale = 1
|
2020-01-06 20:24:54 +04:00
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
|
|
|
viewBackgroundColor: string;
|
|
|
|
name: string;
|
2020-01-17 15:43:24 +01:00
|
|
|
scale?: number;
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
|
|
|
) {
|
|
|
|
if (!elements.length) return window.alert("Cannot export empty canvas.");
|
|
|
|
// calculate smallest area to fit the contents in
|
|
|
|
|
2020-01-17 15:43:24 +01:00
|
|
|
const tempCanvas = getExportCanvasPreview(elements, {
|
|
|
|
exportBackground,
|
|
|
|
viewBackgroundColor,
|
|
|
|
exportPadding,
|
|
|
|
scale
|
2020-01-06 20:24:54 +04:00
|
|
|
});
|
|
|
|
tempCanvas.style.display = "none";
|
|
|
|
document.body.appendChild(tempCanvas);
|
|
|
|
|
2020-01-09 17:37:08 +01:00
|
|
|
if (type === "png") {
|
2020-01-17 11:25:05 +01:00
|
|
|
const fileName = `${name}.png`;
|
2020-01-22 13:55:13 +01:00
|
|
|
tempCanvas.toBlob(async (blob: any) => {
|
|
|
|
if (blob) {
|
|
|
|
await fileSave(blob, {
|
|
|
|
fileName: fileName,
|
|
|
|
description: "Excalidraw image"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-01-09 17:37:08 +01:00
|
|
|
} else if (type === "clipboard") {
|
|
|
|
try {
|
2020-01-19 13:21:33 -08:00
|
|
|
tempCanvas.toBlob(async function(blob: any) {
|
2020-01-09 17:37:08 +01:00
|
|
|
try {
|
|
|
|
await navigator.clipboard.write([
|
|
|
|
new window.ClipboardItem({ "image/png": blob })
|
|
|
|
]);
|
|
|
|
} catch (err) {
|
|
|
|
window.alert("Couldn't copy to clipboard. Try using Chrome browser.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
window.alert("Couldn't copy to clipboard. Try using Chrome browser.");
|
|
|
|
}
|
2020-01-20 00:56:19 -05:00
|
|
|
} else if (type === "backend") {
|
2020-01-20 20:47:27 +02:00
|
|
|
const appState = getDefaultAppState();
|
|
|
|
if (exportBackground) {
|
|
|
|
appState.viewBackgroundColor = viewBackgroundColor;
|
|
|
|
}
|
|
|
|
exportToBackend(elements, appState);
|
2020-01-09 17:37:08 +01:00
|
|
|
}
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
// clean up the DOM
|
|
|
|
if (tempCanvas !== canvas) tempCanvas.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
function restore(
|
2020-01-09 19:22:04 +04:00
|
|
|
savedElements: readonly ExcalidrawElement[],
|
2020-01-15 21:08:52 -05:00
|
|
|
savedState: AppState
|
2020-01-09 19:22:04 +04:00
|
|
|
): DataState {
|
|
|
|
return {
|
|
|
|
elements: savedElements.map(element => ({
|
|
|
|
...element,
|
|
|
|
id: element.id || nanoid(),
|
|
|
|
fillStyle: element.fillStyle || "hachure",
|
|
|
|
strokeWidth: element.strokeWidth || 1,
|
|
|
|
roughness: element.roughness || 1,
|
|
|
|
opacity:
|
|
|
|
element.opacity === null || element.opacity === undefined
|
|
|
|
? 100
|
|
|
|
: element.opacity
|
|
|
|
})),
|
|
|
|
appState: savedState
|
|
|
|
};
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
|
|
|
|
2020-01-09 19:22:04 +04:00
|
|
|
export function restoreFromLocalStorage() {
|
2020-01-06 20:24:54 +04:00
|
|
|
const savedElements = localStorage.getItem(LOCAL_STORAGE_KEY);
|
|
|
|
const savedState = localStorage.getItem(LOCAL_STORAGE_KEY_STATE);
|
|
|
|
|
2020-01-09 19:22:04 +04:00
|
|
|
let elements = [];
|
|
|
|
if (savedElements) {
|
|
|
|
try {
|
2020-01-12 14:08:47 +04:00
|
|
|
elements = JSON.parse(savedElements).map(
|
|
|
|
({ shape, ...element }: ExcalidrawElement) => element
|
|
|
|
);
|
2020-01-09 19:22:04 +04:00
|
|
|
} catch (e) {
|
|
|
|
// Do nothing because elements array is already empty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let appState = null;
|
|
|
|
if (savedState) {
|
|
|
|
try {
|
|
|
|
appState = JSON.parse(savedState);
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing because appState is already null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return restore(elements, appState);
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function saveToLocalStorage(
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-06 20:24:54 +04:00
|
|
|
state: AppState
|
|
|
|
) {
|
|
|
|
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(elements));
|
|
|
|
localStorage.setItem(LOCAL_STORAGE_KEY_STATE, JSON.stringify(state));
|
|
|
|
}
|