2020-01-15 20:42:02 +05:00
|
|
|
|
import "./ExportDialog.css";
|
|
|
|
|
|
|
|
|
|
import React, { useState, useEffect, useRef } from "react";
|
|
|
|
|
|
|
|
|
|
import { Modal } from "./Modal";
|
2020-01-25 14:52:03 -03:00
|
|
|
|
import { ToolButton } from "./ToolButton";
|
2020-01-28 12:25:13 -08:00
|
|
|
|
import { clipboard, exportFile, downloadFile, svgFile, link } from "./icons";
|
2020-01-15 20:42:02 +05:00
|
|
|
|
import { Island } from "./Island";
|
|
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
|
import { AppState } from "../types";
|
2020-01-28 12:25:13 -08:00
|
|
|
|
import { exportToCanvas } from "../scene/export";
|
2020-01-15 20:42:02 +05:00
|
|
|
|
import { ActionsManagerInterface, UpdaterFn } from "../actions/types";
|
|
|
|
|
import Stack from "./Stack";
|
|
|
|
|
|
2020-01-21 01:14:10 +02:00
|
|
|
|
import { useTranslation } from "react-i18next";
|
2020-01-25 19:37:58 -03:00
|
|
|
|
import { KEYS } from "../keys";
|
2020-01-21 01:14:10 +02:00
|
|
|
|
|
2020-01-15 20:42:02 +05:00
|
|
|
|
const probablySupportsClipboard =
|
|
|
|
|
"toBlob" in HTMLCanvasElement.prototype &&
|
|
|
|
|
"clipboard" in navigator &&
|
|
|
|
|
"write" in navigator.clipboard &&
|
|
|
|
|
"ClipboardItem" in window;
|
|
|
|
|
|
2020-01-17 15:43:24 +01:00
|
|
|
|
const scales = [1, 2, 3];
|
|
|
|
|
const defaultScale = scales.includes(devicePixelRatio) ? devicePixelRatio : 1;
|
|
|
|
|
|
2020-01-22 18:27:44 +02:00
|
|
|
|
type ExportCB = (
|
|
|
|
|
elements: readonly ExcalidrawElement[],
|
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-01-25 14:11:26 -08:00
|
|
|
|
function ExportModal({
|
2020-01-15 20:42:02 +05:00
|
|
|
|
elements,
|
|
|
|
|
appState,
|
|
|
|
|
exportPadding = 10,
|
|
|
|
|
actionManager,
|
|
|
|
|
syncActionResult,
|
|
|
|
|
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-25 14:11:26 -08:00
|
|
|
|
onCloseRequest,
|
2020-01-15 20:42:02 +05:00
|
|
|
|
}: {
|
|
|
|
|
appState: AppState;
|
|
|
|
|
elements: readonly ExcalidrawElement[];
|
|
|
|
|
exportPadding?: number;
|
|
|
|
|
actionManager: ActionsManagerInterface;
|
|
|
|
|
syncActionResult: UpdaterFn;
|
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-01-20 00:56:19 -05:00
|
|
|
|
onExportToBackend: ExportCB;
|
2020-01-25 14:11:26 -08:00
|
|
|
|
onCloseRequest: () => void;
|
2020-01-15 20:42:02 +05:00
|
|
|
|
}) {
|
2020-01-21 01:14:10 +02:00
|
|
|
|
const { t } = useTranslation();
|
2020-01-15 20:42:02 +05:00
|
|
|
|
const someElementIsSelected = elements.some(element => element.isSelected);
|
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-01-15 20:42:02 +05:00
|
|
|
|
const { exportBackground, viewBackgroundColor } = appState;
|
2020-01-25 19:37:58 -03:00
|
|
|
|
const pngButton = useRef<HTMLButtonElement>(null);
|
|
|
|
|
const closeButton = useRef<HTMLButtonElement>(null);
|
|
|
|
|
const onlySelectedInput = useRef<HTMLInputElement>(null);
|
2020-01-15 20:42:02 +05:00
|
|
|
|
|
|
|
|
|
const exportedElements = exportSelected
|
|
|
|
|
? elements.filter(element => element.isSelected)
|
|
|
|
|
: elements;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setExportSelected(someElementIsSelected);
|
|
|
|
|
}, [someElementIsSelected]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-01-24 20:02:55 +00:00
|
|
|
|
const previewNode = previewRef.current;
|
2020-01-28 12:25:13 -08:00
|
|
|
|
const canvas = exportToCanvas(exportedElements, {
|
2020-01-15 20:42:02 +05:00
|
|
|
|
exportBackground,
|
|
|
|
|
viewBackgroundColor,
|
2020-01-17 15:43:24 +01:00
|
|
|
|
exportPadding,
|
2020-01-24 12:04:54 +02:00
|
|
|
|
scale,
|
2020-01-15 20:42:02 +05:00
|
|
|
|
});
|
|
|
|
|
previewNode?.appendChild(canvas);
|
|
|
|
|
return () => {
|
|
|
|
|
previewNode?.removeChild(canvas);
|
|
|
|
|
};
|
|
|
|
|
}, [
|
|
|
|
|
exportedElements,
|
|
|
|
|
exportBackground,
|
|
|
|
|
exportPadding,
|
2020-01-17 15:43:24 +01:00
|
|
|
|
viewBackgroundColor,
|
2020-01-24 12:04:54 +02:00
|
|
|
|
scale,
|
2020-01-15 20:42:02 +05:00
|
|
|
|
]);
|
|
|
|
|
|
2020-01-25 19:37:58 -03:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
pngButton.current?.focus();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
function handleKeyDown(e: React.KeyboardEvent) {
|
|
|
|
|
if (e.key === KEYS.TAB) {
|
|
|
|
|
const { activeElement } = document;
|
|
|
|
|
if (e.shiftKey) {
|
|
|
|
|
if (activeElement === pngButton.current) {
|
|
|
|
|
closeButton.current?.focus();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (activeElement === closeButton.current) {
|
|
|
|
|
pngButton.current?.focus();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
if (activeElement === onlySelectedInput.current) {
|
|
|
|
|
closeButton.current?.focus();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-25 14:11:26 -08:00
|
|
|
|
return (
|
2020-01-25 19:37:58 -03:00
|
|
|
|
<div className="ExportDialog__dialog" onKeyDown={handleKeyDown}>
|
2020-01-25 14:11:26 -08:00
|
|
|
|
<Island padding={4}>
|
2020-01-25 19:37:58 -03:00
|
|
|
|
<button
|
|
|
|
|
className="ExportDialog__close"
|
|
|
|
|
onClick={onCloseRequest}
|
|
|
|
|
aria-label={t("buttons.close")}
|
|
|
|
|
ref={closeButton}
|
|
|
|
|
>
|
2020-01-25 14:11:26 -08:00
|
|
|
|
╳
|
|
|
|
|
</button>
|
2020-01-25 19:37:58 -03:00
|
|
|
|
<h2 id="export-title">{t("buttons.export")}</h2>
|
2020-01-25 14:11:26 -08:00
|
|
|
|
<div className="ExportDialog__preview" ref={previewRef}></div>
|
|
|
|
|
<div className="ExportDialog__actions">
|
2020-01-29 02:25:47 +02:00
|
|
|
|
<Stack.Col gap={1}>
|
|
|
|
|
<Stack.Row gap={2}>
|
2020-01-25 14:11:26 -08:00
|
|
|
|
<ToolButton
|
|
|
|
|
type="button"
|
2020-01-29 02:25:47 +02:00
|
|
|
|
icon={downloadFile}
|
|
|
|
|
title={t("buttons.exportToPng")}
|
|
|
|
|
aria-label={t("buttons.exportToPng")}
|
|
|
|
|
onClick={() => onExportToPng(exportedElements, scale)}
|
|
|
|
|
ref={pngButton}
|
2020-01-25 14:11:26 -08:00
|
|
|
|
/>
|
2020-01-29 02:25:47 +02:00
|
|
|
|
<ToolButton
|
|
|
|
|
type="button"
|
|
|
|
|
icon={svgFile}
|
|
|
|
|
title={t("buttons.exportToSvg")}
|
|
|
|
|
aria-label={t("buttons.exportToSvg")}
|
|
|
|
|
onClick={() => onExportToSvg(exportedElements, scale)}
|
|
|
|
|
/>
|
|
|
|
|
{probablySupportsClipboard && (
|
|
|
|
|
<ToolButton
|
|
|
|
|
type="button"
|
|
|
|
|
icon={clipboard}
|
|
|
|
|
title={t("buttons.copyToClipboard")}
|
|
|
|
|
aria-label={t("buttons.copyToClipboard")}
|
|
|
|
|
onClick={() => onExportToClipboard(exportedElements, scale)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<ToolButton
|
|
|
|
|
type="button"
|
|
|
|
|
icon={link}
|
|
|
|
|
title={t("buttons.getShareableLink")}
|
|
|
|
|
aria-label={t("buttons.getShareableLink")}
|
|
|
|
|
onClick={() => onExportToBackend(exportedElements)}
|
|
|
|
|
/>
|
|
|
|
|
</Stack.Row>
|
|
|
|
|
</Stack.Col>
|
2020-01-25 14:11:26 -08:00
|
|
|
|
|
|
|
|
|
{actionManager.renderAction(
|
|
|
|
|
"changeProjectName",
|
|
|
|
|
elements,
|
|
|
|
|
appState,
|
|
|
|
|
syncActionResult,
|
|
|
|
|
t,
|
|
|
|
|
)}
|
|
|
|
|
<Stack.Col gap={1}>
|
|
|
|
|
<div className="ExportDialog__scales">
|
2020-01-29 02:25:47 +02:00
|
|
|
|
<Stack.Row gap={2} align="baseline">
|
2020-01-25 14:11:26 -08:00
|
|
|
|
{scales.map(s => (
|
|
|
|
|
<ToolButton
|
|
|
|
|
key={s}
|
|
|
|
|
size="s"
|
|
|
|
|
type="radio"
|
|
|
|
|
icon={"x" + s}
|
|
|
|
|
name="export-canvas-scale"
|
2020-01-25 19:37:58 -03:00
|
|
|
|
aria-label={`Scale ${s} x`}
|
2020-01-25 14:11:26 -08:00
|
|
|
|
id="export-canvas-scale"
|
|
|
|
|
checked={scale === s}
|
|
|
|
|
onChange={() => setScale(s)}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Stack.Row>
|
|
|
|
|
</div>
|
|
|
|
|
{actionManager.renderAction(
|
|
|
|
|
"changeExportBackground",
|
|
|
|
|
elements,
|
|
|
|
|
appState,
|
|
|
|
|
syncActionResult,
|
|
|
|
|
t,
|
|
|
|
|
)}
|
|
|
|
|
{someElementIsSelected && (
|
|
|
|
|
<div>
|
|
|
|
|
<label>
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
checked={exportSelected}
|
|
|
|
|
onChange={e => setExportSelected(e.currentTarget.checked)}
|
2020-01-25 19:37:58 -03:00
|
|
|
|
ref={onlySelectedInput}
|
2020-01-25 14:11:26 -08:00
|
|
|
|
/>{" "}
|
|
|
|
|
{t("labels.onlySelected")}
|
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</Stack.Col>
|
|
|
|
|
</div>
|
|
|
|
|
</Island>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ExportDialog({
|
|
|
|
|
elements,
|
|
|
|
|
appState,
|
|
|
|
|
exportPadding = 10,
|
|
|
|
|
actionManager,
|
|
|
|
|
syncActionResult,
|
|
|
|
|
onExportToPng,
|
2020-01-28 12:25:13 -08:00
|
|
|
|
onExportToSvg,
|
2020-01-25 14:11:26 -08:00
|
|
|
|
onExportToClipboard,
|
|
|
|
|
onExportToBackend,
|
|
|
|
|
}: {
|
|
|
|
|
appState: AppState;
|
|
|
|
|
elements: readonly ExcalidrawElement[];
|
|
|
|
|
exportPadding?: number;
|
|
|
|
|
actionManager: ActionsManagerInterface;
|
|
|
|
|
syncActionResult: UpdaterFn;
|
|
|
|
|
onExportToPng: ExportCB;
|
2020-01-28 12:25:13 -08:00
|
|
|
|
onExportToSvg: ExportCB;
|
2020-01-25 14:11:26 -08:00
|
|
|
|
onExportToClipboard: ExportCB;
|
|
|
|
|
onExportToBackend: ExportCB;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [modalIsShown, setModalIsShown] = useState(false);
|
2020-01-25 19:37:58 -03:00
|
|
|
|
const triggerButton = useRef<HTMLButtonElement>(null);
|
|
|
|
|
|
|
|
|
|
const handleClose = React.useCallback(() => {
|
|
|
|
|
setModalIsShown(false);
|
|
|
|
|
triggerButton.current?.focus();
|
|
|
|
|
}, []);
|
2020-01-15 20:42:02 +05:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2020-01-25 14:52:03 -03:00
|
|
|
|
<ToolButton
|
2020-01-15 20:42:02 +05:00
|
|
|
|
onClick={() => setModalIsShown(true)}
|
|
|
|
|
icon={exportFile}
|
|
|
|
|
type="button"
|
2020-01-25 19:37:58 -03:00
|
|
|
|
aria-label={t("buttons.export")}
|
2020-01-21 01:14:10 +02:00
|
|
|
|
title={t("buttons.export")}
|
2020-01-25 19:37:58 -03:00
|
|
|
|
ref={triggerButton}
|
2020-01-15 20:42:02 +05:00
|
|
|
|
/>
|
|
|
|
|
{modalIsShown && (
|
2020-01-25 19:37:58 -03:00
|
|
|
|
<Modal
|
|
|
|
|
maxWidth={640}
|
|
|
|
|
onCloseRequest={handleClose}
|
|
|
|
|
labelledBy="export-title"
|
|
|
|
|
>
|
2020-01-25 14:11:26 -08:00
|
|
|
|
<ExportModal
|
|
|
|
|
elements={elements}
|
|
|
|
|
appState={appState}
|
|
|
|
|
exportPadding={exportPadding}
|
|
|
|
|
actionManager={actionManager}
|
|
|
|
|
syncActionResult={syncActionResult}
|
|
|
|
|
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-01-15 20:42:02 +05:00
|
|
|
|
</Modal>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|