feat: unify AI edit workflow
This commit is contained in:
@@ -15,7 +15,7 @@ describe("generation preconditions", () => {
|
||||
});
|
||||
|
||||
test("requires an enabled mask for inpaint", () => {
|
||||
expect(checkGenerationPreconditions(document(), selected(), settings("inpaint"))).toEqual({ ready: false, message: "Add and enable a layer mask on the selected image before inpainting." });
|
||||
expect(checkGenerationPreconditions(document(), selected(), settings("inpaint"))).toEqual({ ready: false, message: "Paint a mask over the area you want AI to replace.", repair: "add-mask" });
|
||||
});
|
||||
|
||||
test("allows inpaint when the selected source has an aligned enabled mask", () => {
|
||||
@@ -24,7 +24,7 @@ describe("generation preconditions", () => {
|
||||
|
||||
test("explains prompt and outpaint-padding requirements", () => {
|
||||
expect(checkGenerationPreconditions(document(), selected(), { ...settings("image-to-image"), prompt: " " })).toEqual({ ready: false, message: "Enter a prompt to generate an image." });
|
||||
expect(checkGenerationPreconditions(document(), selected(), { ...settings("outpaint"), outpaint: { left: 0, top: 0, right: 0, bottom: 0, feathering: 0 } })).toEqual({ ready: false, message: "Add outpaint padding on at least one side." });
|
||||
expect(checkGenerationPreconditions(document(), selected(), { ...settings("outpaint"), outpaint: { left: 0, top: 0, right: 0, bottom: 0, feathering: 0 } })).toEqual({ ready: false, message: "Add outpaint padding on at least one side.", repair: "set-outpaint-padding" });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { GenerateSettings } from "@editor/tools";
|
||||
|
||||
export type GenerationPrecondition =
|
||||
| { ready: true }
|
||||
| { ready: false; message: string };
|
||||
| { ready: false; message: string; repair?: "add-mask" | "set-outpaint-padding" };
|
||||
|
||||
export function checkGenerationPreconditions(
|
||||
document: ImageDocument,
|
||||
@@ -38,14 +38,14 @@ export function checkGenerationPreconditions(
|
||||
if (settings.mode === "outpaint") {
|
||||
const padding = settings.outpaint;
|
||||
if (padding.left + padding.top + padding.right + padding.bottom <= 0) {
|
||||
return missing("Add outpaint padding on at least one side.");
|
||||
return missing("Add outpaint padding on at least one side.", "set-outpaint-padding");
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.mode !== "inpaint") return { ready: true };
|
||||
|
||||
const layerMask = getLayerMask(layerInfo.layer);
|
||||
if (!layerMask?.enabled) return missing("Add and enable a layer mask on the selected image before inpainting.");
|
||||
if (!layerMask?.enabled) return missing("Paint a mask over the area you want AI to replace.", "add-mask");
|
||||
const maskLayer = index.layerById.get(layerMask.maskLayerId);
|
||||
if (!maskLayer || maskLayer.type === "group") return missing("The selected layer mask is missing.");
|
||||
const maskAsset = index.assetById.get(maskLayer.assetId);
|
||||
@@ -69,8 +69,8 @@ function modeSelectionMessage(mode: GenerateSettings["mode"]): string {
|
||||
return "Select exactly one image or raster layer to outpaint.";
|
||||
}
|
||||
|
||||
function missing(message: string): GenerationPrecondition {
|
||||
return { ready: false, message };
|
||||
function missing(message: string, repair?: Extract<GenerationPrecondition, { ready: false }>["repair"]): GenerationPrecondition {
|
||||
return { ready: false, message, ...(repair ? { repair } : {}) };
|
||||
}
|
||||
|
||||
function rectsAligned(a: { x: number; y: number; w: number; h: number }, b: { x: number; y: number; w: number; h: number }): boolean {
|
||||
|
||||
@@ -40,6 +40,22 @@ describe("generation workflow", () => {
|
||||
expect(app.store.getState().editor.generation.candidates).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("repairs replace prerequisites by adding an aligned editable mask through commands", async () => {
|
||||
const app = createTestApp();
|
||||
const state = app.store.getState();
|
||||
state.document.assets.push({ id: "source-asset", name: "Source", mimeType: "image/png", source: "source", intrinsicSize: { w: 80, h: 60 } });
|
||||
state.document.artboards[0]!.layers.push({ id: "source", type: "raster", name: "Source", visible: true, locked: false, opacity: 1, assetId: "source-asset", transform: { position: { x: 4, y: 8 }, scale: { x: 2, y: 2 }, rotation: 0 } });
|
||||
state.editor.selection = { artboardId: "artboard", layerIds: ["source"] };
|
||||
const ids = ["mask-asset", "mask-layer"];
|
||||
const workflow = createGenerationWorkflow(app.store, dependencies({ createId: () => ids.shift() ?? "unused" }));
|
||||
|
||||
await workflow.prepareInpaintMask();
|
||||
|
||||
const next = app.store.getState();
|
||||
expect(next.document.artboards[0]?.layers[0]).toMatchObject({ id: "mask-layer", transform: { position: { x: 4, y: 8 }, scale: { x: 2, y: 2 }, rotation: 0 } });
|
||||
expect(next.editor.maskEdit).toEqual({ targetLayerId: "source", maskLayerId: "mask-layer" });
|
||||
});
|
||||
|
||||
test("cancels the active operation and records a cancelled job", async () => {
|
||||
const app = createTestApp();
|
||||
let receivedSignal: AbortSignal | undefined;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { commandIds } from "@commands/ids";
|
||||
import type { Layer } from "@core/layer";
|
||||
import type { GenerationCandidate, GenerationJobKind } from "@editor/state";
|
||||
import type { AppStore } from "@editor/store";
|
||||
import type { GenerateSettings } from "@editor/tools";
|
||||
@@ -50,6 +51,34 @@ export function createGenerationWorkflow(store: AppStore, dependencies: Generati
|
||||
|
||||
loadResources: () => dependencies.loadGenerationResources(store),
|
||||
|
||||
prepareInpaintMask: async () => {
|
||||
const state = store.getState();
|
||||
const layerId = state.editor.selection.layerIds.length === 1 ? state.editor.selection.layerIds[0] : undefined;
|
||||
if (!layerId) return;
|
||||
const artboard = state.document.artboards.find((candidate) => candidate.id === state.editor.selection.artboardId);
|
||||
const layer = artboard ? findLayer(artboard.layers, layerId) : undefined;
|
||||
if (!layer || layer.type === "group") return;
|
||||
const asset = state.document.assets.find((candidate) => candidate.id === layer.assetId);
|
||||
if (!asset) return;
|
||||
const source = await dependencies.createRefinementMask(asset.intrinsicSize.w, asset.intrinsicSize.h);
|
||||
const maskAssetId = dependencies.createId();
|
||||
const maskLayerId = dependencies.createId();
|
||||
store.dispatch(commandIds.documentAddLayerMask, {
|
||||
layerId,
|
||||
asset: { id: maskAssetId, name: `${layer.name} AI edit mask`, mimeType: "image/png", source, intrinsicSize: { ...asset.intrinsicSize } },
|
||||
maskLayer: {
|
||||
id: maskLayerId,
|
||||
type: "raster",
|
||||
name: `${layer.name} AI edit mask`,
|
||||
visible: true,
|
||||
locked: false,
|
||||
opacity: 1,
|
||||
assetId: maskAssetId,
|
||||
transform: { position: { ...layer.transform.position }, scale: { ...layer.transform.scale }, rotation: layer.transform.rotation },
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
generate: () => job("generate", "Generating", async (signal) => {
|
||||
const state = store.getState();
|
||||
await dependencies.runGenerate({
|
||||
@@ -130,6 +159,17 @@ export function createGenerationWorkflow(store: AppStore, dependencies: Generati
|
||||
};
|
||||
}
|
||||
|
||||
function findLayer(layers: readonly Layer[], layerId: string): Layer | undefined {
|
||||
for (const layer of layers) {
|
||||
if (layer.id === layerId) return layer;
|
||||
if (layer.type === "group") {
|
||||
const child = findLayer(layer.children, layerId);
|
||||
if (child) return child;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function findCandidate(store: AppStore, candidateId: string): GenerationCandidate {
|
||||
const candidate = store.getState().editor.generation.candidates.find((item) => item.id === candidateId);
|
||||
if (!candidate) throw new Error("This generation candidate is no longer available.");
|
||||
|
||||
Reference in New Issue
Block a user