/** * Modal picker for editor dimensions: list + create. * options: { id, label, disabled?, hint? }[] */ export function initDimensionPickerModal ({ onSelect, onCreate }) { const openBtn = document.getElementById("open-dimension-picker"); const currentEl = document.getElementById("dimension-current"); const backdrop = document.getElementById("dimension-picker-backdrop"); const modal = document.getElementById("dimension-picker-modal"); const listEl = document.getElementById("dimension-picker-list"); const closeBtn = document.getElementById("dimension-picker-close"); const createBtn = document.getElementById("dimension-picker-create"); const createIdInput = document.getElementById("dimension-picker-create-id"); const createNameInput = document.getElementById("dimension-picker-create-name"); /** @type {{ id: string, label: string, disabled?: boolean, hint?: string }[]} */ let options = []; let selectedId = null; let open = false; function displayLabel (opt) { if (!opt) { return selectedId ?? "—"; } return opt.label && opt.label !== opt.id ? `${opt.label} (${opt.id})` : opt.id; } function syncCurrentLabel () { if (!currentEl) { return; } const opt = options.find((entry) => entry.id === selectedId) ?? null; currentEl.textContent = displayLabel(opt); } function setOpen (visible) { open = visible; if (backdrop) { backdrop.hidden = !visible; } if (modal) { modal.hidden = !visible; } if (visible) { renderList(); createIdInput?.focus(); } } function renderList () { if (!listEl) { return; } listEl.replaceChildren(); if (!options.length) { const empty = document.createElement("p"); empty.className = "hint"; empty.textContent = "No dimensions yet."; listEl.append(empty); return; } for (const opt of options) { const btn = document.createElement("button"); btn.type = "button"; btn.className = "dimension-picker-option"; if (opt.id === selectedId) { btn.classList.add("active"); } if (opt.disabled) { btn.classList.add("soft-deleted"); } const title = document.createElement("strong"); title.textContent = opt.label && opt.label !== opt.id ? opt.label : opt.id; btn.append(title); if (opt.label && opt.label !== opt.id) { const idLine = document.createElement("span"); idLine.className = "dimension-picker-option-id"; idLine.textContent = opt.id; btn.append(idLine); } if (opt.hint) { const hint = document.createElement("span"); hint.className = "dimension-picker-option-hint"; hint.textContent = opt.hint; btn.append(hint); } btn.addEventListener("click", () => { setOpen(false); onSelect?.(opt.id); }); listEl.append(btn); } } function submitCreate () { const rawId = createIdInput?.value?.trim() ?? ""; if (!rawId) { createIdInput?.focus(); return; } const displayName = createNameInput?.value?.trim() || rawId; if (createIdInput) { createIdInput.value = ""; } if (createNameInput) { createNameInput.value = ""; } setOpen(false); onCreate?.(rawId, displayName); } openBtn?.addEventListener("click", () => setOpen(true)); closeBtn?.addEventListener("click", () => setOpen(false)); backdrop?.addEventListener("click", () => setOpen(false)); createBtn?.addEventListener("click", () => submitCreate()); createIdInput?.addEventListener("keydown", (event) => { if (event.key === "Enter") { event.preventDefault(); submitCreate(); } }); createNameInput?.addEventListener("keydown", (event) => { if (event.key === "Enter") { event.preventDefault(); submitCreate(); } }); return { isOpen: () => open, setOptions (nextOptions) { options = Array.isArray(nextOptions) ? nextOptions.slice() : []; if (selectedId && !options.some((opt) => opt.id === selectedId)) { selectedId = options[0]?.id ?? null; } syncCurrentLabel(); if (open) { renderList(); } }, setValue (id) { selectedId = id; syncCurrentLabel(); if (open) { renderList(); } }, getValue () { return selectedId; }, close () { setOpen(false); } }; }