edytor 6.0.0 → 7.0.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.
Files changed (93) hide show
  1. package/README.md +3 -3
  2. package/dist/cloudflare/DocumentRoom.d.ts +77 -2
  3. package/dist/cloudflare/DocumentRoom.js +37 -1
  4. package/dist/cloudflare/index.d.ts +1 -1
  5. package/dist/cloudflare/index.js +1 -1
  6. package/dist/cloudflare/room/access.d.ts +6 -1
  7. package/dist/cloudflare/room/access.js +30 -4
  8. package/dist/cloudflare/room/admission.d.ts +11 -0
  9. package/dist/cloudflare/room/admission.js +43 -10
  10. package/dist/cloudflare/room/context.d.ts +11 -2
  11. package/dist/cloudflare/room/context.js +17 -6
  12. package/dist/cloudflare/room/forged.d.ts +1 -1
  13. package/dist/cloudflare/room/forged.js +11 -9
  14. package/dist/cloudflare/room/frames.d.ts +2 -0
  15. package/dist/cloudflare/room/frames.js +2 -0
  16. package/dist/cloudflare/room/presence.js +1 -4
  17. package/dist/cloudflare/room/reseed.d.ts +93 -0
  18. package/dist/cloudflare/room/reseed.js +500 -0
  19. package/dist/cloudflare/room/scheduler.d.ts +1 -1
  20. package/dist/cloudflare/room/scheduler.js +9 -1
  21. package/dist/cloudflare/room/shared.d.ts +10 -0
  22. package/dist/cloudflare/room/shared.js +10 -0
  23. package/dist/cloudflare/room/storage.d.ts +7 -0
  24. package/dist/cloudflare/room/storage.js +33 -1
  25. package/dist/components/Content.svelte +7 -16
  26. package/dist/components/Edytor.svelte +32 -8
  27. package/dist/components/Edytor.svelte.d.ts +10 -1
  28. package/dist/components/EdytorRoot.svelte +17 -0
  29. package/dist/components/EdytorRoot.svelte.d.ts +4 -0
  30. package/dist/components/Suggestion.svelte +1 -1
  31. package/dist/components/Text.svelte +36 -33
  32. package/dist/components/Text.svelte.d.ts +2 -1
  33. package/dist/crdt/attribution/block.d.ts +3 -0
  34. package/dist/crdt/attribution/block.js +3 -2
  35. package/dist/crdt/doc/suggest.d.ts +6 -2
  36. package/dist/crdt/doc/suggest.js +6 -5
  37. package/dist/crdt/edytor-doc.d.ts +1 -1
  38. package/dist/crdt/edytor-doc.js +1 -1
  39. package/dist/crdt/engine-room.d.ts +4 -0
  40. package/dist/crdt/engine-room.js +6 -0
  41. package/dist/crdt/index.d.ts +2 -1
  42. package/dist/crdt/protocol.d.ts +2 -2
  43. package/dist/crdt/protocol.js +2 -2
  44. package/dist/crdt/protocols/auth.d.ts +8 -1
  45. package/dist/crdt/protocols/auth.js +14 -1
  46. package/dist/crdt/providers/index.d.ts +1 -0
  47. package/dist/crdt/providers/index.js +22 -3
  48. package/dist/crdt/providers/room.d.ts +15 -0
  49. package/dist/crdt/providers/room.js +16 -1
  50. package/dist/crdt/providers/websocket.d.ts +4 -2
  51. package/dist/crdt/providers/websocket.js +36 -9
  52. package/dist/crdt/reseed.d.ts +127 -0
  53. package/dist/crdt/reseed.js +533 -0
  54. package/dist/crdt/restore.d.ts +13 -1
  55. package/dist/crdt/restore.js +8 -3
  56. package/dist/crdt/schema.d.ts +6 -0
  57. package/dist/crdt/schema.js +6 -0
  58. package/dist/crdt/structs.d.ts +8 -11
  59. package/dist/crdt/structs.js +25 -44
  60. package/dist/crdt/text/index/build.d.ts +3 -9
  61. package/dist/crdt/text/index/build.js +87 -181
  62. package/dist/crdt/vendor/yjs/UPSTREAM.md +101 -5
  63. package/dist/crdt/vendor/yjs/dts/index.d.ts +1 -1
  64. package/dist/crdt/vendor/yjs/dts/utils/Transaction.d.ts +10 -10
  65. package/dist/crdt/vendor/yjs/dts/utils/encoding.d.ts +1 -0
  66. package/dist/crdt/vendor/yjs/src/index.js +1 -1
  67. package/dist/crdt/vendor/yjs/src/structs/Item.js +24 -13
  68. package/dist/crdt/vendor/yjs/src/utils/Transaction.js +12 -10
  69. package/dist/crdt/vendor/yjs/src/utils/encoding.js +42 -1
  70. package/dist/edytor.svelte.js +1 -1
  71. package/dist/events/onBeforeInput.js +7 -0
  72. package/dist/events/onKeyDown.js +3 -2
  73. package/dist/index.d.ts +1 -1
  74. package/dist/index.js +1 -1
  75. package/dist/plugins/blockHandles/BlockHandles.svelte +32 -5
  76. package/dist/selection/selection.svelte.d.ts +1 -1
  77. package/dist/selection/selection.svelte.js +87 -106
  78. package/dist/session/attempt.d.ts +6 -0
  79. package/dist/session/attempt.js +6 -0
  80. package/dist/session/commands.js +49 -51
  81. package/dist/session/composition.svelte.js +19 -7
  82. package/dist/surface/attributes.d.ts +3 -3
  83. package/dist/surface/attributes.js +2 -2
  84. package/dist/surface/cells.d.ts +7 -0
  85. package/dist/surface/cells.js +7 -0
  86. package/dist/surface/observer.svelte.d.ts +7 -7
  87. package/dist/surface/observer.svelte.js +225 -313
  88. package/dist/surface/pin.svelte.d.ts +7 -10
  89. package/dist/surface/pin.svelte.js +12 -15
  90. package/dist/surface/projector.svelte.d.ts +6 -3
  91. package/dist/surface/projector.svelte.js +20 -12
  92. package/dist/themes/notion.css +7 -0
  93. package/package.json +1 -1
@@ -50,51 +50,32 @@ export const structAt = (Y, structs, clock) => {
50
50
  }
51
51
  };
52
52
  /**
53
- * Walk every struct an IdSet covers, in (client, clock) order: `fn(struct,
54
- * from, to)` gets the struct and the clock range `[from, to)` of it the set
55
- * covers (a struct can reach past a range a diff of two id sets cut). The
56
- * walk never splits a struct; `fn` returning `false` halts it.
57
- *
58
- * Returns `false` when coverage was INCOMPLETE — a client's struct list
59
- * was missing or a range start could not be located — so the caller can
60
- * fall back to an all-consumers/opaque answer instead of trusting a
61
- * partial walk. `clientsOf` throws first when the store itself is absent.
62
- */
63
- export const walkIdSet = (Y, doc, idSet, fn) => {
64
- const clients = clientsOf(doc);
65
- let complete = true;
66
- let stopped = false;
67
- idSet.clients.forEach((ranges, client) => {
68
- if (stopped)
69
- return;
70
- const structs = clients.get(client);
71
- if (structs === undefined) {
72
- complete = false;
73
- return;
74
- }
75
- for (const r of ranges.getIds()) {
76
- const end = r.clock + r.len;
77
- let i;
78
- try {
79
- i = Y.findIndexSS(structs, r.clock);
80
- }
81
- catch {
82
- complete = false;
83
- continue;
84
- }
85
- for (; i < structs.length; i++) {
86
- const s = structs[i];
87
- if (s.id.clock >= end)
88
- break;
89
- if (fn(s, Math.max(r.clock, s.id.clock), Math.min(end, s.id.clock + s.length)) === false) {
90
- stopped = true;
91
- return;
92
- }
93
- }
94
- }
95
- });
96
- return complete;
53
+ * Walk every struct the range `client:clock`, `len` units, covers, in clock
54
+ * order: `fn(struct, from, to)` gets the struct and the clock range `[from,
55
+ * to)` of it the range covers (a struct can reach past it). The walk never
56
+ * splits a struct; a client or a clock the store lacks walks nothing.
57
+ */
58
+ export const walkRange = (Y, doc, client, clock, len, fn) => {
59
+ const structs = clientsOf(doc).get(client) ?? [];
60
+ const end = clock + len;
61
+ let i = structs.length;
62
+ try {
63
+ if (clock < end)
64
+ i = Y.findIndexSS(structs, clock);
65
+ }
66
+ catch {
67
+ // past the client's clock: nothing to walk
68
+ }
69
+ for (; i < structs.length && structs[i].id.clock < end; i++) {
70
+ const s = structs[i];
71
+ fn(s, Math.max(clock, s.id.clock), Math.min(end, s.id.clock + s.length));
72
+ }
97
73
  };
74
+ /** Walk every struct an IdSet covers, in (client, clock) order (`walkRange` over each range). */
75
+ export const walkIdSet = (Y, doc, idSet, fn) => idSet.clients.forEach((ranges, client) => {
76
+ for (const r of ranges.getIds())
77
+ walkRange(Y, doc, client, r.clock, r.len, fn);
78
+ });
98
79
  /**
99
80
  * The id the local `redone` chain leads `id` to: the copy an undo or redo
100
81
  * on THIS replica re-created of a deleted item (redo copies chain), or `id`
@@ -10,7 +10,6 @@
10
10
  */
11
11
  import type { EngineDoc, EngineNode, EngineTransaction } from '../../engine-api.js';
12
12
  import { type BlockId, type BlockRec, type ChildSlot, type DisplayOwnership, type ResolvedPlacement } from '../../placement/model.js';
13
- import { type IdSetLike } from '../../structs.js';
14
13
  import { type LiveRow, type RowItem } from '../rows.js';
15
14
  import type { ContentRun, RunView } from '../runs.js';
16
15
  import { type Cached } from './shared.js';
@@ -18,8 +17,7 @@ import { type Claim, type Owner, type Stream } from '../model.js';
18
17
  import { type Edge, type SlotRef } from '../../placement/resolve.js';
19
18
  export type TextEdit = {
20
19
  item: RowItem;
21
- len: number;
22
- del: boolean;
20
+ n: number;
23
21
  };
24
22
  export type TextEdits = {
25
23
  edits: TextEdit[];
@@ -70,13 +68,9 @@ export type Published = {
70
68
  };
71
69
  /** A transaction as the fold reads it. */
72
70
  export type Tx = {
73
- insertSet?: IdSetLike;
74
- deleteSet?: IdSetLike;
75
71
  changed?: EngineTransaction['changed'];
76
- /** The visibility changes hold records made, in order (fork patch YP20). */
77
- holds?: number[];
78
- /** The units whose mover changed, in order (fork patch YP22). */
79
- moves?: number[];
72
+ /** What the transaction did to items, in order: per item `kind, client, clock, len` (fork patch YP33). */
73
+ log?: number[];
80
74
  };
81
75
  /** The index's self-checks (`indexChecks`), run after each fold and each report. */
82
76
  export type SelfChecks = {
@@ -1,6 +1,6 @@
1
1
  import { bySlot, childrenIndex, displaySlotOf, documentOrder, REGISTRY_KEY } from '../../placement/model.js';
2
2
  import { CONTENT, CONTENT_NODE, DOC_DATA_ROOT, hasDeleteMark, hasWithdrawMark, isNodeLike, NONCE } from '../../schema.js';
3
- import { changedUnder, clientsOf, engineOps, firstItem, itemOf, queuedTransactions, sequenceLength, structAt, walkIdSet } from '../../structs.js';
3
+ import { changedUnder, engineOps, firstItem, itemOf, queuedTransactions, sequenceLength, walkRange } from '../../structs.js';
4
4
  import { gapOfItem, headOf, placeRow, rowLength, rowOf, segmentAt, segmentClaims, segmentOfCut, segmentOfGap, shiftGap, streamOfSegment } from '../rows.js';
5
5
  import { addTo, CONTENT_ATTR, DATA_SEQ_FACET, dataOf, dropFrom, EMPTY_IDS, EMPTY_RUNS, ENTRY_FACET, facetOf, indexChecks, keyOf, runEquals, sameShape, SEQ_FACET, typeAttr } from './shared.js';
6
6
  import { cloneJsonSafe, sameIds } from '../../../utils/json.js';
@@ -1782,7 +1782,6 @@ const buildIndex = (doc) => {
1782
1782
  // THE fold: one pass over a transaction's changed `(type, parentSub)` pairs
1783
1783
  // into the index, once at commit and, for read-your-writes, over the
1784
1784
  // pending part of every transaction whose commit fold has not run.
1785
- const ops = engineOps(Y);
1786
1785
  const NO_EDITS = Object.freeze({
1787
1786
  edits: [],
1788
1787
  scan: false,
@@ -1797,26 +1796,28 @@ const buildIndex = (doc) => {
1797
1796
  return true;
1798
1797
  return false;
1799
1798
  };
1800
- /** The struct holding id `client:clock` (unsplit), `null` when the store holds none. */
1801
- const structOf = (client, clock) => structAt(Y, clientsOf(doc).get(client) ?? [], clock);
1802
1799
  /**
1803
- * The edits the id sets `ins`/`del`, the hold log `log` and the move log
1804
- * `moves` (what the fold has not folded yet) hold: per text, its edits
1805
- * (a log entry is a unit a hold record hid or showed, fork patch YP20:
1806
- * `client, clock, ±len`, in order); per sequence, what changed in it
1807
- * (`seq`): the slots written and removed, the records written and
1808
- * removed and the units whose mover changed name the blocks to re-place.
1809
- * Reading the sequences writes the slot map (the slots written and
1810
- * removed, `putSlot`) and lists the ids whose home to read again
1811
- * (`rehome`).
1800
+ * What a transaction's log past a read's cursor did (fork patch YP33:
1801
+ * per run of items `kind, client, clock, len`, in order), in one pass: per text
1802
+ * (`texts`), its edits (the units each item showed or hid, in order); per
1803
+ * sequence (`seqs`), what changed in it: the slots written and removed, the
1804
+ * records written and removed and the units whose mover changed name
1805
+ * the blocks to re-place; with `pairs` (a read's fold), the `(type, key)`
1806
+ * pairs it changed, and the structs it named (`structs`). Reading the sequences writes the
1807
+ * slot map (the slots written and removed, `putSlot`) and lists the ids
1808
+ * whose home to read again (`rehome`).
1812
1809
  */
1813
- const makeEditIndex = (ins, del, log = [], moves = []) => {
1814
- let seqs;
1810
+ const makeEditIndex = (log, [from, seen], pairs) => {
1811
+ const texts = new Map();
1812
+ const seqs = new Map();
1815
1813
  const rehome = new Set();
1816
- const seqOf = (node) => {
1817
- return getOr(seqs, node, () => ({ blocks: new Set(), data: false }));
1814
+ let structs = 0;
1815
+ const seqOf = (node) => getOr(seqs, node, () => ({ blocks: new Set(), data: false }));
1816
+ const note = (s) => {
1817
+ if (pairs && isNodeLike(s.parent))
1818
+ getOr(pairs, s.parent, () => new Set()).add(s.parentSub);
1818
1819
  };
1819
- /** Note unit `c:k` (`value`) of the sequence of `node`: a block slot names its block, a data slot its data. */
1820
+ /** Note unit `c:k` (`value`) of a sequence: a block slot names its block, a data slot its data. */
1820
1821
  const noteUnit = (e, owner, value, c, k, inserted) => {
1821
1822
  if (isBlockSlot(value)) {
1822
1823
  e.blocks.add(slotOwner(registry, kept, value.b, c));
@@ -1828,102 +1829,58 @@ const buildIndex = (doc) => {
1828
1829
  else if (isDataSlot(value))
1829
1830
  e.data = true;
1830
1831
  };
1831
- const visitSeq = (s, from, to, inserted) => {
1832
- const parent = s.parent;
1833
- if (s.parentSub !== null || !isNodeLike(parent) || parent.name === CONTENT_NODE)
1834
- return;
1835
- const e = seqOf(parent);
1836
- const owner = ownerOfNode(registry, kept, parent);
1837
- const it = s;
1838
- if (it.content === undefined)
1839
- return;
1840
- if (isRecord(it)) {
1841
- const ids = it.content.ids;
1842
- for (let i = 0; i < ids.length; i += 3)
1843
- for (let j = 0; j < ids[i + 2]; j++) {
1844
- const u = unitOf(doc, { c: ids[i], k: ids[i + 1] + j });
1845
- if (u !== null)
1846
- noteUnit(e, owner, u.value, ids[i], ids[i + 1] + j, null);
1847
- }
1848
- }
1849
- else if (it.content.arr !== undefined)
1850
- for (let k = from; k < to; k++)
1851
- noteUnit(e, owner, it.content.arr[k - s.id.clock], s.id.client, k, inserted);
1852
- };
1853
- const buildSeqs = () => {
1854
- if (seqs !== undefined)
1855
- return seqs;
1856
- seqs = new Map();
1857
- if (ins === undefined || del === undefined)
1858
- return seqs;
1859
- walkIdSet(Y, doc, ins, (s, from, to) => visitSeq(s, from, to, true));
1860
- walkIdSet(Y, doc, del, (s, from, to) => visitSeq(s, from, to, false));
1861
- for (let i = 0; i < moves.length; i += 3) {
1862
- const s = structOf(moves[i], moves[i + 1]);
1863
- if (s === null)
1864
- continue;
1865
- visitSeq(s, moves[i + 1], moves[i + 1] + moves[i + 2], null);
1866
- // Where the unit shows now: that sequence changed too.
1832
+ /** Walk `n` units of entry `i` from clock `k` (`n`'s sign is the entry's). */
1833
+ const run = (i, k, n) => {
1834
+ const kind = log[i];
1835
+ walkRange(Y, doc, log[i + 1], k, Math.abs(n), (s, a, b) => {
1836
+ structs++;
1837
+ note(s);
1838
+ // A unit a record moved: where it shows now changed too.
1867
1839
  const mover = s.mover;
1868
- if (mover)
1840
+ if (mover) {
1841
+ note(mover);
1869
1842
  seqOf(mover.parent);
1870
- }
1871
- return seqs;
1872
- };
1873
- let built;
1874
- const build = () => {
1875
- if (built !== undefined)
1876
- return built;
1877
- if (ins === undefined || del === undefined)
1878
- return (built = null);
1879
- const out = new Map();
1880
- const edit = (s, from, to, deleted) => {
1843
+ }
1881
1844
  const parent = s.parent;
1882
- if (s.parentSub !== null || !isNodeLike(parent))
1845
+ const it = s;
1846
+ if (s.parentSub !== null || !isNodeLike(parent) || it.content === undefined)
1847
+ return;
1848
+ if (parent.name === CONTENT_NODE) {
1849
+ if (kind > 1)
1850
+ return;
1851
+ const e = getOr(texts, parent, () => ({ edits: [], scan: false, opaque: false }));
1852
+ // A format marker moves no unit; its effect reaches past the edit.
1853
+ if (s.countable !== true)
1854
+ e.opaque = true;
1855
+ else if (holdsBoundary(s, a, b))
1856
+ e.scan = true;
1857
+ else
1858
+ e.edits.push({ item: s, n: n < 0 ? a - b : b - a });
1883
1859
  return;
1884
- const e = getOr(out, parent, () => ({ edits: [], scan: false, opaque: false }));
1885
- // A format marker moves no unit; its effect reaches past the edit.
1886
- if (s.countable !== true)
1887
- return void (e.opaque = true);
1888
- if (holdsBoundary(s, from, to))
1889
- return void (e.scan = true);
1890
- e.edits.push({ item: s, len: to - from, del: deleted });
1891
- };
1892
- const visit = (idSet, deleted) => walkIdSet(Y, doc, idSet, (s, from, to) => {
1893
- // Written and removed (or held) within the fold: nothing moved.
1894
- if (!(deleted ? ins.has(s.id.client, from) : s.deleted))
1895
- edit(s, from, to, deleted);
1896
- });
1897
- // In order, after the inserts and before the deletes (each gap then
1898
- // holds what an edit takes); a unit written within the fold shows or
1899
- // not as its insert says.
1900
- const held = () => {
1901
- for (let i = 0; i < log.length; i += 3) {
1902
- const s = structOf(log[i], log[i + 1]);
1903
- if (s === null)
1904
- return false;
1905
- const n = log[i + 2];
1906
- if (!ins.has(log[i], log[i + 1]))
1907
- edit(s, log[i + 1], log[i + 1] + Math.abs(n), n < 0);
1908
1860
  }
1909
- return true;
1910
- };
1911
- built = visit(ins, false) && held() && visit(del, true) ? out : null;
1912
- return built;
1913
- };
1914
- return {
1915
- /** `node`'s edits (`null`: unknown — rescan, every consumer recomputes). */
1916
- text: (node) => {
1917
- const map = build();
1918
- return map === null ? null : (map.get(node) ?? NO_EDITS);
1919
- },
1920
- /** What changed in `node`'s sequence (`undefined`: nothing). */
1921
- seq: (node) => buildSeqs().get(node),
1922
- /** The sequences the fold changed. */
1923
- seqNodes: () => buildSeqs().keys(),
1924
- /** The ids whose home to read again (their slots changed). */
1925
- rehome
1861
+ const e = seqOf(parent);
1862
+ const owner = ownerOfNode(registry, kept, parent);
1863
+ const inserted = kind > 1 ? null : n > 0;
1864
+ if (isRecord(it)) {
1865
+ const ids = it.content.ids;
1866
+ for (let i = 0; i < ids.length; i += 3)
1867
+ for (let j = 0; j < ids[i + 2]; j++) {
1868
+ const u = unitOf(doc, { c: ids[i], k: ids[i + 1] + j });
1869
+ if (u !== null)
1870
+ noteUnit(e, owner, u.value, ids[i], ids[i + 1] + j, null);
1871
+ }
1872
+ }
1873
+ else if (it.content.arr !== undefined)
1874
+ for (let k = a; k < b; k++)
1875
+ noteUnit(e, owner, it.content.arr[k - s.id.clock], s.id.client, k, inserted);
1876
+ });
1926
1877
  };
1878
+ // The units the engine added to the entry a read folded last (`seen`: its length then), then the rest.
1879
+ if (from > 0 && log[from - 1] !== seen)
1880
+ run(from - 4, log[from - 2] + Math.abs(seen), log[from - 1] - seen);
1881
+ for (let i = from; i < log.length; i += 4)
1882
+ run(i, log[i + 2], log[i + 3]);
1883
+ return { texts, seqs, structs, rehome };
1927
1884
  };
1928
1885
  const runs = (b) => {
1929
1886
  // Read-your-writes: fold the open transaction's pending writes first.
@@ -1986,7 +1943,7 @@ const buildIndex = (doc) => {
1986
1943
  ensureRec(id);
1987
1944
  const rec = blocks.get(id);
1988
1945
  // A data slot in the block's sequence: its arrays' items changed (the seq pass seeded the blocks).
1989
- if (kinds.has('seq') && rec && editsOf.seq(rec.node)?.data)
1946
+ if (kinds.has('seq') && rec && editsOf.seqs.get(rec.node)?.data)
1990
1947
  kinds.add('meta');
1991
1948
  if (kinds.has('meta') && rec) {
1992
1949
  const was = rec.type;
@@ -2040,7 +1997,7 @@ const buildIndex = (doc) => {
2040
1997
  if (!fresh) {
2041
1998
  for (const e of t.edits) {
2042
1999
  const gap = gapOfItem(row, e.item);
2043
- if (gap < 0 || !shiftGap(row, gap, e.del ? -e.len : e.len)) {
2000
+ if (gap < 0 || !shiftGap(row, gap, e.n)) {
2044
2001
  fresh = true;
2045
2002
  break;
2046
2003
  }
@@ -2219,7 +2176,7 @@ const buildIndex = (doc) => {
2219
2176
  continue;
2220
2177
  // A sequence edit of the block's own text, or a change inside one of its atoms.
2221
2178
  const list = getOr(edits, id, () => []);
2222
- list.push(seq ? editsOf.text(type) : null);
2179
+ list.push(seq ? (editsOf.texts.get(type) ?? NO_EDITS) : null);
2223
2180
  }
2224
2181
  }
2225
2182
  const ctx = {
@@ -2230,8 +2187,7 @@ const buildIndex = (doc) => {
2230
2187
  parents: new Set()
2231
2188
  };
2232
2189
  // The sequences (the root's, the blocks'): their slots index the blocks' homes, their changes seed the placements.
2233
- for (const node of editsOf.seqNodes()) {
2234
- const ch = editsOf.seq(node);
2190
+ for (const [node, ch] of editsOf.seqs) {
2235
2191
  seqEpoch++;
2236
2192
  for (const b of ch.blocks)
2237
2193
  placementSeeds.add(b);
@@ -2289,71 +2245,31 @@ const buildIndex = (doc) => {
2289
2245
  }
2290
2246
  return touched;
2291
2247
  };
2292
- /** Each transaction's folded part: copies of its id sets, the hold and move logs' folded lengths, and the watermark. */
2248
+ /** Each transaction's folded part (`Cursor`). */
2293
2249
  const cursors = new WeakMap();
2250
+ const NONE = [0];
2251
+ /** Whether `log` holds entries or units past `cursor`. */
2252
+ const past = (log, [from, seen]) => log.length !== from || log[from - 1] !== seen;
2294
2253
  /** The transactions whose commit fold ran. */
2295
2254
  const committed = new WeakSet();
2296
- const lengthOf = (set) => {
2297
- let n = 0;
2298
- set.clients.forEach((ranges) => {
2299
- for (const r of ranges.getIds())
2300
- n += r.len;
2301
- });
2302
- return n;
2303
- };
2304
- /** How much a transaction wrote so far: its id sets' lengths and its hold and move logs'. */
2305
- const watermark = (tr) => lengthOf(tr.insertSet) +
2306
- lengthOf(tr.deleteSet) +
2307
- (tr.holds?.length ?? 0) +
2308
- (tr.moves?.length ?? 0);
2309
2255
  /**
2310
- * Fold the pending part of `tr` — the structs its insert/delete sets
2311
- * gained since the last fold — before a read inside it (read-your-
2312
- * writes; `transaction.changed` does not grow on a second edit to an
2256
+ * Fold the pending part of `tr` — the entries its log gained since the
2257
+ * last fold — before a read inside it (read-your-writes;
2258
+ * `transaction.changed` does not grow on a second edit to an
2313
2259
  * already-changed type).
2314
2260
  */
2315
2261
  const syncPending = (tr) => {
2316
- if (tr?.insertSet === undefined || tr.deleteSet === undefined)
2317
- return false;
2318
- let cursor = cursors.get(tr);
2319
- if (cursor === undefined)
2320
- cursors.set(tr, (cursor = { ins: ops.idSet(), del: ops.idSet(), holds: 0, moves: 0, mark: 0 }));
2321
- const mark = watermark(tr);
2322
- if (mark === cursor.mark)
2262
+ const log = tr?.log;
2263
+ const cursor = cursors.get(tr) ?? NONE;
2264
+ if (log === undefined || !past(log, cursor))
2323
2265
  return false;
2324
- const ins = ops.diff(tr.insertSet, cursor.ins);
2325
- const del = ops.diff(tr.deleteSet, cursor.del);
2326
- const log = (tr.holds ?? []).slice(cursor.holds);
2327
- const moves = (tr.moves ?? []).slice(cursor.moves);
2328
- ops.insertInto(cursor.ins, ins);
2329
- ops.insertInto(cursor.del, del);
2330
- cursor.holds += log.length;
2331
- cursor.moves += moves.length;
2332
- cursor.mark = mark;
2266
+ cursors.set(tr, [log.length, log[log.length - 1]]);
2333
2267
  for (const f of frames)
2334
2268
  f.wrote = true;
2335
- const changed = new Map();
2336
- const note = (s) => {
2337
- foldStats.structs++;
2338
- if (!isNodeLike(s.parent))
2339
- return;
2340
- const subs = getOr(changed, s.parent, () => new Set());
2341
- subs.add(s.parentSub);
2342
- };
2343
- walkIdSet(Y, doc, ins, note);
2344
- walkIdSet(Y, doc, del, note);
2345
- for (const l of [log, moves])
2346
- for (let i = 0; i < l.length; i += 3) {
2347
- const s = structOf(l[i], l[i + 1]);
2348
- if (s === null)
2349
- continue;
2350
- note(s);
2351
- // A unit a record moved: where it shows now changed too (a held unit has no mover).
2352
- const mover = s.mover;
2353
- if (mover)
2354
- note(mover);
2355
- }
2356
- fold(changed, makeEditIndex(ins, del, log, moves));
2269
+ const pairs = new Map();
2270
+ const edits = makeEditIndex(log, cursor, pairs);
2271
+ foldStats.structs += edits.structs;
2272
+ fold(pairs, edits);
2357
2273
  return true;
2358
2274
  };
2359
2275
  const openTx = () => doc._transaction;
@@ -2371,10 +2287,7 @@ const buildIndex = (doc) => {
2371
2287
  syncPending(tr);
2372
2288
  };
2373
2289
  /** Some queued transaction holds writes no fold has seen. */
2374
- const unfolded = () => queuedTransactions(doc).some((tr) => !committed.has(tr) &&
2375
- tr.insertSet !== undefined &&
2376
- tr.deleteSet !== undefined &&
2377
- watermark(tr) !== (cursors.get(tr)?.mark ?? 0));
2290
+ const unfolded = () => queuedTransactions(doc).some((tr) => !committed.has(tr) && tr.log !== undefined && past(tr.log, cursors.get(tr) ?? NONE));
2378
2291
  /**
2379
2292
  * The commit of a transaction that changed the registry or a node under
2380
2293
  * it: fold `transaction.changed` once (the report publishes from `update`).
@@ -2382,19 +2295,12 @@ const buildIndex = (doc) => {
2382
2295
  const onCommit = (tr) => {
2383
2296
  if (!changedUnder(tr, [registry]))
2384
2297
  return;
2385
- let [ins, del] = [tr.insertSet, tr.deleteSet];
2386
2298
  // The writes a read folded already moved the rows: only the rest
2387
2299
  // are edits now (the facets fold again, idempotent).
2388
- const cursor = cursors.get(tr);
2389
- if (cursor !== undefined && ins !== undefined && del !== undefined) {
2390
- ins = ops.diff(ins, cursor.ins);
2391
- del = ops.diff(del, cursor.del);
2392
- }
2393
- const log = (tr.holds ?? []).slice(cursor?.holds ?? 0);
2394
- const moves = (tr.moves ?? []).slice(cursor?.moves ?? 0);
2300
+ const cursor = cursors.get(tr) ?? NONE;
2395
2301
  cursors.delete(tr);
2396
2302
  committed.add(tr);
2397
- fold(tr.changed ?? new Map(), makeEditIndex(ins, del, log, moves));
2303
+ fold(tr.changed ?? new Map(), makeEditIndex(tr.log ?? [], cursor));
2398
2304
  };
2399
2305
  // ══ The change report ═══════════════════════════════════════════
2400
2306
  // The change report: the fold against the last published index, one per
@@ -33,7 +33,7 @@ is no longer a dependency of any kind: `tests/packed-consumer/smoke.js` and
33
33
  ## Layout
34
34
 
35
35
  ```text
36
- src/ upstream src/, plus patches YP1 (import specifiers), YP4, YP5, YP7, YP8 (pruning), YP9–YP15, YP16 (no delta layer), YP17 (one update encoding), YP18 (no undo manager), YP19 (only what edytor calls), YP20 (hold records), YP21 (claim items), YP22 (move records), YP23 (a block set's trim), YP24 (a string table per update), YP25 (paired marks written in parts), YP26 (a client table per update), YP27 (the parent kind in the info byte), YP28 (large integers in content values), YP29 (a collected mark or claim keeps its class) and YP30 (a transaction's `beforeCommit`)
36
+ src/ upstream src/, plus patches YP1 (import specifiers), YP4, YP5, YP7, YP8 (pruning), YP9–YP15, YP16 (no delta layer), YP17 (one update encoding), YP18 (no undo manager), YP19 (only what edytor calls), YP20 (hold records), YP21 (claim items), YP22 (move records), YP23 (a block set's trim), YP24 (a string table per update), YP25 (paired marks written in parts), YP26 (a client table per update), YP27 (the parent kind in the info byte), YP28 (large integers in content values), YP29 (a collected mark or claim keeps its class), YP30 (a transaction's `beforeCommit`), YP31 (a hold inside a nested node holds nothing), YP32 (a delete that proposes), YP33 (one transaction log) and YP34 (an update read at an earlier state)
37
37
  global.d.ts upstream global.d.ts, plus patches YP1, YP8 and YP19
38
38
  dts/ generated TypeScript declarations (not upstream source — see below)
39
39
  LICENSE upstream MIT license, verbatim
@@ -1022,7 +1022,8 @@ Patch (new code delimited by `// YP20 begin` / `// YP20 end`, edits marked `// Y
1022
1022
  text a room lost is stored and acknowledged at once. A GC struct takes
1023
1023
  nothing. An item that starts or stops being held (and is not removed)
1024
1024
  moves its parent's length, drops its search markers, joins
1025
- `transaction.changed` and is logged in `transaction.holds`
1025
+ `transaction.changed` and is logged in `transaction.holds` (since YP33:
1026
+ `transaction.log`, kind 1)
1026
1027
  (`client, clock, ±len`, in order: the index's edits).
1027
1028
  - **`YNode#delete(index, length, into, fresh)`**: with `into` (the records
1028
1029
  list) the live units are held by one record appended there instead of
@@ -1038,7 +1039,8 @@ Edytor side: a text delete passes the records list (`crdt/text/model.ts`
1038
1039
  copies the record (never the text), an undo of typing holds the typed text,
1039
1040
  a dropped step removes what this writer's records of it hold
1040
1041
  (`hist.delete-marks.release`); the purge removes what a record older than
1041
- the horizon holds; the index folds `transaction.holds`. Removed:
1042
+ the horizon holds; the index folds `transaction.holds` (since YP33, the one
1043
+ `transaction.log`). Removed:
1042
1044
  `bindCrdt(Y).doc.keepCopies`, `PurgeReport.records`, the restoration
1043
1045
  records root, the engine object's `ContentAny`.
1044
1046
 
@@ -1172,7 +1174,8 @@ Patch (new code delimited by `// YP22 begin` / `// YP22 end`, edits marked `// Y
1172
1174
  pending holds only. A
1173
1175
  unit that starts or stops being moved (and is not gone) moves
1174
1176
  its home sequence's length and drops its search markers; one whose mover
1175
- changed is logged in `transaction.moves` (`client, clock, len`, in
1177
+ changed is logged in `transaction.moves` (since YP33: `transaction.log`,
1178
+ kind 2; `client, clock, len`, in
1176
1179
  order), and its home and its new mover's sequences are marked changed.
1177
1180
  - **`doc._clock`** (was YP13's `_markClock`): the greatest Lamport
1178
1181
  timestamp of a mark or a move this document integrated; a local mark or
@@ -1537,7 +1540,7 @@ Patch (new code delimited by `// YP31 begin` / `// YP31 end`, edits marked `// Y
1537
1540
  records list, `textdel`). `ContentHold#integrate` and `ContentHold#delete`
1538
1541
  hold and release only then; inside a nested node's sequence the record is
1539
1542
  a list of ids: it moves no length, waits in no `pendingHolds`, logs
1540
- nothing in `transaction.holds`, and its delete (an undo, a reviewer's
1543
+ nothing in `transaction.holds` (YP33: `transaction.log`), and its delete (an undo, a reviewer's
1541
1544
  withdrawal, its node's delete) releases nothing. `YNode#hold(ids)` on a
1542
1545
  nested node appends one such list entry.
1543
1546
 
@@ -1598,6 +1601,99 @@ schedules with the index's self-checks on), `suggesting-input.test.tsx`
1598
1601
  (typing over a selection, a paste, Backspace and Delete over struck text),
1599
1602
  the YP20 rows, the upstream suite unchanged.
1600
1603
 
1604
+ ### YP33 — one log of what a transaction did to items (`src/utils/Transaction.js`, `src/structs/Item.js`)
1605
+
1606
+ Reason: edytor's index folds each transaction into its derived facts (rows,
1607
+ streams, slots, placements) once at commit and, for read-your-writes, over
1608
+ the part of an open transaction a read has not folded. The engine logged
1609
+ the holds (YP20, `transaction.holds`) and the mover changes (YP22,
1610
+ `transaction.moves`) in order, but the inserts and removals only as id sets,
1611
+ so the fold reconstructed them: it kept, per open transaction, copies of the
1612
+ insert and delete sets and diffed them at every read, walked both sets'
1613
+ structs again for the texts and again for the sequences, and decided by set
1614
+ membership which edits cancelled within a fold (about 1.6 KB minified in
1615
+ every app, `makeEditIndex` and `syncPending` in `crdt/text/index/build.ts`).
1616
+ The engine now logs the edits in the same log as the holds and the moves,
1617
+ and the fold reads one log from a cursor. In memory only: nothing encoded,
1618
+ no schema change.
1619
+
1620
+ Patch (new code delimited by `// YP33 begin` / `// YP33 end`, edits marked `// YP33`):
1621
+
1622
+ - **`transaction.log`** (replaces YP20's `holds` and YP22's `moves`): per
1623
+ run of one writer's items `kind, client, clock, len`, in the order the
1624
+ transaction did them; an item that continues the last entry (its kind,
1625
+ its client, the next clock, its sign) extends it, so a bulk insert or
1626
+ delete is a few entries, as the id sets' ranges were. Kind 0, an edit:
1627
+ `Item#integrate` logs `+length` before its content integrates (a node's
1628
+ children follow it; nothing for an item integrated removed, a
1629
+ `ContentDeleted`), `Item#delete` `-length`; kind 1,
1630
+ a hold record's change of visibility (YP20's entries, unchanged: a held
1631
+ item removed logs its give-back, so an item's kinds 0 and 1 sum to its
1632
+ visibility change); kind 2, a mover change (YP22's entries, unchanged).
1633
+ - **`logItem(tr, kind, item, len, mark)`** (`Item.js`): the one writer (it
1634
+ extends a run's entry), which also marks the parent changed (it replaces
1635
+ `logHold` and `Item#delete`'s `addChangedTypeToTransaction`; `logMove`
1636
+ calls it). `Item#integrate` logs with `mark` false and keeps its own
1637
+ mark, after its content, so `transaction.changed` keeps its order.
1638
+
1639
+ The insert and delete sets, `transaction.changed` and every upstream path
1640
+ are untouched: the log is a list the engine appends to and never reads.
1641
+
1642
+ Edytor side: the fold keeps one number per open transaction (the length of
1643
+ its log a read folded and that entry's last length then, so the units the
1644
+ engine added to it since are folded too), reads each entry's structs once
1645
+ (`walkRange`, the walk `walkIdSet` now runs per range) and replays the units each item showed
1646
+ or hid in order, so an edit made and undone within one fold moves the row
1647
+ by nothing; the sequences read kind 0 as their slots and records written and
1648
+ removed, kind 2 as their units whose mover changed. Removed: the copied and
1649
+ diffed id sets, the watermark, the second and third walks, the membership
1650
+ tests, the incomplete-walk fallback.
1651
+
1652
+ Measured (`scripts/bundle-size.mjs`, own minified): minimal 425,374 →
1653
+ 424,034 (−1,340 B; gzip −290), every plugin 675,230 → 673,890 (−1,340),
1654
+ `edytor/export` unchanged; the engine's share +106 B (one log writer, which
1655
+ extends a run's entry). A first version logged one entry per item: the
1656
+ fold walked them one by one where the id sets gave it ranges, and a 5,000
1657
+ block construction and a 40-block range delete got slower (`bench/scale.js`);
1658
+ the runs bring both back.
1659
+
1660
+ Oracle: `src/tests/crdt/yp33-log.test.ts` (edits, holds and a removed held
1661
+ item's give-back, a move, a remote update with an item integrated removed),
1662
+ the YP20, YP22 and YP31 rows reading their entries of the log, the index's
1663
+ self-checks after every fold (they rebuild from the replicated state and
1664
+ never read the log), the fold's counters (`foldedStructs`, the scale rows),
1665
+ the random corpus, the upstream suite unchanged.
1666
+
1667
+ ### YP34 — an update read at an earlier state (`src/utils/encoding.js`, `src/index.js`)
1668
+
1669
+ Reason: a re-seed's rebase (`room.reseed.rebase`, `crdt/reseed.ts`) gets an
1670
+ offline client's whole old document and must split it into the part the
1671
+ room held at the cut (the client's base) and the rest (its unsaved edits).
1672
+ `diffUpdate(update, sv)` gives the rest; nothing gave the base, and edytor
1673
+ has no access to the lazy struct reader and writer it would need (they are
1674
+ not exported). The engine now gives the counterpart. Encoding unchanged, no
1675
+ schema change.
1676
+
1677
+ Patch (new code delimited by `// YP34 begin` / `// YP34 end`, edits marked `// YP34`):
1678
+
1679
+ - **`clipUpdate(update, sv, ds)`**: the structs of `update` under the state
1680
+ vector `sv` (a struct that crosses it is cut there, through the content's
1681
+ `offsetEnd`; a skip is written only where a later struct of its client
1682
+ follows it), and the deletes of `update` that the id set `ds` also holds
1683
+ (`intersectSets`). Exported from `src/index.js`, not from edytor's engine
1684
+ object (`crdt/engine.js`): only the room calls it, so apps keep none of it.
1685
+
1686
+ Deleted content a document collected cannot come back (a collecting
1687
+ document writes it as deleted); edytor's text deletes are hold records
1688
+ (YP20), which keep it.
1689
+
1690
+ Measured: vendored execution lines +30; the app bundle unchanged (the
1691
+ engine object does not hold it).
1692
+
1693
+ Oracle: `src/tests/crdt/yp34-clip.test.ts` (an update read at an earlier
1694
+ state, its later deletes left out; a run cut at the state vector), the
1695
+ re-seed rows (`src/tests/crdt/reseed/`), the upstream suite unchanged.
1696
+
1601
1697
  ## Generated declarations (`dts/`)
1602
1698
 
1603
1699
  `svelte-package` copies JS verbatim but emits no `.d.ts` for JS inputs, so
@@ -11,7 +11,7 @@ export { GC } from "./structs/GC.js";
11
11
  export { Skip } from "./structs/Skip.js";
12
12
  export type ContentIds = import("./utils/ids.js").ContentIds;
13
13
  export { IdSet, createDeleteSetFromStructStore, diffIdSet, createIdSet, mergeIdSets, insertIntoIdSet, iterateStructsByIdSet, readIdSet, writeIdSet, intersectSets } from "./utils/ids.js";
14
- export { applyUpdate, encodeStateAsUpdate, encodeStateVector, decodeStateVector, diffUpdate, mergeUpdates } from "./utils/encoding.js";
14
+ export { applyUpdate, encodeStateAsUpdate, encodeStateVector, decodeStateVector, diffUpdate, clipUpdate, mergeUpdates } from "./utils/encoding.js";
15
15
  export { ID, createID, compareIDs, findRootTypeKey } from "./utils/ID.js";
16
16
  export { idAtIndex, indexOfId } from "./utils/positions.js";
17
17
  export { findIndexSS, getItemCleanStart, getItemCleanEnd, isKeptReplaced } from "./utils/transaction-helpers.js";