2020-03-13 15:32:47 -04:00
|
|
|
import "./ExportDialog.scss";
|
2020-01-15 20:42:02 +05:00
|
|
|
|
|
|
|
import React, { useState, useEffect, useRef } from "react";
|
|
|
|
|
2020-01-25 14:52:03 -03:00
|
|
|
import { ToolButton } from "./ToolButton";
|
2020-01-29 02:30:03 +02:00
|
|
|
import { clipboard, exportFile, link } from "./icons";
|
2020-04-08 09:49:52 -07:00
|
|
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
2020-01-15 20:42:02 +05:00
|
|
|
import { AppState } from "../types";
|
2020-01-28 12:25:13 -08:00
|
|
|
import { exportToCanvas } from "../scene/export";
|
2020-02-05 22:47:10 +04:00
|
|
|
import { ActionsManagerInterface } from "../actions/types";
|
2020-01-15 20:42:02 +05:00
|
|
|
import Stack from "./Stack";
|
2020-01-31 21:06:06 +00:00
|
|
|
import { t } from "../i18n";
|
2020-01-15 20:42:02 +05:00
|
|
|
|
2020-02-04 11:50:18 +01:00
|
|
|
import { probablySupportsClipboardBlob } from "../clipboard";
|
2020-02-16 22:54:50 +01:00
|
|
|
import { getSelectedElements, isSomeElementSelected } from "../scene";
|
2020-02-20 18:44:38 -05:00
|
|
|
import useIsMobile from "../is-mobile";
|
2020-03-13 15:32:47 -04:00
|
|
|
import { Dialog } from "./Dialog";
|
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;
|
|
|
|
|
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-01-20 00:56:19 -05:00
|
|
|
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-03-08 10:20:55 -07:00
|
|
|
const canvas = exportToCanvas(exportedElements, appState, {
|
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-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark,
|
2020-01-15 20:42:02 +05:00
|
|
|
});
|
|
|
|
previewNode?.appendChild(canvas);
|
|
|
|
return () => {
|
|
|
|
previewNode?.removeChild(canvas);
|
|
|
|
};
|
|
|
|
}, [
|
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-03-13 15:32:47 -04:00
|
|
|
<div className="ExportDialog__preview" ref={previewRef}></div>
|
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
|
|
|
)}
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={link}
|
|
|
|
title={t("buttons.getShareableLink")}
|
|
|
|
aria-label={t("buttons.getShareableLink")}
|
|
|
|
onClick={() => onExportToBackend(exportedElements)}
|
|
|
|
/>
|
|
|
|
</Stack.Row>
|
2020-03-15 13:26:52 -04:00
|
|
|
<div className="ExportDialog__name">
|
|
|
|
{actionManager.renderAction("changeProjectName")}
|
2020-03-13 15:32:47 -04:00
|
|
|
</div>
|
2020-03-15 13:26:52 -04:00
|
|
|
<Stack.Row gap={2}>
|
2020-03-23 13:05:07 +02:00
|
|
|
{scales.map((s) => (
|
2020-03-15 13:26:52 -04:00
|
|
|
<ToolButton
|
|
|
|
key={s}
|
|
|
|
size="s"
|
|
|
|
type="radio"
|
|
|
|
icon={`x${s}`}
|
|
|
|
name="export-canvas-scale"
|
|
|
|
aria-label={`Scale ${s} x`}
|
|
|
|
id="export-canvas-scale"
|
|
|
|
checked={s === scale}
|
|
|
|
onChange={() => setScale(s)}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</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-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;
|
|
|
|
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 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-02-20 18:44:38 -05:00
|
|
|
showAriaLabel={useIsMobile()}
|
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-03-13 15:32:47 -04:00
|
|
|
<Dialog
|
2020-01-29 02:26:02 +02:00
|
|
|
maxWidth={800}
|
2020-01-25 19:37:58 -03:00
|
|
|
onCloseRequest={handleClose}
|
2020-03-13 15:32:47 -04:00
|
|
|
title={t("buttons.export")}
|
2020-01-25 19:37:58 -03:00
|
|
|
>
|
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
|
|
|
};
|