Some checks failed
Python Linting / Run Ruff (push) Has been cancelled
Python Linting / Run Pylint (push) Has been cancelled
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.10, [self-hosted Linux], stable) (push) Has been cancelled
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.11, [self-hosted Linux], stable) (push) Has been cancelled
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.12, [self-hosted Linux], stable) (push) Has been cancelled
Full Comfy CI Workflow Runs / test-unix-nightly (12.1, , linux, 3.11, [self-hosted Linux], nightly) (push) Has been cancelled
Execution Tests / test (macos-latest) (push) Has been cancelled
Execution Tests / test (ubuntu-latest) (push) Has been cancelled
Execution Tests / test (windows-latest) (push) Has been cancelled
Test server launches without errors / test (push) Has been cancelled
Unit Tests / test (macos-latest) (push) Has been cancelled
Unit Tests / test (ubuntu-latest) (push) Has been cancelled
Unit Tests / test (windows-2022) (push) Has been cancelled
Includes 30 custom nodes committed directly, 7 Civitai-exclusive loras stored via Git LFS, and a setup script that installs all dependencies and downloads HuggingFace-hosted models on vast.ai. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
91 lines
2.5 KiB
JavaScript
91 lines
2.5 KiB
JavaScript
import { app } from "../../../scripts/app.js";
|
|
app.registerExtension({
|
|
name: "pysssss.ContextMenuHook",
|
|
init() {
|
|
const getOrSet = (target, name, create) => {
|
|
if (name in target) return target[name];
|
|
return (target[name] = create());
|
|
};
|
|
const symbol = getOrSet(window, "__pysssss__", () => Symbol("__pysssss__"));
|
|
const store = getOrSet(window, symbol, () => ({}));
|
|
const contextMenuHook = getOrSet(store, "contextMenuHook", () => ({}));
|
|
for (const e of ["ctor", "preAddItem", "addItem"]) {
|
|
if (!contextMenuHook[e]) {
|
|
contextMenuHook[e] = [];
|
|
}
|
|
}
|
|
|
|
// Big ol' hack to get allow customizing the context menu
|
|
// 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);
|
|
},
|
|
});
|
|
|
|
function triggerCallbacks(name, getArgs, handler) {
|
|
const callbacks = contextMenuHook[name];
|
|
if (callbacks && callbacks instanceof Array) {
|
|
for (const cb of callbacks) {
|
|
const r = cb(...getArgs());
|
|
handler?.call(this, r);
|
|
}
|
|
} else {
|
|
console.warn("[pysssss 🐍]", `invalid ${name} callbacks`, callbacks, name in contextMenuHook);
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
let el;
|
|
let args = arguments;
|
|
triggerCallbacks(
|
|
"preAddItem",
|
|
() => [el, this, args],
|
|
(r) => {
|
|
if (r !== undefined) el = r;
|
|
}
|
|
);
|
|
|
|
if (el === undefined) {
|
|
el = addItem.apply(proxy, arguments);
|
|
}
|
|
|
|
triggerCallbacks(
|
|
"addItem",
|
|
() => [el, this, args],
|
|
(r) => {
|
|
if (r !== undefined) el = r;
|
|
}
|
|
);
|
|
return el;
|
|
};
|
|
|
|
// 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__;
|
|
}
|
|
}
|
|
|
|
triggerCallbacks("ctor", () => [values, options]);
|
|
return ctxMenu.call(this, values, options);
|
|
};
|
|
LiteGraph.ContextMenu.prototype = ctxMenu.prototype;
|
|
},
|
|
});
|