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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(webv2): constrain gallery image board drops
joshistoast37aac1aac93b
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryBoardSelect.tsx b/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryBoardSelect.tsxindex 626202144e..b380e09dd2 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryBoardSelect.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryBoardSelect.tsx@@ -14,6 +14,7 @@ import {Stack,Text,} from '@chakra-ui/react';+import { useDndContext, useDndMonitor, useDroppable, type DragEndEvent } from '@dnd-kit/core';import { Button, CloseButton, IconButton } from '@workbench/components/ui';import {ArchiveIcon,@@ -32,15 +33,25 @@ import {import { useRef, useState, type MouseEvent } from 'react';import { GalleryBoardMenu, type GalleryBoardMenuTarget } from './GalleryBoardMenu';+import {+ getGalleryBoardDropData,+ getGalleryBoardDropId,+ getGalleryImageNamesOutsideBoard,+ isGalleryBoardDropData,+ isGalleryImageDragData,+} from './galleryDnd';import { getBoardCounts } from './galleryStateView';import { useGalleryWidget } from './GalleryWidgetContext';export const GalleryBoardSelect = () => {- const { actions, gallery, projectName } = useGalleryWidget();+ const { actions, gallery, imageActions, projectName } = useGalleryWidget();+ const { active } = useDndContext();const [isOpen, setIsOpen] = useState(false);const [boardSearchTerm, setBoardSearchTerm] = useState('');const [boardMenuTarget, setBoardMenuTarget] = useState<GalleryBoardMenuTarget | null>(null);const boardMenuActiveRef = useRef(false);+ const dragOpenedMenuRef = useRef(false);+ const isGalleryImageDragActive = isGalleryImageDragData(active?.data.current);const trimmedSearchTerm = boardSearchTerm.trim();const normalizedSearchTerm = trimmedSearchTerm.toLowerCase();const matchesSearch = (name: string) => !normalizedSearchTerm || name.toLowerCase().includes(normalizedSearchTerm);@@ -68,6 +79,44 @@ export const GalleryBoardSelect = () => {setBoardSearchTerm('');};+ const handleDragEnd = (event: DragEndEvent) => {+ const dragData = event.active.data.current;+ const dropData = event.over?.data.current;++ if (isGalleryImageDragData(dragData) && isGalleryBoardDropData(dropData) && dropData.boardKind === 'board') {+ const imageNames = getGalleryImageNamesOutsideBoard(dragData, dropData.boardId);++ if (imageNames.length > 0) {+ void imageActions.moveImagesToBoard(imageNames, dropData.boardId);+ }+ }++ if (dragOpenedMenuRef.current) {+ dragOpenedMenuRef.current = false;+ closeAndReset();+ }+ };++ useDndMonitor({+ onDragCancel: () => {+ if (dragOpenedMenuRef.current) {+ dragOpenedMenuRef.current = false;+ closeAndReset();+ }+ },+ onDragEnd: handleDragEnd,+ onDragStart: (event) => {+ if (!isGalleryImageDragData(event.active.data.current)) {+ return;+ }++ dragOpenedMenuRef.current = true;+ setBoardMenuTarget(null);+ setBoardSearchTerm('');+ setIsOpen(true);+ },+ });+const createBoardFromSearch = () => {if (!canCreateFromSearch) {return;@@ -105,7 +154,7 @@ export const GalleryBoardSelect = () => {// Interactions with the board actions menu (or its dialogs) land// outside this dropdown; swallow the resulting close request so the// user returns to the still-open board list.- if (boardMenuActiveRef.current) {+ if (boardMenuActiveRef.current || isGalleryImageDragActive) {return;}@@ -343,47 +392,65 @@ const BoardRow = ({isSelected: boolean;onOpenMenu?: (x: number, y: number) => void;onSelect: () => void;-}) => (- <Menu.Item- css={onOpenMenu ? { '&:hover .board-row-actions': { opacity: 1 } } : undefined}- value={board.id}- onClick={onSelect}- onContextMenu={- onOpenMenu- ? (event: MouseEvent) => {+}) => {+ const { active } = useDndContext();+ const dragData = active?.data.current;+ const canDropImages =+ board.kind === 'board' &&+ isGalleryImageDragData(dragData) &&+ getGalleryImageNamesOutsideBoard(dragData, board.id).length > 0;+ const { isOver, setNodeRef } = useDroppable({+ data: getGalleryBoardDropData(board.id, board.kind),+ disabled: !canDropImages,+ id: getGalleryBoardDropId(board.id),+ });++ return (+ <Menu.Item+ ref={setNodeRef}+ bg={isOver ? 'accent.subtle' : undefined}+ css={onOpenMenu ? { '&:hover .board-row-actions': { opacity: 1 } } : undefined}+ outline={canDropImages ? '1px dashed' : undefined}+ outlineColor={canDropImages ? 'accent.solid' : undefined}+ value={board.id}+ onClick={onSelect}+ onContextMenu={+ onOpenMenu+ ? (event: MouseEvent) => {+ event.preventDefault();+ event.stopPropagation();+ onOpenMenu(event.clientX, event.clientY);+ }+ : undefined+ }+ >+ <BoardOptionContent badge={badge} board={board} isSelected={isSelected} showOwner />+ {onOpenMenu && (+ <IconButton+ aria-label={`Board actions for ${board.name}`}+ className="board-row-actions"+ flexShrink={0}+ opacity={0}+ size="2xs"+ transition="opacity 0.15s ease"+ variant="ghost"+ onClick={(event) => {event.preventDefault();event.stopPropagation();- onOpenMenu(event.clientX, event.clientY);- }- : undefined- }- >- <BoardOptionContent badge={badge} board={board} isSelected={isSelected} showOwner />- {onOpenMenu && (- <IconButton- aria-label={`Board actions for ${board.name}`}- className="board-row-actions"- flexShrink={0}- opacity={0}- size="2xs"- transition="opacity 0.15s ease"- variant="ghost"- onClick={(event) => {- event.preventDefault();- event.stopPropagation();- const rect = event.currentTarget.getBoundingClientRect();+ const rect = event.currentTarget.getBoundingClientRect();- onOpenMenu(rect.left, rect.bottom);- }}- onPointerDown={(event) => event.stopPropagation()}- onPointerUp={(event) => event.stopPropagation()}- >- <MoreVerticalIcon />- </IconButton>- )}- </Menu.Item>-);+ onOpenMenu(rect.left, rect.bottom);+ }}+ onPointerDown={(event) => event.stopPropagation()}+ onPointerUp={(event) => event.stopPropagation()}+ >+ <MoreVerticalIcon />+ </IconButton>+ )}+ </Menu.Item>+ );+};const BoardOptionContent = ({badge,diff --git a/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryImageGrid.tsx b/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryImageGrid.tsxindex b76038db79..9cd2e6454c 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryImageGrid.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryImageGrid.tsx@@ -2,6 +2,8 @@ import type { GalleryImage } from '@workbench/gallery/api';import type { GalleryThumbnailFit } from '@workbench/gallery/settings';import { Badge, Box, Flex, ProgressCircle, ScrollArea, Skeleton, Spinner, Text } from '@chakra-ui/react';+import { useDraggable } from '@dnd-kit/core';+import { CSS } from '@dnd-kit/utilities';import { useVirtualizer } from '@tanstack/react-virtual';import { useQueueItemProgressImage } from '@workbench/backend/progressImageStore';import { useQueueItemProgress } from '@workbench/backend/progressStore';@@ -11,8 +13,10 @@ import { useActiveProjectSelector, useWorkbenchDispatch } from '@workbench/Workbimport { StarIcon, UploadIcon } from 'lucide-react';import { useEffect, useLayoutEffect, useMemo, useRef, useState, type DragEvent, type MouseEvent } from 'react';+import type { GalleryImageDragImage } from './galleryDnd';import type { GalleryQueuePlaceholder } from './galleryStateView';+import { getGalleryImageDragData, getGalleryImageDragId } from './galleryDnd';import { useGalleryWidget } from './GalleryWidgetContext';const GRID_GAP_PX = 4;@@ -195,6 +199,16 @@ export const GalleryImageGrid = ({ layout }: { layout: 'stacked' | 'wide' }) =>setContextMenuTarget({ images: [image], x, y });};+ const getDragImages = (image: GalleryImage): GalleryImageDragImage[] => {+ if (selectedNames.has(image.imageName) && selectedNames.size > 1) {+ return gallery.images+ .filter((candidate) => selectedNames.has(candidate.imageName))+ .map((candidate) => ({ boardId: candidate.boardId, imageName: candidate.imageName }));+ }++ return [{ boardId: image.boardId, imageName: image.imageName }];+ };+const navigate = (direction: 'down' | 'left' | 'right' | 'up') => {const imageCount = gallery.images.length;@@ -333,6 +347,7 @@ export const GalleryImageGrid = ({ layout }: { layout: 'stacked' | 'wide' }) =>: null}fit={thumbnailFit}+ dragImages={getDragImages(cell.image)}image={cell.image}isPrimary={cell.image.imageName === gallery.selectedImageName}isSelected={selectedNames.has(cell.image.imageName)}@@ -475,6 +490,7 @@ const GalleryPlaceholderCircularProgress = ({ percentage }: { percentage: numberconst GalleryThumbnail = ({alwaysShowDimensions,compareRole,+ dragImages,fit,image,isPrimary,@@ -485,6 +501,7 @@ const GalleryThumbnail = ({}: {alwaysShowDimensions: boolean;compareRole: 'Compare' | 'Viewing' | null;+ dragImages: GalleryImageDragImage[];fit: GalleryThumbnailFit;image: GalleryImage;isPrimary: boolean;@@ -494,9 +511,15 @@ const GalleryThumbnail = ({onToggleStarred: (image: GalleryImage) => void;}) => {const isCompared = compareRole !== null;+ const { isDragging, listeners, setNodeRef, transform } = useDraggable({+ data: getGalleryImageDragData(dragImages),+ id: getGalleryImageDragId(image.imageName),+ });return (<Box+ ref={setNodeRef}+ {...listeners}aspectRatio={1}bg="bg"borderWidth="2px"@@ -504,12 +527,16 @@ const GalleryThumbnail = ({boxShadow={isCompared ? 'inset 0 0 0 1px {colors.accent.solid}' : undefined}css={{ '&:hover .gallery-thumb-overlay': { opacity: 1 } }}minW="0"+ opacity={isDragging ? 0.55 : undefined}overflow="hidden"role="option"aria-selected={isSelected}position="relative"rounded="md"+ style={{ transform: CSS.Transform.toString(transform) }}+ touchAction="none"w="full"+ zIndex={isDragging ? 2 : undefined}onContextMenu={(event) => {event.preventDefault();onContextMenu(image, event.clientX, event.clientY);@@ -535,6 +562,7 @@ const GalleryThumbnail = ({><imgalt={image.imageName}+ draggable={false}src={image.thumbnailUrl || image.imageUrl}style={{display: 'block',diff --git a/invokeai/frontend/webv2/src/workbench/widgets/gallery/galleryDnd.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/gallery/galleryDnd.test.tsnew file mode 100644index 0000000000..b9bb1f188c--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/widgets/gallery/galleryDnd.test.ts@@ -0,0 +1,31 @@+import { describe, expect, it } from 'vitest';++import {+ getGalleryBoardDropData,+ getGalleryImageDragData,+ getGalleryImageNamesOutsideBoard,+ isGalleryBoardDropData,+ isGalleryImageDragData,+} from './galleryDnd';++describe('galleryDnd', () => {+ it('keeps only images outside the target board', () => {+ const dragData = getGalleryImageDragData([+ { boardId: 'board-a', imageName: 'own.png' },+ { boardId: 'board-b', imageName: 'other.png' },+ { boardId: 'none', imageName: 'uncategorized.png' },+ ]);++ expect(getGalleryImageNamesOutsideBoard(dragData, 'board-a')).toEqual(['other.png', 'uncategorized.png']);+ });++ it('recognizes gallery image drag data', () => {+ expect(isGalleryImageDragData(getGalleryImageDragData([{ boardId: 'none', imageName: 'image.png' }]))).toBe(true);+ expect(isGalleryImageDragData({ images: [{ boardId: 'none' }], kind: 'gallery-image' })).toBe(false);+ });++ it('recognizes gallery board drop data', () => {+ expect(isGalleryBoardDropData(getGalleryBoardDropData('board-a', 'board'))).toBe(true);+ expect(isGalleryBoardDropData({ boardId: 'board-a', boardKind: 'generated', kind: 'gallery-board' })).toBe(false);+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/widgets/gallery/galleryDnd.ts b/invokeai/frontend/webv2/src/workbench/widgets/gallery/galleryDnd.tsnew file mode 100644index 0000000000..9db90b86a4--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/widgets/gallery/galleryDnd.ts@@ -0,0 +1,54 @@+import type { GalleryBoardKind } from '@workbench/gallery/api';++export interface GalleryImageDragImage {+ boardId: string;+ imageName: string;+}++export interface GalleryImageDragData {+ kind: 'gallery-image';+ images: GalleryImageDragImage[];+}++export interface GalleryBoardDropData {+ boardId: string;+ boardKind: GalleryBoardKind;+ kind: 'gallery-board';+}++export const getGalleryImageDragId = (imageName: string): string => `gallery-image:${imageName}`;++export const getGalleryBoardDropId = (boardId: string): string => `gallery-board:${boardId}`;++export const getGalleryImageDragData = (images: GalleryImageDragImage[]): GalleryImageDragData => ({+ images,+ kind: 'gallery-image',+});++export const getGalleryBoardDropData = (boardId: string, boardKind: GalleryBoardKind): GalleryBoardDropData => ({+ boardId,+ boardKind,+ kind: 'gallery-board',+});++export const isGalleryImageDragData = (value: unknown): value is GalleryImageDragData =>+ isRecord(value) &&+ value.kind === 'gallery-image' &&+ Array.isArray(value.images) &&+ value.images.every(+ (image) => isRecord(image) && typeof image.imageName === 'string' && typeof image.boardId === 'string'+ );++export const isGalleryBoardDropData = (value: unknown): value is GalleryBoardDropData =>+ isRecord(value) &&+ value.kind === 'gallery-board' &&+ typeof value.boardId === 'string' &&+ isGalleryBoardKind(value.boardKind);++export const getGalleryImageNamesOutsideBoard = (dragData: GalleryImageDragData, boardId: string): string[] =>+ dragData.images.filter((image) => image.boardId !== boardId).map((image) => image.imageName);++const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null;++const isGalleryBoardKind = (value: unknown): value is GalleryBoardKind =>+ value === 'board' || value === 'date' || value === 'uncategorized';