Files

49 lines
1.9 KiB
JavaScript

import { app } from "../../scripts/app.js";
app.registerExtension({
name: "Comfy.DynamicComboGenerator",
async nodeCreated(node) {
if (node.comfyClass !== "DynamicComboGenerator") return;
const textWidget = node.widgets.find(w => w.name === "multiline_entries");
const comboWidget = node.widgets.find(w => w.name === "selected_value");
if (!textWidget || !comboWidget) return;
// Function to extract text lines and push them directly to the combo options array
const updateComboOptions = () => {
const rawText = textWidget.value || "";
// Split by lines, drop empty strings and trimmable spaces
const options = rawText.split("\n")
.map(line => line.trim())
.filter(line => line.length > 0);
if (options.length === 0) {
options.push(""); // Fallback empty item if text area is cleared
}
// Directly swap out the validation array the UI references
comboWidget.options.values = options;
// Integrity fallback: If the existing choice is no longer available in the text box,
// set selection to the first valid line
if (!options.includes(comboWidget.value)) {
comboWidget.value = options[0];
}
node.setDirtyCanvas(true, true);
};
// Hook onto text field modifications
const originalCallback = textWidget.callback;
textWidget.callback = function (value) {
const result = originalCallback ? originalCallback.apply(this, arguments) : value;
updateComboOptions();
return result;
};
// Initialize lists immediately when node instances load into the workspace
setTimeout(updateComboOptions, 10);
}
});