markloom 0.5.0 → 0.6.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.
Files changed (67) hide show
  1. package/PACKAGES.md +26 -0
  2. package/README.md +20 -2
  3. package/collab.md +44 -7
  4. package/dist/ai/index.cjs +4 -4
  5. package/dist/ai/index.d.cts +5 -4
  6. package/dist/ai/index.d.ts +5 -4
  7. package/dist/ai/index.js +3 -3
  8. package/dist/ai/theme.css +66 -102
  9. package/dist/{chunk-YJAWPRJW.cjs → chunk-ACFYBYZJ.cjs} +2817 -903
  10. package/dist/chunk-CGYCJORO.js +759 -0
  11. package/dist/chunk-EADZKEU4.cjs +498 -0
  12. package/dist/chunk-EJSFZHGT.js +24 -0
  13. package/dist/{chunk-SLRD6FMP.cjs → chunk-I5VK4U7J.cjs} +122 -73
  14. package/dist/{chunk-WUHTCCAE.js → chunk-KYZJIOOL.js} +2403 -489
  15. package/dist/chunk-QEVME6ZU.cjs +759 -0
  16. package/dist/{chunk-MFYF2A6U.js → chunk-SNFF6X45.js} +51 -1
  17. package/dist/{chunk-JMOJU5EU.js → chunk-TB2OY3D4.js} +69 -20
  18. package/dist/{chunk-4TMHVNL6.cjs → chunk-TWD5T4YC.cjs} +155 -105
  19. package/dist/{chunk-JS527FDV.cjs → chunk-U3JZDFL4.cjs} +49 -9
  20. package/dist/{chunk-RLTHVQNQ.js → chunk-VSBIMMI7.js} +45 -5
  21. package/dist/chunk-YBAT343I.js +498 -0
  22. package/dist/chunk-ZKQSQSSM.cjs +24 -0
  23. package/dist/{client-BNams4wv.d.cts → client-CLYq17SW.d.ts} +70 -7
  24. package/dist/{client-A0ZQJnyR.d.ts → client-DAth8pPq.d.cts} +70 -7
  25. package/dist/collab/index.cjs +22 -4
  26. package/dist/collab/index.d.cts +24 -3
  27. package/dist/collab/index.d.ts +24 -3
  28. package/dist/collab/index.js +24 -6
  29. package/dist/{controller-BkY1VPzc.d.ts → controller-6AJ36G52.d.ts} +108 -8
  30. package/dist/{controller-CVIZI--A.d.cts → controller-BIOO_XKJ.d.cts} +108 -8
  31. package/dist/index.cjs +57 -7
  32. package/dist/index.d.cts +18 -5
  33. package/dist/index.d.ts +18 -5
  34. package/dist/index.js +54 -4
  35. package/dist/io/index.cjs +1413 -0
  36. package/dist/io/index.d.cts +33 -0
  37. package/dist/io/index.d.ts +33 -0
  38. package/dist/io/index.js +1413 -0
  39. package/dist/react/index.cjs +8 -8
  40. package/dist/react/index.d.cts +5 -4
  41. package/dist/react/index.d.ts +5 -4
  42. package/dist/react/index.js +7 -7
  43. package/dist/{rebase-batch-8rL_aXEE.d.ts → rebase-batch-DlerAcFN.d.ts} +11 -9
  44. package/dist/{rebase-batch-bYnBJpRO.d.cts → rebase-batch-PjENQ742.d.cts} +11 -9
  45. package/dist/suggest-Bk4zLzuv.d.ts +120 -0
  46. package/dist/suggest-BkGGiPKA.d.cts +120 -0
  47. package/dist/types-COUweEav.d.cts +30 -0
  48. package/dist/types-COUweEav.d.ts +30 -0
  49. package/dist/versions-9mG1epfB.d.cts +51 -0
  50. package/dist/versions-Bubyo4pt.d.ts +51 -0
  51. package/dist/view/index.cjs +56 -12
  52. package/dist/view/index.d.cts +203 -11
  53. package/dist/view/index.d.ts +203 -11
  54. package/dist/view/index.js +52 -8
  55. package/dist/view/theme.css +1840 -1526
  56. package/io.md +38 -0
  57. package/package.json +21 -5
  58. package/dist/chunk-6DCI7AL3.js +0 -208
  59. package/dist/chunk-AC5E42NH.cjs +0 -208
  60. package/dist/chunk-GQ2HHRGI.cjs +0 -16
  61. package/dist/chunk-LA5SBCGQ.js +0 -16
  62. package/dist/chunk-LEYAQNJA.js +0 -220
  63. package/dist/chunk-YXQDCW4Y.cjs +0 -220
  64. package/dist/comments-DlExS6f-.d.cts +0 -43
  65. package/dist/comments-PhnR7Gkn.d.ts +0 -43
  66. package/dist/step-BrvHIK7O.d.cts +0 -65
  67. package/dist/step-BrvHIK7O.d.ts +0 -65
