Add filtering tree

This commit is contained in:
pythongosssss 2024-03-14 22:38:35 +00:00
parent 35d3ecbdff
commit b9030c7281
3 changed files with 107 additions and 155 deletions

View File

@ -262,6 +262,31 @@
opacity: 0.8;
}
.comfyui-workflows-panel section header {
display: flex;
justify-content: space-between;
align-items: center;
}
.comfy-ui-workflows-search .mdi {
position: relative;
top: 2px;
pointer-events: none;
}
.comfy-ui-workflows-search input {
background-color: var(--comfy-input-bg);
color: var(--input-text);
border: none;
border-radius: 4px;
padding: 4px 10px;
margin-left: -24px;
text-indent: 18px;
}
.comfy-ui-workflows-search input:placeholder-shown {
width: 10px;
}
.comfy-ui-workflows-search input:placeholder-shown:focus {
width: auto;
}
.comfyui-workflows-actions {
display: flex;
gap: 10px;
@ -308,7 +333,7 @@
padding: 0 0 0 25px;
}
.comfyui-workflows-tree .closed > ul {
.comfyui-workflows-tree:not(.filtered) .closed > ul {
display: none;
}

View File

@ -394,19 +394,96 @@ export class ComfyWorkflowsContent {
current?.replaceWith(this.favoritesElement);
}
filterTree() {
if (!this.filterText) {
this.treeRoot.classList.remove("filtered");
// Unfilter whole tree
for (const item of Object.values(this.treeFiles)) {
item.element.parentElement.style.removeProperty("display");
this.showTreeParents(item.element.parentElement);
}
return;
}
this.treeRoot.classList.add("filtered");
const searchTerms = this.filterText.toLocaleLowerCase().split(" ");
for (const item of Object.values(this.treeFiles)) {
const parts = item.workflow.pathParts;
let termIndex = 0;
let valid = false;
for (const part of parts) {
let currentIndex = 0;
do {
currentIndex = part.indexOf(searchTerms[termIndex], currentIndex);
if (currentIndex > -1) currentIndex += searchTerms[termIndex].length;
} while (currentIndex !== -1 && ++termIndex < searchTerms.length);
if (termIndex >= searchTerms.length) {
valid = true;
break;
}
}
if (valid) {
item.element.parentElement.style.removeProperty("display");
this.showTreeParents(item.element.parentElement);
} else {
item.element.parentElement.style.display = "none";
this.hideTreeParents(item.element.parentElement);
}
}
}
hideTreeParents(element) {
// Hide all parents if no children are visible
if (element.parentElement?.classList.contains("comfyui-workflows-tree") === false) {
for (let i = 1; i < element.parentElement.children.length; i++) {
const c = element.parentElement.children[i];
if (c.style.display !== "none") {
return;
}
}
element.parentElement.style.display = "none";
this.hideTreeParents(element.parentElement);
}
}
showTreeParents(element) {
if (element.parentElement?.classList.contains("comfyui-workflows-tree") === false) {
element.parentElement.style.removeProperty("display");
this.showTreeParents(element.parentElement);
}
}
updateTree() {
const current = this.treeElement;
const nodes = {};
let typingTimeout;
this.treeFiles = {};
const tree = $el("ul.comfyui-workflows-tree");
this.treeElement = $el("section", [$el("h3", "Browse"), tree]);
this.treeRoot = $el("ul.comfyui-workflows-tree");
this.treeElement = $el("section", [
$el("header", [
$el("h3", "Browse"),
$el("div.comfy-ui-workflows-search", [
$el("i.mdi.mdi-18px.mdi-magnify"),
$el("input", {
placeholder: "Search",
value: this.filterText ?? "",
oninput: (e) => {
this.filterText = e.target["value"]?.trim();
clearTimeout(typingTimeout);
typingTimeout = setTimeout(() => this.filterTree(), 100);
},
}),
]),
]),
this.treeRoot,
]);
for (const workflow of this.app.workflowManager.workflows) {
if (!workflow.pathParts) continue;
let currentPath = "";
let currentRoot = tree;
let currentRoot = this.treeRoot;
for (let i = 0; i < workflow.pathParts.length; i++) {
currentPath += (currentPath ? "\\" : "") + workflow.pathParts[i];
@ -418,6 +495,7 @@ export class ComfyWorkflowsContent {
}
current?.replaceWith(this.treeElement);
this.filterTree();
}
#expandNode(el, workflow, thisPath, i) {

View File

@ -153,155 +153,4 @@ export function setStorageValue(id, value) {
sessionStorage.setItem(`${id}:${clientId}`, value);
}
localStorage.setItem(id, value);
}
export class tmpl {
static #subs = Symbol();
constructor(template, components) {
this.template = template;
this.components = components;
}
build = (context) => {
const wrapper = document.createElement("div");
wrapper.innerHTML = this.template;
for (const el of wrapper.children) {
this.#build(el, context);
}
return wrapper.children;
};
#build = (el, ctx) => {
if (this.#loop(el, ctx)) return;
tmpl.#if(el, ctx);
tmpl.#text(el, ctx);
for (const child of el.children) {
this.#build(child, ctx);
}
};
#loop = (el, ctx) => {
const attr = el.getAttribute(":each");
if (attr) {
const id = el.getAttribute(":tmpl");
el.removeAttribute(":each");
el.removeAttribute(":tmpl");
if (!id || !this.components[id]) {
console.warn("Missing template: " + id, el);
return false;
}
tmpl.#bind(ctx[attr], (v) => {
if (v == null) {
v = [];
} else if (!(v instanceof Array)) {
if (typeof v === "object") {
v = Object.entries(v);
} else {
v = [v];
}
}
const res = v.flatMap((item, index, array) => {
const parent = el.cloneNode(true);
const children = this.components[id]({ item, index, array }, ctx, this);
parent.replaceChildren(...children);
return parent;
});
el.replaceWith(...res);
});
return true;
}
return false;
};
static rx(value, onChange, immediate = true) {
let v = value;
const subs = [];
if (onChange) {
subs.push(onChange);
if (immediate) {
onChange(v);
}
}
return {
get value() {
return v;
},
set value(value) {
let old = v;
v = value;
for (const sub of subs) {
sub(v, old);
}
},
[tmpl.#subs]: subs,
};
}
static watch(rx, cb) {
const subs = rx[tmpl.#subs];
if (!subs) throw new Error("Attempt to watch non-reactive object");
subs.push(cb);
return () => subs.splice(subs.indexOf(cb), 1);
}
static #isReactive(value) {
return !!value?.[tmpl.#subs];
}
static #unwrap(value) {
return tmpl.#isReactive(value) ? value.value : value;
}
static #bind(value, check) {
if (tmpl.#isReactive(value)) {
tmpl.watch(value, check);
value = tmpl.#unwrap(value);
}
check(value);
}
static #if(el, ctx) {
let visibilityAttr = el.getAttribute(":if");
let negate = false;
if (visibilityAttr) {
el.removeAttribute(":if");
} else {
visibilityAttr = el.getAttribute(":if!");
if (visibilityAttr) {
el.removeAttribute(":if!");
negate = true;
}
}
if (visibilityAttr) {
el.removeAttribute(visibilityAttr);
tmpl.#bind(ctx[visibilityAttr], (v) => {
if (negate) {
v = !v;
}
if (v) {
el.style.display = "none";
} else {
el.style.removeProperty("display");
}
});
}
}
static #text(el, ctx) {
const attr = el.getAttribute(":text");
if (attr) {
el.removeAttribute(":text");
tmpl.#bind(ctx[attr], (v) => (el.textContent = v));
}
}
}