// presenter-picker.mjs — jump to any slide from the presenter window // (spec §5.3, Paul's mockup #pjump). // // The position button opens a popover under it: a field, a count, and a // grid of every slide in the talk, a picture and a title each, the // full-version-only ones marked. A number finds that slide, then the // slides whose number starts with it; words find titles. Enter goes to // the first match, Esc closes and gives the focus back to the position. // Only the grid scrolls, inside itself, and opening brings the slide // on screen into view in it. // // 105 slides must not mean 105 pictures at once: a tile asks for its // picture when it first comes into view in the grid, and not before. // (loading="lazy" alone is not enough; Chromium loads a page-and-a-half // ahead, which here is most of the talk.) Every tile in view, even // partly, has its picture; the row just below it is asked for too (the // 80px margin is less than two rows), and no more. // // Without scripts the position is a link to ?pick=1 (presenter_pick.html); // this takes it over. Every word comes from the page (data-text-pick-*). import { fill } from './talk-draw.mjs' const words = s => String(s || '').toLowerCase().split(/\s+/).filter(Boolean) /** * The slides a query finds, in the order to show them. A query of * digits finds the slide with that number first, then those whose * number starts with it; words must all be in the title, in any case. * Nothing typed finds the whole talk. */ export function matchSlides (slides, query) { const q = String(query || '').trim() if (!q) return slides.slice() if (/^\d+$/.test(q)) { const exact = slides.filter(s => String(s.number) === q) const starts = slides.filter(s => String(s.number) !== q && String(s.number).startsWith(q)) return exact.concat(starts) } const want = words(q) return slides.filter(s => { const title = String(s.title || '').toLowerCase() return want.every(w => title.includes(w)) }) } /** The tile an arrow key moves to from tile i, in rows of cols; -1 is up out of the grid. */ export function gridStep (i, key, cols, n) { if (key === 'ArrowRight') return Math.min(i + 1, n - 1) if (key === 'ArrowLeft') return Math.max(i - 1, 0) if (key === 'ArrowDown') return Math.min(i + cols, n - 1) if (key === 'ArrowUp') return i - cols < 0 ? -1 : i - cols return i } function el (tag, attr, text) { const e = document.createElement(tag) if (attr) e.setAttribute(attr, '') if (text !== undefined) e.textContent = text return e } /** * mountPicker({root, talk, position, go}): talk.slides is the talk's * slides ({id, number, title, full, thumb, html}) and talk.seq the * version they are of, position() the talk's place ({number}, as the * slide's own), and go(slide) moves the talk there. */ export function mountPicker ({ root, talk, position, go }) { const T = root.dataset const pos = root.querySelector('[presenter-pos]') const head = pos.parentElement pos.setAttribute('role', 'button') pos.setAttribute('aria-haspopup', 'dialog') pos.setAttribute('aria-expanded', 'false') const box = el('div', 'presenter-picker') box.id = 'presenter-picker' box.hidden = true box.setAttribute('role', 'dialog') box.setAttribute('aria-label', T.textPickTitle) pos.setAttribute('aria-controls', box.id) const label = el('label', 'presenter-picker-field') const input = el('input') input.type = 'text' input.autocomplete = 'off' input.placeholder = T.textPickField input.setAttribute('aria-label', T.textPickField) const count = el('span', 'presenter-picker-count') label.append(input, count) const grid = el('div', 'presenter-picker-grid') const none = el('p', 'presenter-picker-none') none.setAttribute('role', 'status') count.setAttribute('role', 'status') box.append(label, none, grid) head.append(box) // One tile per slide, made on first opening and kept: a tile that // moves about keeps its picture. A newer version of the deck brings // each tile up to date (fillTile): its number, its title, its full-only // mark, and, when the slide itself changed, its picture. const tiles = new Map() const drawn = new WeakMap() // what each tile was filled from const lazy = new IntersectionObserver(seen => { for (const e of seen) { if (!e.isIntersecting) continue const img = e.target.querySelector('img') if (img && !img.getAttribute('src')) img.src = img.getAttribute('data-src') lazy.unobserve(e.target) } }, { root: grid, rootMargin: '0px 0px 80px 0px' }) function tile (s) { let t = tiles.get(s.id) if (!t) { t = el('button', 'presenter-pick-item') t.type = 'button' t.setAttribute('data-id', s.id) const img = el('img') img.alt = '' img.width = 1920 img.height = 1080 img.loading = 'lazy' t.append(img, el('span')) t.addEventListener('click', () => choose(tiles.get(s.id).slide)) lazy.observe(t) tiles.set(s.id, t) } fillTile(t, s) return t } // fillTile says s on its tile, changing only what changed. function fillTile (t, s) { t.slide = s const was = drawn.get(t) if (!was || was.number !== s.number || was.title !== s.title || was.full !== !!s.full) { const text = t.querySelector('span') text.replaceChildren(el('b', '', String(s.number)), s.title || '') if (s.full) text.append(' ', el('em', '', '· ' + T.textPickFull)) } const img = t.querySelector('img') if (!was || was.thumb !== s.thumb || was.html !== s.html) { // The picture's address is the same for every version of the // slide, so one drawn since asks with the version it is of. let src = s.thumb || '' if (was && src) src += (src.includes('?') ? '&' : '?') + 'v=' + encodeURIComponent(String(talk.seq || 0)) img.setAttribute('data-src', src) if (img.getAttribute('src')) img.src = src } drawn.set(t, { number: s.number, title: s.title, full: !!s.full, thumb: s.thumb, html: s.html }) } let shown = [] function draw () { const all = talk.slides || [] const hit = matchSlides(all, input.value) shown = hit const here = (position() || {}).number count.textContent = input.value.trim() ? fill(T.textPickMatches, { n: hit.length, total: all.length }) : fill(T.textPickCount, { total: all.length }) none.textContent = hit.length ? '' : T.textPickNone grid.hidden = hit.length === 0 grid.replaceChildren(...hit.map(s => { const t = tile(s) if (s.number === here) t.setAttribute('aria-current', 'true') else t.removeAttribute('aria-current') return t })) } function bringIntoView () { const t = grid.querySelector('[aria-current]') if (t) grid.scrollTop = Math.max(0, t.offsetTop - (grid.clientHeight - t.offsetHeight) / 2) else grid.scrollTop = 0 } function isOpen () { return !box.hidden } function open () { input.value = '' box.hidden = false pos.setAttribute('aria-expanded', 'true') draw() bringIntoView() input.focus({ preventScroll: true }) } function close (refocus = true) { if (!isOpen()) return box.hidden = true pos.setAttribute('aria-expanded', 'false') if (refocus) pos.focus({ preventScroll: true }) } function choose (s) { close() go(s) } // The position is a link that acts as a button: Space opens it too, // and is not the window's Next. pos.addEventListener('keydown', e => { if (e.key !== ' ' || e.metaKey || e.ctrlKey || e.altKey) return e.preventDefault() e.stopPropagation() if (!e.repeat) toggle() }) function toggle () { if (isOpen()) close() else open() } pos.addEventListener('click', e => { e.preventDefault() toggle() }) input.addEventListener('input', () => { draw(); grid.scrollTop = 0 }) // Nothing typed here moves the talk, except what a clicker sends. box.addEventListener('keydown', e => { if (e.key === 'PageDown' || e.key === 'PageUp') return e.stopPropagation() if (e.key === 'Escape') { e.preventDefault() close() } else if (e.key === 'Enter' && e.target === input) { e.preventDefault() if (shown.length) choose(shown[0]) } else if (e.key.startsWith('Arrow') && (e.target === input ? e.key === 'ArrowDown' : grid.contains(e.target))) { arrow(e) } }) // Arrow keys: down from the field into the grid, then about it. function arrow (e) { const list = [...grid.children] if (!list.length) return e.preventDefault() let to if (e.target === input) to = 0 else { const i = list.indexOf(e.target) const cols = list.filter(t => t.offsetTop === list[0].offsetTop).length || 1 to = gridStep(i, e.key, cols, list.length) } if (to < 0) input.focus() else list[to].focus() } document.addEventListener('pointerdown', e => { if (isOpen() && !box.contains(e.target) && !pos.contains(e.target)) close(false) }) return { open, close, isOpen } }