Files
kk8bit-KayTool/web/quickadd.js
T
2025-03-05 16:31:01 +08:00

51 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { app } from "../../../scripts/app.js";
console.log("[KayTool] Loading QuickAdd extension");
// Inspired by:https://github.com/kijai/ComfyUI-KJNodes
function addMenuHandler(nodeType, cb) {
const getOpts = nodeType.prototype.getExtraMenuOptions;
nodeType.prototype.getExtraMenuOptions = function () {
const r = getOpts ? getOpts.apply(this, arguments) : [];
cb.apply(this, arguments);
return r;
};
}
function addNode(name, nextTo, options = {}) {
options = { side: "left", select: true, shiftY: 0, shiftX: 0, ...options };
const node = LiteGraph.createNode(name);
app.graph.add(node);
node.pos = [
options.side === "left" ? nextTo.pos[0] - (node.size[0] + options.offset) : nextTo.pos[0] + nextTo.size[0] + options.offset,
nextTo.pos[1] + options.shiftY,
];
if (options.select) {
app.canvas.selectNode(node, false);
}
return node;
}
app.registerExtension({
name: "KayTool.QuickAdd",
async beforeRegisterNodeDef(nodeType, nodeData, app) {
if (nodeData.input && nodeData.input.required) {
addMenuHandler(nodeType, function (_, options) {
options.unshift(
{
content: "add 🛜 Out",
callback: () => { addNode("KayoutNode", this, { side: "right", offset: 20 }); }
},
{
content: "add 🛜 In",
callback: () => { addNode("KayInNode", this, { side: "left", offset: 20 }); }
}
);
});
}
},
async setup() {
console.log("[KayTool] QuickAdd extension setup complete");
}
});