Skip to content

Commit 58eab62

Browse files
AustinMrozDrJKL
authored andcommitted
Implement input splicing
1 parent b9cd1bd commit 58eab62

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/extensions/core/dynamicCombo.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ function COMFY_DYNAMICCOMBO_V3(
4747

4848
const insertionPoint = node.widgets.findIndex((w) => w === widget) + 1
4949
const startingLength = node.widgets.length
50+
const inputInsertionPoint =
51+
node.inputs.findIndex((i) => i.name === widget.name) + 1
5052
const startingInputLength = node.inputs.length
5153
if (insertionPoint === 0)
5254
throw new Error("Dynamic widget doesn't exist on node")
@@ -72,11 +74,24 @@ function COMFY_DYNAMICCOMBO_V3(
7274
for (const addedWidget of addedWidgets) {
7375
addedWidget.name = `${widget.name}.${addedWidget.name}`
7476
}
77+
node.widgets.splice(insertionPoint, 0, ...addedWidgets)
78+
node.size[1] = node.computeSize([...node.size])[1]
7579
for (const input of node.inputs.slice(startingInputLength)) {
7680
input.name = `${widget.name}.${input.name}`
81+
if (input.widget)
82+
input.widget.name = `${widget.name}.${input.widget.name}`
7783
}
78-
node.widgets.splice(insertionPoint, 0, ...addedWidgets)
79-
node.computeSize(node.size)
84+
if (inputInsertionPoint === 0) {
85+
if (
86+
addedWidgets.length === 0 &&
87+
node.inputs.length !== startingInputLength
88+
)
89+
//input is inputOnly, but lacks an insertion point
90+
throw new Error('Failed to find input socket for ' + widget.name)
91+
return
92+
}
93+
const addedInputs = node.spliceInputs(startingInputLength)
94+
node.spliceInputs(inputInsertionPoint, 0, ...addedInputs)
8095
}
8196
//A little hacky, but onConfigure won't work.
8297
//It fires too late and is overly disruptive

src/lib/litegraph/src/LGraphNode.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,22 @@ export class LGraphNode
16481648
this.onInputRemoved?.(slot, slot_info[0])
16491649
this.setDirtyCanvas(true, true)
16501650
}
1651+
spliceInputs(
1652+
startIndex: number,
1653+
deleteCount = -1,
1654+
...toAdd: INodeInputSlot[]
1655+
): INodeInputSlot[] {
1656+
if (deleteCount === -1) deleteCount = this.inputs.length - startIndex
1657+
1658+
const lengthDelta = toAdd.length - deleteCount
1659+
const ret = this.inputs.splice(startIndex, deleteCount, ...toAdd)
1660+
for (const input of this.inputs.slice(startIndex + toAdd.length)) {
1661+
const link = input.link && this.graph?.links?.get(input.link)
1662+
if (!link) continue
1663+
link.target_slot += lengthDelta
1664+
}
1665+
return ret
1666+
}
16511667

16521668
/**
16531669
* computes the minimum size of a node according to its inputs and output slots

0 commit comments

Comments
 (0)