| 1 | // The JSON view of a deck — core/src/view.rs, key for key. What the drive |
| 2 | // API returns and what the renderer reads; the vectors compare this to |
| 3 | // the core's own view of the same document as parsed JSON, so key order |
| 4 | // does not matter here (generate.mjs sorts keys when it writes a file). |
| 5 | // |
| 6 | // Every key has a default so a deck a peer left half-written still views |
| 7 | // as something: an absent layout is flow, an absent template blank, an |
| 8 | // absent z is 0. slides, and the blocks of one, are in reading order — |
| 9 | // the order key sorted, array index for ties — and the key itself is |
| 10 | // not in the view. A |
| 11 | // block's text key is present for text-bearing blocks |
| 12 | // (any block that carries a text fragment) and absent otherwise; a |
| 13 | // table's cells appear as props.cells, a row-major array of texts. |
| 14 | // |
| 15 | // Four keys are omitted when absent, because their absence is the |
| 16 | // meaning: a block's step (a number above 0) and items (together or |
| 17 | // each), a slide's talk (full or parked), the meta's arrive (appear, |
| 18 | // fade or rise). A value outside those reads as absent. |
| 19 | import * as Y from 'yjs' |
| 20 | import { roots, getString, getNumber, getArray, getFragment, getObject, orderedMaps, outToJSON, anyToJSON, put } from './ydoc.mjs' |
| 21 | import { readText } from './schema.mjs' |
| 22 | import { fontFilesValue } from './font-files.mjs' |
| 23 | import { sceneView } from './scenes.mjs' |
| 24 | import { ITEMS, TALKS, ARRIVALS } from './ops-model.mjs' |
| 25 | |
| 26 | export const DEFAULT_THEME = 'paper' |
| 27 | export const DEFAULT_ASPECT = '16:9' |
| 28 | export const DEFAULT_TEMPLATE = 'blank' |
| 29 | |
| 30 | // --- typed leaves --- |
| 31 | // |
| 32 | // The server reads this view through typed structs, and a peer can put |
| 33 | // any shape at any key (box.x = "a", background.kind = 5, fonts.body = |
| 34 | // [], an asset that is a string). Passed through untyped, any one of |
| 35 | // those fails the server's parse of the whole deck. So each leaf with a |
| 36 | // fixed shape is rebuilt from the fields that have the right type, and |
| 37 | // one that cannot be made is omitted — null, the default, a skipped |
| 38 | // entry. core/src/view.rs applies the same rules, key for key; the |
| 39 | // vectors compare the two, and view.test.mjs is the Rust test's twin. |
| 40 | |
| 41 | const isObject = v => v !== null && typeof v === 'object' && !Array.isArray(v) |
| 42 | |
| 43 | /** A present string field, or null; `undefined` when present and mistyped. */ |
| 44 | function stringField (o, key) { |
| 45 | const v = o[key] |
| 46 | if (v === undefined || v === null) return null |
| 47 | return typeof v === 'string' ? v : undefined |
| 48 | } |
| 49 | |
| 50 | function numberField (o, key) { |
| 51 | const v = o[key] |
| 52 | if (v === undefined || v === null) return null |
| 53 | return typeof v === 'number' ? v : undefined |
| 54 | } |
| 55 | |
| 56 | /** {x, y, w, h}, all four numbers, or null. */ |
| 57 | export function boxValue (v) { |
| 58 | if (!isObject(v)) return null |
| 59 | const out = {} |
| 60 | for (const key of ['x', 'y', 'w', 'h']) { |
| 61 | const n = numberField(v, key) |
| 62 | if (n === null || n === undefined) return null |
| 63 | out[key] = n |
| 64 | } |
| 65 | return out |
| 66 | } |
| 67 | |
| 68 | /** {kind, value} with kind a string, else the theme default. */ |
| 69 | export function backgroundValue (v) { |
| 70 | const fallback = { kind: 'theme', value: '' } |
| 71 | if (!isObject(v)) return fallback |
| 72 | const kind = stringField(v, 'kind') |
| 73 | const value = stringField(v, 'value') |
| 74 | if (kind === null || kind === undefined || value === undefined) return fallback |
| 75 | return { kind, value: value ?? '' } |
| 76 | } |
| 77 | |
| 78 | /** {heading, body}, both strings when present, else null. */ |
| 79 | export function fontsValue (v) { |
| 80 | if (!isObject(v)) return null |
| 81 | const heading = stringField(v, 'heading') |
| 82 | const body = stringField(v, 'body') |
| 83 | if (heading === undefined || body === undefined) return null |
| 84 | return { heading: heading ?? '', body: body ?? '' } |
| 85 | } |
| 86 | |
| 87 | /** meta.fontFiles when the whole list is well formed, else null (the key is omitted). */ |
| 88 | function fontFiles (m) { |
| 89 | const v = m.get('fontFiles') |
| 90 | if (!Array.isArray(v)) return null |
| 91 | try { return fontFilesValue(anyToJSON(v)) } catch { return null } |
| 92 | } |
| 93 | |
| 94 | /** The §2.5 overrides that have the right type, field by field; order is a whole number. */ |
| 95 | export function phoneValue (v) { |
| 96 | if (!isObject(v)) return {} |
| 97 | const out = {} |
| 98 | if (typeof v.hidden === 'boolean') out.hidden = v.hidden |
| 99 | const order = numberField(v, 'order') |
| 100 | if (typeof order === 'number') out.order = Math.trunc(order) |
| 101 | const size = stringField(v, 'size') |
| 102 | if (typeof size === 'string') out.size = size |
| 103 | const box = boxValue(v.box) |
| 104 | if (box) out.box = box |
| 105 | return out |
| 106 | } |
| 107 | |
| 108 | /** {ref, name, width, height, alt} for an entry with a string ref; null otherwise. */ |
| 109 | export function assetValue (v) { |
| 110 | if (!isObject(v)) return null |
| 111 | const ref = stringField(v, 'ref') |
| 112 | if (ref === null || ref === undefined) return null |
| 113 | const text = key => stringField(v, key) ?? '' |
| 114 | const whole = key => { const n = numberField(v, key); return typeof n === 'number' ? Math.trunc(n) : 0 } |
| 115 | const out = { ref, name: text('name'), width: whole('width'), height: whole('height'), alt: text('alt') } |
| 116 | // A clip's video: its poster's ref and whole milliseconds, each only when there is one. |
| 117 | const poster = stringField(v, 'poster') |
| 118 | if (typeof poster === 'string' && poster) out.poster = poster |
| 119 | const ms = numberField(v, 'duration') |
| 120 | if (typeof ms === 'number' && Number.isFinite(ms) && Math.trunc(ms) > 0) out.duration = Math.trunc(ms) |
| 121 | return out |
| 122 | } |
| 123 | |
| 124 | /** A string key whose value is one of vocab, else null: off the vocabulary reads as no key. */ |
| 125 | function vocabString (m, key, vocab) { |
| 126 | const v = getString(m, key) |
| 127 | return v !== null && vocab.includes(v) ? v : null |
| 128 | } |
| 129 | |
| 130 | /** A block's step: a finite number above 0, else null (the box arrives with its slide). */ |
| 131 | function stepValue (m) { |
| 132 | const n = getNumber(m, 'step') |
| 133 | return n !== null && Number.isFinite(n) && n > 0 ? n : null |
| 134 | } |
| 135 | |
| 136 | function blockView (m) { |
| 137 | const props = getObject(m, 'props') ?? {} |
| 138 | const cells = getArray(m, 'cells') |
| 139 | if (cells !== null) { |
| 140 | props.cells = cells.toArray().map(c => (c instanceof Y.XmlFragment && !(c instanceof Y.XmlElement) ? readText(c) : outToJSON(c))) |
| 141 | } |
| 142 | const block = { |
| 143 | id: getString(m, 'id') ?? '', |
| 144 | type: getString(m, 'type') ?? '', |
| 145 | props, |
| 146 | region: getString(m, 'region'), |
| 147 | box: boxValue(getObject(m, 'box')), |
| 148 | z: getNumber(m, 'z') ?? 0, |
| 149 | phone: phoneValue(getObject(m, 'phone')) |
| 150 | } |
| 151 | const text = getFragment(m, 'text') |
| 152 | if (text !== null) block.text = readText(text) |
| 153 | const step = stepValue(m) |
| 154 | if (step !== null) block.step = step |
| 155 | const items = vocabString(m, 'items', ITEMS) |
| 156 | if (items !== null) block.items = items |
| 157 | return block |
| 158 | } |
| 159 | |
| 160 | function slideView (m) { |
| 161 | const blocks = getArray(m, 'blocks') |
| 162 | const notes = getFragment(m, 'notes') |
| 163 | const slide = { |
| 164 | id: getString(m, 'id') ?? '', |
| 165 | layout: getString(m, 'layout') ?? 'flow', |
| 166 | template: getString(m, 'template') ?? DEFAULT_TEMPLATE, |
| 167 | background: backgroundValue(getObject(m, 'background')), |
| 168 | phone: getString(m, 'phone') ?? 'inherit', |
| 169 | transition: getString(m, 'transition') ?? 'none', |
| 170 | notes: notes !== null ? readText(notes) : [], |
| 171 | // Reading order (ydoc.mjs orderedMaps); the order key itself |
| 172 | // stays out of the view, so every reader sees one sequence. |
| 173 | blocks: blocks !== null ? orderedMaps(blocks).map(([, b]) => blockView(b)) : [] |
| 174 | } |
| 175 | const talk = vocabString(m, 'talk', TALKS) |
| 176 | if (talk !== null) slide.talk = talk |
| 177 | return slide |
| 178 | } |
| 179 | |
| 180 | /** The deck as JSON. Never empty: an untouched document views as the defaults. */ |
| 181 | export function view (doc) { |
| 182 | const r = roots(doc) |
| 183 | const assets = {} |
| 184 | r.assets.forEach((v, k) => { |
| 185 | const a = assetValue(outToJSON(v)) |
| 186 | if (a) put(assets, k, a) |
| 187 | }) |
| 188 | const meta = { |
| 189 | title: getString(r.meta, 'title') ?? '', |
| 190 | theme: getString(r.meta, 'theme') ?? DEFAULT_THEME, |
| 191 | accent: getString(r.meta, 'accent'), |
| 192 | fonts: fontsValue(getObject(r.meta, 'fonts')), |
| 193 | aspect: getString(r.meta, 'aspect') ?? DEFAULT_ASPECT |
| 194 | } |
| 195 | const files = fontFiles(r.meta) |
| 196 | if (files !== null) meta.fontFiles = files |
| 197 | const arrive = vocabString(r.meta, 'arrive', ARRIVALS) |
| 198 | if (arrive !== null) meta.arrive = arrive |
| 199 | const out = { |
| 200 | meta, |
| 201 | // Slides in reading order (ydoc.mjs orderedMaps), the same way the |
| 202 | // blocks of one are; the order key itself never reaches the view. |
| 203 | slides: orderedMaps(r.slides).map(([, m]) => slideView(m)), |
| 204 | assets |
| 205 | } |
| 206 | // Omitted when there are none, so a deck without scenes views as it |
| 207 | // always did (scenes.mjs). |
| 208 | const scenes = {} |
| 209 | r.scenes.forEach((v, k) => { |
| 210 | const s = sceneView(outToJSON(v)) |
| 211 | if (s) put(scenes, k, s) |
| 212 | }) |
| 213 | if (Object.keys(scenes).length) out.scenes = scenes |
| 214 | return out |
| 215 | } |
| 216 | |