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(webv2): keep the For loop body box live and across reloads

Josh Corbettf87fddb8d92c
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/LoopBodyBoundaryOverlay.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/editor/LoopBodyBoundaryOverlay.tsx
index 3151b585ca..f68cf0474a 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/LoopBodyBoundaryOverlay.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/LoopBodyBoundaryOverlay.tsx
@@ -2,30 +2,81 @@ import type { WorkflowEdge, WorkflowNode } from '@features/workflow/contracts';
import { Box, Text } from '@chakra-ui/react';
import { getForLoopBodyBoundaries, type LoopBodyBoundaryStatus } from '@features/workflow/utility';
-import { useReactFlow, ViewportPortal } from '@xyflow/react';
-import { memo, useMemo } from 'react';
+import { type ReactFlowState, useStore, ViewportPortal } from '@xyflow/react';
+import { memo, useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
-import type { WorkflowFlowEdge, WorkflowFlowNode } from './flowAdapters';
-
const BOUNDARY_PADDING = 24;
+interface BoundaryRect {
+ height: number;
+ width: number;
+ x: number;
+ y: number;
+}
+
const getStatusColor = (status: LoopBodyBoundaryStatus) =>
status === 'complete'
? { border: 'border.success', text: 'fg.success' }
: { border: 'border.warning', text: 'fg.warning' };
+/** Bounds of the measured body nodes from xyflow's live lookup, so drags and remeasures update the box. */
+const getMeasuredBounds = (nodeLookup: ReactFlowState['nodeLookup'], nodeIds: string[]): BoundaryRect | null => {
+ let x = Infinity;
+ let y = Infinity;
+ let x2 = -Infinity;
+ let y2 = -Infinity;
+
+ for (const nodeId of nodeIds) {
+ const node = nodeLookup.get(nodeId);
+ const width = node?.measured.width;
+ const height = node?.measured.height;
+
+ if (!node || !width || !height) {
+ continue;
+ }
+
+ const { x: nodeX, y: nodeY } = node.internals.positionAbsolute;
+
+ x = Math.min(x, nodeX);
+ y = Math.min(y, nodeY);
+ x2 = Math.max(x2, nodeX + width);
+ y2 = Math.max(y2, nodeY + height);
+ }
+
+ return x2 > x && y2 > y ? { height: y2 - y, width: x2 - x, x, y } : null;
+};
+
+const areSameRects = (a: (BoundaryRect | null)[], b: (BoundaryRect | null)[]): boolean =>
+ a.length === b.length &&
+ a.every((rect, i) => {
+ const other = b[i] ?? null;
+
+ return rect === other
+ ? true
+ : rect !== null &&
+ other !== null &&
+ rect.x === other.x &&
+ rect.y === other.y &&
+ rect.width === other.width &&
+ rect.height === other.height;
+ });
+
export const LoopBodyBoundaryOverlay = ({ nodes, edges }: { nodes: WorkflowNode[]; edges: WorkflowEdge[] }) => {
const { t } = useTranslation();
- const { getNodesBounds } = useReactFlow<WorkflowFlowNode, WorkflowFlowEdge>();
const boundaries = useMemo(() => getForLoopBodyBoundaries(nodes, edges), [edges, nodes]);
+ const selectBounds = useCallback(
+ (state: ReactFlowState) => boundaries.map((boundary) => getMeasuredBounds(state.nodeLookup, boundary.bodyNodeIds)),
+ [boundaries]
+ );
+ const boundsByIndex = useStore(selectBounds, areSameRects);
return (
<ViewportPortal>
- {boundaries.map((boundary) => {
- const bounds = getNodesBounds(boundary.bodyNodeIds);
+ {boundaries.map((boundary, index) => {
+ const bounds = boundsByIndex[index];
- if (bounds.width <= 0 || bounds.height <= 0) {
+ if (!bounds) {
return null;
}
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/flowAdapters.test.ts b/invokeai/frontend/webv2/src/features/workflow/ui/editor/flowAdapters.test.ts
index d3daae7c33..1ebd78293e 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/flowAdapters.test.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/flowAdapters.test.ts
@@ -130,6 +130,15 @@ describe('flowAdapters identity preservation', () => {
expect(second[1]).not.toBe(first[1]);
});
+ it('keeps the measured size when a node is rebuilt after a document change', () => {
+ const doc = createDoc();
+ const first = toFlowNodes(doc).map((node) => ({ ...node, measured: { height: 120, width: 288 } }));
+ const movedNodeB = { ...doc.nodes[1]!, position: { x: 50, y: 50 } };
+ const second = toFlowNodes({ ...doc, nodes: [doc.nodes[0]!, movedNodeB] }, first as typeof first);
+
+ expect(second[1]?.measured).toEqual({ height: 120, width: 288 });
+ });
+
it('recomputes a node when its incoming connections change', () => {
const doc = createDoc();
const first = toFlowNodes(doc);
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/flowAdapters.ts b/invokeai/frontend/webv2/src/features/workflow/ui/editor/flowAdapters.ts
index c32e29c749..290333f057 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/flowAdapters.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/flowAdapters.ts
@@ -184,6 +184,8 @@ export const toFlowNodes = (
return document.nodes.map((documentNode): WorkflowFlowNode => {
const previous = previousById.get(documentNode.id);
const selected = previous?.selected ?? false;
+ // Rebuilt nodes keep their measured size so xyflow does not treat them as unmeasured (0x0) until remeasured.
+ const measured = previous?.measured;
if (documentNode.type === 'invocation') {
const connectedSourceHandles = connectedSourcesByNode.get(documentNode.id) ?? EMPTY_NAMES;
@@ -215,6 +217,7 @@ export const toFlowNodes = (
template,
},
id: documentNode.id,
+ measured,
position: documentNode.position,
selected,
type: 'invocation' as const,
@@ -229,6 +232,7 @@ export const toFlowNodes = (
return {
data: { documentNode },
id: documentNode.id,
+ measured,
position: documentNode.position,
selected,
type: 'notes' as const,
@@ -255,6 +259,7 @@ export const toFlowNodes = (
return {
data: { documentNode, inputFieldType, outputFieldType },
id: documentNode.id,
+ measured,
position: documentNode.position,
selected,
type: 'connector' as const,
@@ -268,6 +273,7 @@ export const toFlowNodes = (
return {
data: { documentNode },
id: documentNode.id,
+ measured,
position: documentNode.position,
selected,
type: 'current_image' as const,