@daasboard/doc 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +5 -0
- package/README.md +6 -2
- package/dist/DocumentAdapter.d.ts +41 -0
- package/dist/DocumentAdapter.js +34 -0
- package/dist/LoroPageDoc.d.ts +137 -0
- package/dist/LoroPageDoc.js +2662 -0
- package/dist/MemoryDocument.d.ts +42 -0
- package/dist/MemoryDocument.js +868 -0
- package/dist/connectors.d.ts +8 -0
- package/dist/connectors.js +44 -0
- package/dist/imageHashes.d.ts +3 -0
- package/dist/imageHashes.js +61 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +12 -0
- package/dist/normalize.d.ts +50 -0
- package/dist/normalize.js +203 -0
- package/dist/presence/RoomPresence.d.ts +107 -0
- package/dist/presence/RoomPresence.js +515 -0
- package/dist/schema.d.ts +728 -0
- package/dist/schema.js +891 -0
- package/dist/text.d.ts +80 -0
- package/dist/text.js +244 -0
- package/dist/textDelta.d.ts +27 -0
- package/dist/textDelta.js +688 -0
- package/dist/transport/WsTransport.d.ts +178 -0
- package/dist/transport/WsTransport.js +1314 -0
- package/dist/transport/connectRoom.d.ts +47 -0
- package/dist/transport/connectRoom.js +303 -0
- package/dist/transport/prefix.d.ts +4 -0
- package/dist/transport/prefix.js +27 -0
- package/dist/transport/slices.d.ts +9 -0
- package/dist/transport/slices.js +151 -0
- package/dist/transport/snapshot.d.ts +46 -0
- package/dist/transport/snapshot.js +248 -0
- package/dist/transport/token.d.ts +2 -0
- package/dist/transport/token.js +6 -0
- package/package.json +34 -3
|
@@ -0,0 +1,2662 @@
|
|
|
1
|
+
import { LoroDoc, LoroMap, LoroText, UndoManager } from "loro-crdt";
|
|
2
|
+
import { attachmentPoint, connectorBounds, endpointSide, exitOrientation, routeReadsSide, TEXT_STYLE_KEYS } from "@daasboard/render-core";
|
|
3
|
+
import { completeConn, endpointValue, isPoint, pointKeyOf, validAnchors } from "./connectors.js";
|
|
4
|
+
import { CONTAINER_KINDS, instanceRef, instKeyValue, NODE_KEYS, normalizeNode, rawOfObject } from "./normalize.js";
|
|
5
|
+
import { MAX_UNDO_ENTRIES, TYPING_MERGE_MS } from "./DocumentAdapter.js";
|
|
6
|
+
import { contentToFlat, loroToFlat, normalizeTextContent, readTextFromDelta, writeText, TEXT_STYLE_CONFIG } from "./text.js";
|
|
7
|
+
import { applyDeltaToString, applyLoroDelta, applyTextDelta, ensureTrailingNewline, formatChangesLoroText, rewriteDelta, sameTextContent, textAfterDelta, textDeltaIsKeystroke, validateTextDelta } from "./textDelta.js";
|
|
8
|
+
export const SCHEMA_VERSION = 2;
|
|
9
|
+
function checkSchema(v, where) {
|
|
10
|
+
if (v === SCHEMA_VERSION)
|
|
11
|
+
return;
|
|
12
|
+
const found = typeof v === "number" ? String(v) : v === undefined ? "none" : JSON.stringify(v);
|
|
13
|
+
throw new Error(`${where}: the document is schema version ${found}; this build reads version ${SCHEMA_VERSION} only (no migration exists before the first deployment)`);
|
|
14
|
+
}
|
|
15
|
+
const TEXT_CAPABLE_KINDS = new Set(["rect", "ellipse", "text", "frame", "sticky", "section", "path"]);
|
|
16
|
+
function textAffectingStyleChanged(a, b) {
|
|
17
|
+
for (const k of TEXT_STYLE_KEYS)
|
|
18
|
+
if (a[k] !== b[k])
|
|
19
|
+
return true;
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
function isFatalWasmError(err) {
|
|
23
|
+
if (err instanceof Error && err.name === "Error" && /^(editText|text delta|LoroPageDoc)/.test(err.message))
|
|
24
|
+
return false;
|
|
25
|
+
const s = String(err?.message ?? err);
|
|
26
|
+
return /unreachable|recursively acquire|recursive use of an object|RuntimeError/.test(s) || (typeof WebAssembly !== "undefined" && err instanceof WebAssembly.RuntimeError);
|
|
27
|
+
}
|
|
28
|
+
function endpointPointChanged(conn, id, was, now, lookup) {
|
|
29
|
+
const read = (ep, at, t) => {
|
|
30
|
+
const live = !t || t.hidden || t.kind === "connector" ? undefined : attachmentPoint(t, ep);
|
|
31
|
+
return { p: live ?? at, side: endpointSide(ep, live ? t : undefined) };
|
|
32
|
+
};
|
|
33
|
+
const target = (ep, state) => (!ep?.ref ? undefined : ep.ref === id ? (state === "was" ? was : now) : lookup(ep.ref));
|
|
34
|
+
const resolve = (ep, at, state) => (ep ? read(ep, at, target(ep, state)) : { p: at, side: undefined });
|
|
35
|
+
const s0 = resolve(conn.start, conn.startAt, "was");
|
|
36
|
+
const s1 = resolve(conn.start, conn.startAt, "now");
|
|
37
|
+
const e0 = resolve(conn.end, conn.endAt, "was");
|
|
38
|
+
const e1 = resolve(conn.end, conn.endAt, "now");
|
|
39
|
+
const moved = (a, b) => !a.p || !b.p || a.p[0] !== b.p[0] || a.p[1] !== b.p[1];
|
|
40
|
+
if (moved(s0, s1) || moved(e0, e1))
|
|
41
|
+
return true;
|
|
42
|
+
return routeReadsSide(conn, "start") && exitOrientation(s0.side, s0.p, e0.p) !== exitOrientation(s1.side, s1.p, e1.p);
|
|
43
|
+
}
|
|
44
|
+
const sameBox = (a, b) => a.x === b.x && a.y === b.y && a.w === b.w && a.h === b.h;
|
|
45
|
+
export { MAX_UNDO_ENTRIES };
|
|
46
|
+
const LORO_MAX_UNDO_STEPS = 100_000;
|
|
47
|
+
const CONTAINER_KEYS = new Set(["style", "conn", "anchors", "inst"]);
|
|
48
|
+
const INST_KEYS = new Set(["def", "defName"]);
|
|
49
|
+
const isPointKey = (key) => key === "startAt" || key === "endAt";
|
|
50
|
+
const endOfPoint = (key) => (key === "startAt" ? "start" : "end");
|
|
51
|
+
const objectText = new WeakMap();
|
|
52
|
+
function copyDerivedObject(object, fields) {
|
|
53
|
+
const copy = { ...object, ...fields };
|
|
54
|
+
const text = objectText.get(object);
|
|
55
|
+
if (text)
|
|
56
|
+
objectText.set(copy, text);
|
|
57
|
+
return copy;
|
|
58
|
+
}
|
|
59
|
+
function deferredContent() {
|
|
60
|
+
const state = objectText.get(this);
|
|
61
|
+
state.finish();
|
|
62
|
+
return state.slot.content;
|
|
63
|
+
}
|
|
64
|
+
function deferredVersion() {
|
|
65
|
+
const state = objectText.get(this);
|
|
66
|
+
return state.revision;
|
|
67
|
+
}
|
|
68
|
+
function readDeferredText(slot) {
|
|
69
|
+
if (slot.loaded)
|
|
70
|
+
return slot.content;
|
|
71
|
+
let delta = slot.snapshot;
|
|
72
|
+
if (!delta) {
|
|
73
|
+
const text = slot.doc.getContainerById(slot.cid);
|
|
74
|
+
try {
|
|
75
|
+
delta = text instanceof LoroText ? text.toDelta() : [];
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
text?.free();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
slot.signature ??= JSON.stringify(delta);
|
|
82
|
+
const previous = slot.previous;
|
|
83
|
+
if (previous && previous.signature === slot.signature)
|
|
84
|
+
slot.content = previous.content;
|
|
85
|
+
else
|
|
86
|
+
slot.content = normalizeTextContent(readTextFromDelta(delta));
|
|
87
|
+
slot.loaded = true;
|
|
88
|
+
delete slot.snapshot;
|
|
89
|
+
delete slot.previous;
|
|
90
|
+
return slot.content;
|
|
91
|
+
}
|
|
92
|
+
function sameObjectText(a, b) {
|
|
93
|
+
const x = objectText.get(a), y = objectText.get(b);
|
|
94
|
+
if (x && y && x.slot === y.slot)
|
|
95
|
+
return x.revision === y.revision;
|
|
96
|
+
return a.text === b.text && a.textVersion === b.textVersion;
|
|
97
|
+
}
|
|
98
|
+
function stableStringify(v) {
|
|
99
|
+
if (v === null || typeof v !== "object")
|
|
100
|
+
return JSON.stringify(v) ?? "null";
|
|
101
|
+
if (Array.isArray(v))
|
|
102
|
+
return "[" + v.map(stableStringify).join(",") + "]";
|
|
103
|
+
const o = v;
|
|
104
|
+
return "{" + Object.keys(o).filter((k) => o[k] !== undefined).sort().map((k) => JSON.stringify(k) + ":" + stableStringify(o[k])).join(",") + "}";
|
|
105
|
+
}
|
|
106
|
+
const deepEq = (a, b) => a === b || stableStringify(a) === stableStringify(b);
|
|
107
|
+
const NO_CHILDREN = Object.freeze([]);
|
|
108
|
+
function fewestMoves(before, after, movable) {
|
|
109
|
+
const moved = new Set();
|
|
110
|
+
const rankBefore = new Map();
|
|
111
|
+
before.forEach((id, i) => rankBefore.set(id, i));
|
|
112
|
+
const seqIds = [];
|
|
113
|
+
const seqRank = [];
|
|
114
|
+
for (const s of after) {
|
|
115
|
+
const rb = rankBefore.get(s);
|
|
116
|
+
if (rb !== undefined) {
|
|
117
|
+
seqIds.push(s);
|
|
118
|
+
seqRank.push(rb);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const n = seqIds.length;
|
|
122
|
+
let lo = n;
|
|
123
|
+
let hi = -1;
|
|
124
|
+
for (let k = 0; k < n; k++) {
|
|
125
|
+
if (!movable.has(seqIds[k]))
|
|
126
|
+
continue;
|
|
127
|
+
lo = Math.min(lo, k, seqRank[k]);
|
|
128
|
+
hi = Math.max(hi, k, seqRank[k]);
|
|
129
|
+
}
|
|
130
|
+
if (hi < 0)
|
|
131
|
+
return moved;
|
|
132
|
+
const w = hi - lo + 1;
|
|
133
|
+
const big = w + 1;
|
|
134
|
+
const best = new Float64Array(w + 1);
|
|
135
|
+
const bestAt = new Int32Array(w + 1).fill(-1);
|
|
136
|
+
const dp = new Float64Array(w);
|
|
137
|
+
const prev = new Int32Array(w).fill(-1);
|
|
138
|
+
let top = -1;
|
|
139
|
+
for (let k = lo; k <= hi; k++) {
|
|
140
|
+
const pos = seqRank[k] - lo;
|
|
141
|
+
let q = 0;
|
|
142
|
+
let qi = -1;
|
|
143
|
+
for (let i = pos; i > 0; i -= i & -i) {
|
|
144
|
+
if (best[i] > q) {
|
|
145
|
+
q = best[i];
|
|
146
|
+
qi = bestAt[i];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const j = k - lo;
|
|
150
|
+
dp[j] = q + (movable.has(seqIds[k]) ? 1 : big);
|
|
151
|
+
prev[j] = qi;
|
|
152
|
+
for (let i = pos + 1; i <= w; i += i & -i) {
|
|
153
|
+
if (dp[j] > best[i]) {
|
|
154
|
+
best[i] = dp[j];
|
|
155
|
+
bestAt[i] = j;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (top < 0 || dp[j] > dp[top])
|
|
159
|
+
top = j;
|
|
160
|
+
}
|
|
161
|
+
const kept = new Set();
|
|
162
|
+
for (let j = top; j >= 0; j = prev[j])
|
|
163
|
+
kept.add(seqIds[j + lo]);
|
|
164
|
+
for (let k = lo; k <= hi; k++) {
|
|
165
|
+
const id = seqIds[k];
|
|
166
|
+
if (movable.has(id) && !kept.has(id))
|
|
167
|
+
moved.add(id);
|
|
168
|
+
}
|
|
169
|
+
return moved;
|
|
170
|
+
}
|
|
171
|
+
const EMPTY_TEXT_SIG = JSON.stringify([{ insert: "\n" }]);
|
|
172
|
+
function sameMaterialization(a, b) {
|
|
173
|
+
return (a.kind === b.kind &&
|
|
174
|
+
a.parent === b.parent &&
|
|
175
|
+
a.x === b.x &&
|
|
176
|
+
a.y === b.y &&
|
|
177
|
+
a.w === b.w &&
|
|
178
|
+
a.h === b.h &&
|
|
179
|
+
a.rot === b.rot &&
|
|
180
|
+
!!a.hidden === !!b.hidden &&
|
|
181
|
+
!!a.clip === !!b.clip &&
|
|
182
|
+
!!a.locked === !!b.locked &&
|
|
183
|
+
!!a.autoWidth === !!b.autoWidth &&
|
|
184
|
+
!!a.autoHeight === !!b.autoHeight &&
|
|
185
|
+
a.name === b.name &&
|
|
186
|
+
a.extId === b.extId &&
|
|
187
|
+
sameObjectText(a, b) &&
|
|
188
|
+
deepEq(a.style, b.style) &&
|
|
189
|
+
deepEq(a.path, b.path) &&
|
|
190
|
+
deepEq(a.image, b.image) &&
|
|
191
|
+
deepEq(a.embed, b.embed) &&
|
|
192
|
+
deepEq(a.anchors, b.anchors) &&
|
|
193
|
+
deepEq(a.inst, b.inst) &&
|
|
194
|
+
deepEq(a.conn, b.conn));
|
|
195
|
+
}
|
|
196
|
+
export class LoroPageDoc {
|
|
197
|
+
carriesDescendants = true;
|
|
198
|
+
loroDoc;
|
|
199
|
+
loroTree;
|
|
200
|
+
unsubscribeRoot;
|
|
201
|
+
trashId;
|
|
202
|
+
trashRoots = new Set();
|
|
203
|
+
problemsById = new Map();
|
|
204
|
+
emptyGroups = new Set();
|
|
205
|
+
objects = new Map();
|
|
206
|
+
textSlots = new Map();
|
|
207
|
+
orderIds = [];
|
|
208
|
+
childrenOf = new Map();
|
|
209
|
+
parentOf = new Map();
|
|
210
|
+
attachedTo = new Map();
|
|
211
|
+
listeners = new Set();
|
|
212
|
+
undoStack = [];
|
|
213
|
+
redoStack = [];
|
|
214
|
+
applying = false;
|
|
215
|
+
pendingLabel = "";
|
|
216
|
+
pendingOrigin = "remote";
|
|
217
|
+
undoManager;
|
|
218
|
+
now;
|
|
219
|
+
onFatal;
|
|
220
|
+
dead = false;
|
|
221
|
+
loading = false;
|
|
222
|
+
pending = [];
|
|
223
|
+
opDepth = 0;
|
|
224
|
+
delivering = false;
|
|
225
|
+
nextSerial = 1;
|
|
226
|
+
pushSerial = 0;
|
|
227
|
+
countingPushes = false;
|
|
228
|
+
textPushes = 0;
|
|
229
|
+
pops = [];
|
|
230
|
+
lastTyping = null;
|
|
231
|
+
versions = new Map();
|
|
232
|
+
inflight = null;
|
|
233
|
+
constructor(doc, trashId, opts) {
|
|
234
|
+
this.loroDoc = doc;
|
|
235
|
+
this.loroTree = doc.getTree("tree");
|
|
236
|
+
this.trashId = trashId;
|
|
237
|
+
this.now = opts.now ?? Date.now;
|
|
238
|
+
this.onFatal = opts.onFatal;
|
|
239
|
+
this.materializeAll();
|
|
240
|
+
this.unsubscribeRoot = doc.subscribe((batch) => this.onBatch(batch));
|
|
241
|
+
this.undoManager = this.newUndoManager(doc);
|
|
242
|
+
}
|
|
243
|
+
get doc() {
|
|
244
|
+
return this.loroDoc;
|
|
245
|
+
}
|
|
246
|
+
get tree() {
|
|
247
|
+
return this.loroTree;
|
|
248
|
+
}
|
|
249
|
+
newUndoManager(doc) {
|
|
250
|
+
return new UndoManager(doc, {
|
|
251
|
+
mergeInterval: 0,
|
|
252
|
+
maxUndoSteps: LORO_MAX_UNDO_STEPS,
|
|
253
|
+
excludeOriginPrefixes: ["sys:", "diagram", "migrate:"],
|
|
254
|
+
onPush: () => {
|
|
255
|
+
if (this.countingPushes)
|
|
256
|
+
this.textPushes++;
|
|
257
|
+
return { value: this.pushSerial, cursors: [] };
|
|
258
|
+
},
|
|
259
|
+
onPop: (_isUndo, meta) => {
|
|
260
|
+
this.pops.push(typeof meta.value === "number" ? meta.value : 0);
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
static create(opts = {}) {
|
|
265
|
+
const page = LoroPageDoc.empty(opts);
|
|
266
|
+
page.init();
|
|
267
|
+
return page;
|
|
268
|
+
}
|
|
269
|
+
static empty(opts = {}) {
|
|
270
|
+
const doc = new LoroDoc();
|
|
271
|
+
if (opts.peerId !== undefined)
|
|
272
|
+
doc.setPeerId(opts.peerId);
|
|
273
|
+
doc.configTextStyle(TEXT_STYLE_CONFIG);
|
|
274
|
+
doc.getTree("tree").enableFractionalIndex(0);
|
|
275
|
+
return new LoroPageDoc(doc, "", opts);
|
|
276
|
+
}
|
|
277
|
+
isInitialized() {
|
|
278
|
+
return this.trashId !== "";
|
|
279
|
+
}
|
|
280
|
+
init() {
|
|
281
|
+
if (this.isInitialized())
|
|
282
|
+
return;
|
|
283
|
+
this.op(() => {
|
|
284
|
+
const meta = this.doc.getMap("meta");
|
|
285
|
+
meta.set("schema", SCHEMA_VERSION);
|
|
286
|
+
meta.set("kind", "page");
|
|
287
|
+
const trash = this.tree.createNode();
|
|
288
|
+
trash.data.set("kind", "trash");
|
|
289
|
+
meta.set("trash", trash.id);
|
|
290
|
+
this.trashId = trash.id;
|
|
291
|
+
this.commit("init", "local", "sys:init");
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
refreshTrashId() {
|
|
295
|
+
if (this.trashId)
|
|
296
|
+
return;
|
|
297
|
+
const t = this.doc.getMap("meta").get("trash");
|
|
298
|
+
if (t)
|
|
299
|
+
this.trashId = t;
|
|
300
|
+
}
|
|
301
|
+
static open(bytes, opts = {}) {
|
|
302
|
+
const doc = new LoroDoc();
|
|
303
|
+
if (opts.peerId !== undefined)
|
|
304
|
+
doc.setPeerId(opts.peerId);
|
|
305
|
+
doc.configTextStyle(TEXT_STYLE_CONFIG);
|
|
306
|
+
doc.import(bytes);
|
|
307
|
+
const meta = doc.getMap("meta");
|
|
308
|
+
const trash = meta.get("trash");
|
|
309
|
+
if (!trash)
|
|
310
|
+
throw new Error("LoroPageDoc.open: not a page document (no trash root)");
|
|
311
|
+
checkSchema(meta.get("schema"), "LoroPageDoc.open");
|
|
312
|
+
return new LoroPageDoc(doc, trash, opts);
|
|
313
|
+
}
|
|
314
|
+
adoptSnapshot(staged) {
|
|
315
|
+
if (this.dead)
|
|
316
|
+
throw new Error("LoroPageDoc: document is dead (a WASM trap killed it); reload");
|
|
317
|
+
if (this.applying || this.opDepth > 0 || this.loading)
|
|
318
|
+
throw new Error("LoroPageDoc.adoptSnapshot: not re-entrant (called from a transaction or a change notification)");
|
|
319
|
+
if (this.isInitialized() || this.loroDoc.oplogFrontiers().length > 0 || this.undoStack.length > 0 || this.redoStack.length > 0)
|
|
320
|
+
throw new Error("LoroPageDoc.adoptSnapshot: the page is not empty; only a page nothing was written to can take a snapshot's document");
|
|
321
|
+
const trash = staged.getMap("meta").get("trash");
|
|
322
|
+
if (typeof trash !== "string" || trash === "")
|
|
323
|
+
throw new Error("LoroPageDoc.adoptSnapshot: not a page document (no trash root)");
|
|
324
|
+
checkSchema(staged.getMap("meta").get("schema"), "LoroPageDoc.adoptSnapshot");
|
|
325
|
+
staged.setPeerId(this.loroDoc.peerIdStr);
|
|
326
|
+
staged.configTextStyle(TEXT_STYLE_CONFIG);
|
|
327
|
+
staged.getTree("tree").enableFractionalIndex(0);
|
|
328
|
+
const empty = { doc: this.loroDoc, tree: this.loroTree, unsubscribe: this.unsubscribeRoot, undoManager: this.undoManager };
|
|
329
|
+
let unsubscribe;
|
|
330
|
+
let undoManager;
|
|
331
|
+
try {
|
|
332
|
+
this.loroDoc = staged;
|
|
333
|
+
this.loroTree = staged.getTree("tree");
|
|
334
|
+
this.trashId = trash;
|
|
335
|
+
this.materializeAll();
|
|
336
|
+
unsubscribe = staged.subscribe((batch) => this.onBatch(batch));
|
|
337
|
+
undoManager = this.newUndoManager(staged);
|
|
338
|
+
}
|
|
339
|
+
catch (err) {
|
|
340
|
+
unsubscribe?.();
|
|
341
|
+
undoManager?.free();
|
|
342
|
+
this.loroDoc = empty.doc;
|
|
343
|
+
this.loroTree = empty.tree;
|
|
344
|
+
this.trashId = "";
|
|
345
|
+
this.materializeAll();
|
|
346
|
+
throw err;
|
|
347
|
+
}
|
|
348
|
+
this.unsubscribeRoot = unsubscribe;
|
|
349
|
+
this.undoManager = undoManager;
|
|
350
|
+
empty.unsubscribe();
|
|
351
|
+
empty.undoManager.free();
|
|
352
|
+
const prevOf = new Map();
|
|
353
|
+
for (const id of this.objects.keys())
|
|
354
|
+
prevOf.set(id, undefined);
|
|
355
|
+
this.pending.push({ cs: this.changeSet(prevOf, [], true), meta: { label: "snapshot", origin: "remote" } });
|
|
356
|
+
this.flush(true);
|
|
357
|
+
}
|
|
358
|
+
snapshot() {
|
|
359
|
+
return this.doc.export({ mode: "snapshot" });
|
|
360
|
+
}
|
|
361
|
+
shallowSnapshot() {
|
|
362
|
+
return this.doc.export({ mode: "shallow-snapshot", frontiers: this.doc.frontiers() });
|
|
363
|
+
}
|
|
364
|
+
version() {
|
|
365
|
+
return this.doc.version();
|
|
366
|
+
}
|
|
367
|
+
exportSince(vv) {
|
|
368
|
+
return this.doc.export({ mode: "update", from: vv });
|
|
369
|
+
}
|
|
370
|
+
exportAll() {
|
|
371
|
+
return this.doc.export({ mode: "update" });
|
|
372
|
+
}
|
|
373
|
+
import(bytes) {
|
|
374
|
+
if (this.dead)
|
|
375
|
+
throw new Error("LoroPageDoc: document is dead (a WASM trap killed it); reload");
|
|
376
|
+
const wasInitialized = this.isInitialized();
|
|
377
|
+
this.op(() => {
|
|
378
|
+
this.doc.import(bytes);
|
|
379
|
+
if (!wasInitialized && this.isInitialized()) {
|
|
380
|
+
try {
|
|
381
|
+
checkSchema(this.doc.getMap("meta").get("schema"), "LoroPageDoc.import");
|
|
382
|
+
}
|
|
383
|
+
catch (err) {
|
|
384
|
+
this.pending.length = 0;
|
|
385
|
+
this.die(err);
|
|
386
|
+
throw err;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
isDead() {
|
|
392
|
+
return this.dead;
|
|
393
|
+
}
|
|
394
|
+
die(err) {
|
|
395
|
+
if (this.dead)
|
|
396
|
+
return;
|
|
397
|
+
this.dead = true;
|
|
398
|
+
try {
|
|
399
|
+
this.onFatal?.(err);
|
|
400
|
+
}
|
|
401
|
+
catch {
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
markDead(err) {
|
|
405
|
+
this.die(err);
|
|
406
|
+
throw new Error(`LoroPageDoc: the document was killed by a WASM trap: ${String(err?.message ?? err)}`);
|
|
407
|
+
}
|
|
408
|
+
get(id) {
|
|
409
|
+
return this.objects.get(id);
|
|
410
|
+
}
|
|
411
|
+
order() {
|
|
412
|
+
return this.orderIds;
|
|
413
|
+
}
|
|
414
|
+
allObjects() {
|
|
415
|
+
return this.orderIds.map((id) => this.objects.get(id));
|
|
416
|
+
}
|
|
417
|
+
problems() {
|
|
418
|
+
return [...this.problemsById.values()];
|
|
419
|
+
}
|
|
420
|
+
state() {
|
|
421
|
+
return LoroPageDoc.stateOf(this.doc);
|
|
422
|
+
}
|
|
423
|
+
static stateOf(doc) {
|
|
424
|
+
const tree = doc.getTree("tree");
|
|
425
|
+
const ids = (nodes) => (nodes ?? []).map((n) => n.id);
|
|
426
|
+
return {
|
|
427
|
+
roots: () => ids(tree.roots()),
|
|
428
|
+
children: (id) => ids(tree.getNodeByID(id)?.children()),
|
|
429
|
+
read: (id) => {
|
|
430
|
+
const n = tree.getNodeByID(id);
|
|
431
|
+
const raw = {};
|
|
432
|
+
if (!n || n.isDeleted())
|
|
433
|
+
return raw;
|
|
434
|
+
for (const key of NODE_KEYS) {
|
|
435
|
+
const v = n.data.get(key);
|
|
436
|
+
if (v === undefined)
|
|
437
|
+
continue;
|
|
438
|
+
if (key === "text") {
|
|
439
|
+
if (v instanceof LoroText) {
|
|
440
|
+
const t = normalizeTextContent(readTextFromDelta(v.toDelta()));
|
|
441
|
+
if (t)
|
|
442
|
+
raw.text = t;
|
|
443
|
+
}
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
raw[key] = v instanceof LoroMap ? v.toJSON() : v;
|
|
447
|
+
}
|
|
448
|
+
return raw;
|
|
449
|
+
},
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
get trash() {
|
|
453
|
+
return this.trashId;
|
|
454
|
+
}
|
|
455
|
+
node(id) {
|
|
456
|
+
const n = this.tree.getNodeByID(id);
|
|
457
|
+
if (n?.isDeleted()) {
|
|
458
|
+
n.free();
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
return n;
|
|
462
|
+
}
|
|
463
|
+
isTrashRoot(id) {
|
|
464
|
+
if (id === this.trashId || this.trashRoots.has(id))
|
|
465
|
+
return true;
|
|
466
|
+
const n = this.tree.getNodeByID(id);
|
|
467
|
+
if (!n)
|
|
468
|
+
return false;
|
|
469
|
+
try {
|
|
470
|
+
if (n.isDeleted())
|
|
471
|
+
return false;
|
|
472
|
+
const data = n.data;
|
|
473
|
+
try {
|
|
474
|
+
if (data.get("kind") !== "trash")
|
|
475
|
+
return false;
|
|
476
|
+
this.trashRoots.add(id);
|
|
477
|
+
return true;
|
|
478
|
+
}
|
|
479
|
+
finally {
|
|
480
|
+
data.free();
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
finally {
|
|
484
|
+
n.free();
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
inTrash(n) {
|
|
488
|
+
for (let p = n; p; p = p.parent())
|
|
489
|
+
if (this.isTrashRoot(p.id))
|
|
490
|
+
return true;
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
inTrashJS(id) {
|
|
494
|
+
for (let p = id; p; p = this.parentOf.get(p))
|
|
495
|
+
if (this.isTrashRoot(p))
|
|
496
|
+
return true;
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
parentIdOf(n) {
|
|
500
|
+
return n.parent()?.id ?? null;
|
|
501
|
+
}
|
|
502
|
+
counters(id) {
|
|
503
|
+
let c = this.versions.get(id);
|
|
504
|
+
if (!c)
|
|
505
|
+
this.versions.set(id, (c = { version: 0, textVersion: 0 }));
|
|
506
|
+
return c;
|
|
507
|
+
}
|
|
508
|
+
bumpVersion(id) {
|
|
509
|
+
const c = this.counters(id);
|
|
510
|
+
c.version = Math.max(c.version, this.objects.get(id)?.version ?? 0) + 1;
|
|
511
|
+
return c.version;
|
|
512
|
+
}
|
|
513
|
+
materializeNode(n, parentAbs, parentId, prev) {
|
|
514
|
+
const d = n.data;
|
|
515
|
+
const id = n.id;
|
|
516
|
+
const plain = (v) => {
|
|
517
|
+
if (!(v instanceof LoroMap))
|
|
518
|
+
return v;
|
|
519
|
+
try {
|
|
520
|
+
return v.toJSON();
|
|
521
|
+
}
|
|
522
|
+
finally {
|
|
523
|
+
v.free();
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
const raw = {
|
|
527
|
+
kind: d.get("kind"),
|
|
528
|
+
xywh: d.get("xywh"),
|
|
529
|
+
rot: d.get("rot"),
|
|
530
|
+
style: plain(d.get("style")),
|
|
531
|
+
hidden: d.get("hidden"),
|
|
532
|
+
path: d.get("path"),
|
|
533
|
+
image: d.get("image"),
|
|
534
|
+
clip: d.get("clip"),
|
|
535
|
+
name: d.get("name"),
|
|
536
|
+
extId: d.get("extId"),
|
|
537
|
+
locked: d.get("locked"),
|
|
538
|
+
autoWidth: d.get("autoWidth"),
|
|
539
|
+
autoHeight: d.get("autoHeight"),
|
|
540
|
+
anchors: plain(d.get("anchors")),
|
|
541
|
+
embed: d.get("embed"),
|
|
542
|
+
inst: plain(d.get("inst")),
|
|
543
|
+
conn: plain(d.get("conn")),
|
|
544
|
+
};
|
|
545
|
+
const text = d.get("text");
|
|
546
|
+
d.free();
|
|
547
|
+
const c = this.counters(id);
|
|
548
|
+
let slot;
|
|
549
|
+
if (text instanceof LoroText) {
|
|
550
|
+
const cid = text.id;
|
|
551
|
+
try {
|
|
552
|
+
slot = this.textSlots.get(cid);
|
|
553
|
+
if (!slot || slot.stale || slot.doc !== this.doc) {
|
|
554
|
+
const hasContent = text.length > 1;
|
|
555
|
+
slot = { doc: this.doc, cid, stale: false, loaded: false, hasContent, ...(slot?.loaded ? { previous: { signature: slot.signature, content: slot.content } } : {}) };
|
|
556
|
+
if (!hasContent) {
|
|
557
|
+
slot.snapshot = text.toDelta();
|
|
558
|
+
slot.hasContent = !!readDeferredText(slot);
|
|
559
|
+
}
|
|
560
|
+
this.textSlots.set(cid, slot);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
finally {
|
|
564
|
+
text.free();
|
|
565
|
+
}
|
|
566
|
+
c.textSlot = slot;
|
|
567
|
+
}
|
|
568
|
+
const r = normalizeNode(id, raw, parentAbs, parentId);
|
|
569
|
+
if (!r.object)
|
|
570
|
+
return { id, reason: r.problem };
|
|
571
|
+
const o = r.object;
|
|
572
|
+
o.version = this.bumpVersion(id);
|
|
573
|
+
if (slot) {
|
|
574
|
+
const before = prev && objectText.get(prev);
|
|
575
|
+
const sameSlot = before?.slot === slot;
|
|
576
|
+
const styleChanged = !!prev && textAffectingStyleChanged(prev.style, o.style);
|
|
577
|
+
let sameText = sameSlot;
|
|
578
|
+
if (before && !sameSlot && !styleChanged) {
|
|
579
|
+
const previousSignature = before.slot.signature ?? (before.slot.snapshot && JSON.stringify(before.slot.snapshot));
|
|
580
|
+
if (slot.signature === undefined) {
|
|
581
|
+
if (!slot.snapshot) {
|
|
582
|
+
const current = this.doc.getContainerById(slot.cid);
|
|
583
|
+
try {
|
|
584
|
+
slot.snapshot = current instanceof LoroText ? current.toDelta() : [];
|
|
585
|
+
}
|
|
586
|
+
finally {
|
|
587
|
+
current?.free();
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
slot.signature = JSON.stringify(slot.snapshot);
|
|
591
|
+
}
|
|
592
|
+
sameText = previousSignature === slot.signature;
|
|
593
|
+
if (sameText) {
|
|
594
|
+
slot.content = readDeferredText(before.slot);
|
|
595
|
+
slot.loaded = true;
|
|
596
|
+
delete slot.snapshot;
|
|
597
|
+
delete slot.previous;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
const revision = sameText && !styleChanged ? before.revision : slot.hasContent || c.hadText ? ++c.textVersion : c.textVersion;
|
|
601
|
+
c.hadText ||= slot.hasContent;
|
|
602
|
+
const target = o;
|
|
603
|
+
let finished = false;
|
|
604
|
+
const state = {
|
|
605
|
+
slot, revision, hadText: !!before?.hadText || !!c.hadText,
|
|
606
|
+
finish() {
|
|
607
|
+
if (finished)
|
|
608
|
+
return;
|
|
609
|
+
const content = readDeferredText(this.slot);
|
|
610
|
+
if (this.slot.content && Object.getOwnPropertyDescriptor(target, "text")?.configurable !== false) {
|
|
611
|
+
Object.defineProperty(target, "text", { value: this.slot.content, writable: true, enumerable: true, configurable: true });
|
|
612
|
+
}
|
|
613
|
+
if ((content || this.hadText) && Object.getOwnPropertyDescriptor(target, "textVersion")?.configurable !== false) {
|
|
614
|
+
Object.defineProperty(target, "textVersion", { value: this.revision, writable: true, enumerable: true, configurable: true });
|
|
615
|
+
}
|
|
616
|
+
this.hadText ||= !!content;
|
|
617
|
+
c.hadText ||= this.hadText;
|
|
618
|
+
finished = true;
|
|
619
|
+
},
|
|
620
|
+
};
|
|
621
|
+
if (slot.hasContent || state.hadText) {
|
|
622
|
+
objectText.set(o, state);
|
|
623
|
+
if (slot.loaded)
|
|
624
|
+
state.finish();
|
|
625
|
+
else {
|
|
626
|
+
if (slot.hasContent)
|
|
627
|
+
Object.defineProperty(o, "text", { get: deferredContent, enumerable: true, configurable: true });
|
|
628
|
+
Object.defineProperty(o, "textVersion", { get: deferredVersion, enumerable: true, configurable: true });
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
if (o.kind === "connector" && o.conn)
|
|
633
|
+
Object.assign(o, connectorBounds(o.conn, this.sceneView()));
|
|
634
|
+
return o;
|
|
635
|
+
}
|
|
636
|
+
materializeAll() {
|
|
637
|
+
const prev = this.objects;
|
|
638
|
+
const previousParents = this.parentOf;
|
|
639
|
+
this.objects = new Map();
|
|
640
|
+
this.orderIds = [];
|
|
641
|
+
this.childrenOf = new Map();
|
|
642
|
+
this.parentOf = new Map();
|
|
643
|
+
this.attachedTo = new Map();
|
|
644
|
+
this.trashRoots = new Set();
|
|
645
|
+
this.problemsById = new Map();
|
|
646
|
+
this.emptyGroups = new Set();
|
|
647
|
+
const roots = [];
|
|
648
|
+
this.childrenOf.set(null, roots);
|
|
649
|
+
const walk = (n, parentAbs, parentId, siblings) => {
|
|
650
|
+
const id = n.id;
|
|
651
|
+
siblings.push(id);
|
|
652
|
+
this.parentOf.set(id, parentId);
|
|
653
|
+
const nodeData = n.data;
|
|
654
|
+
const trash = id === this.trashId || nodeData.get("kind") === "trash";
|
|
655
|
+
nodeData.free();
|
|
656
|
+
if (trash) {
|
|
657
|
+
this.trashRoots.add(id);
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
const r = this.materializeNode(n, parentAbs, parentId, prev.get(id));
|
|
661
|
+
if ("reason" in r) {
|
|
662
|
+
this.problemsById.set(id, r);
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
const o = r;
|
|
666
|
+
this.objects.set(id, o);
|
|
667
|
+
this.orderIds.push(id);
|
|
668
|
+
this.trackAttachment(o);
|
|
669
|
+
const kids = n.children() ?? [];
|
|
670
|
+
const children = kids.length ? [] : NO_CHILDREN;
|
|
671
|
+
this.childrenOf.set(id, children);
|
|
672
|
+
const childAbs = CONTAINER_KINDS.has(o.kind) ? { x: o.x, y: o.y } : parentAbs;
|
|
673
|
+
let live = 0;
|
|
674
|
+
for (const c of kids) {
|
|
675
|
+
try {
|
|
676
|
+
if (walk(c, childAbs, id, children))
|
|
677
|
+
live++;
|
|
678
|
+
}
|
|
679
|
+
finally {
|
|
680
|
+
c.free();
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
if (o.kind === "group" && live === 0 && !o.hidden) {
|
|
684
|
+
o.hidden = true;
|
|
685
|
+
this.emptyGroups.add(id);
|
|
686
|
+
}
|
|
687
|
+
return true;
|
|
688
|
+
};
|
|
689
|
+
for (const r of this.tree.roots()) {
|
|
690
|
+
try {
|
|
691
|
+
walk(r, { x: 0, y: 0 }, null, roots);
|
|
692
|
+
}
|
|
693
|
+
finally {
|
|
694
|
+
r.free();
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
for (const id of previousParents.keys()) {
|
|
698
|
+
if (this.parentOf.has(id))
|
|
699
|
+
continue;
|
|
700
|
+
const counters = this.versions.get(id);
|
|
701
|
+
const slot = counters?.textSlot;
|
|
702
|
+
if (!slot || slot.doc !== this.doc)
|
|
703
|
+
continue;
|
|
704
|
+
if (!slot.loaded && !slot.snapshot) {
|
|
705
|
+
const text = this.doc.getContainerById(slot.cid);
|
|
706
|
+
try {
|
|
707
|
+
slot.snapshot = text instanceof LoroText ? text.toDelta() : [];
|
|
708
|
+
}
|
|
709
|
+
finally {
|
|
710
|
+
text?.free();
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
slot.stale = true;
|
|
714
|
+
if (this.textSlots.get(slot.cid) === slot)
|
|
715
|
+
this.textSlots.delete(slot.cid);
|
|
716
|
+
delete counters.textSlot;
|
|
717
|
+
}
|
|
718
|
+
for (const o of this.objects.values())
|
|
719
|
+
if (o.kind === "connector" && o.conn)
|
|
720
|
+
Object.assign(o, connectorBounds(o.conn, this.sceneView()));
|
|
721
|
+
}
|
|
722
|
+
sceneView() {
|
|
723
|
+
return { objects: this.objects, order: this.orderIds };
|
|
724
|
+
}
|
|
725
|
+
trackAttachment(o) {
|
|
726
|
+
if (o.kind !== "connector" || !o.conn)
|
|
727
|
+
return;
|
|
728
|
+
for (const e of [o.conn.start, o.conn.end]) {
|
|
729
|
+
if (!e?.ref)
|
|
730
|
+
continue;
|
|
731
|
+
let set = this.attachedTo.get(e.ref);
|
|
732
|
+
if (!set)
|
|
733
|
+
this.attachedTo.set(e.ref, (set = new Set()));
|
|
734
|
+
set.add(o.id);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
untrackAttachment(o) {
|
|
738
|
+
if (o.kind !== "connector" || !o.conn)
|
|
739
|
+
return;
|
|
740
|
+
for (const e of [o.conn.start, o.conn.end])
|
|
741
|
+
if (e?.ref)
|
|
742
|
+
this.attachedTo.get(e.ref)?.delete(o.id);
|
|
743
|
+
}
|
|
744
|
+
absOriginOfParent(n) {
|
|
745
|
+
const p = n.parent();
|
|
746
|
+
if (!p || this.isTrashRoot(p.id))
|
|
747
|
+
return { x: 0, y: 0 };
|
|
748
|
+
const kind = p.data.get("kind");
|
|
749
|
+
const origin = this.absOriginOfParent(p);
|
|
750
|
+
if (!CONTAINER_KINDS.has(kind ?? ""))
|
|
751
|
+
return origin;
|
|
752
|
+
const xywh = p.data.get("xywh") ?? [0, 0, 0, 0];
|
|
753
|
+
return { x: origin.x + (xywh[0] ?? 0), y: origin.y + (xywh[1] ?? 0) };
|
|
754
|
+
}
|
|
755
|
+
mutationSiblings() {
|
|
756
|
+
const counts = new Map();
|
|
757
|
+
const lists = new Map();
|
|
758
|
+
const trash = new Map();
|
|
759
|
+
const list = (target) => {
|
|
760
|
+
const parent = target ? target.id : null;
|
|
761
|
+
let ids = lists.get(parent);
|
|
762
|
+
if (ids)
|
|
763
|
+
return ids;
|
|
764
|
+
ids = [];
|
|
765
|
+
const nodes = target ? target.children() ?? [] : this.tree.roots();
|
|
766
|
+
for (const n of nodes) {
|
|
767
|
+
try {
|
|
768
|
+
ids.push(n.id);
|
|
769
|
+
}
|
|
770
|
+
finally {
|
|
771
|
+
n.free();
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
lists.set(parent, ids);
|
|
775
|
+
counts.set(parent, ids.length);
|
|
776
|
+
return ids;
|
|
777
|
+
};
|
|
778
|
+
const count = (target) => {
|
|
779
|
+
const parent = target ? target.id : null;
|
|
780
|
+
const known = counts.get(parent);
|
|
781
|
+
if (known !== undefined)
|
|
782
|
+
return known;
|
|
783
|
+
const nodes = target ? target.children() ?? [] : this.tree.roots();
|
|
784
|
+
const length = nodes.length;
|
|
785
|
+
for (const n of nodes)
|
|
786
|
+
n.free();
|
|
787
|
+
counts.set(parent, length);
|
|
788
|
+
return length;
|
|
789
|
+
};
|
|
790
|
+
const isTrash = (id) => {
|
|
791
|
+
if (id === this.trashId)
|
|
792
|
+
return true;
|
|
793
|
+
const known = trash.get(id);
|
|
794
|
+
if (known !== undefined)
|
|
795
|
+
return known;
|
|
796
|
+
const n = this.tree.getNodeByID(id);
|
|
797
|
+
let result = false;
|
|
798
|
+
if (n) {
|
|
799
|
+
try {
|
|
800
|
+
const data = n.data;
|
|
801
|
+
try {
|
|
802
|
+
result = data.get("kind") === "trash";
|
|
803
|
+
}
|
|
804
|
+
finally {
|
|
805
|
+
data.free();
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
finally {
|
|
809
|
+
n.free();
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
trash.set(id, result);
|
|
813
|
+
return result;
|
|
814
|
+
};
|
|
815
|
+
return {
|
|
816
|
+
count,
|
|
817
|
+
rawIndex: (target, index) => {
|
|
818
|
+
if (index === undefined || target)
|
|
819
|
+
return index;
|
|
820
|
+
const roots = list(undefined);
|
|
821
|
+
let visible = 0;
|
|
822
|
+
for (let raw = 0; raw < roots.length; raw++) {
|
|
823
|
+
if (isTrash(roots[raw]))
|
|
824
|
+
continue;
|
|
825
|
+
if (visible++ === index)
|
|
826
|
+
return raw;
|
|
827
|
+
}
|
|
828
|
+
return undefined;
|
|
829
|
+
},
|
|
830
|
+
moved: (id, from, to, index) => {
|
|
831
|
+
if (from !== to) {
|
|
832
|
+
const fromCount = counts.get(from);
|
|
833
|
+
if (fromCount !== undefined)
|
|
834
|
+
counts.set(from, fromCount - 1);
|
|
835
|
+
const toCount = counts.get(to);
|
|
836
|
+
if (toCount !== undefined)
|
|
837
|
+
counts.set(to, toCount + 1);
|
|
838
|
+
}
|
|
839
|
+
const before = lists.get(from);
|
|
840
|
+
if (before) {
|
|
841
|
+
const old = before.indexOf(id);
|
|
842
|
+
if (old >= 0)
|
|
843
|
+
before.splice(old, 1);
|
|
844
|
+
}
|
|
845
|
+
const after = lists.get(to);
|
|
846
|
+
if (after)
|
|
847
|
+
after.splice(index, 0, id);
|
|
848
|
+
},
|
|
849
|
+
created: (id, parent) => {
|
|
850
|
+
const known = counts.get(parent);
|
|
851
|
+
if (known !== undefined)
|
|
852
|
+
counts.set(parent, known + 1);
|
|
853
|
+
lists.get(parent)?.push(id);
|
|
854
|
+
},
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
originFromObjects(parentId) {
|
|
858
|
+
for (let p = parentId; p;) {
|
|
859
|
+
const po = this.objects.get(p);
|
|
860
|
+
if (!po)
|
|
861
|
+
break;
|
|
862
|
+
if (CONTAINER_KINDS.has(po.kind))
|
|
863
|
+
return { x: po.x, y: po.y };
|
|
864
|
+
p = po.parent;
|
|
865
|
+
}
|
|
866
|
+
return { x: 0, y: 0 };
|
|
867
|
+
}
|
|
868
|
+
applyTreeItem(d, dirty) {
|
|
869
|
+
const target = String(d.target);
|
|
870
|
+
const insert = (parent, index) => {
|
|
871
|
+
let arr = this.childrenOf.get(parent);
|
|
872
|
+
if (!arr)
|
|
873
|
+
return;
|
|
874
|
+
if (arr === NO_CHILDREN)
|
|
875
|
+
this.childrenOf.set(parent, (arr = []));
|
|
876
|
+
arr.splice(Math.max(0, Math.min(index, arr.length)), 0, target);
|
|
877
|
+
};
|
|
878
|
+
const remove = (parent, index) => {
|
|
879
|
+
const arr = this.childrenOf.get(parent);
|
|
880
|
+
if (!arr)
|
|
881
|
+
return;
|
|
882
|
+
if (arr[index] === target)
|
|
883
|
+
arr.splice(index, 1);
|
|
884
|
+
else {
|
|
885
|
+
const i = arr.indexOf(target);
|
|
886
|
+
if (i >= 0)
|
|
887
|
+
arr.splice(i, 1);
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
if (d.action === "create") {
|
|
891
|
+
const parent = d.parent ? String(d.parent) : null;
|
|
892
|
+
this.parentOf.set(target, parent);
|
|
893
|
+
if (!this.childrenOf.has(target) && parent !== this.trashId)
|
|
894
|
+
this.childrenOf.set(target, NO_CHILDREN);
|
|
895
|
+
insert(parent, d.index);
|
|
896
|
+
}
|
|
897
|
+
else if (d.action === "move") {
|
|
898
|
+
const parent = d.parent ? String(d.parent) : null;
|
|
899
|
+
remove(d.oldParent ? String(d.oldParent) : null, d.oldIndex);
|
|
900
|
+
this.parentOf.set(target, parent);
|
|
901
|
+
insert(parent, d.index);
|
|
902
|
+
}
|
|
903
|
+
else {
|
|
904
|
+
remove(d.oldParent ? String(d.oldParent) : null, d.oldIndex);
|
|
905
|
+
this.parentOf.delete(target);
|
|
906
|
+
}
|
|
907
|
+
dirty.add(target);
|
|
908
|
+
}
|
|
909
|
+
ensureChildrenKnown(id, n) {
|
|
910
|
+
let arr = this.childrenOf.get(id);
|
|
911
|
+
if (arr)
|
|
912
|
+
return arr;
|
|
913
|
+
const kids = n.children() ?? [];
|
|
914
|
+
arr = kids.length ? [] : NO_CHILDREN;
|
|
915
|
+
for (const c of kids) {
|
|
916
|
+
try {
|
|
917
|
+
const cid = c.id;
|
|
918
|
+
arr.push(cid);
|
|
919
|
+
this.parentOf.set(cid, id);
|
|
920
|
+
}
|
|
921
|
+
finally {
|
|
922
|
+
c.free();
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
this.childrenOf.set(id, arr);
|
|
926
|
+
return arr;
|
|
927
|
+
}
|
|
928
|
+
applyTreeItems(items, dirty) {
|
|
929
|
+
const named = new Set();
|
|
930
|
+
const parentBefore = new Map();
|
|
931
|
+
const childrenBefore = new Map();
|
|
932
|
+
for (const d of items) {
|
|
933
|
+
const t = String(d.target);
|
|
934
|
+
named.add(t);
|
|
935
|
+
if (parentBefore.has(t) || !this.parentOf.has(t))
|
|
936
|
+
continue;
|
|
937
|
+
const p = this.parentOf.get(t);
|
|
938
|
+
parentBefore.set(t, p);
|
|
939
|
+
if (!childrenBefore.has(p))
|
|
940
|
+
childrenBefore.set(p, [...(this.childrenOf.get(p) ?? [])]);
|
|
941
|
+
}
|
|
942
|
+
for (const d of items)
|
|
943
|
+
this.applyTreeItem(d, dirty);
|
|
944
|
+
const moved = new Set();
|
|
945
|
+
const byParent = new Map();
|
|
946
|
+
for (const [t, p] of parentBefore) {
|
|
947
|
+
const after = this.parentOf.get(t);
|
|
948
|
+
if (after === undefined)
|
|
949
|
+
continue;
|
|
950
|
+
if (after !== p) {
|
|
951
|
+
moved.add(t);
|
|
952
|
+
continue;
|
|
953
|
+
}
|
|
954
|
+
let set = byParent.get(p);
|
|
955
|
+
if (!set)
|
|
956
|
+
byParent.set(p, (set = new Set()));
|
|
957
|
+
set.add(t);
|
|
958
|
+
}
|
|
959
|
+
for (const [p, targets] of byParent) {
|
|
960
|
+
const before = childrenBefore.get(p) ?? [];
|
|
961
|
+
const after = this.childrenOf.get(p) ?? [];
|
|
962
|
+
const stable = (s) => !named.has(s) || (parentBefore.get(s) === p && this.parentOf.get(s) === p);
|
|
963
|
+
if (targets.size === 1) {
|
|
964
|
+
const [t] = targets;
|
|
965
|
+
const neighbours = (arr) => {
|
|
966
|
+
const i = arr.indexOf(t);
|
|
967
|
+
let a = i - 1;
|
|
968
|
+
while (a >= 0 && !stable(arr[a]))
|
|
969
|
+
a--;
|
|
970
|
+
let b = i + 1;
|
|
971
|
+
while (b < arr.length && !stable(arr[b]))
|
|
972
|
+
b++;
|
|
973
|
+
return [arr[a] ?? null, arr[b] ?? null];
|
|
974
|
+
};
|
|
975
|
+
const [pb, sb] = neighbours(before);
|
|
976
|
+
const [pa, sa] = neighbours(after);
|
|
977
|
+
if (pb !== pa || sb !== sa)
|
|
978
|
+
moved.add(t);
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
const stableBefore = before.filter(stable);
|
|
982
|
+
const inBefore = new Set(stableBefore);
|
|
983
|
+
for (const t of fewestMoves(stableBefore, after.filter((s) => inBefore.has(s)), targets))
|
|
984
|
+
moved.add(t);
|
|
985
|
+
}
|
|
986
|
+
return moved;
|
|
987
|
+
}
|
|
988
|
+
metaFor(batch) {
|
|
989
|
+
const isImport = batch.by === "import";
|
|
990
|
+
const origin = isImport ? "remote" : this.pendingOrigin;
|
|
991
|
+
const label = isImport ? "remote" : this.applying ? this.pendingLabel : "remote";
|
|
992
|
+
return { label, origin };
|
|
993
|
+
}
|
|
994
|
+
freezeChangedText(batch) {
|
|
995
|
+
const slots = new Set();
|
|
996
|
+
const obsolete = new Map();
|
|
997
|
+
const add = (slot) => {
|
|
998
|
+
if (slot && !slot.stale && slot.doc === this.doc)
|
|
999
|
+
slots.add(slot);
|
|
1000
|
+
};
|
|
1001
|
+
const deletedRoots = new Set();
|
|
1002
|
+
for (const ev of batch.events) {
|
|
1003
|
+
if (ev.diff.type === "text")
|
|
1004
|
+
add(this.textSlots.get(ev.target));
|
|
1005
|
+
const path = ev.path;
|
|
1006
|
+
if (path[0] !== "tree")
|
|
1007
|
+
continue;
|
|
1008
|
+
if (ev.diff.type === "map" && path.length === 2 && Object.hasOwn(ev.diff.updated, "text")) {
|
|
1009
|
+
const id = String(path[1]);
|
|
1010
|
+
const slot = this.versions.get(id)?.textSlot;
|
|
1011
|
+
add(slot);
|
|
1012
|
+
const next = ev.diff.updated.text;
|
|
1013
|
+
if (slot && (!(next instanceof LoroText) || next.id !== slot.cid))
|
|
1014
|
+
obsolete.set(id, slot);
|
|
1015
|
+
}
|
|
1016
|
+
else if (ev.diff.type === "tree") {
|
|
1017
|
+
for (const item of ev.diff.diff)
|
|
1018
|
+
if (item.action === "delete")
|
|
1019
|
+
deletedRoots.add(String(item.target));
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
const seen = new Set();
|
|
1023
|
+
const addSubtree = (id) => {
|
|
1024
|
+
if (seen.has(id))
|
|
1025
|
+
return;
|
|
1026
|
+
seen.add(id);
|
|
1027
|
+
const slot = this.versions.get(id)?.textSlot;
|
|
1028
|
+
add(slot);
|
|
1029
|
+
if (slot)
|
|
1030
|
+
obsolete.set(id, slot);
|
|
1031
|
+
for (const child of this.childrenOf.get(id) ?? [])
|
|
1032
|
+
addSubtree(child);
|
|
1033
|
+
};
|
|
1034
|
+
for (const id of deletedRoots) {
|
|
1035
|
+
const node = this.tree.getNodeByID(id);
|
|
1036
|
+
try {
|
|
1037
|
+
if (!node || node.isDeleted())
|
|
1038
|
+
addSubtree(id);
|
|
1039
|
+
}
|
|
1040
|
+
finally {
|
|
1041
|
+
node?.free();
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
const gone = new Set(obsolete.values());
|
|
1045
|
+
let backwards;
|
|
1046
|
+
const recreated = new Set();
|
|
1047
|
+
for (const slot of slots) {
|
|
1048
|
+
if (!slot.loaded && !slot.snapshot) {
|
|
1049
|
+
if (!backwards) {
|
|
1050
|
+
backwards = new Map(this.doc.diff(batch.to, batch.from, true));
|
|
1051
|
+
for (const diff of backwards.values())
|
|
1052
|
+
if (diff.type === "map") {
|
|
1053
|
+
for (const value of Object.values(diff.updated))
|
|
1054
|
+
if (typeof value === "string" && value.startsWith("🦜:cid:")) {
|
|
1055
|
+
recreated.add(value.slice("🦜:".length));
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
const diff = backwards.get(slot.cid);
|
|
1060
|
+
const text = this.doc.getContainerById(slot.cid);
|
|
1061
|
+
let delta = [];
|
|
1062
|
+
try {
|
|
1063
|
+
if (text instanceof LoroText && !gone.has(slot) && !recreated.has(slot.cid))
|
|
1064
|
+
delta = text.toDelta();
|
|
1065
|
+
}
|
|
1066
|
+
finally {
|
|
1067
|
+
text?.free();
|
|
1068
|
+
}
|
|
1069
|
+
if (diff?.type === "text") {
|
|
1070
|
+
const scratch = new LoroDoc();
|
|
1071
|
+
scratch.configTextStyle(TEXT_STYLE_CONFIG);
|
|
1072
|
+
const copy = scratch.getText("text");
|
|
1073
|
+
try {
|
|
1074
|
+
copy.applyDelta(delta);
|
|
1075
|
+
copy.applyDelta(diff.diff);
|
|
1076
|
+
delta = copy.toDelta();
|
|
1077
|
+
}
|
|
1078
|
+
finally {
|
|
1079
|
+
copy.free();
|
|
1080
|
+
scratch.free();
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
slot.snapshot = delta;
|
|
1084
|
+
}
|
|
1085
|
+
slot.stale = true;
|
|
1086
|
+
}
|
|
1087
|
+
for (const [id, slot] of obsolete) {
|
|
1088
|
+
if (this.textSlots.get(slot.cid) === slot)
|
|
1089
|
+
this.textSlots.delete(slot.cid);
|
|
1090
|
+
const counters = this.versions.get(id);
|
|
1091
|
+
if (counters?.textSlot === slot)
|
|
1092
|
+
delete counters.textSlot;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
onBatch(batch) {
|
|
1096
|
+
try {
|
|
1097
|
+
this.freezeChangedText(batch);
|
|
1098
|
+
if (this.loading)
|
|
1099
|
+
return;
|
|
1100
|
+
this.applyBatch(batch);
|
|
1101
|
+
}
|
|
1102
|
+
catch (err) {
|
|
1103
|
+
console.error("LoroPageDoc: incremental materialization failed; resynchronizing from a full walk", err);
|
|
1104
|
+
try {
|
|
1105
|
+
this.resync(batch);
|
|
1106
|
+
}
|
|
1107
|
+
catch (err2) {
|
|
1108
|
+
console.error("LoroPageDoc: resynchronization failed; the document is dead", err2);
|
|
1109
|
+
this.die(err2);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
applyBatch(batch) {
|
|
1114
|
+
this.inflight = null;
|
|
1115
|
+
const hadTrash = this.trashId !== "";
|
|
1116
|
+
this.refreshTrashId();
|
|
1117
|
+
const dirty = new Set();
|
|
1118
|
+
const treeItems = [];
|
|
1119
|
+
let textDeltas;
|
|
1120
|
+
const state = { prevOf: new Map(), beforeOrder: this.orderIds, structural: !hadTrash && this.trashId !== "" };
|
|
1121
|
+
for (const ev of batch.events) {
|
|
1122
|
+
const path = ev.path;
|
|
1123
|
+
if (path[0] !== "tree")
|
|
1124
|
+
continue;
|
|
1125
|
+
if (ev.diff.type === "tree") {
|
|
1126
|
+
state.structural = true;
|
|
1127
|
+
for (const d of ev.diff.diff)
|
|
1128
|
+
treeItems.push(d);
|
|
1129
|
+
continue;
|
|
1130
|
+
}
|
|
1131
|
+
if (path.length < 2)
|
|
1132
|
+
continue;
|
|
1133
|
+
const id = String(path[1]);
|
|
1134
|
+
dirty.add(id);
|
|
1135
|
+
if (ev.diff.type === "text" && path.length === 3 && path[2] === "text") {
|
|
1136
|
+
textDeltas ??= new Map();
|
|
1137
|
+
let list = textDeltas.get(id);
|
|
1138
|
+
if (!list)
|
|
1139
|
+
textDeltas.set(id, (list = []));
|
|
1140
|
+
list.push(ev.diff.diff);
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
this.inflight = state;
|
|
1144
|
+
const moved = treeItems.length ? this.applyTreeItems(treeItems, dirty) : new Set();
|
|
1145
|
+
if (dirty.size === 0 && !state.structural) {
|
|
1146
|
+
this.inflight = null;
|
|
1147
|
+
return;
|
|
1148
|
+
}
|
|
1149
|
+
this.materializeDirty(dirty, moved, state);
|
|
1150
|
+
if (state.structural)
|
|
1151
|
+
this.orderIds = this.spliceOrder(state, moved);
|
|
1152
|
+
const cs = this.changeSet(state.prevOf, state.beforeOrder, state.structural);
|
|
1153
|
+
this.inflight = null;
|
|
1154
|
+
const meta = this.metaFor(batch);
|
|
1155
|
+
if (textDeltas)
|
|
1156
|
+
meta.textDeltas = textDeltas;
|
|
1157
|
+
this.pending.push({ cs, meta });
|
|
1158
|
+
if (this.opDepth === 0)
|
|
1159
|
+
this.flush();
|
|
1160
|
+
}
|
|
1161
|
+
resync(batch) {
|
|
1162
|
+
const inflight = this.inflight;
|
|
1163
|
+
this.inflight = null;
|
|
1164
|
+
const before = new Map(this.objects);
|
|
1165
|
+
if (inflight) {
|
|
1166
|
+
for (const [id, was] of inflight.prevOf) {
|
|
1167
|
+
if (was)
|
|
1168
|
+
before.set(id, was);
|
|
1169
|
+
else
|
|
1170
|
+
before.delete(id);
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
const beforeOrder = inflight?.beforeOrder ?? this.orderIds;
|
|
1174
|
+
this.refreshTrashId();
|
|
1175
|
+
this.materializeAll();
|
|
1176
|
+
const named = new Set();
|
|
1177
|
+
for (const ev of batch.events)
|
|
1178
|
+
if (ev.diff.type === "tree")
|
|
1179
|
+
for (const d of ev.diff.diff)
|
|
1180
|
+
named.add(String(d.target));
|
|
1181
|
+
const siblingsBefore = new Map();
|
|
1182
|
+
const siblingsAfter = new Map();
|
|
1183
|
+
const stayed = (id) => {
|
|
1184
|
+
const was = before.get(id);
|
|
1185
|
+
const now = this.objects.get(id);
|
|
1186
|
+
return was && now && was.parent === now.parent ? was.parent : undefined;
|
|
1187
|
+
};
|
|
1188
|
+
const group = (into, order) => {
|
|
1189
|
+
for (const id of order) {
|
|
1190
|
+
const p = stayed(id);
|
|
1191
|
+
if (p === undefined)
|
|
1192
|
+
continue;
|
|
1193
|
+
let arr = into.get(p);
|
|
1194
|
+
if (!arr)
|
|
1195
|
+
into.set(p, (arr = []));
|
|
1196
|
+
arr.push(id);
|
|
1197
|
+
}
|
|
1198
|
+
};
|
|
1199
|
+
group(siblingsBefore, beforeOrder);
|
|
1200
|
+
group(siblingsAfter, this.orderIds);
|
|
1201
|
+
const moved = new Set();
|
|
1202
|
+
for (const [p, seq] of siblingsBefore)
|
|
1203
|
+
for (const id of fewestMoves(seq, siblingsAfter.get(p) ?? [], named))
|
|
1204
|
+
moved.add(id);
|
|
1205
|
+
const prevOf = new Map();
|
|
1206
|
+
for (const [id, was] of before) {
|
|
1207
|
+
const now = this.objects.get(id);
|
|
1208
|
+
if (now && !moved.has(id) && sameMaterialization(was, now) && sameBox(was, now))
|
|
1209
|
+
this.objects.set(id, was);
|
|
1210
|
+
prevOf.set(id, was);
|
|
1211
|
+
}
|
|
1212
|
+
for (const id of this.objects.keys())
|
|
1213
|
+
if (!prevOf.has(id))
|
|
1214
|
+
prevOf.set(id, undefined);
|
|
1215
|
+
const cs = this.changeSet(prevOf, beforeOrder, true);
|
|
1216
|
+
this.pending.push({ cs, meta: this.metaFor(batch) });
|
|
1217
|
+
if (this.opDepth === 0)
|
|
1218
|
+
this.flush();
|
|
1219
|
+
}
|
|
1220
|
+
materializeDirty(dirty, moved, state) {
|
|
1221
|
+
const prevOf = state.prevOf;
|
|
1222
|
+
const done = new Set();
|
|
1223
|
+
const remember = (id) => {
|
|
1224
|
+
if (!prevOf.has(id))
|
|
1225
|
+
prevOf.set(id, this.objects.get(id));
|
|
1226
|
+
};
|
|
1227
|
+
const removeSubtree = (id) => {
|
|
1228
|
+
const o = this.objects.get(id);
|
|
1229
|
+
if (o) {
|
|
1230
|
+
remember(id);
|
|
1231
|
+
this.objects.delete(id);
|
|
1232
|
+
this.untrackAttachment(o);
|
|
1233
|
+
const c = this.counters(id);
|
|
1234
|
+
c.version = Math.max(c.version, o.version);
|
|
1235
|
+
}
|
|
1236
|
+
this.problemsById.delete(id);
|
|
1237
|
+
this.emptyGroups.delete(id);
|
|
1238
|
+
for (const c of this.childrenOf.get(id) ?? [])
|
|
1239
|
+
removeSubtree(c);
|
|
1240
|
+
};
|
|
1241
|
+
const visit = (id, viaCascade = false) => {
|
|
1242
|
+
if (done.has(id))
|
|
1243
|
+
return;
|
|
1244
|
+
done.add(id);
|
|
1245
|
+
if (this.isTrashRoot(id)) {
|
|
1246
|
+
this.problemsById.delete(id);
|
|
1247
|
+
if (this.objects.has(id)) {
|
|
1248
|
+
removeSubtree(id);
|
|
1249
|
+
this.childrenOf.delete(id);
|
|
1250
|
+
state.structural = true;
|
|
1251
|
+
}
|
|
1252
|
+
return;
|
|
1253
|
+
}
|
|
1254
|
+
const n = this.node(id);
|
|
1255
|
+
if (!n) {
|
|
1256
|
+
removeSubtree(id);
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
try {
|
|
1260
|
+
if (!this.parentOf.has(id)) {
|
|
1261
|
+
if (this.inTrash(n)) {
|
|
1262
|
+
removeSubtree(id);
|
|
1263
|
+
return;
|
|
1264
|
+
}
|
|
1265
|
+
this.parentOf.set(id, this.parentIdOf(n));
|
|
1266
|
+
}
|
|
1267
|
+
if (this.inTrashJS(id)) {
|
|
1268
|
+
removeSubtree(id);
|
|
1269
|
+
return;
|
|
1270
|
+
}
|
|
1271
|
+
const parentId = this.parentOf.get(id) ?? null;
|
|
1272
|
+
if (parentId !== null && dirty.has(parentId))
|
|
1273
|
+
visit(parentId);
|
|
1274
|
+
if (parentId !== null && !this.objects.has(parentId)) {
|
|
1275
|
+
removeSubtree(id);
|
|
1276
|
+
done.delete(id);
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
const prev = this.objects.get(id);
|
|
1280
|
+
remember(id);
|
|
1281
|
+
const r = this.materializeNode(n, this.originFromObjects(parentId), parentId, prev);
|
|
1282
|
+
if ("reason" in r) {
|
|
1283
|
+
if (prev)
|
|
1284
|
+
state.structural = true;
|
|
1285
|
+
removeSubtree(id);
|
|
1286
|
+
this.problemsById.set(id, r);
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
1289
|
+
const fresh = r;
|
|
1290
|
+
if (this.problemsById.delete(id))
|
|
1291
|
+
state.structural = true;
|
|
1292
|
+
let base = prev;
|
|
1293
|
+
if (prev !== undefined && this.emptyGroups.has(id)) {
|
|
1294
|
+
const { hidden: _derived, ...stored } = prev;
|
|
1295
|
+
void _derived;
|
|
1296
|
+
base = stored;
|
|
1297
|
+
}
|
|
1298
|
+
const same = base !== undefined && !moved.has(id) && sameMaterialization(base, fresh);
|
|
1299
|
+
const o = same ? prev : fresh;
|
|
1300
|
+
if (o !== prev) {
|
|
1301
|
+
this.emptyGroups.delete(id);
|
|
1302
|
+
this.objects.set(id, o);
|
|
1303
|
+
if (prev)
|
|
1304
|
+
this.untrackAttachment(prev);
|
|
1305
|
+
this.trackAttachment(o);
|
|
1306
|
+
}
|
|
1307
|
+
const isContainer = CONTAINER_KINDS.has(o.kind);
|
|
1308
|
+
const wasContainer = prev !== undefined && CONTAINER_KINDS.has(prev.kind);
|
|
1309
|
+
const cascade = !prev ||
|
|
1310
|
+
((isContainer || wasContainer) && (prev.x !== o.x || prev.y !== o.y || prev.kind !== o.kind)) ||
|
|
1311
|
+
(!isContainer && (viaCascade || prev.parent !== o.parent));
|
|
1312
|
+
if (cascade)
|
|
1313
|
+
for (const c of this.ensureChildrenKnown(id, n)) {
|
|
1314
|
+
dirty.add(c);
|
|
1315
|
+
visit(c, true);
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
finally {
|
|
1319
|
+
n.free();
|
|
1320
|
+
}
|
|
1321
|
+
};
|
|
1322
|
+
const depthOf = (id) => {
|
|
1323
|
+
let d = 0;
|
|
1324
|
+
for (let p = this.parentOf.get(id); p; p = this.parentOf.get(p))
|
|
1325
|
+
d++;
|
|
1326
|
+
return d;
|
|
1327
|
+
};
|
|
1328
|
+
for (const id of [...dirty].sort((a, b) => depthOf(a) - depthOf(b)))
|
|
1329
|
+
visit(id);
|
|
1330
|
+
const groups = new Set();
|
|
1331
|
+
for (const [id, was] of prevOf) {
|
|
1332
|
+
const wasParent = was?.parent ?? null;
|
|
1333
|
+
const nowParent = this.parentOf.get(id) ?? null;
|
|
1334
|
+
if (wasParent !== null)
|
|
1335
|
+
groups.add(wasParent);
|
|
1336
|
+
if (nowParent !== null)
|
|
1337
|
+
groups.add(nowParent);
|
|
1338
|
+
if (this.objects.get(id)?.kind === "group")
|
|
1339
|
+
groups.add(id);
|
|
1340
|
+
}
|
|
1341
|
+
for (const g of groups) {
|
|
1342
|
+
const o = this.objects.get(g);
|
|
1343
|
+
if (!o || o.kind !== "group") {
|
|
1344
|
+
this.emptyGroups.delete(g);
|
|
1345
|
+
continue;
|
|
1346
|
+
}
|
|
1347
|
+
const empty = !(this.childrenOf.get(g) ?? []).some((c) => this.objects.has(c));
|
|
1348
|
+
if (empty && !o.hidden) {
|
|
1349
|
+
remember(g);
|
|
1350
|
+
this.objects.set(g, copyDerivedObject(o, { hidden: true, version: this.bumpVersion(g) }));
|
|
1351
|
+
this.emptyGroups.add(g);
|
|
1352
|
+
}
|
|
1353
|
+
else if (!empty && this.emptyGroups.has(g)) {
|
|
1354
|
+
remember(g);
|
|
1355
|
+
const shown = copyDerivedObject(o, { version: this.bumpVersion(g) });
|
|
1356
|
+
delete shown.hidden;
|
|
1357
|
+
this.objects.set(g, shown);
|
|
1358
|
+
this.emptyGroups.delete(g);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
const view = this.sceneView();
|
|
1362
|
+
const follow = new Set();
|
|
1363
|
+
for (const [id, was] of prevOf) {
|
|
1364
|
+
const now = this.objects.get(id);
|
|
1365
|
+
if (was === now)
|
|
1366
|
+
continue;
|
|
1367
|
+
for (const cid of this.attachedTo.get(id) ?? []) {
|
|
1368
|
+
const c = this.objects.get(cid);
|
|
1369
|
+
if (c?.conn && endpointPointChanged(c.conn, id, was, now, (oid) => this.objects.get(oid)))
|
|
1370
|
+
follow.add(cid);
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
for (const [id, was] of [...prevOf]) {
|
|
1374
|
+
const now = this.objects.get(id);
|
|
1375
|
+
if (now?.kind !== "connector" || !now.conn)
|
|
1376
|
+
continue;
|
|
1377
|
+
const b = connectorBounds(now.conn, view);
|
|
1378
|
+
if (was !== now) {
|
|
1379
|
+
if (!sameBox(now, b))
|
|
1380
|
+
Object.assign(now, b);
|
|
1381
|
+
follow.delete(id);
|
|
1382
|
+
continue;
|
|
1383
|
+
}
|
|
1384
|
+
if (sameBox(now, b) && !follow.has(id))
|
|
1385
|
+
continue;
|
|
1386
|
+
follow.delete(id);
|
|
1387
|
+
this.objects.set(id, copyDerivedObject(now, { ...b, version: this.bumpVersion(id) }));
|
|
1388
|
+
}
|
|
1389
|
+
for (const cid of follow) {
|
|
1390
|
+
const c = this.objects.get(cid);
|
|
1391
|
+
if (!c?.conn)
|
|
1392
|
+
continue;
|
|
1393
|
+
prevOf.set(cid, c);
|
|
1394
|
+
this.objects.set(cid, copyDerivedObject(c, { ...connectorBounds(c.conn, view), version: this.bumpVersion(cid) }));
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
spliceOrder(state, moved) {
|
|
1398
|
+
const before = state.beforeOrder;
|
|
1399
|
+
const affected = new Set(moved);
|
|
1400
|
+
const added = new Set();
|
|
1401
|
+
for (const [id, was] of state.prevOf) {
|
|
1402
|
+
const present = this.objects.has(id);
|
|
1403
|
+
if (!!was !== present)
|
|
1404
|
+
affected.add(id);
|
|
1405
|
+
if (!was && present)
|
|
1406
|
+
added.add(id);
|
|
1407
|
+
}
|
|
1408
|
+
if (!affected.size)
|
|
1409
|
+
return before;
|
|
1410
|
+
const wasPresent = (id) => state.prevOf.has(id) ? state.prevOf.get(id) !== undefined : this.objects.has(id);
|
|
1411
|
+
const oldParent = (id) => (state.prevOf.has(id) ? state.prevOf.get(id)?.parent : this.objects.get(id)?.parent) ?? null;
|
|
1412
|
+
const oldRoots = [];
|
|
1413
|
+
for (const id of affected) {
|
|
1414
|
+
if (!wasPresent(id))
|
|
1415
|
+
continue;
|
|
1416
|
+
let covered = false;
|
|
1417
|
+
for (let p = oldParent(id); p; p = oldParent(p))
|
|
1418
|
+
if (affected.has(p) && wasPresent(p)) {
|
|
1419
|
+
covered = true;
|
|
1420
|
+
break;
|
|
1421
|
+
}
|
|
1422
|
+
if (!covered)
|
|
1423
|
+
oldRoots.push(id);
|
|
1424
|
+
}
|
|
1425
|
+
if (oldRoots.length > 32)
|
|
1426
|
+
return this.computeOrder();
|
|
1427
|
+
const cut = new Set();
|
|
1428
|
+
const ranges = [];
|
|
1429
|
+
for (const root of oldRoots) {
|
|
1430
|
+
const start = before.indexOf(root);
|
|
1431
|
+
if (start < 0)
|
|
1432
|
+
continue;
|
|
1433
|
+
let end = start + 1;
|
|
1434
|
+
cut.add(root);
|
|
1435
|
+
for (; end < before.length; end++) {
|
|
1436
|
+
const id = before[end];
|
|
1437
|
+
let under = false;
|
|
1438
|
+
for (let p = oldParent(id); p; p = oldParent(p))
|
|
1439
|
+
if (p === root) {
|
|
1440
|
+
under = true;
|
|
1441
|
+
break;
|
|
1442
|
+
}
|
|
1443
|
+
if (!under)
|
|
1444
|
+
break;
|
|
1445
|
+
cut.add(id);
|
|
1446
|
+
}
|
|
1447
|
+
ranges.push([start, end]);
|
|
1448
|
+
}
|
|
1449
|
+
ranges.sort((a, b) => a[0] - b[0]);
|
|
1450
|
+
let order;
|
|
1451
|
+
if (ranges.length <= 8) {
|
|
1452
|
+
order = before.slice();
|
|
1453
|
+
for (let i = ranges.length - 1; i >= 0; i--) {
|
|
1454
|
+
const [start, end] = ranges[i];
|
|
1455
|
+
order.splice(start, end - start);
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
else {
|
|
1459
|
+
order = [];
|
|
1460
|
+
let cursor = 0;
|
|
1461
|
+
for (const [start, end] of ranges) {
|
|
1462
|
+
for (; cursor < start; cursor++)
|
|
1463
|
+
order.push(before[cursor]);
|
|
1464
|
+
cursor = end;
|
|
1465
|
+
}
|
|
1466
|
+
for (; cursor < before.length; cursor++)
|
|
1467
|
+
order.push(before[cursor]);
|
|
1468
|
+
}
|
|
1469
|
+
const insert = new Set(added);
|
|
1470
|
+
for (const id of cut)
|
|
1471
|
+
if (this.objects.has(id))
|
|
1472
|
+
insert.add(id);
|
|
1473
|
+
const insertionRoot = (id) => {
|
|
1474
|
+
let root = id;
|
|
1475
|
+
for (let p = this.parentOf.get(id); p; p = this.parentOf.get(p))
|
|
1476
|
+
if (insert.has(p))
|
|
1477
|
+
root = p;
|
|
1478
|
+
return root;
|
|
1479
|
+
};
|
|
1480
|
+
const roots = new Set();
|
|
1481
|
+
for (const id of insert)
|
|
1482
|
+
roots.add(insertionRoot(id));
|
|
1483
|
+
if (roots.size > 32)
|
|
1484
|
+
return this.computeOrder();
|
|
1485
|
+
const predecessor = (id) => {
|
|
1486
|
+
const parent = this.parentOf.get(id) ?? null;
|
|
1487
|
+
const siblings = this.childrenOf.get(parent) ?? [];
|
|
1488
|
+
for (let i = siblings.indexOf(id) - 1; i >= 0; i--) {
|
|
1489
|
+
let previous = siblings[i];
|
|
1490
|
+
if (!this.objects.has(previous))
|
|
1491
|
+
continue;
|
|
1492
|
+
for (;;) {
|
|
1493
|
+
const children = this.childrenOf.get(previous) ?? [];
|
|
1494
|
+
let last;
|
|
1495
|
+
for (let j = children.length - 1; j >= 0; j--)
|
|
1496
|
+
if (this.objects.has(children[j])) {
|
|
1497
|
+
last = children[j];
|
|
1498
|
+
break;
|
|
1499
|
+
}
|
|
1500
|
+
if (last === undefined)
|
|
1501
|
+
return previous;
|
|
1502
|
+
previous = last;
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
return parent ?? undefined;
|
|
1506
|
+
};
|
|
1507
|
+
const placed = new Set();
|
|
1508
|
+
const bulk = roots.size > 8;
|
|
1509
|
+
const anchors = new Map();
|
|
1510
|
+
const runs = new Map();
|
|
1511
|
+
for (const root of roots) {
|
|
1512
|
+
const pending = [root];
|
|
1513
|
+
while (pending.length) {
|
|
1514
|
+
const id = pending[pending.length - 1];
|
|
1515
|
+
if (placed.has(id)) {
|
|
1516
|
+
pending.pop();
|
|
1517
|
+
continue;
|
|
1518
|
+
}
|
|
1519
|
+
const previous = predecessor(id);
|
|
1520
|
+
if (previous !== undefined && insert.has(previous)) {
|
|
1521
|
+
const dependency = insertionRoot(previous);
|
|
1522
|
+
if (!placed.has(dependency)) {
|
|
1523
|
+
pending.push(dependency);
|
|
1524
|
+
continue;
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
const run = [];
|
|
1528
|
+
const stack = [id];
|
|
1529
|
+
while (stack.length) {
|
|
1530
|
+
const next = stack.pop();
|
|
1531
|
+
if (!this.objects.has(next))
|
|
1532
|
+
continue;
|
|
1533
|
+
run.push(next);
|
|
1534
|
+
const children = this.childrenOf.get(next) ?? [];
|
|
1535
|
+
for (let i = children.length - 1; i >= 0; i--)
|
|
1536
|
+
stack.push(children[i]);
|
|
1537
|
+
}
|
|
1538
|
+
if (bulk) {
|
|
1539
|
+
const anchor = previous !== undefined && insert.has(previous) ? anchors.get(insertionRoot(previous)) : previous;
|
|
1540
|
+
anchors.set(id, anchor);
|
|
1541
|
+
let group = runs.get(anchor);
|
|
1542
|
+
if (!group)
|
|
1543
|
+
runs.set(anchor, (group = []));
|
|
1544
|
+
group.push(run);
|
|
1545
|
+
}
|
|
1546
|
+
else {
|
|
1547
|
+
const at = previous === undefined ? 0 : order.indexOf(previous) + 1;
|
|
1548
|
+
if (run.length < 8192)
|
|
1549
|
+
order.splice(at, 0, ...run);
|
|
1550
|
+
else
|
|
1551
|
+
order = order.slice(0, at).concat(run, order.slice(at));
|
|
1552
|
+
}
|
|
1553
|
+
placed.add(id);
|
|
1554
|
+
pending.pop();
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
if (bulk) {
|
|
1558
|
+
const merged = [];
|
|
1559
|
+
const append = (anchor) => {
|
|
1560
|
+
for (const run of runs.get(anchor) ?? [])
|
|
1561
|
+
for (const id of run)
|
|
1562
|
+
merged.push(id);
|
|
1563
|
+
};
|
|
1564
|
+
append(undefined);
|
|
1565
|
+
for (const id of order) {
|
|
1566
|
+
merged.push(id);
|
|
1567
|
+
append(id);
|
|
1568
|
+
}
|
|
1569
|
+
return merged;
|
|
1570
|
+
}
|
|
1571
|
+
return order;
|
|
1572
|
+
}
|
|
1573
|
+
computeOrder() {
|
|
1574
|
+
const out = new Array(this.objects.size);
|
|
1575
|
+
let n = 0;
|
|
1576
|
+
const stack = [];
|
|
1577
|
+
const roots = this.childrenOf.get(null) ?? [];
|
|
1578
|
+
for (let i = roots.length - 1; i >= 0; i--)
|
|
1579
|
+
stack.push(roots[i]);
|
|
1580
|
+
while (stack.length) {
|
|
1581
|
+
const id = stack.pop();
|
|
1582
|
+
if (!this.objects.has(id))
|
|
1583
|
+
continue;
|
|
1584
|
+
out[n++] = id;
|
|
1585
|
+
const kids = this.childrenOf.get(id);
|
|
1586
|
+
if (kids)
|
|
1587
|
+
for (let i = kids.length - 1; i >= 0; i--)
|
|
1588
|
+
stack.push(kids[i]);
|
|
1589
|
+
}
|
|
1590
|
+
return n === out.length ? out : out.slice(0, n);
|
|
1591
|
+
}
|
|
1592
|
+
changeSet(prevOf, beforeOrder, structural) {
|
|
1593
|
+
const cs = { added: [], updated: [], removed: [] };
|
|
1594
|
+
const addedIds = new Set();
|
|
1595
|
+
for (const [id, was] of prevOf) {
|
|
1596
|
+
const now = this.objects.get(id);
|
|
1597
|
+
if (was && !now)
|
|
1598
|
+
cs.removed.push(id);
|
|
1599
|
+
else if (!was && now)
|
|
1600
|
+
addedIds.add(id);
|
|
1601
|
+
else if (now && was !== now)
|
|
1602
|
+
cs.updated.push(now);
|
|
1603
|
+
}
|
|
1604
|
+
if (!structural) {
|
|
1605
|
+
for (const id of addedIds)
|
|
1606
|
+
cs.added.push(this.objects.get(id));
|
|
1607
|
+
return cs;
|
|
1608
|
+
}
|
|
1609
|
+
cs.order = this.orderIds;
|
|
1610
|
+
const removed = new Set(cs.removed);
|
|
1611
|
+
const after = this.orderIds;
|
|
1612
|
+
for (const id of after)
|
|
1613
|
+
if (addedIds.has(id))
|
|
1614
|
+
cs.added.push(this.objects.get(id));
|
|
1615
|
+
let reordered = false;
|
|
1616
|
+
let i = 0;
|
|
1617
|
+
for (const id of beforeOrder) {
|
|
1618
|
+
if (removed.has(id))
|
|
1619
|
+
continue;
|
|
1620
|
+
while (i < after.length && addedIds.has(after[i]))
|
|
1621
|
+
i++;
|
|
1622
|
+
if (after[i] !== id) {
|
|
1623
|
+
reordered = true;
|
|
1624
|
+
break;
|
|
1625
|
+
}
|
|
1626
|
+
i++;
|
|
1627
|
+
}
|
|
1628
|
+
if (reordered)
|
|
1629
|
+
cs.reordered = true;
|
|
1630
|
+
return cs;
|
|
1631
|
+
}
|
|
1632
|
+
flush(quiet = false) {
|
|
1633
|
+
if (this.delivering)
|
|
1634
|
+
return;
|
|
1635
|
+
this.delivering = true;
|
|
1636
|
+
let thrown;
|
|
1637
|
+
let threw = false;
|
|
1638
|
+
try {
|
|
1639
|
+
while (this.pending.length) {
|
|
1640
|
+
const { cs, meta } = this.pending.shift();
|
|
1641
|
+
for (const l of this.listeners) {
|
|
1642
|
+
try {
|
|
1643
|
+
l(cs, meta);
|
|
1644
|
+
}
|
|
1645
|
+
catch (err) {
|
|
1646
|
+
if (!threw) {
|
|
1647
|
+
threw = true;
|
|
1648
|
+
thrown = err;
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
finally {
|
|
1655
|
+
this.delivering = false;
|
|
1656
|
+
}
|
|
1657
|
+
if (threw) {
|
|
1658
|
+
if (!quiet)
|
|
1659
|
+
throw thrown;
|
|
1660
|
+
console.error("LoroPageDoc: a change listener threw", thrown);
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
op(fn) {
|
|
1664
|
+
this.opDepth++;
|
|
1665
|
+
let ok = false;
|
|
1666
|
+
try {
|
|
1667
|
+
const result = fn();
|
|
1668
|
+
ok = true;
|
|
1669
|
+
return result;
|
|
1670
|
+
}
|
|
1671
|
+
finally {
|
|
1672
|
+
if (--this.opDepth === 0)
|
|
1673
|
+
this.flush(!ok);
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
subscribe(l) {
|
|
1677
|
+
this.listeners.add(l);
|
|
1678
|
+
return () => this.listeners.delete(l);
|
|
1679
|
+
}
|
|
1680
|
+
commit(label, origin, loroOrigin) {
|
|
1681
|
+
const was = { applying: this.applying, label: this.pendingLabel, origin: this.pendingOrigin };
|
|
1682
|
+
this.applying = true;
|
|
1683
|
+
this.pendingLabel = label;
|
|
1684
|
+
this.pendingOrigin = origin;
|
|
1685
|
+
try {
|
|
1686
|
+
this.doc.commit({ origin: loroOrigin });
|
|
1687
|
+
}
|
|
1688
|
+
finally {
|
|
1689
|
+
this.applying = was.applying;
|
|
1690
|
+
this.pendingLabel = was.label;
|
|
1691
|
+
this.pendingOrigin = was.origin;
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
pushUndo(e) {
|
|
1695
|
+
this.undoStack.push(e);
|
|
1696
|
+
if (this.undoStack.length > MAX_UNDO_ENTRIES)
|
|
1697
|
+
this.undoStack.shift();
|
|
1698
|
+
}
|
|
1699
|
+
transact(label, fn) {
|
|
1700
|
+
if (this.dead)
|
|
1701
|
+
throw new Error("LoroPageDoc: document is dead (a WASM trap killed it); reload");
|
|
1702
|
+
if (this.applying)
|
|
1703
|
+
throw new Error("LoroPageDoc: cannot mutate the document from within a change notification (transact)");
|
|
1704
|
+
this.op(() => this.transactImpl(label, fn));
|
|
1705
|
+
}
|
|
1706
|
+
load(fn) {
|
|
1707
|
+
if (this.dead)
|
|
1708
|
+
throw new Error("LoroPageDoc: document is dead (a WASM trap killed it); reload");
|
|
1709
|
+
if (this.applying || this.opDepth > 0 || this.loading)
|
|
1710
|
+
throw new Error("LoroPageDoc.load: not re-entrant (called from a transaction or a change notification)");
|
|
1711
|
+
const before = this.objects;
|
|
1712
|
+
const beforeOrder = this.orderIds;
|
|
1713
|
+
this.loading = true;
|
|
1714
|
+
try {
|
|
1715
|
+
fn();
|
|
1716
|
+
}
|
|
1717
|
+
finally {
|
|
1718
|
+
this.loading = false;
|
|
1719
|
+
this.lastTyping = null;
|
|
1720
|
+
}
|
|
1721
|
+
this.op(() => {
|
|
1722
|
+
this.refreshTrashId();
|
|
1723
|
+
this.materializeAll();
|
|
1724
|
+
const prevOf = new Map();
|
|
1725
|
+
for (const [id, o] of before)
|
|
1726
|
+
prevOf.set(id, o);
|
|
1727
|
+
for (const id of this.objects.keys())
|
|
1728
|
+
if (!prevOf.has(id))
|
|
1729
|
+
prevOf.set(id, undefined);
|
|
1730
|
+
const cs = this.changeSet(prevOf, beforeOrder, true);
|
|
1731
|
+
this.pending.push({ cs, meta: { label: "load", origin: "local" } });
|
|
1732
|
+
});
|
|
1733
|
+
}
|
|
1734
|
+
transactImpl(label, fn) {
|
|
1735
|
+
const e = { serial: this.nextSerial++, label, keys: [], moves: [], creates: [], textSteps: 0 };
|
|
1736
|
+
const textEdits = [];
|
|
1737
|
+
let containerCreated = false;
|
|
1738
|
+
const boxChanged = new Set();
|
|
1739
|
+
const shifted = new Set();
|
|
1740
|
+
const moved = new Set();
|
|
1741
|
+
const connTouched = new Set();
|
|
1742
|
+
const connCreated = new Set();
|
|
1743
|
+
const self = this;
|
|
1744
|
+
const siblings = this.mutationSiblings();
|
|
1745
|
+
const textOf = (d) => {
|
|
1746
|
+
const cur = d.get("text");
|
|
1747
|
+
if (cur instanceof LoroText)
|
|
1748
|
+
return cur;
|
|
1749
|
+
containerCreated = true;
|
|
1750
|
+
return self.ensureText(d);
|
|
1751
|
+
};
|
|
1752
|
+
const textModels = new Map();
|
|
1753
|
+
const modelOf = (id, d) => {
|
|
1754
|
+
const m = textModels.get(id);
|
|
1755
|
+
if (m !== undefined)
|
|
1756
|
+
return m;
|
|
1757
|
+
const cur = d.get("text");
|
|
1758
|
+
const raw = cur instanceof LoroText ? cur.toString() : "";
|
|
1759
|
+
return raw.endsWith("\n") ? raw : raw + "\n";
|
|
1760
|
+
};
|
|
1761
|
+
const validatedAfter = (id, model, delta) => {
|
|
1762
|
+
try {
|
|
1763
|
+
validateTextDelta(model, delta);
|
|
1764
|
+
}
|
|
1765
|
+
catch (err) {
|
|
1766
|
+
throw new Error(`editText(${id}): ${err instanceof Error ? err.message : String(err)}`);
|
|
1767
|
+
}
|
|
1768
|
+
return textAfterDelta(model, delta);
|
|
1769
|
+
};
|
|
1770
|
+
const setKey = (n, sub, key, value, def) => self.writeKey(e, n, sub, key, value, def, () => {
|
|
1771
|
+
containerCreated = true;
|
|
1772
|
+
});
|
|
1773
|
+
const tx = {
|
|
1774
|
+
set(id, path, value) {
|
|
1775
|
+
const n = self.node(id);
|
|
1776
|
+
if (!n || self.inTrash(n))
|
|
1777
|
+
return;
|
|
1778
|
+
const d = n.data;
|
|
1779
|
+
const [head, ...rest] = path.split(".");
|
|
1780
|
+
switch (head) {
|
|
1781
|
+
case "x":
|
|
1782
|
+
case "y":
|
|
1783
|
+
case "w":
|
|
1784
|
+
case "h": {
|
|
1785
|
+
if (d.get("kind") === "connector")
|
|
1786
|
+
return;
|
|
1787
|
+
const cur = (d.get("xywh") ?? [0, 0, 100, 100]).slice();
|
|
1788
|
+
const origin = self.absOriginOfParent(n);
|
|
1789
|
+
const idx = { x: 0, y: 1, w: 2, h: 3 }[head];
|
|
1790
|
+
const rel = head === "x" ? value - origin.x : head === "y" ? value - origin.y : value;
|
|
1791
|
+
cur[idx] = rel;
|
|
1792
|
+
if (setKey(n, undefined, "xywh", cur)) {
|
|
1793
|
+
boxChanged.add(id);
|
|
1794
|
+
if ((head === "x" || head === "y") && CONTAINER_KINDS.has(d.get("kind") ?? ""))
|
|
1795
|
+
shifted.add(id);
|
|
1796
|
+
}
|
|
1797
|
+
return;
|
|
1798
|
+
}
|
|
1799
|
+
case "style": {
|
|
1800
|
+
if (rest.length === 1)
|
|
1801
|
+
setKey(n, "style", rest[0], value);
|
|
1802
|
+
return;
|
|
1803
|
+
}
|
|
1804
|
+
case "conn": {
|
|
1805
|
+
const isConnector = d.get("kind") === "connector";
|
|
1806
|
+
const isEndpoint = (k) => k === "start" || k === "end";
|
|
1807
|
+
const derived = (k) => {
|
|
1808
|
+
const ep = self.readKey(d, "conn", endOfPoint(k));
|
|
1809
|
+
return !!ep && typeof ep === "object" && self.anchorOf(ep) !== undefined;
|
|
1810
|
+
};
|
|
1811
|
+
let changed = false;
|
|
1812
|
+
const writeOne = (k, v) => {
|
|
1813
|
+
if (isEndpoint(k)) {
|
|
1814
|
+
const ep = endpointValue(v);
|
|
1815
|
+
if (isConnector && (!ep || typeof ep !== "object"))
|
|
1816
|
+
return;
|
|
1817
|
+
if (isConnector) {
|
|
1818
|
+
const cur = self.readKey(d, "conn", k);
|
|
1819
|
+
const drawn = cur && typeof cur === "object" ? self.drawnPointOf(cur) : undefined;
|
|
1820
|
+
if (drawn && !(ep && typeof ep === "object" && self.anchorOf(ep) !== undefined)) {
|
|
1821
|
+
const pk = pointKeyOf(k);
|
|
1822
|
+
const stored = self.readKey(d, "conn", pk);
|
|
1823
|
+
if (!(isPoint(stored) && stored[0] === drawn[0] && stored[1] === drawn[1]))
|
|
1824
|
+
setKey(n, "conn", pk, drawn);
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
if (setKey(n, "conn", k, ep))
|
|
1828
|
+
changed = true;
|
|
1829
|
+
}
|
|
1830
|
+
else if (isConnector && isPointKey(k)) {
|
|
1831
|
+
if (isPoint(v) && !derived(k))
|
|
1832
|
+
setKey(n, "conn", k, v);
|
|
1833
|
+
}
|
|
1834
|
+
else if (setKey(n, "conn", k, v))
|
|
1835
|
+
changed = true;
|
|
1836
|
+
};
|
|
1837
|
+
if (rest.length === 0) {
|
|
1838
|
+
if (!value || typeof value !== "object") {
|
|
1839
|
+
if (!isConnector)
|
|
1840
|
+
setKey(n, undefined, "conn", undefined);
|
|
1841
|
+
return;
|
|
1842
|
+
}
|
|
1843
|
+
const entries = Object.entries(value);
|
|
1844
|
+
for (const [k, val] of entries)
|
|
1845
|
+
if (isEndpoint(k))
|
|
1846
|
+
writeOne(k, val);
|
|
1847
|
+
for (const [k, val] of entries)
|
|
1848
|
+
if (!isEndpoint(k))
|
|
1849
|
+
writeOne(k, val);
|
|
1850
|
+
}
|
|
1851
|
+
else {
|
|
1852
|
+
const key = rest[0];
|
|
1853
|
+
let v = value;
|
|
1854
|
+
if (rest.length > 1) {
|
|
1855
|
+
if (!isEndpoint(key))
|
|
1856
|
+
return;
|
|
1857
|
+
const cur = self.readKey(d, "conn", key) ?? {};
|
|
1858
|
+
const group = { ...cur };
|
|
1859
|
+
if (value === undefined)
|
|
1860
|
+
delete group[rest[1]];
|
|
1861
|
+
else
|
|
1862
|
+
group[rest[1]] = value;
|
|
1863
|
+
v = group;
|
|
1864
|
+
}
|
|
1865
|
+
writeOne(key, v);
|
|
1866
|
+
}
|
|
1867
|
+
if (changed)
|
|
1868
|
+
connTouched.add(id);
|
|
1869
|
+
return;
|
|
1870
|
+
}
|
|
1871
|
+
case "anchors": {
|
|
1872
|
+
let changed = false;
|
|
1873
|
+
if (rest.length === 0) {
|
|
1874
|
+
if (!value || typeof value !== "object") {
|
|
1875
|
+
const cur = self.readKey(d, undefined, "anchors");
|
|
1876
|
+
if (cur)
|
|
1877
|
+
for (const k of Object.keys(cur))
|
|
1878
|
+
if (setKey(n, "anchors", k, undefined))
|
|
1879
|
+
changed = true;
|
|
1880
|
+
}
|
|
1881
|
+
else
|
|
1882
|
+
for (const [k, v] of Object.entries(value))
|
|
1883
|
+
if ((v === undefined || isPoint(v)) && setKey(n, "anchors", k, v))
|
|
1884
|
+
changed = true;
|
|
1885
|
+
}
|
|
1886
|
+
else if (rest.length === 1 && (value === undefined || isPoint(value)))
|
|
1887
|
+
changed = setKey(n, "anchors", rest[0], value);
|
|
1888
|
+
if (changed)
|
|
1889
|
+
boxChanged.add(id);
|
|
1890
|
+
return;
|
|
1891
|
+
}
|
|
1892
|
+
case "inst": {
|
|
1893
|
+
if (d.get("kind") !== "instance")
|
|
1894
|
+
return;
|
|
1895
|
+
if (rest.length === 0) {
|
|
1896
|
+
if (!value || typeof value !== "object") {
|
|
1897
|
+
const cur = self.readKey(d, undefined, "inst");
|
|
1898
|
+
if (cur)
|
|
1899
|
+
for (const k of Object.keys(cur))
|
|
1900
|
+
setKey(n, "inst", k, undefined);
|
|
1901
|
+
}
|
|
1902
|
+
else
|
|
1903
|
+
for (const [k, v] of Object.entries(value)) {
|
|
1904
|
+
const r = instKeyValue(k, v);
|
|
1905
|
+
if (r.ok)
|
|
1906
|
+
setKey(n, "inst", k, r.value);
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
else if (rest.length === 1) {
|
|
1910
|
+
const r = instKeyValue(rest[0], value);
|
|
1911
|
+
if (r.ok)
|
|
1912
|
+
setKey(n, "inst", rest[0], r.value);
|
|
1913
|
+
}
|
|
1914
|
+
return;
|
|
1915
|
+
}
|
|
1916
|
+
case "hidden":
|
|
1917
|
+
if (setKey(n, undefined, "hidden", value))
|
|
1918
|
+
boxChanged.add(id);
|
|
1919
|
+
return;
|
|
1920
|
+
case "kind": {
|
|
1921
|
+
const curKind = d.get("kind") ?? "rect";
|
|
1922
|
+
if (value === "connector" || curKind === "connector" || value === "trash" || value === "instance")
|
|
1923
|
+
return;
|
|
1924
|
+
if (setKey(n, undefined, "kind", value)) {
|
|
1925
|
+
boxChanged.add(id);
|
|
1926
|
+
shifted.add(id);
|
|
1927
|
+
}
|
|
1928
|
+
return;
|
|
1929
|
+
}
|
|
1930
|
+
case "text": {
|
|
1931
|
+
const content = value ?? { paragraphs: [] };
|
|
1932
|
+
let model = contentToFlat(content).text;
|
|
1933
|
+
for (const t of textEdits)
|
|
1934
|
+
if (t.id === id && t.delta)
|
|
1935
|
+
model = validatedAfter(id, model, t.delta);
|
|
1936
|
+
textModels.set(id, model);
|
|
1937
|
+
textEdits.push({ id, text: textOf(d), content });
|
|
1938
|
+
return;
|
|
1939
|
+
}
|
|
1940
|
+
case "rot":
|
|
1941
|
+
if (setKey(n, undefined, "rot", value, 0))
|
|
1942
|
+
boxChanged.add(id);
|
|
1943
|
+
return;
|
|
1944
|
+
default:
|
|
1945
|
+
if (head === "extId")
|
|
1946
|
+
return;
|
|
1947
|
+
if (rest.length)
|
|
1948
|
+
return;
|
|
1949
|
+
setKey(n, undefined, head, value);
|
|
1950
|
+
}
|
|
1951
|
+
},
|
|
1952
|
+
move(id, parent, index) {
|
|
1953
|
+
const n = self.node(id);
|
|
1954
|
+
if (!n || self.inTrash(n))
|
|
1955
|
+
return;
|
|
1956
|
+
const fromParent = self.parentIdOf(n);
|
|
1957
|
+
const oldOrigin = self.absOriginOfParent(n);
|
|
1958
|
+
const target = parent ? self.node(parent) : undefined;
|
|
1959
|
+
if (parent && (!target || self.inTrash(target)))
|
|
1960
|
+
return;
|
|
1961
|
+
const raw = siblings.rawIndex(target, index);
|
|
1962
|
+
const siblingCount = siblings.count(target);
|
|
1963
|
+
const curRaw = n.index() ?? 0;
|
|
1964
|
+
const rawIdx = raw === undefined || !target ? raw : Math.min(raw, siblingCount - (fromParent === (parent ?? null) ? 1 : 0));
|
|
1965
|
+
if (fromParent === (parent ?? null)) {
|
|
1966
|
+
const resolved = rawIdx === undefined ? siblingCount - 1 : Math.min(rawIdx, siblingCount - 1);
|
|
1967
|
+
if (resolved === curRaw)
|
|
1968
|
+
return;
|
|
1969
|
+
}
|
|
1970
|
+
try {
|
|
1971
|
+
n.move(target, rawIdx);
|
|
1972
|
+
}
|
|
1973
|
+
catch (err) {
|
|
1974
|
+
void err;
|
|
1975
|
+
return;
|
|
1976
|
+
}
|
|
1977
|
+
siblings.moved(id, fromParent, parent, n.index() ?? 0);
|
|
1978
|
+
e.moves.push({ id, fromParent, fromIndex: curRaw, toParent: parent });
|
|
1979
|
+
moved.add(id);
|
|
1980
|
+
const newOrigin = self.absOriginOfParent(n);
|
|
1981
|
+
if (oldOrigin.x !== newOrigin.x || oldOrigin.y !== newOrigin.y) {
|
|
1982
|
+
self.rebaseXywh(n, oldOrigin, newOrigin, setKey);
|
|
1983
|
+
shifted.add(id);
|
|
1984
|
+
}
|
|
1985
|
+
},
|
|
1986
|
+
create(obj) {
|
|
1987
|
+
if (!normalizeNode(obj.id, rawOfObject(obj), { x: 0, y: 0 }, null).object)
|
|
1988
|
+
return;
|
|
1989
|
+
const parentNode = obj.parent ? self.node(obj.parent) : undefined;
|
|
1990
|
+
const liveParent = parentNode && !self.inTrash(parentNode) ? parentNode : undefined;
|
|
1991
|
+
if (!liveParent)
|
|
1992
|
+
obj.parent = null;
|
|
1993
|
+
const n = self.tree.createNode(liveParent ? liveParent.id : undefined);
|
|
1994
|
+
const origin = self.absOriginOfParent(n);
|
|
1995
|
+
const d = n.data;
|
|
1996
|
+
d.set("kind", obj.kind);
|
|
1997
|
+
d.set("xywh", [obj.x - origin.x, obj.y - origin.y, obj.w, obj.h]);
|
|
1998
|
+
if (obj.rot)
|
|
1999
|
+
d.set("rot", obj.rot);
|
|
2000
|
+
if (obj.hidden)
|
|
2001
|
+
d.set("hidden", true);
|
|
2002
|
+
if (obj.locked)
|
|
2003
|
+
d.set("locked", true);
|
|
2004
|
+
if (obj.autoWidth)
|
|
2005
|
+
d.set("autoWidth", true);
|
|
2006
|
+
if (obj.autoHeight)
|
|
2007
|
+
d.set("autoHeight", true);
|
|
2008
|
+
if (typeof obj.name === "string")
|
|
2009
|
+
d.set("name", obj.name);
|
|
2010
|
+
if (typeof obj.extId === "string")
|
|
2011
|
+
d.set("extId", obj.extId);
|
|
2012
|
+
if (obj.embed && typeof obj.embed === "object" && typeof obj.embed.url === "string")
|
|
2013
|
+
d.set("embed", obj.embed);
|
|
2014
|
+
const inst = instanceRef(obj.inst);
|
|
2015
|
+
if (obj.kind === "instance" || inst) {
|
|
2016
|
+
const m = d.setContainer("inst", new LoroMap());
|
|
2017
|
+
for (const [k, v] of Object.entries(inst ?? {}))
|
|
2018
|
+
m.set(k, v);
|
|
2019
|
+
}
|
|
2020
|
+
if (obj.kind !== "connector") {
|
|
2021
|
+
const m = d.setContainer("anchors", new LoroMap());
|
|
2022
|
+
for (const [k, v] of Object.entries(validAnchors(obj.anchors) ?? {}))
|
|
2023
|
+
m.set(k, v);
|
|
2024
|
+
}
|
|
2025
|
+
const style = d.setContainer("style", new LoroMap());
|
|
2026
|
+
for (const [k, v] of Object.entries(obj.style))
|
|
2027
|
+
if (v !== undefined)
|
|
2028
|
+
style.set(k, v);
|
|
2029
|
+
if (obj.text || TEXT_CAPABLE_KINDS.has(obj.kind))
|
|
2030
|
+
writeText(d.setContainer("text", new LoroText()), obj.text ?? { paragraphs: [] });
|
|
2031
|
+
if (obj.path)
|
|
2032
|
+
d.set("path", obj.path);
|
|
2033
|
+
if (obj.image)
|
|
2034
|
+
d.set("image", obj.image);
|
|
2035
|
+
if (obj.clip)
|
|
2036
|
+
d.set("clip", true);
|
|
2037
|
+
if (obj.conn) {
|
|
2038
|
+
const full = completeConn(obj.conn, { x: obj.x, y: obj.y, w: obj.w, h: obj.h });
|
|
2039
|
+
const start = endpointValue(full.start);
|
|
2040
|
+
const end = endpointValue(full.end);
|
|
2041
|
+
obj.conn = { ...full, start, end, startAt: self.anchorOf(start) ?? full.startAt, endAt: self.anchorOf(end) ?? full.endAt };
|
|
2042
|
+
const conn = d.setContainer("conn", new LoroMap());
|
|
2043
|
+
for (const [k, v] of Object.entries(obj.conn))
|
|
2044
|
+
if (v !== undefined)
|
|
2045
|
+
conn.set(k, v);
|
|
2046
|
+
connCreated.add(n.id);
|
|
2047
|
+
}
|
|
2048
|
+
obj.id = n.id;
|
|
2049
|
+
siblings.created(n.id, obj.parent);
|
|
2050
|
+
e.creates.push(n.id);
|
|
2051
|
+
},
|
|
2052
|
+
delete(id) {
|
|
2053
|
+
const n = self.node(id);
|
|
2054
|
+
if (!n || self.inTrash(n))
|
|
2055
|
+
return;
|
|
2056
|
+
self.anchorBeforeTrash(id, setKey);
|
|
2057
|
+
const fromParent = self.parentIdOf(n);
|
|
2058
|
+
e.moves.push({ id, fromParent, fromIndex: n.index() ?? 0, toParent: self.trashId, at: self.positionOf(n) });
|
|
2059
|
+
n.move(self.node(self.trashId));
|
|
2060
|
+
siblings.moved(id, fromParent, self.trashId, n.index() ?? 0);
|
|
2061
|
+
},
|
|
2062
|
+
editText(id, delta) {
|
|
2063
|
+
const n = self.node(id);
|
|
2064
|
+
if (!n || self.inTrash(n))
|
|
2065
|
+
return;
|
|
2066
|
+
textModels.set(id, validatedAfter(id, modelOf(id, n.data), delta));
|
|
2067
|
+
textEdits.push({ id, text: textOf(n.data), delta });
|
|
2068
|
+
},
|
|
2069
|
+
};
|
|
2070
|
+
let threw = false;
|
|
2071
|
+
let thrown;
|
|
2072
|
+
try {
|
|
2073
|
+
fn(tx);
|
|
2074
|
+
}
|
|
2075
|
+
catch (err) {
|
|
2076
|
+
threw = true;
|
|
2077
|
+
thrown = err;
|
|
2078
|
+
}
|
|
2079
|
+
const editedId = textEdits[0]?.id;
|
|
2080
|
+
const typingId = editedId !== undefined &&
|
|
2081
|
+
textEdits.every((t) => t.id === editedId && t.delta !== undefined && textDeltaIsKeystroke(t.delta)) &&
|
|
2082
|
+
e.creates.length === 0 &&
|
|
2083
|
+
e.moves.length === 0 &&
|
|
2084
|
+
e.keys.every((k) => {
|
|
2085
|
+
if (k.id !== editedId || k.sub !== undefined || k.key !== "xywh")
|
|
2086
|
+
return false;
|
|
2087
|
+
const before = k.before;
|
|
2088
|
+
const after = k.after;
|
|
2089
|
+
return Array.isArray(before) && Array.isArray(after) && before.length === 4 && after.length === 4 && before.slice(0, 3).every((v, i) => v === after[i]);
|
|
2090
|
+
})
|
|
2091
|
+
? editedId
|
|
2092
|
+
: null;
|
|
2093
|
+
try {
|
|
2094
|
+
this.refreshAnchors(boxChanged, shifted, moved, connTouched, connCreated, setKey);
|
|
2095
|
+
}
|
|
2096
|
+
catch (err) {
|
|
2097
|
+
if (!threw) {
|
|
2098
|
+
threw = true;
|
|
2099
|
+
thrown = err;
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
const hasDiagramOps = e.keys.length > 0 || e.moves.length > 0 || e.creates.length > 0;
|
|
2103
|
+
if (threw) {
|
|
2104
|
+
if (hasDiagramOps || containerCreated)
|
|
2105
|
+
this.commit(label, "local", "diagram");
|
|
2106
|
+
if (hasDiagramOps && !this.loading) {
|
|
2107
|
+
this.pushUndo(e);
|
|
2108
|
+
this.redoStack = [];
|
|
2109
|
+
this.lastTyping = null;
|
|
2110
|
+
}
|
|
2111
|
+
throw thrown;
|
|
2112
|
+
}
|
|
2113
|
+
if (!hasDiagramOps && !containerCreated && !textEdits.length)
|
|
2114
|
+
return;
|
|
2115
|
+
const now = this.now();
|
|
2116
|
+
const merge = typingId !== null && this.lastTyping !== null && this.lastTyping.id === typingId && now - this.lastTyping.at <= TYPING_MERGE_MS ? this.lastTyping : null;
|
|
2117
|
+
const entry = merge ? merge.entry : e;
|
|
2118
|
+
const previousTextSteps = entry.textSteps;
|
|
2119
|
+
if (hasDiagramOps || containerCreated)
|
|
2120
|
+
this.commit(label, "local", "diagram");
|
|
2121
|
+
try {
|
|
2122
|
+
if (textEdits.length)
|
|
2123
|
+
this.commitTextEdits(label, entry, textEdits);
|
|
2124
|
+
}
|
|
2125
|
+
finally {
|
|
2126
|
+
if (this.loading) {
|
|
2127
|
+
}
|
|
2128
|
+
else if (merge && entry.textSteps > previousTextSteps) {
|
|
2129
|
+
entry.keys.push(...e.keys);
|
|
2130
|
+
merge.at = now;
|
|
2131
|
+
this.redoStack = [];
|
|
2132
|
+
}
|
|
2133
|
+
else if (hasDiagramOps || e.textSteps > 0) {
|
|
2134
|
+
this.pushUndo(e);
|
|
2135
|
+
this.redoStack = [];
|
|
2136
|
+
this.lastTyping = typingId !== null && e.textSteps > 0 ? { id: typingId, at: now, entry: e } : null;
|
|
2137
|
+
}
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
commitTextEdits(label, entry, edits) {
|
|
2141
|
+
this.textPushes = 0;
|
|
2142
|
+
this.countingPushes = true;
|
|
2143
|
+
this.pushSerial = entry.serial;
|
|
2144
|
+
let error;
|
|
2145
|
+
const seenTexts = new Set();
|
|
2146
|
+
const ordered = [...edits.filter((t) => t.content), ...edits.filter((t) => t.delta)];
|
|
2147
|
+
try {
|
|
2148
|
+
for (const { id, text, delta, content } of ordered) {
|
|
2149
|
+
if (seenTexts.has(id))
|
|
2150
|
+
this.commit(label, "local", this.loading ? "sys:load" : "text");
|
|
2151
|
+
seenTexts.add(id);
|
|
2152
|
+
if (content) {
|
|
2153
|
+
const curFlat = loroToFlat(text);
|
|
2154
|
+
const d = rewriteDelta(curFlat, contentToFlat(content));
|
|
2155
|
+
if (!d.length)
|
|
2156
|
+
continue;
|
|
2157
|
+
ensureTrailingNewline(text);
|
|
2158
|
+
applyLoroDelta(text, curFlat.text, d);
|
|
2159
|
+
continue;
|
|
2160
|
+
}
|
|
2161
|
+
const raw = text.toString();
|
|
2162
|
+
const cur = raw.endsWith("\n") ? raw : raw + "\n";
|
|
2163
|
+
try {
|
|
2164
|
+
validateTextDelta(cur, delta);
|
|
2165
|
+
}
|
|
2166
|
+
catch (err) {
|
|
2167
|
+
throw new Error(`editText(${id}): ${err instanceof Error ? err.message : String(err)}`);
|
|
2168
|
+
}
|
|
2169
|
+
if (textDeltaIsKeystroke(delta)) {
|
|
2170
|
+
const next = applyDeltaToString(cur, delta);
|
|
2171
|
+
if ((next.endsWith("\n") ? next : next + "\n") === cur) {
|
|
2172
|
+
const beforeContent = readTextFromDelta(text.toDelta());
|
|
2173
|
+
if (sameTextContent(applyTextDelta(beforeContent, delta), beforeContent))
|
|
2174
|
+
continue;
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
else if (!formatChangesLoroText(text.toDelta(), delta))
|
|
2178
|
+
continue;
|
|
2179
|
+
const withTerminator = ensureTrailingNewline(text);
|
|
2180
|
+
try {
|
|
2181
|
+
applyLoroDelta(text, withTerminator, delta);
|
|
2182
|
+
}
|
|
2183
|
+
catch (err) {
|
|
2184
|
+
if (isFatalWasmError(err))
|
|
2185
|
+
throw err;
|
|
2186
|
+
throw new Error(`editText(${id}): ${String(err)}`);
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
catch (err) {
|
|
2191
|
+
error = err;
|
|
2192
|
+
}
|
|
2193
|
+
try {
|
|
2194
|
+
this.commit(label, "local", this.loading ? "sys:load" : "text");
|
|
2195
|
+
}
|
|
2196
|
+
catch (err) {
|
|
2197
|
+
if (error === undefined)
|
|
2198
|
+
error = err;
|
|
2199
|
+
}
|
|
2200
|
+
finally {
|
|
2201
|
+
this.countingPushes = false;
|
|
2202
|
+
entry.textSteps += this.textPushes;
|
|
2203
|
+
}
|
|
2204
|
+
if (error !== undefined) {
|
|
2205
|
+
if (isFatalWasmError(error))
|
|
2206
|
+
this.markDead(error);
|
|
2207
|
+
throw error;
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
2210
|
+
ensureMap(d, key) {
|
|
2211
|
+
const cur = d.get(key);
|
|
2212
|
+
if (cur instanceof LoroMap)
|
|
2213
|
+
return cur;
|
|
2214
|
+
const m = d.setContainer(key, new LoroMap());
|
|
2215
|
+
if (cur && typeof cur === "object")
|
|
2216
|
+
for (const [k, v] of Object.entries(cur))
|
|
2217
|
+
m.set(k, v);
|
|
2218
|
+
return m;
|
|
2219
|
+
}
|
|
2220
|
+
readKey(d, sub, key) {
|
|
2221
|
+
let v;
|
|
2222
|
+
if (sub === undefined)
|
|
2223
|
+
v = d.get(key);
|
|
2224
|
+
else {
|
|
2225
|
+
const child = d.get(sub);
|
|
2226
|
+
v = child instanceof LoroMap ? child.get(key) : child && typeof child === "object" ? child[key] : undefined;
|
|
2227
|
+
}
|
|
2228
|
+
if (!(v instanceof LoroMap))
|
|
2229
|
+
return v;
|
|
2230
|
+
const json = v.toJSON();
|
|
2231
|
+
return sub === undefined && (key === "conn" || key === "anchors" || key === "inst") && Object.keys(json).length === 0 ? undefined : json;
|
|
2232
|
+
}
|
|
2233
|
+
writeKey(entry, n, sub, key, value, def, created) {
|
|
2234
|
+
const d = n.data;
|
|
2235
|
+
const b = this.readKey(d, sub, key);
|
|
2236
|
+
const effBefore = b === undefined && def !== undefined ? def : b;
|
|
2237
|
+
if (deepEq(effBefore, value))
|
|
2238
|
+
return false;
|
|
2239
|
+
let map = d;
|
|
2240
|
+
if (sub !== undefined) {
|
|
2241
|
+
const child = d.get(sub);
|
|
2242
|
+
if (child instanceof LoroMap)
|
|
2243
|
+
map = child;
|
|
2244
|
+
else {
|
|
2245
|
+
map = this.ensureMap(d, sub);
|
|
2246
|
+
created?.();
|
|
2247
|
+
}
|
|
2248
|
+
}
|
|
2249
|
+
const change = { id: n.id, sub, key, before: b, after: value };
|
|
2250
|
+
if (sub === "conn" && isPointKey(key))
|
|
2251
|
+
change.owner = this.readKey(d, "conn", endOfPoint(key));
|
|
2252
|
+
entry.keys.push(change);
|
|
2253
|
+
this.writeKeyValue(map, sub, key, value);
|
|
2254
|
+
return true;
|
|
2255
|
+
}
|
|
2256
|
+
writeKeyValue(map, sub, key, value) {
|
|
2257
|
+
if (value === undefined)
|
|
2258
|
+
map.delete(key);
|
|
2259
|
+
else if (sub === undefined && CONTAINER_KEYS.has(key) && typeof value === "object" && !Array.isArray(value)) {
|
|
2260
|
+
const m = map.setContainer(key, new LoroMap());
|
|
2261
|
+
for (const [k, v] of Object.entries(value))
|
|
2262
|
+
if (v !== undefined)
|
|
2263
|
+
m.set(k, v);
|
|
2264
|
+
}
|
|
2265
|
+
else
|
|
2266
|
+
map.set(key, value);
|
|
2267
|
+
}
|
|
2268
|
+
rebaseXywh(n, oldOrigin, newOrigin, write) {
|
|
2269
|
+
const cur = (n.data.get("xywh") ?? [0, 0, 100, 100]).slice();
|
|
2270
|
+
cur[0] = (cur[0] ?? 0) + oldOrigin.x - newOrigin.x;
|
|
2271
|
+
cur[1] = (cur[1] ?? 0) + oldOrigin.y - newOrigin.y;
|
|
2272
|
+
write(n, undefined, "xywh", cur);
|
|
2273
|
+
}
|
|
2274
|
+
positionOf(n) {
|
|
2275
|
+
const rel = (n.data.get("xywh") ?? [0, 0, 100, 100]).slice();
|
|
2276
|
+
const box = this.liveAbsBox(n);
|
|
2277
|
+
return { rel, abs: box ? [box.x, box.y] : [rel[0] ?? 0, rel[1] ?? 0] };
|
|
2278
|
+
}
|
|
2279
|
+
isTrashMove(m) {
|
|
2280
|
+
return (m.toParent !== null && this.isTrashRoot(m.toParent)) || (m.fromParent !== null && this.isTrashRoot(m.fromParent));
|
|
2281
|
+
}
|
|
2282
|
+
someContainerOrigin(pred) {
|
|
2283
|
+
if (pred({ x: 0, y: 0 }))
|
|
2284
|
+
return true;
|
|
2285
|
+
for (const o of this.objects.values())
|
|
2286
|
+
if (CONTAINER_KINDS.has(o.kind) && pred(o))
|
|
2287
|
+
return true;
|
|
2288
|
+
return false;
|
|
2289
|
+
}
|
|
2290
|
+
ensureText(d) {
|
|
2291
|
+
const cur = d.get("text");
|
|
2292
|
+
if (cur instanceof LoroText)
|
|
2293
|
+
return cur;
|
|
2294
|
+
return d.setContainer("text", new LoroText());
|
|
2295
|
+
}
|
|
2296
|
+
drawnPointOf(ep) {
|
|
2297
|
+
const n = ep.ref ? this.node(ep.ref) : undefined;
|
|
2298
|
+
if (!n || n.data.get("hidden"))
|
|
2299
|
+
return undefined;
|
|
2300
|
+
return this.anchorOf(ep);
|
|
2301
|
+
}
|
|
2302
|
+
anchorOf(ep) {
|
|
2303
|
+
if (!ep.ref)
|
|
2304
|
+
return undefined;
|
|
2305
|
+
const n = this.node(ep.ref);
|
|
2306
|
+
if (!n)
|
|
2307
|
+
return undefined;
|
|
2308
|
+
const kind = n.data.get("kind");
|
|
2309
|
+
if (kind === "connector" || kind === "trash")
|
|
2310
|
+
return undefined;
|
|
2311
|
+
const box = this.liveAbsBox(n);
|
|
2312
|
+
if (!box)
|
|
2313
|
+
return undefined;
|
|
2314
|
+
if (ep.anchor === undefined)
|
|
2315
|
+
return attachmentPoint(box, ep);
|
|
2316
|
+
const raw = n.data.get("anchors");
|
|
2317
|
+
return attachmentPoint({ ...box, anchors: validAnchors(raw instanceof LoroMap ? raw.toJSON() : raw) }, ep);
|
|
2318
|
+
}
|
|
2319
|
+
liveAbsBox(n) {
|
|
2320
|
+
const origins = [];
|
|
2321
|
+
for (let p = n.parent(); p; p = p.parent()) {
|
|
2322
|
+
const kind = p.data.get("kind");
|
|
2323
|
+
if (p.id === this.trashId || kind === "trash")
|
|
2324
|
+
return undefined;
|
|
2325
|
+
if (CONTAINER_KINDS.has(kind ?? ""))
|
|
2326
|
+
origins.push(p.data.get("xywh") ?? [0, 0, 0, 0]);
|
|
2327
|
+
}
|
|
2328
|
+
let ox = 0;
|
|
2329
|
+
let oy = 0;
|
|
2330
|
+
for (let i = origins.length - 1; i >= 0; i--) {
|
|
2331
|
+
ox += origins[i][0] ?? 0;
|
|
2332
|
+
oy += origins[i][1] ?? 0;
|
|
2333
|
+
}
|
|
2334
|
+
const xywh = n.data.get("xywh") ?? [0, 0, 100, 100];
|
|
2335
|
+
return { x: ox + (xywh[0] ?? 0), y: oy + (xywh[1] ?? 0), w: xywh[2] ?? 0, h: xywh[3] ?? 0, rot: n.data.get("rot") ?? 0 };
|
|
2336
|
+
}
|
|
2337
|
+
refreshAnchors(boxChanged, shifted, moved, connTouched, connCreated, setKey) {
|
|
2338
|
+
const conns = new Set(connTouched);
|
|
2339
|
+
if ((this.attachedTo.size || connCreated.size) && (boxChanged.size || shifted.size)) {
|
|
2340
|
+
const targets = new Set(boxChanged);
|
|
2341
|
+
const walked = new Set();
|
|
2342
|
+
const walk = (id) => {
|
|
2343
|
+
if (walked.has(id))
|
|
2344
|
+
return;
|
|
2345
|
+
walked.add(id);
|
|
2346
|
+
for (const c of this.childrenOf.get(id) ?? []) {
|
|
2347
|
+
targets.add(c);
|
|
2348
|
+
walk(c);
|
|
2349
|
+
}
|
|
2350
|
+
};
|
|
2351
|
+
for (const id of shifted)
|
|
2352
|
+
walk(id);
|
|
2353
|
+
if (shifted.size)
|
|
2354
|
+
for (const id of moved) {
|
|
2355
|
+
if (walked.has(id))
|
|
2356
|
+
continue;
|
|
2357
|
+
for (let p = this.node(id)?.parent(); p; p = p.parent()) {
|
|
2358
|
+
if (!shifted.has(p.id))
|
|
2359
|
+
continue;
|
|
2360
|
+
targets.add(id);
|
|
2361
|
+
walk(id);
|
|
2362
|
+
break;
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
for (const t of targets)
|
|
2366
|
+
for (const cid of this.attachedTo.get(t) ?? [])
|
|
2367
|
+
conns.add(cid);
|
|
2368
|
+
for (const cid of connCreated)
|
|
2369
|
+
conns.add(cid);
|
|
2370
|
+
}
|
|
2371
|
+
for (const cid of conns)
|
|
2372
|
+
this.writeAnchors(cid, setKey);
|
|
2373
|
+
}
|
|
2374
|
+
writeAnchors(cid, write, targets, drawn = false) {
|
|
2375
|
+
const cn = this.node(cid);
|
|
2376
|
+
if (!cn)
|
|
2377
|
+
return;
|
|
2378
|
+
const raw = cn.data.get("conn");
|
|
2379
|
+
if (!raw || typeof raw !== "object")
|
|
2380
|
+
return;
|
|
2381
|
+
const read = (k) => (raw instanceof LoroMap ? raw.get(k) : raw[k]);
|
|
2382
|
+
for (const key of ["start", "end"]) {
|
|
2383
|
+
const ep = read(key);
|
|
2384
|
+
if (!ep || typeof ep !== "object" || (targets && (!ep.ref || !targets.has(ep.ref))))
|
|
2385
|
+
continue;
|
|
2386
|
+
const at = drawn ? this.drawnPointOf(ep) : this.anchorOf(ep);
|
|
2387
|
+
if (!at)
|
|
2388
|
+
continue;
|
|
2389
|
+
const cur = read(pointKeyOf(key));
|
|
2390
|
+
if (isPoint(cur) && cur[0] === at[0] && cur[1] === at[1])
|
|
2391
|
+
continue;
|
|
2392
|
+
write(cn, "conn", pointKeyOf(key), at);
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2395
|
+
refreshInverseAnchors(into, acrossOrigins, revived, write) {
|
|
2396
|
+
const boxChanged = new Set();
|
|
2397
|
+
const shifted = new Set(acrossOrigins);
|
|
2398
|
+
const moved = new Set();
|
|
2399
|
+
const connTouched = new Set();
|
|
2400
|
+
for (const c of into.keys) {
|
|
2401
|
+
if (c.sub === "conn")
|
|
2402
|
+
connTouched.add(c.id);
|
|
2403
|
+
else if (c.sub === "anchors")
|
|
2404
|
+
boxChanged.add(c.id);
|
|
2405
|
+
else if (c.sub === undefined) {
|
|
2406
|
+
if (c.key === "xywh") {
|
|
2407
|
+
boxChanged.add(c.id);
|
|
2408
|
+
const b = c.before;
|
|
2409
|
+
const a = c.after;
|
|
2410
|
+
const originMoved = !Array.isArray(b) || !Array.isArray(a) || b[0] !== a[0] || b[1] !== a[1];
|
|
2411
|
+
if (originMoved && CONTAINER_KINDS.has(this.node(c.id)?.data.get("kind") ?? ""))
|
|
2412
|
+
shifted.add(c.id);
|
|
2413
|
+
}
|
|
2414
|
+
else if (c.key === "hidden" || c.key === "rot" || c.key === "anchors")
|
|
2415
|
+
boxChanged.add(c.id);
|
|
2416
|
+
else if (c.key === "kind") {
|
|
2417
|
+
boxChanged.add(c.id);
|
|
2418
|
+
shifted.add(c.id);
|
|
2419
|
+
}
|
|
2420
|
+
else if (c.key === "conn")
|
|
2421
|
+
connTouched.add(c.id);
|
|
2422
|
+
}
|
|
2423
|
+
}
|
|
2424
|
+
for (const m of into.moves)
|
|
2425
|
+
moved.add(m.id);
|
|
2426
|
+
for (const root of revived) {
|
|
2427
|
+
const stack = [root];
|
|
2428
|
+
while (stack.length) {
|
|
2429
|
+
const n = stack.pop();
|
|
2430
|
+
const id = n.id;
|
|
2431
|
+
boxChanged.add(id);
|
|
2432
|
+
if (n.data.get("kind") === "connector")
|
|
2433
|
+
connTouched.add(id);
|
|
2434
|
+
for (const c of n.children() ?? [])
|
|
2435
|
+
stack.push(c);
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
this.refreshAnchors(boxChanged, shifted, moved, connTouched, new Set(), write);
|
|
2439
|
+
}
|
|
2440
|
+
anchorBeforeTrash(id, setKey) {
|
|
2441
|
+
if (!this.attachedTo.size)
|
|
2442
|
+
return;
|
|
2443
|
+
const subtree = new Set();
|
|
2444
|
+
const stack = [id];
|
|
2445
|
+
while (stack.length) {
|
|
2446
|
+
const cur = stack.pop();
|
|
2447
|
+
if (subtree.has(cur))
|
|
2448
|
+
continue;
|
|
2449
|
+
subtree.add(cur);
|
|
2450
|
+
for (const c of this.childrenOf.get(cur) ?? [])
|
|
2451
|
+
stack.push(c);
|
|
2452
|
+
}
|
|
2453
|
+
const conns = new Set();
|
|
2454
|
+
for (const t of subtree)
|
|
2455
|
+
for (const cid of this.attachedTo.get(t) ?? [])
|
|
2456
|
+
conns.add(cid);
|
|
2457
|
+
for (const cid of conns)
|
|
2458
|
+
this.writeAnchors(cid, setKey, subtree, true);
|
|
2459
|
+
}
|
|
2460
|
+
canUndo() {
|
|
2461
|
+
return this.undoStack.length > 0;
|
|
2462
|
+
}
|
|
2463
|
+
canRedo() {
|
|
2464
|
+
return this.redoStack.length > 0;
|
|
2465
|
+
}
|
|
2466
|
+
applyInverse(e, into, origin) {
|
|
2467
|
+
const siblings = this.mutationSiblings();
|
|
2468
|
+
const write = (n, sub, key, value) => this.writeKey(into, n, sub, key, value);
|
|
2469
|
+
const lastXywh = new Map();
|
|
2470
|
+
for (const c of e.keys)
|
|
2471
|
+
if (c.sub === undefined && c.key === "xywh")
|
|
2472
|
+
lastXywh.set(c.id, c);
|
|
2473
|
+
const lastReparent = new Map();
|
|
2474
|
+
for (const m of e.moves)
|
|
2475
|
+
if (!this.isTrashMove(m))
|
|
2476
|
+
lastReparent.set(m.id, m);
|
|
2477
|
+
const skipUnit = new Set();
|
|
2478
|
+
for (const [id, m] of lastReparent) {
|
|
2479
|
+
const k = lastXywh.get(id);
|
|
2480
|
+
if (!k)
|
|
2481
|
+
continue;
|
|
2482
|
+
const n = this.node(id);
|
|
2483
|
+
if (!n || !deepEq(this.readKey(n.data, undefined, "xywh"), k.after) || this.parentIdOf(n) !== m.toParent)
|
|
2484
|
+
skipUnit.add(id);
|
|
2485
|
+
}
|
|
2486
|
+
const supersededKeys = new Set();
|
|
2487
|
+
for (const c of [...e.keys].reverse()) {
|
|
2488
|
+
if (c.sub === undefined && c.key === "xywh" && skipUnit.has(c.id))
|
|
2489
|
+
continue;
|
|
2490
|
+
const keyId = `${c.id}\0${c.sub ?? ""}\0${c.key}`;
|
|
2491
|
+
if (supersededKeys.has(keyId))
|
|
2492
|
+
continue;
|
|
2493
|
+
const n = this.node(c.id);
|
|
2494
|
+
if (!n)
|
|
2495
|
+
continue;
|
|
2496
|
+
if (!deepEq(this.readKey(n.data, c.sub, c.key), c.after)) {
|
|
2497
|
+
supersededKeys.add(keyId);
|
|
2498
|
+
continue;
|
|
2499
|
+
}
|
|
2500
|
+
if (c.sub === "conn" && isPointKey(c.key) && !deepEq(this.readKey(n.data, "conn", endOfPoint(c.key)), c.owner)) {
|
|
2501
|
+
supersededKeys.add(keyId);
|
|
2502
|
+
continue;
|
|
2503
|
+
}
|
|
2504
|
+
write(n, c.sub, c.key, c.before);
|
|
2505
|
+
}
|
|
2506
|
+
const revived = [];
|
|
2507
|
+
const acrossOrigins = new Set();
|
|
2508
|
+
for (const m of [...e.moves].reverse()) {
|
|
2509
|
+
if (skipUnit.has(m.id))
|
|
2510
|
+
continue;
|
|
2511
|
+
const n = this.node(m.id);
|
|
2512
|
+
if (!n)
|
|
2513
|
+
continue;
|
|
2514
|
+
if (this.parentIdOf(n) !== m.toParent)
|
|
2515
|
+
continue;
|
|
2516
|
+
const target = m.fromParent ? this.node(m.fromParent) : undefined;
|
|
2517
|
+
if (m.fromParent && !target)
|
|
2518
|
+
continue;
|
|
2519
|
+
const fromTrash = m.toParent !== null && this.isTrashRoot(m.toParent);
|
|
2520
|
+
const toTrash = m.fromParent !== null && this.isTrashRoot(m.fromParent);
|
|
2521
|
+
const record = { id: m.id, fromParent: this.parentIdOf(n), fromIndex: n.index() ?? 0, toParent: m.fromParent };
|
|
2522
|
+
if (toTrash)
|
|
2523
|
+
record.at = this.positionOf(n);
|
|
2524
|
+
const oldOrigin = fromTrash || toTrash ? undefined : this.absOriginOfParent(n);
|
|
2525
|
+
const siblingCount = siblings.count(target) - (record.fromParent === m.fromParent ? 1 : 0);
|
|
2526
|
+
try {
|
|
2527
|
+
n.move(target, Math.min(m.fromIndex, siblingCount));
|
|
2528
|
+
}
|
|
2529
|
+
catch (err) {
|
|
2530
|
+
void err;
|
|
2531
|
+
continue;
|
|
2532
|
+
}
|
|
2533
|
+
siblings.moved(m.id, record.fromParent, m.fromParent, n.index() ?? 0);
|
|
2534
|
+
into.moves.push(record);
|
|
2535
|
+
if (fromTrash) {
|
|
2536
|
+
revived.push(n);
|
|
2537
|
+
if (m.at && !deepEq(n.data.get("xywh"), m.at.rel)) {
|
|
2538
|
+
const cur = n.data.get("xywh") ?? [0, 0, 100, 100];
|
|
2539
|
+
const [ax, ay] = m.at.abs;
|
|
2540
|
+
const denotes = (o) => o.x + (cur[0] ?? 0) === ax && o.y + (cur[1] ?? 0) === ay;
|
|
2541
|
+
const origin = this.absOriginOfParent(n);
|
|
2542
|
+
if (!denotes(origin) && this.someContainerOrigin(denotes))
|
|
2543
|
+
write(n, undefined, "xywh", [ax - origin.x, ay - origin.y, cur[2] ?? 0, cur[3] ?? 0]);
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
else if (oldOrigin) {
|
|
2547
|
+
const newOrigin = this.absOriginOfParent(n);
|
|
2548
|
+
if (oldOrigin.x !== newOrigin.x || oldOrigin.y !== newOrigin.y) {
|
|
2549
|
+
if (!lastXywh.has(m.id))
|
|
2550
|
+
this.rebaseXywh(n, oldOrigin, newOrigin, write);
|
|
2551
|
+
acrossOrigins.add(m.id);
|
|
2552
|
+
}
|
|
2553
|
+
}
|
|
2554
|
+
}
|
|
2555
|
+
for (const id of e.creates) {
|
|
2556
|
+
const n = this.node(id);
|
|
2557
|
+
if (!n || this.inTrash(n))
|
|
2558
|
+
continue;
|
|
2559
|
+
const record = { id, fromParent: this.parentIdOf(n), fromIndex: n.index() ?? 0, toParent: this.trashId, at: this.positionOf(n) };
|
|
2560
|
+
try {
|
|
2561
|
+
n.move(this.node(this.trashId));
|
|
2562
|
+
}
|
|
2563
|
+
catch (err) {
|
|
2564
|
+
void err;
|
|
2565
|
+
continue;
|
|
2566
|
+
}
|
|
2567
|
+
siblings.moved(id, record.fromParent, this.trashId, n.index() ?? 0);
|
|
2568
|
+
into.moves.push(record);
|
|
2569
|
+
}
|
|
2570
|
+
try {
|
|
2571
|
+
this.refreshInverseAnchors(into, acrossOrigins, revived, write);
|
|
2572
|
+
}
|
|
2573
|
+
finally {
|
|
2574
|
+
this.commit(e.label, origin, `diagram:${origin}`);
|
|
2575
|
+
}
|
|
2576
|
+
}
|
|
2577
|
+
walk(direction, e) {
|
|
2578
|
+
const inverse = { serial: e.serial, label: e.label, keys: [], moves: [], creates: [], textSteps: 0 };
|
|
2579
|
+
this.applyInverse(e, inverse, direction);
|
|
2580
|
+
const forward = direction === "undo" ? () => this.undoManager.undo() : () => this.undoManager.redo();
|
|
2581
|
+
const back = direction === "undo" ? () => this.undoManager.redo() : () => this.undoManager.undo();
|
|
2582
|
+
const was = { applying: this.applying, label: this.pendingLabel, origin: this.pendingOrigin };
|
|
2583
|
+
this.applying = true;
|
|
2584
|
+
this.pendingLabel = e.label;
|
|
2585
|
+
this.pendingOrigin = direction;
|
|
2586
|
+
this.countingPushes = true;
|
|
2587
|
+
this.textPushes = 0;
|
|
2588
|
+
this.pushSerial = inverse.serial;
|
|
2589
|
+
try {
|
|
2590
|
+
for (let remaining = e.textSteps; remaining > 0;) {
|
|
2591
|
+
this.pops = [];
|
|
2592
|
+
if (!forward())
|
|
2593
|
+
break;
|
|
2594
|
+
const applied = this.pops[this.pops.length - 1];
|
|
2595
|
+
if (applied === undefined || applied === e.serial) {
|
|
2596
|
+
remaining -= Math.max(1, this.pops.length);
|
|
2597
|
+
continue;
|
|
2598
|
+
}
|
|
2599
|
+
this.countingPushes = false;
|
|
2600
|
+
this.pushSerial = applied;
|
|
2601
|
+
back();
|
|
2602
|
+
this.textPushes--;
|
|
2603
|
+
break;
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
finally {
|
|
2607
|
+
this.countingPushes = false;
|
|
2608
|
+
this.applying = was.applying;
|
|
2609
|
+
this.pendingLabel = was.label;
|
|
2610
|
+
this.pendingOrigin = was.origin;
|
|
2611
|
+
}
|
|
2612
|
+
inverse.textSteps = Math.max(0, this.textPushes);
|
|
2613
|
+
return inverse;
|
|
2614
|
+
}
|
|
2615
|
+
undo() {
|
|
2616
|
+
if (this.dead)
|
|
2617
|
+
throw new Error("LoroPageDoc: document is dead (a WASM trap killed it); reload");
|
|
2618
|
+
if (this.applying)
|
|
2619
|
+
throw new Error("LoroPageDoc: cannot undo from within a change notification");
|
|
2620
|
+
const e = this.undoStack.pop();
|
|
2621
|
+
if (!e)
|
|
2622
|
+
return false;
|
|
2623
|
+
this.lastTyping = null;
|
|
2624
|
+
return this.op(() => {
|
|
2625
|
+
try {
|
|
2626
|
+
this.redoStack.push(this.walk("undo", e));
|
|
2627
|
+
}
|
|
2628
|
+
catch (err) {
|
|
2629
|
+
this.undoStack.push(e);
|
|
2630
|
+
if (isFatalWasmError(err))
|
|
2631
|
+
this.markDead(err);
|
|
2632
|
+
throw err;
|
|
2633
|
+
}
|
|
2634
|
+
return true;
|
|
2635
|
+
});
|
|
2636
|
+
}
|
|
2637
|
+
endTyping() {
|
|
2638
|
+
this.lastTyping = null;
|
|
2639
|
+
}
|
|
2640
|
+
redo() {
|
|
2641
|
+
if (this.dead)
|
|
2642
|
+
throw new Error("LoroPageDoc: document is dead (a WASM trap killed it); reload");
|
|
2643
|
+
if (this.applying)
|
|
2644
|
+
throw new Error("LoroPageDoc: cannot redo from within a change notification");
|
|
2645
|
+
const e = this.redoStack.pop();
|
|
2646
|
+
if (!e)
|
|
2647
|
+
return false;
|
|
2648
|
+
this.lastTyping = null;
|
|
2649
|
+
return this.op(() => {
|
|
2650
|
+
try {
|
|
2651
|
+
this.pushUndo(this.walk("redo", e));
|
|
2652
|
+
}
|
|
2653
|
+
catch (err) {
|
|
2654
|
+
this.redoStack.push(e);
|
|
2655
|
+
if (isFatalWasmError(err))
|
|
2656
|
+
this.markDead(err);
|
|
2657
|
+
throw err;
|
|
2658
|
+
}
|
|
2659
|
+
return true;
|
|
2660
|
+
});
|
|
2661
|
+
}
|
|
2662
|
+
}
|