2020-12-03 17:03:02 +02:00
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
2020-11-04 18:50:53 +01:00
|
|
|
import { render, unmountComponentAtNode } from "react-dom";
|
2020-02-05 22:47:10 +04:00
|
|
|
import { ActionsManagerInterface } from "../actions/types";
|
2020-02-04 11:50:18 +01:00
|
|
|
import { probablySupportsClipboardBlob } from "../clipboard";
|
2020-10-28 20:52:53 +01:00
|
|
|
import { canvasToBlob } from "../data/blob";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
2020-10-28 20:52:53 +01:00
|
|
|
import { CanvasError } from "../errors";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { t } from "../i18n";
|
2021-04-08 19:54:50 +02:00
|
|
|
import { useIsMobile } from "../components/App";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { getSelectedElements, isSomeElementSelected } from "../scene";
|
|
|
|
import { exportToCanvas, getExportSize } from "../scene/export";
|
|
|
|
import { AppState } from "../types";
|
|
|
|
import { Dialog } from "./Dialog";
|
|
|
|
import "./ExportDialog.scss";
|
|
|
|
import { clipboard, exportFile, link } from "./icons";
|
|
|
|
import Stack from "./Stack";
|
|
|
|
import { ToolButton } from "./ToolButton";
|
2020-01-15 20:42:02 +05:00
|
|
|
|
2020-01-17 15:43:24 +01:00
|
|
|
const scales = [1, 2, 3];
|
|
|
|
const defaultScale = scales.includes(devicePixelRatio) ? devicePixelRatio : 1;
|
|
|
|
|
2021-02-24 19:52:17 +05:30
|
|
|
const supportsContextFilters =
|
|
|
|
"filter" in document.createElement("canvas").getContext("2d")!;
|
|
|
|
|
2020-10-28 20:52:53 +01:00
|
|
|
export const ErrorCanvasPreview = () => {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<h3>{t("canvasError.cannotShowPreview")}</h3>
|
|
|
|
<p>
|
|
|
|
<span>{t("canvasError.canvasTooBig")}</span>
|
|
|
|
</p>
|
|
|
|
<em>({t("canvasError.canvasTooBigTip")})</em>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-11-04 18:50:53 +01:00
|
|
|
const renderPreview = (
|
|
|
|
content: HTMLCanvasElement | Error,
|
|
|
|
previewNode: HTMLDivElement,
|
|
|
|
) => {
|
|
|
|
unmountComponentAtNode(previewNode);
|
|
|
|
previewNode.innerHTML = "";
|
|
|
|
if (content instanceof HTMLCanvasElement) {
|
|
|
|
previewNode.appendChild(content);
|
|
|
|
} else {
|
|
|
|
render(<ErrorCanvasPreview />, previewNode);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
export type ExportCB = (
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2020-01-24 12:04:54 +02:00
|
|
|
scale?: number,
|
2020-01-22 18:27:44 +02:00
|
|
|
) => void;
|
2020-01-17 15:43:24 +01:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const ExportModal = ({
|
2020-01-15 20:42:02 +05:00
|
|
|
elements,
|
|
|
|
appState,
|
|
|
|
exportPadding = 10,
|
|
|
|
actionManager,
|
|
|
|
onExportToPng,
|
2020-01-28 12:25:13 -08:00
|
|
|
onExportToSvg,
|
2020-01-20 00:56:19 -05:00
|
|
|
onExportToClipboard,
|
2020-01-24 12:04:54 +02:00
|
|
|
onExportToBackend,
|
2020-01-15 20:42:02 +05:00
|
|
|
}: {
|
|
|
|
appState: AppState;
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[];
|
2020-01-15 20:42:02 +05:00
|
|
|
exportPadding?: number;
|
|
|
|
actionManager: ActionsManagerInterface;
|
2020-01-17 15:43:24 +01:00
|
|
|
onExportToPng: ExportCB;
|
2020-01-28 12:25:13 -08:00
|
|
|
onExportToSvg: ExportCB;
|
2020-01-17 15:43:24 +01:00
|
|
|
onExportToClipboard: ExportCB;
|
2020-12-20 19:44:04 +05:30
|
|
|
onExportToBackend?: ExportCB;
|
2020-01-25 14:11:26 -08:00
|
|
|
onCloseRequest: () => void;
|
2020-05-20 16:21:37 +03:00
|
|
|
}) => {
|
2020-03-08 10:20:55 -07:00
|
|
|
const someElementIsSelected = isSomeElementSelected(elements, appState);
|
2020-01-17 15:43:24 +01:00
|
|
|
const [scale, setScale] = useState(defaultScale);
|
2020-01-15 20:42:02 +05:00
|
|
|
const [exportSelected, setExportSelected] = useState(someElementIsSelected);
|
2020-01-24 20:02:55 +00:00
|
|
|
const previewRef = useRef<HTMLDivElement>(null);
|
2020-04-19 20:50:23 +01:00
|
|
|
const {
|
|
|
|
exportBackground,
|
|
|
|
viewBackgroundColor,
|
|
|
|
shouldAddWatermark,
|
|
|
|
} = appState;
|
2020-01-15 20:42:02 +05:00
|
|
|
|
|
|
|
const exportedElements = exportSelected
|
2020-03-08 10:20:55 -07:00
|
|
|
? getSelectedElements(elements, appState)
|
2020-01-15 20:42:02 +05:00
|
|
|
: elements;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setExportSelected(someElementIsSelected);
|
|
|
|
}, [someElementIsSelected]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-01-24 20:02:55 +00:00
|
|
|
const previewNode = previewRef.current;
|
2020-10-28 20:52:53 +01:00
|
|
|
if (!previewNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const canvas = exportToCanvas(exportedElements, appState, {
|
|
|
|
exportBackground,
|
|
|
|
viewBackgroundColor,
|
|
|
|
exportPadding,
|
|
|
|
scale,
|
|
|
|
shouldAddWatermark,
|
|
|
|
});
|
|
|
|
|
|
|
|
// if converting to blob fails, there's some problem that will
|
|
|
|
// likely prevent preview and export (e.g. canvas too big)
|
|
|
|
canvasToBlob(canvas)
|
|
|
|
.then(() => {
|
2020-11-04 18:50:53 +01:00
|
|
|
renderPreview(canvas, previewNode);
|
2020-10-28 20:52:53 +01:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
2020-11-04 18:50:53 +01:00
|
|
|
renderPreview(new CanvasError(), previewNode);
|
2020-10-28 20:52:53 +01:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2020-11-04 18:50:53 +01:00
|
|
|
renderPreview(new CanvasError(), previewNode);
|
2020-10-28 20:52:53 +01:00
|
|
|
}
|
2020-01-15 20:42:02 +05:00
|
|
|
}, [
|
2020-03-08 10:20:55 -07:00
|
|
|
appState,
|
2020-01-15 20:42:02 +05:00
|
|
|
exportedElements,
|
|
|
|
exportBackground,
|
|
|
|
exportPadding,
|
2020-01-17 15:43:24 +01:00
|
|
|
viewBackgroundColor,
|
2020-01-24 12:04:54 +02:00
|
|
|
scale,
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark,
|
2020-01-15 20:42:02 +05:00
|
|
|
]);
|
|
|
|
|
2020-01-25 14:11:26 -08:00
|
|
|
return (
|
2020-04-08 21:31:40 +09:00
|
|
|
<div className="ExportDialog">
|
2020-11-04 18:50:53 +01:00
|
|
|
<div className="ExportDialog__preview" ref={previewRef} />
|
2021-02-24 19:52:17 +05:30
|
|
|
{supportsContextFilters &&
|
|
|
|
actionManager.renderAction("exportWithDarkMode")}
|
2020-03-15 13:26:52 -04:00
|
|
|
<Stack.Col gap={2} align="center">
|
|
|
|
<div className="ExportDialog__actions">
|
2020-03-13 15:32:47 -04:00
|
|
|
<Stack.Row gap={2}>
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
label="PNG"
|
|
|
|
title={t("buttons.exportToPng")}
|
|
|
|
aria-label={t("buttons.exportToPng")}
|
|
|
|
onClick={() => onExportToPng(exportedElements, scale)}
|
|
|
|
/>
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
label="SVG"
|
|
|
|
title={t("buttons.exportToSvg")}
|
|
|
|
aria-label={t("buttons.exportToSvg")}
|
|
|
|
onClick={() => onExportToSvg(exportedElements, scale)}
|
|
|
|
/>
|
|
|
|
{probablySupportsClipboardBlob && (
|
2020-01-29 02:25:47 +02:00
|
|
|
<ToolButton
|
|
|
|
type="button"
|
2020-03-13 15:32:47 -04:00
|
|
|
icon={clipboard}
|
2020-03-23 16:15:22 +01:00
|
|
|
title={t("buttons.copyPngToClipboard")}
|
|
|
|
aria-label={t("buttons.copyPngToClipboard")}
|
2020-03-13 15:32:47 -04:00
|
|
|
onClick={() => onExportToClipboard(exportedElements, scale)}
|
2020-01-29 02:25:47 +02:00
|
|
|
/>
|
2020-03-13 15:32:47 -04:00
|
|
|
)}
|
2020-12-20 19:44:04 +05:30
|
|
|
{onExportToBackend && (
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={link}
|
|
|
|
title={t("buttons.getShareableLink")}
|
|
|
|
aria-label={t("buttons.getShareableLink")}
|
|
|
|
onClick={() => onExportToBackend(exportedElements)}
|
|
|
|
/>
|
|
|
|
)}
|
2020-03-13 15:32:47 -04:00
|
|
|
</Stack.Row>
|
2020-12-01 14:00:13 +01:00
|
|
|
<div className="ExportDialog__name">
|
|
|
|
{actionManager.renderAction("changeProjectName")}
|
|
|
|
</div>
|
2020-03-15 13:26:52 -04:00
|
|
|
<Stack.Row gap={2}>
|
2020-10-07 06:37:38 -07:00
|
|
|
{scales.map((s) => {
|
|
|
|
const [width, height] = getExportSize(
|
|
|
|
exportedElements,
|
|
|
|
exportPadding,
|
|
|
|
shouldAddWatermark,
|
|
|
|
s,
|
|
|
|
);
|
|
|
|
|
|
|
|
const scaleButtonTitle = `${t(
|
|
|
|
"buttons.scale",
|
|
|
|
)} ${s}x (${width}x${height})`;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ToolButton
|
|
|
|
key={s}
|
|
|
|
size="s"
|
|
|
|
type="radio"
|
|
|
|
icon={`${s}x`}
|
|
|
|
name="export-canvas-scale"
|
|
|
|
title={scaleButtonTitle}
|
|
|
|
aria-label={scaleButtonTitle}
|
|
|
|
id="export-canvas-scale"
|
|
|
|
checked={s === scale}
|
|
|
|
onChange={() => setScale(s)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
2020-03-15 13:26:52 -04:00
|
|
|
</Stack.Row>
|
|
|
|
</div>
|
|
|
|
{actionManager.renderAction("changeExportBackground")}
|
|
|
|
{someElementIsSelected && (
|
|
|
|
<div>
|
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={exportSelected}
|
2020-03-23 13:05:07 +02:00
|
|
|
onChange={(event) =>
|
2020-03-15 13:26:52 -04:00
|
|
|
setExportSelected(event.currentTarget.checked)
|
|
|
|
}
|
|
|
|
/>{" "}
|
|
|
|
{t("labels.onlySelected")}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
)}
|
2020-10-13 16:55:08 +02:00
|
|
|
{actionManager.renderAction("changeExportEmbedScene")}
|
2020-04-19 20:50:23 +01:00
|
|
|
{actionManager.renderAction("changeShouldAddWatermark")}
|
2020-03-15 13:26:52 -04:00
|
|
|
</Stack.Col>
|
2020-01-25 14:11:26 -08:00
|
|
|
</div>
|
|
|
|
);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-25 14:11:26 -08:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const ExportDialog = ({
|
2020-01-25 14:11:26 -08:00
|
|
|
elements,
|
|
|
|
appState,
|
|
|
|
exportPadding = 10,
|
|
|
|
actionManager,
|
|
|
|
onExportToPng,
|
2020-01-28 12:25:13 -08:00
|
|
|
onExportToSvg,
|
2020-01-25 14:11:26 -08:00
|
|
|
onExportToClipboard,
|
|
|
|
onExportToBackend,
|
|
|
|
}: {
|
|
|
|
appState: AppState;
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[];
|
2020-01-25 14:11:26 -08:00
|
|
|
exportPadding?: number;
|
|
|
|
actionManager: ActionsManagerInterface;
|
|
|
|
onExportToPng: ExportCB;
|
2020-01-28 12:25:13 -08:00
|
|
|
onExportToSvg: ExportCB;
|
2020-01-25 14:11:26 -08:00
|
|
|
onExportToClipboard: ExportCB;
|
2020-12-20 19:44:04 +05:30
|
|
|
onExportToBackend?: ExportCB;
|
2020-05-20 16:21:37 +03:00
|
|
|
}) => {
|
2020-01-25 14:11:26 -08:00
|
|
|
const [modalIsShown, setModalIsShown] = useState(false);
|
2020-01-25 19:37:58 -03:00
|
|
|
|
|
|
|
const handleClose = React.useCallback(() => {
|
|
|
|
setModalIsShown(false);
|
|
|
|
}, []);
|
2020-01-15 20:42:02 +05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2020-01-25 14:52:03 -03:00
|
|
|
<ToolButton
|
2020-12-03 12:03:29 +02:00
|
|
|
onClick={() => {
|
|
|
|
setModalIsShown(true);
|
|
|
|
}}
|
2021-03-20 16:08:03 +05:30
|
|
|
data-testid="export-button"
|
2020-01-15 20:42:02 +05:00
|
|
|
icon={exportFile}
|
|
|
|
type="button"
|
2020-01-25 19:37:58 -03:00
|
|
|
aria-label={t("buttons.export")}
|
2020-02-20 18:44:38 -05:00
|
|
|
showAriaLabel={useIsMobile()}
|
2020-01-21 01:14:10 +02:00
|
|
|
title={t("buttons.export")}
|
2020-01-15 20:42:02 +05:00
|
|
|
/>
|
|
|
|
{modalIsShown && (
|
2020-12-27 17:07:05 -05:00
|
|
|
<Dialog onCloseRequest={handleClose} title={t("buttons.export")}>
|
2020-01-25 14:11:26 -08:00
|
|
|
<ExportModal
|
|
|
|
elements={elements}
|
|
|
|
appState={appState}
|
|
|
|
exportPadding={exportPadding}
|
|
|
|
actionManager={actionManager}
|
|
|
|
onExportToPng={onExportToPng}
|
2020-01-28 12:25:13 -08:00
|
|
|
onExportToSvg={onExportToSvg}
|
2020-01-25 14:11:26 -08:00
|
|
|
onExportToClipboard={onExportToClipboard}
|
|
|
|
onExportToBackend={onExportToBackend}
|
2020-01-25 19:37:58 -03:00
|
|
|
onCloseRequest={handleClose}
|
2020-01-25 14:11:26 -08:00
|
|
|
/>
|
2020-03-13 15:32:47 -04:00
|
|
|
</Dialog>
|
2020-01-15 20:42:02 +05:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|