Support filter on subfolders

This commit is contained in:
pythongosssss
2023-05-05 21:33:17 +01:00
parent c0aa1da9ff
commit c1dedd29ea
+42
View File
@@ -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;