mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-21 21:22:11 -03:00
checkpoint
This commit is contained in:
112
web/comfyui/lora_stacker.js
Normal file
112
web/comfyui/lora_stacker.js
Normal file
@@ -0,0 +1,112 @@
|
||||
import { app } from "../../scripts/app.js";
|
||||
import { addLorasWidget } from "./loras_widget.js";
|
||||
|
||||
// Extract pattern into a constant for consistent use
|
||||
const LORA_PATTERN = /<lora:([^:]+):([-\d\.]+)>/g;
|
||||
|
||||
function mergeLoras(lorasText, lorasArr) {
|
||||
const result = [];
|
||||
let match;
|
||||
|
||||
// Parse text input and create initial entries
|
||||
while ((match = LORA_PATTERN.exec(lorasText)) !== null) {
|
||||
const name = match[1];
|
||||
const inputStrength = Number(match[2]);
|
||||
|
||||
// Find if this lora exists in the array data
|
||||
const existingLora = lorasArr.find(l => l.name === name);
|
||||
|
||||
result.push({
|
||||
name: name,
|
||||
// Use existing strength if available, otherwise use input strength
|
||||
strength: existingLora ? existingLora.strength : inputStrength,
|
||||
active: existingLora ? existingLora.active : true
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
app.registerExtension({
|
||||
name: "LoraManager.LoraStacker",
|
||||
|
||||
async nodeCreated(node) {
|
||||
if (node.comfyClass === "Lora Stacker (LoraManager)") {
|
||||
// Enable widget serialization
|
||||
node.serialize_widgets = true;
|
||||
|
||||
// Wait for node to be properly initialized
|
||||
requestAnimationFrame(() => {
|
||||
// Restore saved value if exists
|
||||
let existingLoras = [];
|
||||
if (node.widgets_values && node.widgets_values.length > 0) {
|
||||
const savedValue = node.widgets_values[1];
|
||||
// TODO: clean up this code
|
||||
try {
|
||||
// Check if the value is already an array/object
|
||||
if (typeof savedValue === 'object' && savedValue !== null) {
|
||||
existingLoras = savedValue;
|
||||
} else if (typeof savedValue === 'string') {
|
||||
existingLoras = JSON.parse(savedValue);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Failed to parse loras data:", e);
|
||||
existingLoras = [];
|
||||
}
|
||||
}
|
||||
// Merge the loras data
|
||||
const mergedLoras = mergeLoras(node.widgets[0].value, existingLoras);
|
||||
|
||||
// Add flag to prevent callback loops
|
||||
let isUpdating = false;
|
||||
|
||||
// Get the widget object directly from the returned object
|
||||
const result = addLorasWidget(node, "loras", {
|
||||
defaultVal: mergedLoras // Pass object directly
|
||||
}, (value) => {
|
||||
// Prevent recursive calls
|
||||
if (isUpdating) return;
|
||||
isUpdating = true;
|
||||
|
||||
try {
|
||||
// Remove loras that are not in the value array
|
||||
const inputWidget = node.widgets[0];
|
||||
const currentLoras = value.map(l => l.name);
|
||||
|
||||
// Use the constant pattern here as well
|
||||
let newText = inputWidget.value.replace(LORA_PATTERN, (match, name, strength) => {
|
||||
return currentLoras.includes(name) ? match : '';
|
||||
});
|
||||
|
||||
// Clean up multiple spaces and trim
|
||||
newText = newText.replace(/\s+/g, ' ').trim();
|
||||
|
||||
inputWidget.value = newText;
|
||||
} finally {
|
||||
isUpdating = false;
|
||||
}
|
||||
});
|
||||
|
||||
node.lorasWidget = result.widget;
|
||||
|
||||
// Update input widget callback
|
||||
const inputWidget = node.widgets[0];
|
||||
inputWidget.callback = (value) => {
|
||||
if (isUpdating) return;
|
||||
isUpdating = true;
|
||||
|
||||
try {
|
||||
const currentLoras = node.lorasWidget.value || [];
|
||||
const mergedLoras = mergeLoras(value, currentLoras);
|
||||
|
||||
node.lorasWidget.value = mergedLoras;
|
||||
} finally {
|
||||
isUpdating = false;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
console.log("Lora Stacker node created:", node);
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -750,6 +750,7 @@ export function addLorasWidget(node, name, opts, callback) {
|
||||
widget.callback = callback;
|
||||
|
||||
widget.serializeValue = () => {
|
||||
console.log("Serializing loras data: ", widgetValue);
|
||||
// Add dummy items to avoid the 2-element serialization issue, a bug in comfyui
|
||||
return [...widgetValue,
|
||||
{ name: "__dummy_item1__", strength: 0, active: false, _isDummy: true },
|
||||
|
||||
@@ -22,7 +22,12 @@ app.registerExtension({
|
||||
node.serialize_widgets = true;
|
||||
|
||||
// Wait for node to be properly initialized
|
||||
requestAnimationFrame(() => {
|
||||
requestAnimationFrame(() => {
|
||||
node.addInput("trigger_words", 'string', {
|
||||
"default": "",
|
||||
"defaultInput": false, // Changed to make it optional
|
||||
"optional": true // Marking the input as optional
|
||||
});
|
||||
// Get the widget object directly from the returned object
|
||||
const result = addTagsWidget(node, "toggle_trigger_words", {
|
||||
defaultVal: []
|
||||
@@ -39,11 +44,11 @@ app.registerExtension({
|
||||
// Restore saved value if exists
|
||||
if (node.widgets_values && node.widgets_values.length > 0) {
|
||||
// 0 is group mode, 1 is input, 2 is tag widget, 3 is original message
|
||||
const savedValue = node.widgets_values[2];
|
||||
const savedValue = node.widgets_values[1];
|
||||
if (savedValue) {
|
||||
result.widget.value = savedValue;
|
||||
}
|
||||
const originalMessage = node.widgets_values[3];
|
||||
const originalMessage = node.widgets_values[2];
|
||||
if (originalMessage) {
|
||||
hiddenWidget.value = originalMessage;
|
||||
}
|
||||
@@ -51,10 +56,12 @@ app.registerExtension({
|
||||
|
||||
const groupModeWidget = node.widgets[0];
|
||||
groupModeWidget.callback = (value) => {
|
||||
if (node.widgets[3].value) {
|
||||
this.updateTagsBasedOnMode(node, node.widgets[3].value, value);
|
||||
if (node.widgets[2].value) {
|
||||
this.updateTagsBasedOnMode(node, node.widgets[2].value, value);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("node ", node);
|
||||
});
|
||||
}
|
||||
},
|
||||
@@ -68,7 +75,7 @@ app.registerExtension({
|
||||
}
|
||||
|
||||
// Store the original message for mode switching
|
||||
node.widgets[3].value = message;
|
||||
node.widgets[2].value = message;
|
||||
|
||||
if (node.tagWidget) {
|
||||
// Parse tags based on current group mode
|
||||
|
||||
Reference in New Issue
Block a user