diff --git a/src/lib/litegraph/src/LGraphNode.test.ts b/src/lib/litegraph/src/LGraphNode.test.ts index afa7f468810..bd760c0ad0b 100644 --- a/src/lib/litegraph/src/LGraphNode.test.ts +++ b/src/lib/litegraph/src/LGraphNode.test.ts @@ -18,7 +18,8 @@ import { LGraph, LLink, NodeInputSlot, - NodeOutputSlot + NodeOutputSlot, + RenderShape } from '@/lib/litegraph/src/litegraph' import { test } from './__fixtures__/testExtensions' @@ -139,6 +140,235 @@ describe('LGraphNode', () => { expect(Array.from(node.size)).toEqual([70, 80]) }) + describe('missing-node placeholder serialization', () => { + function createPlaceholder( + overrides: Partial = {} + ): LGraphNode { + const lastSerialization = getMockISerialisedNode({ + type: 'UninstalledNodeType', + pos: [100, 100], + size: [140, 60], + widgets_values: [512], + ...overrides + }) + const placeholder = new LGraphNode('') + placeholder.last_serialization = lastSerialization + placeholder.has_errors = true + placeholder.configure(lastSerialization) + return placeholder + } + + test('carries the live pos and size through to the replayed serialization', () => { + const placeholder = createPlaceholder() + placeholder.setPos(999, 888) + placeholder.setSize([777, 666]) + + const serialized = placeholder.serialize() + + expect(serialized.pos).toEqual([999, 888]) + expect(serialized.size).toEqual([777, 666]) + }) + + test('still replays the fields it has no definition to regenerate', () => { + const placeholder = createPlaceholder() + placeholder.setSize([777, 666]) + + const serialized = placeholder.serialize() + + expect(serialized.type).toEqual('UninstalledNodeType') + expect(serialized.widgets_values).toEqual([512]) + }) + + test('restores the recorded size after configure-time widgets auto-expand the node', () => { + const lastSerialization = getMockISerialisedNode({ + type: 'UninstalledNodeType', + pos: [100, 100], + size: [140, 60], + widgets_values: [512] + }) + const placeholder = new LGraphNode('') + placeholder.last_serialization = lastSerialization + placeholder.has_errors = true + // The missing-node error UI adds one widget per recorded value from + // `onConfigure`, and `addWidget` -> `expandToFitContent` widens the + // node past the size the file recorded. + placeholder.onConfigure = function (this: LGraphNode) { + this.addWidget('number', 'UNKNOWN', 512, () => {}) + } + + placeholder.configure(lastSerialization) + + expect(Array.from(placeholder.size)).toEqual([140, 60]) + expect(placeholder.serialize().size).toEqual([140, 60]) + }) + + test('does not invent a size when the recorded serialization has none', () => { + const placeholder = createPlaceholder() + delete (placeholder.last_serialization as Partial).size + + const serialized = placeholder.serialize() + + expect(serialized).not.toHaveProperty('size') + }) + + test('keeps the recorded size while collapsed, since a placeholder cannot re-derive an expanded one', () => { + const placeholder = createPlaceholder({ flags: { collapsed: false } }) + new LGraph().add(placeholder) + placeholder.collapse(true) + placeholder.setSize([80, 30]) + + const serialized = placeholder.serialize() + + expect(serialized.size).toEqual([140, 60]) + expect(serialized.flags).toEqual({ collapsed: true }) + }) + + test('records the size it was collapsed from, so a resize survives collapsing', () => { + const placeholder = createPlaceholder({ flags: { collapsed: false } }) + new LGraph().add(placeholder) + placeholder.setSize([777, 666]) + placeholder.collapse() + + const serialized = placeholder.serialize() + + expect(serialized.size).toEqual([777, 666]) + expect(serialized.flags).toEqual({ collapsed: true }) + }) + + test('does not invent a recorded size on collapse when the file recorded none', () => { + const placeholder = createPlaceholder() + delete (placeholder.last_serialization as Partial).size + new LGraph().add(placeholder) + placeholder.setSize([777, 666]) + placeholder.collapse() + + expect(placeholder.serialize()).not.toHaveProperty('size') + }) + + test('restores the recorded size when expanding, rather than serializing the collapsed card measurement', () => { + const placeholder = createPlaceholder({ flags: { collapsed: false } }) + new LGraph().add(placeholder) + placeholder.setSize([777, 666]) + placeholder.collapse() + // Simulate the Vue nodes mode race: `size` still measures the collapsed + // card immediately after expanding, before the next ResizeObserver tick + // lands the real measurement. + placeholder.setSize([80, 30]) + placeholder.collapse() + + expect(placeholder.last_serialization?.size).toEqual([777, 666]) + expect(placeholder.serialize().size).toEqual([777, 666]) + }) + + test('keeps the recorded size through an expand/collapse pair made before the card is re-measured', () => { + const placeholder = createPlaceholder({ flags: { collapsed: false } }) + new LGraph().add(placeholder) + placeholder.setSize([777, 666]) + placeholder.collapse() + placeholder.setSize([80, 30]) + placeholder.collapse() + placeholder.collapse() + + expect(placeholder.serialize().size).toEqual([777, 666]) + }) + + test('replaces the recorded serialization rather than mutating the object it was configured from', () => { + const placeholder = createPlaceholder({ flags: { collapsed: false } }) + const configuredFrom = placeholder.last_serialization! + new LGraph().add(placeholder) + placeholder.setSize([777, 666]) + placeholder.collapse() + + expect(configuredFrom.size).toEqual([140, 60]) + expect(placeholder.last_serialization?.size).toEqual([777, 666]) + }) + + test('carries a live pin through', () => { + const placeholder = createPlaceholder() + new LGraph().add(placeholder) + placeholder.pin(true) + + expect(placeholder.serialize().flags).toEqual({ pinned: true }) + }) + + test('carries a live rename through', () => { + const placeholder = createPlaceholder({ title: 'Recorded Title' }) + placeholder.title = 'renamed' + + expect(placeholder.serialize().title).toEqual('renamed') + }) + + test('carries a live recolour through, over the recorded colours', () => { + const placeholder = createPlaceholder({ + color: '#111', + bgcolor: '#222' + }) + placeholder.setColorOption(LGraphCanvas.node_colors.red) + + const serialized = placeholder.serialize() + + expect(serialized.color).toEqual(LGraphCanvas.node_colors.red.color) + expect(serialized.bgcolor).toEqual(LGraphCanvas.node_colors.red.bgcolor) + }) + + test('drops the recorded colours when the live colour is cleared', () => { + const placeholder = createPlaceholder({ + color: '#111', + bgcolor: '#222' + }) + placeholder.setColorOption(null) + + const serialized = placeholder.serialize() + + expect(serialized).not.toHaveProperty('color') + expect(serialized).not.toHaveProperty('bgcolor') + }) + + test('carries a live shape and box colour through, over the recorded ones', () => { + const placeholder = createPlaceholder({ + shape: RenderShape.BOX, + boxcolor: '#333' + }) + placeholder.shape = RenderShape.CARD + placeholder.boxcolor = '#444' + + const serialized = placeholder.serialize() + + expect(serialized.shape).toEqual(RenderShape.CARD) + expect(serialized.boxcolor).toEqual('#444') + }) + + test('round-trips an untouched placeholder that recorded decorations', () => { + const recorded = { + title: 'Recorded Title', + flags: { collapsed: true, pinned: true }, + color: '#111', + bgcolor: '#222', + boxcolor: '#333', + shape: RenderShape.CARD + } + const serialized = createPlaceholder(recorded).serialize() + + expect(serialized).toMatchObject(recorded) + }) + + test('round-trips an untouched placeholder that recorded no decorations', () => { + const serialized = createPlaceholder().serialize() + + expect(serialized).not.toHaveProperty('title') + expect(serialized).not.toHaveProperty('color') + expect(serialized).not.toHaveProperty('bgcolor') + expect(serialized.flags).toEqual({}) + }) + + test('emits empty flags when the recorded serialization has none, since the schema requires the key', () => { + const placeholder = createPlaceholder() + delete (placeholder.last_serialization as Partial).flags + + expect(placeholder.serialize().flags).toEqual({}) + }) + }) + test('should configure inputs correctly', () => { const node = new LGraphNode('TestNode') node.configure( diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 07026c41fef..9f29ecc1f1e 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -1,3 +1,4 @@ +import { omit, pickBy } from 'es-toolkit' import { toValue } from 'vue' import { @@ -149,6 +150,18 @@ import type { WidgetTypeMap } from './widgets/widgetMap' export type { NodeProperty } from '@/types/nodeState' +/** + * Serialised keys a missing-node placeholder mirrors from the live node rather + * than replaying from the file it was loaded with. + */ +const PLACEHOLDER_LIVE_DECORATIONS = [ + 'title', + 'color', + 'bgcolor', + 'boxcolor', + 'shape' +] as const satisfies readonly (keyof ISerialisedNode)[] + interface INodePropertyInfo { name?: string type?: string @@ -1193,6 +1206,15 @@ export class LGraphNode } finally { useWidgetValueStore().clearNodeWidgetRestoration(graphId, this.id) } + + // The missing-node error UI just added placeholder widgets via + // `onConfigure` above, which auto-expands the node to fit them + // (`addWidget` -> `expandToFitContent`). A placeholder has no + // definition to re-derive a real size from, so the recorded size — + // the sole record `serialize()` has of it — is restored afterward. + if (this.constructor === LGraphNode && info.size) { + this.size = [info.size[0], info.size[1]] + } } /** @@ -1216,12 +1238,47 @@ export class LGraphNode } // special case for when there were errors - if (this.constructor === LGraphNode && state.lastSerialization) + if (this.constructor === LGraphNode && state.lastSerialization) { + // Only an expanded placeholder whose file recorded a size has a live + // size worth keeping. Vue nodes mode drops the CSS width/height floors + // on collapse, so a collapsed node's `size` measures the collapsed card + // rather than the expanded geometry a reload has to restore — which + // `collapse()` has already captured into `last_serialization`. With no + // recorded size there is no definition to compute a real one from, so + // keeping the live size would invent a dimension the file never had. + const carriesLiveSize = + state.lastSerialization.size != null && !state.flags.collapsed + + // The live node owns its decorations, so the file's recorded values are + // dropped before whatever is set now is re-applied: skipping them + // instead would let a cleared decoration resurrect the recorded one. + // `title` mirrors the normal path's guard against the class default. + const liveDecorations = pickBy( + { + title: + state.title === this.constructor.title ? undefined : state.title, + color: state.color, + bgcolor: state.bgcolor, + boxcolor: state.boxcolor, + shape: state.shape + }, + Boolean + ) + return { - ...LiteGraph.cloneObject(state.lastSerialization), + ...omit( + LiteGraph.cloneObject(state.lastSerialization), + PLACEHOLDER_LIVE_DECORATIONS + ), mode: o.mode, - pos: o.pos + pos: o.pos, + // Required by the workflow schema, so an empty object rather than a + // missing key represents "no flags". + flags: o.flags ?? {}, + ...(carriesLiveSize && { size: o.size }), + ...liveDecorations } + } if (state.inputs) o.inputs = state.inputs.map((input, i) => @@ -3752,6 +3809,37 @@ export class LGraphNode if (!this.collapsible && !force) return if (!this.graph) throw new NullGraphError() this.graph.incrementVersion() + + // A missing-node placeholder has no definition to re-derive an expanded + // size from, so `last_serialization.size` is the sole record of it and has + // to track the live node in BOTH directions. Both halves run before the + // flag flips, because in Vue nodes mode `size` is a DOM measurement of + // whichever card is currently rendered and the other card's measurement + // only lands a frame later: + // collapsing captures `size` while it still measures the expanded card; + // expanding restores `size`, which still measures the collapsed card, so + // that neither a serialize() inside that window nor an immediate second + // collapse can substitute the collapsed dimensions for the recorded ones. + // A recorded serialization without a size still gets none, matching + // `serialize()`. + const recordedSerialization = this.last_serialization + if ( + this.constructor === LGraphNode && + recordedSerialization?.size != null + ) { + if (this.flags.collapsed) { + this.size = recordedSerialization.size + } else { + // Replaced rather than mutated in place: `LGraph.configure()` assigns + // `last_serialization` by reference straight out of the caller's + // workflow data, which this node does not own. + this.last_serialization = { + ...recordedSerialization, + size: [this.size[0], this.size[1]] + } + } + } + this.flags.collapsed = !this.flags.collapsed this.setDirtyCanvas(true, true) } diff --git a/src/lib/litegraph/src/__snapshots__/LGraph.test.ts.snap b/src/lib/litegraph/src/__snapshots__/LGraph.test.ts.snap index 836ad17563b..48d1df305b5 100644 --- a/src/lib/litegraph/src/__snapshots__/LGraph.test.ts.snap +++ b/src/lib/litegraph/src/__snapshots__/LGraph.test.ts.snap @@ -28,6 +28,7 @@ exports[`LGraph > supports schema v0.4 graphs > oldSchemaGraph 1`] = ` "links": [], "nodes": [ { + "flags": {}, "id": 1, "mode": 0, "pos": [ diff --git a/src/platform/nodeReplacement/useNodeReplacement.test.ts b/src/platform/nodeReplacement/useNodeReplacement.test.ts index 2d15b166b73..22eca396b0b 100644 --- a/src/platform/nodeReplacement/useNodeReplacement.test.ts +++ b/src/platform/nodeReplacement/useNodeReplacement.test.ts @@ -7,9 +7,14 @@ import { canTransferReplacementOwnership, transferReplacementOwnership } from '@/core/graph/nodeShell/nodeShellState' -import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' -import { NodeSlotType } from '@/lib/litegraph/src/types/globalEnums' +import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' +import type { Point, Size } from '@/lib/litegraph/src/interfaces' +import type { LGraph } from '@/lib/litegraph/src/litegraph' +import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' +import { + NodeSlotType, + RenderShape +} from '@/lib/litegraph/src/types/globalEnums' import type { PendingWarnings } from '@/platform/workflow/management/stores/comfyWorkflow' import { useLinkStore } from '@/stores/linkStore' import { usePreviewExposureStore } from '@/stores/previewExposureStore' @@ -34,7 +39,8 @@ vi.mock('@/lib/litegraph/src/litegraph', async (importOriginal) => { } }) -vi.mock('@/core/graph/nodeShell/nodeShellState', () => ({ +vi.mock('@/core/graph/nodeShell/nodeShellState', async (importOriginal) => ({ + ...(await importOriginal>()), canTransferReplacementOwnership: vi.fn(() => true), transferReplacementOwnership: vi.fn( (node: LGraphNode, replacement: LGraphNode) => { @@ -187,43 +193,30 @@ function createPlaceholderNode( outputs: { name: string; links: number[] | null }[] = [], graph?: LGraph ): LGraphNode { - return fromAny({ + const lastSerialization = fromAny({ id, type, - pos: [100, 200], - size: [200, 100], + pos: [100, 200] satisfies Point, + size: [200, 100] satisfies Size, + flags: {}, order: 0, mode: 0, - flags: {}, - has_errors: true, - last_serialization: { - id, - type, - pos: [100, 200], - size: [200, 100], - flags: {}, - order: 0, - mode: 0, - inputs: inputs.map((i) => ({ ...i, type: 'IMAGE' })), - outputs: outputs.map((o) => ({ ...o, type: 'IMAGE' })), - widgets_values: [] - }, inputs: inputs.map((i) => ({ ...i, type: 'IMAGE' })), outputs: outputs.map((o) => ({ ...o, type: 'IMAGE' })), - graph: graph ?? null, - serialize: vi.fn(() => ({ - id, - type, - pos: [100, 200], - size: [200, 100], - flags: {}, - order: 0, - mode: 0, - inputs: inputs.map((i) => ({ ...i, type: 'IMAGE' })), - outputs: outputs.map((o) => ({ ...o, type: 'IMAGE' })), - widgets_values: [] - })) + widgets_values: [] }) + + // A real node, recorded and configured from one object the way + // `LGraph.configure` builds a placeholder for an unregistered type, so these + // tests exercise the production `serialize()` placeholder branch rather than + // a copy of it. + const node = new LGraphNode('', type) + node.last_serialization = lastSerialization + node.has_errors = true + node.configure(lastSerialization) + node.graph = graph ?? null + + return node } function createNewNode( @@ -307,7 +300,7 @@ describe('useNodeReplacement', () => { expect(result).toEqual(['Load3DAnimation']) expect(newNode.configure).not.toHaveBeenCalled() - expect(newNode.id).toBe(1) + expect(newNode.id).toBe(toNodeId(1)) expect(newNode.has_errors).toBe(false) }) @@ -771,7 +764,7 @@ describe('useNodeReplacement', () => { ]) expect(newNode.widgets![0].value).toBe(512) - expect(setNodeId).toHaveBeenCalledWith(1) + expect(setNodeId).toHaveBeenCalledWith(toNodeId(1)) }) it('should skip replacement when new node type is not registered', () => { @@ -931,14 +924,16 @@ describe('useNodeReplacement', () => { }) ]) - expect(newNode.id).toBe(42) + expect(newNode.id).toBe(toNodeId(42)) expect(replacementGeometryBeforeTransfer).toEqual([ { pos: [0, 0], size: [100, 50] } ]) expect(newNode.pos).toEqual([300, 400]) expect(newNode.size).toEqual([250, 150]) expect(newNode.order).toBe(6) - expect(placeholder.order).toBe(6) + // Order is graph-scoped, and replacement detaches the placeholder, so + // the handover is complete rather than shared. + expect(placeholder.graph).toBeNull() expect(graph._nodes[0]).toBe(newNode) expect(placeholder.onRemoved).toHaveBeenCalledOnce() }) @@ -1085,7 +1080,7 @@ describe('useNodeReplacement', () => { ]) // Default mapping transfers connections and widget values by name - expect(newNode.id).toBe(13) + expect(newNode.id).toBe(toNodeId(13)) expect(link.target_id).toBe(toNodeId(13)) expect(link.target_slot).toBe(0) expect(outLink.origin_id).toBe(toNodeId(13)) @@ -1752,5 +1747,98 @@ describe('useNodeReplacement', () => { expect(dispatch).toHaveBeenCalledWith('node:added', { node: newNode }) }) + + it('carries the live title and decorations of a renamed placeholder onto the replacement', () => { + const placeholder = createPlaceholderNode(1, 'OldType') + Object.assign(placeholder.last_serialization!, { + title: 'Title From File', + color: '#000000', + bgcolor: '#111111', + boxcolor: '#222222', + shape: RenderShape.BOX + }) + Object.assign(placeholder, { + title: 'My Rename', + color: '#123456', + bgcolor: '#654321', + boxcolor: '#abcdef', + shape: RenderShape.ROUND + }) + const graph = createMockGraph([placeholder]) + placeholder.graph = graph + Object.assign(app, { rootGraph: graph }) + + vi.mocked(collectAllNodes).mockReturnValue([placeholder]) + + const newNode = createNewNode() + vi.mocked(LiteGraph.createNode).mockReturnValue(newNode) + + const { replaceNodesInPlace } = useNodeReplacement() + replaceNodesInPlace([ + makeMissingNodeType('OldType', { + new_node_id: 'NewType', + old_node_id: 'OldType', + old_widget_ids: null, + input_mapping: null, + output_mapping: null + }) + ]) + + expect(newNode.title).toBe('My Rename') + expect(newNode.color).toBe('#123456') + expect(newNode.bgcolor).toBe('#654321') + expect(newNode.boxcolor).toBe('#abcdef') + expect(newNode.shape).toBe(RenderShape.ROUND) + }) + + it('does not resurrect a file decoration the placeholder no longer carries', () => { + const placeholder = createPlaceholderNode(1, 'OldType') + Object.assign(placeholder.last_serialization!, { + title: 'Title From File', + color: '#000000', + bgcolor: '#111111', + boxcolor: '#222222', + shape: RenderShape.BOX + }) + Object.assign(placeholder, { + title: undefined, + color: undefined, + bgcolor: '', + boxcolor: undefined, + shape: undefined + }) + const graph = createMockGraph([placeholder]) + placeholder.graph = graph + Object.assign(app, { rootGraph: graph }) + + vi.mocked(collectAllNodes).mockReturnValue([placeholder]) + + const newNode = createNewNode() + Object.assign(newNode, { + title: 'New Node', + color: undefined, + bgcolor: undefined, + boxcolor: undefined, + shape: undefined + }) + vi.mocked(LiteGraph.createNode).mockReturnValue(newNode) + + const { replaceNodesInPlace } = useNodeReplacement() + replaceNodesInPlace([ + makeMissingNodeType('OldType', { + new_node_id: 'NewType', + old_node_id: 'OldType', + old_widget_ids: null, + input_mapping: null, + output_mapping: null + }) + ]) + + expect(newNode.title).toBe('New Node') + expect(newNode.color).toBeUndefined() + expect(newNode.bgcolor).toBeUndefined() + expect(newNode.boxcolor).toBeUndefined() + expect(newNode.shape).toBeUndefined() + }) }) }) diff --git a/src/platform/nodeReplacement/useNodeReplacement.ts b/src/platform/nodeReplacement/useNodeReplacement.ts index e906a5226ff..f76ad6bf885 100644 --- a/src/platform/nodeReplacement/useNodeReplacement.ts +++ b/src/platform/nodeReplacement/useNodeReplacement.ts @@ -240,7 +240,8 @@ function replaceWithMapping( newNode: LGraphNode, replacement: NodeReplacement, nodeGraph: LGraph, - idx: number + idx: number, + serialized: ISerialisedNode ): boolean { const order = node.order newNode.id = node.id @@ -257,8 +258,11 @@ function replaceWithMapping( return false } - const serialized = node.last_serialization ?? node.serialize() if (serialized.title != null) newNode.title = serialized.title + if (serialized.color != null) newNode.color = serialized.color + if (serialized.bgcolor != null) newNode.bgcolor = serialized.bgcolor + if (serialized.boxcolor != null) newNode.boxcolor = serialized.boxcolor + if (serialized.shape != null) newNode.shape = serialized.shape if (serialized.properties) { newNode.properties = { ...serialized.properties } if ('Node name for S&R' in newNode.properties) { @@ -473,21 +477,25 @@ export function useNodeReplacement() { replacement.input_mapping != null || replacement.output_mapping != null + // Snapshot the live node before any graph mutation: a placeholder the + // user renamed or recoloured carries those edits only on serialize() + // output, and one snapshot keeps the mapping and the value transfer + // reading the same state. + const serialized = node.serialize() + const effectiveReplacement = hasMapping ? replacement : { ...replacement, - ...generateDefaultMapping( - node.last_serialization ?? node.serialize(), - newNode - ) + ...generateDefaultMapping(serialized, newNode) } const replaced = replaceWithMapping( node, newNode, effectiveReplacement, nodeGraph, - idx + idx, + serialized ) if (!replaced) { replacementFailed = true