2020-01-13 11:04:28 -08:00
|
|
|
import rough from "roughjs/bin/rough";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
|
2020-01-07 19:04:52 +04:00
|
|
|
import { getElementAbsoluteCoords } from "../element";
|
2020-01-15 21:08:52 -05:00
|
|
|
import { getDefaultAppState } from "../appState";
|
2020-01-07 19:04:52 +04:00
|
|
|
|
|
|
|
import { renderScene } from "../renderer";
|
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-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-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-17 11:25:05 +01:00
|
|
|
export async function saveAsJSON(
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-15 21:08:52 -05:00
|
|
|
appState: AppState
|
2020-01-09 19:22:04 +04:00
|
|
|
) {
|
2020-01-06 20:24:54 +04:00
|
|
|
const serialized = JSON.stringify({
|
|
|
|
version: 1,
|
|
|
|
source: window.location.origin,
|
2020-01-15 21:08:52 -05:00
|
|
|
elements: elements.map(({ shape, ...el }) => el),
|
|
|
|
appState: 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,
|
|
|
|
"data:text/plain;charset=utf-8," + encodeURIComponent(serialized)
|
|
|
|
);
|
|
|
|
}
|
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-15 20:42:02 +05:00
|
|
|
export function getExportCanvasPreview(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
{
|
|
|
|
exportBackground,
|
|
|
|
exportPadding = 10,
|
2020-01-17 15:19:56 +03:00
|
|
|
viewBackgroundColor,
|
|
|
|
scale = 1
|
2020-01-15 20:42:02 +05:00
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
2020-01-17 15:19:56 +03:00
|
|
|
scale?: number;
|
2020-01-15 20:42:02 +05:00
|
|
|
viewBackgroundColor: string;
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
// calculate smallest area to fit the contents in
|
|
|
|
let subCanvasX1 = Infinity;
|
|
|
|
let subCanvasX2 = 0;
|
|
|
|
let subCanvasY1 = Infinity;
|
|
|
|
let subCanvasY2 = 0;
|
|
|
|
|
|
|
|
elements.forEach(element => {
|
|
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
|
|
|
|
subCanvasX1 = Math.min(subCanvasX1, x1);
|
|
|
|
subCanvasY1 = Math.min(subCanvasY1, y1);
|
|
|
|
subCanvasX2 = Math.max(subCanvasX2, x2);
|
|
|
|
subCanvasY2 = Math.max(subCanvasY2, y2);
|
|
|
|
});
|
|
|
|
|
|
|
|
function distance(x: number, y: number) {
|
|
|
|
return Math.abs(x > y ? x - y : y - x);
|
|
|
|
}
|
|
|
|
|
|
|
|
const tempCanvas = document.createElement("canvas");
|
2020-01-17 15:19:56 +03:00
|
|
|
const width = distance(subCanvasX1, subCanvasX2) + exportPadding * 2;
|
|
|
|
const height = distance(subCanvasY1, subCanvasY2) + exportPadding * 2;
|
|
|
|
tempCanvas.style.width = width + "px";
|
|
|
|
tempCanvas.style.height = height + "px";
|
|
|
|
tempCanvas.width = width * scale;
|
|
|
|
tempCanvas.height = height * scale;
|
|
|
|
tempCanvas.getContext("2d")?.scale(scale, scale);
|
2020-01-15 20:42:02 +05:00
|
|
|
|
|
|
|
renderScene(
|
|
|
|
elements,
|
|
|
|
rough.canvas(tempCanvas),
|
|
|
|
tempCanvas,
|
|
|
|
{
|
|
|
|
viewBackgroundColor: exportBackground ? viewBackgroundColor : null,
|
|
|
|
scrollX: 0,
|
|
|
|
scrollY: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
offsetX: -subCanvasX1 + exportPadding,
|
|
|
|
offsetY: -subCanvasY1 + exportPadding,
|
|
|
|
renderScrollbars: false,
|
|
|
|
renderSelection: false
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return tempCanvas;
|
|
|
|
}
|
|
|
|
|
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:19:56 +03:00
|
|
|
name,
|
|
|
|
scale = 1
|
2020-01-06 20:24:54 +04:00
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
|
|
|
viewBackgroundColor: string;
|
|
|
|
name: string;
|
2020-01-17 15:19:56 +03: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:19:56 +03: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) {
|
|
|
|
tempCanvas.toBlob(async blob => {
|
|
|
|
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 {
|
|
|
|
tempCanvas.toBlob(async function(blob) {
|
|
|
|
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-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));
|
|
|
|
}
|