| 1 | // scene-places.mjs — where a slide's scene blocks are, as the frame |
| 2 | // layer needs them (scene-layer.mjs): which state each is at, what |
| 3 | // counts as the same place on the next slide, whether anything is drawn |
| 4 | // over it, and whether it can run at all. |
| 5 | // |
| 6 | // A block runs only once it and every box holding it have arrived (no |
| 7 | // data-waiting from it up), and only outside a phone view. A block that |
| 8 | // something above it on the slide covers (a canvas box with a higher z, |
| 9 | // or the same z later in the slide, over its box; image-full's heading |
| 10 | // over its media) runs inside its own block, where the slide's stacking |
| 11 | // holds, and so never carries on to another slide. |
| 12 | |
| 13 | /** |
| 14 | * The scene blocks under container that can run: [{el, block, scene, |
| 15 | * state, key, inside, slot}]. slot is the block's scene and the number |
| 16 | * of that scene's blocks before it on the slide, "<scene> <n>", counted |
| 17 | * over every one of them, arrived or not: two blocks of one scene are |
| 18 | * two placements, each with a frame of its own, the next slide's are |
| 19 | * paired with these by slot (scene-layer.mjs), and a block arriving |
| 20 | * never renumbers one already running. |
| 21 | */ |
| 22 | export function scenePositions (container) { |
| 23 | const out = [] |
| 24 | if (!container) return out |
| 25 | const seen = new Map() |
| 26 | for (const el of container.querySelectorAll('[slides-block][data-block="scene"][data-scene]')) { |
| 27 | const scene = el.getAttribute('data-scene') |
| 28 | const n = seen.get(scene) || 0 |
| 29 | seen.set(scene, n + 1) |
| 30 | if (waiting(el)) continue |
| 31 | const states = el.children ? [...el.children].filter(c => c.hasAttribute('data-scene-state')) : [] |
| 32 | const shown = states.filter(c => !c.hasAttribute('data-waiting')) |
| 33 | const at = shown[shown.length - 1] |
| 34 | const state = at ? at.getAttribute('data-scene-state') : '' |
| 35 | if (!state) continue |
| 36 | const block = el.getAttribute('data-id') |
| 37 | const inside = covered(el) |
| 38 | out.push({ el, block, scene, state, key: inside ? 'inside:' + block : placeKey(el), inside, slot: `${scene} ${n}` }) |
| 39 | } |
| 40 | return out |
| 41 | } |
| 42 | |
| 43 | /** Whether a block, or a box holding it, is still to arrive. */ |
| 44 | function waiting (el) { |
| 45 | for (let n = el; n && n.getAttribute; n = n.parentNode) { |
| 46 | if (n.hasAttribute('data-waiting')) return true |
| 47 | if (n.hasAttribute('slides-slide')) return false |
| 48 | } |
| 49 | return false |
| 50 | } |
| 51 | |
| 52 | /** What "the same box" means: the box on a canvas, the region in a flow. */ |
| 53 | export function placeKey (el) { |
| 54 | const section = el.closest && el.closest('[slides-slide]') |
| 55 | if (section && section.getAttribute('data-layout') === 'canvas') { |
| 56 | const s = el.style || {} |
| 57 | return 'box:' + [s.left, s.top, s.width, s.height].join(',') |
| 58 | } |
| 59 | const region = el.closest && el.closest('[slides-region]') |
| 60 | return 'region:' + ((region && region.getAttribute('data-region')) || 'body') |
| 61 | } |
| 62 | |
| 63 | /** Whether a block shows its stills only: a phone view, rendered for one or laid out as one. */ |
| 64 | export function stillsOnly (el) { |
| 65 | const section = el.closest && el.closest('[slides-slide]') |
| 66 | if (!section) return true |
| 67 | if (section.getAttribute('data-vp') === 'phone') return true |
| 68 | const w = section.offsetWidth || 0 |
| 69 | return w > 0 && w <= 600 |
| 70 | } |
| 71 | |
| 72 | const pct = v => parseFloat(v) || 0 |
| 73 | function boxOf (el) { |
| 74 | const s = el.style || {} |
| 75 | return { x: pct(s.left), y: pct(s.top), w: pct(s.width), h: pct(s.height) } |
| 76 | } |
| 77 | |
| 78 | /** Whether anything on the slide is drawn over the block. */ |
| 79 | export function covered (el) { |
| 80 | const section = el.closest && el.closest('[slides-slide]') |
| 81 | if (!section) return false |
| 82 | if (section.getAttribute('data-layout') === 'canvas') { |
| 83 | const z = Number(el.getAttribute('data-z')) || 0 |
| 84 | const a = boxOf(el) |
| 85 | const kids = [...section.children] |
| 86 | const at = kids.indexOf(el) |
| 87 | return kids.some((o, i) => { |
| 88 | if (o === el || !o.hasAttribute('slides-block')) return false |
| 89 | const oz = Number(o.getAttribute('data-z')) || 0 |
| 90 | if (oz < z || (oz === z && i < at)) return false |
| 91 | const b = boxOf(o) |
| 92 | return a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h |
| 93 | }) |
| 94 | } |
| 95 | if (section.getAttribute('data-template') === 'image-full') { |
| 96 | const region = el.closest('[slides-region]') |
| 97 | return !!region && region.getAttribute('data-region') === 'media' && !!section.querySelector('[slides-region][data-region="heading"]') |
| 98 | } |
| 99 | return false |
| 100 | } |
| 101 | |