mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-25 15:15:44 -03:00
feat: Enhance LoraManager by updating trigger words handling and dynamically loading widget modules.
This commit is contained in:
@@ -39,21 +39,18 @@ function getConnectedTriggerToggleNodes(node) {
|
|||||||
function updateConnectedTriggerWords(node, text) {
|
function updateConnectedTriggerWords(node, text) {
|
||||||
const connectedNodeIds = getConnectedTriggerToggleNodes(node);
|
const connectedNodeIds = getConnectedTriggerToggleNodes(node);
|
||||||
if (connectedNodeIds.length > 0) {
|
if (connectedNodeIds.length > 0) {
|
||||||
// Extract lora names from the text
|
const loraNames = new Set();
|
||||||
const loraNames = [];
|
|
||||||
let match;
|
let match;
|
||||||
// Reset the RegExp object's lastIndex to start from the beginning
|
|
||||||
LORA_PATTERN.lastIndex = 0;
|
LORA_PATTERN.lastIndex = 0;
|
||||||
while ((match = LORA_PATTERN.exec(text)) !== null) {
|
while ((match = LORA_PATTERN.exec(text)) !== null) {
|
||||||
loraNames.push(match[1]); // match[1] contains the lora name
|
loraNames.add(match[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call API to get trigger words
|
|
||||||
fetch("/loramanager/get_trigger_words", {
|
fetch("/loramanager/get_trigger_words", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
lora_names: loraNames,
|
lora_names: Array.from(loraNames),
|
||||||
node_ids: connectedNodeIds
|
node_ids: connectedNodeIds
|
||||||
})
|
})
|
||||||
}).catch(err => console.error("Error fetching trigger words:", err));
|
}).catch(err => console.error("Error fetching trigger words:", err));
|
||||||
|
|||||||
@@ -1,9 +1,58 @@
|
|||||||
import { app } from "../../scripts/app.js";
|
import { app } from "../../scripts/app.js";
|
||||||
import { addLorasWidget } from "./loras_widget.js";
|
import { dynamicImportByVersion } from "./utils.js";
|
||||||
|
|
||||||
// Extract pattern into a constant for consistent use
|
// Extract pattern into a constant for consistent use
|
||||||
const LORA_PATTERN = /<lora:([^:]+):([-\d\.]+)>/g;
|
const LORA_PATTERN = /<lora:([^:]+):([-\d\.]+)>/g;
|
||||||
|
|
||||||
|
// Function to get the appropriate loras widget based on ComfyUI version
|
||||||
|
async function getLorasWidgetModule() {
|
||||||
|
return await dynamicImportByVersion("./loras_widget.js", "./legacy_loras_widget.js");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to get connected trigger toggle nodes
|
||||||
|
function getConnectedTriggerToggleNodes(node) {
|
||||||
|
const connectedNodes = [];
|
||||||
|
|
||||||
|
if (node.outputs && node.outputs.length > 0) {
|
||||||
|
for (const output of node.outputs) {
|
||||||
|
if (output.links && output.links.length > 0) {
|
||||||
|
for (const linkId of output.links) {
|
||||||
|
const link = app.graph.links[linkId];
|
||||||
|
if (link) {
|
||||||
|
const targetNode = app.graph.getNodeById(link.target_id);
|
||||||
|
if (targetNode && targetNode.comfyClass === "TriggerWord Toggle (LoraManager)") {
|
||||||
|
connectedNodes.push(targetNode.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return connectedNodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to update trigger words for connected toggle nodes
|
||||||
|
function updateConnectedTriggerWords(node, text) {
|
||||||
|
const connectedNodeIds = getConnectedTriggerToggleNodes(node);
|
||||||
|
if (connectedNodeIds.length > 0) {
|
||||||
|
const loraNames = new Set();
|
||||||
|
let match;
|
||||||
|
LORA_PATTERN.lastIndex = 0;
|
||||||
|
while ((match = LORA_PATTERN.exec(text)) !== null) {
|
||||||
|
loraNames.add(match[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch("/loramanager/get_trigger_words", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
lora_names: Array.from(loraNames),
|
||||||
|
node_ids: connectedNodeIds
|
||||||
|
})
|
||||||
|
}).catch(err => console.error("Error fetching trigger words:", err));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function mergeLoras(lorasText, lorasArr) {
|
function mergeLoras(lorasText, lorasArr) {
|
||||||
const result = [];
|
const result = [];
|
||||||
let match;
|
let match;
|
||||||
@@ -40,7 +89,7 @@ app.registerExtension({
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Wait for node to be properly initialized
|
// Wait for node to be properly initialized
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(async () => {
|
||||||
// Restore saved value if exists
|
// Restore saved value if exists
|
||||||
let existingLoras = [];
|
let existingLoras = [];
|
||||||
if (node.widgets_values && node.widgets_values.length > 0) {
|
if (node.widgets_values && node.widgets_values.length > 0) {
|
||||||
@@ -64,7 +113,10 @@ app.registerExtension({
|
|||||||
// Add flag to prevent callback loops
|
// Add flag to prevent callback loops
|
||||||
let isUpdating = false;
|
let isUpdating = false;
|
||||||
|
|
||||||
// Get the widget object directly from the returned object
|
// Dynamically load the appropriate widget module
|
||||||
|
const lorasModule = await getLorasWidgetModule();
|
||||||
|
const { addLorasWidget } = lorasModule;
|
||||||
|
|
||||||
const result = addLorasWidget(node, "loras", {
|
const result = addLorasWidget(node, "loras", {
|
||||||
defaultVal: mergedLoras // Pass object directly
|
defaultVal: mergedLoras // Pass object directly
|
||||||
}, (value) => {
|
}, (value) => {
|
||||||
@@ -86,6 +138,9 @@ app.registerExtension({
|
|||||||
newText = newText.replace(/\s+/g, ' ').trim();
|
newText = newText.replace(/\s+/g, ' ').trim();
|
||||||
|
|
||||||
inputWidget.value = newText;
|
inputWidget.value = newText;
|
||||||
|
|
||||||
|
// Update trigger words when lorasWidget changes
|
||||||
|
updateConnectedTriggerWords(node, newText);
|
||||||
} finally {
|
} finally {
|
||||||
isUpdating = false;
|
isUpdating = false;
|
||||||
}
|
}
|
||||||
@@ -104,6 +159,9 @@ app.registerExtension({
|
|||||||
const mergedLoras = mergeLoras(value, currentLoras);
|
const mergedLoras = mergeLoras(value, currentLoras);
|
||||||
|
|
||||||
node.lorasWidget.value = mergedLoras;
|
node.lorasWidget.value = mergedLoras;
|
||||||
|
|
||||||
|
// Update trigger words when input changes
|
||||||
|
updateConnectedTriggerWords(node, value);
|
||||||
} finally {
|
} finally {
|
||||||
isUpdating = false;
|
isUpdating = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user