2021-01-27 19:47:55 +01:00
|
|
|
import { fileSave } from "browser-fs-access";
|
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-05-30 15:31:12 +01:00
|
|
|
import { DEFAULT_EXPORT_PADDING } 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";
|
2020-12-05 20:00:53 +05:30
|
|
|
import { AppState } from "../types";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { canvasToBlob } from "./blob";
|
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,
|
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,
|
2020-03-07 10:20:38 -05:00
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
|
|
|
viewBackgroundColor: string;
|
2020-12-01 14:00:13 +01:00
|
|
|
name: string;
|
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") {
|
2020-03-07 10:20:38 -05:00
|
|
|
const tempSvg = exportToSvg(elements, {
|
|
|
|
exportBackground,
|
2021-02-24 19:52:17 +05:30
|
|
|
exportWithDarkMode: appState.exportWithDarkMode,
|
2020-03-07 10:20:38 -05:00
|
|
|
viewBackgroundColor,
|
|
|
|
exportPadding,
|
2021-05-30 15:31:12 +01:00
|
|
|
exportScale: appState.exportScale,
|
2020-10-15 21:31:21 +02:00
|
|
|
metadata:
|
|
|
|
appState.exportEmbedScene && type === "svg"
|
2020-10-18 23:06:25 +05:30
|
|
|
? await (
|
|
|
|
await import(/* webpackChunkName: "image" */ "./image")
|
|
|
|
).encodeSvgMetadata({
|
2020-10-15 21:31:21 +02:00
|
|
|
text: serializeAsJSON(elements, appState),
|
|
|
|
})
|
|
|
|
: undefined,
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
2020-04-05 16:13:17 -07:00
|
|
|
if (type === "svg") {
|
|
|
|
await fileSave(new Blob([tempSvg.outerHTML], { type: "image/svg+xml" }), {
|
2020-12-01 14:00:13 +01:00
|
|
|
fileName: `${name}.svg`,
|
2020-09-10 12:00:18 +02:00
|
|
|
extensions: [".svg"],
|
2020-04-05 16:13:17 -07:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else if (type === "clipboard-svg") {
|
2020-09-04 14:58:32 +02:00
|
|
|
copyTextToSystemClipboard(tempSvg.outerHTML);
|
2020-04-05 16:13:17 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
const tempCanvas = exportToCanvas(elements, appState, {
|
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-12-01 14:00:13 +01:00
|
|
|
const fileName = `${name}.png`;
|
2020-10-28 20:52:53 +01:00
|
|
|
if (appState.exportEmbedScene) {
|
|
|
|
blob = await (
|
|
|
|
await import(/* webpackChunkName: "image" */ "./image")
|
|
|
|
).encodePngMetadata({
|
|
|
|
blob,
|
|
|
|
metadata: serializeAsJSON(elements, appState),
|
|
|
|
});
|
|
|
|
}
|
2020-10-13 14:47:07 +02:00
|
|
|
|
2020-10-28 20:52:53 +01:00
|
|
|
await fileSave(blob, {
|
2020-12-01 14:00:13 +01:00
|
|
|
fileName,
|
2020-10-28 20:52:53 +01:00
|
|
|
extensions: [".png"],
|
2020-03-07 10:20:38 -05:00
|
|
|
});
|
|
|
|
} else if (type === "clipboard") {
|
|
|
|
try {
|
2021-05-13 18:21:15 +01:00
|
|
|
await copyBlobToClipboardAsPng(blob);
|
2020-10-28 20:52:53 +01:00
|
|
|
} catch (error) {
|
|
|
|
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
|
|
|
};
|