oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// The deck's shape over yjs: root names and the small set of accessors
2// ops, layout and view share — core/src/schema.rs's "roots and lookups"
3// and "JSON <-> yrs values" sections. Every accessor is as tolerant as
4// the core's: a key holding the wrong kind of value reads as absent, a
5// non-map entry in an array of maps is skipped, never trapped on.
6import * as Y from 'yjs'
7import { readText } from './schema.mjs'
8
9export const ROOT_META = 'meta'
10export const ROOT_SLIDES = 'slides'
11export const ROOT_ASSETS = 'assets'
12export const ROOT_SCENES = 'scenes'
13
14/** The four root types. */
15export function roots (doc) {
16 return {
17 meta: doc.getMap(ROOT_META),
18 slides: doc.getArray(ROOT_SLIDES),
19 assets: doc.getMap(ROOT_ASSETS),
20 scenes: doc.getMap(ROOT_SCENES)
21 }
22}
23
24/** What the editor's one undo manager tracks (editor-entry.js): every root a person's edit can change. */
25export function undoScope (doc) {
26 const r = roots(doc)
27 return [r.slides, r.meta, r.assets, r.scenes]
28}
29
30export function getString (map, key) {
31 const v = map.get(key)
32 return typeof v === 'string' ? v : null
33}
34
35export function getNumber (map, key) {
36 const v = map.get(key)
37 if (typeof v === 'number') return v
38 if (typeof v === 'bigint') return Number(v)
39 return null
40}
41
42export function getMap (map, key) {
43 const v = map.get(key)
44 return v instanceof Y.Map ? v : null
45}
46
47export function getArray (map, key) {
48 const v = map.get(key)
49 return v instanceof Y.Array ? v : null
50}
51
52export function getFragment (map, key) {
53 const v = map.get(key)
54 return v instanceof Y.XmlFragment && !(v instanceof Y.XmlElement) ? v : null
55}
56
57/** The maps of an array of maps, in order, as [index, map] pairs. */
58export function mapsOf (array) {
59 const out = []
60 array.forEach((v, i) => { if (v instanceof Y.Map) out.push([i, v]) })
61 return out
62}
63
64/** The map in array whose id is id, as [index, map], or null. */
65export function findById (array, id) {
66 return mapsOf(array).find(([, m]) => getString(m, 'id') === id) ?? null
67}
68
69// --- reading order (schema.rs, "reading order"; spec §2.2, §2.3,
70// rulings 18 and 21) ---
71//
72// Both of the document's ordered arrays — the deck's slides and a
73// slide's blocks — are SETS; the reading order is the `order` key on
74// each map in them, with array index as the tie-break and as the rank
75// of an entry that carries none. Reordering by moving array entries
76// means deleting a map and inserting a copy, and a peer's edit in
77// flight against the original is lost on merge (codex 4, at both
78// levels); writing a number onto the map it already is loses nothing.
79
80/** The rank an entry sorts by: its order when a finite number, else its array index. */
81function rank (index, m) {
82 const n = getNumber(m, 'order')
83 return n !== null && Number.isFinite(n) ? n : index
84}
85
86/** The maps of an ordered array (slides, a slide's blocks) in reading order, as [arrayIndex, map] pairs. */
87export function orderedMaps (array) {
88 return mapsOf(array).sort((a, b) => (rank(a[0], a[1]) - rank(b[0], b[1])) || (a[0] - b[0]))
89}
90
91/** Whether any entry in the array carries an explicit order. */
92export function hasExplicitOrder (array) {
93 return mapsOf(array).some(([, m]) => { const n = getNumber(m, 'order'); return n !== null && Number.isFinite(n) })
94}
95
96/** Writes order 0..n-1 onto maps in the order given. */
97export function renumber (maps) {
98 maps.forEach((m, k) => m.set('order', k))
99}
100
101/**
102 * Sets key k of the plain object o to v as an own property, whatever k
103 * is. A plain `o[k] = v` with k "__proto__" sets the object's prototype
104 * instead and the key is lost; documents may hold any key.
105 */
106export function put (o, k, v) {
107 Object.defineProperty(o, k, { value: v, enumerable: true, writable: true, configurable: true })
108 return o
109}
110
111/** A plain object: a shared type rendered by content, a plain value as is. */
112function isPlainObject (v) {
113 return v !== null && typeof v === 'object' && !Array.isArray(v) && !(v instanceof Y.AbstractType)
114}
115
116/** `Some(json)` when the value is a map (shared or plain), else null. */
117export function getObject (map, key) {
118 const v = map.get(key)
119 if (v instanceof Y.Map) return mapToJSON(v)
120 if (isPlainObject(v)) return anyToJSON(v)
121 return null
122}
123
124export function anyToJSON (v) {
125 if (v === undefined) return null
126 if (typeof v === 'bigint') return Number(v)
127 if (Array.isArray(v)) return v.map(anyToJSON)
128 if (v instanceof Uint8Array) return Array.from(v)
129 if (isPlainObject(v)) {
130 const out = {}
131 for (const k of Object.keys(v)) put(out, k, anyToJSON(v[k]))
132 return out
133 }
134 return v
135}
136
137/**
138 * Any value the document holds, as JSON: a map as an object, an array
139 * as an array, a text fragment as its paragraphs, a text as its string.
140 */
141export function outToJSON (v) {
142 if (v instanceof Y.Map) return mapToJSON(v)
143 if (v instanceof Y.Array) return v.toArray().map(outToJSON)
144 if (v instanceof Y.XmlElement) return v.toString()
145 if (v instanceof Y.XmlFragment) return readText(v)
146 if (v instanceof Y.Text) return v.toString()
147 if (v instanceof Y.AbstractType) return null
148 return anyToJSON(v)
149}
150
151export function mapToJSON (map) {
152 const out = {}
153 map.forEach((v, k) => { put(out, k, outToJSON(v)) })
154 return out
155}
156
157/** A deep copy of a map ready to insert elsewhere (yrs's as_prelim). */
158export function cloneMap (map) {
159 return map.clone()
160}
161