Refactor Lora widget and dynamic module loading

- Updated lora_loader.js to dynamically import the appropriate loras widget based on ComfyUI version, enhancing compatibility and maintainability.
- Enhanced loras_widget.js with improved height management and styling for better user experience.
- Introduced utility functions in utils.js for version checking and dynamic imports, streamlining widget loading processes.
- Improved overall structure and readability of the code, ensuring better performance and easier future updates.
This commit is contained in:
Will Miao
2025-04-07 09:02:36 +08:00
parent d31e641496
commit 50a51c2e79
5 changed files with 1032 additions and 100 deletions

View File

@@ -1,34 +1,10 @@
import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";
import { CONVERTED_TYPE, dynamicImportByVersion } from "./utils.js";
const CONVERTED_TYPE = 'converted-widget'
function getComfyUIFrontendVersion() {
// 直接访问全局变量
return window['__COMFYUI_FRONTEND_VERSION__'];
}
// Dynamically import the appropriate tags widget based on app version
function getTagsWidgetModule() {
// Parse app version and compare with 1.12.6
const currentVersion = getComfyUIFrontendVersion() || "0.0.0";
console.log("currentVersion", currentVersion);
const versionParts = currentVersion.split('.').map(part => parseInt(part, 10));
const requiredVersion = [1, 12, 6];
// Compare version numbers
for (let i = 0; i < 3; i++) {
if (versionParts[i] > requiredVersion[i]) {
console.log("Using tags_widget.js");
return import("./tags_widget.js");
} else if (versionParts[i] < requiredVersion[i]) {
console.log("Using legacy_tags_widget.js");
return import("./legacy_tags_widget.js");
}
}
// If we get here, versions are equal, use the new module
return import("./tags_widget.js");
// Function to get the appropriate tags widget based on ComfyUI version
async function getTagsWidgetModule() {
return await dynamicImportByVersion("./tags_widget.js", "./legacy_tags_widget.js");
}
// TriggerWordToggle extension for ComfyUI
@@ -55,8 +31,8 @@ app.registerExtension({
// Wait for node to be properly initialized
requestAnimationFrame(async () => {
// Dynamically import the appropriate tags widget module
const tagsWidgetModule = await getTagsWidgetModule();
const { addTagsWidget } = tagsWidgetModule;
const tagsModule = await getTagsWidgetModule();
const { addTagsWidget } = tagsModule;
// Get the widget object directly from the returned object
const result = addTagsWidget(node, "toggle_trigger_words", {