From 5b94cffc74668c59dbc00c11b780774c6b97e7a4 Mon Sep 17 00:00:00 2001 From: David Luzar Date: Mon, 16 Oct 2023 11:38:57 +0200 Subject: [PATCH] fix: ensure `ClipboardItem` created in the same tick to fix safari (#7066) --- src/data/blob.ts | 9 ++++++--- src/data/index.ts | 8 +------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/data/blob.ts b/src/data/blob.ts index 92240030..cb2c0519 100644 --- a/src/data/blob.ts +++ b/src/data/blob.ts @@ -8,7 +8,7 @@ import { t } from "../i18n"; import { calculateScrollCenter } from "../scene"; import { AppState, DataURL, LibraryItem } from "../types"; import { ValueOf } from "../utility-types"; -import { bytesToHexString } from "../utils"; +import { bytesToHexString, isPromiseLike } from "../utils"; import { FileSystemHandle, nativeFileSystemSupported } from "./filesystem"; import { isValidExcalidrawData, isValidLibrary } from "./json"; import { restore, restoreLibraryItems } from "./restore"; @@ -207,10 +207,13 @@ export const loadLibraryFromBlob = async ( }; export const canvasToBlob = async ( - canvas: HTMLCanvasElement, + canvas: HTMLCanvasElement | Promise, ): Promise => { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { try { + if (isPromiseLike(canvas)) { + canvas = await canvas; + } canvas.toBlob((blob) => { if (!blob) { return reject( diff --git a/src/data/index.ts b/src/data/index.ts index 20ba75eb..29eedb2a 100644 --- a/src/data/index.ts +++ b/src/data/index.ts @@ -66,17 +66,14 @@ export const exportCanvas = async ( } } - const tempCanvas = await exportToCanvas(elements, appState, files, { + const tempCanvas = exportToCanvas(elements, appState, files, { exportBackground, viewBackgroundColor, exportPadding, }); - tempCanvas.style.display = "none"; - document.body.appendChild(tempCanvas); if (type === "png") { let blob = await canvasToBlob(tempCanvas); - tempCanvas.remove(); if (appState.exportEmbedScene) { blob = await ( await import(/* webpackChunkName: "image" */ "./image") @@ -114,11 +111,8 @@ export const exportCanvas = async ( } else { throw new Error(t("alerts.couldNotCopyToClipboard")); } - } finally { - tempCanvas.remove(); } } else { - tempCanvas.remove(); // shouldn't happen throw new Error("Unsupported export type"); }