Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 231 additions & 1 deletion src/lib/litegraph/src/LGraphNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
LGraph,
LLink,
NodeInputSlot,
NodeOutputSlot
NodeOutputSlot,
RenderShape
} from '@/lib/litegraph/src/litegraph'

import { test } from './__fixtures__/testExtensions'
Expand Down Expand Up @@ -139,6 +140,235 @@ describe('LGraphNode', () => {
expect(Array.from(node.size)).toEqual([70, 80])
})

describe('missing-node placeholder serialization', () => {
function createPlaceholder(
overrides: Partial<ISerialisedNode> = {}
): 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<ISerialisedNode>).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<ISerialisedNode>).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<ISerialisedNode>).flags

expect(placeholder.serialize().flags).toEqual({})
})
})

test('should configure inputs correctly', () => {
const node = new LGraphNode('TestNode')
node.configure(
Expand Down
94 changes: 91 additions & 3 deletions src/lib/litegraph/src/LGraphNode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { omit, pickBy } from 'es-toolkit'
import { toValue } from 'vue'

import {
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this JSDoc. PLACEHOLDER_LIVE_DECORATIONS and its use in the omit operation already state exactly which values come from the live placeholder.

* 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
Expand Down Expand Up @@ -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]]
}
}

/**
Expand All @@ -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,
Comment thread
mattmillerai marked this conversation as resolved.
Resolved
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 }),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
...liveDecorations
}
}

if (state.inputs)
o.inputs = state.inputs.map((input, i) =>
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/litegraph/src/__snapshots__/LGraph.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ exports[`LGraph > supports schema v0.4 graphs > oldSchemaGraph 1`] = `
"links": [],
"nodes": [
{
"flags": {},
"id": 1,
"mode": 0,
"pos": [
Expand Down
Loading
Loading