@dou-ui/editor 0.1.0-alpha.3 → 0.1.0-alpha.30

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 (159) hide show
  1. package/dist/collaboration/binding.js +371 -57
  2. package/dist/collaboration/binding.js.map +1 -1
  3. package/dist/collaboration/cursors.js +42 -10
  4. package/dist/collaboration/cursors.js.map +1 -1
  5. package/dist/collaboration/index.js +61 -11
  6. package/dist/collaboration/index.js.map +1 -1
  7. package/dist/collaboration/inline-diff.js +138 -0
  8. package/dist/collaboration/inline-diff.js.map +1 -0
  9. package/dist/collaboration/merged-props.js +152 -0
  10. package/dist/collaboration/merged-props.js.map +1 -0
  11. package/dist/collaboration/order.js +41 -1
  12. package/dist/collaboration/order.js.map +1 -1
  13. package/dist/collaboration/table.js +64 -25
  14. package/dist/collaboration/table.js.map +1 -1
  15. package/dist/collaboration/text.js +213 -1
  16. package/dist/collaboration/text.js.map +1 -1
  17. package/dist/components/block-handle.js.map +1 -1
  18. package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js +127 -17
  19. package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js.map +1 -1
  20. package/dist/components/slash-choices.js +6 -0
  21. package/dist/components/slash-choices.js.map +1 -0
  22. package/dist/components/slash-choices.vue_vue_type_script_setup_true_lang.js +56 -0
  23. package/dist/components/slash-choices.vue_vue_type_script_setup_true_lang.js.map +1 -0
  24. package/dist/components/slash-menu.js.map +1 -1
  25. package/dist/components/slash-menu.vue_vue_type_script_setup_true_lang.js +185 -20
  26. package/dist/components/slash-menu.vue_vue_type_script_setup_true_lang.js.map +1 -1
  27. package/dist/core/block-selection.js +32 -4
  28. package/dist/core/block-selection.js.map +1 -1
  29. package/dist/core/blocks.js +21 -3
  30. package/dist/core/blocks.js.map +1 -1
  31. package/dist/core/cjk.js +54 -0
  32. package/dist/core/cjk.js.map +1 -0
  33. package/dist/core/commands.js +125 -13
  34. package/dist/core/commands.js.map +1 -1
  35. package/dist/core/comments.js +85 -3
  36. package/dist/core/comments.js.map +1 -1
  37. package/dist/core/convert.js +3 -3
  38. package/dist/core/convert.js.map +1 -1
  39. package/dist/core/extension.js +6 -2
  40. package/dist/core/extension.js.map +1 -1
  41. package/dist/core/model.js +10 -3
  42. package/dist/core/model.js.map +1 -1
  43. package/dist/core/output.js +26 -10
  44. package/dist/core/output.js.map +1 -1
  45. package/dist/core/prepare.js +12 -5
  46. package/dist/core/prepare.js.map +1 -1
  47. package/dist/core/schema.js.map +1 -1
  48. package/dist/core/transaction.js +19 -4
  49. package/dist/core/transaction.js.map +1 -1
  50. package/dist/define-block.js +8 -4
  51. package/dist/define-block.js.map +1 -1
  52. package/dist/document.js +59 -4
  53. package/dist/document.js.map +1 -1
  54. package/dist/dom.js +109 -13
  55. package/dist/dom.js.map +1 -1
  56. package/dist/editor.js.map +1 -1
  57. package/dist/editor.vue_vue_type_script_setup_true_lang.js +6 -5
  58. package/dist/editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
  59. package/dist/editor2.js.map +1 -1
  60. package/dist/extensions/callout/html.js +5 -4
  61. package/dist/extensions/callout/html.js.map +1 -1
  62. package/dist/extensions/columns/column-picker.js +6 -0
  63. package/dist/extensions/columns/column-picker.js.map +1 -0
  64. package/dist/extensions/columns/column-picker.vue_vue_type_script_setup_true_lang.js +57 -0
  65. package/dist/extensions/columns/column-picker.vue_vue_type_script_setup_true_lang.js.map +1 -0
  66. package/dist/extensions/columns/columns-controls.js +6 -0
  67. package/dist/extensions/columns/columns-controls.js.map +1 -0
  68. package/dist/extensions/columns/columns-controls.vue_vue_type_script_setup_true_lang.js +341 -0
  69. package/dist/extensions/columns/columns-controls.vue_vue_type_script_setup_true_lang.js.map +1 -0
  70. package/dist/extensions/columns/html.js +34 -0
  71. package/dist/extensions/columns/html.js.map +1 -0
  72. package/dist/extensions/columns/index.js +57 -0
  73. package/dist/extensions/columns/index.js.map +1 -0
  74. package/dist/extensions/columns/keys.js +53 -0
  75. package/dist/extensions/columns/keys.js.map +1 -0
  76. package/dist/extensions/columns/plugin.js +213 -0
  77. package/dist/extensions/columns/plugin.js.map +1 -0
  78. package/dist/extensions/columns/schema.js +58 -0
  79. package/dist/extensions/columns/schema.js.map +1 -0
  80. package/dist/extensions/columns/select-columns.js +6 -0
  81. package/dist/extensions/columns/select-columns.js.map +1 -0
  82. package/dist/extensions/columns/select-columns.vue_vue_type_script_setup_true_lang.js +71 -0
  83. package/dist/extensions/columns/select-columns.vue_vue_type_script_setup_true_lang.js.map +1 -0
  84. package/dist/extensions/editor-kit.js +3 -1
  85. package/dist/extensions/editor-kit.js.map +1 -1
  86. package/dist/extensions/list-html.js +1 -1
  87. package/dist/extensions/list-html.js.map +1 -1
  88. package/dist/extensions/table/plugin.js +33 -1
  89. package/dist/extensions/table/plugin.js.map +1 -1
  90. package/dist/extensions/table/schema.js +5 -1
  91. package/dist/extensions/table/schema.js.map +1 -1
  92. package/dist/extensions/table/table-controls.js.map +1 -1
  93. package/dist/extensions/table/table-controls.vue_vue_type_script_setup_true_lang.js +80 -9
  94. package/dist/extensions/table/table-controls.vue_vue_type_script_setup_true_lang.js.map +1 -1
  95. package/dist/extensions/table/view.js +181 -3
  96. package/dist/extensions/table/view.js.map +1 -1
  97. package/dist/flow/board-block.js +6 -0
  98. package/dist/flow/board-block.js.map +1 -0
  99. package/dist/flow/board-block.vue_vue_type_script_setup_true_lang.js +659 -0
  100. package/dist/flow/board-block.vue_vue_type_script_setup_true_lang.js.map +1 -0
  101. package/dist/flow/board.js +221 -0
  102. package/dist/flow/board.js.map +1 -0
  103. package/dist/flow/index.js +173 -0
  104. package/dist/flow/index.js.map +1 -0
  105. package/dist/index.js +3 -2
  106. package/dist/index.js.map +1 -1
  107. package/dist/locale.js +78 -6
  108. package/dist/locale.js.map +1 -1
  109. package/dist/markdown/cjk-friendly.js +62 -0
  110. package/dist/markdown/cjk-friendly.js.map +1 -0
  111. package/dist/markdown/index.js +4 -3
  112. package/dist/markdown/index.js.map +1 -1
  113. package/dist/style.css +1 -1
  114. package/dist/style.js +2 -0
  115. package/dist/types/collaboration/binding.d.ts +12 -0
  116. package/dist/types/collaboration/cursors.d.ts +29 -1
  117. package/dist/types/collaboration/index.d.ts +53 -6
  118. package/dist/types/collaboration/inline-diff.d.ts +9 -0
  119. package/dist/types/collaboration/merged-props.d.ts +20 -0
  120. package/dist/types/collaboration/order.d.ts +5 -0
  121. package/dist/types/collaboration/table.d.ts +5 -1
  122. package/dist/types/collaboration/text.d.ts +20 -0
  123. package/dist/types/components/slash-choices.vue.d.ts +18 -0
  124. package/dist/types/core/blocks.d.ts +10 -0
  125. package/dist/types/core/cjk.d.ts +23 -0
  126. package/dist/types/core/commands.d.ts +17 -2
  127. package/dist/types/core/comments.d.ts +39 -3
  128. package/dist/types/core/convert.d.ts +10 -2
  129. package/dist/types/core/extension.d.ts +58 -4
  130. package/dist/types/core/model.d.ts +6 -1
  131. package/dist/types/core/prepare.d.ts +16 -2
  132. package/dist/types/core/schema.d.ts +1 -1
  133. package/dist/types/core/transaction.d.ts +6 -0
  134. package/dist/types/define-block.d.ts +16 -4
  135. package/dist/types/document.d.ts +5 -2
  136. package/dist/types/dom.d.ts +41 -4
  137. package/dist/types/editor.d.ts +15 -1
  138. package/dist/types/editor.vue.d.ts +24 -0
  139. package/dist/types/extensions/columns/column-picker.vue.d.ts +10 -0
  140. package/dist/types/extensions/columns/columns-controls.vue.d.ts +3 -0
  141. package/dist/types/extensions/columns/html.d.ts +10 -0
  142. package/dist/types/extensions/columns/index.d.ts +9 -0
  143. package/dist/types/extensions/columns/keys.d.ts +6 -0
  144. package/dist/types/extensions/columns/plugin.d.ts +28 -0
  145. package/dist/types/extensions/columns/schema.d.ts +17 -0
  146. package/dist/types/extensions/columns/select-columns.vue.d.ts +13 -0
  147. package/dist/types/extensions/editor-kit.d.ts +4 -1
  148. package/dist/types/extensions/table/view.d.ts +36 -2
  149. package/dist/types/flow/board-block.vue.d.ts +16 -0
  150. package/dist/types/flow/board.d.ts +53 -0
  151. package/dist/types/flow/index.d.ts +65 -0
  152. package/dist/types/index.d.ts +29 -4
  153. package/dist/types/locale.d.ts +34 -0
  154. package/dist/types/markdown/cjk-friendly.d.ts +6 -0
  155. package/dist/types/markdown/index.d.ts +11 -4
  156. package/dist/types/use-comments.d.ts +1 -0
  157. package/dist/use-comments.js +63 -9
  158. package/dist/use-comments.js.map +1 -1
  159. package/package.json +14 -5
