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-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-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-06 20:24:54 +04:00
|
|
|
function saveFile(name: string, data: string) {
|
|
|
|
// create a temporary <a> elem which we'll use to download the image
|
|
|
|
const link = document.createElement("a");
|
|
|
|
link.setAttribute("download", name);
|
|
|
|
link.setAttribute("href", data);
|
|
|
|
link.click();
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
link.remove();
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:25:05 +01:00
|
|
|
async function saveFileNative(name: string, data: Blob) {
|
|
|
|
const options = {
|
|
|
|
type: "saveFile",
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
description: `Excalidraw ${
|
|
|
|
data.type === "image/png" ? "image" : "file"
|
|
|
|
}`,
|
|
|
|
extensions: [data.type.split("/")[1]],
|
|
|
|
mimeTypes: [data.type]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
let handle;
|
|
|
|
if (data.type === "application/json") {
|
|
|
|
// For Excalidraw files (i.e., `application/json` files):
|
|
|
|
// If it exists, write back to a previously opened file.
|
|
|
|
// Else, create a new file.
|
|
|
|
if ((window as any).handle) {
|
|
|
|
handle = (window as any).handle;
|
|
|
|
} else {
|
|
|
|
handle = await (window as any).chooseFileSystemEntries(options);
|
|
|
|
(window as any).handle = handle;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For image export files (i.e., `image/png` files):
|
|
|
|
// Always create a new file.
|
|
|
|
handle = await (window as any).chooseFileSystemEntries(options);
|
|
|
|
}
|
|
|
|
const writer = await handle.createWriter();
|
|
|
|
await writer.truncate(0);
|
|
|
|
await writer.write(0, data, data.type);
|
|
|
|
await writer.close();
|
|
|
|
} catch (err) {
|
|
|
|
if (err.name !== "AbortError") {
|
|
|
|
console.error(err.name, err.message);
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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`;
|
|
|
|
if ("chooseFileSystemEntries" in window) {
|
|
|
|
await saveFileNative(
|
|
|
|
name,
|
|
|
|
new Blob([serialized], { type: "application/json" })
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
saveFile(
|
|
|
|
name,
|
2020-01-19 21:15:40 +00:00
|
|
|
"data:application/json;charset=utf-8," + encodeURIComponent(serialized)
|
2020-01-17 11:25:05 +01:00
|
|
|
);
|
|
|
|
}
|
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-17 11:25:05 +01:00
|
|
|
if ("chooseFileSystemEntries" in window) {
|
|
|
|
try {
|
|
|
|
(window as any).handle = await (window as any).chooseFileSystemEntries({
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
description: "Excalidraw files",
|
|
|
|
extensions: ["json"],
|
|
|
|
mimeTypes: ["application/json"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
const file = await (window as any).handle.getFile();
|
|
|
|
const contents = await file.text();
|
|
|
|
const { elements, appState } = updateAppState(contents);
|
|
|
|
return new Promise<DataState>(resolve => {
|
2020-01-15 21:08:52 -05:00
|
|
|
resolve(restore(elements, appState));
|
2020-01-17 11:25:05 +01:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
if (err.name !== "AbortError") {
|
|
|
|
console.error(err.name, err.message);
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
2020-01-17 11:25:05 +01:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const input = document.createElement("input");
|
|
|
|
const reader = new FileReader();
|
|
|
|
input.type = "file";
|
|
|
|
input.accept = ".json";
|
|
|
|
|
|
|
|
input.onchange = () => {
|
|
|
|
if (!input.files!.length) {
|
|
|
|
alert("A file was not selected.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.readAsText(input.files![0], "utf8");
|
2020-01-06 20:24:54 +04:00
|
|
|
};
|
2020-01-17 11:25:05 +01:00
|
|
|
|
|
|
|
input.click();
|
|
|
|
|
|
|
|
return new Promise<DataState>(resolve => {
|
|
|
|
reader.onloadend = () => {
|
|
|
|
if (reader.readyState === FileReader.DONE) {
|
|
|
|
const { elements, appState } = updateAppState(
|
|
|
|
reader.result as string
|
|
|
|
);
|
|
|
|
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 20:11:03 +02:00
|
|
|
url.searchParams.append("json", 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`;
|
|
|
|
if ("chooseFileSystemEntries" in window) {
|
2020-01-19 13:21:33 -08:00
|
|
|
tempCanvas.toBlob(async (blob: any) => {
|
2020-01-17 11:25:05 +01:00
|
|
|
if (blob) {
|
|
|
|
await saveFileNative(fileName, blob);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
saveFile(fileName, tempCanvas.toDataURL("image/png"));
|
|
|
|
}
|
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));
|
|
|
|
}
|