fix(web): only copy images via canvas (#22225)

This commit is contained in:
Jason Rasmussen 2025-09-21 15:34:10 -04:00 committed by GitHub
parent afc4085b55
commit 0bbeb20595
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 8 additions and 34 deletions

View file

@ -620,26 +620,7 @@ const imgToBlob = async (imageElement: HTMLImageElement) => {
throw new Error('Canvas context is null');
};
const urlToBlob = async (imageSource: string) => {
const response = await fetch(imageSource);
return await response.blob();
};
export const copyImageToClipboard = async (
source: HTMLImageElement | string,
): Promise<{ success: true } | { success: false; mimeType: string }> => {
if (source instanceof HTMLImageElement) {
// do not await, so the Safari clipboard write happens in the context of the user gesture
await navigator.clipboard.write([new ClipboardItem({ ['image/png']: imgToBlob(source) })]);
return { success: true };
}
// if we had a way to get the mime type synchronously, we could do the same thing here
const blob = await urlToBlob(source);
if (!ClipboardItem.supports(blob.type)) {
return { success: false, mimeType: blob.type };
}
await navigator.clipboard.write([new ClipboardItem({ [blob.type]: blob })]);
return { success: true };
export const copyImageToClipboard = async (source: HTMLImageElement) => {
// do not await, so the Safari clipboard write happens in the context of the user gesture
await navigator.clipboard.write([new ClipboardItem({ ['image/png']: imgToBlob(source) })]);
};