2020-01-28 12:25:13 -08:00
|
|
|
import rough from "roughjs/bin/rough";
|
2020-04-08 09:49:52 -07:00
|
|
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
2020-01-28 12:25:13 -08:00
|
|
|
import { getCommonBounds } from "../element/bounds";
|
|
|
|
import { renderScene, renderSceneToSvg } from "../renderer/renderScene";
|
2020-06-25 21:21:27 +02:00
|
|
|
import { distance, SVG_NS } from "../utils";
|
2020-03-08 10:20:55 -07:00
|
|
|
import { AppState } from "../types";
|
2021-05-30 15:31:12 +01:00
|
|
|
import { DEFAULT_EXPORT_PADDING, THEME_FILTER } from "../constants";
|
2020-11-04 17:49:15 +00:00
|
|
|
import { getDefaultAppState } from "../appState";
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2020-04-06 23:02:17 +02:00
|
|
|
export const SVG_EXPORT_TAG = `<!-- svg-source:excalidraw -->`;
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const exportToCanvas = (
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2020-01-28 12:25:13 -08:00
|
|
|
{
|
|
|
|
exportBackground,
|
2021-05-30 15:31:12 +01:00
|
|
|
exportPadding = DEFAULT_EXPORT_PADDING,
|
2020-01-28 12:25:13 -08:00
|
|
|
viewBackgroundColor,
|
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
|
|
|
viewBackgroundColor: string;
|
|
|
|
},
|
2021-02-04 14:54:08 +01:00
|
|
|
createCanvas: (
|
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
) => { canvas: HTMLCanvasElement; scale: number } = (width, height) => {
|
2021-05-30 15:31:12 +01:00
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = width * appState.exportScale;
|
|
|
|
canvas.height = height * appState.exportScale;
|
|
|
|
return { canvas, scale: appState.exportScale };
|
2020-01-28 12:25:13 -08:00
|
|
|
},
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2021-05-26 21:44:54 +02:00
|
|
|
const [minX, minY, width, height] = getCanvasSize(elements, exportPadding);
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2021-05-30 15:31:12 +01:00
|
|
|
const { canvas, scale = 1 } = createCanvas(width, height);
|
2020-01-28 12:25:13 -08:00
|
|
|
|
|
|
|
renderScene(
|
2021-05-26 21:44:54 +02:00
|
|
|
elements,
|
2020-03-08 10:20:55 -07:00
|
|
|
appState,
|
2020-02-05 22:47:10 +04:00
|
|
|
null,
|
2021-05-30 15:31:12 +01:00
|
|
|
scale,
|
|
|
|
rough.canvas(canvas),
|
|
|
|
canvas,
|
2020-01-28 12:25:13 -08:00
|
|
|
{
|
|
|
|
viewBackgroundColor: exportBackground ? viewBackgroundColor : null,
|
2021-02-24 19:52:17 +05:30
|
|
|
exportWithDarkMode: appState.exportWithDarkMode,
|
2021-01-31 10:47:43 +01:00
|
|
|
scrollX: -minX + exportPadding,
|
|
|
|
scrollY: -minY + exportPadding,
|
2020-11-04 17:49:15 +00:00
|
|
|
zoom: getDefaultAppState().zoom,
|
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
|
|
|
remotePointerViewportCoords: {},
|
2020-04-04 16:02:16 +02:00
|
|
|
remoteSelectedElementIds: {},
|
2020-03-28 16:59:36 -07:00
|
|
|
shouldCacheIgnoreZoom: false,
|
2020-04-07 14:02:42 +01:00
|
|
|
remotePointerUsernames: {},
|
2021-02-04 11:55:43 +01:00
|
|
|
remotePointerUserStates: {},
|
2020-01-28 12:25:13 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
renderScrollbars: false,
|
|
|
|
renderSelection: false,
|
2020-02-19 08:25:01 -08:00
|
|
|
renderOptimizations: false,
|
2020-06-24 17:16:03 +09:00
|
|
|
renderGrid: false,
|
2020-01-28 12:25:13 -08:00
|
|
|
},
|
|
|
|
);
|
2020-04-19 20:50:23 +01:00
|
|
|
|
2021-05-30 15:31:12 +01:00
|
|
|
return canvas;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const exportToSvg = (
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2020-01-28 12:25:13 -08:00
|
|
|
{
|
|
|
|
exportBackground,
|
2021-05-30 15:31:12 +01:00
|
|
|
exportPadding = DEFAULT_EXPORT_PADDING,
|
2020-01-28 12:25:13 -08:00
|
|
|
viewBackgroundColor,
|
2021-02-24 19:52:17 +05:30
|
|
|
exportWithDarkMode,
|
2021-05-30 15:31:12 +01:00
|
|
|
exportScale = 1,
|
2020-10-13 14:47:07 +02:00
|
|
|
metadata = "",
|
2020-01-28 12:25:13 -08:00
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
2021-05-30 15:31:12 +01:00
|
|
|
exportScale?: number;
|
2020-01-28 12:25:13 -08:00
|
|
|
viewBackgroundColor: string;
|
2021-02-24 19:52:17 +05:30
|
|
|
exportWithDarkMode?: boolean;
|
2020-10-13 14:47:07 +02:00
|
|
|
metadata?: string;
|
2020-01-28 12:25:13 -08:00
|
|
|
},
|
2020-05-20 16:21:37 +03:00
|
|
|
): SVGSVGElement => {
|
2021-05-26 21:44:54 +02:00
|
|
|
const [minX, minY, width, height] = getCanvasSize(elements, exportPadding);
|
2020-01-28 12:25:13 -08:00
|
|
|
|
|
|
|
// initialze SVG root
|
|
|
|
const svgRoot = document.createElementNS(SVG_NS, "svg");
|
|
|
|
svgRoot.setAttribute("version", "1.1");
|
|
|
|
svgRoot.setAttribute("xmlns", SVG_NS);
|
|
|
|
svgRoot.setAttribute("viewBox", `0 0 ${width} ${height}`);
|
2021-05-30 15:31:12 +01:00
|
|
|
svgRoot.setAttribute("width", `${width * exportScale}`);
|
|
|
|
svgRoot.setAttribute("height", `${height * exportScale}`);
|
2021-02-24 19:52:17 +05:30
|
|
|
if (exportWithDarkMode) {
|
2021-03-13 18:58:06 +05:30
|
|
|
svgRoot.setAttribute("filter", THEME_FILTER);
|
2021-02-24 19:52:17 +05:30
|
|
|
}
|
2020-01-28 12:25:13 -08:00
|
|
|
|
|
|
|
svgRoot.innerHTML = `
|
2020-04-06 23:02:17 +02:00
|
|
|
${SVG_EXPORT_TAG}
|
2020-10-13 14:47:07 +02:00
|
|
|
${metadata}
|
2020-01-28 12:25:13 -08:00
|
|
|
<defs>
|
|
|
|
<style>
|
|
|
|
@font-face {
|
|
|
|
font-family: "Virgil";
|
2021-02-28 14:22:32 +02:00
|
|
|
src: url("https://excalidraw.com/Virgil.woff2");
|
2020-01-28 12:25:13 -08:00
|
|
|
}
|
|
|
|
@font-face {
|
|
|
|
font-family: "Cascadia";
|
2020-04-13 21:53:07 +02:00
|
|
|
src: url("https://excalidraw.com/Cascadia.woff2");
|
2020-01-28 12:25:13 -08:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</defs>
|
|
|
|
`;
|
|
|
|
|
2020-11-02 20:14:20 +01:00
|
|
|
// render background rect
|
2020-01-28 12:25:13 -08:00
|
|
|
if (exportBackground && viewBackgroundColor) {
|
|
|
|
const rect = svgRoot.ownerDocument!.createElementNS(SVG_NS, "rect");
|
|
|
|
rect.setAttribute("x", "0");
|
|
|
|
rect.setAttribute("y", "0");
|
|
|
|
rect.setAttribute("width", `${width}`);
|
|
|
|
rect.setAttribute("height", `${height}`);
|
|
|
|
rect.setAttribute("fill", viewBackgroundColor);
|
|
|
|
svgRoot.appendChild(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rsvg = rough.svg(svgRoot);
|
2021-05-26 21:44:54 +02:00
|
|
|
renderSceneToSvg(elements, rsvg, svgRoot, {
|
2020-01-28 12:25:13 -08:00
|
|
|
offsetX: -minX + exportPadding,
|
|
|
|
offsetY: -minY + exportPadding,
|
|
|
|
});
|
2020-04-19 20:50:23 +01:00
|
|
|
|
2020-01-28 12:25:13 -08:00
|
|
|
return svgRoot;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-04-19 20:50:23 +01:00
|
|
|
|
2020-10-07 06:37:38 -07:00
|
|
|
// calculate smallest area to fit the contents in
|
|
|
|
const getCanvasSize = (
|
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
exportPadding: number,
|
|
|
|
): [number, number, number, number] => {
|
|
|
|
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
|
|
|
|
const width = distance(minX, maxX) + exportPadding * 2;
|
2021-05-26 21:44:54 +02:00
|
|
|
const height = distance(minY, maxY) + exportPadding + exportPadding;
|
2020-10-07 06:37:38 -07:00
|
|
|
|
|
|
|
return [minX, minY, width, height];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getExportSize = (
|
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
exportPadding: number,
|
|
|
|
scale: number,
|
|
|
|
): [number, number] => {
|
|
|
|
const [, , width, height] = getCanvasSize(
|
2021-05-26 21:44:54 +02:00
|
|
|
elements,
|
2020-10-07 06:37:38 -07:00
|
|
|
exportPadding,
|
|
|
|
).map((dimension) => Math.trunc(dimension * scale));
|
|
|
|
|
|
|
|
return [width, height];
|
|
|
|
};
|