File size: 7,421 Bytes
681fa96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { is_UEnode } from "./use_everywhere_utilities.js";
import { ComfyWidgets} from "../../scripts/widgets.js";
import { app } from "../../scripts/app.js";
import { LinkRenderController } from "./use_everywhere_ui.js";

function update_picklist(node, inputname) {
    const d = document.getElementById("uedynamiclist");
    while (d.firstChild) { d.removeChild(d.lastChild); };
    let options = [];
    if (inputname=="title_regex") { options = LinkRenderController.instance().ue_list?.all_nodes_with_unmatched_input(node.input_type[0]); }
    else if (inputname=="input_regex") { options = LinkRenderController.instance().ue_list?.all_unmatched_input_names(node.input_type[0]); }
    else if (inputname=="group_regex") { options = LinkRenderController.instance().ue_list?.all_group_names(node.input_type[0]); }
    options.forEach((option) => {
        const theOption = document.createElement("option");
        theOption.setAttribute("value", option);
        d.appendChild(theOption)
    })
}

function intersect(a, b) {
	const x = Math.max(a.x, b.x);
	const num1 = Math.min(a.x + a.width, b.x + b.width);
	const y = Math.max(a.y, b.y);
	const num2 = Math.min(a.y + a.height, b.y + b.height);
	if (num1 >= x && num2 >= y) return [x, y, num1 - x, num2 - y];
	else return null;
}

function union(a,b) {
    if (!b) return a;
    if (!a) return b;
    const x = Math.min(a.x, b.x);
    const y = Math.min(a.y,b.y);
    const width = Math.max(a.x+a.width, b.x+b.width) - x;
    const height = Math.max(a.y+a.height, b.y+b.height) - x;
    return { x:x, y:y, width:width, height:height };
}

function getClipPath(node, element) {
    const scale = app.canvas.ds.scale;
    const widgetRect = element.getBoundingClientRect();
    var onTopOfMe = false;
    var clip = null;
    app.graph._nodes.forEach((other_node) => {
        if (other_node.id == node.id) {
            onTopOfMe = true;
        }
        else if (onTopOfMe) {
            const MARGIN = other_node.is_selected ? 7 : 2;
            const bounding = other_node.getBounding();
            const intersection = intersect(
                { x: widgetRect.x / scale, y: widgetRect.y / scale, width: widgetRect.width / scale, height: widgetRect.height / scale },
                {
                    x: other_node.pos[0] + app.canvas.ds.offset[0] - MARGIN,
                    y: other_node.pos[1] + app.canvas.ds.offset[1] - LiteGraph.NODE_TITLE_HEIGHT - MARGIN,
                    width: bounding[2] + MARGIN + MARGIN,
                    height: bounding[3] + MARGIN + MARGIN,
                }
            );
            if (intersection) {
                clip = union(clip, { 
                    x : intersection[0] - widgetRect.x / scale, 
                    y : intersection[1] - widgetRect.y / scale,
                    width : intersection[2],
                    height : intersection[3]
                })
                //const newpath = `0% 0%, 0% 100%, ${clipX} 100%, ${clipX} ${clipY}, calc(${clipX} + ${clipWidth}) ${clipY}, calc(${clipX} + ${clipWidth}) calc(${clipY} + ${clipHeight}), ${clipX} calc(${clipY} + ${clipHeight}), ${clipX} 100%, 100% 100%, 100% 0%`;
                //path = path != '' ? `${path}, ${newpath}` : newpath;
            }
        }
    })
    const path = clip ? `polygon(0% 0%, 0% 100%, ${clip.x}px 100%, ${clip.x}px ${clip.y}px, ${clip.x + clip.width}px ${clip.y}px, ${clip.x + clip.width}px ${clip.y + clip.height}px, ${clip.x}px ${clip.y + clip.height}px, ${clip.x}px 100%, 100% 100%, 100% 0%)` : '';
	return path;
}

function active_text_widget(node, inputname) {
    const label = document.createElement("label");
    label.className = "graphdialog ueprompt";


    const label_text = document.createElement("span");
    label_text.innerText = `${inputname.substring(0,5)} `;
    label_text.className = "ueprompttext";
    label.appendChild(label_text);

    const span = document.createElement("span");
    span.className = "uepromptspan";
    label.appendChild(span);

    const inputEl = document.createElement("input");
    inputEl.setAttribute("type", "text");
    inputEl.className = "uepromptinput";
    span.appendChild(inputEl);
 
    const widget = node.addDOMWidget(inputname, "input", label, {
        getValue() { return inputEl.value; },
        setValue(v) { inputEl.value = v; },
        onDraw(w) { 
            // are we the most recently selected node? 
            if (Object.values(app.canvas.selected_nodes)[0]?.id == node.id) {
                // if so, turn off DOM clipping
                w.element.style.clipPath = null; w.element.style.willChange = null; 
            } else {
                w.element.style.zIndex = 0;
                const p = getClipPath(node, w.element);
                w.element.style.clipPath = p;
                let a;
            }
        }
    });
    //widget.element.hidden = true;
    //widget.element.classList.add("ue_hide")
    widget.element.style.display="block"

    inputEl.onmousedown = function(e) {
        const x = app.canvas.prompt("Value",widget.value,function(v) { this.value = v; }.bind(widget), e, false );
        const input = x.getElementsByClassName("value")[0];
        input.setAttribute("list", "uedynamiclist");
        input.parentNode.style.zIndex = `${parseInt(label.style.zIndex ? label.style.zIndex : '0')+1}`;
        input.addEventListener("input", function (v) {
            widget.value = this.value;
            LinkRenderController.instance().mark_link_list_outdated();
            app.graph.setDirtyCanvas(true,true);
        }.bind(input));
        update_picklist(node, inputname);
        e.stopImmediatePropagation();
    }
    
    widget.computeSize = function (parent_width) {
        return parent_width ? [parent_width, 27] : [400, 20];
    }
    
    inputEl.addEventListener("focus", () => {
        if (inputEl.value==".*") inputEl.value = "";
    });
    
    widget.onModeChange = function (mode) { 
        label.style.opacity = mode==4 ? 0.2 : 1.0;
    }

    node.loaded_when_collapsed = function() {
        node.widgets?.forEach((widget) => {
            if (widget.element) {
                widget.element.hidden = true;
                widget.element.classList.add("ue_hide")
            }
        })
    }

    return { widget };
}

function activate(node, widget) {
    if (node.flags?.collapsed) return;
    widget.element.hidden = false;
    widget.element.classList.remove("ue_hide")
}

function add_autoprompts() {
    const STRING = ComfyWidgets.STRING;
    ComfyWidgets.STRING = function (node, inputName, inputData, app) {
        if (true || !is_UEnode(node) || !inputName?.includes("regex") || !app.ui.settings.getSettingValue('AE.autoprompt', true)) {
            return STRING.apply(this, arguments);
        }
        const atw = active_text_widget(node, inputName);
        const orig_onAdded = node.onAdded;
        node.onAdded = function () {
            orig_onAdded?.apply(this, arguments);
            activate(node, atw.widget);
        }
        return atw;
    }
    const datalist = document.createElement("datalist");
    datalist.id = "uedynamiclist";    
    document.body.append(datalist);
}

function node_added(node) {
    const a = 1;
}


export { add_autoprompts }