diff --git a/js/text.js b/js/text.js index 414303d..0bfe461 100644 --- a/js/text.js +++ b/js/text.js @@ -1,5 +1,5 @@ import {app } from "../../scripts/app.js"; -import { log, dynamic_connection, findWidgetByName, toggleWidget } from './ulits.js' +import { log, dynamic_connection, findWidgetByName, toggleWidget } from './utils.js' app.registerExtension({ name: "TextTranslation.text", @@ -25,7 +25,7 @@ function text_concatenate_widget(nodeType, nodeData, app) { const onNodeCreated = nodeType.prototype.onNodeCreated; nodeType.prototype.onNodeCreated = function () { const onc = onNodeCreated?.apply(this, arguments); - this.addInput(`${input_name}`, "STRING"); + this.addInput(`${input_name}1`, "STRING"); return onc; }; diff --git a/js/ulits.js b/js/utils.js similarity index 51% rename from js/ulits.js rename to js/utils.js index 60f0230..a6a2420 100644 --- a/js/ulits.js +++ b/js/utils.js @@ -40,41 +40,53 @@ export const dynamic_connection = ( connectionType = 'PSDLAYER' ) => { - if (!connected && (node.inputs.length > 1)) { - const stackTrace = new Error().stack; - for (let i = 0; i < node.inputs.length -1; i++) { - if(!stackTrace.includes('LGraphNode.prototype.connect') && - !stackTrace.includes('LGraphNode.connect') && - !stackTrace.includes('loadGraphData') && - !node.inputs[i].link) { - node.removeInput(i); - } + if (!node._isRestoring) { + node._dynamicInputs = new Set(); + } + + const removeUnusedInputs = () => { + for (let i = node.inputs.length - 1; i >= 0; i--) { + const input = node.inputs[i]; + const isDynamic = node._dynamicInputs?.has(input.name); + + if (input.link || (isDynamic && node._isRestoring)) { + continue; } - } - - let last_slot = node.inputs[node.inputs.length - 1]; - if (last_slot.link != undefined) { - node.addInput(`${connectionPrefix}`, connectionType); - } - - if (connected && (node.inputs.length > 1)) { - const stackTrace = new Error().stack; - for (let i = 0; i < node.inputs.length -1; i++) { - if(!stackTrace.includes('LGraphNode.prototype.connect') && - !stackTrace.includes('LGraphNode.connect') && - !stackTrace.includes('loadGraphData') && - !node.inputs[i].link) { - node.removeInput(i); - } + + if (isDynamic) { + node.removeInput(i); + node._dynamicInputs.delete(input.name); } + } + }; + + const prevRestoring = node._isRestoring; + if (node._isRestoring) { + node._dynamicInputs = new Set( + node.inputs + .filter((input) => input.name.startsWith(connectionPrefix)) + .map((input) => input.name) + ); } - - if ((node.inputs.length > 1)) { - for (let i = 0; i < node.inputs.length -1; i++) { - node.inputs[i].name = `${connectionPrefix}${i + 1}` - node.inputs[i].label = `${connectionPrefix}${i + 1}` - } + + removeUnusedInputs(); + + const lastInput = node.inputs[node.inputs.length - 1]; + if (lastInput?.link && !node._isRestoring) { + const newIndex = node.inputs.length + 1; + const newName = `${connectionPrefix}${newIndex}`; + const newInput = node.addInput(newName, connectionType); + node._dynamicInputs.add(newName); } + + let validIndex = 1; + node.inputs.forEach((input) => { + if (input.name.startsWith(connectionPrefix)) { + input.name = `${connectionPrefix}${validIndex++}`; + input.label = input.name; + } + }); + node._isRestoring = prevRestoring; } export const log = (...args) => {