| 1 | /** |
| 2 | * Searchable combobox for editor dimension picking. |
| 3 | * options: { id, label, disabled?, hint? }[] |
| 4 | */ |
| 5 | export function createDimensionCombobox ({ |
| 6 | root, |
| 7 | allowCreate = false, |
| 8 | placeholder = "Dimension…", |
| 9 | onSelect, |
| 10 | onCreate |
| 11 | }) |
| 12 | { |
| 13 | root.classList.add("dim-combo"); |
| 14 | root.innerHTML = ""; |
| 15 | |
| 16 | const input = document.createElement("input"); |
| 17 | input.type = "text"; |
| 18 | input.className = "dim-combo-input"; |
| 19 | input.placeholder = placeholder; |
| 20 | input.autocomplete = "off"; |
| 21 | input.setAttribute("role", "combobox"); |
| 22 | input.setAttribute("aria-autocomplete", "list"); |
| 23 | input.setAttribute("aria-expanded", "false"); |
| 24 | |
| 25 | const toggle = document.createElement("button"); |
| 26 | toggle.type = "button"; |
| 27 | toggle.className = "dim-combo-toggle"; |
| 28 | toggle.tabIndex = -1; |
| 29 | toggle.setAttribute("aria-label", "Show dimensions"); |
| 30 | toggle.textContent = "▾"; |
| 31 | |
| 32 | const list = document.createElement("ul"); |
| 33 | list.className = "dim-combo-list"; |
| 34 | list.hidden = true; |
| 35 | list.setAttribute("role", "listbox"); |
| 36 | |
| 37 | root.append(input, toggle, list); |
| 38 | |
| 39 | /** @type {{ id: string, label: string, disabled?: boolean, hint?: string }[]} */ |
| 40 | let options = []; |
| 41 | let selectedId = null; |
| 42 | let open = false; |
| 43 | |
| 44 | function selectedOption () |
| 45 | { |
| 46 | return options.find((opt) => opt.id === selectedId) ?? null; |
| 47 | } |
| 48 | |
| 49 | function displayLabel (opt) |
| 50 | { |
| 51 | if (!opt) |
| 52 | { return ""; } |
| 53 | return opt.label && opt.label !== opt.id ? `${opt.label} (${opt.id})` : opt.id; |
| 54 | } |
| 55 | |
| 56 | function syncInputFromSelection () |
| 57 | { |
| 58 | const opt = selectedOption(); |
| 59 | input.value = opt ? displayLabel(opt) : (selectedId ?? ""); |
| 60 | } |
| 61 | |
| 62 | function setOpen (next) |
| 63 | { |
| 64 | open = next; |
| 65 | list.hidden = !open; |
| 66 | input.setAttribute("aria-expanded", open ? "true" : "false"); |
| 67 | if (open) |
| 68 | { renderList(); } |
| 69 | } |
| 70 | |
| 71 | function filtered () |
| 72 | { |
| 73 | const q = input.value.trim().toLowerCase(); |
| 74 | if (!q) |
| 75 | { return options.slice(); } |
| 76 | return options.filter((opt) => |
| 77 | opt.id.toLowerCase().includes(q) |
| 78 | || (opt.label ?? "").toLowerCase().includes(q) |
| 79 | || (opt.hint ?? "").toLowerCase().includes(q) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | function exactMatchId (text) |
| 84 | { |
| 85 | const q = String(text ?? "").trim().toLowerCase(); |
| 86 | if (!q) |
| 87 | { return null; } |
| 88 | const hit = options.find((opt) => opt.id.toLowerCase() === q || (opt.label ?? "").toLowerCase() === q); |
| 89 | return hit?.id ?? null; |
| 90 | } |
| 91 | |
| 92 | function renderList () |
| 93 | { |
| 94 | list.innerHTML = ""; |
| 95 | const rows = filtered(); |
| 96 | const typed = input.value.trim(); |
| 97 | const typedLower = typed.toLowerCase(); |
| 98 | const hasExact = options.some((opt) => |
| 99 | opt.id.toLowerCase() === typedLower || (opt.label ?? "").toLowerCase() === typedLower |
| 100 | ); |
| 101 | |
| 102 | for (const opt of rows) |
| 103 | { |
| 104 | const li = document.createElement("li"); |
| 105 | li.className = "dim-combo-option"; |
| 106 | if (opt.disabled) |
| 107 | { li.classList.add("disabled"); } |
| 108 | if (opt.id === selectedId) |
| 109 | { li.classList.add("selected"); } |
| 110 | li.setAttribute("role", "option"); |
| 111 | li.dataset.id = opt.id; |
| 112 | const main = document.createElement("span"); |
| 113 | main.className = "dim-combo-option-label"; |
| 114 | main.textContent = displayLabel(opt); |
| 115 | li.append(main); |
| 116 | if (opt.hint) |
| 117 | { |
| 118 | const hint = document.createElement("span"); |
| 119 | hint.className = "dim-combo-option-hint"; |
| 120 | hint.textContent = opt.hint; |
| 121 | li.append(hint); |
| 122 | } |
| 123 | li.addEventListener("mousedown", (event) => |
| 124 | { |
| 125 | event.preventDefault(); |
| 126 | choose(opt.id); |
| 127 | }); |
| 128 | list.append(li); |
| 129 | } |
| 130 | |
| 131 | if (allowCreate && typed && !hasExact) |
| 132 | { |
| 133 | const li = document.createElement("li"); |
| 134 | li.className = "dim-combo-option create"; |
| 135 | li.setAttribute("role", "option"); |
| 136 | li.textContent = `Create «${typed}»`; |
| 137 | li.addEventListener("mousedown", (event) => |
| 138 | { |
| 139 | event.preventDefault(); |
| 140 | setOpen(false); |
| 141 | onCreate?.(typed); |
| 142 | }); |
| 143 | list.append(li); |
| 144 | } |
| 145 | |
| 146 | if (!list.children.length) |
| 147 | { |
| 148 | const empty = document.createElement("li"); |
| 149 | empty.className = "dim-combo-empty"; |
| 150 | empty.textContent = "No dimensions"; |
| 151 | list.append(empty); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | function choose (id) |
| 156 | { |
| 157 | selectedId = id; |
| 158 | syncInputFromSelection(); |
| 159 | setOpen(false); |
| 160 | onSelect?.(id); |
| 161 | } |
| 162 | |
| 163 | input.addEventListener("focus", () => |
| 164 | { |
| 165 | setOpen(true); |
| 166 | }); |
| 167 | input.addEventListener("mousedown", (event) => |
| 168 | { |
| 169 | if (!open) |
| 170 | { |
| 171 | event.preventDefault(); |
| 172 | setOpen(true); |
| 173 | input.focus(); |
| 174 | } |
| 175 | }); |
| 176 | toggle.addEventListener("mousedown", (event) => |
| 177 | { |
| 178 | event.preventDefault(); |
| 179 | if (open) |
| 180 | { |
| 181 | syncInputFromSelection(); |
| 182 | setOpen(false); |
| 183 | input.blur(); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | setOpen(true); |
| 188 | input.focus(); |
| 189 | } |
| 190 | }); |
| 191 | input.addEventListener("input", () => |
| 192 | { |
| 193 | setOpen(true); |
| 194 | renderList(); |
| 195 | }); |
| 196 | input.addEventListener("keydown", (event) => |
| 197 | { |
| 198 | if (event.key === "Escape") |
| 199 | { |
| 200 | syncInputFromSelection(); |
| 201 | setOpen(false); |
| 202 | input.blur(); |
| 203 | return; |
| 204 | } |
| 205 | if (event.key === "Enter") |
| 206 | { |
| 207 | event.preventDefault(); |
| 208 | const typed = input.value.trim(); |
| 209 | const exact = exactMatchId(typed); |
| 210 | if (exact) |
| 211 | { |
| 212 | choose(exact); |
| 213 | return; |
| 214 | } |
| 215 | const rows = filtered().filter((opt) => !opt.disabled); |
| 216 | if (rows.length === 1) |
| 217 | { |
| 218 | choose(rows[0].id); |
| 219 | return; |
| 220 | } |
| 221 | if (allowCreate && typed && !exactMatchId(typed)) |
| 222 | { |
| 223 | setOpen(false); |
| 224 | onCreate?.(typed); |
| 225 | } |
| 226 | } |
| 227 | }); |
| 228 | input.addEventListener("blur", () => |
| 229 | { |
| 230 | window.setTimeout(() => |
| 231 | { |
| 232 | if (!root.contains(document.activeElement)) |
| 233 | { |
| 234 | syncInputFromSelection(); |
| 235 | setOpen(false); |
| 236 | } |
| 237 | }, 0); |
| 238 | }); |
| 239 | |
| 240 | return { |
| 241 | setOptions (nextOptions) |
| 242 | { |
| 243 | options = Array.isArray(nextOptions) ? nextOptions.slice() : []; |
| 244 | if (selectedId && !options.some((opt) => opt.id === selectedId)) |
| 245 | { selectedId = options[0]?.id ?? null; } |
| 246 | syncInputFromSelection(); |
| 247 | if (open) |
| 248 | { renderList(); } |
| 249 | }, |
| 250 | setValue (id) |
| 251 | { |
| 252 | selectedId = id; |
| 253 | syncInputFromSelection(); |
| 254 | if (open) |
| 255 | { renderList(); } |
| 256 | }, |
| 257 | getValue () |
| 258 | { |
| 259 | return selectedId; |
| 260 | }, |
| 261 | close () |
| 262 | { |
| 263 | setOpen(false); |
| 264 | } |
| 265 | }; |
| 266 | } |