2020-04-05 16:13:17 -07:00
|
|
|
import {
|
2021-05-13 18:21:15 +01:00
|
|
|
copyBlobToClipboardAsPng,
|
2020-09-04 14:58:32 +02:00
|
|
|
copyTextToSystemClipboard,
|
2020-04-05 16:13:17 -07:00
|
|
|
} from "../clipboard";
|
2021-10-21 22:05:48 +02:00
|
|
|
import { DEFAULT_EXPORT_PADDING, MIME_TYPES } from "../constants";
|
2020-12-20 19:44:04 +05:30
|
|
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { t } from "../i18n";
|
|
|
|
import { exportToCanvas, exportToSvg } from "../scene/export";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { ExportType } from "../scene/types";
|
2021-10-21 22:05:48 +02:00
|
|
|
import { AppState, BinaryFiles } from "../types";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { canvasToBlob } from "./blob";
|
2021-10-07 13:19:40 +02:00
|
|
|
import { fileSave, FileSystemHandle } from "./filesystem";
|
2020-12-03 17:03:02 +02:00
|
|
|
import { serializeAsJSON } from "./json";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
export { loadFromBlob } from "./blob";
|
2020-12-03 17:03:02 +02:00
|
|
|
export { loadFromJSON, saveAsJSON } from "./json";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const exportCanvas = async (
|
2020-03-07 10:20:38 -05:00
|
|
|
type: ExportType,
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2021-10-21 22:05:48 +02:00
|
|
|
files: BinaryFiles,
|
2020-03-07 10:20:38 -05:00
|
|
|
{
|
|
|
|
exportBackground,
|
2021-05-30 15:31:12 +01:00
|
|
|
exportPadding = DEFAULT_EXPORT_PADDING,
|
2020-03-07 10:20:38 -05:00
|
|
|
viewBackgroundColor,
|
2020-12-01 14:00:13 +01:00
|
|
|
name,
|
2021-07-15 09:54:26 -04:00
|
|
|
fileHandle = null,
|
2020-03-07 10:20:38 -05:00
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
|
|
|
viewBackgroundColor: string;
|
2020-12-01 14:00:13 +01:00
|
|
|
name: string;
|
2021-07-15 09:54:26 -04:00
|
|
|
fileHandle?: FileSystemHandle | null;
|
2020-03-07 10:20:38 -05:00
|
|
|
},
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2020-04-08 09:49:52 -07:00
|
|
|
if (elements.length === 0) {
|
2021-01-15 20:32:46 +05:30
|
|
|
throw new Error(t("alerts.cannotExportEmptyCanvas"));
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
2020-04-05 16:13:17 -07:00
|
|
|
if (type === "svg" || type === "clipboard-svg") {
|
2021-10-21 22:05:48 +02:00
|
|
|
const tempSvg = await exportToSvg(
|
|
|
|
elements,
|
|
|
|
{
|
|
|
|
exportBackground,
|
|
|
|
exportWithDarkMode: appState.exportWithDarkMode,
|
|
|
|
viewBackgroundColor,
|
|
|
|
exportPadding,
|
|
|
|
exportScale: appState.exportScale,
|
|
|
|
exportEmbedScene: appState.exportEmbedScene && type === "svg",
|
|
|
|
},
|
|
|
|
files,
|
|
|
|
);
|
2020-04-05 16:13:17 -07:00
|
|
|
if (type === "svg") {
|
2021-07-15 09:54:26 -04:00
|
|
|
return await fileSave(
|
2021-10-21 22:05:48 +02:00
|
|
|
new Blob([tempSvg.outerHTML], { type: MIME_TYPES.svg }),
|
2021-07-15 09:54:26 -04:00
|
|
|
{
|
2021-10-07 13:19:40 +02:00
|
|
|
name,
|
|
|
|
extension: "svg",
|
|
|
|
fileHandle,
|
2021-07-15 09:54:26 -04:00
|
|
|
},
|
|
|
|
);
|
2020-04-05 16:13:17 -07:00
|
|
|
} else if (type === "clipboard-svg") {
|
2021-07-03 02:07:01 +05:30
|
|
|
await copyTextToSystemClipboard(tempSvg.outerHTML);
|
2020-04-05 16:13:17 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
const tempCanvas = await exportToCanvas(elements, appState, files, {
|
2020-03-07 10:20:38 -05:00
|
|
|
exportBackground,
|
|
|
|
viewBackgroundColor,
|
|
|
|
exportPadding,
|
|
|
|
});
|
|
|
|
tempCanvas.style.display = "none";
|
2020-06-03 12:12:43 +02:00
|
|
|
document.body.appendChild(tempCanvas);
|
2021-05-13 18:21:15 +01:00
|
|
|
let blob = await canvasToBlob(tempCanvas);
|
|
|
|
tempCanvas.remove();
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
if (type === "png") {
|
2020-10-28 20:52:53 +01:00
|
|
|
if (appState.exportEmbedScene) {
|
|
|
|
blob = await (
|
|
|
|
await import(/* webpackChunkName: "image" */ "./image")
|
|
|
|
).encodePngMetadata({
|
|
|
|
blob,
|
2021-10-21 22:05:48 +02:00
|
|
|
metadata: serializeAsJSON(elements, appState, files, "local"),
|
2020-10-28 20:52:53 +01:00
|
|
|
});
|
|
|
|
}
|
2020-10-13 14:47:07 +02:00
|
|
|
|
2021-10-07 13:19:40 +02:00
|
|
|
return await fileSave(blob, {
|
|
|
|
name,
|
|
|
|
extension: "png",
|
2021-07-15 09:54:26 -04:00
|
|
|
fileHandle,
|
2021-10-07 13:19:40 +02:00
|
|
|
});
|
2020-03-07 10:20:38 -05:00
|
|
|
} else if (type === "clipboard") {
|
|
|
|
try {
|
2021-05-13 18:21:15 +01:00
|
|
|
await copyBlobToClipboardAsPng(blob);
|
2021-11-02 14:24:16 +02:00
|
|
|
} catch (error: any) {
|
2020-10-28 20:52:53 +01:00
|
|
|
if (error.name === "CANVAS_POSSIBLY_TOO_BIG") {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
throw new Error(t("alerts.couldNotCopyToClipboard"));
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|