Skip to content

Commit

Permalink
fix(ui): passing Promise into ClipboardItem to make it work in Safari
Browse files Browse the repository at this point in the history
throwing Error in getBaseLayerBlob, instead of returning nil
using copyBlobToClipboard for both Canvas and Text2Image clipboard functionality
  • Loading branch information
parnas authored and psychedelicious committed Sep 21, 2023
1 parent 5aefa49 commit aa82f93
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { startAppListening } from '..';
import { $logger } from 'app/logging/logger';
import { getBaseLayerBlob } from 'features/canvas/util/getBaseLayerBlob';
import { addToast } from 'features/system/store/systemSlice';
import { copyBlobToClipboard } from 'features/canvas/util/copyBlobToClipboard';
import { copyBlobToClipboard } from 'features/system/util/copyBlobToClipboard';
import { t } from 'i18next';

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

const blob = await getBaseLayerBlob(state);
try {
const blob = getBaseLayerBlob(state);

if (!blob) {
moduleLog.error('Problem getting base layer blob');
copyBlobToClipboard(blob);
} catch (err) {
moduleLog.error(String(err));
dispatch(
addToast({
title: t('toast.problemCopyingCanvas'),
Expand All @@ -29,8 +31,6 @@ export const addCanvasCopiedToClipboardListener = () => {
return;
}

copyBlobToClipboard(blob);

dispatch(
addToast({
title: t('toast.canvasCopiedClipboard'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export const addCanvasDownloadedAsImageListener = () => {
.child({ namespace: 'canvasSavedToGalleryListener' });
const state = getState();

const blob = await getBaseLayerBlob(state);

if (!blob) {
moduleLog.error('Problem getting base layer blob');
let blob;
try {
blob = await getBaseLayerBlob(state);
} catch (err) {
moduleLog.error(String(err));
dispatch(
addToast({
title: t('toast.problemDownloadingCanvas'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export const addCanvasImageToControlNetListener = () => {
const log = logger('canvas');
const state = getState();

const blob = await getBaseLayerBlob(state);

if (!blob) {
log.error('Problem getting base layer blob');
let blob;
try {
blob = await getBaseLayerBlob(state);
} catch (err) {
log.error(String(err));
dispatch(
addToast({
title: t('toast.problemSavingCanvas'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ export const addCanvasSavedToGalleryListener = () => {
const log = logger('canvas');
const state = getState();

const blob = await getBaseLayerBlob(state);

if (!blob) {
log.error('Problem getting base layer blob');
let blob;
try {
blob = await getBaseLayerBlob(state);
} catch (err) {
log.error(String(err));
dispatch(
addToast({
title: t('toast.problemSavingCanvas'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const getBaseLayerBlob = async (state: RootState) => {
const canvasBaseLayer = getCanvasBaseLayer();

if (!canvasBaseLayer) {
return;
throw new Error('Problem getting base layer blob');
}

const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/**
* Copies a blob to the clipboard by calling navigator.clipboard.write().
*/
export const copyBlobToClipboard = (blob: Blob) => {
export const copyBlobToClipboard = (
blob: Promise<Blob>,
type = 'image/png'
) => {
navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob,
[type]: blob,
}),
]);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useAppToaster } from 'app/components/Toaster';
import { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { copyBlobToClipboard } from 'features/system/util/copyBlobToClipboard';

export const useCopyImageToClipboard = () => {
const toaster = useAppToaster();
Expand All @@ -22,13 +23,13 @@ export const useCopyImageToClipboard = () => {
});
}
try {
const response = await fetch(image_url);
const blob = await response.blob();
await navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob,
}),
]);
const getImageBlob = async () => {
const response = await fetch(image_url);
return await response.blob();
};

copyBlobToClipboard(getImageBlob());

toaster({
title: t('toast.imageCopied'),
status: 'success',
Expand Down

0 comments on commit aa82f93

Please # to comment.