From 32e78b2babe13c393d5231a458d375d2c7d6e41f Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Wed, 2 Jul 2025 08:37:26 -0300 Subject: [PATCH] [Added] JavaScript ComfyUI helpers --- js/node_change.js | 57 +++++++++++++++++++++++++++++++++++++++++++++ js/toast_handler.js | 32 +++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 js/node_change.js create mode 100644 js/toast_handler.js diff --git a/js/node_change.js b/js/node_change.js new file mode 100644 index 0000000..bb8a1fb --- /dev/null +++ b/js/node_change.js @@ -0,0 +1,57 @@ +// Copyright (c) 2025 Salvador E. Tropea +// Copyright (c) 2025 Instituto Nacional de TecnologĂ­a Industrial +// License: GPLv3 +// Project: ComfyUI-AudioSeparation + +// This script adds an event named "set-audioseparation-node" +// It can currently just modify a widget value for the current node + +import { app } from "/scripts/app.js"; + +// Register a new extension +app.registerExtension({ + name: "SET.AudioSeparation.NodeAdjust", // Unique name + + // The setup function is executed when the extension is loaded + setup() { + // Add a listener for our custom event + app.api.addEventListener("set-audioseparation-node", (event) => { + // The data from Python is in event.detail + const { action, arg1, arg2 } = event.detail; + + // Find the node that is currently being executed + const node = app.graph.getNodeById(app.runningNodeId); + if (!node) { + console.warn(`[SET.AudioSeparation] Could not find running node with ID: ${app.runningNodeId}`); + return; + } + + // --- ACTION EXECUTED HERE --- + switch (action) { + case 'change_widget': + // arg1 = widget name (e.g., "model") + // arg2 = new value (e.g., "💾 My Awesome Model") + + const widget = node.widgets.find(w => w.name === arg1); + if (widget) { + // This is the key part for combo boxes (dropdowns) + // If the new value isn't in the list of options, add it first. + if (!widget.options.values.includes(arg2)) { + widget.options.values.push(arg2); + } + + // Set the widget value + widget.setValue(arg2, node, app.canvas); + } else { + console.error(`[SET.AudioSeparation] Widget '${arg1}' not found on node ${node.id}`); + } + break; + + // Other actions here in the future + // case 'disable_widget': + // ... + // break; + } + }); + }, +}); diff --git a/js/toast_handler.js b/js/toast_handler.js new file mode 100644 index 0000000..f40f6fe --- /dev/null +++ b/js/toast_handler.js @@ -0,0 +1,32 @@ +// Copyright (c) 2025 Salvador E. Tropea +// Copyright (c) 2025 Instituto Nacional de TecnologĂ­a Industrial +// License: GPLv3 +// Project: ComfyUI-AudioSeparation + +// This script adds an event named "set-audioseparation-toast" +// Used to notify the user in the GUI using the Toast API + +import { app } from "/scripts/app.js"; + +// Register a new extension +app.registerExtension({ + name: "SET.AudioSeparation.ToastHandler", // Unique name + + // The setup function is executed when the extension is loaded + setup() { + // Add a listener for our custom event + app.api.addEventListener("set-audioseparation-toast", (event) => { + // The data from Python is in event.detail + const { message, summary, severity } = event.detail; + + // Use the ComfyUI toast API to show the message + // app.ui.toast.addMessage is the modern way to do this + app.extensionManager.toast.add({ + severity: severity, + summary: summary, + detail: message, + life: 6000 + }); + }); + }, +});