oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/inspector-steps.mjs — how a box appears (steps and presenter
2// spec §4, In the editor): the Appears row at the head of the box's
3// card, With the slide, On a press and, for a list, Item by item; and
4// After…, a small popover listing the slide's presses, which places the
5// box after one of them.
6//
7// A box's step is a number compared with its neighbours' and nothing
8// else (presses.mjs), so nothing is ever renumbered:
9//
10// - On a press, on a box with no step, takes one more than the largest
11// step on the slide: the box arrives last. It clears items, so a list
12// arrives whole even if it was once item by item.
13// - Item by item is a list's: items "each", and a step as On a press
14// when the list has none.
15// - With the slide clears the step and the items.
16// - After press n writes the midpoint of the value press n was made
17// from and the next value on the slide, or one more after the last.
18// The box's own value is not counted: it is the one moving. Value 0 is
19// never offered: it is a scene block's with no step (its states are
20// the slide's first presses), and nothing is placed after the slide's
21// arrival but by On a press.
22// - scenes is the deck's (the view's scenes map), so a scene block's
23// states count as presses (presses.mjs).
24//
25// Each choice is one set_block, so one commit and one undo step.
26// fillAppears puts the box's choice in the row and the slide's presses
27// in After…; mountAppears hears After…'s rows. The press numbers on the
28// stage are step-marks.mjs's; the box menu's rows box-menu-appears.mjs's.
29import { presses, pressValues } from './presses.mjs'
30
31const stepOf = b => (b && typeof b.step === 'number' && b.step > 0 ? b.step : 0)
32
33/** 'slide', 'press' or 'each': how the box arrives. */
34export function appearsOf (block) {
35 if (!stepOf(block)) return 'slide'
36 return block.type === 'list' && block.items === 'each' ? 'each' : 'press'
37}
38
39/** One more than the largest step on the slide (1 on a slide without steps). */
40function nextStep (slide) {
41 const blocks = Array.isArray(slide && slide.blocks) ? slide.blocks : []
42 return blocks.reduce((m, b) => Math.max(m, stepOf(b)), 0) + 1
43}
44
45const setBlock = (slide, block, fields) => [{ op: 'set_block', slide: slide.id, id: block.id, ...fields }]
46
47/** The ops for choosing how block arrives: [] when it already does, or the choice is not the box's. */
48export function appearsOps (slide, block, choice) {
49 const now = appearsOf(block)
50 if (choice === now) return []
51 if (choice === 'slide') return setBlock(slide, block, { step: null, items: null })
52 if (choice === 'press') {
53 if (stepOf(block)) return setBlock(slide, block, { items: null })
54 // Items "each" left on a list with no step would make it item by
55 // item the moment it has one: they go with the same op.
56 return setBlock(slide, block, block.items ? { step: nextStep(slide), items: null } : { step: nextStep(slide) })
57 }
58 if (choice === 'each' && block.type === 'list') {
59 return setBlock(slide, block, stepOf(block) ? { items: 'each' } : { step: nextStep(slide), items: 'each' })
60 }
61 return []
62}
63
64/** The presses After… lists for block: every value another box makes, numbered by its last press; never value 0. */
65export function afterChoices (slide, block, scenes) {
66 const ps = presses(slide, scenes)
67 const at = pressValues(slide, scenes)
68 const out = []
69 ps.forEach((press, i) => {
70 if (!(at[i] > 0) || !press.some(m => m.block !== block.id)) return
71 const last = out.length ? out[out.length - 1] : null
72 if (last && last.value === at[i]) last.n = i + 1
73 else out.push({ n: i + 1, value: at[i] })
74 })
75 return out
76}
77
78/** The op placing block after the press made from value: the midpoint to the next, or value + 1. */
79export function afterOps (slide, block, value, scenes) {
80 const later = afterChoices(slide, block, scenes).map(c => c.value).filter(v => v > value)
81 const step = later.length ? (value + Math.min(...later)) / 2 : value + 1
82 if (step === stepOf(block)) return []
83 return setBlock(slide, block, { step })
84}
85
86/** Puts block's choice in the card's Appears row, and the slide's presses in After…. */
87export function fillAppears (form, slide, block, scenes) {
88 const now = appearsOf(block)
89 for (const r of form.querySelectorAll('input[name="rst-block-appears"]')) r.checked = r.value === now
90 const button = form.querySelector('[slides-appears-after]')
91 const pop = form.querySelector('#slides-pop-after')
92 if (!button || !pop) return
93 const choices = afterChoices(slide, block, scenes)
94 button.hidden = choices.length === 0
95 if (!choices.length && typeof pop.hidePopover === 'function' && pop.matches(':popover-open')) pop.hidePopover()
96 // The rows rebuilt only when they change, so a row with focus keeps it.
97 const key = choices.map(c => `${c.n}:${c.value}`).join(' ')
98 if (pop.dataset.drawn === key) return
99 pop.dataset.drawn = key
100 const label = pop.dataset.label || '{n}'
101 pop.replaceChildren(...choices.map(c => {
102 const row = document.createElement('button')
103 row.type = 'button'
104 row.setAttribute('slides-card-row', '')
105 row.dataset.after = String(c.value)
106 row.textContent = label.replace('{n}', String(c.n))
107 return row
108 }))
109}
110
111/**
112 * Hears After…'s rows in form, the card's block form: a row commits
113 * afterOps for the selected box, closes the popover and puts focus back
114 * on After…; Escape while it is open does the last two. getSlide() and
115 * getBlock() are the card's current slide and box, getScenes() the
116 * deck's scenes. Returns {destroy}.
117 */
118export function mountAppears ({ form, getSlide, getBlock, getScenes = () => null, commit }) {
119 const button = form.querySelector('[slides-appears-after]')
120 const pop = form.querySelector('#slides-pop-after')
121 const abort = new AbortController()
122 if (!button || !pop) return { destroy () { abort.abort() } }
123 const back = () => {
124 if (pop.matches(':popover-open')) pop.hidePopover()
125 button.focus()
126 }
127 pop.addEventListener('click', e => {
128 const row = e.target && e.target.closest ? e.target.closest('[data-after]') : null
129 if (!row) return
130 const slide = getSlide()
131 const block = getBlock()
132 back()
133 if (!slide || !block) return
134 const ops = afterOps(slide, block, Number(row.dataset.after), getScenes())
135 if (ops.length) commit(ops)
136 }, { signal: abort.signal })
137 // Escape while it is open, from its rows or from After… itself, closes
138 // it and goes no further: the card's own Escape (inspector.mjs) would
139 // send focus to the box and, by taking the key, leave the popover open.
140 form.addEventListener('keydown', e => {
141 if (e.key !== 'Escape' || e.isComposing || !pop.matches(':popover-open')) return
142 e.preventDefault()
143 e.stopPropagation()
144 back()
145 }, { signal: abort.signal })
146 return { destroy () { abort.abort() } }
147}
148