| 1 | /** |
| 2 | * One placeable card (sprite thumb + name), shared by the sidebar and the modal. |
| 3 | */ |
| 4 | export function createPaletteCard ({ label, url, key, selected = false, onClick }) |
| 5 | { |
| 6 | const button = document.createElement("button"); |
| 7 | button.type = "button"; |
| 8 | button.className = "palette-card"; |
| 9 | button.title = label; |
| 10 | button.dataset.paletteKey = key ?? ""; |
| 11 | button.setAttribute("aria-pressed", selected ? "true" : "false"); |
| 12 | if (selected) |
| 13 | { button.classList.add("selected"); } |
| 14 | |
| 15 | const thumb = document.createElement("div"); |
| 16 | thumb.className = "palette-thumb"; |
| 17 | if (url) |
| 18 | { |
| 19 | const img = document.createElement("img"); |
| 20 | img.alt = ""; |
| 21 | img.src = url; |
| 22 | img.onerror = () => |
| 23 | { |
| 24 | img.remove(); |
| 25 | thumb.textContent = label.slice(0, 1); |
| 26 | }; |
| 27 | thumb.append(img); |
| 28 | } |
| 29 | else |
| 30 | { thumb.textContent = label.slice(0, 1); } |
| 31 | |
| 32 | const caption = document.createElement("span"); |
| 33 | caption.textContent = label; |
| 34 | button.append(thumb, caption); |
| 35 | button.addEventListener("click", onClick); |
| 36 | return button; |
| 37 | } |
| 38 | |
| 39 | /** First sprite name that resolves to a URL. */ |
| 40 | export function firstSpriteUrl (names, imageUrlFor) |
| 41 | { |
| 42 | if (typeof imageUrlFor !== "function") |
| 43 | { return null; } |
| 44 | for (const name of names ?? []) |
| 45 | { |
| 46 | const url = imageUrlFor(name); |
| 47 | if (url) |
| 48 | { return url; } |
| 49 | } |
| 50 | return null; |
| 51 | } |