| 1 | // The checking pass of applyOps — core/src/ops.rs's Model and the |
| 2 | // vocabularies it closes. |
| 3 | // |
| 4 | // A Yjs transaction cannot be rolled back, so "a failure applies |
| 5 | // nothing" has to be true before the first write: every op, once parsed |
| 6 | // (ops-parse.mjs), is checked against a model of the deck — the slide |
| 7 | // ids, their block ids and types, the asset ids — that each op updates |
| 8 | // as it is checked, so a reference that only becomes wrong halfway |
| 9 | // through a batch (remove_block on a block an earlier op removed) is |
| 10 | // caught here too. The messages are the core's, word for word, so the |
| 11 | // inspector and the drive API say the same thing about the same mistake. |
| 12 | import { fontFilesValue } from './font-files.mjs' |
| 13 | import { getString, getArray, getObject, orderedMaps } from './ydoc.mjs' |
| 14 | import { checkSceneOp, newBlockScene, setBlockScene, sceneRefOf, sceneStatesOf } from './scenes.mjs' |
| 15 | |
| 16 | export { parseOp } from './ops-parse.mjs' |
| 17 | |
| 18 | export const LAYOUTS = ['flow', 'canvas'] |
| 19 | export const TEMPLATES = ['title', 'title-body', 'two-column', 'section', 'image-full', 'quote', 'blank'] |
| 20 | export const REGIONS = ['heading', 'body', 'aside', 'media'] |
| 21 | export const BACKGROUND_KINDS = ['theme', 'color', 'image'] |
| 22 | export const SLIDE_PHONES = ['inherit', 'reflow'] |
| 23 | export const TRANSITIONS = ['none', 'fade'] |
| 24 | export const BLOCK_TYPES = ['heading', 'text', 'list', 'quote', 'image', 'columns', 'code', 'shape', 'table', 'divider', 'spacer', 'scene', 'clip', 'qr'] |
| 25 | export const TEXT_TYPES = ['heading', 'text', 'list', 'quote', 'code', 'shape'] |
| 26 | export const SIZES = ['s', 'm', 'l'] |
| 27 | export const ITEMS = ['together', 'each'] |
| 28 | export const TALKS = ['full', 'parked'] |
| 29 | export const ARRIVALS = ['appear', 'fade', 'rise'] |
| 30 | export const MAX_TABLE_SIDE = 100 |
| 31 | |
| 32 | export const isTextType = kind => TEXT_TYPES.includes(kind) |
| 33 | |
| 34 | export class OpsError extends Error { |
| 35 | constructor (message, index) { |
| 36 | super(message) |
| 37 | this.name = 'OpsError' |
| 38 | this.index = index |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | const fail = msg => { throw new Error(msg) } |
| 43 | const isInt = v => Number.isInteger(v) |
| 44 | |
| 45 | function inVocab (what, value, vocab) { |
| 46 | if (!vocab.includes(value)) fail(`${what} ${JSON.stringify(value)} is not one of ${vocab.join(', ')}`) |
| 47 | } |
| 48 | function nonEmpty (what, value) { |
| 49 | if (value === '') fail(`${what} is empty`) |
| 50 | } |
| 51 | |
| 52 | export const QR_URL_MESSAGE = 'qr url must be a follow link: http(s)://<host>/s/<token>?follow=1' |
| 53 | |
| 54 | /** |
| 55 | * Whether url is shaped like a share link's follow address: |
| 56 | * http(s)://<host>/s/<token>?follow=1, a host with no user info, a |
| 57 | * token of 16 to 128 base64url characters. core/src/schema.rs's |
| 58 | * is_follow_url, and internal/slides/deck's FollowAddress: the server |
| 59 | * mints the address, so which host is its business. |
| 60 | */ |
| 61 | export function isFollowURL (url) { |
| 62 | return typeof url === 'string' && /^https?:\/\/[A-Za-z0-9.:[\]-]{1,253}\/s\/[A-Za-z0-9_-]{16,128}\?follow=1$/.test(url) |
| 63 | } |
| 64 | |
| 65 | /** A qr block's url from props when present: a follow link, or null, which clears it. */ |
| 66 | export function qrURL (props) { |
| 67 | const v = props.url |
| 68 | if (v === undefined || v === null) return |
| 69 | if (!isFollowURL(v)) fail(QR_URL_MESSAGE) |
| 70 | } |
| 71 | |
| 72 | /** rows/cols from props when present: whole numbers within 1..=MAX_TABLE_SIDE. */ |
| 73 | export function tableSide (props, key) { |
| 74 | const v = props[key] |
| 75 | if (v === undefined) return null |
| 76 | if (!isInt(v) || v < 1 || v > MAX_TABLE_SIDE) fail(`table ${key} must be a whole number from 1 to ${MAX_TABLE_SIDE}`) |
| 77 | return v |
| 78 | } |
| 79 | |
| 80 | function blockIndex (s, id) { |
| 81 | const i = s.blocks.findIndex(b => b.id === id) |
| 82 | if (i === -1) fail(`no block ${JSON.stringify(id)} in slide ${JSON.stringify(s.id)}`) |
| 83 | return i |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * A columns block's props.children (spec §2.4): block ids, each naming |
| 88 | * a block on s other than selfId, each named once, none already a child |
| 89 | * of another columns block, and none with selfId underneath it — the |
| 90 | * children of a slide are a forest, one parent per block and no way |
| 91 | * back up (codex 7). null clears. The messages are core/src/ops.rs's, |
| 92 | * word for word. |
| 93 | */ |
| 94 | function validateChildren (s, selfId, value) { |
| 95 | if (value === null) return [] |
| 96 | if (!Array.isArray(value)) fail('columns children must be an array of block ids') |
| 97 | const out = [] |
| 98 | for (const cid of value) { |
| 99 | if (typeof cid !== 'string') fail('columns children must be an array of block ids') |
| 100 | if (cid === selfId) fail(`columns ${JSON.stringify(selfId)} cannot name itself in children`) |
| 101 | if (!s.blocks.some(b => b.id === cid)) fail(`columns children names ${JSON.stringify(cid)}, not a block on slide ${JSON.stringify(s.id)}`) |
| 102 | if (out.includes(cid)) fail(`columns children names ${JSON.stringify(cid)} twice`) |
| 103 | const other = s.blocks.find(b => b.id !== selfId && b.children !== null && b.children.includes(cid)) |
| 104 | if (other) fail(`columns children names ${JSON.stringify(cid)}, already a child of columns ${JSON.stringify(other.id)}`) |
| 105 | if (contains(s, cid, selfId)) fail(`columns children names ${JSON.stringify(cid)}, which would put columns ${JSON.stringify(selfId)} inside itself`) |
| 106 | out.push(cid) |
| 107 | } |
| 108 | return out |
| 109 | } |
| 110 | |
| 111 | /** Whether target is root or sits anywhere under it, following the slide's columns children as the model holds them. */ |
| 112 | function contains (s, root, target) { |
| 113 | const stack = [root] |
| 114 | const seen = new Set() |
| 115 | while (stack.length) { |
| 116 | const id = stack.pop() |
| 117 | if (id === target) return true |
| 118 | if (seen.has(id)) continue |
| 119 | seen.add(id) |
| 120 | const b = s.blocks.find(x => x.id === id) |
| 121 | if (b && b.children !== null) stack.push(...b.children) |
| 122 | } |
| 123 | return false |
| 124 | } |
| 125 | |
| 126 | export class Model { |
| 127 | static read (r) { |
| 128 | const m = new Model() |
| 129 | m.slides = orderedMaps(r.slides).map(([, sm]) => { |
| 130 | const blocks = getArray(sm, 'blocks') |
| 131 | // Reading order: after and duplicate_slide's new_block_ids are |
| 132 | // positional, and the position that means anything is the one a |
| 133 | // reader of the view sees. |
| 134 | return { |
| 135 | id: getString(sm, 'id') ?? '', |
| 136 | blocks: blocks === null ? [] : orderedMaps(blocks).map(([, b]) => { |
| 137 | const kind = getString(b, 'type') ?? '' |
| 138 | let children = null |
| 139 | if (kind === 'columns') { |
| 140 | const c = (getObject(b, 'props') ?? {}).children |
| 141 | children = Array.isArray(c) ? c.filter(x => typeof x === 'string') : [] |
| 142 | } |
| 143 | const scene = kind === 'scene' ? sceneRefOf(getObject(b, 'props') ?? {}) : null |
| 144 | return { id: getString(b, 'id') ?? '', kind, children, scene } |
| 145 | }) |
| 146 | } |
| 147 | }) |
| 148 | m.assets = new Set() |
| 149 | r.assets.forEach((_, k) => m.assets.add(k)) |
| 150 | m.scenes = sceneStatesOf(r.scenes) |
| 151 | return m |
| 152 | } |
| 153 | |
| 154 | slideIndex (id) { |
| 155 | const i = this.slides.findIndex(s => s.id === id) |
| 156 | if (i === -1) fail(`no slide ${JSON.stringify(id)}`) |
| 157 | return i |
| 158 | } |
| 159 | |
| 160 | hasSlide (id) { return this.slides.some(s => s.id === id) } |
| 161 | |
| 162 | slide (id) { return this.slides[this.slideIndex(id)] } |
| 163 | |
| 164 | check (op) { |
| 165 | switch (op.op) { |
| 166 | case 'set_meta': |
| 167 | if (typeof op.arrive === 'string') inVocab('arrive', op.arrive, ARRIVALS) |
| 168 | if (Array.isArray(op.fontFiles)) fontFilesValue(op.fontFiles) |
| 169 | return |
| 170 | case 'add_slide': { |
| 171 | nonEmpty('id', op.id) |
| 172 | if (this.hasSlide(op.id)) fail(`slide ${JSON.stringify(op.id)} already exists`) |
| 173 | inVocab('layout', op.layout, LAYOUTS) |
| 174 | if (op.template !== undefined) inVocab('template', op.template, TEMPLATES) |
| 175 | const at = op.after === undefined ? this.slides.length : op.after === null ? 0 : this.slideIndex(op.after) + 1 |
| 176 | this.slides.splice(at, 0, { id: op.id, blocks: [] }) |
| 177 | return |
| 178 | } |
| 179 | case 'remove_slide': |
| 180 | this.slides.splice(this.slideIndex(op.id), 1) |
| 181 | return |
| 182 | case 'move_slide': { |
| 183 | this.slideIndex(op.id) |
| 184 | if (op.after !== undefined) { |
| 185 | if (op.after === op.id) fail('move_slide: after is the slide itself') |
| 186 | this.slideIndex(op.after) |
| 187 | } |
| 188 | const [s] = this.slides.splice(this.slideIndex(op.id), 1) |
| 189 | const at = op.after === undefined ? 0 : this.slideIndex(op.after) + 1 |
| 190 | this.slides.splice(at, 0, s) |
| 191 | return |
| 192 | } |
| 193 | case 'duplicate_slide': { |
| 194 | nonEmpty('new_id', op.new_id) |
| 195 | const i = this.slideIndex(op.id) |
| 196 | if (this.hasSlide(op.new_id)) fail(`slide ${JSON.stringify(op.new_id)} already exists`) |
| 197 | const n = this.slides[i].blocks.length |
| 198 | if (op.new_block_ids.length !== n) fail(`duplicate_slide: ${n} blocks but ${op.new_block_ids.length} new_block_ids`) |
| 199 | if (new Set(op.new_block_ids).size !== n || op.new_block_ids.some(x => x === '')) fail('duplicate_slide: new_block_ids must be distinct and non-empty') |
| 200 | const idMap = new Map(this.slides[i].blocks.map((b, k) => [b.id, op.new_block_ids[k]])) |
| 201 | const blocks = this.slides[i].blocks.map((b, k) => ({ |
| 202 | id: op.new_block_ids[k], |
| 203 | kind: b.kind, |
| 204 | children: b.children === null ? null : b.children.map(c => idMap.get(c) ?? c), |
| 205 | scene: b.scene && { ...b.scene } |
| 206 | })) |
| 207 | this.slides.splice(i + 1, 0, { id: op.new_id, blocks }) |
| 208 | return |
| 209 | } |
| 210 | case 'set_slide': |
| 211 | this.slideIndex(op.id) |
| 212 | if (op.layout !== undefined) inVocab('layout', op.layout, LAYOUTS) |
| 213 | if (op.template !== undefined) inVocab('template', op.template, TEMPLATES) |
| 214 | if (op.background !== undefined) inVocab('background.kind', op.background.kind, BACKGROUND_KINDS) |
| 215 | if (op.phone !== undefined) inVocab('phone', op.phone, SLIDE_PHONES) |
| 216 | if (op.transition !== undefined) inVocab('transition', op.transition, TRANSITIONS) |
| 217 | if (typeof op.talk === 'string') inVocab('talk', op.talk, TALKS) |
| 218 | return |
| 219 | case 'set_notes': |
| 220 | this.slideIndex(op.id) |
| 221 | return |
| 222 | case 'add_block': { |
| 223 | nonEmpty('id', op.id) |
| 224 | inVocab('type', op.type, BLOCK_TYPES) |
| 225 | if (op.region !== undefined) inVocab('region', op.region, REGIONS) |
| 226 | if (op.type === 'table') { tableSide(op.props, 'rows'); tableSide(op.props, 'cols') } |
| 227 | if (op.type === 'qr') qrURL(op.props) |
| 228 | const scene = newBlockScene(this, op.type, op.props) |
| 229 | const s = this.slide(op.slide) |
| 230 | if (s.blocks.some(b => b.id === op.id)) fail(`block ${JSON.stringify(op.id)} already exists in slide ${JSON.stringify(op.slide)}`) |
| 231 | let children = null |
| 232 | if (op.type === 'columns') children = op.props.children === undefined ? [] : validateChildren(s, op.id, op.props.children) |
| 233 | const at = op.after === undefined ? s.blocks.length : op.after === null ? 0 : blockIndex(s, op.after) + 1 |
| 234 | s.blocks.splice(at, 0, { id: op.id, kind: op.type, children, scene }) |
| 235 | return |
| 236 | } |
| 237 | case 'remove_block': { |
| 238 | const s = this.slide(op.slide) |
| 239 | s.blocks.splice(blockIndex(s, op.id), 1) |
| 240 | for (const b of s.blocks) if (b.children !== null) b.children = b.children.filter(c => c !== op.id) |
| 241 | return |
| 242 | } |
| 243 | case 'move_block': { |
| 244 | const s = this.slide(op.slide) |
| 245 | const i = blockIndex(s, op.id) |
| 246 | if (op.after !== undefined) { |
| 247 | if (op.after === op.id) fail('move_block: after is the block itself') |
| 248 | blockIndex(s, op.after) |
| 249 | } |
| 250 | const [b] = s.blocks.splice(i, 1) |
| 251 | const at = op.after === undefined ? 0 : blockIndex(s, op.after) + 1 |
| 252 | s.blocks.splice(at, 0, b) |
| 253 | return |
| 254 | } |
| 255 | case 'set_block': { |
| 256 | const s = this.slide(op.slide) |
| 257 | const i = blockIndex(s, op.id) |
| 258 | if (op.props !== undefined) setBlockScene(this, s.blocks[i], op.props) |
| 259 | if (typeof op.region === 'string') inVocab('region', op.region, REGIONS) |
| 260 | if (typeof op.step === 'number' && !(Number.isFinite(op.step) && op.step > 0)) fail('step must be a number above 0') |
| 261 | if (typeof op.items === 'string') { |
| 262 | inVocab('items', op.items, ITEMS) |
| 263 | if (s.blocks[i].kind !== 'list') fail(`block ${JSON.stringify(op.id)} is a ${s.blocks[i].kind}; items is for lists only`) |
| 264 | } |
| 265 | if (s.blocks[i].kind === 'table' && op.props !== undefined) { tableSide(op.props, 'rows'); tableSide(op.props, 'cols') } |
| 266 | if (s.blocks[i].kind === 'qr' && op.props !== undefined) qrURL(op.props) |
| 267 | if (s.blocks[i].kind === 'columns' && op.props !== undefined && op.props.children !== undefined) { |
| 268 | s.blocks[i].children = validateChildren(s, op.id, op.props.children) |
| 269 | } |
| 270 | return |
| 271 | } |
| 272 | case 'set_text': { |
| 273 | const s = this.slide(op.slide) |
| 274 | const b = s.blocks[blockIndex(s, op.id)] |
| 275 | if (!isTextType(b.kind)) fail(`block ${JSON.stringify(op.id)} is a ${b.kind} and carries no text`) |
| 276 | return |
| 277 | } |
| 278 | case 'set_phone': { |
| 279 | const s = this.slide(op.slide) |
| 280 | blockIndex(s, op.id) |
| 281 | if (op.phone.size !== undefined) inVocab('phone.size', op.phone.size, SIZES) |
| 282 | return |
| 283 | } |
| 284 | case 'register_asset': |
| 285 | nonEmpty('id', op.id) |
| 286 | nonEmpty('ref', op.ref) |
| 287 | if (op.duration < 0) fail('duration is below 0') |
| 288 | if (this.assets.has(op.id)) fail(`asset ${JSON.stringify(op.id)} is already registered`) |
| 289 | this.assets.add(op.id) |
| 290 | return |
| 291 | case 'remove_asset': |
| 292 | if (!this.assets.delete(op.id)) fail(`no asset ${JSON.stringify(op.id)}`) |
| 293 | return |
| 294 | case 'add_scene': case 'set_scene': case 'remove_scene': |
| 295 | checkSceneOp(this, op) |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |