// presses.mjs — a slide's presses, worked out from its boxes and never // stored: internal/slides/deck/presses.go's Presses, for the browser. // vectors/presses.json holds the two to one answer, so a rule changes on // both sides or on neither. // // presses(slide, scenes) is [[{block, item}]]: element 0 is press 1, and // press 0 is the slide as it arrives. item -1 is the whole box; otherwise // it is the index of a list paragraph, blank paragraphs counted, or of a // scene's state in its states. scenes is the deck's (the view's scenes // map), needed only for scene blocks. // pressValues(slide, scenes) is the value each of those presses was made // from, for the editor's After… (inspector-steps.mjs). // // - A box's value is its own step, or its columns container's when that // is later (and so on up). A box with no step of its own arrives with // whatever holds it, the slide or its container. // - The distinct values, ascending, are the presses, compared exactly as // stored. Boxes sharing a value arrive together. // - An `each` list at value v takes one press per non-blank item from v // on; everything else at v comes with the first of them, lists tied at // v pair up by index, and the next value starts after the longest. // - A box that shows nothing is in no press, so a value with only such // boxes makes none. A columns box shows nothing until one of its // children does, so it arrives with the earliest of them. // - A scene block is an `each` list whose items are its scene's states // strictly after `from`, up to and including `to` (plan call R2). It is // there when the slide arrives, so with no step of its own (or a // container's) its value is 0: its presses come first. With no such // states — from equal to to, a scene or state the deck no longer has — // it makes none. // - A clip that plays on a press (play: press) is a box arriving at its // value, 0 with no step (plan call R3). Any other clip is a box like // any other. // Go's unicode.IsSpace, spelled out: JS's \s differs at U+0085 and U+FEFF. const BLANK = /^[\t\n\v\f\r \u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]*$/ const plain = para => (Array.isArray(para) ? para : []).map(run => (run && typeof run.t === 'string') ? run.t : '').join('') /** The indices of a text's non-blank paragraphs. */ function filledItems (text) { const out = [] ;(Array.isArray(text) ? text : []).forEach((para, i) => { if (!BLANK.test(plain(para))) out.push(i) }) return out } const ownStep = b => (typeof b.step === 'number' && b.step > 0) ? b.step : 0 /** A scene block's items: the indices of its scene's states after from, up to and including to. */ function sceneItems (b, scenes) { const p = b.props || {} const scene = scenes && typeof p.scene === 'string' && Object.hasOwn(scenes, p.scene) ? scenes[p.scene] : null const ids = scene && Array.isArray(scene.states) ? scene.states.map(s => s && s.id) : [] const from = typeof p.from === 'string' ? ids.indexOf(p.from) : -1 const to = typeof p.to === 'string' ? ids.indexOf(p.to) : -1 const out = [] if (from === -1 || to === -1) return out for (let i = from + 1; i <= to; i++) out.push(i) return out } /** A columns block's props.children, the strings among them. */ function children (b) { const v = b.props && b.props.children return Array.isArray(v) ? v.filter(id => typeof id === 'string') : [] } /** The blocks by id, and the columns box holding each: the first in block order naming it. */ function slideBoxes (blocks) { const byID = new Map() for (const b of blocks) byID.set(b.id, b) const parent = new Map() for (const b of blocks) { if (b.type !== 'columns') continue for (const id of children(b)) { if (byID.has(id) && id !== b.id && !parent.has(id)) parent.set(id, b.id) } } return { byID, parent } } /** The latest of a box's own step and its containers', until a container repeats. */ function value (sl, b) { let v = ownStep(b) const seen = new Set([b.id]) for (let id = sl.parent.get(b.id); id !== undefined && !seen.has(id); id = sl.parent.get(id)) { seen.add(id) v = Math.max(v, ownStep(sl.byID.get(id))) } return v } /** Whether a box puts anything on the slide; on is the columns boxes being looked into. */ function shows (sl, b, on) { switch (b.type) { case 'list': return filledItems(b.text).length > 0 case 'heading': case 'text': case 'quote': return !BLANK.test((Array.isArray(b.text) ? b.text : []).map(plain).join('\n')) case 'columns': on.add(b.id) for (const id of children(b)) { const c = sl.byID.get(id) if (c && !on.has(id) && shows(sl, c, on)) return true } return false } return true } /** The value a box first shows at: a columns box waits for its earliest child that shows. */ function arrival (sl, b, on) { const v = value(sl, b) if (b.type !== 'columns') return v on.add(b.id) let first = Infinity for (const id of children(b)) { const c = sl.byID.get(id) if (!c || on.has(id) || !shows(sl, c, new Set())) continue first = Math.min(first, arrival(sl, c, on)) } return first > v && first !== Infinity ? first : v } export function presses (slide, scenes) { return build(slide, scenes).out } /** The value each press was made from, in press order: an `each` list's items all share theirs. */ export function pressValues (slide, scenes) { return build(slide, scenes).at } function build (slide, scenes) { const blocks = Array.isArray(slide && slide.blocks) ? slide.blocks : [] const sl = slideBoxes(blocks) const all = [] const values = [] for (const b of blocks) { let items = null if (b.type === 'scene') { items = sceneItems(b, scenes) if (!items.length) continue } else if (!(b.type === 'clip' && b.props && b.props.play === 'press')) { if (ownStep(b) === 0 || !shows(sl, b, new Set())) continue if (b.type === 'list' && b.items === 'each') items = filledItems(b.text) } const a = { id: b.id, value: arrival(sl, b, new Set()), items } all.push(a) if (!values.includes(a.value)) values.push(a.value) } values.sort((x, y) => x - y) const out = [] const at = [] for (const v of values) { let n = 1 for (const a of all) if (a.value === v && a.items && a.items.length > n) n = a.items.length for (let j = 0; j < n; j++) { const press = [] for (const a of all) { if (a.value !== v) continue if (a.items) { if (j < a.items.length) press.push({ block: a.id, item: a.items[j] }) } else if (j === 0) { press.push({ block: a.id, item: -1 }) } } out.push(press) at.push(v) } } return { out, at } }