// fixtures/scene-card.mjs — a scene block's card (scenes and clips spec // §5, "The slide's card"): which of the deck's scenes the block shows // (a choice when there are several, its name when there is one), From // and To by state name, the scene's words as fields, a note when its // pictures are older than the scene with the way to an agent who can // redraw them, the states with no picture, and two panels, its data // (scene-data.mjs) and its code (scene-code.mjs). The markup and every // word are the page's (editor.html's scene group). // // - From and To are each one set_block, and stay in order: From past // To takes To with it, To before From takes From. // - A word is one set_scene{words} of that word, when its field is // left (a change), never per key. // - Another scene starts at its first state and walks to its last, as // Insert → Scene places one (insertProps, commands.mjs). // - The note is stillsCurrent's (scene-version.mjs): a still made from // another version of the scene than the one it has now. "Ask an agent // to redraw them" is a link to the Agents page, in a tab of its own: // no agent inside the editor can draw a still, and the deck's agents // see stale and missing stills in the deck's JSON and the stills API. // - Deck settings lists every scene with stills missing or out of date // ([slides-scene-stills], stillsList), whatever is selected. // - Insert → Scene ([data-needs-scene]) waits for a slide and a scene. // // mountSceneCard({root, form, getView, getSlide, getBlock, commit, // scene}) → {fill(), destroy()}; scene is the code panel's seams // ({base, save, preview}, editor-entry.js). import { stillsCurrent } from './scene-version.mjs' import { setValue } from './inspector-block.mjs' import { mountSceneData } from './scene-data.mjs' import { mountSceneCode } from './scene-code.mjs' const own = (o, k) => !!o && Object.prototype.hasOwnProperty.call(o, k) /** The deck's scenes, [{id, name, states}], in name order. */ export function scenesByName (scenes) { return Object.entries(scenes || {}) .filter(([, s]) => s && Array.isArray(s.states)) .map(([id, s]) => ({ id, name: s.name || '', states: s.states })) .sort((a, b) => a.name.localeCompare(b.name) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)) } const span = s => (s.states.length ? { scene: s.id, from: s.states[0].id, to: s.states[s.states.length - 1].id } : null) /** A new scene block's props: the first scene by name, first state to last; null with none. */ export function insertProps (scenes) { for (const s of scenesByName(scenes)) { const p = span(s) if (p) return p } return null } const setBlock = (slide, block, props) => [{ op: 'set_block', slide: slide.id, id: block.id, props }] /** The block showing scene id instead, from its first state to its last. */ export function pickOps (slide, block, scenes, id) { if (!own(scenes, id) || (block.props && block.props.scene === id)) return [] const p = span({ id, ...scenes[id] }) return p ? setBlock(slide, block, p) : [] } /** The block's From or To (field) set to state value, the other moved to keep them in order. */ export function rangeOps (slide, block, scene, field, value) { const ids = scene && Array.isArray(scene.states) ? scene.states.map(s => s.id) : [] const p = block.props || {} const at = ids.indexOf(value) if (at === -1 || p[field] === value) return [] if (field === 'from') return setBlock(slide, block, ids.indexOf(p.to) < at ? { from: value, to: value } : { from: value }) if (field === 'to') return setBlock(slide, block, ids.indexOf(p.from) > at ? { from: value, to: value } : { to: value }) return [] } /** One word of scene id. */ export const wordOps = (id, key, text) => [{ op: 'set_scene', id, words: { [key]: text } }] /** The card's line on the scene's pictures: stale, and the states with none (or none registered in assets) in words ('' for none). */ export function picturesNote (scene, texts, assets) { const c = stillsCurrent(scene, assets) const n = c.missing.length const missing = n === 0 ? '' : n === 1 ? texts.missingOne : texts.missing.replace('{n}', String(n)) return { stale: c.stale.length > 0, missing } } /** The scenes whose stills are missing or stale, in name order: [{id, name, lines}] in the card's words. */ export function stillsList (scenes, texts, assets) { const out = [] for (const s of scenesByName(scenes)) { const note = picturesNote(scenes[s.id], texts, assets) const lines = [note.missing, note.stale ? texts.stale : ''].filter(Boolean) if (lines.length) out.push({ id: s.id, name: s.name, lines }) } return out } /** Deck settings' list (el, [slides-scene-stills]): drawn again only when it changes. */ function fillStillsList (el, scenes, texts, assets) { if (!el) return const items = stillsList(scenes, texts, assets) el.hidden = items.length === 0 const key = JSON.stringify(items) if (el.dataset.drawn === key) return el.dataset.drawn = key el.querySelector('[slides-scene-stills-list]').replaceChildren(...items.map(item => { const li = document.createElement('li') const name = document.createElement('strong') name.textContent = item.name li.append(name) for (const line of item.lines) li.append(' ', Object.assign(document.createElement('span'), { textContent: line })) return li })) } /** Puts pairs [[value, text]] in a select, drawn again only when they change. */ function options (select, pairs) { const key = JSON.stringify(pairs) if (select.dataset.drawn === key) return select.dataset.drawn = key select.replaceChildren(...pairs.map(([value, text]) => new Option(text, value))) } export function mountSceneCard ({ root, form, getView, getSlide, getBlock, commit, scene: seams = {} }) { const body = form.querySelector('[slides-scene-body]') if (!body) return { fill () {}, destroy () {} } const q = s => body.querySelector(s) const pickWrap = q('[slides-scene-pick]') const pick = q('select[name="rst-block-scene"]') const nameEl = q('[slides-scene-name]') const from = q('select[name="rst-block-scene-from"]') const to = q('select[name="rst-block-scene-to"]') const words = q('[slides-scene-words]') const stale = q('[slides-scene-stale]') const missing = q('[slides-scene-missing]') const dataEl = q('[slides-scene-data]') const codeEl = q('[slides-scene-code]') const panels = q('[slides-scene-panels]') const texts = body.dataset let labels = {} try { labels = JSON.parse(texts.authors || '{}') } catch { labels = {} } const data = mountSceneData({ el: dataEl, commit }) const code = mountSceneCode({ el: codeEl, base: seams.base || '', labels, texts: { author: texts.textAuthor, refused: texts.textRefused, refusedBig: texts.textRefusedBig, failed: texts.textFailed, loadFailed: texts.textLoadFailed }, commit, save: seams.save || (() => Promise.reject(new Error('no store'))), preview: seams.preview || (() => false) }) let current = { slide: null, block: null, id: '', scene: null } const abort = new AbortController() const on = (node, type, fn) => node.addEventListener(type, fn, { signal: abort.signal }) function fillWords (sc) { const keys = sc ? Object.keys(sc.words || {}).sort() : [] words.hidden = keys.length === 0 const drawn = [...words.querySelectorAll('input[data-word]')].map(i => i.dataset.word) if (drawn.join('\n') !== keys.join('\n')) { words.replaceChildren(words.querySelector('legend'), ...keys.map((k, i) => { const field = document.createElement('div') field.setAttribute('rst-field', '') const label = document.createElement('label') label.setAttribute('rst-field-label', '') label.htmlFor = `slides-scene-word-${i}` label.textContent = k const input = document.createElement('input') input.setAttribute('rst-input', '') input.type = 'text' input.id = label.htmlFor input.dataset.word = k field.append(label, input) return field })) } for (const input of words.querySelectorAll('input[data-word]')) setValue(input, sc.words[input.dataset.word]) } const noteTexts = { missing: texts.textMissing, missingOne: texts.textMissingOne, stale: texts.textStale } function fill () { const v = getView() || {} const scenes = v.scenes || {} const assets = v.assets || {} fillStillsList(root.querySelector('[slides-scene-stills]'), scenes, noteTexts, assets) const hasScenes = !!insertProps(scenes) for (const el of root.querySelectorAll('[data-needs-scene]')) el.disabled = !(getSlide() && hasScenes) const slide = getSlide() const block = getBlock() if (!slide || !block || block.type !== 'scene') return const p = block.props || {} const id = typeof p.scene === 'string' && own(scenes, p.scene) ? p.scene : '' const sc = id ? scenes[id] : null current = { slide, block, id, scene: sc } const list = scenesByName(scenes) options(pick, list.map(s => [s.id, s.name])) pickWrap.hidden = list.length < 2 && !!sc setValue(pick, id) nameEl.hidden = !pickWrap.hidden nameEl.textContent = sc ? sc.name : '' const states = sc ? sc.states.map(s => [s.id, s.name || s.id]) : [] options(from, states) options(to, states) setValue(from, p.from) setValue(to, p.to) fillWords(sc) const note = sc ? picturesNote(sc, noteTexts, assets) : { stale: false, missing: '' } stale.hidden = !note.stale missing.hidden = !note.missing missing.textContent = note.missing const panel = (panels.querySelector('input:checked') || {}).value || 'data' dataEl.hidden = panel !== 'data' codeEl.hidden = panel !== 'code' data.fill(id, sc) code.fill(id, sc, panel === 'code') } const send = ops => { if (ops.length) commit(ops) } on(body, 'change', e => { const el = e.target const { slide, block, id, scene } = current if (!slide || !block) return if (el === pick) send(pickOps(slide, block, getView().scenes, el.value)) else if (el === from || el === to) send(rangeOps(slide, block, scene, el === from ? 'from' : 'to', el.value)) else if (el.dataset.word && id) send(wordOps(id, el.dataset.word, el.value)) else if (el.name === 'slides-scene-panel') fill() }) return { fill, destroy () { data.destroy(); code.destroy(); abort.abort() } } }