Browse Source

fix: ensure `ClipboardItem` created in the same tick to fix safari (#7066)

David Luzar 1 year ago
parent
commit
5b94cffc74
2 changed files with 7 additions and 10 deletions
  1. 6 3
      src/data/blob.ts
  2. 1 7
      src/data/index.ts

+ 6 - 3
src/data/blob.ts

@@ -8,7 +8,7 @@ import { t } from "../i18n";
 import { calculateScrollCenter } from "../scene";
 import { calculateScrollCenter } from "../scene";
 import { AppState, DataURL, LibraryItem } from "../types";
 import { AppState, DataURL, LibraryItem } from "../types";
 import { ValueOf } from "../utility-types";
 import { ValueOf } from "../utility-types";
-import { bytesToHexString } from "../utils";
+import { bytesToHexString, isPromiseLike } from "../utils";
 import { FileSystemHandle, nativeFileSystemSupported } from "./filesystem";
 import { FileSystemHandle, nativeFileSystemSupported } from "./filesystem";
 import { isValidExcalidrawData, isValidLibrary } from "./json";
 import { isValidExcalidrawData, isValidLibrary } from "./json";
 import { restore, restoreLibraryItems } from "./restore";
 import { restore, restoreLibraryItems } from "./restore";
@@ -207,10 +207,13 @@ export const loadLibraryFromBlob = async (
 };
 };
 
 
 export const canvasToBlob = async (
 export const canvasToBlob = async (
-  canvas: HTMLCanvasElement,
+  canvas: HTMLCanvasElement | Promise<HTMLCanvasElement>,
 ): Promise<Blob> => {
 ): Promise<Blob> => {
-  return new Promise((resolve, reject) => {
+  return new Promise(async (resolve, reject) => {
     try {
     try {
+      if (isPromiseLike(canvas)) {
+        canvas = await canvas;
+      }
       canvas.toBlob((blob) => {
       canvas.toBlob((blob) => {
         if (!blob) {
         if (!blob) {
           return reject(
           return reject(

+ 1 - 7
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,
     exportBackground,
     viewBackgroundColor,
     viewBackgroundColor,
     exportPadding,
     exportPadding,
   });
   });
-  tempCanvas.style.display = "none";
-  document.body.appendChild(tempCanvas);
 
 
   if (type === "png") {
   if (type === "png") {
     let blob = await canvasToBlob(tempCanvas);
     let blob = await canvasToBlob(tempCanvas);
-    tempCanvas.remove();
     if (appState.exportEmbedScene) {
     if (appState.exportEmbedScene) {
       blob = await (
       blob = await (
         await import(/* webpackChunkName: "image" */ "./image")
         await import(/* webpackChunkName: "image" */ "./image")
@@ -114,11 +111,8 @@ export const exportCanvas = async (
       } else {
       } else {
         throw new Error(t("alerts.couldNotCopyToClipboard"));
         throw new Error(t("alerts.couldNotCopyToClipboard"));
       }
       }
-    } finally {
-      tempCanvas.remove();
     }
     }
   } else {
   } else {
-    tempCanvas.remove();
     // shouldn't happen
     // shouldn't happen
     throw new Error("Unsupported export type");
     throw new Error("Unsupported export type");
   }
   }