2020-11-02 20:14:20 +01:00
|
|
|
import {
|
|
|
|
exportToCanvas as _exportToCanvas,
|
|
|
|
exportToSvg as _exportToSvg,
|
2023-12-13 17:21:27 +01:00
|
|
|
} from "../excalidraw/scene/export";
|
|
|
|
import { getDefaultAppState } from "../excalidraw/appState";
|
2024-05-08 10:51:50 +02:00
|
|
|
import type { AppState, BinaryFiles } from "../excalidraw/types";
|
|
|
|
import type {
|
2023-11-09 17:00:21 +01:00
|
|
|
ExcalidrawElement,
|
2023-11-23 23:07:53 +01:00
|
|
|
ExcalidrawFrameLikeElement,
|
2023-11-09 17:00:21 +01:00
|
|
|
NonDeleted,
|
2023-12-13 17:21:27 +01:00
|
|
|
} from "../excalidraw/element/types";
|
|
|
|
import { restore } from "../excalidraw/data/restore";
|
|
|
|
import { MIME_TYPES } from "../excalidraw/constants";
|
|
|
|
import { encodePngMetadata } from "../excalidraw/data/image";
|
|
|
|
import { serializeAsJSON } from "../excalidraw/data/json";
|
2022-04-29 15:28:44 +02:00
|
|
|
import {
|
|
|
|
copyBlobToClipboardAsPng,
|
|
|
|
copyTextToSystemClipboard,
|
|
|
|
copyToClipboard,
|
2023-12-13 17:21:27 +01:00
|
|
|
} from "../excalidraw/clipboard";
|
2020-11-02 20:14:20 +01:00
|
|
|
|
2022-05-09 15:53:04 +02:00
|
|
|
export { MIME_TYPES };
|
|
|
|
|
2020-11-02 20:14:20 +01:00
|
|
|
type ExportOpts = {
|
2021-11-17 19:23:43 +01:00
|
|
|
elements: readonly NonDeleted<ExcalidrawElement>[];
|
2021-03-16 17:51:56 +01:00
|
|
|
appState?: Partial<Omit<AppState, "offsetTop" | "offsetLeft">>;
|
2021-10-21 22:05:48 +02:00
|
|
|
files: BinaryFiles | null;
|
2021-11-26 11:46:13 +01:00
|
|
|
maxWidthOrHeight?: number;
|
2023-11-23 23:07:53 +01:00
|
|
|
exportingFrame?: ExcalidrawFrameLikeElement | null;
|
2021-03-31 14:28:25 +02:00
|
|
|
getDimensions?: (
|
2020-11-02 20:14:20 +01:00
|
|
|
width: number,
|
|
|
|
height: number,
|
2021-11-26 11:46:13 +01:00
|
|
|
) => { width: number; height: number; scale?: number };
|
2020-11-02 20:14:20 +01:00
|
|
|
};
|
|
|
|
|
2020-12-14 14:11:48 +01:00
|
|
|
export const exportToCanvas = ({
|
2020-11-02 20:14:20 +01:00
|
|
|
elements,
|
2021-03-16 17:51:56 +01:00
|
|
|
appState,
|
2021-10-21 22:05:48 +02:00
|
|
|
files,
|
2021-11-26 11:46:13 +01:00
|
|
|
maxWidthOrHeight,
|
|
|
|
getDimensions,
|
2022-08-30 09:18:24 +02:00
|
|
|
exportPadding,
|
2023-11-09 17:00:21 +01:00
|
|
|
exportingFrame,
|
2022-08-30 09:18:24 +02:00
|
|
|
}: ExportOpts & {
|
|
|
|
exportPadding?: number;
|
|
|
|
}) => {
|
2021-03-16 17:51:56 +01:00
|
|
|
const { elements: restoredElements, appState: restoredAppState } = restore(
|
|
|
|
{ elements, appState },
|
|
|
|
null,
|
2021-07-04 22:23:35 +02:00
|
|
|
null,
|
2021-03-16 17:51:56 +01:00
|
|
|
);
|
2021-05-26 21:44:54 +02:00
|
|
|
const { exportBackground, viewBackgroundColor } = restoredAppState;
|
2020-11-02 20:14:20 +01:00
|
|
|
return _exportToCanvas(
|
2023-11-09 17:00:21 +01:00
|
|
|
restoredElements,
|
2021-04-04 11:35:16 +02:00
|
|
|
{ ...restoredAppState, offsetTop: 0, offsetLeft: 0, width: 0, height: 0 },
|
2021-10-21 22:05:48 +02:00
|
|
|
files || {},
|
2023-11-09 17:00:21 +01:00
|
|
|
{ exportBackground, exportPadding, viewBackgroundColor, exportingFrame },
|
2020-11-02 20:14:20 +01:00
|
|
|
(width: number, height: number) => {
|
|
|
|
const canvas = document.createElement("canvas");
|
2021-11-26 11:46:13 +01:00
|
|
|
|
|
|
|
if (maxWidthOrHeight) {
|
|
|
|
if (typeof getDimensions === "function") {
|
|
|
|
console.warn(
|
|
|
|
"`getDimensions()` is ignored when `maxWidthOrHeight` is supplied.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const max = Math.max(width, height);
|
|
|
|
|
2023-04-18 15:27:51 +02:00
|
|
|
// if content is less then maxWidthOrHeight, fallback on supplied scale
|
|
|
|
const scale =
|
|
|
|
maxWidthOrHeight < max
|
|
|
|
? maxWidthOrHeight / max
|
|
|
|
: appState?.exportScale ?? 1;
|
2021-11-26 11:46:13 +01:00
|
|
|
|
|
|
|
canvas.width = width * scale;
|
|
|
|
canvas.height = height * scale;
|
|
|
|
|
|
|
|
return {
|
|
|
|
canvas,
|
|
|
|
scale,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const ret = getDimensions?.(width, height) || { width, height };
|
2020-11-02 20:14:20 +01:00
|
|
|
|
|
|
|
canvas.width = ret.width;
|
|
|
|
canvas.height = ret.height;
|
|
|
|
|
2021-11-26 11:46:13 +01:00
|
|
|
return {
|
|
|
|
canvas,
|
|
|
|
scale: ret.scale ?? 1,
|
|
|
|
};
|
2020-11-02 20:14:20 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
export const exportToBlob = async (
|
2020-11-02 20:14:20 +01:00
|
|
|
opts: ExportOpts & {
|
|
|
|
mimeType?: string;
|
|
|
|
quality?: number;
|
2022-08-30 09:18:24 +02:00
|
|
|
exportPadding?: number;
|
2020-11-02 20:14:20 +01:00
|
|
|
},
|
2022-04-29 15:28:44 +02:00
|
|
|
): Promise<Blob> => {
|
2021-10-21 22:05:48 +02:00
|
|
|
let { mimeType = MIME_TYPES.png, quality } = opts;
|
2020-11-02 20:14:20 +01:00
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
if (mimeType === MIME_TYPES.png && typeof quality === "number") {
|
|
|
|
console.warn(`"quality" will be ignored for "${MIME_TYPES.png}" mimeType`);
|
2020-11-02 20:14:20 +01:00
|
|
|
}
|
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
// typo in MIME type (should be "jpeg")
|
2020-11-02 20:14:20 +01:00
|
|
|
if (mimeType === "image/jpg") {
|
2021-10-21 22:05:48 +02:00
|
|
|
mimeType = MIME_TYPES.jpg;
|
2020-11-02 20:14:20 +01:00
|
|
|
}
|
|
|
|
|
2021-11-30 22:08:55 +01:00
|
|
|
if (mimeType === MIME_TYPES.jpg && !opts.appState?.exportBackground) {
|
|
|
|
console.warn(
|
|
|
|
`Defaulting "exportBackground" to "true" for "${MIME_TYPES.jpg}" mimeType`,
|
|
|
|
);
|
|
|
|
opts = {
|
|
|
|
...opts,
|
|
|
|
appState: { ...opts.appState, exportBackground: true },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-09 17:00:21 +01:00
|
|
|
const canvas = await exportToCanvas(opts);
|
|
|
|
|
2020-11-02 20:14:20 +01:00
|
|
|
quality = quality ? quality : /image\/jpe?g/.test(mimeType) ? 0.92 : 0.8;
|
|
|
|
|
2022-04-29 15:28:44 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-11-02 20:14:20 +01:00
|
|
|
canvas.toBlob(
|
2022-04-29 15:28:44 +02:00
|
|
|
async (blob) => {
|
|
|
|
if (!blob) {
|
|
|
|
return reject(new Error("couldn't export to blob"));
|
|
|
|
}
|
2022-04-16 16:30:11 +02:00
|
|
|
if (
|
|
|
|
blob &&
|
|
|
|
mimeType === MIME_TYPES.png &&
|
|
|
|
opts.appState?.exportEmbedScene
|
|
|
|
) {
|
|
|
|
blob = await encodePngMetadata({
|
|
|
|
blob,
|
|
|
|
metadata: serializeAsJSON(
|
2023-04-16 11:56:25 +02:00
|
|
|
// NOTE as long as we're using the Scene hack, we need to ensure
|
|
|
|
// we pass the original, uncloned elements when serializing
|
|
|
|
// so that we keep ids stable
|
|
|
|
opts.elements,
|
2022-04-16 16:30:11 +02:00
|
|
|
opts.appState,
|
|
|
|
opts.files || {},
|
|
|
|
"local",
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
2020-11-02 20:14:20 +01:00
|
|
|
resolve(blob);
|
|
|
|
},
|
|
|
|
mimeType,
|
|
|
|
quality,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-07-02 22:37:01 +02:00
|
|
|
export const exportToSvg = async ({
|
2020-11-02 20:14:20 +01:00
|
|
|
elements,
|
|
|
|
appState = getDefaultAppState(),
|
2021-10-21 22:05:48 +02:00
|
|
|
files = {},
|
2020-11-02 20:14:20 +01:00
|
|
|
exportPadding,
|
2023-07-24 16:51:53 +02:00
|
|
|
renderEmbeddables,
|
2023-11-09 17:00:21 +01:00
|
|
|
exportingFrame,
|
2024-07-25 18:55:55 +02:00
|
|
|
skipInliningFonts,
|
2021-03-31 14:28:25 +02:00
|
|
|
}: Omit<ExportOpts, "getDimensions"> & {
|
2020-11-02 20:14:20 +01:00
|
|
|
exportPadding?: number;
|
2023-07-24 16:51:53 +02:00
|
|
|
renderEmbeddables?: boolean;
|
2024-07-25 18:55:55 +02:00
|
|
|
skipInliningFonts?: true;
|
2021-07-02 22:37:01 +02:00
|
|
|
}): Promise<SVGSVGElement> => {
|
2021-03-16 17:51:56 +01:00
|
|
|
const { elements: restoredElements, appState: restoredAppState } = restore(
|
|
|
|
{ elements, appState },
|
|
|
|
null,
|
2021-07-04 22:23:35 +02:00
|
|
|
null,
|
2021-03-16 17:51:56 +01:00
|
|
|
);
|
2023-04-16 11:56:25 +02:00
|
|
|
|
|
|
|
const exportAppState = {
|
|
|
|
...restoredAppState,
|
|
|
|
exportPadding,
|
|
|
|
};
|
|
|
|
|
2023-11-09 17:00:21 +01:00
|
|
|
return _exportToSvg(restoredElements, exportAppState, files, {
|
|
|
|
exportingFrame,
|
|
|
|
renderEmbeddables,
|
2024-07-25 18:55:55 +02:00
|
|
|
skipInliningFonts,
|
2023-11-09 17:00:21 +01:00
|
|
|
});
|
2020-11-02 20:14:20 +01:00
|
|
|
};
|
2021-05-12 00:24:41 +02:00
|
|
|
|
2022-04-29 15:28:44 +02:00
|
|
|
export const exportToClipboard = async (
|
|
|
|
opts: ExportOpts & {
|
|
|
|
mimeType?: string;
|
|
|
|
quality?: number;
|
|
|
|
type: "png" | "svg" | "json";
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
if (opts.type === "svg") {
|
|
|
|
const svg = await exportToSvg(opts);
|
|
|
|
await copyTextToSystemClipboard(svg.outerHTML);
|
|
|
|
} else if (opts.type === "png") {
|
|
|
|
await copyBlobToClipboardAsPng(exportToBlob(opts));
|
|
|
|
} else if (opts.type === "json") {
|
2023-04-24 10:26:21 +02:00
|
|
|
await copyToClipboard(opts.elements, opts.files);
|
2022-04-29 15:28:44 +02:00
|
|
|
} else {
|
|
|
|
throw new Error("Invalid export type");
|
|
|
|
}
|
|
|
|
};
|