| 1 | // fixtures/inspector.mjs — the inspector: three forms the server |
| 2 | // rendered (templates/editor.html: Slide and Block in the card, Deck in |
| 3 | // the Deck settings dialog under File, spec §8), filled from |
| 4 | // the deck's view for the current slide and the selected block, and |
| 5 | // every change turned into the one op inspector-ops.mjs names for it. |
| 6 | // |
| 7 | // Nothing here draws a control, and nothing here says which form |
| 8 | // shows (that is data-inspect, inspect.mjs). The bundle fills values, |
| 9 | // shows the fields a block type or a slide layout has and hides the |
| 10 | // rest, and listens: a change event on a field is an op, a press on a |
| 11 | // button is a command the entry runs (commands.mjs, one place for |
| 12 | // every surface's commands). fill() runs on every |
| 13 | // document change, whoever made it, so the person sees a peer's edit |
| 14 | // in the controls as well as on the stage — except in the field they |
| 15 | // are typing in, which is never overwritten under them. |
| 16 | // |
| 17 | // The Block form is inspector-block.mjs; this file is the deck and |
| 18 | // slide forms, and the wiring. |
| 19 | // |
| 20 | // Position and size is a closed group of the Block form (<details |
| 21 | // slides-place>, the quieter editor §4.3): openPlace() opens it for the |
| 22 | // box's menu, and open or closed is remembered for this page load |
| 23 | // (mountPlace, in memory): a resync's rebuilt editor keeps it, a |
| 24 | // reload starts closed. |
| 25 | // |
| 26 | // The keyboard goes to and fro between a box and its options (spec |
| 27 | // §4.4): every box is a tab stop on the stage (stage-focus.mjs), and |
| 28 | // Escape inside a box's options puts focus back on that box, still |
| 29 | // selected. A second Escape, on the box, gives it up (selection.mjs). |
| 30 | import { view } from './ops.mjs' |
| 31 | import { deckOp, slideOp } from './inspector-ops.mjs' |
| 32 | import { fillBlock, blockChange, setValue, showWhere, forceWrites } from './inspector-block.mjs' |
| 33 | import { mountAssets } from './inspector-image.mjs' |
| 34 | import { mountLayoutPick } from './layout-pick.mjs' |
| 35 | import { mountAppears } from './inspector-steps.mjs' |
| 36 | import { mountDeckFonts, fillFontField, refreshYourFonts } from './deck-fonts.mjs' |
| 37 | import { mountSceneCard } from './scene-card.mjs' |
| 38 | |
| 39 | // Position and size open or closed, for this page load (module scope). |
| 40 | let placeOpen = false |
| 41 | |
| 42 | /** Keeps a <details slides-place> as this page last left it; returns {destroy()}. */ |
| 43 | export function mountPlace (details) { |
| 44 | const abort = new AbortController() |
| 45 | details.open = placeOpen |
| 46 | details.addEventListener('toggle', () => { placeOpen = !!details.open }, { signal: abort.signal }) |
| 47 | return { destroy () { abort.abort() } } |
| 48 | } |
| 49 | |
| 50 | /** A custom property off the stage's section — the theme's own accent or background — as a hex, or the fallback. */ |
| 51 | function sectionVar (stageEl, name, fallback) { |
| 52 | const s = stageEl && stageEl.querySelector('[slides-slide]') |
| 53 | if (!s) return fallback |
| 54 | const v = getComputedStyle(s).getPropertyValue(name).trim() |
| 55 | return /^#[0-9a-fA-F]{6}$/.test(v) ? v.toLowerCase() : fallback |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Mounts the inspector on root, the editor ([slides-editor]), which |
| 60 | * holds the card's two forms and the Deck settings dialog's. |
| 61 | * getSlideId(), getSelected(), getView() read the editor's state; |
| 62 | * commit(ops) applies a batch; onCommand(name) runs a button in the |
| 63 | * card or the deck's form; focusBox(id) puts focus on a box on the |
| 64 | * stage (stage-focus.mjs); labels.upload words the image upload; |
| 65 | * scene is a scene block's code panel's seams (scene-card.mjs). |
| 66 | * Returns {fill, openPlace, …}. |
| 67 | */ |
| 68 | export function mountInspector ({ root, ydoc, stageEl, getSlideId, getSelected, getView, commit, onCommand, focusBox = () => {}, labels, upload, scene }) { |
| 69 | const forms = {} |
| 70 | for (const s of ['slide', 'deck', 'block']) forms[s] = root.querySelector(`[slides-inspector-form][data-section="${s}"]`) |
| 71 | // Where the listeners go: the card, and the deck's form in its |
| 72 | // dialog. Not the whole editor, whose other [data-command] buttons |
| 73 | // are editor-entry.js's to run. |
| 74 | const card = forms.slide.closest('[slides-inspector]') |
| 75 | const field = (form, name) => form.querySelector(`[name="${name}"]`) |
| 76 | let current = { meta: null, slide: null, block: null, deck: null } |
| 77 | |
| 78 | // The image block's file input and the Deck section's Unused images |
| 79 | // list (inspector-image.mjs). Mounted here so it shares this file's |
| 80 | // fill() and destroy() rather than needing its own lifecycle beside |
| 81 | // them. |
| 82 | const assets = mountAssets({ |
| 83 | root, |
| 84 | getView: () => current.deck || { slides: [], assets: {} }, |
| 85 | getSlideId, |
| 86 | getSelected, |
| 87 | getBlock: () => current.block, |
| 88 | commit, |
| 89 | upload, |
| 90 | labels: labels.upload || {} |
| 91 | }) |
| 92 | |
| 93 | // The card at rest: the layout pictures, the question before leaving |
| 94 | // Free placement, and the rows' popovers (layout-pick.mjs). |
| 95 | const layoutPick = mountLayoutPick({ form: forms.slide, getSlide: () => current.slide, commit }) |
| 96 | // The box's After… popover (inspector-steps.mjs): its rows' choices. |
| 97 | const appears = mountAppears({ form: forms.block, getSlide: () => current.slide, getBlock: () => current.block, getScenes: () => current.deck && current.deck.scenes, commit }) |
| 98 | // A scene block's card, data and code (scene-card.mjs). |
| 99 | const sceneCard = mountSceneCard({ root, form: forms.block, getView: () => current.deck, getSlide: () => current.slide, getBlock: () => current.block, commit, scene }) |
| 100 | // The two font fields, with the person's own fonts (deck-fonts.mjs). |
| 101 | const deckFonts = mountDeckFonts({ base: `/d/${root.dataset.deck}`, getMeta: () => current.meta, commit, refill: () => fill() }) |
| 102 | // A family uploaded in the Fonts tab is offered once this window has |
| 103 | // focus again (the link is there only where there is a library). |
| 104 | const uploadLink = root.querySelector('[slides-font-upload]') |
| 105 | const refreshing = new AbortController() |
| 106 | if (uploadLink) { |
| 107 | const again = () => refreshYourFonts({ |
| 108 | selects: [field(forms.deck, 'rst-deck-font-heading'), field(forms.deck, 'rst-deck-font-body')], |
| 109 | label: uploadLink.dataset.yoursLabel, |
| 110 | refill: () => fill() |
| 111 | }).catch(err => console.error('deck fonts:', err)) |
| 112 | globalThis.addEventListener('focus', again, { signal: refreshing.signal }) |
| 113 | document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'visible') again() }, { signal: refreshing.signal }) |
| 114 | } |
| 115 | |
| 116 | // The theme's own accent, from the page's catalog: the stage, which |
| 117 | // would say it too, is not redrawn until a moment after an undo. |
| 118 | function themeAccent (theme) { |
| 119 | try { |
| 120 | const raw = forms.deck.closest('dialog')?.dataset.themeAccents |
| 121 | const hex = raw ? JSON.parse(raw)[theme] : '' |
| 122 | return /^#[0-9a-fA-F]{6}$/.test(hex || '') ? hex.toLowerCase() : '' |
| 123 | } catch { return '' } |
| 124 | } |
| 125 | |
| 126 | function fillDeck (meta) { |
| 127 | const f = forms.deck |
| 128 | setValue(field(f, 'rst-deck-theme'), meta.theme) |
| 129 | setValue(field(f, 'rst-deck-accent'), meta.accent || themeAccent(meta.theme) || sectionVar(stageEl, '--slides-accent', '#000000')) |
| 130 | fillFontField(field(f, 'rst-deck-font-heading'), meta.fonts ? meta.fonts.heading : '', meta.fontFiles, setValue) |
| 131 | fillFontField(field(f, 'rst-deck-font-body'), meta.fonts ? meta.fonts.body : '', meta.fontFiles, setValue) |
| 132 | setValue(field(f, 'rst-deck-arrive'), meta.arrive || 'appear') |
| 133 | } |
| 134 | |
| 135 | function fillSlide (slide) { |
| 136 | const f = forms.slide |
| 137 | const bg = slide.background || { kind: 'theme', value: '' } |
| 138 | setValue(field(f, 'rst-slide-background'), bg.kind === 'color' ? 'color' : 'theme') |
| 139 | setValue(field(f, 'rst-slide-background-color'), bg.kind === 'color' && bg.value ? bg.value : sectionVar(stageEl, '--slides-bg', '#ffffff')) |
| 140 | setValue(field(f, 'rst-slide-transition'), slide.transition) |
| 141 | setValue(field(f, 'rst-slide-phone'), slide.phone) |
| 142 | showWhere(f, 'data-layout', v => v === slide.layout) |
| 143 | showWhere(f, 'data-background', v => v === bg.kind) |
| 144 | } |
| 145 | |
| 146 | function fill ({ force = false } = {}) { |
| 147 | if (force) return forceWrites(() => fill()) |
| 148 | const v = view(ydoc) |
| 149 | const slide = v.slides.find(s => s.id === getSlideId()) || null |
| 150 | const block = slide ? slide.blocks.find(b => b.id === getSelected()) || null : null |
| 151 | current = { meta: v.meta, slide, block, slides: v.slides, deck: v } |
| 152 | fillDeck(v.meta) |
| 153 | assets.fill() |
| 154 | // Which of the three forms shows is not this file's to say: it is |
| 155 | // the editor root's data-inspect (inspect.mjs) and editor.css. |
| 156 | // Here they are filled, and the controls inside them shown or |
| 157 | // hidden for the slide's layout and the block's type. |
| 158 | if (slide) fillSlide(slide) |
| 159 | // With no slide too: a question about a slide that has gone withdraws. |
| 160 | layoutPick.fill() |
| 161 | if (block) fillBlock({ form: forms.block, slide, block, view: getView(), ink: sectionVar(stageEl, '--slides-fg', '#000000'), scenes: v.scenes }) |
| 162 | sceneCard.fill() |
| 163 | } |
| 164 | |
| 165 | function onChange (e) { |
| 166 | const el = e.target |
| 167 | const form = el.closest('form[data-section]') |
| 168 | if (!form) return |
| 169 | const name = el.name || '' |
| 170 | try { |
| 171 | if (form === forms.deck && current.meta) { |
| 172 | const f = name.replace('rst-deck-', '') |
| 173 | if (f === 'font-heading' || f === 'font-body') deckFonts.change(f, el) |
| 174 | else if (['theme', 'accent', 'arrive'].includes(f)) commit([deckOp(f, el.value, current.meta)]) |
| 175 | } else if (form === forms.slide && current.slide) { |
| 176 | const s = current.slide |
| 177 | if (name === 'rst-slide-transition') commit([slideOp(s, 'transition', el.value)]) |
| 178 | else if (name === 'rst-slide-phone') commit([slideOp(s, 'phone', el.value)]) |
| 179 | else if (name === 'rst-slide-background-color') commit([slideOp(s, 'background-color', el.value)]) |
| 180 | else if (name === 'rst-slide-background') { |
| 181 | commit([el.value === 'color' |
| 182 | ? slideOp(s, 'background-color', field(form, 'rst-slide-background-color').value) |
| 183 | : slideOp(s, 'background', 'theme')]) |
| 184 | } |
| 185 | } else if (form === forms.block && current.slide && current.block) { |
| 186 | const ops = blockChange({ el, slide: current.slide, block: current.block, view: getView() }) |
| 187 | if (ops.length) commit(ops) |
| 188 | } |
| 189 | } catch (err) { |
| 190 | console.error('inspector:', err) |
| 191 | fill() |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | function onClick (e) { |
| 196 | const btn = e.target.closest('button') |
| 197 | if (!btn || !root.contains(btn)) return |
| 198 | if (btn.dataset.command) onCommand(btn.dataset.command) |
| 199 | } |
| 200 | |
| 201 | // Escape in a box's options: focus back on that box on the stage, and |
| 202 | // the box still selected, its options still showing. Heard here, |
| 203 | // before selection.mjs's own Escape on the document (which would give |
| 204 | // the box up and leave focus nowhere), and not from what Escape |
| 205 | // closes itself: a menu, an input method's composition. |
| 206 | function onKeydown (e) { |
| 207 | if (e.key !== 'Escape' || e.isComposing || e.defaultPrevented) return |
| 208 | const t = e.target instanceof Element ? e.target : null |
| 209 | if (!t || !card.contains(t) || t.closest('[rst-dropdown]')) return |
| 210 | if (root.closest('[data-inspect]')?.dataset.inspect !== 'box') return |
| 211 | const id = getSelected() |
| 212 | if (!id) return |
| 213 | e.preventDefault() |
| 214 | focusBox(id) |
| 215 | } |
| 216 | |
| 217 | // One signal for every listener: the inspector's markup is the |
| 218 | // page's and survives a resync, so a second mount over it must be |
| 219 | // able to say that the first is finished with it. The deck's form |
| 220 | // submits to close its dialog (method="dialog"), so only the card's |
| 221 | // submits are stopped. |
| 222 | const abort = new AbortController() |
| 223 | const { signal } = abort |
| 224 | for (const el of [card, forms.deck]) { |
| 225 | el.addEventListener('change', onChange, { signal }) |
| 226 | el.addEventListener('click', onClick, { signal }) |
| 227 | } |
| 228 | card.addEventListener('submit', e => e.preventDefault(), { signal }) |
| 229 | card.addEventListener('keydown', onKeydown, { signal }) |
| 230 | |
| 231 | // Position and size: as this page load last left it. |
| 232 | const place = forms.block.querySelector('[slides-place]') |
| 233 | const placeMount = place ? mountPlace(place) : null |
| 234 | |
| 235 | return { |
| 236 | // Position and size open (the box's menu), and its first field. |
| 237 | openPlace () { |
| 238 | if (!place) return null |
| 239 | place.open = true |
| 240 | return place.querySelector('input') |
| 241 | }, |
| 242 | fill, |
| 243 | // The uploader's progress line, so editor-entry.js can hand |
| 244 | // createUploader an onState without reaching past this mount. |
| 245 | onUploadState: assets.onState, |
| 246 | destroy () { assets.destroy(); layoutPick.destroy(); appears.destroy(); sceneCard.destroy(); if (placeMount) placeMount.destroy(); refreshing.abort(); abort.abort() }, |
| 247 | get slide () { return current.slide }, |
| 248 | get block () { return current.block } |
| 249 | } |
| 250 | } |
| 251 | |