2020-01-15 20:42:02 +05:00
|
|
|
|
import "./ExportDialog.css";
|
|
|
|
|
|
|
|
|
|
import React, { useState, useEffect, useRef } from "react";
|
|
|
|
|
|
|
|
|
|
import { Modal } from "./Modal";
|
|
|
|
|
import { ToolIcon } from "./ToolIcon";
|
2020-01-20 00:56:19 -05:00
|
|
|
|
import { clipboard, exportFile, downloadFile, 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-19 13:21:33 -08:00
|
|
|
|
import { getExportCanvasPreview } from "../scene/getExportCanvasPreview";
|
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-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;
|
|
|
|
|
|
|
|
|
|
type ExportCB = (elements: readonly ExcalidrawElement[], scale: number) => void;
|
|
|
|
|
|
2020-01-15 20:42:02 +05:00
|
|
|
|
export function ExportDialog({
|
|
|
|
|
elements,
|
|
|
|
|
appState,
|
|
|
|
|
exportPadding = 10,
|
|
|
|
|
actionManager,
|
|
|
|
|
syncActionResult,
|
|
|
|
|
onExportToPng,
|
2020-01-20 00:56:19 -05:00
|
|
|
|
onExportToClipboard,
|
|
|
|
|
onExportToBackend
|
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;
|
|
|
|
|
onExportToClipboard: ExportCB;
|
2020-01-20 00:56:19 -05:00
|
|
|
|
onExportToBackend: ExportCB;
|
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);
|
|
|
|
|
const [modalIsShown, setModalIsShown] = useState(false);
|
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);
|
|
|
|
|
const previeRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const { exportBackground, viewBackgroundColor } = appState;
|
|
|
|
|
|
|
|
|
|
const exportedElements = exportSelected
|
|
|
|
|
? elements.filter(element => element.isSelected)
|
|
|
|
|
: elements;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setExportSelected(someElementIsSelected);
|
|
|
|
|
}, [someElementIsSelected]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const previewNode = previeRef.current;
|
|
|
|
|
const canvas = getExportCanvasPreview(exportedElements, {
|
|
|
|
|
exportBackground,
|
|
|
|
|
viewBackgroundColor,
|
2020-01-17 15:43:24 +01:00
|
|
|
|
exportPadding,
|
|
|
|
|
scale
|
2020-01-15 20:42:02 +05:00
|
|
|
|
});
|
|
|
|
|
previewNode?.appendChild(canvas);
|
|
|
|
|
return () => {
|
|
|
|
|
previewNode?.removeChild(canvas);
|
|
|
|
|
};
|
|
|
|
|
}, [
|
|
|
|
|
modalIsShown,
|
|
|
|
|
exportedElements,
|
|
|
|
|
exportBackground,
|
|
|
|
|
exportPadding,
|
2020-01-17 15:43:24 +01:00
|
|
|
|
viewBackgroundColor,
|
|
|
|
|
scale
|
2020-01-15 20:42:02 +05:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
function handleClose() {
|
|
|
|
|
setModalIsShown(false);
|
|
|
|
|
setExportSelected(someElementIsSelected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<ToolIcon
|
|
|
|
|
onClick={() => setModalIsShown(true)}
|
|
|
|
|
icon={exportFile}
|
|
|
|
|
type="button"
|
|
|
|
|
aria-label="Show export dialog"
|
2020-01-21 01:14:10 +02:00
|
|
|
|
title={t("buttons.export")}
|
2020-01-15 20:42:02 +05:00
|
|
|
|
/>
|
|
|
|
|
{modalIsShown && (
|
|
|
|
|
<Modal maxWidth={640} onCloseRequest={handleClose}>
|
|
|
|
|
<div className="ExportDialog__dialog">
|
|
|
|
|
<Island padding={4}>
|
|
|
|
|
<button className="ExportDialog__close" onClick={handleClose}>
|
|
|
|
|
╳
|
|
|
|
|
</button>
|
2020-01-21 01:14:10 +02:00
|
|
|
|
<h2>{t("buttons.export")}</h2>
|
2020-01-15 20:42:02 +05:00
|
|
|
|
<div className="ExportDialog__preview" ref={previeRef}></div>
|
|
|
|
|
<div className="ExportDialog__actions">
|
|
|
|
|
<Stack.Row gap={2}>
|
|
|
|
|
<ToolIcon
|
|
|
|
|
type="button"
|
|
|
|
|
icon={downloadFile}
|
2020-01-21 01:14:10 +02:00
|
|
|
|
title={t("buttons.exportToPng")}
|
|
|
|
|
aria-label={t("buttons.exportToPng")}
|
2020-01-17 15:43:24 +01:00
|
|
|
|
onClick={() => onExportToPng(exportedElements, scale)}
|
2020-01-15 20:42:02 +05:00
|
|
|
|
/>
|
|
|
|
|
{probablySupportsClipboard && (
|
|
|
|
|
<ToolIcon
|
|
|
|
|
type="button"
|
|
|
|
|
icon={clipboard}
|
2020-01-21 01:14:10 +02:00
|
|
|
|
title={t("buttons.copyToClipboard")}
|
|
|
|
|
aria-label={t("buttons.copyToClipboard")}
|
2020-01-17 15:43:24 +01:00
|
|
|
|
onClick={() =>
|
|
|
|
|
onExportToClipboard(exportedElements, scale)
|
|
|
|
|
}
|
2020-01-15 20:42:02 +05:00
|
|
|
|
/>
|
|
|
|
|
)}
|
2020-01-20 00:56:19 -05:00
|
|
|
|
<ToolIcon
|
|
|
|
|
type="button"
|
|
|
|
|
icon={link}
|
2020-01-22 16:25:04 +02:00
|
|
|
|
title={t("buttons.getShareableLink")}
|
|
|
|
|
aria-label={t("buttons.getShareableLink")}
|
2020-01-20 00:56:19 -05:00
|
|
|
|
onClick={() => onExportToBackend(exportedElements, 1)}
|
|
|
|
|
/>
|
2020-01-15 20:42:02 +05:00
|
|
|
|
</Stack.Row>
|
|
|
|
|
|
|
|
|
|
{actionManager.renderAction(
|
|
|
|
|
"changeProjectName",
|
|
|
|
|
elements,
|
|
|
|
|
appState,
|
2020-01-21 01:14:10 +02:00
|
|
|
|
syncActionResult,
|
|
|
|
|
t
|
2020-01-15 20:42:02 +05:00
|
|
|
|
)}
|
|
|
|
|
<Stack.Col gap={1}>
|
2020-01-17 15:43:24 +01:00
|
|
|
|
<div className="ExportDialog__scales">
|
|
|
|
|
<Stack.Row gap={1} align="baseline">
|
|
|
|
|
{scales.map(s => (
|
|
|
|
|
<ToolIcon
|
2020-01-20 20:11:03 +02:00
|
|
|
|
key={s}
|
2020-01-17 15:43:24 +01:00
|
|
|
|
size="s"
|
|
|
|
|
type="radio"
|
|
|
|
|
icon={"x" + s}
|
|
|
|
|
name="export-canvas-scale"
|
|
|
|
|
id="export-canvas-scale"
|
|
|
|
|
checked={scale === s}
|
|
|
|
|
onChange={() => setScale(s)}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Stack.Row>
|
|
|
|
|
</div>
|
2020-01-15 20:42:02 +05:00
|
|
|
|
{actionManager.renderAction(
|
|
|
|
|
"changeExportBackground",
|
|
|
|
|
elements,
|
|
|
|
|
appState,
|
2020-01-21 01:14:10 +02:00
|
|
|
|
syncActionResult,
|
|
|
|
|
t
|
2020-01-15 20:42:02 +05:00
|
|
|
|
)}
|
|
|
|
|
{someElementIsSelected && (
|
|
|
|
|
<div>
|
|
|
|
|
<label>
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
checked={exportSelected}
|
|
|
|
|
onChange={e =>
|
|
|
|
|
setExportSelected(e.currentTarget.checked)
|
|
|
|
|
}
|
|
|
|
|
/>{" "}
|
2020-01-21 01:14:10 +02:00
|
|
|
|
{t("labels.onlySelected")}
|
2020-01-15 20:42:02 +05:00
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</Stack.Col>
|
|
|
|
|
</div>
|
|
|
|
|
</Island>
|
|
|
|
|
</div>
|
|
|
|
|
</Modal>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|