Skip to content

Commit

Permalink
fix: ensure ClipboardItem created in the same tick to fix safari (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle authored Oct 16, 2023
1 parent aaf73c8 commit 5b94cff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/data/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -207,10 +207,13 @@ export const loadLibraryFromBlob = async (
};

export const canvasToBlob = async (
canvas: HTMLCanvasElement,
canvas: HTMLCanvasElement | Promise<HTMLCanvasElement>,
): Promise<Blob> => {
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(
Expand Down
8 changes: 1 addition & 7 deletions src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 5b94cff

Please sign in to comment.