Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Remove family originals only from the family view

Bitti6ebbff43c63c
diff --git a/src/components/workspace/ImageBar.tsx b/src/components/workspace/ImageBar.tsx
index 82c4b1d..a5201db 100644
--- a/src/components/workspace/ImageBar.tsx
+++ b/src/components/workspace/ImageBar.tsx
@@ -1,5 +1,5 @@
import { forwardRef, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState, type KeyboardEvent } from "react";
-import { imageFamilyRoot } from "../../lib/imageHistory";
+import { imageFamily, imageFamilyRoot } from "../../lib/imageHistory";
import type { ProjectAsset } from "../../lib/project";
import { ChevronLeft16, Edit11, Image24 } from "../ui/icons";
import { ReferenceImage } from "./ReferenceImage";
@@ -9,8 +9,8 @@ import "../../styles/image-editor.css";
Export tab: one thumbnail per image, the selected one ringed in accent, a
draft badged. The mouse wheel scrolls it sideways, and a new image scrolls
into view at the end; the arrow keys step through it. `onMenu` opens a
- context menu for an image (id) or for the bar (null) at a point; without it
- the bar has no menu.
+ context menu for an image (id) or for the bar (null) at a point, and says
+ whether the bar is showing a family; without it the bar has no menu.
An image regenerated or edited from another joins that image's family. The
bar lists the originals; while an image with a family is selected it shows
@@ -21,20 +21,20 @@ export const ImageBar = forwardRef<HTMLDivElement | null, {
selectedId: string | null | undefined;
folderPath: string;
onSelect: (id: string) => void;
- onMenu?: (id: string | null, x: number, y: number) => void;
+ onMenu?: (id: string | null, x: number, y: number, inFamily: boolean) => void;
}>(function ImageBar({ images, selectedId, folderPath, onSelect, onMenu }, ref) {
const bar = useRef<HTMLDivElement>(null);
useImperativeHandle(ref, () => bar.current as HTMLDivElement);
const root = imageFamilyRoot(images, selectedId);
- const family = root ? images.filter((asset) => asset.id === root || imageFamilyRoot(images, asset.id) === root) : [];
+ const family = imageFamily(images, selectedId);
// Selecting an image opens its family; Back shows the originals until the
// next selection.
const [showAll, setShowAll] = useState(false);
useEffect(() => setShowAll(false), [selectedId]);
const inFamily = !showAll && family.length > 1;
const originals = images.filter((asset) => imageFamilyRoot(images, asset.id) === asset.id);
- const shown = inFamily ? [images.find((asset) => asset.id === root)!, ...family.filter((asset) => asset.id !== root)] : originals;
+ const shown = inFamily ? family : originals;
const pressed = inFamily ? selectedId : root;
const childCount = (id: string) => images.filter((asset) => asset.id !== id && imageFamilyRoot(images, asset.id) === id).length;
@@ -82,24 +82,24 @@ export const ImageBar = forwardRef<HTMLDivElement | null, {
const menuKey = (event: KeyboardEvent) => event.key === "ContextMenu" || (event.shiftKey && event.key === "F10");
return <div ref={bar} tabIndex={0} className="image-results" aria-label="Generated images"
- onContextMenu={onMenu && ((event) => { event.preventDefault(); onMenu(null, event.clientX, event.clientY); })}
+ onContextMenu={onMenu && ((event) => { event.preventDefault(); onMenu(null, event.clientX, event.clientY, inFamily); })}
onKeyDown={(event) => {
if (arrowKey(event)) return;
if (!onMenu || event.target !== event.currentTarget || !menuKey(event)) return;
event.preventDefault();
const bounds = event.currentTarget.getBoundingClientRect();
- onMenu(null, bounds.left, bounds.top);
+ onMenu(null, bounds.left, bounds.top, inFamily);
}}>
{inFamily && <button type="button" className="image-results__back" aria-label="Back to all images" data-tooltip="All images"
onClick={() => { setShowAll(true); bar.current?.focus(); }}><ChevronLeft16 aria-hidden="true" /></button>}
{shown.map((asset) => <button key={asset.id} type="button" data-image-asset={asset.id} data-tooltip={asset.name}
aria-label={`View ${asset.name}${!inFamily && childCount(asset.id) ? `, ${childCount(asset.id) + 1} versions` : ""}`} aria-pressed={asset.id === pressed}
- onContextMenu={onMenu && ((event) => { event.preventDefault(); event.stopPropagation(); onMenu(asset.id, event.clientX, event.clientY); })}
+ onContextMenu={onMenu && ((event) => { event.preventDefault(); event.stopPropagation(); onMenu(asset.id, event.clientX, event.clientY, inFamily); })}
onKeyDown={onMenu && ((event) => {
if (!menuKey(event)) return;
event.preventDefault();
const bounds = event.currentTarget.getBoundingClientRect();
- onMenu(asset.id, bounds.left, bounds.bottom);
+ onMenu(asset.id, bounds.left, bounds.bottom, inFamily);
})}
onClick={() => { setShowAll(false); onSelect(asset.id); }}>
{asset.relativePath || asset.sourcePath ? <ReferenceImage folderPath={folderPath} relativePath={asset.relativePath} sourcePath={asset.sourcePath} alt={asset.name} /> : <Image24 aria-hidden="true" />}
diff --git a/src/components/workspace/ImageEditor.test.tsx b/src/components/workspace/ImageEditor.test.tsx
index 37ccfc6..f8838c5 100644
--- a/src/components/workspace/ImageEditor.test.tsx
+++ b/src/components/workspace/ImageEditor.test.tsx
@@ -536,3 +536,34 @@ it("shows an image's family behind a back button, the original first", () => {
fireEvent.click(screen.getByRole("button", { name: "View Other" }));
expect(bar()).toEqual(["View Original, 3 versions", "View Other"]);
});
+
+it("removes an original with a family only from inside the family, and the leftmost image takes its place", () => {
+ const initial = parseProjectConfig(fixture);
+ const image = (id: string, parentAssetId?: string) => ({ id, name: id, kind: "image" as const, relativePath: `media/generated/${id}.jpg`, mimeType: "image/jpeg", createdAt: initial.createdAt, ...(parentAssetId ? { parentAssetId } : {}) });
+ initial.assets = [image("Original"), image("Other"), image("Retry", "Original"), image("Edited", "Original")];
+ initial.imageScene!.outputAssetId = "Other";
+ const current = setup(initial);
+ const bar = () => within(screen.getByLabelText("Generated images")).getAllByRole("button").map((button) => button.getAttribute("aria-label"));
+ const remove = (name: string) => {
+ fireEvent.contextMenu(screen.getByRole("button", { name }));
+ return screen.getByRole("menuitem", { name: "Remove" });
+ };
+ // On the originals, an original with a family cannot be removed; one alone can.
+ expect(remove("View Original, 3 versions")).toBeDisabled();
+ fireEvent.keyDown(document.activeElement!, { key: "Escape" });
+ expect(remove("View Other")).toBeEnabled();
+ fireEvent.keyDown(document.activeElement!, { key: "Escape" });
+
+ // Inside the family it can, and the leftmost image becomes the original.
+ fireEvent.click(screen.getByRole("button", { name: "View Original, 3 versions" }));
+ fireEvent.click(remove("View Original"));
+ expect(bar()).toEqual(["Back to all images", "View Retry", "View Edited"]);
+ expect(screen.getByRole("button", { name: "View Retry" })).toHaveAttribute("aria-pressed", "true");
+ expect(current().assets.map((asset) => [asset.id, asset.parentAssetId])).toEqual([["Other", undefined], ["Retry", undefined], ["Edited", "Retry"]]);
+
+ // Emptying the family takes it off the originals too.
+ fireEvent.click(remove("View Retry"));
+ expect(bar()).toEqual(["View Other", "View Edited"]);
+ fireEvent.click(remove("View Edited"));
+ expect(bar()).toEqual(["View Other"]);
+});
diff --git a/src/components/workspace/ImageEditor.tsx b/src/components/workspace/ImageEditor.tsx
index bbae5c5..632aba4 100644
--- a/src/components/workspace/ImageEditor.tsx
+++ b/src/components/workspace/ImageEditor.tsx
@@ -5,7 +5,7 @@ import { addImageNode, createImageEditScene, createImageScene, duplicateImageNod
import { compileImageEdits, editGeneratedImage, imageEditDebugPrompt } from "../../lib/imageEditing";
import { outputDimensions } from "../../lib/export";
import { compileImagePrompt } from "../../lib/imagePrompt";
-import { createEmptyImage, removeImageAsset, restoreGeneratedImage, saveImageDraft } from "../../lib/imageHistory";
+import { createEmptyImage, imageFamily, removeImageAsset, restoreGeneratedImage, saveImageDraft } from "../../lib/imageHistory";
import { isTauri } from "../../lib/persistence";
import { PROJECT_RESOLUTIONS, referenceTypeLabel, type ProjectConfig } from "../../lib/project";
import { defaultGeneratorTemplate, loadDebugOptionsEnabled, loadGeneratorTemplateSettings, subscribeDebugOptions, subscribeGeneratorTemplates, templateNeedsDownload, type GeneratorTemplate } from "../../lib/settings";
@@ -46,7 +46,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
const [draggedNode, setDraggedNode] = useState<string | null>(null);
const [treeDrop, setTreeDrop] = useState<{ id: string; placement: "before" | "inside" | "after" } | null>(null);
const [contextMenu, setContextMenu] = useState<{ x: number; y: number } | null>(null);
- const [imageMenu, setImageMenu] = useState<{ id: string | null; x: number; y: number } | null>(null);
+ const [imageMenu, setImageMenu] = useState<{ id: string | null; x: number; y: number; inFamily: boolean } | null>(null);
const imageResults = useRef<HTMLDivElement>(null);
const [renaming, setRenaming] = useState<{ id: string; value: string } | null>(null);
const renameEnding = useRef(false);
@@ -191,9 +191,11 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
const remaining = currentImages.filter((asset) => asset.id !== id);
const currentScene = current.imageScene ?? scene;
const removingSelected = currentScene.outputAssetId === id;
- const next = removingSelected
- ? remaining[Math.min(currentImages.findIndex((asset) => asset.id === id), remaining.length - 1)]
- : remaining.find((asset) => asset.id === currentScene.outputAssetId);
+ const family = imageFamily(currentImages, id);
+ const familyLeft = imageFamily(removeImageAsset(currentImages, id), family.find((asset) => asset.id !== id)?.id);
+ const next = !removingSelected ? remaining.find((asset) => asset.id === currentScene.outputAssetId)
+ : familyLeft.length ? familyLeft[Math.min(family.findIndex((asset) => asset.id === id), familyLeft.length - 1)]
+ : remaining[Math.min(currentImages.findIndex((asset) => asset.id === id), remaining.length - 1)];
const updated = { ...current,
assets: removeImageAsset(current.assets, id),
imageScene: removingSelected && removed.imageDraft && !next ? currentScene.rootType === "image" ? createImageEditScene(null, currentScene) : createImageScene() : { ...currentScene, outputAssetId: removingSelected ? next?.id ?? null : currentScene.outputAssetId },
@@ -471,7 +473,9 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
}) },
...(imageMenu.id ? [
{ label: "Edit", icon: <Edit16 />, action: () => attempt(() => config.assets.find((asset) => asset.id === imageMenu.id)?.imageDraft ? selectImage(imageMenu.id!) : replaceScene(editGeneratedImage(config, imageMenu.id!).imageScene!)) },
- { label: "Remove", icon: <Delete16 />, danger: true, disabled: workItems.some((item) => isWorkActive(item) && item.imageAssetId === imageMenu.id), action: () => removeImage(imageMenu.id!) },
+ { label: "Remove", icon: <Delete16 />, danger: true,
+ disabled: workItems.some((item) => isWorkActive(item) && item.imageAssetId === imageMenu.id) || (!imageMenu.inFamily && imageFamily(images, imageMenu.id).length > 1),
+ action: () => removeImage(imageMenu.id!) },
] : []),
]} />}
<section className="image-center" aria-label="Image panel">
@@ -560,7 +564,7 @@ export function ImageEditor({ config, folderPath, onChange: changeConfig, onGene
{(error || work?.error) && <InfoBar className="image-error" severity="error" title="Couldn’t update the image" message={error ?? work?.error ?? ""} onClose={error ? () => setError(null) : undefined} />}
<div className="image-status" role="status">{active && <ProgressBar value={work.progress * 100} aria-label="Image generation progress" />}<span>{active ? work.detail : imageRoot ? editPlan?.error ?? `${width} × ${height} · ${editPlan?.edits.length} edits in hierarchy order` : !isTauri() ? "Image generation is available in the desktop app." : `${width} × ${height} · Placement boxes guide the prompt`}</span></div>
<ImageBar ref={imageResults} images={images} selectedId={scene.outputAssetId} folderPath={folderPath} onSelect={selectImage}
- onMenu={(id, x, y) => { setContextMenu(null); setImageMenu({ id, x, y }); }} />
+ onMenu={(id, x, y, inFamily) => { setContextMenu(null); setImageMenu({ id, x, y, inFamily }); }} />
</section>
<Splitter {...inspectorPane.splitterProps} reverse aria-label="Resize inspector" />
<aside className="image-inspector" aria-label="Image node inspector">
diff --git a/src/lib/imageHistory.ts b/src/lib/imageHistory.ts
index f31fe50..d5dbcf1 100644
--- a/src/lib/imageHistory.ts
+++ b/src/lib/imageHistory.ts
@@ -26,8 +26,16 @@ export function imageFamilyRoot(assets: readonly ProjectAsset[], id: string | nu
return asset.parentAssetId && assets.some((candidate) => candidate.id === asset.parentAssetId && candidate.kind === "image") ? asset.parentAssetId : asset.id;
}
-/** Removes an image. Its children stay a family: the oldest becomes the
- * original the others belong to. */
+/** An image's family in the bar's order: the original, then the images made
+ * from it. An image with no family is a family of one. */
+export function imageFamily(assets: readonly ProjectAsset[], id: string | null | undefined): ProjectAsset[] {
+ const root = imageFamilyRoot(assets, id);
+ if (!root) return [];
+ return [assets.find((asset) => asset.id === root)!, ...assets.filter((asset) => asset.id !== root && imageFamilyRoot(assets, asset.id) === root)];
+}
+
+/** Removes an image. Its children stay a family: the leftmost of them — the
+ * oldest — becomes the original the others belong to. */
export function removeImageAsset(assets: readonly ProjectAsset[], id: string): ProjectAsset[] {
const children = assets.filter((asset) => asset.parentAssetId === id);
const heir = children[0]?.id;