|
@@ -10,6 +10,11 @@ import { restore } from "../data/restore";
|
|
|
import { MIME_TYPES } from "../constants";
|
|
|
import { encodePngMetadata } from "../data/image";
|
|
|
import { serializeAsJSON } from "../data/json";
|
|
|
+import {
|
|
|
+ copyBlobToClipboardAsPng,
|
|
|
+ copyTextToSystemClipboard,
|
|
|
+ copyToClipboard,
|
|
|
+} from "../clipboard";
|
|
|
|
|
|
type ExportOpts = {
|
|
|
elements: readonly NonDeleted<ExcalidrawElement>[];
|
|
@@ -81,7 +86,7 @@ export const exportToBlob = async (
|
|
|
mimeType?: string;
|
|
|
quality?: number;
|
|
|
},
|
|
|
-): Promise<Blob | null> => {
|
|
|
+): Promise<Blob> => {
|
|
|
let { mimeType = MIME_TYPES.png, quality } = opts;
|
|
|
|
|
|
if (mimeType === MIME_TYPES.png && typeof quality === "number") {
|
|
@@ -107,9 +112,12 @@ export const exportToBlob = async (
|
|
|
|
|
|
quality = quality ? quality : /image\/jpe?g/.test(mimeType) ? 0.92 : 0.8;
|
|
|
|
|
|
- return new Promise((resolve) => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
canvas.toBlob(
|
|
|
- async (blob: Blob | null) => {
|
|
|
+ async (blob) => {
|
|
|
+ if (!blob) {
|
|
|
+ return reject(new Error("couldn't export to blob"));
|
|
|
+ }
|
|
|
if (
|
|
|
blob &&
|
|
|
mimeType === MIME_TYPES.png &&
|
|
@@ -156,6 +164,33 @@ export const exportToSvg = async ({
|
|
|
);
|
|
|
};
|
|
|
|
|
|
+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") {
|
|
|
+ const appState = {
|
|
|
+ offsetTop: 0,
|
|
|
+ offsetLeft: 0,
|
|
|
+ width: 0,
|
|
|
+ height: 0,
|
|
|
+ ...getDefaultAppState(),
|
|
|
+ ...opts.appState,
|
|
|
+ };
|
|
|
+ await copyToClipboard(opts.elements, appState, opts.files);
|
|
|
+ } else {
|
|
|
+ throw new Error("Invalid export type");
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
export { serializeAsJSON, serializeLibraryAsJSON } from "../data/json";
|
|
|
export { loadFromBlob, loadLibraryFromBlob } from "../data/blob";
|
|
|
export { getFreeDrawSvgPath } from "../renderer/renderElement";
|