oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/inspector-ops.mjs — every inspector control as the one
2// drive operation it is (spec §3.1: "every inspector control is one
3// drive operation, and the editor calls the same function the API
4// handler calls"). Pure: a field name and a value in, an op out, over
5// the deck's JSON view (view.mjs), so inspector.test.mjs can hold each
6// control to its op with no DOM.
7//
8// The view-dependent block fields — box, size, hidden, order — go
9// through switcher.mjs's routeEdit, which is where the desktop/phone
10// rule lives; everything here is the rest.
11import { routeEdit } from './switcher.mjs'
12import { defaultRegion } from './layout.mjs'
13
14export const FONT_KEYS = ['heading', 'body']
15export const DEFAULT_BOX = { x: 0.1, y: 0.1, w: 0.5, h: 0.3 }
16export const TEXT_TYPES = ['heading', 'text', 'list', 'quote', 'code', 'shape']
17
18// --- deck ---
19
20/** field: title | theme | accent | accent-reset | font-heading | font-body | arrive (appear is no setting: null). */
21export function deckOp (field, value, meta) {
22 switch (field) {
23 case 'title': return { op: 'set_meta', title: value }
24 case 'theme': return { op: 'set_meta', theme: value }
25 case 'accent': return { op: 'set_meta', accent: value }
26 case 'accent-reset': return { op: 'set_meta', accent: null }
27 case 'arrive': return { op: 'set_meta', arrive: value === 'appear' || !value ? null : value }
28 case 'font-heading':
29 case 'font-body': {
30 const fonts = { heading: '', body: '', ...(meta && meta.fonts ? meta.fonts : {}) }
31 fonts[field.slice(5)] = value
32 if (!fonts.heading && !fonts.body) return { op: 'set_meta', fonts: null }
33 return { op: 'set_meta', fonts: { heading: fonts.heading || '', body: fonts.body || '' } }
34 }
35 }
36 throw new Error(`deckOp: unknown field ${field}`)
37}
38
39// --- slide ---
40
41const DEFAULT_COLOR = '#ffffff'
42
43/** field: layout | template | background | background-color | transition | phone. */
44export function slideOp (slide, field, value) {
45 const id = slide.id
46 switch (field) {
47 case 'layout': return { op: 'set_slide', id, layout: value }
48 case 'template': return { op: 'set_slide', id, template: value }
49 case 'transition': return { op: 'set_slide', id, transition: value }
50 case 'phone': return { op: 'set_slide', id, phone: value }
51 case 'background': {
52 if (value === 'color') {
53 const was = slide.background && slide.background.kind === 'color' ? slide.background.value : ''
54 return { op: 'set_slide', id, background: { kind: 'color', value: was || DEFAULT_COLOR } }
55 }
56 return { op: 'set_slide', id, background: { kind: 'theme', value: '' } }
57 }
58 case 'background-color': return { op: 'set_slide', id, background: { kind: 'color', value } }
59 }
60 throw new Error(`slideOp: unknown field ${field}`)
61}
62
63/**
64 * The slide's place in the talk (steps and presenter spec §4): Skip in
65 * the short version ('full') and Park ('parked') are toggles and never
66 * both. One set_slide writes the one chosen, which clears the other, or
67 * takes it away when the slide already has it.
68 */
69export function talkOp (slide, talk) {
70 return { op: 'set_slide', id: slide.id, talk: slide.talk === talk ? null : talk }
71}
72
73/** move_slide one step: after the previous-but-one going up, after the next going down; null when already at the end. */
74export function moveSlideOp (ids, id, dir) {
75 const i = ids.indexOf(id)
76 if (i < 0) return null
77 if (dir === 'up') {
78 if (i === 0) return null
79 return i === 1 ? { op: 'move_slide', id } : { op: 'move_slide', id, after: ids[i - 2] }
80 }
81 if (i >= ids.length - 1) return null
82 return { op: 'move_slide', id, after: ids[i + 1] }
83}
84
85// --- block ---
86
87/** A prop from a control: kind 'int' parses a number, 'bool' is a checkbox, '' clears the prop. */
88export function propValue (raw, kind) {
89 if (kind === 'bool') return raw === true || raw === 'true' || raw === 'on'
90 if (raw === '' || raw === null || raw === undefined) return null
91 if (kind === 'int') {
92 const n = Number(raw)
93 return Number.isFinite(n) ? Math.trunc(n) : null
94 }
95 return String(raw)
96}
97
98/** set_block on one prop. */
99export function propOp (slideId, block, prop, value) {
100 return { op: 'set_block', slide: slideId, id: block.id, props: { [prop]: value } }
101}
102
103/** set_block region on a flow slide. */
104export function regionOp (slideId, block, region) {
105 return { op: 'set_block', slide: slideId, id: block.id, region }
106}
107
108/** A shape's label: set_text over the block's one paragraph. */
109export function labelOp (slideId, block, text) {
110 return { op: 'set_text', slide: slideId, id: block.id, markdown: text }
111}
112
113/** One box field (x, y, w, h) typed as a percentage, routed by view. */
114export function boxFieldOp ({ view, slideId, block, key, percent }) {
115 const base = view === 'phone' && block.phone && block.phone.box ? block.phone.box : block.box
116 if (!base) return null
117 const n = Number(percent)
118 if (!Number.isFinite(n)) return null
119 const box = { ...base, [key]: n / 100 }
120 return routeEdit({ view, slide: slideId, block, field: 'box', value: box })
121}
122
123/** move_block one step within the slide's array order; null at the end. */
124export function moveBlockOp (slideId, ids, id, dir) {
125 const i = ids.indexOf(id)
126 if (i < 0) return null
127 if (dir === 'up') {
128 if (i === 0) return null
129 return i === 1 ? { op: 'move_block', slide: slideId, id } : { op: 'move_block', slide: slideId, id, after: ids[i - 2] }
130 }
131 if (i >= ids.length - 1) return null
132 return { op: 'move_block', slide: slideId, id, after: ids[i + 1] }
133}
134
135/**
136 * Bring forward / send back: the blocks sorted by z (array order for
137 * ties), the block swapped with its neighbour, and every block whose
138 * z then differs from its position written — so z is a permutation
139 * again however the slide arrived. [] when nothing changes.
140 */
141export function zOps (slideId, blocks, id, dir) {
142 const order = blocks.map((b, i) => ({ id: b.id, z: Number.isFinite(b.z) ? b.z : 0, i }))
143 .sort((a, b) => (a.z - b.z) || (a.i - b.i))
144 const at = order.findIndex(b => b.id === id)
145 if (at < 0) return []
146 const to = dir === 'forward' ? at + 1 : at - 1
147 if (to < 0 || to >= order.length) return []
148 const [moved] = order.splice(at, 1)
149 order.splice(to, 0, moved)
150 return order.map((b, z) => ({ ...b, z })).filter(b => b.z !== blocks[b.i].z)
151 .map(b => ({ op: 'set_block', slide: slideId, id: b.id, z: b.z }))
152}
153
154/**
155 * The op adding a block of kind to slide (the view's slide JSON) after
156 * the selected block, or last: on a flow slide in the body region (a
157 * heading in the heading region, an image in media), on a canvas
158 * slide in the default box at the top z. A heading is level 2 unless
159 * the slide has none; text-bearing types get an empty paragraph to
160 * type into.
161 */
162export function addBlockOp (slide, kind, id, after, props = {}) {
163 const op = { op: 'add_block', slide: slide.id, id, type: kind, props: { ...props } }
164 if (after && slide.blocks.some(b => b.id === after)) op.after = after
165 if (kind === 'heading') op.props.level = slide.blocks.some(b => b.type === 'heading') ? 2 : 1
166 if (kind === 'table') { op.props.rows = 2; op.props.cols = 2 }
167 if (kind === 'columns') op.props.count = 2
168 if (TEXT_TYPES.includes(kind)) op.markdown = ''
169 if (slide.layout === 'canvas') {
170 op.box = { ...DEFAULT_BOX }
171 op.z = slide.blocks.reduce((m, b) => Math.max(m, Number.isFinite(b.z) ? b.z + 1 : 0), 0)
172 } else {
173 op.region = defaultRegion(kind)
174 }
175 return op
176}
177
178/** A block's name in the Outline (outline.mjs): its type's label, then its first words (once, where they are that label). */
179export function blockLabel (block, typeLabel, max = 40) {
180 const paras = Array.isArray(block.text) ? block.text : []
181 const words = paras.map(p => p.map(r => r.t || '').join('')).join(' ').replace(/\s+/g, ' ').trim()
182 if (!words || words.toLocaleLowerCase() === typeLabel.toLocaleLowerCase()) return typeLabel
183 const cut = words.length > max ? words.slice(0, max - 1).trimEnd() + '…' : words
184 return `${typeLabel}: ${cut}`
185}
186
187// --- assets (§2.7) ---
188
189/**
190 * The two ops one finished upload is: the registration itself, and the
191 * selected image block pointing at it.
192 *
193 * `record` is what POST /d/{sqid}/assets answered — the id the server
194 * minted, the ref it verified, and the name, size and alt it validated.
195 * The client writes them rather than the server, so an upload arrives
196 * in the document through the same path, the same update log and the
197 * same undo step as every other change (uploads.mjs's header).
198 *
199 * One batch, so undo takes both back: an asset registered with no block
200 * using it would be an entry in the Unused images list nobody asked
201 * for.
202 */
203export function registerAssetOps (record, slideId, blockId) {
204 const ops = [{
205 op: 'register_asset',
206 id: record.id,
207 ref: record.ref,
208 name: record.name || '',
209 width: Number.isInteger(record.width) ? record.width : 0,
210 height: Number.isInteger(record.height) ? record.height : 0,
211 alt: record.alt || ''
212 }]
213 if (slideId && blockId) {
214 ops.push({ op: 'set_block', slide: slideId, id: blockId, props: { asset: record.id } })
215 }
216 return ops
217}
218
219/**
220 * A finished clip (scenes and clips spec §2): its poster, its video
221 * naming the poster's ref, and the clip block naming the video, one
222 * batch and one undo.
223 */
224export function registerClipOps (video, poster, slideId, blockId) {
225 const ops = [...registerAssetOps(poster, '', ''), ...registerAssetOps(video, slideId, blockId)]
226 ops[1].poster = video.poster
227 if (Number.isInteger(video.duration) && video.duration > 0) ops[1].duration = video.duration
228 return ops
229}
230
231/** Dropping a registration. The bytes stay in the store, which is shared and content-addressed. */
232export function removeAssetOp (id) {
233 return { op: 'remove_asset', id }
234}
235
236/**
237 * The assets no slide in the deck refers to any more, as [{id, ...asset}]
238 * in the document's own order.
239 *
240 * Four things refer to one: an image or clip block's props.asset, a
241 * slide background of kind "image", a used video's poster, and a
242 * scene's still (scenes[*].stills[*].ref, which thumbnails, the PDF and
243 * a frame that is not ready all draw). Deleting the last block that used an
244 * asset deliberately leaves the registration behind — an undo has to be
245 * able to put the block back — so this list is how a person clears them
246 * out, and it is derived from the document every time rather than
247 * tracked.
248 */
249export function unusedAssets (deckView) {
250 const assets = (deckView && deckView.assets) || {}
251 const used = new Set()
252 for (const slide of (deckView && deckView.slides) || []) {
253 const bg = slide.background
254 if (bg && bg.kind === 'image' && bg.value) used.add(bg.value)
255 for (const block of slide.blocks || []) {
256 const id = block.props && block.props.asset
257 if (typeof id === 'string' && id) used.add(id)
258 }
259 }
260 // A clip's poster is in use while its video is; a scene's still while
261 // the scene names it.
262 const refs = new Set([...used].map(id => assets[id] && assets[id].poster).filter(Boolean))
263 for (const scene of Object.values((deckView && deckView.scenes) || {})) {
264 for (const still of Object.values((scene && scene.stills) || {})) {
265 if (still && typeof still.ref === 'string') refs.add(still.ref)
266 }
267 }
268 return Object.keys(assets)
269 .filter(id => !used.has(id) && !refs.has(assets[id].ref))
270 .map(id => ({ id, ...assets[id] }))
271}
272