@@ -1,11 +1,13 @@
1
1
  import { createId } from "../core/model.js";
2
2
  import { blockAround, findBlockById } from "../core/blocks.js";
3
3
  import { originKey } from "../core/editor.js";
4
- import { ownStepKey } from "../core/transaction.js";
5
- import { keysBetween, orderKeys } from "./order.js";
6
- import { inlineFrom, insertInline, updateText } from "./text.js";
7
- import { cellAt, cellStart, clearTable, readTable, writeTable } from "./table.js";
4
+ import { joinStepKey, ownStepKey } from "../core/transaction.js";
5
+ import { keptIndexes, keysBetween, orderKeys } from "./order.js";
6
+ import { createDiffBudget } from "./inline-diff.js";
7
+ import { inlineFrom, insertInline, keepInlineComments, prepareTextFromBase, updateText } from "./text.js";
8
+ import { cellAt, cellStart, clearTable, prepareTableText, readTable, writeTable } from "./table.js";
8
9
  import { placeOf } from "./cursors.js";
10
+ import { mergedPropsOf, newMerged, readMerged, writeMerged } from "./merged-props.js";
9
11
  import { Plugin, PluginKey, TextSelection } from "prosemirror-state";
10
12
  import { Fragment } from "prosemirror-model";
11
13
  import * as Y from "yjs";
