Media page shows the whole library, multi-file upload; pill pickers on Redeems

- src/library.js: one list of every media file (built-in public/alerts + uploads),
  shared by the Media page and the Redeems pickers.
- Media page rebuilt: drag & drop / multi-file upload with progress (audio, video,
  images for stickers), Videos / Sounds / Stickers & art with previews, size,
  'used by N' and a filter; only uploads can be deleted.
- Fix: /media no longer 301-redirects to /media/ (express.static redirect:false).
- Redeems: sound / video / sticker / TTS voice are pills (click to change, x to
  clear) with a type-to-search list (arrows, Enter, Esc) instead of dropdowns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-22 16:24:18 +02:00
parent 8ac8fe024c
commit a32a55c264
6 changed files with 313 additions and 109 deletions

View file

@ -50,6 +50,25 @@
.thumb { width:34px; height:34px; object-fit:contain; }
.vol-out { font-size:.75rem; color:var(--pico-muted-color); width:2.6em; }
.preview { margin-top:.5rem; max-width:320px; border-radius:8px; background:#0006; }
/* pickers: a pill with the current choice (click to change, x to clear) or "+ …"; typing filters */
.pick { position:relative; display:inline-flex; align-items:center; }
.chip { display:inline-flex; align-items:center; gap:.35rem; padding:.18rem .3rem .18rem .6rem; border-radius:99px; font-size:.82rem;
background:rgba(1,114,173,.18); border:1px solid rgba(1,114,173,.55); cursor:pointer; max-width:340px; }
.chip .lbl { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.chip img { width:22px; height:22px; object-fit:contain; }
.chip .x { border:0; background:none; color:inherit; padding:0 .3rem; margin:0; width:auto; font-size:.95rem; line-height:1; opacity:.7; cursor:pointer; }
.chip .x:hover { opacity:1; }
.chip .tag { font-size:.65rem; opacity:.7; }
.chip.add { background:none; border-style:dashed; color:var(--pico-muted-color); padding-right:.6rem; }
.pick.disabled .chip { opacity:.45; }
.pick input.q { margin:0; padding:.25rem .6rem; font-size:.82rem; height:auto; width:260px; border-radius:99px; }
.pick ul.opts { position:absolute; top:100%; left:0; z-index:30; list-style:none; margin:.2rem 0 0; padding:.25rem; min-width:260px; max-height:260px; overflow:auto;
background:var(--pico-card-background-color); border:1px solid var(--pico-muted-border-color); border-radius:10px; box-shadow:0 8px 20px rgba(0,0,0,.4); }
.pick ul.opts li { list-style:none; padding:.25rem .5rem; border-radius:6px; font-size:.82rem; cursor:pointer; display:flex; align-items:center; gap:.4rem; }
.pick ul.opts li img { width:22px; height:22px; object-fit:contain; }
.pick ul.opts li .tag { margin-left:auto; font-size:.68rem; color:var(--pico-muted-color); }
.pick ul.opts li.on { background:rgba(1,114,173,.3); }
.pick ul.opts li.none { color:var(--pico-muted-color); cursor:default; }
[data-show] { display:none !important; }
.vcard[data-display="cassette"] [data-show~="cassette"],
.vcard[data-display="sound"] [data-show~="sound"],
@ -121,31 +140,22 @@
</div>
<div class="vrow" data-show="cassette sound">
<span class="field grow"><select data-f="sound" title="Sound that plays (TTS on: the voice replaces it)">
<option value="">— silent —</option>
<% for (const s of sounds) { %><option value="<%= s.url %>" <%= v.sound === s.url ? 'selected' : '' %>><%= s.label %></option><% } %>
</select></span>
<input type="hidden" data-f="sound" value="<%= v.sound || '' %>">
<span class="pick" data-pick="sound" title="Sound that plays (TTS on: the voice replaces it)"></span>
<button type="button" class="outline btn-sm" data-play-sound title="Play the sound here in the browser">▶</button>
</div>
<div class="vrow" data-show="cassette">
<span class="field"><img class="thumb" data-thumb alt="">
<select data-f="art" style="width:auto;" title="Sticker in the cassette's corner">
<option value="">headset Streamer</option>
<% for (const st of stickers) { %><option value="<%= st.url %>" <%= v.art === st.url ? 'selected' : '' %>><%= st.label %></option><% } %>
</select></span>
<input type="hidden" data-f="art" value="<%= v.art || '' %>">
<span class="pick" data-pick="art" title="Sticker in the cassette's corner"></span>
<span class="field grow"><input data-f="caption" value="<%= v.caption %>" title="Text after the viewer's name on the paper strip. {reward} = reward name, {variation} = this variation's name. The viewer's typed message is added automatically."></span>
<select data-f="tts_voice_id" style="width:auto;" title="Read the viewer's typed text aloud (voice from the TTS page), shown as a big paper note. The voice replaces the sound. The reward needs 'Require Viewer to Enter Text' on Twitch.">
<option value="">TTS: off</option>
<% for (const tv of ttsVoices) { %><option value="<%= tv.id %>" <%= v.tts_voice_id === tv.id ? 'selected' : '' %>>TTS: <%= tv.name %></option><% } %>
</select>
<input type="hidden" data-f="tts_voice_id" value="<%= v.tts_voice_id || '' %>">
<span class="pick" data-pick="tts_voice_id" title="Read the viewer's typed text aloud (voice from the TTS page), shown as a big paper note. The voice replaces the sound. The reward needs 'Require Viewer to Enter Text' on Twitch."></span>
</div>
<div class="vrow" data-show="video">
<span class="field grow"><select data-f="video" title="Video file: uploads on the Media page (transparent WebM looks best)">
<option value="">— pick a video —</option>
<% for (const o of videos) { %><option value="<%= o.url %>" <%= v.video === o.url ? 'selected' : '' %>><%= o.label %></option><% } %>
</select></span>
<input type="hidden" data-f="video" value="<%= v.video || '' %>">
<span class="pick" data-pick="video" title="Video file: built-in or uploaded on the Media page (transparent WebM looks best)"></span>
<button type="button" class="outline btn-sm" data-play-video title="Preview the video here">▶</button>
<% if (!videos.length) { %><a href="/media" class="meta">upload a video on Media</a><% } %>
</div>
@ -166,6 +176,15 @@
<script>
(() => {
const RARITY = <%- JSON.stringify(rarity) %>;
// picker options (same files as the Media page)
const OPTS = {
sound: <%- JSON.stringify(sounds) %>,
video: <%- JSON.stringify(videos) %>,
art: <%- JSON.stringify(stickers) %>,
tts_voice_id: <%- JSON.stringify(ttsVoices.map(t => ({ url: String(t.id), label: t.name }))) %>,
};
const ADD = { sound: '+ sound', video: '+ video', art: '+ sticker', tts_voice_id: '+ TTS voice' };
const DEFAULT_STICKER = { url: '', label: 'headset Streamer (default)', img: '/alerts/stickers/default-sticker.png' };
const DRAFT_KEY = 'redeemsDraft', OPEN_KEY = 'redeemsOpen';
const stateEl = document.getElementById('save-state');
const dirty = new Set();
@ -205,10 +224,9 @@
c.dataset.display = o.display;
c.classList.toggle('off', !o.enabled);
c.querySelector('[data-vol]').textContent = Math.round(o.volume * 100) + '%';
const art = c.querySelector('[data-f="art"]'), th = c.querySelector('[data-thumb]');
th.src = art.value || '/alerts/stickers/default-sticker.png'; // cassette.js DEFAULT_STICKER
const snd = c.querySelector('[data-f="sound"]');
snd.disabled = !!o.tts_voice_id && o.display === 'cassette';
const ttsOn = !!o.tts_voice_id && o.display === 'cassette';
c.querySelector('[data-pick="sound"]').classList.toggle('disabled', ttsOn);
c.querySelectorAll('[data-pick]').forEach(renderPick);
chances(c.closest('[data-trigger]'));
}
function chances(t) {
@ -219,6 +237,62 @@
if (el && cs.length) el.textContent = `${n}/${cs.length} on`;
}
// --- pickers ---
const esc = s => String(s).replace(/[&<>"]/g, ch => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[ch]));
const field = el => el.closest('.vcard').querySelector(`[data-f="${el.dataset.pick}"]`);
function setVal(el, val) {
const h = field(el); h.value = val;
h.dispatchEvent(new Event('change', { bubbles: true })); // -> sync + unsaved change
}
function renderPick(el) {
if (el.querySelector('input.q')) return; // editing right now
const f = el.dataset.pick, val = field(el).value;
let o = OPTS[f].find(x => String(x.url) === String(val));
if (!o && val) o = { url: val, label: val.split('/').pop() + ' (missing)' };
if (!o && f === 'art') o = DEFAULT_STICKER;
if (!o) { el.innerHTML = `<span class="chip add" tabindex="0">${ADD[f]}</span>`; return; }
const img = f === 'art' ? `<img src="${esc(o.img || o.url)}" alt="">` : '';
const clear = val ? '<button type="button" class="x" title="Remove">×</button>' : '';
el.innerHTML = `<span class="chip" tabindex="0" title="Click to change">${img}<span class="lbl">${esc(o.label)}</span>${o.note ? `<span class="tag">${esc(o.note)}</span>` : ''}${clear}</span>`;
}
function openPick(el) {
if (el.querySelector('input.q')) return;
const f = el.dataset.pick, list = OPTS[f];
el.innerHTML = '<input class="q" placeholder="type to search…" autocomplete="off"><ul class="opts"></ul>';
const q = el.querySelector('input.q'), ul = el.querySelector('ul.opts');
let hits = [], at = 0;
const draw = () => {
const s = q.value.trim().toLowerCase();
hits = list.filter(o => !s || o.label.toLowerCase().includes(s) || String(o.url).toLowerCase().includes(s)).slice(0, 80);
at = Math.min(at, Math.max(0, hits.length - 1));
ul.innerHTML = hits.length ? hits.map((o, i) => `<li data-i="${i}" class="${i === at ? 'on' : ''}">${f === 'art' ? `<img src="${esc(o.url)}" alt="">` : ''}${esc(o.label)}${o.note ? `<span class="tag">${esc(o.note)}</span>` : ''}</li>`).join('')
: '<li class="none">nothing matches</li>';
ul.querySelector('li.on')?.scrollIntoView({ block: 'nearest' });
};
const close = () => { el.innerHTML = ''; renderPick(el); };
const choose = i => { const o = hits[i]; if (!o) return; el.innerHTML = ''; setVal(el, String(o.url)); renderPick(el); };
q.addEventListener('input', () => { at = 0; draw(); });
q.addEventListener('keydown', e => {
if (e.key === 'ArrowDown') { e.preventDefault(); at = Math.min(at + 1, hits.length - 1); draw(); }
else if (e.key === 'ArrowUp') { e.preventDefault(); at = Math.max(at - 1, 0); draw(); }
else if (e.key === 'Enter') { e.preventDefault(); choose(at); }
else if (e.key === 'Escape') { e.preventDefault(); close(); }
});
ul.addEventListener('mousedown', e => { const li = e.target.closest('li[data-i]'); if (li) { e.preventDefault(); choose(+li.dataset.i); } });
q.addEventListener('blur', () => setTimeout(() => { if (el.contains(q)) close(); }, 120));
draw(); q.focus();
}
document.addEventListener('click', e => {
const x = e.target.closest('.pick .x');
if (x) { e.stopPropagation(); const el = x.closest('.pick'); setVal(el, ''); renderPick(el); return; }
const chip = e.target.closest('.pick .chip');
if (chip) openPick(chip.closest('.pick'));
});
document.addEventListener('keydown', e => {
const chip = e.target.closest?.('.pick .chip');
if (chip && (e.key === 'Enter' || e.key === ' ')) { e.preventDefault(); openPick(chip.closest('.pick')); }
});
function markDirty(c) {
dirty.add(c.dataset.id); c.classList.add('dirty');
const d = store.get(DRAFT_KEY) || { at: Date.now(), cards: {} };