| 1 | // fixtures/step-marks.mjs — the order of a slide's presses, drawn |
| 2 | // (steps and presenter spec §4): a small muted number in each box's |
| 3 | // margin, the press it arrives on, counted 1, 2, 3 in press order |
| 4 | // whatever its step value; an item-by-item list has one beside each |
| 5 | // item. Drawn only while a box on a slide with presses is selected, so a |
| 6 | // slide without steps, or a slide nobody is working on, shows nothing. |
| 7 | // |
| 8 | // The numbers are an overlay laid over the stage, outside the slide's |
| 9 | // own DOM (ProseMirror owns the boxes' insides) and hidden from readers |
| 10 | // (aria-hidden): the card's Appears row says the same in words. Placed |
| 11 | // from the boxes' rectangles as presence-view.mjs places its marks, and |
| 12 | // empty (so not drawn: editor.css) when there is nothing to show. |
| 13 | // |
| 14 | // A scene block's states are presses too (scenes the deck's, presses.mjs): |
| 15 | // it has one mark, its first press to its last ("1–2"). |
| 16 | // |
| 17 | // mountStepMarks({stageEl, getSlide, getSelected, getScenes}) → |
| 18 | // {place(), destroy()}: place() after anything that moves a box or |
| 19 | // changes the document. |
| 20 | import { presses } from './presses.mjs' |
| 21 | |
| 22 | /** The numbers a slide shows: each box (item -1), or list item, with its press, 1-based; a scene's last press too. */ |
| 23 | export function marksFor (slide, scenes) { |
| 24 | const out = [] |
| 25 | const seen = new Map() |
| 26 | const blocks = Array.isArray(slide && slide.blocks) ? slide.blocks : [] |
| 27 | const scene = new Set(blocks.filter(b => b.type === 'scene').map(b => b.id)) |
| 28 | presses(slide, scenes).forEach((press, i) => { |
| 29 | for (const m of press) { |
| 30 | const item = scene.has(m.block) ? -1 : m.item |
| 31 | const key = `${m.block}:${item}` |
| 32 | if (seen.has(key)) { |
| 33 | if (item === -1 && scene.has(m.block)) seen.get(key).last = i + 1 |
| 34 | continue |
| 35 | } |
| 36 | const mark = { block: m.block, item, n: i + 1 } |
| 37 | seen.set(key, mark) |
| 38 | out.push(mark) |
| 39 | } |
| 40 | }) |
| 41 | return out |
| 42 | } |
| 43 | |
| 44 | export function mountStepMarks ({ stageEl, getSlide, getSelected, getScenes = () => null }) { |
| 45 | const marks = document.createElement('div') |
| 46 | marks.setAttribute('slides-step-marks', '') |
| 47 | marks.setAttribute('aria-hidden', 'true') |
| 48 | stageEl.append(marks) |
| 49 | |
| 50 | const boxEl = id => stageEl.querySelector(`[slides-slide] [slides-block][data-id="${CSS.escape(id)}"]`) |
| 51 | |
| 52 | function place () { |
| 53 | if (!marks.isConnected) stageEl.append(marks) |
| 54 | marks.replaceChildren() |
| 55 | const id = getSelected() |
| 56 | const slide = id ? getSlide() : null |
| 57 | if (!slide || !Array.isArray(slide.blocks) || !slide.blocks.some(b => b.id === id) || !boxEl(id)) return |
| 58 | const frame = stageEl.getBoundingClientRect() |
| 59 | for (const m of marksFor(slide, getScenes())) { |
| 60 | const el = boxEl(m.block) |
| 61 | if (!el) continue |
| 62 | const items = m.item >= 0 ? el.querySelectorAll('li') : null |
| 63 | const at = items && items[m.item] ? items[m.item] : el |
| 64 | const box = el.getBoundingClientRect() |
| 65 | const r = at.getBoundingClientRect() |
| 66 | if (!box.width && !box.height) continue |
| 67 | const mark = document.createElement('span') |
| 68 | mark.setAttribute('slides-step-mark', '') |
| 69 | mark.textContent = m.last ? `${m.n}–${m.last}` : String(m.n) |
| 70 | // Its end edge against the box's start edge, level with the |
| 71 | // box's top, or the item's. |
| 72 | const rtl = getComputedStyle(stageEl).direction === 'rtl' |
| 73 | const x = (rtl ? box.right : box.left) - frame.left + stageEl.scrollLeft |
| 74 | mark.style.left = x + 'px' |
| 75 | if (rtl) mark.setAttribute('data-rtl', '') |
| 76 | mark.style.top = (r.top - frame.top + stageEl.scrollTop) + 'px' |
| 77 | marks.append(mark) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | const ro = typeof ResizeObserver === 'function' ? new ResizeObserver(() => place()) : null |
| 82 | if (ro) ro.observe(stageEl) |
| 83 | |
| 84 | return { |
| 85 | place, |
| 86 | destroy () { |
| 87 | if (ro) ro.disconnect() |
| 88 | marks.remove() |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |