approximately preserve scroll position

This commit is contained in:
Raphael Walker 2024-06-18 17:30:25 +02:00
parent c968159b4c
commit ee2cf8772e
No known key found for this signature in database
GPG Key ID: E6F58BE3395D3AA8

View File

@ -204,6 +204,7 @@ class ComfyList {
this.#reverse = reverse || false; this.#reverse = reverse || false;
this.element = $el("div.comfy-list"); this.element = $el("div.comfy-list");
this.element.style.display = "none"; this.element.style.display = "none";
this.last_keys = null;
} }
get visible() { get visible() {
@ -212,13 +213,8 @@ class ComfyList {
async load() { async load() {
const items = await api.getItems(this.#type); const items = await api.getItems(this.#type);
this.element.replaceChildren( const keys = Object.keys(items);
...Object.keys(items).flatMap((section) => [ var entry = (item) => {
$el("h4", {
textContent: section,
}),
$el("div.comfy-list-items", [
...(this.#reverse ? items[section].reverse() : items[section]).map((item) => {
// Allow items to specify a custom remove action (e.g. for interrupt current prompt) // Allow items to specify a custom remove action (e.g. for interrupt current prompt)
const removeAction = item.remove || { const removeAction = item.remove || {
name: "Delete", name: "Delete",
@ -242,7 +238,28 @@ class ComfyList {
}, },
}), }),
]); ]);
};
if (JSON.stringify(keys) === JSON.stringify(this.last_keys)) {
// we can replace inside the container elements instead of recreating them
// this will preserve the scroll position approximately
// to truely preserve the scroll position, we would need to do something react-like
// in the sense of preserving the DOM nodes and reusing them
Array.from(this.element.getElementsByClassName("comfy-list-items")).forEach((section, i) =>
section.replaceChildren(...(this.#reverse ? items[keys[i]].reverse() : items[keys[i]]).map(entry))
);
} else {
this.last_keys = keys;
// rebuild the list of lists
this.element.replaceChildren(
...keys.flatMap((section) => [
$el("h4", {
textContent: section,
}), }),
$el("div.comfy-list-items", [
...(this.#reverse ? items[section].reverse() : items[section]).map(entry),
]), ]),
]), ]),
$el("div.comfy-list-actions", [ $el("div.comfy-list-actions", [
@ -257,6 +274,7 @@ class ComfyList {
]) ])
); );
} }
}
async update() { async update() {
if (this.visible) { if (this.visible) {