BlueGit

timfa questward

Files

questward / client / src / editor / palettePanel.js

palettePanel.js

feat: add a palette tab to the world editor sidebar
timfa project_85689633_bot_760517ee9267c86ff1d6c3a0237abf7d · pushed by timfa · 6 weeks ago
4251842f History
palettePanel.js 4.0 KiB Download Raw
1import { OVERWORLD_ID, normalizeDimensionId } from "/shared/world/dimensions.js";
2import
3{
4 PALETTE_TABS,
5 listPaletteEntries,
6 paletteLabelMatches,
7 matchingPaletteEntryKeys
8} from "./paletteEntries.js";
9import { createPaletteCard, firstSpriteUrl } from "./paletteCard.js";
10
11/**
12 * Right-sidebar placeable browser. Picking a card calls `onSelect` with the
13 * same payload as the Select placeable modal (`selectTool` in the editor).
14 */
15export function initPalettePanel ({
16 onSelect,
17 catalog,
18 imageUrlFor,
19 getSelection,
20 getDimensionId
21})
22{
23 const searchInput = document.getElementById("palette-panel-search");
24 const tabsEl = document.getElementById("palette-panel-tabs");
25 const resultsEl = document.getElementById("palette-panel-results");
26 let activeTab = PALETTE_TABS[0].id;
27 let searchQuery = "";
28 let renderedSignature = "";
29 /** @type {ReturnType<typeof listPaletteEntries>} */
30 let allEntries = [];
31 /** @type {ReturnType<typeof listPaletteEntries>} */
32 let visibleEntries = [];
33
34 function dimensionId ()
35 {
36 return normalizeDimensionId(getDimensionId?.() ?? OVERWORLD_ID);
37 }
38
39 function updateTabStyles ()
40 {
41 if (!tabsEl)
42 { return; }
43 const buttons = tabsEl.querySelectorAll("[data-palette-tab]");
44 for (const button of buttons)
45 {
46 const on = button.dataset.paletteTab === activeTab;
47 button.classList.toggle("active", on);
48 button.setAttribute("aria-selected", on ? "true" : "false");
49 }
50 }
51
52 function renderTabs ()
53 {
54 if (!tabsEl)
55 { return; }
56 tabsEl.replaceChildren();
57 for (const tab of PALETTE_TABS)
58 {
59 const button = document.createElement("button");
60 button.type = "button";
61 button.className = "settings-tab";
62 button.dataset.paletteTab = tab.id;
63 button.id = `palette-tab-${tab.id}`;
64 button.setAttribute("role", "tab");
65 button.textContent = tab.label;
66 button.addEventListener("click", () =>
67 {
68 if (activeTab === tab.id)
69 { return; }
70 activeTab = tab.id;
71 updateTabStyles();
72 renderList();
73 });
74 tabsEl.append(button);
75 }
76 updateTabStyles();
77 }
78
79 function paintGrid ()
80 {
81 if (!resultsEl)
82 { return; }
83 resultsEl.replaceChildren();
84 if (!visibleEntries.length)
85 {
86 const empty = document.createElement("p");
87 empty.className = "hint";
88 empty.textContent = searchQuery.trim()
89 ? "No matches for that search."
90 : "Nothing to show.";
91 resultsEl.append(empty);
92 return;
93 }
94 const selected = matchingPaletteEntryKeys(visibleEntries, allEntries, getSelection?.() ?? null);
95 const grid = document.createElement("div");
96 grid.className = "palette-grid";
97 for (const entry of visibleEntries)
98 {
99 grid.append(createPaletteCard({
100 label: entry.label,
101 url: firstSpriteUrl(entry.spriteNames, imageUrlFor),
102 key: entry.key,
103 selected: selected.has(entry.key),
104 onClick ()
105 {
106 onSelect?.(entry.selection);
107 }
108 }));
109 }
110 resultsEl.append(grid);
111 }
112
113 function applyHighlight ()
114 {
115 if (!resultsEl)
116 { return; }
117 const selected = matchingPaletteEntryKeys(visibleEntries, allEntries, getSelection?.() ?? null);
118 for (const card of resultsEl.querySelectorAll(".palette-card"))
119 {
120 const on = selected.has(card.dataset.paletteKey);
121 card.classList.toggle("selected", on);
122 card.setAttribute("aria-pressed", on ? "true" : "false");
123 }
124 }
125
126 function renderList ()
127 {
128 const dim = dimensionId();
129 allEntries = listPaletteEntries(activeTab, { catalog, dimensionId: dim });
130 visibleEntries = allEntries.filter((entry) => paletteLabelMatches(entry.label, searchQuery));
131 const signature = [
132 dim,
133 activeTab,
134 searchQuery,
135 visibleEntries.map((entry) => `${entry.key}\t${entry.spriteNames.join(",")}`).join("\n")
136 ].join("\u0000");
137 if (signature !== renderedSignature)
138 {
139 renderedSignature = signature;
140 paintGrid();
141 return;
142 }
143 applyHighlight();
144 }
145
146 renderTabs();
147 searchInput?.addEventListener("input", () =>
148 {
149 searchQuery = searchInput.value ?? "";
150 renderList();
151 });
152 renderList();
153
154 return {
155 /** Re-read the active tool and the dimension being edited. */
156 sync ()
157 {
158 renderList();
159 }
160 };
161}