|
| 1 | +import { app } from "../../scripts/app.js"; |
| 2 | +import { ComfyWidgets } from '../../scripts/widgets.js'; |
| 3 | +import { isGetter, isSetter, setColorAndBgColor } from "./utils.js"; |
| 4 | + |
| 5 | + |
| 6 | +function setTypeOtherNode(node, type) { |
| 7 | + node.outputs[0].type = type; |
| 8 | +} |
| 9 | + |
| 10 | + |
| 11 | +let isAlertShown = false; |
| 12 | + |
| 13 | +function showAlertWithThrottle(message, delay) { |
| 14 | + if (!isAlertShown) { |
| 15 | + isAlertShown = true; |
| 16 | + alert(message); |
| 17 | + setTimeout(() => isAlertShown = false, delay); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +export class JsSetter { |
| 22 | + defaultVisibility = true; |
| 23 | + serialize_widgets = true; |
| 24 | + constructor() { |
| 25 | + if (!this.properties) { |
| 26 | + this.properties = { |
| 27 | + "previousName": "" |
| 28 | + }; |
| 29 | + } |
| 30 | + this.properties.showOutputText = JsSetter.defaultVisibility; |
| 31 | + |
| 32 | + const node = this; |
| 33 | + |
| 34 | + this.addWidget( |
| 35 | + "text", |
| 36 | + "key", |
| 37 | + '', |
| 38 | + (s, t, u, v, x) => { |
| 39 | + node.validateName(node.graph); |
| 40 | + if(this.widgets[0].value !== ''){ |
| 41 | + this.title = "Set_" + this.widgets[0].value; |
| 42 | + } |
| 43 | + this.update(); |
| 44 | + this.properties.previousName = this.widgets[0].value; |
| 45 | + }, |
| 46 | + {} |
| 47 | + ) |
| 48 | + |
| 49 | + this.addInput("*", "*"); |
| 50 | + this.addOutput("*", '*'); |
| 51 | + |
| 52 | + this.onConnectionsChange = function( |
| 53 | + slotType, //1 = input, 2 = output |
| 54 | + slot, |
| 55 | + isChangeConnect, |
| 56 | + link_info, |
| 57 | + output |
| 58 | + ) { |
| 59 | + //On Disconnect |
| 60 | + if (slotType == 1 && !isChangeConnect) { |
| 61 | + if(this.inputs[slot].name === ''){ |
| 62 | + this.inputs[slot].type = '*'; |
| 63 | + this.inputs[slot].name = '*'; |
| 64 | + this.title = "Set" |
| 65 | + } |
| 66 | + } |
| 67 | + if (slotType == 2 && !isChangeConnect) { |
| 68 | + this.outputs[slot].type = '*'; |
| 69 | + this.outputs[slot].name = '*'; |
| 70 | + |
| 71 | + } |
| 72 | + //On Connect |
| 73 | + if (link_info && node.graph && slotType == 1 && isChangeConnect) { |
| 74 | + const fromNode = node.graph._nodes.find((otherNode) => otherNode.id == link_info.origin_id); |
| 75 | + |
| 76 | + if (fromNode && fromNode.outputs && fromNode.outputs[link_info.origin_slot]) { |
| 77 | + const type = fromNode.outputs[link_info.origin_slot].type; |
| 78 | + |
| 79 | + if (this.title === "Set"){ |
| 80 | + this.title = "Set_" + type; |
| 81 | + } |
| 82 | + if (this.widgets[0].value === '*'){ |
| 83 | + this.widgets[0].value = type |
| 84 | + } |
| 85 | + |
| 86 | + this.validateName(node.graph); |
| 87 | + this.inputs[0].type = type; |
| 88 | + this.inputs[0].name = type; |
| 89 | + |
| 90 | + if (app.ui.settings.getSettingValue("KJNodes.nodeAutoColor")){ |
| 91 | + setColorAndBgColor.call(this, type); |
| 92 | + } |
| 93 | + } else { |
| 94 | + alert("Error: Set node input undefined. Most likely you're missing custom nodes"); |
| 95 | + } |
| 96 | + } |
| 97 | + if (link_info && node.graph && slotType == 2 && isChangeConnect) { |
| 98 | + const fromNode = node.graph._nodes.find((otherNode) => otherNode.id == link_info.origin_id); |
| 99 | + |
| 100 | + if (fromNode && fromNode.inputs && fromNode.inputs[link_info.origin_slot]) { |
| 101 | + const type = fromNode.inputs[link_info.origin_slot].type; |
| 102 | + |
| 103 | + this.outputs[0].type = type; |
| 104 | + this.outputs[0].name = type; |
| 105 | + } else { |
| 106 | + alert("Error: Get Set node output undefined. Most likely you're missing custom nodes"); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + //Update either way |
| 112 | + this.update(); |
| 113 | + } |
| 114 | + |
| 115 | + this.validateName = function(graph) { |
| 116 | + let widgetValue = node.widgets[0].value; |
| 117 | + |
| 118 | + if (widgetValue !== '') { |
| 119 | + let tries = 0; |
| 120 | + const existingValues = new Set(); |
| 121 | + |
| 122 | + graph._nodes.forEach(otherNode => { |
| 123 | + if (otherNode !== this && isSetter(otherNode)) { |
| 124 | + existingValues.add(otherNode.widgets[0].value); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + while (existingValues.has(widgetValue)) { |
| 129 | + widgetValue = node.widgets[0].value + "_" + tries; |
| 130 | + tries++; |
| 131 | + } |
| 132 | + |
| 133 | + node.widgets[0].value = widgetValue; |
| 134 | + this.update(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + this.clone = function () { |
| 139 | + const cloned = JsSetter.prototype.clone.apply(this); |
| 140 | + cloned.inputs[0].name = '*'; |
| 141 | + cloned.inputs[0].type = '*'; |
| 142 | + cloned.value = ''; |
| 143 | + cloned.properties.previousName = ''; |
| 144 | + cloned.size = cloned.computeSize(); |
| 145 | + return cloned; |
| 146 | + }; |
| 147 | + |
| 148 | + this.onAdded = function(graph) { |
| 149 | + this.validateName(graph); |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + this.update = function() { |
| 154 | + if (!node.graph) { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + const getters = this.findGetters(node.graph); |
| 159 | + getters.forEach(getter => { |
| 160 | + if (getter.setType) { |
| 161 | + getter.setType?.(this.inputs[0].type); |
| 162 | + } else { |
| 163 | + setTypeOtherNode(getter, this.inputs[0].type); |
| 164 | + } |
| 165 | + }); |
| 166 | + |
| 167 | + if (this.widgets[0].value) { |
| 168 | + const gettersWithPreviousName = this.findGetters(node.graph, true); |
| 169 | + gettersWithPreviousName.forEach(getter => { |
| 170 | + if (getter.setName) { |
| 171 | + getter.setName(this.widgets[0].value); |
| 172 | + } else { |
| 173 | + getter.widgets[0].value = this.widgets[0].value; |
| 174 | + } |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + const allGetters = node.graph._nodes.filter(otherNode => otherNode.type === "GetNode"); |
| 179 | + allGetters.forEach(otherNode => { |
| 180 | + if (otherNode.setComboValues) { |
| 181 | + otherNode.setComboValues(); |
| 182 | + } |
| 183 | + }); |
| 184 | + } |
| 185 | + |
| 186 | + |
| 187 | + this.findGetters = function(graph, checkForPreviousName) { |
| 188 | + const name = checkForPreviousName ? this.properties.previousName : this.widgets[0].value; |
| 189 | + return graph._nodes.filter(otherNode => isGetter(otherNode) && otherNode.widgets[0].value === name && name !== '' ); |
| 190 | + } |
| 191 | + |
| 192 | + // This node is purely frontend and does not impact the resulting prompt so should not be serialized |
| 193 | + this.isVirtualNode = true; |
| 194 | + } |
| 195 | + |
| 196 | + onRemoved() { |
| 197 | + const allGetters = this.graph._nodes.filter((otherNode) => isGetter(otherNode)); |
| 198 | + allGetters.forEach((otherNode) => { |
| 199 | + if (otherNode.setComboValues) { |
| 200 | + otherNode.setComboValues([this]); |
| 201 | + } |
| 202 | + }) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | +export class JsGetter { |
| 208 | + |
| 209 | + defaultVisibility = true; |
| 210 | + serialize_widgets = true; |
| 211 | + |
| 212 | + constructor() { |
| 213 | + if (!this.properties) { |
| 214 | + this.properties = {}; |
| 215 | + } |
| 216 | + this.properties.showOutputText = JsGetter.defaultVisibility; |
| 217 | + |
| 218 | + const node = this; |
| 219 | + this.addWidget( |
| 220 | + "combo", |
| 221 | + "key", |
| 222 | + "", |
| 223 | + (e) => { |
| 224 | + this.onRename(); |
| 225 | + }, |
| 226 | + { |
| 227 | + values: () => { |
| 228 | + const setterNodes = graph._nodes.filter((otherNode) => isSetter(otherNode)); |
| 229 | + return setterNodes.map((otherNode) => otherNode.widgets[0].value).sort(); |
| 230 | + } |
| 231 | + } |
| 232 | + ) |
| 233 | + |
| 234 | + this.addInput("*", "*"); |
| 235 | + this.addOutput("*", '*'); |
| 236 | + this.onConnectionsChange = function( |
| 237 | + slotType, //0 = output, 1 = input |
| 238 | + slot, //self-explanatory |
| 239 | + isChangeConnect, |
| 240 | + link_info, |
| 241 | + output |
| 242 | + ) { |
| 243 | + // this.validateLinks(); |
| 244 | + } |
| 245 | + |
| 246 | + this.setName = function(name) { |
| 247 | + node.widgets[0].value = name; |
| 248 | + node.onRename(); |
| 249 | + node.serialize(); |
| 250 | + } |
| 251 | + |
| 252 | + this.onRename = function() { |
| 253 | + const setter = this.findSetter(node.graph); |
| 254 | + if (setter) { |
| 255 | + let linkType = (setter.inputs[0].type); |
| 256 | + |
| 257 | + this.setType(linkType); |
| 258 | + this.title = "Get_" + setter.widgets[0].value; |
| 259 | + |
| 260 | + if (app.ui.settings.getSettingValue("KJNodes.nodeAutoColor")){ |
| 261 | + setColorAndBgColor.call(this, linkType); |
| 262 | + } |
| 263 | + |
| 264 | + } else { |
| 265 | + this.setType('*'); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + this.clone = function () { |
| 270 | + const cloned = JsGetter.prototype.clone.apply(this); |
| 271 | + cloned.size = cloned.computeSize(); |
| 272 | + return cloned; |
| 273 | + }; |
| 274 | + |
| 275 | + this.validateLinks = function() { |
| 276 | + if (this.outputs[0].type !== '*' && this.outputs[0].links) { |
| 277 | + this.outputs[0].links.filter(linkId => { |
| 278 | + const link = node.graph.links[linkId]; |
| 279 | + return link && (link.type !== this.outputs[0].type && link.type !== '*'); |
| 280 | + }).forEach(linkId => { |
| 281 | + node.graph.removeLink(linkId); |
| 282 | + }); |
| 283 | + } |
| 284 | + }; |
| 285 | + |
| 286 | + this.setType = function(type) { |
| 287 | + this.outputs[0].name = type; |
| 288 | + this.outputs[0].type = type; |
| 289 | + // this.validateLinks(); |
| 290 | + } |
| 291 | + |
| 292 | + this.findSetter = function(graph) { |
| 293 | + const name = this.widgets[0].value; |
| 294 | + return graph._nodes.find(otherNode => isSetter(otherNode) && otherNode.widgets[0].value === name && name !== ''); |
| 295 | + }; |
| 296 | + |
| 297 | + // This node is purely frontend and does not impact the resulting prompt so should not be serialized |
| 298 | + this.isVirtualNode = true; |
| 299 | + } |
| 300 | + |
| 301 | + getInputLink(slot) { |
| 302 | + const setter = this.findSetter(this.graph); |
| 303 | + |
| 304 | + if (setter) { |
| 305 | + const slotInfo = setter.inputs[slot]; |
| 306 | + const link = this.graph.links[slotInfo.link]; |
| 307 | + return link; |
| 308 | + } else { |
| 309 | + const errorMessage = "No SetNode found for " + this.widgets[0].value + "(" + this.type + ")"; |
| 310 | + showAlertWithThrottle(errorMessage, 5000); |
| 311 | + throw new Error(errorMessage); |
| 312 | + } |
| 313 | + } |
| 314 | + onAdded(graph) { |
| 315 | + } |
| 316 | +} |
| 317 | + |
| 318 | + |
| 319 | +// app.registerExtension({ |
| 320 | +// name: "JsSetter", |
| 321 | +// registerCustomNodes() { |
| 322 | + |
| 323 | +// LiteGraph.registerNodeType( |
| 324 | +// "JsSetter", |
| 325 | +// Object.assign(JsSetter, { |
| 326 | +// title: "Setter (adv)", |
| 327 | +// }) |
| 328 | +// ); |
| 329 | + |
| 330 | +// JsSetter.category = "komojini"; |
| 331 | +// }, |
| 332 | +// }); |
| 333 | + |
| 334 | + |
| 335 | +// app.registerExtension({ |
| 336 | +// name: "JsGetter", |
| 337 | +// registerCustomNodes() { |
| 338 | +// LiteGraph.registerNodeType( |
| 339 | +// "JsGetter", |
| 340 | +// Object.assign(JsGetter, { |
| 341 | +// title: "Getter (adv)", |
| 342 | +// }) |
| 343 | +// ); |
| 344 | + |
| 345 | +// JsGetter.category = "komojini"; |
| 346 | +// }, |
| 347 | +// }); |
0 commit comments