File size: 2,978 Bytes
c6535db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {app} from "/scripts/app.js";

app.registerExtension({
    name: "ComfyLiterals.OperationNode",
    nodeCreated(node, app) {
        if (node['comfyClass'] === 'Operation') {
            const onAdded = node.onAdded
            node.onAdded = function (graph) {
                console.log("OperationNode onAdded")
                const firstCallbackResp = onAdded ? onAdded.apply(this, arguments) : undefined;

                /**
                 * @type {Record<string, INodeInputSlot>}
                 */
                const inputCache = {
                    "A": node.inputs[1],
                    "B": node.inputs[3]
                }

                if (this.widgets_values) {
                    const aType = this.widgets_values[0]
                    const bType = this.widgets_values[1]

                    // [IntA, FloatA, IntB, FloatB]
                    const aIdxToDelete = aType === "INT" ? 1 : 0
                    // [*A, IntB, FloatB]
                    const bIdxToDelete = bType === "INT" ? 3 : 1

                    inputCache["A"] = node.inputs[aIdxToDelete]
                    this.removeInput(aIdxToDelete)
                    inputCache["B"] = node.inputs[bIdxToDelete]
                    this.removeInput(bIdxToDelete)
                } else {
                    // Nodes being restored/pasted don't have widget_values
                    // Node has 4 inputs(IntA, FloatA, IntB, FloatB)
                    // Remove both float inputs, Float B moves to index 2 after Float A is removed
                    this.removeInput(1)
                    this.removeInput(2)
                }

                // Add a toggle widget to the node
                this.widgets[0].callback = function (v, canvas, node) {
                    addInputAtIndex(node, inputCache["A"], 0)
                    inputCache["A"] = node.inputs[1]
                    node.removeInput(1)
                }
                this.widgets[1].callback = function (v, canvas, node) {
                    addInputAtIndex(node, inputCache["B"], 2)
                    inputCache["B"] = node.inputs[1]
                    node.removeInput(1)
                }
            }
        }
    }
})

/**
 * Adds an input to a node at the given index.
 * @param node {LGraphNode}
 * @param input {INodeInputSlot}
 * @param index {number}
 * @returns {INodeInputSlot}
 */
function addInputAtIndex(node, input, index) {
    if (!node.inputs) {
        node.inputs = [];
    }

    if (index > node.inputs.length) {
        console.warn("LiteGraph: Warning adding port index: " + index + " of node " + node.id + ", it doesnt have so many inputs");
        node.inputs.push(input);
    } else {
        node.inputs.splice(index, 0, input);
    }
    if (node.onInputAdded) {
        node.onInputAdded(input);
    }
    node.setSize(node.computeSize());
    LiteGraph.registerNodeAndSlotType(node, input.type || 0);

    node.setDirtyCanvas(true, true);
    return input;
}