@daasboard/doc 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,688 @@
1
+ import { contentToFlat, flatToContent, flatToLoroDelta, isCodePointBoundary, sameAttrs, snapDown, snapUp, PARA_KEYS, TEXT_CASES } from "./text.js";
2
+ const PARA_KEY_SET = new Set(PARA_KEYS);
3
+ export const isClearValue = (v) => v === undefined || v === null;
4
+ export function emptyTextContent() {
5
+ return { paragraphs: [{ runs: [] }] };
6
+ }
7
+ export function textDeltaIsKeystroke(delta) {
8
+ for (const op of delta) {
9
+ if ("insert" in op && op.insert.length > 0)
10
+ return true;
11
+ if ("delete" in op && op.delete > 0)
12
+ return true;
13
+ }
14
+ return false;
15
+ }
16
+ export function textDeltaIsEmpty(delta) {
17
+ for (const op of delta) {
18
+ if ("insert" in op) {
19
+ if (op.insert.length > 0)
20
+ return false;
21
+ }
22
+ else if ("delete" in op) {
23
+ if (op.delete > 0)
24
+ return false;
25
+ }
26
+ else if (op.attributes && Object.keys(op.attributes).length > 0)
27
+ return false;
28
+ }
29
+ return true;
30
+ }
31
+ export function textDeltaOnlyClears(delta) {
32
+ for (const op of delta) {
33
+ if ("insert" in op) {
34
+ if (op.insert.length > 0)
35
+ return false;
36
+ }
37
+ else if ("delete" in op) {
38
+ if (op.delete > 0)
39
+ return false;
40
+ }
41
+ else if (op.attributes)
42
+ for (const v of Object.values(op.attributes))
43
+ if (!isClearValue(v))
44
+ return false;
45
+ }
46
+ return true;
47
+ }
48
+ export function sameTextContent(a, b) {
49
+ const fa = contentToFlat(a);
50
+ const fb = contentToFlat(b);
51
+ if (fa.text !== fb.text)
52
+ return false;
53
+ for (let i = 0; i < fa.text.length; i++)
54
+ if (!sameAttrs(fa.attrs[i], fb.attrs[i]))
55
+ return false;
56
+ return true;
57
+ }
58
+ export function applyDeltaToString(text, delta) {
59
+ let out = "";
60
+ let pos = 0;
61
+ for (const op of delta) {
62
+ if ("insert" in op)
63
+ out += op.insert;
64
+ else if ("delete" in op)
65
+ pos += op.delete;
66
+ else {
67
+ out += text.slice(pos, pos + op.retain);
68
+ pos += op.retain;
69
+ }
70
+ }
71
+ return out + text.slice(pos);
72
+ }
73
+ export function textAfterDelta(text, delta) {
74
+ const next = applyDeltaToString(text, delta);
75
+ return next.endsWith("\n") ? next : next + "\n";
76
+ }
77
+ function canonicalAttr(k, v) {
78
+ if (isClearValue(v))
79
+ return undefined;
80
+ switch (k) {
81
+ case "italic":
82
+ case "underline":
83
+ case "strike":
84
+ return typeof v === "boolean" ? v : undefined;
85
+ case "checked":
86
+ return v === true ? true : undefined;
87
+ case "color":
88
+ case "family":
89
+ case "link":
90
+ case "align":
91
+ return typeof v === "string" && v.length ? v : undefined;
92
+ case "textCase":
93
+ return TEXT_CASES.has(v) ? v : undefined;
94
+ case "weight":
95
+ case "size":
96
+ case "letterSpacing":
97
+ case "lineHeight":
98
+ case "start":
99
+ return typeof v === "number" ? v : undefined;
100
+ case "list":
101
+ return LISTS.has(v) ? v : undefined;
102
+ case "indent":
103
+ return typeof v === "number" && v ? v : undefined;
104
+ default:
105
+ return undefined;
106
+ }
107
+ }
108
+ export function formatChangesLoroText(ops, delta) {
109
+ const spans = [];
110
+ let len = 0;
111
+ for (const op of ops) {
112
+ const s = op.insert;
113
+ spans.push({ start: len, end: len + s.length, text: s, attrs: (op.attributes ?? {}) });
114
+ len += s.length;
115
+ }
116
+ if (!len || spans[spans.length - 1].text.charCodeAt(spans[spans.length - 1].text.length - 1) !== 10)
117
+ spans.push({ start: len, end: len + 1, text: "\n", attrs: {} });
118
+ let pos = 0;
119
+ for (const op of delta) {
120
+ if ("insert" in op) {
121
+ if (op.insert.length)
122
+ return true;
123
+ continue;
124
+ }
125
+ if ("delete" in op) {
126
+ if (op.delete > 0)
127
+ return true;
128
+ continue;
129
+ }
130
+ const a = op.attributes;
131
+ const end = pos + op.retain;
132
+ if (a)
133
+ for (const span of spans) {
134
+ if (span.end <= pos)
135
+ continue;
136
+ if (span.start >= end)
137
+ break;
138
+ const seg = span.text.slice(Math.max(pos, span.start) - span.start, Math.min(end, span.end) - span.start);
139
+ const nl = seg.includes("\n");
140
+ const chars = seg.length > (nl ? seg.split("\n").length - 1 : 0);
141
+ for (const [k, v] of Object.entries(a)) {
142
+ if ((PARA_KEY_SET.has(k) ? nl : chars) && canonicalAttr(k, span.attrs[k]) !== canonicalAttr(k, v))
143
+ return true;
144
+ }
145
+ }
146
+ pos = end;
147
+ }
148
+ return false;
149
+ }
150
+ export function rewriteDelta(cur, target) {
151
+ const a = cur.text;
152
+ const b = target.text;
153
+ const max = Math.min(a.length, b.length);
154
+ let p = 0;
155
+ while (p < max && a.charCodeAt(p) === b.charCodeAt(p) && sameAttrs(cur.attrs[p], target.attrs[p]))
156
+ p++;
157
+ p = snapDown(a, p);
158
+ let s = 0;
159
+ while (s < max - p && a.charCodeAt(a.length - 1 - s) === b.charCodeAt(b.length - 1 - s) && sameAttrs(cur.attrs[a.length - 1 - s], target.attrs[b.length - 1 - s]))
160
+ s++;
161
+ s = a.length - snapUp(a, a.length - s);
162
+ const aEnd = a.length - s;
163
+ const bEnd = b.length - s;
164
+ if (aEnd === p && bEnd === p)
165
+ return [];
166
+ const out = [];
167
+ if (p)
168
+ out.push({ retain: p });
169
+ if (aEnd > p)
170
+ out.push({ delete: aEnd - p });
171
+ for (const op of flatToLoroDelta({ text: b.slice(p, bEnd), attrs: target.attrs.slice(p, bEnd) })) {
172
+ out.push(op.attributes ? { insert: op.insert, attributes: op.attributes } : { insert: op.insert });
173
+ }
174
+ return out;
175
+ }
176
+ export function deltaLength(delta) {
177
+ let n = 0;
178
+ for (const op of delta) {
179
+ if ("retain" in op)
180
+ n += op.retain;
181
+ else if ("delete" in op)
182
+ n += op.delete;
183
+ }
184
+ return n;
185
+ }
186
+ function hasUnpairedSurrogate(s) {
187
+ for (let i = 0; i < s.length; i++) {
188
+ const c = s.charCodeAt(i);
189
+ if (c >= 0xd800 && c <= 0xdbff) {
190
+ const next = i + 1 < s.length ? s.charCodeAt(i + 1) : 0;
191
+ if (!(next >= 0xdc00 && next <= 0xdfff))
192
+ return true;
193
+ i++;
194
+ }
195
+ else if (c >= 0xdc00 && c <= 0xdfff)
196
+ return true;
197
+ }
198
+ return false;
199
+ }
200
+ const countOf = (v, what, i) => {
201
+ if (typeof v !== "number" || !Number.isInteger(v) || v < 0)
202
+ throw new Error(`text delta op ${i}: ${what} must be a non-negative integer, got ${String(v)}`);
203
+ return v;
204
+ };
205
+ const ALIGNS = new Set(["left", "center", "right", "justify"]);
206
+ const LISTS = new Set(["bullet", "number", "task"]);
207
+ export function validateTextAttributes(attributes, i) {
208
+ if (!attributes)
209
+ return;
210
+ for (const [k, v] of Object.entries(attributes)) {
211
+ if (isClearValue(v))
212
+ continue;
213
+ const bad = (what) => new Error(`text delta op ${i}: attribute ${k} must be ${what}, got ${JSON.stringify(v)}`);
214
+ switch (k) {
215
+ case "italic":
216
+ case "underline":
217
+ case "strike":
218
+ if (typeof v !== "boolean")
219
+ throw bad("true, false or a clear");
220
+ break;
221
+ case "checked":
222
+ if (v !== true)
223
+ throw bad("true or a clear");
224
+ break;
225
+ case "weight":
226
+ if (typeof v !== "number" || !Number.isFinite(v) || v < 100 || v > 900)
227
+ throw bad("a number from 100 to 900");
228
+ break;
229
+ case "size":
230
+ case "lineHeight":
231
+ if (typeof v !== "number" || !Number.isFinite(v) || v <= 0)
232
+ throw bad("a positive finite number");
233
+ break;
234
+ case "letterSpacing":
235
+ if (typeof v !== "number" || !Number.isFinite(v))
236
+ throw bad("a finite number");
237
+ break;
238
+ case "color":
239
+ case "family":
240
+ case "link":
241
+ if (typeof v !== "string" || v.length === 0)
242
+ throw bad("a non-empty string");
243
+ break;
244
+ case "align":
245
+ if (typeof v !== "string" || !ALIGNS.has(v))
246
+ throw bad("left, center, right or justify");
247
+ break;
248
+ case "textCase":
249
+ if (!TEXT_CASES.has(v))
250
+ throw bad("none, upper or lower");
251
+ break;
252
+ case "list":
253
+ if (!LISTS.has(v))
254
+ throw bad("bullet, number or task");
255
+ break;
256
+ case "indent":
257
+ case "start":
258
+ if (typeof v !== "number" || !Number.isInteger(v) || v < 0)
259
+ throw bad("a non-negative integer");
260
+ break;
261
+ default:
262
+ throw bad("a known key");
263
+ }
264
+ }
265
+ }
266
+ export function validateTextDelta(text, delta) {
267
+ let pos = 0;
268
+ const boundary = (at, i, what) => {
269
+ if (!isCodePointBoundary(text, at))
270
+ throw new Error(`text delta op ${i}: ${what} at ${at} would split a surrogate pair`);
271
+ };
272
+ for (let i = 0; i < delta.length; i++) {
273
+ const op = delta[i];
274
+ if ("insert" in op) {
275
+ if (typeof op.insert !== "string")
276
+ throw new Error(`text delta op ${i}: insert must be a string`);
277
+ if (hasUnpairedSurrogate(op.insert))
278
+ throw new Error(`text delta op ${i}: inserted text contains an unpaired surrogate`);
279
+ boundary(pos, i, "insert");
280
+ validateTextAttributes(op.attributes, i);
281
+ }
282
+ else if ("delete" in op) {
283
+ const n = countOf(op.delete, "delete", i);
284
+ if (pos + n > text.length)
285
+ throw new Error(`text delta op ${i}: delete ${n} at ${pos} runs past the end (length ${text.length})`);
286
+ boundary(pos, i, "delete");
287
+ boundary(pos + n, i, "delete end");
288
+ pos += n;
289
+ }
290
+ else if ("retain" in op) {
291
+ const n = countOf(op.retain, "retain", i);
292
+ if (pos + n > text.length)
293
+ throw new Error(`text delta op ${i}: retain ${n} at ${pos} runs past the end (length ${text.length})`);
294
+ boundary(pos, i, "retain");
295
+ boundary(pos + n, i, "retain end");
296
+ validateTextAttributes(op.attributes, i);
297
+ pos += n;
298
+ }
299
+ else
300
+ throw new Error(`text delta op ${i}: not an insert, delete, or retain: ${JSON.stringify(op)}`);
301
+ }
302
+ }
303
+ function splitInsertAttrs(attributes) {
304
+ const inline = {};
305
+ const para = {};
306
+ if (attributes)
307
+ for (const [k, v] of Object.entries(attributes)) {
308
+ if (isClearValue(v))
309
+ continue;
310
+ (PARA_KEY_SET.has(k) ? para : inline)[k] = v;
311
+ }
312
+ return { inline, para };
313
+ }
314
+ function partitionRetainAttrs(attributes) {
315
+ const inline = {};
316
+ const para = {};
317
+ for (const [k, v] of Object.entries(attributes))
318
+ (PARA_KEY_SET.has(k) ? para : inline)[k] = v;
319
+ return { inline, para };
320
+ }
321
+ const hasKeys = (a) => Object.keys(a).length > 0;
322
+ export function applyTextDelta(content, delta) {
323
+ const flat = contentToFlat(content);
324
+ validateTextDelta(flat.text, delta);
325
+ return flatToContent(terminated(applyDeltaToFlat(flat, delta)));
326
+ }
327
+ function terminated(f) {
328
+ return f.text.endsWith("\n") ? f : { text: f.text + "\n", attrs: f.attrs.concat([{}]) };
329
+ }
330
+ function applyDeltaToFlat(flat, delta) {
331
+ let text = flat.text;
332
+ let attrs = flat.attrs;
333
+ let pos = 0;
334
+ for (const op of delta) {
335
+ if ("insert" in op) {
336
+ const s = op.insert;
337
+ if (!s.length)
338
+ continue;
339
+ const { inline, para } = splitInsertAttrs(op.attributes);
340
+ const ins = new Array(s.length);
341
+ for (let i = 0; i < s.length; i++)
342
+ ins[i] = s.charCodeAt(i) === 10 ? para : inline;
343
+ text = text.slice(0, pos) + s + text.slice(pos);
344
+ attrs = attrs.slice(0, pos).concat(ins, attrs.slice(pos));
345
+ pos += s.length;
346
+ }
347
+ else if ("delete" in op) {
348
+ text = text.slice(0, pos) + text.slice(pos + op.delete);
349
+ attrs = attrs.slice(0, pos).concat(attrs.slice(pos + op.delete));
350
+ }
351
+ else {
352
+ const a = op.attributes;
353
+ if (a && Object.keys(a).length > 0) {
354
+ if (attrs === flat.attrs)
355
+ attrs = attrs.slice();
356
+ let lastIn;
357
+ let lastOut;
358
+ let lastNl = false;
359
+ for (let i = pos; i < pos + op.retain; i++) {
360
+ const cur = attrs[i];
361
+ const nl = text.charCodeAt(i) === 10;
362
+ if (cur === lastIn && nl === lastNl) {
363
+ attrs[i] = lastOut;
364
+ continue;
365
+ }
366
+ const out = { ...cur };
367
+ for (const [k, v] of Object.entries(a)) {
368
+ if (PARA_KEY_SET.has(k) !== nl)
369
+ continue;
370
+ if (isClearValue(v))
371
+ delete out[k];
372
+ else
373
+ out[k] = v;
374
+ }
375
+ lastIn = cur;
376
+ lastNl = nl;
377
+ lastOut = out;
378
+ attrs[i] = out;
379
+ }
380
+ }
381
+ pos += op.retain;
382
+ }
383
+ }
384
+ return { text, attrs };
385
+ }
386
+ export function flatTextOf(content) {
387
+ if (content.paragraphs.length === 0)
388
+ return "\n";
389
+ let s = "";
390
+ for (const p of content.paragraphs) {
391
+ for (const r of p.runs)
392
+ s += r.text;
393
+ s += "\n";
394
+ }
395
+ return s;
396
+ }
397
+ const readbacks = new WeakMap();
398
+ function sameRun(a, b) {
399
+ const ka = Object.keys(a);
400
+ const kb = Object.keys(b);
401
+ if (ka.length !== kb.length)
402
+ return false;
403
+ for (let i = 0; i < ka.length; i++)
404
+ if (ka[i] !== kb[i] || a[ka[i]] !== b[ka[i]])
405
+ return false;
406
+ return true;
407
+ }
408
+ function sameParagraph(a, b) {
409
+ if (a === b)
410
+ return true;
411
+ const ka = Object.keys(a);
412
+ const kb = Object.keys(b);
413
+ if (ka.length !== kb.length)
414
+ return false;
415
+ for (let i = 0; i < ka.length; i++) {
416
+ const k = ka[i];
417
+ if (k !== kb[i])
418
+ return false;
419
+ if (k === "runs") {
420
+ if (a.runs.length !== b.runs.length)
421
+ return false;
422
+ for (let j = 0; j < a.runs.length; j++)
423
+ if (!sameRun(a.runs[j], b.runs[j]))
424
+ return false;
425
+ }
426
+ else if (a[k] !== b[k])
427
+ return false;
428
+ }
429
+ return true;
430
+ }
431
+ function readback(p) {
432
+ let r = readbacks.get(p);
433
+ if (r !== undefined)
434
+ return r;
435
+ const flat = contentToFlat({ paragraphs: [p] });
436
+ if (flat.text.indexOf("\n") !== flat.text.length - 1)
437
+ r = null;
438
+ else {
439
+ const read = flatToContent(flat).paragraphs[0];
440
+ if (sameParagraph(read, p))
441
+ r = { paragraph: p, same: true, flat: true };
442
+ else
443
+ r = { paragraph: read, same: false, flat: sameTextContent({ paragraphs: [read] }, { paragraphs: [p] }) };
444
+ }
445
+ readbacks.set(p, r);
446
+ return r;
447
+ }
448
+ export function applyTextDeltaToParagraphs(content, delta) {
449
+ const reference = () => {
450
+ const after = applyTextDelta(content, delta);
451
+ return { content: after, changed: !sameTextContent(after, content) };
452
+ };
453
+ const paragraphs = content.paragraphs.length ? content.paragraphs : [{ runs: [] }];
454
+ const n = paragraphs.length;
455
+ const reads = new Array(n);
456
+ const starts = new Array(n + 1);
457
+ let off = 0;
458
+ let unstable = false;
459
+ for (let i = 0; i < n; i++) {
460
+ const r = readback(paragraphs[i]);
461
+ if (!r)
462
+ return reference();
463
+ reads[i] = r;
464
+ if (!r.flat)
465
+ unstable = true;
466
+ starts[i] = off;
467
+ for (const run of paragraphs[i].runs)
468
+ off += run.text.length;
469
+ off += 1;
470
+ }
471
+ starts[n] = off;
472
+ validateTextDelta(flatTextOf(content), delta);
473
+ let lo = -1;
474
+ let hi = 0;
475
+ let first = 0;
476
+ let last = -1;
477
+ let pos = 0;
478
+ for (let i = 0; i < delta.length; i++) {
479
+ const op = delta[i];
480
+ let touches;
481
+ let end = pos;
482
+ if ("insert" in op)
483
+ touches = op.insert.length > 0;
484
+ else if ("delete" in op) {
485
+ touches = op.delete > 0;
486
+ end = pos + op.delete;
487
+ }
488
+ else {
489
+ touches = !!op.attributes && Object.keys(op.attributes).length > 0;
490
+ end = pos + op.retain;
491
+ }
492
+ if (touches) {
493
+ if (lo < 0) {
494
+ lo = pos;
495
+ first = i;
496
+ }
497
+ last = i;
498
+ hi = end;
499
+ }
500
+ pos = end;
501
+ }
502
+ const paragraphOf = (at) => {
503
+ if (at >= off)
504
+ return n - 1;
505
+ let a = 0;
506
+ let b = n - 1;
507
+ while (a < b) {
508
+ const mid = (a + b + 1) >> 1;
509
+ if (starts[mid] <= at)
510
+ a = mid;
511
+ else
512
+ b = mid - 1;
513
+ }
514
+ return a;
515
+ };
516
+ const kept = (from, to) => {
517
+ const out = new Array(to - from);
518
+ for (let i = from; i < to; i++)
519
+ out[i - from] = reads[i].paragraph;
520
+ return out;
521
+ };
522
+ if (lo < 0) {
523
+ const same = reads.every((r) => r.same);
524
+ return { content: same && content.paragraphs.length ? content : { paragraphs: kept(0, n) }, changed: unstable };
525
+ }
526
+ const a = paragraphOf(lo);
527
+ const b = Math.max(a, paragraphOf(hi));
528
+ const window = { paragraphs: paragraphs.slice(a, b + 1) };
529
+ const local = lo > starts[a] ? [{ retain: lo - starts[a] }] : [];
530
+ for (let i = first; i <= last; i++)
531
+ local.push(delta[i]);
532
+ const flat = applyDeltaToFlat(contentToFlat(window), local);
533
+ if (!flat.text.length && a > 0)
534
+ return { content: { paragraphs: kept(0, a) }, changed: true };
535
+ const edited = flatToContent(terminated(flat)).paragraphs;
536
+ const changed = unstable || !sameTextContent(window, { paragraphs: edited });
537
+ return { content: { paragraphs: kept(0, a).concat(edited, kept(b + 1, n)) }, changed };
538
+ }
539
+ export function toLoroDelta(delta) {
540
+ const out = [];
541
+ for (const op of delta) {
542
+ if ("insert" in op) {
543
+ if (!op.insert.length)
544
+ continue;
545
+ const attributes = {};
546
+ let any = false;
547
+ if (op.attributes)
548
+ for (const [k, v] of Object.entries(op.attributes)) {
549
+ if (isClearValue(v))
550
+ continue;
551
+ attributes[k] = v;
552
+ any = true;
553
+ }
554
+ out.push(any ? { insert: op.insert, attributes } : { insert: op.insert });
555
+ }
556
+ else if ("delete" in op) {
557
+ if (op.delete > 0)
558
+ out.push({ delete: op.delete });
559
+ }
560
+ else {
561
+ if (op.retain <= 0)
562
+ continue;
563
+ const attributes = {};
564
+ let any = false;
565
+ if (op.attributes)
566
+ for (const [k, v] of Object.entries(op.attributes)) {
567
+ attributes[k] = isClearValue(v) ? null : v;
568
+ any = true;
569
+ }
570
+ out.push(any ? { retain: op.retain, attributes } : { retain: op.retain });
571
+ }
572
+ }
573
+ while (out.length) {
574
+ const last = out[out.length - 1];
575
+ if (last.retain !== undefined && !last.attributes)
576
+ out.pop();
577
+ else
578
+ break;
579
+ }
580
+ return out;
581
+ }
582
+ export function splitDeleteAtAstral(text, pos, n) {
583
+ const out = [];
584
+ let i = pos;
585
+ const end = pos + n;
586
+ let stretch = 0;
587
+ while (i < end) {
588
+ const c = text.charCodeAt(i);
589
+ const astral = c >= 0xd800 && c <= 0xdbff && i + 1 < end;
590
+ if (astral) {
591
+ if (stretch)
592
+ out.push(stretch);
593
+ stretch = 0;
594
+ out.push(2);
595
+ i += 2;
596
+ }
597
+ else {
598
+ stretch++;
599
+ i++;
600
+ }
601
+ }
602
+ if (stretch)
603
+ out.push(stretch);
604
+ return out;
605
+ }
606
+ export function applyLoroDelta(t, cur, delta) {
607
+ let pending = [];
608
+ let pos = 0;
609
+ let src = 0;
610
+ const flush = () => {
611
+ const ops = toLoroDelta(pending);
612
+ if (ops.length)
613
+ t.applyDelta(ops);
614
+ pending = pos ? [{ retain: pos }] : [];
615
+ };
616
+ const pushRetain = (from, to, attributes) => {
617
+ if (!attributes) {
618
+ if (to > from)
619
+ pending.push({ retain: to - from });
620
+ return;
621
+ }
622
+ const { inline, para } = partitionRetainAttrs(attributes);
623
+ let i = from;
624
+ while (i < to) {
625
+ if (cur.charCodeAt(i) === 10) {
626
+ pending.push(hasKeys(para) ? { retain: 1, attributes: para } : { retain: 1 });
627
+ i++;
628
+ }
629
+ else {
630
+ let j = i;
631
+ while (j < to && cur.charCodeAt(j) !== 10)
632
+ j++;
633
+ pending.push(hasKeys(inline) ? { retain: j - i, attributes: inline } : { retain: j - i });
634
+ i = j;
635
+ }
636
+ }
637
+ };
638
+ const pushInsert = (s, attributes) => {
639
+ const { inline, para } = splitInsertAttrs(attributes);
640
+ let i = 0;
641
+ while (i < s.length) {
642
+ if (s.charCodeAt(i) === 10) {
643
+ pending.push(hasKeys(para) ? { insert: "\n", attributes: para } : { insert: "\n" });
644
+ i++;
645
+ }
646
+ else {
647
+ let j = i;
648
+ while (j < s.length && s.charCodeAt(j) !== 10)
649
+ j++;
650
+ pending.push(hasKeys(inline) ? { insert: s.slice(i, j), attributes: inline } : { insert: s.slice(i, j) });
651
+ i = j;
652
+ }
653
+ }
654
+ };
655
+ for (const op of delta) {
656
+ if ("insert" in op) {
657
+ pushInsert(op.insert, op.attributes);
658
+ pos += op.insert.length;
659
+ }
660
+ else if ("retain" in op) {
661
+ pushRetain(src, src + op.retain, op.attributes);
662
+ pos += op.retain;
663
+ src += op.retain;
664
+ }
665
+ else {
666
+ const pieces = splitDeleteAtAstral(cur, src, op.delete);
667
+ src += op.delete;
668
+ if (pieces.length <= 1) {
669
+ pending.push(op);
670
+ continue;
671
+ }
672
+ for (const piece of pieces) {
673
+ pending.push({ delete: piece });
674
+ flush();
675
+ }
676
+ }
677
+ }
678
+ const ops = toLoroDelta(pending);
679
+ if (ops.length)
680
+ t.applyDelta(ops);
681
+ }
682
+ export function ensureTrailingNewline(t) {
683
+ const cur = t.toString();
684
+ if (cur.endsWith("\n"))
685
+ return cur;
686
+ t.applyDelta(cur.length ? [{ retain: cur.length }, { insert: "\n" }] : [{ insert: "\n" }]);
687
+ return cur + "\n";
688
+ }