A relay's Model settings, hundreds of models, was a small box to scroll
with no way to find one. A list of more than five now has a search field
in its head, matching each row's id, name and typed names (every word
typed, each anywhere), and an Expand that lets the list be as tall as its
rows while the editor scrolls. Escape clears the search before it closes
anything; a list of five or fewer has neither. The code is modelfind.js,
imported when such a list is drawn, so app.js gains one line.
Run (mote): node --test model-find (chromium + webkit, en/zh/ja/de, 420px
wide), names-fold, old-webkit, i18n-keys; gofmt -l; go vet ./... for
darwin, linux -tags nogui and windows.
Broken by hand: the modelFind call turned off, model-find red in every
language.
Red not mine: old-webkit's "styles hold up in Safari 15.0" fails on
.side-foot:has(> .nav-sub) from d374168d0, filed as #5919.
Not tried: the real app window with a real relay of hundreds of models.
50 lines
2.1 KiB
JavaScript
50 lines
2.1 KiB
JavaScript
// Model settings' search and its Expand, for a provider with many models, a
|
|
// relay's hundreds (soilzhu80 on X). app.js imports this only for such a list,
|
|
// so it weighs nothing on the core. Each redraw of the list calls it again.
|
|
const findQs = {};
|
|
let findOpen = false;
|
|
window.modelFind = function (names, head) {
|
|
if (!head.isConnected) return;
|
|
const pid = names.dataset.provider;
|
|
const q = input(findQs[pid] || "", t("Search models"), "search");
|
|
q.classList.add("mfind");
|
|
q.setAttribute("aria-label", t("Search models"));
|
|
const none = el("span", "hint mnone");
|
|
const big = el("button", "text action mbig");
|
|
big.type = "button";
|
|
const drawBig = () => {
|
|
names.classList.toggle("open", findOpen);
|
|
big.textContent = findOpen ? t("Collapse the list") : t("Expand the list");
|
|
big.setAttribute("aria-expanded", findOpen);
|
|
};
|
|
big.onclick = (e) => { e.stopPropagation(); findOpen = !findOpen; drawBig(); };
|
|
// a row matches on its ids, its own name and the ones typed
|
|
const sift = () => {
|
|
const words = (findQs[pid] || "").trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
let n = 0;
|
|
for (const r of names.querySelectorAll(":scope > .mname")) {
|
|
const hay = [r.querySelector(".mwho code")?.textContent, ...[...r.querySelectorAll(".mwho input, .mlocal input")].map((i) => i.value + " " + i.placeholder)].join(" ").toLowerCase();
|
|
r.hidden = !words.every((w) => hay.includes(w));
|
|
if (!r.hidden) n++;
|
|
}
|
|
none.hidden = !words.length || n > 0;
|
|
none.textContent = t("No model matches “{q}”", { q: (findQs[pid] || "").trim() });
|
|
};
|
|
q.oninput = () => { findQs[pid] = q.value; sift(); };
|
|
// Escape clears what is typed first, then does what it does in any field
|
|
const own = q.onkeydown;
|
|
q.onkeydown = (e) => {
|
|
if (e.key === "Enter") e.preventDefault();
|
|
if (e.key !== "Escape" || !q.value) return own(e);
|
|
e.stopPropagation();
|
|
e.preventDefault();
|
|
q.value = findQs[pid] = "";
|
|
sift();
|
|
};
|
|
head.classList.add("finds");
|
|
head.querySelector("b").after(q, big);
|
|
(names.querySelector(":scope > .msuffix") || head).after(none);
|
|
drawBig();
|
|
sift();
|
|
};
|