@@ -39,9 +41,15 @@ function contentFrom(yblock, schema) {
39
41
  const name = yblock.get("type");
40
42
  const known = schema.nodes[name];
41
43
  const type = known?.isInGroup("blockContent") ? known : schema.nodes.paragraph;
42
- const props = yblock.get("props")?.toJSON() ?? {};
44
+ const stored = yblock.get("props");
45
+ const props = stored?.toJSON() ?? {};
46
+ const merged = mergedPropsOf(type);
43
47
  const attrs = {};
44
- for (const attr of Object.keys(type.spec.attrs ?? {})) if (attr in props) attrs[attr] = props[attr];
48
+ for (const attr of Object.keys(type.spec.attrs ?? {})) {
49
+ if (!(attr in props)) continue;
50
+ const spec = merged?.[attr];
51
+ attrs[attr] = spec ? readMerged(stored.get(attr), spec) : props[attr];
52
+ }
45
53
  if (type.spec.rows) return readTable(yblock, type, attrs);
46
54
  if (!type.inlineContent) return type.create(attrs);
47
55
  const text = yblock.get("text");
@@ -57,27 +65,38 @@ function writeContent(yblock, content) {
57
65
  return;
58
66
  }
59
67
  const props = new Y.Map();
60
- for (const [name, value] of Object.entries(content.attrs)) props.set(name, value);
68
+ const merged = mergedPropsOf(content.type);
61
69
  yblock.set("props", props);
70
+ for (const [name, value] of Object.entries(content.attrs)) {
71
+ const spec = merged?.[name];
72
+ props.set(name, spec ? newMerged(value, spec) : value);
73
+ }
62
74
  if (content.inlineContent) insertInline(text, 0, content, 0, content.content.size);
63
75
  }
64
76
  /**
65
77
  * Bring a stored block's type, props and text from `before` to `after`.
66
78
  * Returns whether the stored block holds more than `after` shows (see `writeTable`).
67
79
  */
68
- function updateContent(yblock, before, after) {
80
+ function updateContent(yblock, before, after, current = before, plans) {
69
81
  if (before.type !== after.type) yblock.set("type", after.type.name);
70
- if (after.type.spec.rows) return writeTable(yblock, before.type === after.type ? before : null, after);
82
+ if (after.type.spec.rows) return writeTable(yblock, before.type === after.type ? before : null, after, plans);
71
83
  clearTable(yblock);
72
84
  const props = yblock.get("props");
73
- for (const [name, value] of Object.entries(after.attrs)) if (JSON.stringify(props.get(name)) !== JSON.stringify(value)) props.set(name, value);
85
+ const merged = mergedPropsOf(after.type);
86
+ for (const [name, value] of Object.entries(after.attrs)) {
87
+ const spec = merged?.[name];
88
+ if (spec) writeMerged(props, name, spec, value, before.type === after.type ? before.attrs[name] : void 0);
89
+ else if (before.type !== after.type || JSON.stringify(before.attrs[name]) !== JSON.stringify(value)) props.set(name, value);
90
+ }
74
91
  for (const name of Array.from(props.keys())) if (!(name in after.attrs)) props.delete(name);
75
92
  const text = yblock.get("text");
76
93
  if (!after.inlineContent) {
77
94
  if (text.length) text.delete(0, text.length);
78
95
  return false;
79
96
  }
80
- updateText(text, before, after);
97
+ const plan = plans?.get(text);
98
+ if (plan) plan(text);
99
+ else updateText(text, current, after);
81
100
  return false;
82
101
  }
83
102
  /**
@@ -180,6 +199,332 @@ function writeDoc(map, doc) {
180
199
  }
181
200
  });
182
201
  }
202
+ /**
203
+ * Write the changes from `before` to `after` into the map: the content of
204
+ * blocks that changed, blocks added and removed, and the places of the
205
+ * blocks that moved. Returns the blocks that hold more than `after` shows,
206
+ * to be read back (see `writeTable`). Call it inside a transaction.
207
+ */
208
+ function writeChanges(map, before, after, schema) {
209
+ const stale = [];
210
+ for (const [id, placed] of after.blocks) {
211
+ const yblock = map.get(id);
212
+ const old = before.blocks.get(id);
213
+ if (!yblock) {
214
+ const created = new Y.Map();
215
+ map.set(id, created);
216
+ writeContent(created, placed.content);
217
+ } else if (old && old.content !== placed.content) {
218
+ if (updateContent(yblock, old.content, placed.content)) stale.push(id);
219
+ } else if (!old) {
220
+ if (updateContent(yblock, contentFrom(yblock, schema), placed.content)) stale.push(id);
221
+ }
222
+ }
223
+ for (const id of before.blocks.keys()) if (!after.blocks.has(id)) map.delete(id);
224
+ for (const [key, ids] of after.children) {
225
+ const old = before.children.get(key);
226
+ if (old && old.length === ids.length && old.every((id, i) => ids[i] === id)) {
227
+ if (ids.every((id) => map.get(id).get("order") !== void 0)) continue;
228
+ }
229
+ const parent = key === ROOT ? null : key;
230
+ const keys = ids.map((id) => {
231
+ const yblock = map.get(id);
232
+ const order = yblock.get("order");
233
+ return yblock.get("parent") === parent && order !== void 0 ? order : null;
234
+ });
235
+ const next = orderKeys(keys);
236
+ ids.forEach((id, index) => {
237
+ if (keys[index] === next[index]) return;
238
+ const yblock = map.get(id);
239
+ if (yblock.get("parent") !== parent) yblock.set("parent", parent);
240
+ yblock.set("order", next[index]);
241
+ });
242
+ }
243
+ return stale;
244
+ }
245
+ /** The content node of block `id`. */
246
+ var contentOf = (index, id) => index.blocks.get(id).content;
247
+ /** A content node as a string, to tell equal blocks apart quickly. */
248
+ var keyOf = (content) => JSON.stringify(content.toJSON());
249
+ /**
250
+ * Pairs of `a` and `b` with equal keys, in the order of both: the equal
251
+ * start and end, then in the middle each of `a` with the first free equal
252
+ * one of `b`, keeping the longest run in order. Indexes into `a` and `b`;
253
+ * about linear, so a long document stays fast.
254
+ */
255
+ function equalPairs(a, b) {
256
+ const pairs = [];
257
+ let start = 0;
258
+ while (start < a.length && start < b.length && a[start] === b[start]) {
259
+ pairs.push([start, start]);
260
+ start++;
261
+ }
262
+ let endA = a.length;
263
+ let endB = b.length;
264
+ const tail = [];
265
+ while (endA > start && endB > start && a[endA - 1] === b[endB - 1]) {
266
+ endA--;
267
+ endB--;
268
+ tail.push([endA, endB]);
269
+ }
270
+ const free = /* @__PURE__ */ new Map();
271
+ for (let j = endB - 1; j >= start; j--) {
272
+ const list = free.get(b[j]);
273
+ if (list) list.push(j);
274
+ else free.set(b[j], [j]);
275
+ }
276
+ const middle = [];
277
+ for (let i = start; i < endA; i++) {
278
+ const j = free.get(a[i])?.pop();
279
+ if (j !== void 0) middle.push([i, j]);
280
+ }
281
+ const kept = keptIndexes(middle.map(([, j]) => String(j).padStart(10, "0")));
282
+ middle.forEach((pair, index) => kept.has(index) && pairs.push(pair));
283
+ for (let k = tail.length - 1; k >= 0; k--) pairs.push(tail[k]);
284
+ return pairs;
285
+ }
286
+ /** How alike two texts are, 0 to 1, by the pairs of letters they share. */
287
+ function likeness(a, b) {
288
+ if (a === b) return 1;
289
+ if (a.length < 2 || b.length < 2) return 0;
290
+ const pairs = /* @__PURE__ */ new Map();
291
+ for (let i = 0; i < a.length - 1; i++) {
292
+ const pair = a.slice(i, i + 2);
293
+ pairs.set(pair, (pairs.get(pair) ?? 0) + 1);
294
+ }
295
+ let shared = 0;
296
+ for (let i = 0; i < b.length - 1; i++) {
297
+ const pair = b.slice(i, i + 2);
298
+ const left = pairs.get(pair);
299
+ if (left) {
300
+ shared++;
301
+ pairs.set(pair, left - 1);
302
+ }
303
+ }
304
+ return 2 * shared / (a.length + b.length - 2);
305
+ }
306
+ /** How alike two blocks must be to count as one edited. */
307
+ var ALIKE = .4;
308
+ /** How many stored blocks ahead a block is looked for between two equal ones. */
309
+ var LOOK_AHEAD = 32;
310
+ /** How alike a block moved out of its order must be to count as the same one edited. */
311
+ var MOVED_ALIKE = .8;
312
+ /** The most comparisons spent on blocks moved out of their order. */
313
+ var MOVED_PAIRS = 4e4;
314
+ /**
315
+ * Which block of `before` each block of `after` stands for: by id when it
316
+ * keeps one, otherwise by place among the blocks of the same parent: first
317
+ * the blocks with equal content, kept in order, then between those, each
318
+ * block with the most alike one of its type further on (its text is
319
+ * changed in place). A block with nothing alike enough is new. Returns
320
+ * new id → old id.
321
+ */
322
+ function matchBlocks(before, after) {
323
+ const match = /* @__PURE__ */ new Map();
324
+ const used = /* @__PURE__ */ new Set();
325
+ for (const id of after.blocks.keys()) if (before.blocks.has(id)) {
326
+ match.set(id, id);
327
+ used.add(id);
328
+ }
329
+ const pair = (a, b) => {
330
+ match.set(a, b);
331
+ used.add(b);
332
+ };
333
+ const visit = (key, oldKey) => {
334
+ const ids = after.children.get(key) ?? [];
335
+ const fresh = ids.filter((id) => !match.has(id));
336
+ const olds = (oldKey === null ? [] : before.children.get(oldKey) ?? []).filter((id) => !used.has(id));
337
+ const anchors = equalPairs(fresh.map((id) => keyOf(contentOf(after, id))), olds.map((id) => keyOf(contentOf(before, id))));
338
+ let i = 0;
339
+ let j = 0;
340
+ for (const [ai, aj] of [...anchors, [fresh.length, olds.length]]) {
341
+ for (; i < ai; i++) {
342
+ const node = contentOf(after, fresh[i]);
343
+ const text = node.textContent;
344
+ let best = -1;
345
+ let score = ALIKE;
346
+ for (let k = j; k < Math.min(aj, j + LOOK_AHEAD); k++) {
347
+ const old = contentOf(before, olds[k]);
348
+ if (old.type !== node.type) continue;
349
+ const like = likeness(text, old.textContent);
350
+ if (like > score || best < 0 && like >= score) {
351
+ best = k;
352
+ score = like;
353
+ }
354
+ }
355
+ if (best >= 0) {
356
+ pair(fresh[i], olds[best]);
357
+ j = best + 1;
358
+ }
359
+ }
360
+ if (ai < fresh.length) pair(fresh[ai], olds[aj]);
361
+ i = ai + 1;
362
+ j = aj + 1;
363
+ }
364
+ const leftNew = fresh.filter((id) => !match.has(id));
365
+ const leftOld = olds.filter((id) => !used.has(id));
366
+ if (leftNew.length && leftOld.length) {
367
+ const byKey = /* @__PURE__ */ new Map();
368
+ for (let k = leftOld.length - 1; k >= 0; k--) {
369
+ const id = leftOld[k];
370
+ const text = keyOf(contentOf(before, id));
371
+ const list = byKey.get(text);
372
+ if (list) list.push(id);
373
+ else byKey.set(text, [id]);
374
+ }
375
+ const rest = [];
376
+ for (const id of leftNew) {
377
+ const old = byKey.get(keyOf(contentOf(after, id)))?.pop();
378
+ if (old) pair(id, old);
379
+ else rest.push(id);
380
+ }
381
+ if (rest.length * leftOld.length <= MOVED_PAIRS) for (const id of rest) {
382
+ const node = contentOf(after, id);
383
+ let best = null;
384
+ let score = MOVED_ALIKE;
385
+ for (const old of leftOld) {
386
+ if (used.has(old) || contentOf(before, old).type !== node.type) continue;
387
+ const like = likeness(node.textContent, contentOf(before, old).textContent);
388
+ if (like >= score) {
389
+ best = old;
390
+ score = like;
391
+ }
392
+ }
393
+ if (best) pair(id, best);
394
+ }
395
+ }
396
+ for (const id of ids) if (after.children.has(id)) visit(id, match.get(id) ?? null);
397
+ };
398
+ visit(ROOT, ROOT);
399
+ return match;
400
+ }
401
+ /** Every block id in `index` under `id`, at any depth. */
402
+ function descendants(index, id, out = []) {
403
+ for (const child of index.children.get(id) ?? []) {
404
+ out.push(child);
405
+ descendants(index, child, out);
406
+ }
407
+ return out;
408
+ }
409
+ /**
410
+ * Change the map from `base` to `doc`, leaving what changed in the map since
411
+ * `base` was read: blocks of `doc` are matched to those of `base` (see
412
+ * `matchBlocks`), and those of `base` to the stored ones; only blocks that
413
+ * differ between `base` and `doc` are written, removed or moved. Without
414
+ * `base`, the map as it is now. Call it inside a transaction.
415
+ */
416
+ function updateDoc(map, doc, schema, base) {
417
+ const current = readDoc(map, schema);
418
+ if (!current) return void writeDoc(map, doc);
419
+ const now = indexDoc(current);
420
+ const from = base ? indexDoc(base) : now;
421
+ const to = indexDoc(doc);
422
+ const toBase = matchBlocks(from, to);
423
+ const baseToNow = base ? matchBlocks(now, from) : null;
424
+ /** The stored block a block of `base` is now, if it still is. */
425
+ const nowOf = (old) => {
426
+ const there = baseToNow ? baseToNow.get(old) : old;
427
+ return there !== void 0 && now.blocks.has(there) ? there : null;
428
+ };
429
+ /** The stored block each block of `doc` is written to. */
430
+ const stored = /* @__PURE__ */ new Map();
431
+ for (const [id, old] of toBase) {
432
+ const there = nowOf(old);
433
+ if (there) stored.set(id, there);
434
+ }
435
+ const gone = /* @__PURE__ */ new Set();
436
+ const walk = (key, inGone) => {
437
+ for (const id of to.children.get(key) ?? []) {
438
+ const lost = toBase.get(id) !== void 0 && !stored.has(id) || inGone && !stored.has(id);
439
+ if (lost) gone.add(id);
440
+ walk(id, lost);
441
+ }
442
+ };
443
+ walk(ROOT, false);
444
+ for (const id of to.blocks.keys()) if (!stored.has(id) && !gone.has(id)) stored.set(id, map.has(id) ? createId() : id);
445
+ const kept = new Set(stored.values());
446
+ const inDoc = new Set(toBase.values());
447
+ const budget = createDiffBudget();
448
+ const contentUpdates = /* @__PURE__ */ new Map();
449
+ const textPlans = /* @__PURE__ */ new Map();
450
+ for (const [id, placed] of to.blocks) {
451
+ if (gone.has(id)) continue;
452
+ const target = stored.get(id);
453
+ const there = now.blocks.get(target)?.content;
454
+ if (!there) continue;
455
+ const old = toBase.get(id);
456
+ const was = old !== void 0 ? from.blocks.get(old).content : null;
457
+ const compared = was ? keepInlineComments(was, placed.content, budget) : placed.content;
458
+ if (was && compared.eq(was)) continue;
459
+ const before = was && was.type === there.type && was.type === placed.content.type ? was : there;
460
+ const after = before === was ? compared : keepInlineComments(before, placed.content, budget);
461
+ const yblock = map.get(target);
462
+ contentUpdates.set(id, {
463
+ before,
464
+ after,
465
+ current: there
466
+ });
467
+ if (after.type.spec.rows) prepareTableText(yblock, before.type === after.type ? before : null, after, textPlans, budget);
468
+ else if (after.inlineContent) {
469
+ const text = yblock.get("text");
470
+ const currentInline = there.inlineContent ? there : after.type.create(after.attrs, inlineFrom(text, schema, !!after.type.spec.code));
471
+ const baseInline = before.inlineContent ? before : currentInline;
472
+ textPlans.set(text, prepareTextFromBase(baseInline, after, currentInline, budget));
473
+ }
474
+ }
475
+ for (const old of from.blocks.keys()) {
476
+ const there = inDoc.has(old) ? null : nowOf(old);
477
+ if (!there) continue;
478
+ for (const id of [there, ...descendants(now, there)]) if (!kept.has(id) && map.has(id)) map.delete(id);
479
+ }
480
+ for (const [id, placed] of to.blocks) {
481
+ if (gone.has(id)) continue;
482
+ const target = stored.get(id);
483
+ const yblock = map.get(target);
484
+ if (!now.blocks.has(target)) {
485
+ const created = new Y.Map();
486
+ map.set(target, created);
487
+ writeContent(created, placed.content);
488
+ continue;
489
+ }
490
+ const update = contentUpdates.get(id);
491
+ if (update) updateContent(yblock, update.before, update.after, update.current, textPlans);
492
+ }
493
+ for (const [key, all] of to.children) {
494
+ if (gone.has(key)) continue;
495
+ const ids = all.filter((id) => !gone.has(id));
496
+ const parent = key === ROOT ? null : stored.get(key);
497
+ const oldParent = key === ROOT ? ROOT : toBase.get(key);
498
+ const siblings = oldParent === void 0 ? [] : from.children.get(oldParent) ?? [];
499
+ const at = new Map(siblings.map((id, index) => [id, index]));
500
+ const inBase = ids.map((id) => {
501
+ const old = toBase.get(id);
502
+ return old !== void 0 && at.has(old) ? String(at.get(old)).padStart(10, "0") : null;
503
+ });
504
+ const stays = keptIndexes(inBase);
505
+ const moved = new Set(ids.filter((_, index) => !stays.has(index)).map((id) => stored.get(id)));
506
+ if (!moved.size) continue;
507
+ const list = (now.children.get(parentKey(parent)) ?? []).filter((id) => map.has(id) && !moved.has(id));
508
+ let previous = null;
509
+ for (const id of ids) {
510
+ const target = stored.get(id);
511
+ if (moved.has(target)) list.splice(previous === null ? 0 : list.indexOf(previous) + 1, 0, target);
512
+ if (list.includes(target)) previous = target;
513
+ }
514
+ const keys = list.map((id) => {
515
+ const yblock = map.get(id);
516
+ const order = yblock.get("order");
517
+ return !moved.has(id) && yblock.get("parent") === parent && order !== void 0 ? order : null;
518
+ });
519
+ const next = orderKeys(keys);
520
+ list.forEach((id, index) => {
521
+ if (keys[index] === next[index]) return;
522
+ const yblock = map.get(id);
523
+ if (yblock.get("parent") !== parent) yblock.set("parent", parent);
524
+ yblock.set("order", next[index]);
525
+ });
526
+ }
527
+ }
183
528
  /** The document a map holds, read without an editor; `null` while the map is empty. */
184
529
  function readDoc(map, schema) {
185
530
  const children = placeAll(map);
@@ -258,46 +603,9 @@ function syncPlugin(binding) {
258
603
  if (!old || old.length !== ids.length || old.some((id, i) => ids[i] !== id)) structural = true;
259
604
  }
260
605
  if (structural) binding.stopCapturing?.();
261
- const stale = [];
262
- ydoc.transact(() => {
263
- for (const [id, placed] of after.blocks) {
264
- const yblock = map.get(id);
265
- const old = before.blocks.get(id);
266
- if (!yblock) {
267
- const created = new Y.Map();
268
- map.set(id, created);
269
- writeContent(created, placed.content);
270
- } else if (old && old.content !== placed.content) {
271
- if (updateContent(yblock, old.content, placed.content)) stale.push(id);
272
- } else if (!old) {
273
- if (updateContent(yblock, contentFrom(yblock, schema), placed.content)) stale.push(id);
274
- }
275
- cache.set(id, placed.content);
276
- }
277
- for (const id of before.blocks.keys()) if (!after.blocks.has(id)) {
278
- map.delete(id);
279
- cache.delete(id);
280
- }
281
- for (const [key, ids] of after.children) {
282
- const old = before.children.get(key);
283
- if (old && old.length === ids.length && old.every((id, i) => ids[i] === id)) {
284
- if (ids.every((id) => map.get(id).get("order") !== void 0)) continue;
285
- }
286
- const parent = key === ROOT ? null : key;
287
- const keys = ids.map((id) => {
288
- const yblock = map.get(id);
289
- const order = yblock.get("order");
290
- return yblock.get("parent") === parent && order !== void 0 ? order : null;
291
- });
292
- const next = orderKeys(keys);
293
- ids.forEach((id, index) => {
294
- if (keys[index] === next[index]) return;
295
- const yblock = map.get(id);
296
- if (yblock.get("parent") !== parent) yblock.set("parent", parent);
297
- yblock.set("order", next[index]);
298
- });
299
- }
300
- }, tracked ? binding.origin : binding.untracked);
606
+ const stale = ydoc.transact(() => writeChanges(map, before, after, schema), tracked ? binding.origin : binding.untracked);
607
+ for (const id of after.blocks.keys()) cache.set(id, after.blocks.get(id).content);
608
+ for (const id of before.blocks.keys()) if (!after.blocks.has(id)) cache.delete(id);
301
609
  if (structural) binding.stopCapturing?.();
302
610
  synced = after;
303
611
  if (stale.length) {
@@ -313,7 +621,8 @@ function syncPlugin(binding) {
313
621
  init: () => ({
314
622
  remote: false,
315
623
  tracked: false,
316
- own: false
624
+ own: false,
625
+ join: false
317
626
  }),
318
627
  apply(tr, value) {
319
628
  const meta = tr.getMeta(collaborationKey);
@@ -321,18 +630,22 @@ function syncPlugin(binding) {
321
630
  if (meta?.remote) return {
322
631
  remote: true,
323
632
  tracked: false,
324
- own: false
633
+ own: false,
634
+ join: false
325
635
  };
326
636
  const tracked = tr.getMeta("addToHistory") !== false;
327
637
  const own = tr.getMeta(ownStepKey) === true;
328
- return !!tr.getMeta("appendedTransaction") && !value.remote ? {
638
+ const join = tr.getMeta(joinStepKey) === true;
639
+ return !join && !!tr.getMeta("appendedTransaction") && !value.remote ? {
329
640
  remote: false,
330
641
  tracked: value.tracked || tracked,
331
- own: value.own || own
642
+ own: value.own || own,
643
+ join: value.join || join
332
644
  } : {
333
645
  remote: false,
334
646
  tracked,
335
- own
647
+ own,
648
+ join
336
649
  };
337
650
  }
338
651
  },
@@ -365,6 +678,7 @@ function syncPlugin(binding) {
365
678
  }
366
679
  binding.selectionBefore = placeOf(binding, previous.doc, previous.selection.head);
367
680
  if (state.own) binding.stopCapturing?.();
681
+ else if (state.join) binding.joinCapturing?.();
368
682
  writeMap(current, state.tracked);
369
683
  if (state.own) binding.stopCapturing?.();
370
684
  },
@@ -376,6 +690,6 @@ function syncPlugin(binding) {
376
690
  }
377
691
  });
378
692
  }
379
- export { collaborationKey, createBinding, readDoc, syncPlugin, writeDoc };
693
+ export { collaborationKey, createBinding, readDoc, syncPlugin, updateDoc, writeDoc };
380
694
 
381
695
  //# sourceMappingURL=binding.js.map