tldraw
Version:
A tiny little drawing editor.
47 lines (46 loc) • 1.27 kB
JavaScript
import {
FileHelpers,
exhaustiveSwitchError
} from "@tldraw/editor";
async function getSvgString(editor, ids, opts) {
const svg = await editor.getSvgString(ids, opts);
if (!svg) {
throw new Error("Could not construct SVG.");
}
return svg;
}
async function exportToString(editor, ids, format, opts = {}) {
switch (format) {
case "svg": {
return (await getSvgString(editor, ids, opts))?.svg;
}
case "json": {
const data = await editor.resolveAssetsInContent(editor.getContentFromCurrentPage(ids));
return JSON.stringify(data);
}
default: {
exhaustiveSwitchError(format);
}
}
}
const clipboardMimeTypesByFormat = {
jpeg: "image/jpeg",
png: "image/png",
webp: "image/webp",
svg: "text/plain"
};
function exportToImagePromiseForClipboard(editor, ids, opts = {}) {
const idsToUse = ids?.length ? ids : [...editor.getCurrentPageShapeIds()];
const format = opts.format ?? "png";
return {
blobPromise: editor.toImage(idsToUse, opts).then(
(result) => FileHelpers.rewriteMimeType(result.blob, clipboardMimeTypesByFormat[format])
),
mimeType: clipboardMimeTypesByFormat[format]
};
}
export {
exportToImagePromiseForClipboard,
exportToString
};
//# sourceMappingURL=export.mjs.map