Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

feat(canvas): add copy layer to clipboard

joshistoasta3e2383b6f4a
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx
index b5498c1057..7df6c5e8c0 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx
@@ -36,6 +36,7 @@ import { useTranslation } from 'react-i18next';
import type { LayerMoveKind } from './layerGroups';
import { getLayerContextActions, type LayerContextAction, type LayerContextActionId } from './layerContextActions';
+import { copyBlobToClipboard } from './layerExportActions';
import { reorderWithinGroupByKind } from './layerGroups';
import { resolveMenuTargetForRender } from './layerMenuState';
import {
@@ -322,6 +323,14 @@ const LayerMenu = ({
await uploadGalleryImage(file, 'none');
}, [engine, layer.id]);
+ const handleCopyToClipboard = useCallback(async () => {
+ const result = await engine?.exportBakedLayerBlob(layer.id, { includeDisabled: true });
+ if (!result || result.status !== 'ok') {
+ return;
+ }
+ await copyBlobToClipboard(result.blob);
+ }, [engine, layer.id]);
+
const handleLayerConfigAction = useCallback(
(id: LayerContextActionId) => {
if (id === 'control-transparency-effect' && layer.type === 'control') {
@@ -380,6 +389,9 @@ const LayerMenu = ({
case 'save-to-assets':
void handleSaveToAssets();
break;
+ case 'copy-to-clipboard':
+ void handleCopyToClipboard();
+ break;
case 'rasterize':
handleRasterize();
break;
@@ -409,6 +421,7 @@ const LayerMenu = ({
[
handleConvertToControl,
handleConvertToRaster,
+ handleCopyToClipboard,
handleDelete,
handleDuplicate,
handleFitToBbox,
@@ -593,6 +606,7 @@ const LAYER_ACTION_GROUPS: readonly LayerContextAction['group'][] = [
const LAYER_ACTION_ICONS: Record<LayerContextActionId, LucideIcon> = {
'control-transparency-effect': SlidersHorizontalIcon,
+ 'copy-to-clipboard': CopyIcon,
'convert-to-control': SlidersHorizontalIcon,
'convert-to-raster': ImageIcon,
delete: Trash2Icon,
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextActions.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextActions.test.ts
index 5a9a452b39..ae170a6efa 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextActions.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextActions.test.ts
@@ -28,6 +28,7 @@ describe('getLayerContextActions', () => {
'transform',
'fit-to-bbox',
'save-to-assets',
+ 'copy-to-clipboard',
'merge-down',
'delete',
])
@@ -81,6 +82,7 @@ describe('getLayerContextActions', () => {
expect(actions.find((action) => action.id === 'transform')?.isDisabled).toBe(true);
expect(actions.find((action) => action.id === 'save-to-assets')?.isDisabled).toBe(true);
+ expect(actions.find((action) => action.id === 'copy-to-clipboard')?.isDisabled).toBe(true);
expect(actions.find((action) => action.id === 'merge-down')?.isDisabled).toBe(true);
});
});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextActions.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextActions.ts
index 326fea2311..a40727275f 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextActions.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerContextActions.ts
@@ -13,6 +13,7 @@ export type LayerContextActionId =
| 'transform'
| 'fit-to-bbox'
| 'save-to-assets'
+ | 'copy-to-clipboard'
| 'rasterize'
| 'convert-to-control'
| 'convert-to-raster'
@@ -117,6 +118,13 @@ export const LAYER_CONTEXT_ACTION_DEFINITIONS: readonly LayerContextActionDefini
isDisabled: (ctx) => !ctx.hasEngine,
labelKey: 'widgets.layers.actions.saveLayerToAssets',
},
+ {
+ defaultLabel: 'Copy layer to clipboard',
+ group: 'edit',
+ id: 'copy-to-clipboard',
+ isDisabled: (ctx) => !ctx.hasEngine,
+ labelKey: 'widgets.layers.actions.copyLayerToClipboard',
+ },
{
defaultLabel: 'Rasterize',
group: 'convert',
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.test.ts
new file mode 100644
index 0000000000..485e8ea25b
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import { copyBlobToClipboard } from './layerExportActions';
+
+describe('copyBlobToClipboard', () => {
+ it('writes a ClipboardItem using the blob MIME type', async () => {
+ const write = vi.fn(() => Promise.resolve());
+ const created: Record<string, Blob>[] = [];
+ class TestClipboardItem {
+ presentationStyle: PresentationStyle = 'unspecified';
+ types: string[] = [];
+ constructor(items: Record<string, Blob>) {
+ created.push(items);
+ this.types = Object.keys(items);
+ }
+ getType(type: string): Promise<Blob> {
+ return Promise.resolve(created.at(-1)?.[type] ?? new Blob());
+ }
+ }
+ const blob = new Blob(['pixels'], { type: 'image/png' });
+
+ await copyBlobToClipboard(blob, { ClipboardItemCtor: TestClipboardItem, clipboard: { write } });
+
+ expect(created).toHaveLength(1);
+ expect(created[0]?.['image/png']).toBe(blob);
+ expect(write).toHaveBeenCalledWith([expect.any(TestClipboardItem)]);
+ });
+
+ it('fails when the clipboard API is unavailable', async () => {
+ await expect(copyBlobToClipboard(new Blob(['pixels']))).rejects.toThrow('Clipboard image copy is unavailable');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.ts
new file mode 100644
index 0000000000..259d904af0
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.ts
@@ -0,0 +1,17 @@
+type ClipboardLike = { write(items: ClipboardItem[]): Promise<void> };
+type ClipboardItemCtor = new (items: Record<string, Blob>) => ClipboardItem;
+
+export interface CopyBlobToClipboardDeps {
+ clipboard?: ClipboardLike;
+ ClipboardItemCtor?: ClipboardItemCtor;
+}
+
+export const copyBlobToClipboard = async (blob: Blob, deps: CopyBlobToClipboardDeps = {}): Promise<void> => {
+ const clipboard = deps.clipboard ?? globalThis.navigator?.clipboard;
+ const ClipboardItemImpl = deps.ClipboardItemCtor ?? globalThis.ClipboardItem;
+ if (!clipboard || !ClipboardItemImpl) {
+ throw new Error('Clipboard image copy is unavailable');
+ }
+ const type = blob.type || 'image/png';
+ await clipboard.write([new ClipboardItemImpl({ [type]: blob })]);
+};