2020-04-08 09:49:52 -07:00
|
|
|
import {
|
|
|
|
ExcalidrawElement,
|
|
|
|
NonDeletedExcalidrawElement,
|
|
|
|
} from "../element/types";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
import { getDefaultAppState } from "../appState";
|
|
|
|
|
|
|
|
import { AppState } from "../types";
|
|
|
|
import { exportToCanvas, exportToSvg } from "../scene/export";
|
|
|
|
import { fileSave } from "browser-nativefs";
|
|
|
|
|
|
|
|
import { t } from "../i18n";
|
2020-04-05 16:13:17 -07:00
|
|
|
import {
|
|
|
|
copyCanvasToClipboardAsPng,
|
|
|
|
copyCanvasToClipboardAsSvg,
|
|
|
|
} from "../clipboard";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { serializeAsJSON } from "./json";
|
|
|
|
|
|
|
|
import { ExportType } from "../scene/types";
|
|
|
|
import { restore } from "./restore";
|
|
|
|
import { restoreFromLocalStorage } from "./localStorage";
|
|
|
|
|
|
|
|
export { loadFromBlob } from "./blob";
|
|
|
|
export { saveAsJSON, loadFromJSON } from "./json";
|
|
|
|
export { saveToLocalStorage } from "./localStorage";
|
|
|
|
|
|
|
|
const BACKEND_GET = "https://json.excalidraw.com/api/v1/";
|
|
|
|
|
|
|
|
const BACKEND_V2_POST = "https://json.excalidraw.com/api/v2/post/";
|
|
|
|
const BACKEND_V2_GET = "https://json.excalidraw.com/api/v2/";
|
|
|
|
|
basic Socket.io implementation of collaborative editing (#879)
* Enable collaborative syncing for elements
* Don't fall back to local storage if using a room, as that is confusing
* Use remote socket server
* Send updates to new users when they join
* ~
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* prettier
* Fix bug with remote pointers not changing on scroll
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Fix bug with remote pointers not changing on scroll
* remove UI for collaboration
* remove link
* clean up lingering unused UI
* set random IV passed per encrypted message, reduce room id length, refactored socket broadcasting API, rename room_id to room, removed throttling of pointer movement
* fix package.json conflict
2020-03-09 08:48:25 -07:00
|
|
|
export const SOCKET_SERVER = "https://excalidraw-socket.herokuapp.com";
|
|
|
|
|
|
|
|
export type EncryptedData = {
|
|
|
|
data: ArrayBuffer;
|
|
|
|
iv: Uint8Array;
|
|
|
|
};
|
|
|
|
|
2020-03-14 21:25:07 +01:00
|
|
|
export type SocketUpdateDataSource = {
|
2020-03-29 11:35:56 +09:00
|
|
|
SCENE_INIT: {
|
|
|
|
type: "SCENE_INIT";
|
|
|
|
payload: {
|
|
|
|
elements: readonly ExcalidrawElement[];
|
|
|
|
};
|
|
|
|
};
|
2020-03-14 21:25:07 +01:00
|
|
|
SCENE_UPDATE: {
|
|
|
|
type: "SCENE_UPDATE";
|
|
|
|
payload: {
|
|
|
|
elements: readonly ExcalidrawElement[];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
MOUSE_LOCATION: {
|
|
|
|
type: "MOUSE_LOCATION";
|
|
|
|
payload: {
|
|
|
|
socketID: string;
|
|
|
|
pointerCoords: { x: number; y: number };
|
2020-04-04 16:12:19 +01:00
|
|
|
button: "down" | "up";
|
2020-04-04 16:02:16 +02:00
|
|
|
selectedElementIds: AppState["selectedElementIds"];
|
2020-04-07 14:02:42 +01:00
|
|
|
username: string;
|
2020-03-14 21:25:07 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SocketUpdateDataIncoming =
|
|
|
|
| SocketUpdateDataSource[keyof SocketUpdateDataSource]
|
basic Socket.io implementation of collaborative editing (#879)
* Enable collaborative syncing for elements
* Don't fall back to local storage if using a room, as that is confusing
* Use remote socket server
* Send updates to new users when they join
* ~
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* prettier
* Fix bug with remote pointers not changing on scroll
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Fix bug with remote pointers not changing on scroll
* remove UI for collaboration
* remove link
* clean up lingering unused UI
* set random IV passed per encrypted message, reduce room id length, refactored socket broadcasting API, rename room_id to room, removed throttling of pointer movement
* fix package.json conflict
2020-03-09 08:48:25 -07:00
|
|
|
| {
|
|
|
|
type: "INVALID_RESPONSE";
|
|
|
|
};
|
|
|
|
|
2020-03-07 10:20:38 -05: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;
|
|
|
|
|
basic Socket.io implementation of collaborative editing (#879)
* Enable collaborative syncing for elements
* Don't fall back to local storage if using a room, as that is confusing
* Use remote socket server
* Send updates to new users when they join
* ~
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* prettier
* Fix bug with remote pointers not changing on scroll
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Fix bug with remote pointers not changing on scroll
* remove UI for collaboration
* remove link
* clean up lingering unused UI
* set random IV passed per encrypted message, reduce room id length, refactored socket broadcasting API, rename room_id to room, removed throttling of pointer movement
* fix package.json conflict
2020-03-09 08:48:25 -07:00
|
|
|
function byteToHex(byte: number): string {
|
|
|
|
return `0${byte.toString(16)}`.slice(-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateRandomID() {
|
|
|
|
const arr = new Uint8Array(10);
|
|
|
|
window.crypto.getRandomValues(arr);
|
|
|
|
return Array.from(arr, byteToHex).join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateEncryptionKey() {
|
|
|
|
const key = await window.crypto.subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: "AES-GCM",
|
|
|
|
length: 128,
|
|
|
|
},
|
|
|
|
true, // extractable
|
|
|
|
["encrypt", "decrypt"],
|
|
|
|
);
|
|
|
|
return (await window.crypto.subtle.exportKey("jwk", key)).k;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createIV() {
|
|
|
|
const arr = new Uint8Array(12);
|
|
|
|
return window.crypto.getRandomValues(arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCollaborationLinkData(link: string) {
|
|
|
|
if (link.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const hash = new URL(link).hash;
|
|
|
|
return hash.match(/^#room=([a-zA-Z0-9_-]+),([a-zA-Z0-9_-]+)$/);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function generateCollaborationLink() {
|
|
|
|
const id = await generateRandomID();
|
|
|
|
const key = await generateEncryptionKey();
|
2020-03-25 16:18:22 +01:00
|
|
|
return `${window.location.origin}${window.location.pathname}#room=${id},${key}`;
|
basic Socket.io implementation of collaborative editing (#879)
* Enable collaborative syncing for elements
* Don't fall back to local storage if using a room, as that is confusing
* Use remote socket server
* Send updates to new users when they join
* ~
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* prettier
* Fix bug with remote pointers not changing on scroll
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Fix bug with remote pointers not changing on scroll
* remove UI for collaboration
* remove link
* clean up lingering unused UI
* set random IV passed per encrypted message, reduce room id length, refactored socket broadcasting API, rename room_id to room, removed throttling of pointer movement
* fix package.json conflict
2020-03-09 08:48:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getImportedKey(key: string, usage: string): Promise<CryptoKey> {
|
|
|
|
return await window.crypto.subtle.importKey(
|
|
|
|
"jwk",
|
|
|
|
{
|
|
|
|
alg: "A128GCM",
|
|
|
|
ext: true,
|
|
|
|
k: key,
|
|
|
|
key_ops: ["encrypt", "decrypt"],
|
|
|
|
kty: "oct",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "AES-GCM",
|
|
|
|
length: 128,
|
|
|
|
},
|
|
|
|
false, // extractable
|
|
|
|
[usage],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function encryptAESGEM(
|
|
|
|
data: Uint8Array,
|
|
|
|
key: string,
|
|
|
|
): Promise<EncryptedData> {
|
|
|
|
const importedKey = await getImportedKey(key, "encrypt");
|
|
|
|
const iv = createIV();
|
|
|
|
return {
|
|
|
|
data: await window.crypto.subtle.encrypt(
|
|
|
|
{
|
|
|
|
name: "AES-GCM",
|
|
|
|
iv,
|
|
|
|
},
|
|
|
|
importedKey,
|
|
|
|
data,
|
|
|
|
),
|
|
|
|
iv,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function decryptAESGEM(
|
|
|
|
data: ArrayBuffer,
|
|
|
|
key: string,
|
|
|
|
iv: Uint8Array,
|
2020-03-14 21:25:07 +01:00
|
|
|
): Promise<SocketUpdateDataIncoming> {
|
basic Socket.io implementation of collaborative editing (#879)
* Enable collaborative syncing for elements
* Don't fall back to local storage if using a room, as that is confusing
* Use remote socket server
* Send updates to new users when they join
* ~
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* prettier
* Fix bug with remote pointers not changing on scroll
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Fix bug with remote pointers not changing on scroll
* remove UI for collaboration
* remove link
* clean up lingering unused UI
* set random IV passed per encrypted message, reduce room id length, refactored socket broadcasting API, rename room_id to room, removed throttling of pointer movement
* fix package.json conflict
2020-03-09 08:48:25 -07:00
|
|
|
try {
|
|
|
|
const importedKey = await getImportedKey(key, "decrypt");
|
|
|
|
const decrypted = await window.crypto.subtle.decrypt(
|
|
|
|
{
|
|
|
|
name: "AES-GCM",
|
|
|
|
iv: iv,
|
|
|
|
},
|
|
|
|
importedKey,
|
|
|
|
data,
|
|
|
|
);
|
|
|
|
|
|
|
|
const decodedData = new TextDecoder("utf-8").decode(
|
|
|
|
new Uint8Array(decrypted) as any,
|
|
|
|
);
|
|
|
|
return JSON.parse(decodedData);
|
|
|
|
} catch (error) {
|
|
|
|
window.alert(t("alerts.decryptFailed"));
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: "INVALID_RESPONSE",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export async function exportToBackend(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
) {
|
|
|
|
const json = serializeAsJSON(elements, appState);
|
|
|
|
const encoded = new TextEncoder().encode(json);
|
|
|
|
|
|
|
|
const key = await window.crypto.subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: "AES-GCM",
|
|
|
|
length: 128,
|
|
|
|
},
|
|
|
|
true, // extractable
|
|
|
|
["encrypt", "decrypt"],
|
|
|
|
);
|
|
|
|
// The iv is set to 0. We are never going to reuse the same key so we don't
|
|
|
|
// need to have an iv. (I hope that's correct...)
|
|
|
|
const iv = new Uint8Array(12);
|
|
|
|
// We use symmetric encryption. AES-GCM is the recommended algorithm and
|
|
|
|
// includes checks that the ciphertext has not been modified by an attacker.
|
|
|
|
const encrypted = await window.crypto.subtle.encrypt(
|
|
|
|
{
|
|
|
|
name: "AES-GCM",
|
|
|
|
iv: iv,
|
|
|
|
},
|
|
|
|
key,
|
|
|
|
encoded,
|
|
|
|
);
|
|
|
|
// We use jwk encoding to be able to extract just the base64 encoded key.
|
|
|
|
// We will hardcode the rest of the attributes when importing back the key.
|
|
|
|
const exportedKey = await window.crypto.subtle.exportKey("jwk", key);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch(BACKEND_V2_POST, {
|
|
|
|
method: "POST",
|
|
|
|
body: encrypted,
|
|
|
|
});
|
|
|
|
const json = await response.json();
|
|
|
|
if (json.id) {
|
|
|
|
const url = new URL(window.location.href);
|
|
|
|
// We need to store the key (and less importantly the id) as hash instead
|
|
|
|
// of queryParam in order to never send it to the server
|
|
|
|
url.hash = `json=${json.id},${exportedKey.k!}`;
|
|
|
|
const urlString = url.toString();
|
|
|
|
|
|
|
|
window.prompt(`🔒${t("alerts.uploadedSecurly")}`, urlString);
|
|
|
|
} else {
|
|
|
|
window.alert(t("alerts.couldNotCreateShareableLink"));
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
window.alert(t("alerts.couldNotCreateShareableLink"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function importFromBackend(
|
|
|
|
id: string | null,
|
|
|
|
privateKey: string | undefined,
|
|
|
|
) {
|
|
|
|
let elements: readonly ExcalidrawElement[] = [];
|
|
|
|
let appState: AppState = getDefaultAppState();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch(
|
|
|
|
privateKey ? `${BACKEND_V2_GET}${id}` : `${BACKEND_GET}${id}.json`,
|
|
|
|
);
|
|
|
|
if (!response.ok) {
|
|
|
|
window.alert(t("alerts.importBackendFailed"));
|
|
|
|
return restore(elements, appState, { scrollToContent: true });
|
|
|
|
}
|
|
|
|
let data;
|
|
|
|
if (privateKey) {
|
|
|
|
const buffer = await response.arrayBuffer();
|
basic Socket.io implementation of collaborative editing (#879)
* Enable collaborative syncing for elements
* Don't fall back to local storage if using a room, as that is confusing
* Use remote socket server
* Send updates to new users when they join
* ~
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* prettier
* Fix bug with remote pointers not changing on scroll
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Fix bug with remote pointers not changing on scroll
* remove UI for collaboration
* remove link
* clean up lingering unused UI
* set random IV passed per encrypted message, reduce room id length, refactored socket broadcasting API, rename room_id to room, removed throttling of pointer movement
* fix package.json conflict
2020-03-09 08:48:25 -07:00
|
|
|
const key = await getImportedKey(privateKey, "decrypt");
|
2020-03-07 10:20:38 -05:00
|
|
|
const iv = new Uint8Array(12);
|
|
|
|
const decrypted = await window.crypto.subtle.decrypt(
|
|
|
|
{
|
|
|
|
name: "AES-GCM",
|
|
|
|
iv: iv,
|
|
|
|
},
|
|
|
|
key,
|
|
|
|
buffer,
|
|
|
|
);
|
|
|
|
// We need to convert the decrypted array buffer to a string
|
|
|
|
const string = new window.TextDecoder("utf-8").decode(
|
|
|
|
new Uint8Array(decrypted) as any,
|
|
|
|
);
|
|
|
|
data = JSON.parse(string);
|
|
|
|
} else {
|
|
|
|
// Legacy format
|
|
|
|
data = await response.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
elements = data.elements || elements;
|
|
|
|
appState = data.appState || appState;
|
|
|
|
} catch (error) {
|
|
|
|
window.alert(t("alerts.importBackendFailed"));
|
|
|
|
console.error(error);
|
|
|
|
} finally {
|
|
|
|
return restore(elements, appState, { scrollToContent: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function exportCanvas(
|
|
|
|
type: ExportType,
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2020-03-07 10:20:38 -05:00
|
|
|
canvas: HTMLCanvasElement,
|
|
|
|
{
|
|
|
|
exportBackground,
|
|
|
|
exportPadding = 10,
|
|
|
|
viewBackgroundColor,
|
|
|
|
name,
|
|
|
|
scale = 1,
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark,
|
2020-03-07 10:20:38 -05:00
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
|
|
|
viewBackgroundColor: string;
|
|
|
|
name: string;
|
|
|
|
scale?: number;
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark: boolean;
|
2020-03-07 10:20:38 -05:00
|
|
|
},
|
|
|
|
) {
|
2020-04-08 09:49:52 -07:00
|
|
|
if (elements.length === 0) {
|
2020-03-07 10:20:38 -05:00
|
|
|
return window.alert(t("alerts.cannotExportEmptyCanvas"));
|
|
|
|
}
|
2020-04-05 16:13:17 -07:00
|
|
|
if (type === "svg" || type === "clipboard-svg") {
|
2020-03-07 10:20:38 -05:00
|
|
|
const tempSvg = exportToSvg(elements, {
|
|
|
|
exportBackground,
|
|
|
|
viewBackgroundColor,
|
|
|
|
exportPadding,
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark,
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-04-05 16:13:17 -07:00
|
|
|
if (type === "svg") {
|
|
|
|
await fileSave(new Blob([tempSvg.outerHTML], { type: "image/svg+xml" }), {
|
|
|
|
fileName: `${name}.svg`,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else if (type === "clipboard-svg") {
|
|
|
|
copyCanvasToClipboardAsSvg(tempSvg);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
const tempCanvas = exportToCanvas(elements, appState, {
|
2020-03-07 10:20:38 -05:00
|
|
|
exportBackground,
|
|
|
|
viewBackgroundColor,
|
|
|
|
exportPadding,
|
|
|
|
scale,
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark,
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
|
|
|
tempCanvas.style.display = "none";
|
|
|
|
document.body.appendChild(tempCanvas);
|
|
|
|
|
|
|
|
if (type === "png") {
|
|
|
|
const fileName = `${name}.png`;
|
|
|
|
tempCanvas.toBlob(async (blob: any) => {
|
|
|
|
if (blob) {
|
|
|
|
await fileSave(blob, {
|
|
|
|
fileName: fileName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (type === "clipboard") {
|
|
|
|
try {
|
|
|
|
copyCanvasToClipboardAsPng(tempCanvas);
|
|
|
|
} catch {
|
|
|
|
window.alert(t("alerts.couldNotCopyToClipboard"));
|
|
|
|
}
|
|
|
|
} else if (type === "backend") {
|
|
|
|
const appState = getDefaultAppState();
|
|
|
|
if (exportBackground) {
|
|
|
|
appState.viewBackgroundColor = viewBackgroundColor;
|
|
|
|
}
|
|
|
|
exportToBackend(elements, appState);
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up the DOM
|
|
|
|
if (tempCanvas !== canvas) {
|
|
|
|
tempCanvas.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadScene(id: string | null, privateKey?: string) {
|
|
|
|
let data;
|
|
|
|
if (id != null) {
|
|
|
|
// the private key is used to decrypt the content from the server, take
|
|
|
|
// extra care not to leak it
|
|
|
|
data = await importFromBackend(id, privateKey);
|
|
|
|
window.history.replaceState({}, "Excalidraw", window.location.origin);
|
|
|
|
} else {
|
|
|
|
data = restoreFromLocalStorage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
elements: data.elements,
|
2020-03-12 19:40:04 +01:00
|
|
|
appState: data.appState && { ...data.appState },
|
2020-03-19 14:51:05 +01:00
|
|
|
commitToHistory: false,
|
2020-03-07 10:20:38 -05:00
|
|
|
};
|
|
|
|
}
|