diff --git a/lora-subfolders/loraSubfolders.js b/lora-subfolders/loraSubfolders.js index 616a82d..7d85e2b 100644 --- a/lora-subfolders/loraSubfolders.js +++ b/lora-subfolders/loraSubfolders.js @@ -4,6 +4,48 @@ import { app } from "/scripts/app.js"; app.registerExtension({ name: "pysssss.LoraSubfolder", + init() { + // Big ol' hack to get filtering working in submenus + // Replace the addItem function with our own that wraps the context of "this" with a proxy + // That proxy then replaces the constructor with another proxy + // That proxy then calls the custom ContextMenu that supports filters + + const ctorProxy = new Proxy(LiteGraph.ContextMenu, { + construct(target, args) { + return new LiteGraph.ContextMenu(...args); + }, + }); + + const addItem = LiteGraph.ContextMenu.prototype.addItem; + LiteGraph.ContextMenu.prototype.addItem = function () { + const proxy = new Proxy(this, { + get(target, prop) { + if (prop === "constructor") { + return ctorProxy; + } + return target[prop]; + }, + }); + proxy.__target__ = this; + return addItem.apply(proxy, arguments); + }; + + // We also need to patch the ContextMenu constructor to unwrap the parent else it fails a LiteGraph type check + const ctxMenu = LiteGraph.ContextMenu; + LiteGraph.ContextMenu = function (values, options) { + if (options?.parentMenu) { + if (options.parentMenu.__target__) { + options.parentMenu = options.parentMenu.__target__; + } + // Copy the class from the parent so if we are dark we are also dark + if (options.parentMenu.options?.className === "dark") { + options.className = "dark"; + } + } + ctxMenu.call(this, values, options); + }; + LiteGraph.ContextMenu.prototype = ctxMenu.prototype; + }, async beforeRegisterNodeDef(nodeType, nodeData, app) { if (nodeData.name === "LoraLoader") { const onNodeCreated = nodeType.prototype.onNodeCreated;