feat: unify AI edit workflow

This commit is contained in:
syntaxbullet
2026-07-11 11:58:16 +02:00
parent 49b029d60a
commit bef2fb3051
12 changed files with 173 additions and 41 deletions

View File

@@ -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.");