@@ -0,0 +1,759 @@
1
+ import {
2
+ apply,
3
+ applyAll,
4
+ checkTable,
5
+ invertAll,
6
+ nodeAt,
7
+ validateAll
8
+ } from "./chunk-SNFF6X45.js";
9
+ import {
10
+ mapSelectionThrough,
11
+ rebaseStepsThrough
12
+ } from "./chunk-VSBIMMI7.js";
13
+ import {
14
+ isElement,
15
+ isInlineAtom,
16
+ isText
17
+ } from "./chunk-EJSFZHGT.js";
18
+
19
+ // src/tree/history.ts
20
+ function init(doc, validate) {
21
+ return { doc, past: [], future: [], validate };
22
+ }
23
+ function gate(h, next) {
24
+ const v = h.validate?.(next);
25
+ if (v)
26
+ throw new Error(`[invariant] \u63D0\u4EA4\u4EA7\u751F\u975E\u6CD5 doc: ${v}`);
27
+ }
28
+ function commit(h, steps, before, after) {
29
+ const inverse = invertAll(h.doc, steps);
30
+ const next = applyAll(h.doc, steps);
31
+ gate(h, next);
32
+ const entry = { steps, inverse };
33
+ if (before)
34
+ entry.before = before;
35
+ if (after)
36
+ entry.after = after;
37
+ return { ...h, doc: next, past: h.past.concat(entry), future: [] };
38
+ }
39
+ function applyForeign(h, steps, thisSite = 0, foreignSite = 1) {
40
+ if (steps.length === 0)
41
+ return h;
42
+ const next = applyAll(h.doc, steps);
43
+ gate(h, next);
44
+ const overs = [{ siteId: foreignSite, steps }];
45
+ const mapCommit = (c) => {
46
+ const fwd = rebaseStepsThrough(c.steps, overs, thisSite);
47
+ const inv = rebaseStepsThrough(c.inverse, overs, thisSite);
48
+ if (fwd.length === 0 && inv.length === 0)
49
+ return null;
50
+ const entry = { steps: fwd, inverse: inv };
51
+ const b = mapSelectionThrough(c.before, steps);
52
+ const a = mapSelectionThrough(c.after, steps);
53
+ if (b)
54
+ entry.before = b;
55
+ if (a)
56
+ entry.after = a;
57
+ return entry;
58
+ };
59
+ return {
60
+ ...h,
61
+ doc: next,
62
+ past: h.past.map(mapCommit).filter((c) => c != null),
63
+ future: h.future.map(mapCommit).filter((c) => c != null)
64
+ };
65
+ }
66
+ function amend(h, more, after, before) {
67
+ if (more.length === 0)
68
+ return h;
69
+ if (h.past.length === 0)
70
+ return commit(h, more, before, after);
71
+ const last = h.past[h.past.length - 1];
72
+ const docBefore = applyAll(h.doc, last.inverse);
73
+ const steps = last.steps.concat(more);
74
+ const inverse = invertAll(docBefore, steps);
75
+ const next = applyAll(docBefore, steps);
76
+ gate(h, next);
77
+ const entry = { steps, inverse };
78
+ if (last.before)
79
+ entry.before = last.before;
80
+ if (after)
81
+ entry.after = after;
82
+ else if (last.after)
83
+ entry.after = last.after;
84
+ return {
85
+ ...h,
86
+ doc: next,
87
+ past: h.past.slice(0, -1).concat(entry),
88
+ future: []
89
+ };
90
+ }
91
+ function undo(h) {
92
+ const last = h.past[h.past.length - 1];
93
+ if (!last)
94
+ return h;
95
+ const next = applyAll(h.doc, last.inverse);
96
+ gate(h, next);
97
+ return { ...h, doc: next, past: h.past.slice(0, -1), future: [last, ...h.future] };
98
+ }
99
+ function redo(h) {
100
+ const next = h.future[0];
101
+ if (!next)
102
+ return h;
103
+ const doc = applyAll(h.doc, next.steps);
104
+ gate(h, doc);
105
+ return { ...h, doc, past: h.past.concat(next), future: h.future.slice(1) };
106
+ }
107
+
108
+ // src/tree/invariants.ts
109
+ function invariants(schema, doc) {
110
+ const out = [];
111
+ out.push(...validateAll(schema, doc));
112
+ const ids = /* @__PURE__ */ new Map();
113
+ walk(doc, [], schema, out, ids);
114
+ return out;
115
+ }
116
+ function walk(node, path, schema, out, ids) {
117
+ if (isText(node)) {
118
+ return;
119
+ }
120
+ if (node.id !== void 0) {
121
+ ids.set(node.id, (ids.get(node.id) ?? 0) + 1);
122
+ if (ids.get(node.id) === 2) {
123
+ out.push({ path, reason: `\u91CD\u590D id "${node.id}"` });
124
+ }
125
+ }
126
+ const spec = schema.nodes[node.type];
127
+ if (spec?.atom) {
128
+ if (node.type === "image") {
129
+ if (!node.attrs?.url)
130
+ out.push({ path, reason: `[image] \u7F3A url` });
131
+ return;
132
+ }
133
+ if (node.type === "mention") {
134
+ if (!node.attrs?.id)
135
+ out.push({ path, reason: `[mention] \u7F3A id` });
136
+ return;
137
+ }
138
+ if (node.type === "math") {
139
+ if (typeof node.attrs?.latex !== "string")
140
+ out.push({ path, reason: `[math] \u7F3A latex` });
141
+ return;
142
+ }
143
+ if (node.type === "embed") {
144
+ if (!node.attrs?.kind)
145
+ out.push({ path, reason: `[embed] \u7F3A kind` });
146
+ if (!node.attrs?.src)
147
+ out.push({ path, reason: `[embed] \u7F3A src` });
148
+ return;
149
+ }
150
+ for (const reason of checkTable(node)) {
151
+ out.push({ path, reason: `[table] ${reason}` });
152
+ }
153
+ return;
154
+ }
155
+ if (spec?.marks) {
156
+ for (let i = 0; i < node.children.length; i++) {
157
+ const child = node.children[i];
158
+ if (isText(child) && child.marks) {
159
+ for (const m of child.marks) {
160
+ if (!spec.marks.includes(m.name)) {
161
+ out.push({ path: path.concat(i), reason: `${node.type} \u4E0D\u5141\u8BB8 mark "${m.name}"` });
162
+ }
163
+ }
164
+ }
165
+ }
166
+ }
167
+ for (let i = 0; i < node.children.length; i++) {
168
+ walk(node.children[i], path.concat(i), schema, out, ids);
169
+ }
170
+ }
171
+ function firstViolation(vs) {
172
+ if (vs.length === 0) {
173
+ return null;
174
+ }
175
+ const v = vs[0];
176
+ return `${v.path.join(",")}: ${v.reason}`;
177
+ }
178
+
179
+ // src/tree/toc.ts
180
+ function headingText(node) {
181
+ let out = "";
182
+ for (const c of node.children) {
183
+ if (isText(c))
184
+ out += c.text;
185
+ }
186
+ return out;
187
+ }
188
+ function outline(doc) {
189
+ const out = [];
190
+ const walk3 = (node, path) => {
191
+ if (isText(node))
192
+ return;
193
+ if (node.type === "heading") {
194
+ const level = Math.min(Math.max(Number(node.attrs?.depth ?? 1), 1), 6);
195
+ out.push({ level, text: headingText(node), path, id: node.id });
196
+ return;
197
+ }
198
+ for (let i = 0; i < node.children.length; i++)
199
+ walk3(node.children[i], path.concat(i));
200
+ };
201
+ walk3(doc, []);
202
+ return out;
203
+ }
204
+
205
+ // src/tree/comments.ts
206
+ function commentIdsInDoc(doc) {
207
+ const ids = /* @__PURE__ */ new Set();
208
+ const walk3 = (n) => {
209
+ if (isText(n)) {
210
+ for (const m of n.marks ?? []) {
211
+ if (m.name === "comment" && m.attrs?.id != null)
212
+ ids.add(String(m.attrs.id));
213
+ }
214
+ return;
215
+ }
216
+ if (isElement(n))
217
+ for (const c of n.children)
218
+ walk3(c);
219
+ };
220
+ walk3(doc);
221
+ return ids;
222
+ }
223
+ function stripLeaf(leaf, id) {
224
+ const rest = (leaf.marks ?? []).filter((m) => !(m.name === "comment" && String(m.attrs?.id) === id));
225
+ return rest.length ? { ...leaf, marks: rest } : { text: leaf.text };
226
+ }
227
+ function stripCommentMarks(doc, id) {
228
+ const steps = [];
229
+ const walk3 = (node, path) => {
230
+ if (node.children.every((c) => isText(c) || isInlineAtom(c))) {
231
+ let changed = false;
232
+ const children = node.children.map((c) => {
233
+ if (!isText(c) || !c.marks?.some((m) => m.name === "comment" && String(m.attrs?.id) === id))
234
+ return c;
235
+ changed = true;
236
+ return stripLeaf(c, id);
237
+ });
238
+ if (changed)
239
+ steps.push({ kind: "replaceNode", path, node: { ...node, children } });
240
+ return;
241
+ }
242
+ node.children.forEach((c, i) => {
243
+ if (isElement(c))
244
+ walk3(c, path.concat(i));
245
+ });
246
+ };
247
+ doc.children.forEach((c, i) => {
248
+ if (isElement(c))
249
+ walk3(c, [i]);
250
+ });
251
+ return steps;
252
+ }
253
+
254
+ // src/tree/search.ts
255
+ var DEFAULT_SKIP = ["code"];
256
+ function findText(doc, query, opts = {}) {
257
+ if (!query)
258
+ return [];
259
+ const skip = new Set(opts.skipTypes ?? DEFAULT_SKIP);
260
+ const cs = opts.caseSensitive === true;
261
+ const needle = cs ? query : query.toLowerCase();
262
+ const out = [];
263
+ walk2(doc, [], skip, (path, text) => {
264
+ const hay = cs ? text : text.toLowerCase();
265
+ let from = 0;
266
+ while (from <= hay.length - needle.length) {
267
+ const i = hay.indexOf(needle, from);
268
+ if (i < 0)
269
+ break;
270
+ out.push({ path: path.slice(), offset: i, length: query.length });
271
+ from = i + Math.max(1, needle.length);
272
+ }
273
+ });
274
+ return out;
275
+ }
276
+ function replaceMatch(match, replacement) {
277
+ const del = {
278
+ kind: "deleteText",
279
+ path: match.path,
280
+ offset: match.offset,
281
+ length: match.length
282
+ };
283
+ if (!replacement)
284
+ return [del];
285
+ return [
286
+ del,
287
+ {
288
+ kind: "insertText",
289
+ path: match.path,
290
+ offset: match.offset,
291
+ text: replacement
292
+ }
293
+ ];
294
+ }
295
+ function replaceAll(doc, query, replacement, opts = {}) {
296
+ if (!query)
297
+ return { steps: [], count: 0 };
298
+ const skip = new Set(opts.skipTypes ?? DEFAULT_SKIP);
299
+ const cs = opts.caseSensitive === true;
300
+ const steps = [];
301
+ let count = 0;
302
+ walk2(doc, [], skip, (path, text, leaf) => {
303
+ const { text: next, n } = replaceInString(text, query, replacement, cs);
304
+ if (n === 0)
305
+ return;
306
+ count += n;
307
+ steps.push({
308
+ kind: "replaceNode",
309
+ path: path.slice(),
310
+ node: { ...leaf, text: next }
311
+ });
312
+ });
313
+ return { steps, count };
314
+ }
315
+ function applyReplaceAll(doc, query, replacement, opts = {}) {
316
+ const { steps } = replaceAll(doc, query, replacement, opts);
317
+ let d = doc;
318
+ for (const s of steps)
319
+ d = apply(d, s);
320
+ return d;
321
+ }
322
+ function replaceInString(text, query, replacement, caseSensitive) {
323
+ if (!query)
324
+ return { text, n: 0 };
325
+ if (caseSensitive) {
326
+ if (!text.includes(query))
327
+ return { text, n: 0 };
328
+ const parts = text.split(query);
329
+ return { text: parts.join(replacement), n: parts.length - 1 };
330
+ }
331
+ const needle = query.toLowerCase();
332
+ const hay = text.toLowerCase();
333
+ let out = "";
334
+ let i = 0;
335
+ let n = 0;
336
+ while (i < text.length) {
337
+ if (i <= text.length - query.length && hay.startsWith(needle, i)) {
338
+ out += replacement;
339
+ i += query.length;
340
+ n++;
341
+ } else {
342
+ out += text[i];
343
+ i++;
344
+ }
345
+ }
346
+ return { text: out, n };
347
+ }
348
+ function walk2(node, path, skip, onLeaf) {
349
+ if (isText(node)) {
350
+ onLeaf(path, node.text, node);
351
+ return;
352
+ }
353
+ if (!isElement(node))
354
+ return;
355
+ if (skip.has(node.type))
356
+ return;
357
+ for (let i = 0; i < node.children.length; i++)
358
+ walk2(node.children[i], path.concat(i), skip, onLeaf);
359
+ }
360
+
361
+ // src/tree/versions.ts
362
+ function cloneDoc(doc) {
363
+ return typeof structuredClone === "function" ? structuredClone(doc) : JSON.parse(JSON.stringify(doc));
364
+ }
365
+ function nid() {
366
+ return typeof crypto !== "undefined" && crypto.randomUUID ? crypto.randomUUID() : `v${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
367
+ }
368
+ function snapshotDoc(doc, name, id) {
369
+ const label = name.trim() || defaultName();
370
+ return {
371
+ id: id ?? nid(),
372
+ name: label,
373
+ createdAt: Date.now(),
374
+ doc: cloneDoc(doc)
375
+ };
376
+ }
377
+ function defaultName() {
378
+ const d = /* @__PURE__ */ new Date();
379
+ const p = (n) => n < 10 ? `0${n}` : `${n}`;
380
+ return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
381
+ }
382
+ function pushVersion(list, v, max = 50) {
383
+ const next = list.filter((x) => x.id !== v.id).concat(v);
384
+ return next.length > max ? next.slice(next.length - max) : next;
385
+ }
386
+ function getVersion(list, id) {
387
+ return list.find((v) => v.id === id);
388
+ }
389
+ function removeVersion(list, id) {
390
+ return list.filter((v) => v.id !== id);
391
+ }
392
+
393
+ // src/tree/suggest.ts
394
+ function cmpPath(a, b) {
395
+ const n = Math.min(a.length, b.length);
396
+ for (let i = 0; i < n; i++) {
397
+ if (a[i] !== b[i])
398
+ return a[i] - b[i];
399
+ }
400
+ return a.length - b.length;
401
+ }
402
+ function suggestMark(id, kind, author) {
403
+ const attrs = { id, kind };
404
+ if (author?.id)
405
+ attrs.authorId = author.id;
406
+ if (author?.name)
407
+ attrs.authorName = author.name;
408
+ return { name: "suggest", attrs };
409
+ }
410
+ function isSuggestMark(m, id) {
411
+ if (m.name !== "suggest")
412
+ return false;
413
+ if (id == null)
414
+ return true;
415
+ return String(m.attrs?.id ?? "") === id;
416
+ }
417
+ function suggestIdsInDoc(doc) {
418
+ const ids = /* @__PURE__ */ new Set();
419
+ const walk3 = (n) => {
420
+ if (isText(n)) {
421
+ for (const m of n.marks ?? []) {
422
+ if (m.name === "suggest" && m.attrs?.id != null)
423
+ ids.add(String(m.attrs.id));
424
+ }
425
+ return;
426
+ }
427
+ if (isElement(n))
428
+ for (const c of n.children)
429
+ walk3(c);
430
+ };
431
+ walk3(doc);
432
+ return ids;
433
+ }
434
+ function listSuggestions(doc) {
435
+ const order = [];
436
+ const map = /* @__PURE__ */ new Map();
437
+ const walk3 = (n) => {
438
+ if (isText(n)) {
439
+ for (const m of n.marks ?? []) {
440
+ if (m.name !== "suggest" || m.attrs?.id == null)
441
+ continue;
442
+ const id = String(m.attrs.id);
443
+ const kind = m.attrs.kind === "delete" ? "delete" : "insert";
444
+ const cur = map.get(id);
445
+ if (!cur) {
446
+ order.push(id);
447
+ map.set(id, {
448
+ id,
449
+ kind,
450
+ quote: n.text,
451
+ authorId: m.attrs.authorId != null ? String(m.attrs.authorId) : void 0,
452
+ authorName: m.attrs.authorName != null ? String(m.attrs.authorName) : void 0
453
+ });
454
+ } else {
455
+ cur.quote += n.text;
456
+ }
457
+ }
458
+ return;
459
+ }
460
+ if (isElement(n))
461
+ for (const c of n.children)
462
+ walk3(c);
463
+ };
464
+ walk3(doc);
465
+ return order.map((id) => map.get(id));
466
+ }
467
+ function stripLeaf2(leaf, id) {
468
+ const rest = (leaf.marks ?? []).filter((m) => !isSuggestMark(m, id));
469
+ return rest.length ? { ...leaf, marks: rest } : { text: leaf.text };
470
+ }
471
+ function leafHasSuggest(leaf, id) {
472
+ return (leaf.marks ?? []).some((m) => isSuggestMark(m, id));
473
+ }
474
+ function leafSuggestKind(leaf, id) {
475
+ const m = (leaf.marks ?? []).find((m2) => isSuggestMark(m2, id));
476
+ if (!m)
477
+ return null;
478
+ return m.attrs?.kind === "delete" ? "delete" : "insert";
479
+ }
480
+ function stripSuggestMarks(doc, id) {
481
+ const steps = [];
482
+ const walk3 = (node, path) => {
483
+ if (node.children.every((c) => isText(c) || isInlineAtom(c))) {
484
+ let changed = false;
485
+ const children = node.children.map((c) => {
486
+ if (!isText(c) || !leafHasSuggest(c, id))
487
+ return c;
488
+ changed = true;
489
+ return stripLeaf2(c, id);
490
+ });
491
+ if (changed)
492
+ steps.push({ kind: "replaceNode", path, node: { ...node, children } });
493
+ return;
494
+ }
495
+ node.children.forEach((c, i) => {
496
+ if (isElement(c))
497
+ walk3(c, path.concat(i));
498
+ });
499
+ };
500
+ doc.children.forEach((c, i) => {
501
+ if (isElement(c))
502
+ walk3(c, [i]);
503
+ });
504
+ return steps;
505
+ }
506
+ function dropSuggestText(doc, id) {
507
+ const steps = [];
508
+ const walk3 = (node, path) => {
509
+ if (node.children.every((c) => isText(c) || isInlineAtom(c))) {
510
+ let changed = false;
511
+ const children = [];
512
+ for (const c of node.children) {
513
+ if (isText(c) && leafHasSuggest(c, id)) {
514
+ changed = true;
515
+ continue;
516
+ }
517
+ children.push(c);
518
+ }
519
+ if (changed) {
520
+ const next = children.length ? children : [{ text: "" }];
521
+ steps.push({ kind: "replaceNode", path, node: { ...node, children: next } });
522
+ }
523
+ return;
524
+ }
525
+ node.children.forEach((c, i) => {
526
+ if (isElement(c))
527
+ walk3(c, path.concat(i));
528
+ });
529
+ };
530
+ doc.children.forEach((c, i) => {
531
+ if (isElement(c))
532
+ walk3(c, [i]);
533
+ });
534
+ return steps;
535
+ }
536
+ function kindOfId(doc, id) {
537
+ let kind = "insert";
538
+ const walk3 = (n) => {
539
+ if (isText(n)) {
540
+ const k = leafSuggestKind(n, id);
541
+ if (k) {
542
+ kind = k;
543
+ return true;
544
+ }
545
+ return false;
546
+ }
547
+ if (isElement(n)) {
548
+ for (const c of n.children) {
549
+ if (walk3(c))
550
+ return true;
551
+ }
552
+ }
553
+ return false;
554
+ };
555
+ walk3(doc);
556
+ return kind;
557
+ }
558
+ function acceptSuggestion(doc, id) {
559
+ return kindOfId(doc, id) === "delete" ? dropSuggestText(doc, id) : stripSuggestMarks(doc, id);
560
+ }
561
+ function rejectSuggestion(doc, id) {
562
+ return kindOfId(doc, id) === "delete" ? stripSuggestMarks(doc, id) : dropSuggestText(doc, id);
563
+ }
564
+ function split(path) {
565
+ return {
566
+ blockPath: path.slice(0, -2),
567
+ leafIdx: path[path.length - 2],
568
+ char: path[path.length - 1]
569
+ };
570
+ }
571
+ function blockChar(children, leafIdx, char) {
572
+ let n = 0;
573
+ for (let i = 0; i < leafIdx; i++) {
574
+ const c = children[i];
575
+ n += isText(c) ? c.text.length : 1;
576
+ }
577
+ return n + char;
578
+ }
579
+ function totalChars(children) {
580
+ return children.reduce((a, c) => a + (isText(c) ? c.text.length : 1), 0);
581
+ }
582
+ function leafBlocksBetween(doc, fromBlock, toBlock) {
583
+ const all = [];
584
+ const walk3 = (node, path) => {
585
+ if (!isElement(node))
586
+ return;
587
+ if (node.children.every((c) => isText(c) || isInlineAtom(c)))
588
+ all.push({ path, node });
589
+ else
590
+ node.children.forEach((c, i) => walk3(c, path.concat(i)));
591
+ };
592
+ doc.children.forEach((c, i) => walk3(c, [i]));
593
+ const fi = all.findIndex((b) => cmpPath(b.path, fromBlock) === 0);
594
+ const ti = all.findIndex((b) => cmpPath(b.path, toBlock) === 0);
595
+ return fi < 0 || ti < 0 ? [] : all.slice(fi, ti + 1);
596
+ }
597
+ function setSuggestOn(leaf, mark) {
598
+ const rest = (leaf.marks ?? []).filter((m) => m.name !== "suggest");
599
+ return { ...leaf, marks: [...rest, mark] };
600
+ }
601
+ function markChars(block, from, to, mark) {
602
+ const out = [];
603
+ let pos = 0;
604
+ for (const leaf of block.children) {
605
+ if (!isText(leaf)) {
606
+ out.push(leaf);
607
+ pos += 1;
608
+ continue;
609
+ }
610
+ const len = leaf.text.length;
611
+ const lo = Math.max(from, pos);
612
+ const hi = Math.min(to, pos + len);
613
+ if (lo >= hi) {
614
+ out.push(leaf);
615
+ } else {
616
+ if (lo > pos)
617
+ out.push({ ...leaf, text: leaf.text.slice(0, lo - pos) });
618
+ out.push(setSuggestOn({ ...leaf, text: leaf.text.slice(lo - pos, hi - pos) }, mark));
619
+ if (hi < pos + len)
620
+ out.push({ ...leaf, text: leaf.text.slice(hi - pos) });
621
+ }
622
+ pos += len;
623
+ }
624
+ return coalesce(out);
625
+ }
626
+ function coalesce(nodes) {
627
+ const out = [];
628
+ for (const n of nodes) {
629
+ const prev = out[out.length - 1];
630
+ if (prev && isText(prev) && isText(n) && sameMarks(prev.marks, n.marks))
631
+ out[out.length - 1] = { ...prev, text: prev.text + n.text };
632
+ else
633
+ out.push(n);
634
+ }
635
+ return out;
636
+ }
637
+ function sameMarks(a, b) {
638
+ const x = a ?? [];
639
+ const y = b ?? [];
640
+ if (x.length !== y.length)
641
+ return false;
642
+ return x.every((m, i) => m.name === y[i].name && JSON.stringify(m.attrs ?? {}) === JSON.stringify(y[i].attrs ?? {}));
643
+ }
644
+ function rangeForDelete(doc, sel, dir) {
645
+ if (cmpPath(sel.anchor.path, sel.head.path) !== 0)
646
+ return sel;
647
+ const path = sel.head.path;
648
+ if (path.length < 2)
649
+ return null;
650
+ const char = path[path.length - 1];
651
+ const leafPath = path.slice(0, -1);
652
+ const leaf = nodeAt(doc, leafPath);
653
+ if (!isText(leaf))
654
+ return null;
655
+ if (dir === "backward") {
656
+ if (char <= 0)
657
+ return null;
658
+ return {
659
+ anchor: { path: leafPath.concat(char - 1) },
660
+ head: { path: leafPath.concat(char) }
661
+ };
662
+ }
663
+ if (char >= leaf.text.length)
664
+ return null;
665
+ return {
666
+ anchor: { path: leafPath.concat(char) },
667
+ head: { path: leafPath.concat(char + 1) }
668
+ };
669
+ }
670
+ function rangeIsSuggestInsert(doc, sel) {
671
+ const [lo, hi] = cmpPath(sel.anchor.path, sel.head.path) <= 0 ? [sel.anchor.path, sel.head.path] : [sel.head.path, sel.anchor.path];
672
+ if (cmpPath(lo, hi) === 0)
673
+ return false;
674
+ const a = split(lo);
675
+ const b = split(hi);
676
+ const blocks = leafBlocksBetween(doc, a.blockPath, b.blockPath);
677
+ if (!blocks.length)
678
+ return false;
679
+ let any = false;
680
+ for (let i = 0; i < blocks.length; i++) {
681
+ const blk = blocks[i];
682
+ const from = i === 0 ? blockChar(blk.node.children, a.leafIdx, a.char) : 0;
683
+ const to = i === blocks.length - 1 ? blockChar(blk.node.children, b.leafIdx, b.char) : totalChars(blk.node.children);
684
+ if (from >= to)
685
+ continue;
686
+ let pos = 0;
687
+ for (const leaf of blk.node.children) {
688
+ if (!isText(leaf)) {
689
+ pos += 1;
690
+ continue;
691
+ }
692
+ const len = leaf.text.length;
693
+ const loC = Math.max(from, pos);
694
+ const hiC = Math.min(to, pos + len);
695
+ if (loC < hiC) {
696
+ any = true;
697
+ const hasIns = (leaf.marks ?? []).some((m) => m.name === "suggest" && m.attrs?.kind !== "delete");
698
+ if (!hasIns)
699
+ return false;
700
+ }
701
+ pos += len;
702
+ }
703
+ }
704
+ return any;
705
+ }
706
+ function markSuggestRange(doc, sel, kind, id, author) {
707
+ const [lo, hi] = cmpPath(sel.anchor.path, sel.head.path) <= 0 ? [sel.anchor.path, sel.head.path] : [sel.head.path, sel.anchor.path];
708
+ if (cmpPath(lo, hi) === 0)
709
+ return [];
710
+ const a = split(lo);
711
+ const b = split(hi);
712
+ const blocks = leafBlocksBetween(doc, a.blockPath, b.blockPath);
713
+ if (!blocks.length)
714
+ return [];
715
+ const mark = suggestMark(id, kind, author);
716
+ const steps = [];
717
+ blocks.forEach((blk, i) => {
718
+ const from = i === 0 ? blockChar(blk.node.children, a.leafIdx, a.char) : 0;
719
+ const to = i === blocks.length - 1 ? blockChar(blk.node.children, b.leafIdx, b.char) : totalChars(blk.node.children);
720
+ if (from >= to)
721
+ return;
722
+ const children = markChars(blk.node, from, to, mark);
723
+ steps.push({ kind: "replaceNode", path: blk.path, node: { ...blk.node, children } });
724
+ });
725
+ return steps;
726
+ }
727
+
728
+ export {
729
+ init,
730
+ commit,
731
+ applyForeign,
732
+ amend,
733
+ undo,
734
+ redo,
735
+ invariants,
736
+ firstViolation,
737
+ outline,
738
+ commentIdsInDoc,
739
+ stripCommentMarks,
740
+ findText,
741
+ replaceMatch,
742
+ replaceAll,
743
+ applyReplaceAll,
744
+ snapshotDoc,
745
+ pushVersion,
746
+ getVersion,
747
+ removeVersion,
748
+ suggestMark,
749
+ isSuggestMark,
750
+ suggestIdsInDoc,
751
+ listSuggestions,
752
+ stripSuggestMarks,
753
+ dropSuggestText,
754
+ acceptSuggestion,
755
+ rejectSuggestion,
756
+ rangeForDelete,
757
+ rangeIsSuggestInsert,
758
+ markSuggestRange
759
+ };