oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/scene-card.mjs — a scene block's card (scenes and clips spec
2// §5, "The slide's card"): which of the deck's scenes the block shows
3// (a choice when there are several, its name when there is one), From
4// and To by state name, the scene's words as fields, a note when its
5// pictures are older than the scene with the way to an agent who can
6// redraw them, the states with no picture, and two panels, its data
7// (scene-data.mjs) and its code (scene-code.mjs). The markup and every
8// word are the page's (editor.html's scene group).
9//
10// - From and To are each one set_block, and stay in order: From past
11// To takes To with it, To before From takes From.
12// - A word is one set_scene{words} of that word, when its field is
13// left (a change), never per key.
14// - Another scene starts at its first state and walks to its last, as
15// Insert → Scene places one (insertProps, commands.mjs).
16// - The note is stillsCurrent's (scene-version.mjs): a still made from
17// another version of the scene than the one it has now. "Ask an agent
18// to redraw them" is a link to the Agents page, in a tab of its own:
19// no agent inside the editor can draw a still, and the deck's agents
20// see stale and missing stills in the deck's JSON and the stills API.
21// - Deck settings lists every scene with stills missing or out of date
22// ([slides-scene-stills], stillsList), whatever is selected.
23// - Insert → Scene ([data-needs-scene]) waits for a slide and a scene.
24//
25// mountSceneCard({root, form, getView, getSlide, getBlock, commit,
26// scene}) → {fill(), destroy()}; scene is the code panel's seams
27// ({base, save, preview}, editor-entry.js).
28import { stillsCurrent } from './scene-version.mjs'
29import { setValue } from './inspector-block.mjs'
30import { mountSceneData } from './scene-data.mjs'
31import { mountSceneCode } from './scene-code.mjs'
32
33const own = (o, k) => !!o && Object.prototype.hasOwnProperty.call(o, k)
34
35/** The deck's scenes, [{id, name, states}], in name order. */
36export function scenesByName (scenes) {
37 return Object.entries(scenes || {})
38 .filter(([, s]) => s && Array.isArray(s.states))
39 .map(([id, s]) => ({ id, name: s.name || '', states: s.states }))
40 .sort((a, b) => a.name.localeCompare(b.name) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
41}
42
43const span = s => (s.states.length ? { scene: s.id, from: s.states[0].id, to: s.states[s.states.length - 1].id } : null)
44
45/** A new scene block's props: the first scene by name, first state to last; null with none. */
46export function insertProps (scenes) {
47 for (const s of scenesByName(scenes)) {
48 const p = span(s)
49 if (p) return p
50 }
51 return null
52}
53
54const setBlock = (slide, block, props) => [{ op: 'set_block', slide: slide.id, id: block.id, props }]
55
56/** The block showing scene id instead, from its first state to its last. */
57export function pickOps (slide, block, scenes, id) {
58 if (!own(scenes, id) || (block.props && block.props.scene === id)) return []
59 const p = span({ id, ...scenes[id] })
60 return p ? setBlock(slide, block, p) : []
61}
62
63/** The block's From or To (field) set to state value, the other moved to keep them in order. */
64export function rangeOps (slide, block, scene, field, value) {
65 const ids = scene && Array.isArray(scene.states) ? scene.states.map(s => s.id) : []
66 const p = block.props || {}
67 const at = ids.indexOf(value)
68 if (at === -1 || p[field] === value) return []
69 if (field === 'from') return setBlock(slide, block, ids.indexOf(p.to) < at ? { from: value, to: value } : { from: value })
70 if (field === 'to') return setBlock(slide, block, ids.indexOf(p.from) > at ? { from: value, to: value } : { to: value })
71 return []
72}
73
74/** One word of scene id. */
75export const wordOps = (id, key, text) => [{ op: 'set_scene', id, words: { [key]: text } }]
76
77/** The card's line on the scene's pictures: stale, and the states with none (or none registered in assets) in words ('' for none). */
78export function picturesNote (scene, texts, assets) {
79 const c = stillsCurrent(scene, assets)
80 const n = c.missing.length
81 const missing = n === 0 ? '' : n === 1 ? texts.missingOne : texts.missing.replace('{n}', String(n))
82 return { stale: c.stale.length > 0, missing }
83}
84
85/** The scenes whose stills are missing or stale, in name order: [{id, name, lines}] in the card's words. */
86export function stillsList (scenes, texts, assets) {
87 const out = []
88 for (const s of scenesByName(scenes)) {
89 const note = picturesNote(scenes[s.id], texts, assets)
90 const lines = [note.missing, note.stale ? texts.stale : ''].filter(Boolean)
91 if (lines.length) out.push({ id: s.id, name: s.name, lines })
92 }
93 return out
94}
95
96/** Deck settings' list (el, [slides-scene-stills]): drawn again only when it changes. */
97function fillStillsList (el, scenes, texts, assets) {
98 if (!el) return
99 const items = stillsList(scenes, texts, assets)
100 el.hidden = items.length === 0
101 const key = JSON.stringify(items)
102 if (el.dataset.drawn === key) return
103 el.dataset.drawn = key
104 el.querySelector('[slides-scene-stills-list]').replaceChildren(...items.map(item => {
105 const li = document.createElement('li')
106 const name = document.createElement('strong')
107 name.textContent = item.name
108 li.append(name)
109 for (const line of item.lines) li.append(' ', Object.assign(document.createElement('span'), { textContent: line }))
110 return li
111 }))
112}
113
114/** Puts pairs [[value, text]] in a select, drawn again only when they change. */
115function options (select, pairs) {
116 const key = JSON.stringify(pairs)
117 if (select.dataset.drawn === key) return
118 select.dataset.drawn = key
119 select.replaceChildren(...pairs.map(([value, text]) => new Option(text, value)))
120}
121
122export function mountSceneCard ({ root, form, getView, getSlide, getBlock, commit, scene: seams = {} }) {
123 const body = form.querySelector('[slides-scene-body]')
124 if (!body) return { fill () {}, destroy () {} }
125 const q = s => body.querySelector(s)
126 const pickWrap = q('[slides-scene-pick]')
127 const pick = q('select[name="rst-block-scene"]')
128 const nameEl = q('[slides-scene-name]')
129 const from = q('select[name="rst-block-scene-from"]')
130 const to = q('select[name="rst-block-scene-to"]')
131 const words = q('[slides-scene-words]')
132 const stale = q('[slides-scene-stale]')
133 const missing = q('[slides-scene-missing]')
134 const dataEl = q('[slides-scene-data]')
135 const codeEl = q('[slides-scene-code]')
136 const panels = q('[slides-scene-panels]')
137 const texts = body.dataset
138 let labels = {}
139 try { labels = JSON.parse(texts.authors || '{}') } catch { labels = {} }
140 const data = mountSceneData({ el: dataEl, commit })
141 const code = mountSceneCode({
142 el: codeEl,
143 base: seams.base || '',
144 labels,
145 texts: { author: texts.textAuthor, refused: texts.textRefused, refusedBig: texts.textRefusedBig, failed: texts.textFailed, loadFailed: texts.textLoadFailed },
146 commit,
147 save: seams.save || (() => Promise.reject(new Error('no store'))),
148 preview: seams.preview || (() => false)
149 })
150 let current = { slide: null, block: null, id: '', scene: null }
151 const abort = new AbortController()
152 const on = (node, type, fn) => node.addEventListener(type, fn, { signal: abort.signal })
153
154 function fillWords (sc) {
155 const keys = sc ? Object.keys(sc.words || {}).sort() : []
156 words.hidden = keys.length === 0
157 const drawn = [...words.querySelectorAll('input[data-word]')].map(i => i.dataset.word)
158 if (drawn.join('\n') !== keys.join('\n')) {
159 words.replaceChildren(words.querySelector('legend'), ...keys.map((k, i) => {
160 const field = document.createElement('div')
161 field.setAttribute('rst-field', '')
162 const label = document.createElement('label')
163 label.setAttribute('rst-field-label', '')
164 label.htmlFor = `slides-scene-word-${i}`
165 label.textContent = k
166 const input = document.createElement('input')
167 input.setAttribute('rst-input', '')
168 input.type = 'text'
169 input.id = label.htmlFor
170 input.dataset.word = k
171 field.append(label, input)
172 return field
173 }))
174 }
175 for (const input of words.querySelectorAll('input[data-word]')) setValue(input, sc.words[input.dataset.word])
176 }
177
178 const noteTexts = { missing: texts.textMissing, missingOne: texts.textMissingOne, stale: texts.textStale }
179 function fill () {
180 const v = getView() || {}
181 const scenes = v.scenes || {}
182 const assets = v.assets || {}
183 fillStillsList(root.querySelector('[slides-scene-stills]'), scenes, noteTexts, assets)
184 const hasScenes = !!insertProps(scenes)
185 for (const el of root.querySelectorAll('[data-needs-scene]')) el.disabled = !(getSlide() && hasScenes)
186 const slide = getSlide()
187 const block = getBlock()
188 if (!slide || !block || block.type !== 'scene') return
189 const p = block.props || {}
190 const id = typeof p.scene === 'string' && own(scenes, p.scene) ? p.scene : ''
191 const sc = id ? scenes[id] : null
192 current = { slide, block, id, scene: sc }
193 const list = scenesByName(scenes)
194 options(pick, list.map(s => [s.id, s.name]))
195 pickWrap.hidden = list.length < 2 && !!sc
196 setValue(pick, id)
197 nameEl.hidden = !pickWrap.hidden
198 nameEl.textContent = sc ? sc.name : ''
199 const states = sc ? sc.states.map(s => [s.id, s.name || s.id]) : []
200 options(from, states)
201 options(to, states)
202 setValue(from, p.from)
203 setValue(to, p.to)
204 fillWords(sc)
205 const note = sc ? picturesNote(sc, noteTexts, assets) : { stale: false, missing: '' }
206 stale.hidden = !note.stale
207 missing.hidden = !note.missing
208 missing.textContent = note.missing
209 const panel = (panels.querySelector('input:checked') || {}).value || 'data'
210 dataEl.hidden = panel !== 'data'
211 codeEl.hidden = panel !== 'code'
212 data.fill(id, sc)
213 code.fill(id, sc, panel === 'code')
214 }
215
216 const send = ops => { if (ops.length) commit(ops) }
217 on(body, 'change', e => {
218 const el = e.target
219 const { slide, block, id, scene } = current
220 if (!slide || !block) return
221 if (el === pick) send(pickOps(slide, block, getView().scenes, el.value))
222 else if (el === from || el === to) send(rangeOps(slide, block, scene, el === from ? 'from' : 'to', el.value))
223 else if (el.dataset.word && id) send(wordOps(id, el.dataset.word, el.value))
224 else if (el.name === 'slides-scene-panel') fill()
225 })
226 return { fill, destroy () { data.destroy(); code.destroy(); abort.abort() } }
227}
228