2020-03-07 10:20:38 -05:00
|
|
|
import { fileSave } from "browser-nativefs";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { EVENT_IO, trackEvent } from "../analytics";
|
|
|
|
import { getDefaultAppState } from "../appState";
|
2020-04-05 16:13:17 -07:00
|
|
|
import {
|
|
|
|
copyCanvasToClipboardAsPng,
|
2020-09-04 14:58:32 +02:00
|
|
|
copyTextToSystemClipboard,
|
2020-04-05 16:13:17 -07:00
|
|
|
} from "../clipboard";
|
2020-12-03 17:03:02 +02:00
|
|
|
import {
|
|
|
|
ExcalidrawElement,
|
|
|
|
NonDeletedExcalidrawElement,
|
|
|
|
} from "../element/types";
|
|
|
|
import { t } from "../i18n";
|
|
|
|
import { exportToCanvas, exportToSvg } from "../scene/export";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { ExportType } from "../scene/types";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { canvasToBlob } from "./blob";
|
2020-12-05 20:00:53 +05:30
|
|
|
import { AppState } from "../types";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { serializeAsJSON } from "./json";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
export { loadFromBlob } from "./blob";
|
2020-12-03 17:03:02 +02:00
|
|
|
export { loadFromJSON, saveAsJSON } from "./json";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-07-02 16:52:58 +01:00
|
|
|
const BACKEND_V2_POST = process.env.REACT_APP_BACKEND_V2_POST_URL;
|
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
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const exportToBackend = async (
|
2020-03-07 10:20:38 -05:00
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2020-03-07 10:20:38 -05:00
|
|
|
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",
|
2020-11-29 18:32:51 +02:00
|
|
|
iv,
|
2020-03-07 10:20:38 -05:00
|
|
|
},
|
|
|
|
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);
|
2020-12-03 17:03:02 +02:00
|
|
|
trackEvent(EVENT_IO, "export", "backend");
|
2020-10-18 10:39:55 +02:00
|
|
|
} else if (json.error_class === "RequestTooLargeError") {
|
|
|
|
window.alert(t("alerts.couldNotCreateShareableLinkTooBig"));
|
2020-03-07 10:20:38 -05:00
|
|
|
} else {
|
|
|
|
window.alert(t("alerts.couldNotCreateShareableLink"));
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
window.alert(t("alerts.couldNotCreateShareableLink"));
|
|
|
|
}
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const exportCanvas = async (
|
2020-03-07 10:20:38 -05:00
|
|
|
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,
|
2020-12-01 14:00:13 +01:00
|
|
|
name,
|
2020-03-07 10:20:38 -05:00
|
|
|
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;
|
2020-12-01 14:00:13 +01:00
|
|
|
name: string;
|
2020-03-07 10:20:38 -05:00
|
|
|
scale?: number;
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark: boolean;
|
2020-03-07 10:20:38 -05:00
|
|
|
},
|
2020-05-20 16:21:37 +03: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-10-28 17:10:22 +00:00
|
|
|
scale,
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark,
|
2020-10-15 21:31:21 +02:00
|
|
|
metadata:
|
|
|
|
appState.exportEmbedScene && type === "svg"
|
2020-10-18 23:06:25 +05:30
|
|
|
? await (
|
|
|
|
await import(/* webpackChunkName: "image" */ "./image")
|
|
|
|
).encodeSvgMetadata({
|
2020-10-15 21:31:21 +02:00
|
|
|
text: serializeAsJSON(elements, appState),
|
|
|
|
})
|
|
|
|
: undefined,
|
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" }), {
|
2020-12-01 14:00:13 +01:00
|
|
|
fileName: `${name}.svg`,
|
2020-09-10 12:00:18 +02:00
|
|
|
extensions: [".svg"],
|
2020-04-05 16:13:17 -07:00
|
|
|
});
|
2020-12-03 17:03:02 +02:00
|
|
|
trackEvent(EVENT_IO, "export", "svg");
|
2020-04-05 16:13:17 -07:00
|
|
|
return;
|
|
|
|
} else if (type === "clipboard-svg") {
|
2020-12-03 17:03:02 +02:00
|
|
|
trackEvent(EVENT_IO, "export", "clipboard-svg");
|
2020-09-04 14:58:32 +02:00
|
|
|
copyTextToSystemClipboard(tempSvg.outerHTML);
|
2020-04-05 16:13:17 -07:00
|
|
|
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";
|
2020-06-03 12:12:43 +02:00
|
|
|
document.body.appendChild(tempCanvas);
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
if (type === "png") {
|
2020-12-01 14:00:13 +01:00
|
|
|
const fileName = `${name}.png`;
|
2020-10-28 20:52:53 +01:00
|
|
|
let blob = await canvasToBlob(tempCanvas);
|
|
|
|
if (appState.exportEmbedScene) {
|
|
|
|
blob = await (
|
|
|
|
await import(/* webpackChunkName: "image" */ "./image")
|
|
|
|
).encodePngMetadata({
|
|
|
|
blob,
|
|
|
|
metadata: serializeAsJSON(elements, appState),
|
|
|
|
});
|
|
|
|
}
|
2020-10-13 14:47:07 +02:00
|
|
|
|
2020-10-28 20:52:53 +01:00
|
|
|
await fileSave(blob, {
|
2020-12-01 14:00:13 +01:00
|
|
|
fileName,
|
2020-10-28 20:52:53 +01:00
|
|
|
extensions: [".png"],
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-12-03 17:03:02 +02:00
|
|
|
trackEvent(EVENT_IO, "export", "png");
|
2020-03-07 10:20:38 -05:00
|
|
|
} else if (type === "clipboard") {
|
|
|
|
try {
|
2020-10-28 20:52:53 +01:00
|
|
|
await copyCanvasToClipboardAsPng(tempCanvas);
|
2020-12-03 17:03:02 +02:00
|
|
|
trackEvent(EVENT_IO, "export", "clipboard-png");
|
2020-10-28 20:52:53 +01:00
|
|
|
} catch (error) {
|
|
|
|
if (error.name === "CANVAS_POSSIBLY_TOO_BIG") {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
throw new Error(t("alerts.couldNotCopyToClipboard"));
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
|
|
|
} else if (type === "backend") {
|
2020-07-10 02:20:23 -07:00
|
|
|
exportToBackend(elements, {
|
|
|
|
...appState,
|
|
|
|
viewBackgroundColor: exportBackground
|
|
|
|
? appState.viewBackgroundColor
|
|
|
|
: getDefaultAppState().viewBackgroundColor,
|
|
|
|
});
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// clean up the DOM
|
|
|
|
if (tempCanvas !== canvas) {
|
|
|
|
tempCanvas.remove();
|
|
|
|
}
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|