From 36aba4a2c744cd62f4afa5543adda61f4f252c00 Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Mon, 3 Mar 2025 05:55:53 +0800 Subject: [PATCH] Refactor Lora handling to improve data consistency and remove duplicates in widget value --- web/comfyui/lora_loader.js | 6 +++--- web/comfyui/loras_widget.js | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/web/comfyui/lora_loader.js b/web/comfyui/lora_loader.js index 9d7ef728..71763808 100644 --- a/web/comfyui/lora_loader.js +++ b/web/comfyui/lora_loader.js @@ -4,7 +4,7 @@ import { addLorasWidget } from "./loras_widget.js"; // Extract pattern into a constant for consistent use const LORA_PATTERN = //g; -function mergeLoras(lorasText, lorasJson) { +function mergeLoras(lorasText, lorasArr) { const result = []; let match; @@ -13,8 +13,8 @@ function mergeLoras(lorasText, lorasJson) { const name = match[1]; const inputStrength = Number(match[2]); - // Find if this lora exists in the JSON data - const existingLora = lorasJson.find(l => l.name === name); + // Find if this lora exists in the array data + const existingLora = lorasArr.find(l => l.name === name); result.push({ name: name, diff --git a/web/comfyui/loras_widget.js b/web/comfyui/loras_widget.js index 04bd540f..0183ea8d 100644 --- a/web/comfyui/loras_widget.js +++ b/web/comfyui/loras_widget.js @@ -664,7 +664,15 @@ export function addLorasWidget(node, name, opts, callback) { return widgetValue; }, setValue: function(v) { - widgetValue = v || ""; + // Remove duplicates by keeping the last occurrence of each lora name + const uniqueValue = (v || []).reduce((acc, lora) => { + // Remove any existing lora with the same name + const filtered = acc.filter(l => l.name !== lora.name); + // Add the current lora + return [...filtered, lora]; + }, []); + + widgetValue = uniqueValue; renderLoras(widgetValue, widget); // Update container height after rendering