/** * Searchable combobox for editor dimension picking. * options: { id, label, disabled?, hint? }[] */ export function createDimensionCombobox ({ root, allowCreate = false, placeholder = "Dimension…", onSelect, onCreate }) { root.classList.add("dim-combo"); root.innerHTML = ""; const input = document.createElement("input"); input.type = "text"; input.className = "dim-combo-input"; input.placeholder = placeholder; input.autocomplete = "off"; input.setAttribute("role", "combobox"); input.setAttribute("aria-autocomplete", "list"); input.setAttribute("aria-expanded", "false"); const toggle = document.createElement("button"); toggle.type = "button"; toggle.className = "dim-combo-toggle"; toggle.tabIndex = -1; toggle.setAttribute("aria-label", "Show dimensions"); toggle.textContent = "▾"; const list = document.createElement("ul"); list.className = "dim-combo-list"; list.hidden = true; list.setAttribute("role", "listbox"); root.append(input, toggle, list); /** @type {{ id: string, label: string, disabled?: boolean, hint?: string }[]} */ let options = []; let selectedId = null; let open = false; function selectedOption () { return options.find((opt) => opt.id === selectedId) ?? null; } function displayLabel (opt) { if (!opt) { return ""; } return opt.label && opt.label !== opt.id ? `${opt.label} (${opt.id})` : opt.id; } function syncInputFromSelection () { const opt = selectedOption(); input.value = opt ? displayLabel(opt) : (selectedId ?? ""); } function setOpen (next) { open = next; list.hidden = !open; input.setAttribute("aria-expanded", open ? "true" : "false"); if (open) { renderList(); } } function filtered () { const q = input.value.trim().toLowerCase(); if (!q) { return options.slice(); } return options.filter((opt) => opt.id.toLowerCase().includes(q) || (opt.label ?? "").toLowerCase().includes(q) || (opt.hint ?? "").toLowerCase().includes(q) ); } function exactMatchId (text) { const q = String(text ?? "").trim().toLowerCase(); if (!q) { return null; } const hit = options.find((opt) => opt.id.toLowerCase() === q || (opt.label ?? "").toLowerCase() === q); return hit?.id ?? null; } function renderList () { list.innerHTML = ""; const rows = filtered(); const typed = input.value.trim(); const typedLower = typed.toLowerCase(); const hasExact = options.some((opt) => opt.id.toLowerCase() === typedLower || (opt.label ?? "").toLowerCase() === typedLower ); for (const opt of rows) { const li = document.createElement("li"); li.className = "dim-combo-option"; if (opt.disabled) { li.classList.add("disabled"); } if (opt.id === selectedId) { li.classList.add("selected"); } li.setAttribute("role", "option"); li.dataset.id = opt.id; const main = document.createElement("span"); main.className = "dim-combo-option-label"; main.textContent = displayLabel(opt); li.append(main); if (opt.hint) { const hint = document.createElement("span"); hint.className = "dim-combo-option-hint"; hint.textContent = opt.hint; li.append(hint); } li.addEventListener("mousedown", (event) => { event.preventDefault(); choose(opt.id); }); list.append(li); } if (allowCreate && typed && !hasExact) { const li = document.createElement("li"); li.className = "dim-combo-option create"; li.setAttribute("role", "option"); li.textContent = `Create «${typed}»`; li.addEventListener("mousedown", (event) => { event.preventDefault(); setOpen(false); onCreate?.(typed); }); list.append(li); } if (!list.children.length) { const empty = document.createElement("li"); empty.className = "dim-combo-empty"; empty.textContent = "No dimensions"; list.append(empty); } } function choose (id) { selectedId = id; syncInputFromSelection(); setOpen(false); onSelect?.(id); } input.addEventListener("focus", () => { setOpen(true); }); input.addEventListener("mousedown", (event) => { if (!open) { event.preventDefault(); setOpen(true); input.focus(); } }); toggle.addEventListener("mousedown", (event) => { event.preventDefault(); if (open) { syncInputFromSelection(); setOpen(false); input.blur(); } else { setOpen(true); input.focus(); } }); input.addEventListener("input", () => { setOpen(true); renderList(); }); input.addEventListener("keydown", (event) => { if (event.key === "Escape") { syncInputFromSelection(); setOpen(false); input.blur(); return; } if (event.key === "Enter") { event.preventDefault(); const typed = input.value.trim(); const exact = exactMatchId(typed); if (exact) { choose(exact); return; } const rows = filtered().filter((opt) => !opt.disabled); if (rows.length === 1) { choose(rows[0].id); return; } if (allowCreate && typed && !exactMatchId(typed)) { setOpen(false); onCreate?.(typed); } } }); input.addEventListener("blur", () => { window.setTimeout(() => { if (!root.contains(document.activeElement)) { syncInputFromSelection(); setOpen(false); } }, 0); }); return { setOptions (nextOptions) { options = Array.isArray(nextOptions) ? nextOptions.slice() : []; if (selectedId && !options.some((opt) => opt.id === selectedId)) { selectedId = options[0]?.id ?? null; } syncInputFromSelection(); if (open) { renderList(); } }, setValue (id) { selectedId = id; syncInputFromSelection(); if (open) { renderList(); } }, getValue () { return selectedId; }, close () { setOpen(false); } }; }