Add files via upload

This commit is contained in:
FaberVS
2025-08-08 17:52:52 +03:00
committed by GitHub
parent 4cbca53cda
commit b9fbc5eb3c
6 changed files with 64 additions and 143 deletions
+2 -34
View File
@@ -1,5 +1,4 @@
import { app } from "../../../scripts/app.js";
import { ComfyWidgets } from "../../../scripts/widgets.js";
import { app } from "/scripts/app.js";
// Simple debounce function
function debounce(func, delay) {
@@ -20,9 +19,6 @@ app.registerExtension({
async beforeRegisterNodeDef(nodeType, nodeData, app) {
// Compare with the class name
if (nodeData.name === nodeClassName) {
console.log(`[ModelSwitch] Patching ${nodeClassName}`);
// --- ФУНКЦІЇ ---
function handleModeSwitch(modeWidget, indexWidget, app, value) {
const updateVisibility = (modeValue) => {
if (indexWidget) {
@@ -41,14 +37,11 @@ app.registerExtension({
}
function updateDynamicInputIndexes(node) {
console.log('[ModelSwitch] Оновлення індексів динамічних входів');
const prefix = node._input_prefix || "input_";
let dynamicInputs = node.inputs ? node.inputs.filter(inp => inp.name !== "mode" && inp.name !== "index") : [];
// Перейменовуємо динамічні входи по порядку
for (let i = 0; i < dynamicInputs.length; i++) {
const expectedName = `${prefix}${i + 1}`;
if (dynamicInputs[i].name !== expectedName) {
console.log(`[ModelSwitch] Перейменування входу ${dynamicInputs[i].name} -> ${expectedName}`);
dynamicInputs[i].name = expectedName;
}
}
@@ -81,29 +74,18 @@ app.registerExtension({
return;
}
if (connected) {
console.log(`[ModelSwitch] Вхід "${inputName}" (${index}) підключено`);
if (index === node.last_empty) {
const prefix = node._input_prefix || "input_";
let dynamicInputs = node.inputs.filter(inp => inp.name !== "mode" && inp.name !== "index");
node.addInput(`${prefix}${dynamicInputs.length + 1}`, "*");
console.log('[ModelSwitch] Додаємо ще один порожній динамічний вхід');
updateDynamicInputIndexes(node);
} else {
console.log('[ModelSwitch] Підключено не до останнього вільного входу, новий не додаємо');
}
} else {
console.log(`[ModelSwitch] Вхід "${inputName}" (${index}) відключено`);
// Якщо відключено від останнього підключеного, прибираємо цей вхід, але лише якщо порожніх динамічних входів більше одного
let dynamicInputs = node.inputs.filter(inp => inp.name !== "mode" && inp.name !== "index");
let emptyInputs = dynamicInputs.filter(inp => inp.link == null);
if (index === node.last_connected && emptyInputs.length > 1) {
node.removeInput(index);
console.log('[ModelSwitch] Відключено від останнього підключеного — видаляємо цей вхід');
updateDynamicInputIndexes(node);
} else if (index === node.last_connected) {
console.log('[ModelSwitch] Це останній порожній вхід — не видаляємо');
} else {
console.log('[ModelSwitch] Відключено не від останнього підключеного — нічого не робимо');
}
}
node._updatingInputs = false;
@@ -113,12 +95,7 @@ app.registerExtension({
// --- Віджети ---
const modeWidget = node.widgets.find(w => w.name === "mode");
const indexWidget = node.widgets.find(w => w.name === "index");
if (!modeWidget) {
console.error(`[ModelSwitch] ${nodeClassName}: 'mode' widget not found! Cannot apply dynamic visibility.`);
return;
}
if (!indexWidget) {
console.error(`[ModelSwitch] ${nodeClassName}: 'index' widget not found! Cannot apply dynamic visibility.`);
if (!modeWidget || !indexWidget) {
return;
}
handleModeSwitch(modeWidget, indexWidget, app);
@@ -127,24 +104,17 @@ app.registerExtension({
node._input_prefix = "input_";
const prefix = node._input_prefix;
let dynamicInputs = node.inputs ? node.inputs.filter(inp => inp.name !== "mode" && inp.name !== "index") : [];
console.log('[ModelSwitch] dynamicInputs:', dynamicInputs.map(inp => ({name: inp.name, link: inp.link})));
// --- Якщо входів немає, додаємо перший ---
if (dynamicInputs.length === 0) {
console.log('[ModelSwitch] Входів немає, додаємо перший');
node.addInput(`${prefix}1`, "*");
dynamicInputs = node.inputs.filter(inp => inp.name !== "mode" && inp.name !== "index");
}
let connectedInputs = dynamicInputs.filter(inp => inp.link != null);
let emptyInputs = dynamicInputs.filter(inp => inp.link == null);
console.log('[ModelSwitch] connectedInputs:', connectedInputs);
console.log('[ModelSwitch] emptyInputs:', emptyInputs);
if (emptyInputs.length === 0) {
console.log('[ModelSwitch] Додаємо новий порожній вхід');
node.addInput(`${prefix}${connectedInputs.length + 1}`, "*");
dynamicInputs = node.inputs.filter(inp => inp.name !== "mode" && inp.name !== "index");
} else if (emptyInputs.length > 1) {
console.log('[ModelSwitch] Видаляємо зайві порожні входи');
for (let i = node.inputs.length - 1; i >= 0; i--) {
if (node.inputs[i].name !== "mode" && node.inputs[i].name !== "index" && node.inputs[i].link == null && emptyInputs.length > 1) {
node.removeInput(i);
@@ -153,7 +123,6 @@ app.registerExtension({
}
dynamicInputs = node.inputs.filter(inp => inp.name !== "mode" && inp.name !== "index");
}
console.log('[ModelSwitch] dynamicInputs після додавання/видалення:', dynamicInputs.map(inp => ({name: inp.name, link: inp.link})));
updateDynamicInputIndexes(node);
// --- Зберігаємо попередній стан links ---
node._prevLinks = [];
@@ -175,7 +144,6 @@ app.registerExtension({
const onNodeCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function() {
onNodeCreated?.apply(this, arguments);
console.log(`[ModelSwitch] ${nodeClassName} created`);
setTimeout(() => {
handleNodeCreation(this, app);
}, 500);