109 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-11-02 20:14:20 +01:00
import {
exportToCanvas as _exportToCanvas,
exportToSvg as _exportToSvg,
} from "../scene/export";
import { getDefaultAppState } from "../appState";
import { AppState, BinaryFiles } from "../types";
2020-11-02 20:14:20 +01:00
import { ExcalidrawElement } from "../element/types";
import { getNonDeletedElements } from "../element";
import { restore } from "../data/restore";
import { MIME_TYPES } from "../constants";
2020-11-02 20:14:20 +01:00
type ExportOpts = {
elements: readonly ExcalidrawElement[];
appState?: Partial<Omit<AppState, "offsetTop" | "offsetLeft">>;
files: BinaryFiles | null;
getDimensions?: (
2020-11-02 20:14:20 +01:00
width: number,
height: number,
) => { width: number; height: number; scale: number };
};
export const exportToCanvas = ({
2020-11-02 20:14:20 +01:00
elements,
appState,
files,
2020-11-02 20:14:20 +01:00
getDimensions = (width, height) => ({ width, height, scale: 1 }),
}: ExportOpts) => {
const { elements: restoredElements, appState: restoredAppState } = restore(
{ elements, appState },
null,
null,
);
const { exportBackground, viewBackgroundColor } = restoredAppState;
2020-11-02 20:14:20 +01:00
return _exportToCanvas(
getNonDeletedElements(restoredElements),
{ ...restoredAppState, offsetTop: 0, offsetLeft: 0, width: 0, height: 0 },
files || {},
{ exportBackground, viewBackgroundColor },
2020-11-02 20:14:20 +01:00
(width: number, height: number) => {
const canvas = document.createElement("canvas");
const ret = getDimensions(width, height);
canvas.width = ret.width;
canvas.height = ret.height;
return { canvas, scale: ret.scale };
2020-11-02 20:14:20 +01:00
},
);
};
export const exportToBlob = async (
2020-11-02 20:14:20 +01:00
opts: ExportOpts & {
mimeType?: string;
quality?: number;
},
): Promise<Blob | null> => {
const canvas = await exportToCanvas(opts);
2020-11-02 20:14:20 +01:00
let { mimeType = MIME_TYPES.png, quality } = opts;
2020-11-02 20:14:20 +01: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
}
// typo in MIME type (should be "jpeg")
2020-11-02 20:14:20 +01:00
if (mimeType === "image/jpg") {
mimeType = MIME_TYPES.jpg;
2020-11-02 20:14:20 +01:00
}
quality = quality ? quality : /image\/jpe?g/.test(mimeType) ? 0.92 : 0.8;
return new Promise((resolve) => {
canvas.toBlob(
(blob: Blob | null) => {
resolve(blob);
},
mimeType,
quality,
);
});
};
export const exportToSvg = async ({
2020-11-02 20:14:20 +01:00
elements,
appState = getDefaultAppState(),
files = {},
2020-11-02 20:14:20 +01:00
exportPadding,
}: Omit<ExportOpts, "getDimensions"> & {
2020-11-02 20:14:20 +01:00
exportPadding?: number;
}): Promise<SVGSVGElement> => {
const { elements: restoredElements, appState: restoredAppState } = restore(
{ elements, appState },
null,
null,
);
return _exportToSvg(
getNonDeletedElements(restoredElements),
{
...restoredAppState,
exportPadding,
},
files,
);
2020-11-02 20:14:20 +01:00
};
export { serializeAsJSON } from "../data/json";
export { loadFromBlob, loadLibraryFromBlob } from "../data/blob";
export { getFreeDrawSvgPath } from "../renderer/renderElement";