Skip to content

Commit aa82f93

Browse files
parnaspsychedelicious
authored andcommitted
fix(ui): passing Promise into ClipboardItem to make it work in Safari
throwing Error in getBaseLayerBlob, instead of returning nil using copyBlobToClipboard for both Canvas and Text2Image clipboard functionality
1 parent 5aefa49 commit aa82f93

File tree

7 files changed

+35
-28
lines changed

7 files changed

+35
-28
lines changed

invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/canvasCopiedToClipboard.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { startAppListening } from '..';
33
import { $logger } from 'app/logging/logger';
44
import { getBaseLayerBlob } from 'features/canvas/util/getBaseLayerBlob';
55
import { addToast } from 'features/system/store/systemSlice';
6-
import { copyBlobToClipboard } from 'features/canvas/util/copyBlobToClipboard';
6+
import { copyBlobToClipboard } from 'features/system/util/copyBlobToClipboard';
77
import { t } from 'i18next';
88

99
export const addCanvasCopiedToClipboardListener = () => {
@@ -15,10 +15,12 @@ export const addCanvasCopiedToClipboardListener = () => {
1515
.child({ namespace: 'canvasCopiedToClipboardListener' });
1616
const state = getState();
1717

18-
const blob = await getBaseLayerBlob(state);
18+
try {
19+
const blob = getBaseLayerBlob(state);
1920

20-
if (!blob) {
21-
moduleLog.error('Problem getting base layer blob');
21+
copyBlobToClipboard(blob);
22+
} catch (err) {
23+
moduleLog.error(String(err));
2224
dispatch(
2325
addToast({
2426
title: t('toast.problemCopyingCanvas'),
@@ -29,8 +31,6 @@ export const addCanvasCopiedToClipboardListener = () => {
2931
return;
3032
}
3133

32-
copyBlobToClipboard(blob);
33-
3434
dispatch(
3535
addToast({
3636
title: t('toast.canvasCopiedClipboard'),

invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/canvasDownloadedAsImage.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ export const addCanvasDownloadedAsImageListener = () => {
1515
.child({ namespace: 'canvasSavedToGalleryListener' });
1616
const state = getState();
1717

18-
const blob = await getBaseLayerBlob(state);
19-
20-
if (!blob) {
21-
moduleLog.error('Problem getting base layer blob');
18+
let blob;
19+
try {
20+
blob = await getBaseLayerBlob(state);
21+
} catch (err) {
22+
moduleLog.error(String(err));
2223
dispatch(
2324
addToast({
2425
title: t('toast.problemDownloadingCanvas'),

invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/canvasImageToControlNet.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ export const addCanvasImageToControlNetListener = () => {
1414
const log = logger('canvas');
1515
const state = getState();
1616

17-
const blob = await getBaseLayerBlob(state);
18-
19-
if (!blob) {
20-
log.error('Problem getting base layer blob');
17+
let blob;
18+
try {
19+
blob = await getBaseLayerBlob(state);
20+
} catch (err) {
21+
log.error(String(err));
2122
dispatch(
2223
addToast({
2324
title: t('toast.problemSavingCanvas'),

invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/canvasSavedToGallery.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ export const addCanvasSavedToGalleryListener = () => {
1313
const log = logger('canvas');
1414
const state = getState();
1515

16-
const blob = await getBaseLayerBlob(state);
17-
18-
if (!blob) {
19-
log.error('Problem getting base layer blob');
16+
let blob;
17+
try {
18+
blob = await getBaseLayerBlob(state);
19+
} catch (err) {
20+
log.error(String(err));
2021
dispatch(
2122
addToast({
2223
title: t('toast.problemSavingCanvas'),

invokeai/frontend/web/src/features/canvas/util/getBaseLayerBlob.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const getBaseLayerBlob = async (state: RootState) => {
99
const canvasBaseLayer = getCanvasBaseLayer();
1010

1111
if (!canvasBaseLayer) {
12-
return;
12+
throw new Error('Problem getting base layer blob');
1313
}
1414

1515
const {

invokeai/frontend/web/src/features/canvas/util/copyBlobToClipboard.ts renamed to invokeai/frontend/web/src/features/system/util/copyBlobToClipboard.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
/**
22
* Copies a blob to the clipboard by calling navigator.clipboard.write().
33
*/
4-
export const copyBlobToClipboard = (blob: Blob) => {
4+
export const copyBlobToClipboard = (
5+
blob: Promise<Blob>,
6+
type = 'image/png'
7+
) => {
58
navigator.clipboard.write([
69
new ClipboardItem({
7-
[blob.type]: blob,
10+
[type]: blob,
811
}),
912
]);
1013
};

invokeai/frontend/web/src/features/ui/hooks/useCopyImageToClipboard.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useAppToaster } from 'app/components/Toaster';
22
import { useCallback, useMemo } from 'react';
33
import { useTranslation } from 'react-i18next';
4+
import { copyBlobToClipboard } from 'features/system/util/copyBlobToClipboard';
45

56
export const useCopyImageToClipboard = () => {
67
const toaster = useAppToaster();
@@ -22,13 +23,13 @@ export const useCopyImageToClipboard = () => {
2223
});
2324
}
2425
try {
25-
const response = await fetch(image_url);
26-
const blob = await response.blob();
27-
await navigator.clipboard.write([
28-
new ClipboardItem({
29-
[blob.type]: blob,
30-
}),
31-
]);
26+
const getImageBlob = async () => {
27+
const response = await fetch(image_url);
28+
return await response.blob();
29+
};
30+
31+
copyBlobToClipboard(getImageBlob());
32+
3233
toaster({
3334
title: t('toast.imageCopied'),
3435
status: 'success',

0 commit comments

Comments
 (0)