File size: 4,827 Bytes
e0e88f3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
import { app } from "/scripts/app.js";
import { ComfyWidgets } from "/scripts/widgets.js";
import { $el } from "/scripts/ui.js";
app.registerExtension({
name: "pysssss.Combo++",
init() {
$el("style", {
textContent: `
.litemenu-entry:hover .pysssss-combo-image {
display: block;
}
.pysssss-combo-image {
display: none;
position: absolute;
left: 0;
top: 0;
transform: translate(-100%, 0);
width: 256px;
height: 256px;
background-size: cover;
background-position: center;
filter: brightness(65%);
}
`,
parent: document.body,
});
const submenuSetting = app.ui.settings.addSetting({
id: "pysssss.Combo++.Submenu",
name: "🐍 Enable submenu in custom nodes",
defaultValue: true,
type: "boolean",
});
// Ensure hook callbacks are available
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] = [];
}
}
// // Checks if this is a custom combo item
const isCustomItem = (value) => value && typeof value === "object" && "image" in value && value.content;
// Simple check for what separator to split by
const splitBy = (navigator.platform || navigator.userAgent).includes("Win") ? "\\" : "/";
// TODO: Once filtering submenus is fixed uncomment this
/*
contextMenuHook["ctor"].push(function (values, options) {
// Copy the class from the parent so if we are dark we are also dark
// this enables the filter box
if (options.parentMenu?.options?.className === "dark") {
options.className = "dark";
}
});
*/
// After an element is created for an item, add an image if it has one
contextMenuHook["addItem"].push(function (el, menu, [name, value, options]) {
if (el && isCustomItem(value) && value?.image && !value.submenu) {
el.textContent += " *";
$el("div.pysssss-combo-image", {
parent: el,
style: {
backgroundImage: `url(/pysssss/view/${encodeURIComponent(value.image)})`,
},
});
}
});
function buildMenu(widget, values) {
const lookup = {
"": { options: [] },
};
// Split paths into menu structure
for (const value of values) {
const split = value.content.split(splitBy);
let path = "";
for (let i = 0; i < split.length; i++) {
const s = split[i];
const last = i === split.length - 1;
if (last) {
// Leaf node, manually add handler that sets the lora
lookup[path].options.push({
...value,
title: s,
callback: () => {
widget.value = value;
widget.callback(value);
app.graph.setDirtyCanvas(true);
},
});
} else {
const prevPath = path;
path += s + splitBy;
if (!lookup[path]) {
const sub = {
title: s,
submenu: {
options: [],
title: s,
},
};
// Add to tree
lookup[path] = sub.submenu;
lookup[prevPath].options.push(sub);
}
}
}
}
return lookup[""].options;
}
// Override COMBO widgets to patch their values
const combo = ComfyWidgets["COMBO"];
ComfyWidgets["COMBO"] = function (node, inputName, inputData) {
const type = inputData[0];
const res = combo.apply(this, arguments);
if (isCustomItem(type[0])) {
let value = res.widget.value;
let values = res.widget.options.values;
let menu = null;
// Override the option values to check if we should render a menu structure
Object.defineProperty(res.widget.options, "values", {
get() {
if (submenuSetting.value) {
if (!menu) {
// Only build the menu once
menu = buildMenu(res.widget, values);
}
return menu;
}
return values;
},
set(v) {
// Options are changing (refresh) so reset the menu so it can be rebuilt if required
values = v;
menu = null;
},
});
Object.defineProperty(res.widget, "value", {
get() {
// HACK: litegraph supports rendering items with "content" in the menu, but not on the widget
// This detects when its being called by the widget drawing and just returns the text
if (res.widget && new Error().stack.includes("drawNodeWidgets")) {
return (value || type[0]).content;
}
return value;
},
set(v) {
if (v.submenu) {
// Dont allow selection of submenus
return;
}
value = v;
},
});
}
return res;
};
},
});
|