import { OVERWORLD_ID, normalizeDimensionId } from "/shared/world/dimensions.js"; import { PALETTE_TABS, listPaletteEntries, paletteLabelMatches, matchingPaletteEntryKeys } from "./paletteEntries.js"; import { createPaletteCard, firstSpriteUrl } from "./paletteCard.js"; /** * Right-sidebar placeable browser. Picking a card calls `onSelect` with the * same payload as the Select placeable modal (`selectTool` in the editor). */ export function initPalettePanel ({ onSelect, catalog, imageUrlFor, getSelection, getDimensionId }) { const searchInput = document.getElementById("palette-panel-search"); const tabsEl = document.getElementById("palette-panel-tabs"); const resultsEl = document.getElementById("palette-panel-results"); let activeTab = PALETTE_TABS[0].id; let searchQuery = ""; let renderedSignature = ""; /** @type {ReturnType} */ let allEntries = []; /** @type {ReturnType} */ let visibleEntries = []; function dimensionId () { return normalizeDimensionId(getDimensionId?.() ?? OVERWORLD_ID); } function updateTabStyles () { if (!tabsEl) { return; } const buttons = tabsEl.querySelectorAll("[data-palette-tab]"); for (const button of buttons) { const on = button.dataset.paletteTab === activeTab; button.classList.toggle("active", on); button.setAttribute("aria-selected", on ? "true" : "false"); } } function renderTabs () { if (!tabsEl) { return; } tabsEl.replaceChildren(); for (const tab of PALETTE_TABS) { const button = document.createElement("button"); button.type = "button"; button.className = "settings-tab"; button.dataset.paletteTab = tab.id; button.id = `palette-tab-${tab.id}`; button.setAttribute("role", "tab"); button.textContent = tab.label; button.addEventListener("click", () => { if (activeTab === tab.id) { return; } activeTab = tab.id; updateTabStyles(); renderList(); }); tabsEl.append(button); } updateTabStyles(); } function paintGrid () { if (!resultsEl) { return; } resultsEl.replaceChildren(); if (!visibleEntries.length) { const empty = document.createElement("p"); empty.className = "hint"; empty.textContent = searchQuery.trim() ? "No matches for that search." : "Nothing to show."; resultsEl.append(empty); return; } const selected = matchingPaletteEntryKeys(visibleEntries, allEntries, getSelection?.() ?? null); const grid = document.createElement("div"); grid.className = "palette-grid"; for (const entry of visibleEntries) { grid.append(createPaletteCard({ label: entry.label, url: firstSpriteUrl(entry.spriteNames, imageUrlFor), key: entry.key, selected: selected.has(entry.key), onClick () { onSelect?.(entry.selection); } })); } resultsEl.append(grid); } function applyHighlight () { if (!resultsEl) { return; } const selected = matchingPaletteEntryKeys(visibleEntries, allEntries, getSelection?.() ?? null); for (const card of resultsEl.querySelectorAll(".palette-card")) { const on = selected.has(card.dataset.paletteKey); card.classList.toggle("selected", on); card.setAttribute("aria-pressed", on ? "true" : "false"); } } function renderList () { const dim = dimensionId(); allEntries = listPaletteEntries(activeTab, { catalog, dimensionId: dim }); visibleEntries = allEntries.filter((entry) => paletteLabelMatches(entry.label, searchQuery)); const signature = [ dim, activeTab, searchQuery, visibleEntries.map((entry) => `${entry.key}\t${entry.spriteNames.join(",")}`).join("\n") ].join("\u0000"); if (signature !== renderedSignature) { renderedSignature = signature; paintGrid(); return; } applyHighlight(); } renderTabs(); searchInput?.addEventListener("input", () => { searchQuery = searchInput.value ?? ""; renderList(); }); renderList(); return { /** Re-read the active tool and the dimension being edited. */ sync () { renderList(); } }; }