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.

fix(gallery): pick the deletion successor in display order

Josh Corbettb90f1401b0fd
diff --git a/invokeai/frontend/webv2/src/features/gallery/contracts.ts b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
index 48d6ead09a..72e1ecacaf 100644
--- a/invokeai/frontend/webv2/src/features/gallery/contracts.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
@@ -69,6 +69,7 @@ export {
type GallerySelectedImageQuery,
} from './ui/galleryStateView';
export {
+ getGalleryDeletionSuccessor,
getPersistedSelectedGalleryItemKeys,
getSelectedGalleryImageFromValues,
getSelectedGalleryItemFromValues,
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/selection.test.ts b/invokeai/frontend/webv2/src/features/gallery/core/selection.test.ts
index ca9ef3b0fd..ed7c7dfedc 100644
--- a/invokeai/frontend/webv2/src/features/gallery/core/selection.test.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/core/selection.test.ts
@@ -144,3 +144,45 @@ describe('persisted Gallery selection readers', () => {
expect(getPersistedSelectedGalleryItemKeys({ selectedImage: videoItem })).toEqual(['video:shared']);
});
});
+
+describe('getGalleryDeletionSuccessor', () => {
+ const ref = (name: string): { kind: 'image'; name: string } => ({ kind: 'image', name });
+ const keys = (...names: string[]): Set<`image:${string}`> => new Set(names.map((name) => `image:${name}` as const));
+
+ it('picks the next item in display order — the one that slides into the deleted slot', () => {
+ expect(selection.getGalleryDeletionSuccessor([ref('a'), ref('b'), ref('c')], 'image:b', keys('b'))).toEqual(
+ ref('c')
+ );
+ });
+
+ it('falls back to the nearest earlier item at the end of the list', () => {
+ expect(selection.getGalleryDeletionSuccessor([ref('a'), ref('b'), ref('c')], 'image:c', keys('c'))).toEqual(
+ ref('b')
+ );
+ });
+
+ it('skips ineligible refs in both directions', () => {
+ expect(
+ selection.getGalleryDeletionSuccessor([ref('a'), ref('b'), ref('c'), ref('d')], 'image:b', keys('b', 'c', 'd'))
+ ).toEqual(ref('a'));
+ });
+
+ it('stays in the regular block when starred-first names lead the list', () => {
+ // Regression: the old earlier-first walk crossed the section boundary, so
+ // deleting the first regular item selected the LAST STARRED item.
+ const orderedRefs = [ref('starred-1'), ref('starred-2'), ref('regular-1'), ref('regular-2')];
+
+ expect(selection.getGalleryDeletionSuccessor(orderedRefs, 'image:regular-1', keys('regular-1'))).toEqual(
+ ref('regular-2')
+ );
+ });
+
+ it('returns null when the primary is not in the list', () => {
+ expect(selection.getGalleryDeletionSuccessor([ref('a')], 'image:missing', keys('a'))).toBeNull();
+ expect(selection.getGalleryDeletionSuccessor([], 'image:a', keys('a'))).toBeNull();
+ });
+
+ it('returns null when nothing survives', () => {
+ expect(selection.getGalleryDeletionSuccessor([ref('a')], 'image:a', keys('a'))).toBeNull();
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/selection.ts b/invokeai/frontend/webv2/src/features/gallery/core/selection.ts
index 8a3154d5e8..7464f5cf64 100644
--- a/invokeai/frontend/webv2/src/features/gallery/core/selection.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/core/selection.ts
@@ -8,6 +8,7 @@ import {
toGalleryItemKey,
type GalleryItem,
type GalleryItemKey,
+ type GalleryItemRef,
} from './items';
const isRecord = (value: unknown): value is Record<string, unknown> => Boolean(value) && typeof value === 'object';
@@ -95,6 +96,43 @@ export const getPersistedSelectedGalleryItemKeys = (galleryValues: Record<string
return selectedItem ? [toGalleryItemKey(selectedItem)] : [];
};
+/**
+ * The item that should hold the selection after `primaryKey` is deleted: the
+ * next eligible entry in display order — the one that visually slides into
+ * the deleted slot — then, when nothing later survives, the nearest earlier
+ * entry. Earlier-first walking crosses the starred/regular section boundary
+ * under starred-first ordering, so later always wins.
+ */
+export const getGalleryDeletionSuccessor = (
+ orderedRefs: readonly GalleryItemRef[],
+ primaryKey: GalleryItemKey,
+ ineligibleKeys: ReadonlySet<GalleryItemKey>
+): GalleryItemRef | null => {
+ const primaryIndex = orderedRefs.findIndex((ref) => toGalleryItemKey(ref) === primaryKey);
+
+ if (primaryIndex < 0) {
+ return null;
+ }
+
+ for (let index = primaryIndex + 1; index < orderedRefs.length; index += 1) {
+ const candidate = orderedRefs[index];
+
+ if (candidate && !ineligibleKeys.has(toGalleryItemKey(candidate))) {
+ return candidate;
+ }
+ }
+
+ for (let index = primaryIndex - 1; index >= 0; index -= 1) {
+ const candidate = orderedRefs[index];
+
+ if (candidate && !ineligibleKeys.has(toGalleryItemKey(candidate))) {
+ return candidate;
+ }
+ }
+
+ return null;
+};
+
/*
* Reveal requests: an explicit "scroll this item into view" signal from
* surfaces outside the grid (the image map's reveal). Deliberately NOT derived
diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx
index 219cdbaa57..f6a9c946d7 100644
--- a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx
@@ -888,7 +888,7 @@ const galleryItem = (kind: GalleryItem['kind'], name: string): GalleryItem => {
};
describe('primary successor after confirmed deletion', () => {
- it('selects the nearest surviving predecessor from ordered names before a successor', async () => {
+ it('selects the next surviving item in display order — the one that takes the deleted slot', async () => {
const before = galleryItem('video', 'before.mp4');
const primary = galleryItem('image', 'primary.png');
const after = galleryItem('image', 'after.png');
@@ -909,21 +909,51 @@ describe('primary successor after confirmed deletion', () => {
await getItemActions().deleteItems([{ kind: 'image', name: 'primary.png' }]);
});
- expect(mocks.gallerySelectItem).toHaveBeenCalledWith(before, 'project-1');
+ expect(mocks.gallerySelectItem).toHaveBeenCalledWith(after, 'project-1');
});
- it('resolves an unloaded predecessor by qualified ref', async () => {
+ it('does not jump into the leading starred block when deleting the first regular item', async () => {
+ // Ordered names lead with the starred section under infinite pagination;
+ // the successor must stay in the regular section the primary came from.
+ const starred = { ...galleryItem('image', 'starred.png'), starred: true };
const primary = galleryItem('image', 'primary.png');
- const after = galleryItem('image', 'after.png');
+ const nextRegular = galleryItem('image', 'next-regular.png');
+ currentItemActionContext = {
+ filterIdentity: 'filter-a',
+ items: [starred, primary, nextRegular],
+ loadOrderedRefs: () =>
+ Promise.resolve([
+ { kind: 'image' as const, name: starred.name },
+ { kind: 'image' as const, name: primary.name },
+ { kind: 'image' as const, name: nextRegular.name },
+ ]),
+ selectedItemKey: 'image:primary.png',
+ };
+ mocks.itemDelete.mockResolvedValue({
+ affectedBoardIds: ['board-1'],
+ failed: [],
+ succeeded: [{ kind: 'image', name: primary.name }],
+ });
+
+ await act(async () => {
+ await getItemActions().deleteItems([{ kind: 'image', name: primary.name }]);
+ });
+
+ expect(mocks.gallerySelectItem).toHaveBeenCalledWith(nextRegular, 'project-1');
+ });
+
+ it('resolves an unloaded successor by qualified ref', async () => {
+ const primary = galleryItem('image', 'primary.png');
+ const before = galleryItem('image', 'before.png');
const unloaded = galleryItem('video', 'unloaded.mp4');
currentItemActionContext = {
filterIdentity: 'filter-a',
- items: [primary, after],
+ items: [before, primary],
loadOrderedRefs: () =>
Promise.resolve([
+ { kind: 'image' as const, name: before.name },
{ kind: 'video' as const, name: unloaded.name },
{ kind: 'image' as const, name: primary.name },
- { kind: 'image' as const, name: after.name },
]),
selectedItemKey: 'image:primary.png',
};
@@ -1040,7 +1070,7 @@ describe('primary successor after confirmed deletion', () => {
await getItemActions().deleteItems([{ kind: 'image', name: 'primary.png' }]);
});
- expect(mocks.gallerySelectItem).toHaveBeenCalledWith(before, 'project-1', 30, true);
+ expect(mocks.gallerySelectItem).toHaveBeenCalledWith(after, 'project-1', 30, true);
});
it('opens an item in Preview at the page the host navigates from', () => {
diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts
index 23aa127332..1d7acf195e 100644
--- a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts
+++ b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts
@@ -18,7 +18,7 @@ import {
galleryVideos,
type GalleryVideoItem,
} from '@features/gallery';
-import { getGalleryBoardLabel } from '@features/gallery/contracts';
+import { getGalleryBoardLabel, getGalleryDeletionSuccessor } from '@features/gallery/contracts';
import {
getGalleryItemBoardIdsFromCaches,
getGalleryItemStarredFromCaches,
@@ -363,34 +363,8 @@ export const useImageActions = ({
if (deletionContext && primaryKey && succeededKeys.has(primaryKey) && isDeletionContextCurrent()) {
const refs = orderedRefs ?? deletionContext.items.map(toGalleryItemRef);
- const primaryIndex = refs.findIndex((ref) => toGalleryItemKey(ref) === primaryKey);
- const ineligibleKeys = new Set([
- ...result.succeeded.map(toGalleryItemKey),
- ...result.failed.map(toGalleryItemKey),
- ]);
- let successorRef: GalleryItemRef | null = null;
-
- if (primaryIndex >= 0) {
- for (let index = primaryIndex - 1; index >= 0; index -= 1) {
- const candidate = refs[index];
-
- if (candidate && !ineligibleKeys.has(toGalleryItemKey(candidate))) {
- successorRef = candidate;
- break;
- }
- }
-
- if (!successorRef) {
- for (let index = primaryIndex + 1; index < refs.length; index += 1) {
- const candidate = refs[index];
-
- if (candidate && !ineligibleKeys.has(toGalleryItemKey(candidate))) {
- successorRef = candidate;
- break;
- }
- }
- }
- }
+ const ineligibleKeys = new Set([...succeededKeys, ...result.failed.map(toGalleryItemKey)]);
+ const successorRef = getGalleryDeletionSuccessor(refs, primaryKey, ineligibleKeys);
if (successorRef) {
successor =