mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-11-20 21:47:04 +08:00
Fix to contextMenuFilter.js
This commit is contained in:
parent
2e3aa47f61
commit
e1557ea69b
@ -1,148 +1,147 @@
|
|||||||
import {app} from "../../scripts/app.js";
|
import {app} from "../../scripts/app.js";
|
||||||
|
import { LiteGraph } from "../../lib/litegraph.core.js";
|
||||||
|
|
||||||
// Adds filtering to combo context menus
|
// Adds filtering to combo context menus
|
||||||
|
|
||||||
const ext = {
|
const ext = {
|
||||||
name: "Comfy.ContextMenuFilter",
|
name: "Comfy.ContextMenuFilter",
|
||||||
init() {
|
init() {
|
||||||
const ctxMenu = LiteGraph.ContextMenu;
|
class FilterContextMenu extends LiteGraph.ContextMenu {
|
||||||
|
constructor(values, options){
|
||||||
|
super(values, options);
|
||||||
|
|
||||||
LiteGraph.ContextMenu = function (values, options) {
|
// If we are a dark menu (only used for combo boxes) then add a filter input
|
||||||
const ctx = ctxMenu.call(this, values, options);
|
if (options?.className === "dark" && values?.length > 10) {
|
||||||
|
const filter = document.createElement("input");
|
||||||
|
filter.classList.add("comfy-context-menu-filter");
|
||||||
|
filter.placeholder = "Filter list";
|
||||||
|
this.root.prepend(filter);
|
||||||
|
|
||||||
// If we are a dark menu (only used for combo boxes) then add a filter input
|
const items = Array.from(this.root.querySelectorAll(".litemenu-entry"));
|
||||||
if (options?.className === "dark" && values?.length > 10) {
|
let displayedItems = [...items];
|
||||||
const filter = document.createElement("input");
|
let itemCount = displayedItems.length;
|
||||||
filter.classList.add("comfy-context-menu-filter");
|
|
||||||
filter.placeholder = "Filter list";
|
|
||||||
this.root.prepend(filter);
|
|
||||||
|
|
||||||
const items = Array.from(this.root.querySelectorAll(".litemenu-entry"));
|
// We must request an animation frame for the current node of the active canvas to update.
|
||||||
let displayedItems = [...items];
|
requestAnimationFrame(() => {
|
||||||
let itemCount = displayedItems.length;
|
const currentNode = LGraphCanvas.active_canvas.current_node;
|
||||||
|
const clickedComboValue = currentNode.widgets
|
||||||
// We must request an animation frame for the current node of the active canvas to update.
|
?.filter(w => w.type === "combo" && w.options.values.length === values.length)
|
||||||
requestAnimationFrame(() => {
|
.find(w => w.options.values.every((v, i) => v === values[i]))
|
||||||
const currentNode = LGraphCanvas.active_canvas.current_node;
|
?.value;
|
||||||
const clickedComboValue = currentNode.widgets
|
|
||||||
?.filter(w => w.type === "combo" && w.options.values.length === values.length)
|
|
||||||
.find(w => w.options.values.every((v, i) => v === values[i]))
|
|
||||||
?.value;
|
|
||||||
|
|
||||||
let selectedIndex = clickedComboValue ? values.findIndex(v => v === clickedComboValue) : 0;
|
|
||||||
if (selectedIndex < 0) {
|
|
||||||
selectedIndex = 0;
|
|
||||||
}
|
|
||||||
let selectedItem = displayedItems[selectedIndex];
|
|
||||||
updateSelected();
|
|
||||||
|
|
||||||
// Apply highlighting to the selected item
|
|
||||||
function updateSelected() {
|
|
||||||
selectedItem?.style.setProperty("background-color", "");
|
|
||||||
selectedItem?.style.setProperty("color", "");
|
|
||||||
selectedItem = displayedItems[selectedIndex];
|
|
||||||
selectedItem?.style.setProperty("background-color", "#ccc", "important");
|
|
||||||
selectedItem?.style.setProperty("color", "#000", "important");
|
|
||||||
}
|
|
||||||
|
|
||||||
const positionList = () => {
|
|
||||||
const rect = this.root.getBoundingClientRect();
|
|
||||||
|
|
||||||
// If the top is off-screen then shift the element with scaling applied
|
|
||||||
if (rect.top < 0) {
|
|
||||||
const scale = 1 - this.root.getBoundingClientRect().height / this.root.clientHeight;
|
|
||||||
const shift = (this.root.clientHeight * scale) / 2;
|
|
||||||
this.root.style.top = -shift + "px";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arrow up/down to select items
|
|
||||||
filter.addEventListener("keydown", (event) => {
|
|
||||||
switch (event.key) {
|
|
||||||
case "ArrowUp":
|
|
||||||
event.preventDefault();
|
|
||||||
if (selectedIndex === 0) {
|
|
||||||
selectedIndex = itemCount - 1;
|
|
||||||
} else {
|
|
||||||
selectedIndex--;
|
|
||||||
}
|
|
||||||
updateSelected();
|
|
||||||
break;
|
|
||||||
case "ArrowRight":
|
|
||||||
event.preventDefault();
|
|
||||||
selectedIndex = itemCount - 1;
|
|
||||||
updateSelected();
|
|
||||||
break;
|
|
||||||
case "ArrowDown":
|
|
||||||
event.preventDefault();
|
|
||||||
if (selectedIndex === itemCount - 1) {
|
|
||||||
selectedIndex = 0;
|
|
||||||
} else {
|
|
||||||
selectedIndex++;
|
|
||||||
}
|
|
||||||
updateSelected();
|
|
||||||
break;
|
|
||||||
case "ArrowLeft":
|
|
||||||
event.preventDefault();
|
|
||||||
selectedIndex = 0;
|
|
||||||
updateSelected();
|
|
||||||
break;
|
|
||||||
case "Enter":
|
|
||||||
selectedItem?.click();
|
|
||||||
break;
|
|
||||||
case "Escape":
|
|
||||||
this.close();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
filter.addEventListener("input", () => {
|
|
||||||
// Hide all items that don't match our filter
|
|
||||||
const term = filter.value.toLocaleLowerCase();
|
|
||||||
// When filtering, recompute which items are visible for arrow up/down and maintain selection.
|
|
||||||
displayedItems = items.filter(item => {
|
|
||||||
const isVisible = !term || item.textContent.toLocaleLowerCase().includes(term);
|
|
||||||
item.style.display = isVisible ? "block" : "none";
|
|
||||||
return isVisible;
|
|
||||||
});
|
|
||||||
|
|
||||||
selectedIndex = 0;
|
|
||||||
if (displayedItems.includes(selectedItem)) {
|
|
||||||
selectedIndex = displayedItems.findIndex(d => d === selectedItem);
|
|
||||||
}
|
|
||||||
itemCount = displayedItems.length;
|
|
||||||
|
|
||||||
|
let selectedIndex = clickedComboValue ? values.findIndex(v => v === clickedComboValue) : 0;
|
||||||
|
if (selectedIndex < 0) {
|
||||||
|
selectedIndex = 0;
|
||||||
|
}
|
||||||
|
let selectedItem = displayedItems[selectedIndex];
|
||||||
updateSelected();
|
updateSelected();
|
||||||
|
|
||||||
// If we have an event then we can try and position the list under the source
|
// Apply highlighting to the selected item
|
||||||
if (options.event) {
|
function updateSelected() {
|
||||||
let top = options.event.clientY - 10;
|
selectedItem?.style.setProperty("background-color", "");
|
||||||
|
selectedItem?.style.setProperty("color", "");
|
||||||
const bodyRect = document.body.getBoundingClientRect();
|
selectedItem = displayedItems[selectedIndex];
|
||||||
const rootRect = this.root.getBoundingClientRect();
|
selectedItem?.style.setProperty("background-color", "#ccc", "important");
|
||||||
if (bodyRect.height && top > bodyRect.height - rootRect.height - 10) {
|
selectedItem?.style.setProperty("color", "#000", "important");
|
||||||
top = Math.max(0, bodyRect.height - rootRect.height - 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.root.style.top = top + "px";
|
|
||||||
positionList();
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
requestAnimationFrame(() => {
|
const positionList = () => {
|
||||||
// Focus the filter box when opening
|
const rect = this.root.getBoundingClientRect();
|
||||||
filter.focus();
|
|
||||||
|
|
||||||
positionList();
|
// If the top is off-screen then shift the element with scaling applied
|
||||||
});
|
if (rect.top < 0) {
|
||||||
})
|
const scale = 1 - this.root.getBoundingClientRect().height / this.root.clientHeight;
|
||||||
|
const shift = (this.root.clientHeight * scale) / 2;
|
||||||
|
this.root.style.top = -shift + "px";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Arrow up/down to select items
|
||||||
|
filter.addEventListener("keydown", (event) => {
|
||||||
|
switch (event.key) {
|
||||||
|
case "ArrowUp":
|
||||||
|
event.preventDefault();
|
||||||
|
if (selectedIndex === 0) {
|
||||||
|
selectedIndex = itemCount - 1;
|
||||||
|
} else {
|
||||||
|
selectedIndex--;
|
||||||
|
}
|
||||||
|
updateSelected();
|
||||||
|
break;
|
||||||
|
case "ArrowRight":
|
||||||
|
event.preventDefault();
|
||||||
|
selectedIndex = itemCount - 1;
|
||||||
|
updateSelected();
|
||||||
|
break;
|
||||||
|
case "ArrowDown":
|
||||||
|
event.preventDefault();
|
||||||
|
if (selectedIndex === itemCount - 1) {
|
||||||
|
selectedIndex = 0;
|
||||||
|
} else {
|
||||||
|
selectedIndex++;
|
||||||
|
}
|
||||||
|
updateSelected();
|
||||||
|
break;
|
||||||
|
case "ArrowLeft":
|
||||||
|
event.preventDefault();
|
||||||
|
selectedIndex = 0;
|
||||||
|
updateSelected();
|
||||||
|
break;
|
||||||
|
case "Enter":
|
||||||
|
selectedItem?.click();
|
||||||
|
break;
|
||||||
|
case "Escape":
|
||||||
|
this.close();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
filter.addEventListener("input", () => {
|
||||||
|
// Hide all items that don't match our filter
|
||||||
|
const term = filter.value.toLocaleLowerCase();
|
||||||
|
// When filtering, recompute which items are visible for arrow up/down and maintain selection.
|
||||||
|
displayedItems = items.filter(item => {
|
||||||
|
const isVisible = !term || item.textContent.toLocaleLowerCase().includes(term);
|
||||||
|
item.style.display = isVisible ? "block" : "none";
|
||||||
|
return isVisible;
|
||||||
|
});
|
||||||
|
|
||||||
|
selectedIndex = 0;
|
||||||
|
if (displayedItems.includes(selectedItem)) {
|
||||||
|
selectedIndex = displayedItems.findIndex(d => d === selectedItem);
|
||||||
|
}
|
||||||
|
itemCount = displayedItems.length;
|
||||||
|
|
||||||
|
updateSelected();
|
||||||
|
|
||||||
|
// If we have an event then we can try and position the list under the source
|
||||||
|
if (options.event) {
|
||||||
|
let top = options.event.clientY - 10;
|
||||||
|
|
||||||
|
const bodyRect = document.body.getBoundingClientRect();
|
||||||
|
const rootRect = this.root.getBoundingClientRect();
|
||||||
|
if (bodyRect.height && top > bodyRect.height - rootRect.height - 10) {
|
||||||
|
top = Math.max(0, bodyRect.height - rootRect.height - 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.root.style.top = top + "px";
|
||||||
|
positionList();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
// Focus the filter box when opening
|
||||||
|
filter.focus();
|
||||||
|
|
||||||
|
positionList();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return ctx;
|
LiteGraph.ContextMenu = FilterContextMenu;
|
||||||
};
|
window.ContextMenu = FilterContextMenu;
|
||||||
|
}
|
||||||
LiteGraph.ContextMenu.prototype = ctxMenu.prototype;
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.registerExtension(ext);
|
app.registerExtension(ext);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user