| 1 | // presenter-picker.mjs — jump to any slide from the presenter window |
| 2 | // (spec §5.3, Paul's mockup #pjump). |
| 3 | // |
| 4 | // The position button opens a popover under it: a field, a count, and a |
| 5 | // grid of every slide in the talk, a picture and a title each, the |
| 6 | // full-version-only ones marked. A number finds that slide, then the |
| 7 | // slides whose number starts with it; words find titles. Enter goes to |
| 8 | // the first match, Esc closes and gives the focus back to the position. |
| 9 | // Only the grid scrolls, inside itself, and opening brings the slide |
| 10 | // on screen into view in it. |
| 11 | // |
| 12 | // 105 slides must not mean 105 pictures at once: a tile asks for its |
| 13 | // picture when it first comes into view in the grid, and not before. |
| 14 | // (loading="lazy" alone is not enough; Chromium loads a page-and-a-half |
| 15 | // ahead, which here is most of the talk.) Every tile in view, even |
| 16 | // partly, has its picture; the row just below it is asked for too (the |
| 17 | // 80px margin is less than two rows), and no more. |
| 18 | // |
| 19 | // Without scripts the position is a link to ?pick=1 (presenter_pick.html); |
| 20 | // this takes it over. Every word comes from the page (data-text-pick-*). |
| 21 | import { fill } from './talk-draw.mjs' |
| 22 | |
| 23 | const words = s => String(s || '').toLowerCase().split(/\s+/).filter(Boolean) |
| 24 | |
| 25 | /** |
| 26 | * The slides a query finds, in the order to show them. A query of |
| 27 | * digits finds the slide with that number first, then those whose |
| 28 | * number starts with it; words must all be in the title, in any case. |
| 29 | * Nothing typed finds the whole talk. |
| 30 | */ |
| 31 | export function matchSlides (slides, query) { |
| 32 | const q = String(query || '').trim() |
| 33 | if (!q) return slides.slice() |
| 34 | if (/^\d+$/.test(q)) { |
| 35 | const exact = slides.filter(s => String(s.number) === q) |
| 36 | const starts = slides.filter(s => String(s.number) !== q && String(s.number).startsWith(q)) |
| 37 | return exact.concat(starts) |
| 38 | } |
| 39 | const want = words(q) |
| 40 | return slides.filter(s => { |
| 41 | const title = String(s.title || '').toLowerCase() |
| 42 | return want.every(w => title.includes(w)) |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | /** The tile an arrow key moves to from tile i, in rows of cols; -1 is up out of the grid. */ |
| 47 | export function gridStep (i, key, cols, n) { |
| 48 | if (key === 'ArrowRight') return Math.min(i + 1, n - 1) |
| 49 | if (key === 'ArrowLeft') return Math.max(i - 1, 0) |
| 50 | if (key === 'ArrowDown') return Math.min(i + cols, n - 1) |
| 51 | if (key === 'ArrowUp') return i - cols < 0 ? -1 : i - cols |
| 52 | return i |
| 53 | } |
| 54 | |
| 55 | function el (tag, attr, text) { |
| 56 | const e = document.createElement(tag) |
| 57 | if (attr) e.setAttribute(attr, '') |
| 58 | if (text !== undefined) e.textContent = text |
| 59 | return e |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * mountPicker({root, talk, position, go}): talk.slides is the talk's |
| 64 | * slides ({id, number, title, full, thumb, html}) and talk.seq the |
| 65 | * version they are of, position() the talk's place ({number}, as the |
| 66 | * slide's own), and go(slide) moves the talk there. |
| 67 | */ |
| 68 | export function mountPicker ({ root, talk, position, go }) { |
| 69 | const T = root.dataset |
| 70 | const pos = root.querySelector('[presenter-pos]') |
| 71 | const head = pos.parentElement |
| 72 | pos.setAttribute('role', 'button') |
| 73 | pos.setAttribute('aria-haspopup', 'dialog') |
| 74 | pos.setAttribute('aria-expanded', 'false') |
| 75 | |
| 76 | const box = el('div', 'presenter-picker') |
| 77 | box.id = 'presenter-picker' |
| 78 | box.hidden = true |
| 79 | box.setAttribute('role', 'dialog') |
| 80 | box.setAttribute('aria-label', T.textPickTitle) |
| 81 | pos.setAttribute('aria-controls', box.id) |
| 82 | const label = el('label', 'presenter-picker-field') |
| 83 | const input = el('input') |
| 84 | input.type = 'text' |
| 85 | input.autocomplete = 'off' |
| 86 | input.placeholder = T.textPickField |
| 87 | input.setAttribute('aria-label', T.textPickField) |
| 88 | const count = el('span', 'presenter-picker-count') |
| 89 | label.append(input, count) |
| 90 | const grid = el('div', 'presenter-picker-grid') |
| 91 | const none = el('p', 'presenter-picker-none') |
| 92 | none.setAttribute('role', 'status') |
| 93 | count.setAttribute('role', 'status') |
| 94 | box.append(label, none, grid) |
| 95 | head.append(box) |
| 96 | |
| 97 | // One tile per slide, made on first opening and kept: a tile that |
| 98 | // moves about keeps its picture. A newer version of the deck brings |
| 99 | // each tile up to date (fillTile): its number, its title, its full-only |
| 100 | // mark, and, when the slide itself changed, its picture. |
| 101 | const tiles = new Map() |
| 102 | const drawn = new WeakMap() // what each tile was filled from |
| 103 | const lazy = new IntersectionObserver(seen => { |
| 104 | for (const e of seen) { |
| 105 | if (!e.isIntersecting) continue |
| 106 | const img = e.target.querySelector('img') |
| 107 | if (img && !img.getAttribute('src')) img.src = img.getAttribute('data-src') |
| 108 | lazy.unobserve(e.target) |
| 109 | } |
| 110 | }, { root: grid, rootMargin: '0px 0px 80px 0px' }) |
| 111 | |
| 112 | function tile (s) { |
| 113 | let t = tiles.get(s.id) |
| 114 | if (!t) { |
| 115 | t = el('button', 'presenter-pick-item') |
| 116 | t.type = 'button' |
| 117 | t.setAttribute('data-id', s.id) |
| 118 | const img = el('img') |
| 119 | img.alt = '' |
| 120 | img.width = 1920 |
| 121 | img.height = 1080 |
| 122 | img.loading = 'lazy' |
| 123 | t.append(img, el('span')) |
| 124 | t.addEventListener('click', () => choose(tiles.get(s.id).slide)) |
| 125 | lazy.observe(t) |
| 126 | tiles.set(s.id, t) |
| 127 | } |
| 128 | fillTile(t, s) |
| 129 | return t |
| 130 | } |
| 131 | |
| 132 | // fillTile says s on its tile, changing only what changed. |
| 133 | function fillTile (t, s) { |
| 134 | t.slide = s |
| 135 | const was = drawn.get(t) |
| 136 | if (!was || was.number !== s.number || was.title !== s.title || was.full !== !!s.full) { |
| 137 | const text = t.querySelector('span') |
| 138 | text.replaceChildren(el('b', '', String(s.number)), s.title || '') |
| 139 | if (s.full) text.append(' ', el('em', '', '· ' + T.textPickFull)) |
| 140 | } |
| 141 | const img = t.querySelector('img') |
| 142 | if (!was || was.thumb !== s.thumb || was.html !== s.html) { |
| 143 | // The picture's address is the same for every version of the |
| 144 | // slide, so one drawn since asks with the version it is of. |
| 145 | let src = s.thumb || '' |
| 146 | if (was && src) src += (src.includes('?') ? '&' : '?') + 'v=' + encodeURIComponent(String(talk.seq || 0)) |
| 147 | img.setAttribute('data-src', src) |
| 148 | if (img.getAttribute('src')) img.src = src |
| 149 | } |
| 150 | drawn.set(t, { number: s.number, title: s.title, full: !!s.full, thumb: s.thumb, html: s.html }) |
| 151 | } |
| 152 | |
| 153 | let shown = [] |
| 154 | function draw () { |
| 155 | const all = talk.slides || [] |
| 156 | const hit = matchSlides(all, input.value) |
| 157 | shown = hit |
| 158 | const here = (position() || {}).number |
| 159 | count.textContent = input.value.trim() |
| 160 | ? fill(T.textPickMatches, { n: hit.length, total: all.length }) |
| 161 | : fill(T.textPickCount, { total: all.length }) |
| 162 | none.textContent = hit.length ? '' : T.textPickNone |
| 163 | grid.hidden = hit.length === 0 |
| 164 | grid.replaceChildren(...hit.map(s => { |
| 165 | const t = tile(s) |
| 166 | if (s.number === here) t.setAttribute('aria-current', 'true') |
| 167 | else t.removeAttribute('aria-current') |
| 168 | return t |
| 169 | })) |
| 170 | } |
| 171 | |
| 172 | function bringIntoView () { |
| 173 | const t = grid.querySelector('[aria-current]') |
| 174 | if (t) grid.scrollTop = Math.max(0, t.offsetTop - (grid.clientHeight - t.offsetHeight) / 2) |
| 175 | else grid.scrollTop = 0 |
| 176 | } |
| 177 | |
| 178 | function isOpen () { return !box.hidden } |
| 179 | |
| 180 | function open () { |
| 181 | input.value = '' |
| 182 | box.hidden = false |
| 183 | pos.setAttribute('aria-expanded', 'true') |
| 184 | draw() |
| 185 | bringIntoView() |
| 186 | input.focus({ preventScroll: true }) |
| 187 | } |
| 188 | |
| 189 | function close (refocus = true) { |
| 190 | if (!isOpen()) return |
| 191 | box.hidden = true |
| 192 | pos.setAttribute('aria-expanded', 'false') |
| 193 | if (refocus) pos.focus({ preventScroll: true }) |
| 194 | } |
| 195 | |
| 196 | function choose (s) { |
| 197 | close() |
| 198 | go(s) |
| 199 | } |
| 200 | |
| 201 | // The position is a link that acts as a button: Space opens it too, |
| 202 | // and is not the window's Next. |
| 203 | pos.addEventListener('keydown', e => { |
| 204 | if (e.key !== ' ' || e.metaKey || e.ctrlKey || e.altKey) return |
| 205 | e.preventDefault() |
| 206 | e.stopPropagation() |
| 207 | if (!e.repeat) toggle() |
| 208 | }) |
| 209 | function toggle () { |
| 210 | if (isOpen()) close() |
| 211 | else open() |
| 212 | } |
| 213 | pos.addEventListener('click', e => { |
| 214 | e.preventDefault() |
| 215 | toggle() |
| 216 | }) |
| 217 | input.addEventListener('input', () => { draw(); grid.scrollTop = 0 }) |
| 218 | |
| 219 | // Nothing typed here moves the talk, except what a clicker sends. |
| 220 | box.addEventListener('keydown', e => { |
| 221 | if (e.key === 'PageDown' || e.key === 'PageUp') return |
| 222 | e.stopPropagation() |
| 223 | if (e.key === 'Escape') { |
| 224 | e.preventDefault() |
| 225 | close() |
| 226 | } else if (e.key === 'Enter' && e.target === input) { |
| 227 | e.preventDefault() |
| 228 | if (shown.length) choose(shown[0]) |
| 229 | } else if (e.key.startsWith('Arrow') && (e.target === input ? e.key === 'ArrowDown' : grid.contains(e.target))) { |
| 230 | arrow(e) |
| 231 | } |
| 232 | }) |
| 233 | |
| 234 | // Arrow keys: down from the field into the grid, then about it. |
| 235 | function arrow (e) { |
| 236 | const list = [...grid.children] |
| 237 | if (!list.length) return |
| 238 | e.preventDefault() |
| 239 | let to |
| 240 | if (e.target === input) to = 0 |
| 241 | else { |
| 242 | const i = list.indexOf(e.target) |
| 243 | const cols = list.filter(t => t.offsetTop === list[0].offsetTop).length || 1 |
| 244 | to = gridStep(i, e.key, cols, list.length) |
| 245 | } |
| 246 | if (to < 0) input.focus() |
| 247 | else list[to].focus() |
| 248 | } |
| 249 | |
| 250 | document.addEventListener('pointerdown', e => { |
| 251 | if (isOpen() && !box.contains(e.target) && !pos.contains(e.target)) close(false) |
| 252 | }) |
| 253 | |
| 254 | return { open, close, isOpen } |
| 255 | } |
| 256 | |