From 18ec4c77ae796b8696b5eff379c2e411d6e67dad Mon Sep 17 00:00:00 2001 From: MakkiShizu <142442886+MakkiShizu@users.noreply.github.com> Date: Sun, 27 Apr 2025 16:48:15 +0800 Subject: [PATCH 1/2] Refactored `dynamic_connection` to fix text_concatenate_widget --- js/text.js | 2 +- js/ulits.js | 74 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/js/text.js b/js/text.js index 414303d..84a1d38 100644 --- a/js/text.js +++ b/js/text.js @@ -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/ulits.js index 60f0230..a6a2420 100644 --- a/js/ulits.js +++ b/js/ulits.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) => { From 4af5b4c0aa25da7f0f190f1836b529f1ecb4ccc8 Mon Sep 17 00:00:00 2001 From: MakkiShizu <142442886+MakkiShizu@users.noreply.github.com> Date: Sun, 27 Apr 2025 16:56:58 +0800 Subject: [PATCH 2/2] renamed --- js/text.js | 2 +- js/{ulits.js => utils.js} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename js/{ulits.js => utils.js} (100%) diff --git a/js/text.js b/js/text.js index 84a1d38..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", diff --git a/js/ulits.js b/js/utils.js similarity index 100% rename from js/ulits.js rename to js/utils.js