Fix "copy as png" in firefox when dom.events.asyncClipboard.clipboardItem
is enabled (#1342)
Fixes the issue outlined in #1325. ### Test Plan In firefox 1. Enable clipboard in firefox by going to `about:config` and enabling `dom.events.asyncClipboard.clipboardItem` (remember to turn it off afterwards) 2. Test that `copy-as -> PNG` copies the image In other browsers 1. Test that `copy-as -> PNG` still copies the image ### Release Notes - Fix "copy as png" in firefox when `dom.events.asyncClipboard.clipboardItem` is enabled
This commit is contained in:
parent
9d5f3fb2fd
commit
88e6f441b1
1 changed files with 38 additions and 20 deletions
|
@ -47,11 +47,7 @@ export function useCopyAs() {
|
|||
case 'jpeg':
|
||||
case 'png': {
|
||||
const mimeType = format === 'jpeg' ? 'image/jpeg' : 'image/png'
|
||||
|
||||
window.navigator.clipboard.write([
|
||||
new ClipboardItem({
|
||||
// Note: This needs to use the promise based approach for safari/ios to not bail on a permissions error.
|
||||
[mimeType]: getExportedImageBlob(app, ids, format).then((blob) => {
|
||||
const blobPromise = getExportedImageBlob(app, ids, format).then((blob) => {
|
||||
if (blob) {
|
||||
if (window.navigator.clipboard) {
|
||||
return blob
|
||||
|
@ -66,9 +62,31 @@ export function useCopyAs() {
|
|||
})
|
||||
throw new Error('Copy not possible')
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
window.navigator.clipboard
|
||||
.write([
|
||||
new ClipboardItem({
|
||||
// Note: This needs to use the promise based approach for safari/ios to not bail on a permissions error.
|
||||
[mimeType]: blobPromise,
|
||||
}),
|
||||
])
|
||||
.catch((err: any) => {
|
||||
// Firefox will fail with the above if `dom.events.asyncClipboard.clipboardItem` is enabled.
|
||||
// See <https://github.com/tldraw/tldraw/issues/1325>
|
||||
if (!err.toString().match(/^TypeError: DOMString not supported/)) {
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
blobPromise.then((blob) => {
|
||||
window.navigator.clipboard.write([
|
||||
new ClipboardItem({
|
||||
// Note: This needs to use the promise based approach for safari/ios to not bail on a permissions error.
|
||||
[mimeType]: blob,
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
break
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue