| 1 | // presses.mjs — a slide's presses, worked out from its boxes and never |
| 2 | // stored: internal/slides/deck/presses.go's Presses, for the browser. |
| 3 | // vectors/presses.json holds the two to one answer, so a rule changes on |
| 4 | // both sides or on neither. |
| 5 | // |
| 6 | // presses(slide, scenes) is [[{block, item}]]: element 0 is press 1, and |
| 7 | // press 0 is the slide as it arrives. item -1 is the whole box; otherwise |
| 8 | // it is the index of a list paragraph, blank paragraphs counted, or of a |
| 9 | // scene's state in its states. scenes is the deck's (the view's scenes |
| 10 | // map), needed only for scene blocks. |
| 11 | // pressValues(slide, scenes) is the value each of those presses was made |
| 12 | // from, for the editor's After… (inspector-steps.mjs). |
| 13 | // |
| 14 | // - A box's value is its own step, or its columns container's when that |
| 15 | // is later (and so on up). A box with no step of its own arrives with |
| 16 | // whatever holds it, the slide or its container. |
| 17 | // - The distinct values, ascending, are the presses, compared exactly as |
| 18 | // stored. Boxes sharing a value arrive together. |
| 19 | // - An `each` list at value v takes one press per non-blank item from v |
| 20 | // on; everything else at v comes with the first of them, lists tied at |
| 21 | // v pair up by index, and the next value starts after the longest. |
| 22 | // - A box that shows nothing is in no press, so a value with only such |
| 23 | // boxes makes none. A columns box shows nothing until one of its |
| 24 | // children does, so it arrives with the earliest of them. |
| 25 | // - A scene block is an `each` list whose items are its scene's states |
| 26 | // strictly after `from`, up to and including `to` (plan call R2). It is |
| 27 | // there when the slide arrives, so with no step of its own (or a |
| 28 | // container's) its value is 0: its presses come first. With no such |
| 29 | // states — from equal to to, a scene or state the deck no longer has — |
| 30 | // it makes none. |
| 31 | // - A clip that plays on a press (play: press) is a box arriving at its |
| 32 | // value, 0 with no step (plan call R3). Any other clip is a box like |
| 33 | // any other. |
| 34 | |
| 35 | // Go's unicode.IsSpace, spelled out: JS's \s differs at U+0085 and U+FEFF. |
| 36 | const BLANK = /^[\t\n\v\f\r \u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]*$/ |
| 37 | |
| 38 | const plain = para => (Array.isArray(para) ? para : []).map(run => (run && typeof run.t === 'string') ? run.t : '').join('') |
| 39 | |
| 40 | /** The indices of a text's non-blank paragraphs. */ |
| 41 | function filledItems (text) { |
| 42 | const out = [] |
| 43 | ;(Array.isArray(text) ? text : []).forEach((para, i) => { |
| 44 | if (!BLANK.test(plain(para))) out.push(i) |
| 45 | }) |
| 46 | return out |
| 47 | } |
| 48 | |
| 49 | const ownStep = b => (typeof b.step === 'number' && b.step > 0) ? b.step : 0 |
| 50 | |
| 51 | /** A scene block's items: the indices of its scene's states after from, up to and including to. */ |
| 52 | function sceneItems (b, scenes) { |
| 53 | const p = b.props || {} |
| 54 | const scene = scenes && typeof p.scene === 'string' && Object.hasOwn(scenes, p.scene) ? scenes[p.scene] : null |
| 55 | const ids = scene && Array.isArray(scene.states) ? scene.states.map(s => s && s.id) : [] |
| 56 | const from = typeof p.from === 'string' ? ids.indexOf(p.from) : -1 |
| 57 | const to = typeof p.to === 'string' ? ids.indexOf(p.to) : -1 |
| 58 | const out = [] |
| 59 | if (from === -1 || to === -1) return out |
| 60 | for (let i = from + 1; i <= to; i++) out.push(i) |
| 61 | return out |
| 62 | } |
| 63 | |
| 64 | /** A columns block's props.children, the strings among them. */ |
| 65 | function children (b) { |
| 66 | const v = b.props && b.props.children |
| 67 | return Array.isArray(v) ? v.filter(id => typeof id === 'string') : [] |
| 68 | } |
| 69 | |
| 70 | /** The blocks by id, and the columns box holding each: the first in block order naming it. */ |
| 71 | function slideBoxes (blocks) { |
| 72 | const byID = new Map() |
| 73 | for (const b of blocks) byID.set(b.id, b) |
| 74 | const parent = new Map() |
| 75 | for (const b of blocks) { |
| 76 | if (b.type !== 'columns') continue |
| 77 | for (const id of children(b)) { |
| 78 | if (byID.has(id) && id !== b.id && !parent.has(id)) parent.set(id, b.id) |
| 79 | } |
| 80 | } |
| 81 | return { byID, parent } |
| 82 | } |
| 83 | |
| 84 | /** The latest of a box's own step and its containers', until a container repeats. */ |
| 85 | function value (sl, b) { |
| 86 | let v = ownStep(b) |
| 87 | const seen = new Set([b.id]) |
| 88 | for (let id = sl.parent.get(b.id); id !== undefined && !seen.has(id); id = sl.parent.get(id)) { |
| 89 | seen.add(id) |
| 90 | v = Math.max(v, ownStep(sl.byID.get(id))) |
| 91 | } |
| 92 | return v |
| 93 | } |
| 94 | |
| 95 | /** Whether a box puts anything on the slide; on is the columns boxes being looked into. */ |
| 96 | function shows (sl, b, on) { |
| 97 | switch (b.type) { |
| 98 | case 'list': |
| 99 | return filledItems(b.text).length > 0 |
| 100 | case 'heading': case 'text': case 'quote': |
| 101 | return !BLANK.test((Array.isArray(b.text) ? b.text : []).map(plain).join('\n')) |
| 102 | case 'columns': |
| 103 | on.add(b.id) |
| 104 | for (const id of children(b)) { |
| 105 | const c = sl.byID.get(id) |
| 106 | if (c && !on.has(id) && shows(sl, c, on)) return true |
| 107 | } |
| 108 | return false |
| 109 | } |
| 110 | return true |
| 111 | } |
| 112 | |
| 113 | /** The value a box first shows at: a columns box waits for its earliest child that shows. */ |
| 114 | function arrival (sl, b, on) { |
| 115 | const v = value(sl, b) |
| 116 | if (b.type !== 'columns') return v |
| 117 | on.add(b.id) |
| 118 | let first = Infinity |
| 119 | for (const id of children(b)) { |
| 120 | const c = sl.byID.get(id) |
| 121 | if (!c || on.has(id) || !shows(sl, c, new Set())) continue |
| 122 | first = Math.min(first, arrival(sl, c, on)) |
| 123 | } |
| 124 | return first > v && first !== Infinity ? first : v |
| 125 | } |
| 126 | |
| 127 | export function presses (slide, scenes) { |
| 128 | return build(slide, scenes).out |
| 129 | } |
| 130 | |
| 131 | /** The value each press was made from, in press order: an `each` list's items all share theirs. */ |
| 132 | export function pressValues (slide, scenes) { |
| 133 | return build(slide, scenes).at |
| 134 | } |
| 135 | |
| 136 | function build (slide, scenes) { |
| 137 | const blocks = Array.isArray(slide && slide.blocks) ? slide.blocks : [] |
| 138 | const sl = slideBoxes(blocks) |
| 139 | const all = [] |
| 140 | const values = [] |
| 141 | for (const b of blocks) { |
| 142 | let items = null |
| 143 | if (b.type === 'scene') { |
| 144 | items = sceneItems(b, scenes) |
| 145 | if (!items.length) continue |
| 146 | } else if (!(b.type === 'clip' && b.props && b.props.play === 'press')) { |
| 147 | if (ownStep(b) === 0 || !shows(sl, b, new Set())) continue |
| 148 | if (b.type === 'list' && b.items === 'each') items = filledItems(b.text) |
| 149 | } |
| 150 | const a = { id: b.id, value: arrival(sl, b, new Set()), items } |
| 151 | all.push(a) |
| 152 | if (!values.includes(a.value)) values.push(a.value) |
| 153 | } |
| 154 | values.sort((x, y) => x - y) |
| 155 | |
| 156 | const out = [] |
| 157 | const at = [] |
| 158 | for (const v of values) { |
| 159 | let n = 1 |
| 160 | for (const a of all) if (a.value === v && a.items && a.items.length > n) n = a.items.length |
| 161 | for (let j = 0; j < n; j++) { |
| 162 | const press = [] |
| 163 | for (const a of all) { |
| 164 | if (a.value !== v) continue |
| 165 | if (a.items) { |
| 166 | if (j < a.items.length) press.push({ block: a.id, item: a.items[j] }) |
| 167 | } else if (j === 0) { |
| 168 | press.push({ block: a.id, item: -1 }) |
| 169 | } |
| 170 | } |
| 171 | out.push(press) |
| 172 | at.push(v) |
| 173 | } |
| 174 | } |
| 175 | return { out, at } |
| 176 | } |
| 177 | |