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
@@ -61,6 +61,12 @@ export declare const messageChunk = 5;
61
61
  * room sends it only to a socket that subscribed.
62
62
  */
63
63
  export declare const messageComments = 6;
64
+ /**
65
+ * Client → server, after a re-seed (`room.reseed.protocol`): the client's
66
+ * whole old document, its unsaved edits in it, for the room to rebase onto
67
+ * the current epoch. Sent only when the room asks (its epoch notice).
68
+ */
69
+ export declare const messageRebase = 7;
64
70
  /** A WebSocket message limit on Cloudflare (32 MiB): a larger frame is sent as chunks. */
65
71
  export declare const MAX_FRAME_BYTES: number;
66
72
  /**
@@ -203,6 +209,15 @@ export declare const CLOSE: {
203
209
  readonly denied: 4403;
204
210
  /** The dialed replica is bound to another user. */
205
211
  readonly replicaTaken: 4409;
212
+ /**
213
+ * The room re-seeded the document (`room.reseed.protocol`; the reason is
214
+ * the room's epoch): a refusal of this document, once a client with
215
+ * unsaved edits redialed and handed them over. The new epoch is a new
216
+ * document's. Also the answer to a copy the room cannot rebase (an
217
+ * epoch past the cuts it keeps, or a later one) and to an older epoch's
218
+ * content on a socket of the current one.
219
+ */
220
+ readonly reseeded: 4410;
206
221
  /**
207
222
  * A room quota refused the socket's write (`quota: document`,
208
223
  * `quota: rate`, `quota: frame`): the frame was not applied, and a
@@ -63,6 +63,12 @@ export const messageChunk = 5;
63
63
  * room sends it only to a socket that subscribed.
64
64
  */
65
65
  export const messageComments = 6;
66
+ /**
67
+ * Client → server, after a re-seed (`room.reseed.protocol`): the client's
68
+ * whole old document, its unsaved edits in it, for the room to rebase onto
69
+ * the current epoch. Sent only when the room asks (its epoch notice).
70
+ */
71
+ export const messageRebase = 7;
66
72
  /** A WebSocket message limit on Cloudflare (32 MiB): a larger frame is sent as chunks. */
67
73
  export const MAX_FRAME_BYTES = 32 * 1024 * 1024;
68
74
  const chunkStart = 0;
@@ -334,6 +340,15 @@ export const CLOSE = {
334
340
  denied: 4403,
335
341
  /** The dialed replica is bound to another user. */
336
342
  replicaTaken: 4409,
343
+ /**
344
+ * The room re-seeded the document (`room.reseed.protocol`; the reason is
345
+ * the room's epoch): a refusal of this document, once a client with
346
+ * unsaved edits redialed and handed them over. The new epoch is a new
347
+ * document's. Also the answer to a copy the room cannot rebase (an
348
+ * epoch past the cuts it keeps, or a later one) and to an older epoch's
349
+ * content on a socket of the current one.
350
+ */
351
+ reseeded: 4410,
337
352
  /**
338
353
  * A room quota refused the socket's write (`quota: document`,
339
354
  * `quota: rate`, `quota: frame`): the frame was not applied, and a
@@ -353,7 +368,7 @@ export const validRoomId = (id) => id.length > 0 && id.length <= 256 && id !== '
353
368
  export const assertRoomId = (id) => {
354
369
  if (validRoomId(id))
355
370
  return;
356
- throw new TypeError(`room id ${JSON.stringify(id)} cannot be dialed: it must be 1-256 characters, not "." or ".." (a URL collapses those path segments), with no lone surrogate`);
371
+ throw new TypeError(`room id ${JSON.stringify(id)} cannot be dialed: 1-256 characters, not "." or "..", no lone surrogate`);
357
372
  };
358
373
  /** A close code that refuses this client for good: policy (`1008`) or an application code (`4xxx`) but `expired`. */
359
374
  export const isRefusal = (code) => code === CLOSE.refused || (code >= 4000 && code < 5000 && code !== CLOSE.expired);
@@ -174,6 +174,8 @@ export declare const bindWebsocketProvider: (Y: EngineApi) => {
174
174
  /** The room asks a read-only socket for its state again: what this actor wrote is unsaved until stored. */
175
175
  allow(): void;
176
176
  };
177
+ /** @internal The room re-seeded: this document's unsaved edits are being handed over (`room.reseed.protocol`). */
178
+ _rebasing: boolean;
177
179
  messageHandlers: Record<number, RoomMessageHandler</*elided*/ any>>;
178
180
  hasSynced: boolean;
179
181
  whenSynced: Promise<unknown>;
@@ -193,8 +195,8 @@ export declare const bindWebsocketProvider: (Y: EngineApi) => {
193
195
  * decodes it). `''`, `.`, `..`, an id over 256 characters and one with a
194
196
  * lone surrogate are refused at construction ({@link assertRoomId}). `params` are read at every dial (a refreshed token
195
197
  * reaches the next connection), with `replica` = this document's
196
- * client id unless `params` names one: the room binds the socket's
197
- * writes to it.
198
+ * client id unless `params` names one (the room binds the socket's
199
+ * writes to it), and `epoch`, the document's, which no param replaces.
198
200
  */
199
201
  get url(): string;
200
202
  /** Updates of this document's actor the room has not acknowledged as persisted yet. */
@@ -47,18 +47,19 @@
47
47
  * there (TCP never does).
48
48
  */
49
49
  import * as decoding from 'lib0-v14/decoding';
50
+ import * as encoding from 'lib0-v14/encoding';
50
51
  import * as time from 'lib0-v14/time';
51
52
  import * as math from 'lib0-v14/math';
52
53
  import * as url from 'lib0-v14/url';
53
54
  import { Awareness, removeAwarenessStates } from '../protocols/awareness.js';
54
55
  import { IsolatedObservable } from '../protocols/observable.js';
55
- import { messagePermissionDenied, messageAccess, messageReadOnly, READ_ONLY_DENIAL, SUGGEST_ONLY_DENIAL, readAuthMessage } from '../protocols/auth.js';
56
+ import { messagePermissionDenied, messageAccess, messageEpoch, messageReadOnly, READ_ONLY_DENIAL, SUGGEST_ONLY_DENIAL, readAuthMessage } from '../protocols/auth.js';
56
57
  import { bindSync } from '../protocols/sync.js';
57
58
  import { readCommentsMessage, writeCommentsSubscribe } from '../protocols/comments.js';
58
59
  import { frame } from '../protocols/envelope.js';
59
- import { assertRoomId, beginDestroy, bindRoomProtocol, tabs, chunkFrame, CLOSE, createChunkReader, emitFailed, fault, initLifecycle, isRefusal, unknownType, LOCAL_PRESENCE_LOSS, markSynced, messageAuth, messageChunk, messageComments, messageSaved, messageSync, MAX_FRAME_BYTES, SyncRefusedError } from './room.js';
60
+ import { assertRoomId, beginDestroy, bindRoomProtocol, tabs, chunkFrame, CLOSE, createChunkReader, emitFailed, fault, initLifecycle, isRefusal, unknownType, LOCAL_PRESENCE_LOSS, markSynced, messageAuth, messageChunk, messageComments, messageRebase, messageSaved, messageSync, MAX_FRAME_BYTES, SyncRefusedError } from './room.js';
60
61
  import { asEngineDoc } from '../structs.js';
61
- import { ATTRIBUTION_ROOT } from '../schema.js';
62
+ import { ATTRIBUTION_ROOT, EPOCH_ROOT } from '../schema.js';
62
63
  import { ATTRIBUTION_ORIGIN, writerOf } from '../attribution/attribution.js';
63
64
  // @todo - this should depend on awareness.outdatedTime
64
65
  const messageReconnectTimeout = 30000;
@@ -129,10 +130,17 @@ export const bindWebsocketProvider = (Y) => {
129
130
  // The room's notice at the join: a state, not a refusal (no event).
130
131
  () => provider._writes.deny(),
131
132
  // The room's word at the join, and at a change in place (`read`).
132
- (access) => provider.emit('access', [access, provider]));
133
+ (access) => provider.emit('access', [access, provider]),
134
+ // The room re-seeded since this document's epoch: it asks for the
135
+ // document, its unsaved edits in it (`room.reseed.protocol`).
136
+ () => {
137
+ provider._rebasing = true;
138
+ send(provider, frame(messageRebase, (e) => encoding.writeVarUint8Array(e, Y.encodeStateAsUpdate(provider.doc))));
139
+ });
133
140
  if (authType !== messagePermissionDenied &&
134
141
  authType !== messageReadOnly &&
135
- authType !== messageAccess) {
142
+ authType !== messageAccess &&
143
+ authType !== messageEpoch) {
136
144
  // An auth type we do not speak inside a valid v14 envelope is
137
145
  // protocol skew — reported, never dropped silently.
138
146
  unknownType(provider, 'auth', authType);
@@ -400,6 +408,13 @@ export const bindWebsocketProvider = (Y) => {
400
408
  provider.awareness.meta.delete(client);
401
409
  provider.emit('status', [{ status: 'disconnected' }]);
402
410
  }
411
+ // A re-seed (`room.reseed.protocol`): unsaved edits are handed over first,
412
+ // by a dial of this document's epoch, older now, which the room asks for
413
+ // them. Then, or with none, it is a refusal: the new epoch is a new document's.
414
+ if (event?.code === CLOSE.reseeded && provider.unsaved > 0 && !provider._rebasing) {
415
+ provider._rebasing = true;
416
+ return setupWS(provider);
417
+ }
403
418
  if (event && isRefusal(event.code)) {
404
419
  // Terminal: the next dial would be refused the same way.
405
420
  provider.shouldConnect = false;
@@ -511,6 +526,8 @@ export const bindWebsocketProvider = (Y) => {
511
526
  _chunks = createChunkReader();
512
527
  /** This actor's writes pending the room's acknowledgement. */
513
528
  _writes;
529
+ /** @internal The room re-seeded: this document's unsaved edits are being handed over (`room.reseed.protocol`). */
530
+ _rebasing = false;
514
531
  messageHandlers = room.messageHandlers;
515
532
  // The room lifecycle — installed by `initLifecycle`.
516
533
  hasSynced;
@@ -596,12 +613,22 @@ export const bindWebsocketProvider = (Y) => {
596
613
  * decodes it). `''`, `.`, `..`, an id over 256 characters and one with a
597
614
  * lone surrogate are refused at construction ({@link assertRoomId}). `params` are read at every dial (a refreshed token
598
615
  * reaches the next connection), with `replica` = this document's
599
- * client id unless `params` names one: the room binds the socket's
600
- * writes to it.
616
+ * client id unless `params` names one (the room binds the socket's
617
+ * writes to it), and `epoch`, the document's, which no param replaces.
601
618
  */
602
619
  get url() {
603
- const query = url.encodeQueryParams({ replica: String(this.doc.clientID), ...this.params });
604
- return `${this.serverUrl}/${encodeURIComponent(this.roomname)}?${query}`;
620
+ // The document's epoch (`new`: it holds nothing yet), which the room checks (`room.reseed.protocol`).
621
+ const epoch = this.doc.store.clients.size
622
+ ? String(asEngineDoc(this.doc).get(EPOCH_ROOT).getAttr('n') ?? 0)
623
+ : 'new';
624
+ const query = {
625
+ replica: String(this.doc.clientID),
626
+ epoch,
627
+ ...this.params
628
+ };
629
+ // In its place, but no param of the app's names it instead.
630
+ query.epoch = epoch;
631
+ return `${this.serverUrl}/${encodeURIComponent(this.roomname)}?${url.encodeQueryParams(query)}`;
605
632
  }
606
633
  /** Updates of this document's actor the room has not acknowledged as persisted yet. */
607
634
  get unsaved() {
@@ -0,0 +1,127 @@
1
+ import type { EngineDoc } from './engine-api.js';
2
+ import { type BlockId } from './placement/model.js';
3
+ import { type DataPatch } from './data.js';
4
+ import { restoreDocument, type RestoreFacade } from './restore.js';
5
+ import type { ModelView } from './placement/model.js';
6
+ import { type SuggestContext } from './doc/suggest.js';
7
+ import type { JSONDoc } from '../utils/json.js';
8
+ /** What the rebase reads and writes through: a facade over a document (structurally). */
9
+ export type ReseedFacade<Plan = unknown> = RestoreFacade<Plan> & {
10
+ order(): readonly BlockId[];
11
+ displayLength(id: BlockId): number;
12
+ toJSON(options?: {
13
+ suggestions?: boolean;
14
+ }): JSONDoc;
15
+ /** The facade's reads beyond its operations: the index's view, the base view (`doc/suggest.ts` builds over it too). */
16
+ part: {
17
+ view(): ModelView;
18
+ base?: SuggestContext['base'];
19
+ };
20
+ prepare: RestoreFacade<Plan>['prepare'] & {
21
+ patchData(target: {
22
+ block: BlockId;
23
+ atom: string;
24
+ }, ops: DataPatch[]): Plan;
25
+ formatRange(id: BlockId, offset: number, length: number, marks: Record<string, unknown>): Plan;
26
+ splitBlock(id: BlockId, offset: number, newId: BlockId): Plan;
27
+ mergeBlocks(from: BlockId, into: BlockId): Plan;
28
+ };
29
+ };
30
+ /** A document and the facade over it. */
31
+ export type Replica = {
32
+ doc: EngineDoc;
33
+ facade: ReseedFacade;
34
+ };
35
+ /**
36
+ * A document without its pending suggestions (`sugg.json.base`), its JSON
37
+ * and each unit's identity read from one view: what a cut is made from.
38
+ */
39
+ export type Base = {
40
+ json: JSONDoc;
41
+ ids: Map<BlockId, string[]>;
42
+ };
43
+ export declare const baseOf: (facade: ReseedFacade) => Base;
44
+ /** What the room keeps of a re-seed (`room.reseed.cut`). */
45
+ export type Cut = {
46
+ /** The old document's state vector at the cut. */
47
+ sv: Uint8Array;
48
+ /** The old document's deletes at the cut (an update without structs). */
49
+ ds: Uint8Array;
50
+ /** The cut map (`encodeRuns`). */
51
+ map: Uint8Array;
52
+ };
53
+ /** A cut as one value (`Cut` → bytes and back), for the room's storage. */
54
+ export declare const encodeCut: (cut: Cut) => Uint8Array<ArrayBuffer>;
55
+ export declare const decodeCut: (bytes: Uint8Array) => Cut;
56
+ /**
57
+ * The new epoch's document (`room.reseed.cut`): `seed` builds it from the
58
+ * old one's base (`baseOf`: the caller's semantics and actor), then its
59
+ * blocks take the old ones' attribution, and it takes the old one's actor
60
+ * profiles (`u/<actorId>`: names and colors). Reads `old`, writes only the
61
+ * new document.
62
+ */
63
+ export declare const seedFrom: (old: Replica, base: Base, seed: (json: JSONDoc) => Replica) => Replica;
64
+ /**
65
+ * The cut between `old` and `fresh`, the new epoch seeded from its base
66
+ * ({@link seedFrom}): each seed unit stands for the base's unit at its
67
+ * place.
68
+ */
69
+ export declare const cutBetween: (old: Replica, base: Base, fresh: Replica) => Cut;
70
+ /** Cut `old` (`room.reseed.cut`): the new epoch's document and the cut ({@link seedFrom}, {@link cutBetween}). */
71
+ export declare const cutDocument: (old: Replica, seed: (json: JSONDoc) => Replica) => {
72
+ document: Replica;
73
+ cut: Cut;
74
+ };
75
+ /**
76
+ * Make `doc` show `json`, where `ids` names each unit of each of its blocks
77
+ * (an identity `doc` holds is that unit, kept where it can be; any other
78
+ * is new text). Splits and merges first: each block of `doc` is cut where
79
+ * its units go to another block of `json` (or where another block's text
80
+ * comes between them), then each block of `json` is assembled from its
81
+ * pieces in order (its first piece takes its id, the others are merged
82
+ * into it; a unit `json` no longer shows stays in the piece before it).
83
+ * Then `restoreDocument` writes the rest, its content by identity, and
84
+ * keeps the pending suggestions. An identity `doc` does not hold (`new:`,
85
+ * `~`, an old unit it never had) names no unit of it: a unit this restore
86
+ * wrote and `ids` names nowhere else stands for it, so it is written once
87
+ * (a `new:` one, the caller's own new unit, any such unit). Runs inside the
88
+ * caller's transaction.
89
+ */
90
+ export declare const restoreByIdentity: (doc: EngineDoc, facade: ReseedFacade, json: JSONDoc, ids: ReadonlyMap<BlockId, readonly string[]>) => import("./restore.js").RestoreReport;
91
+ /** What a rebase did: nothing to merge, or the restore's report of its write. */
92
+ export type RebaseReport = {
93
+ status: 'noop';
94
+ } | ({
95
+ status: 'applied';
96
+ } & ReturnType<typeof restoreDocument>);
97
+ /**
98
+ * What one user's rebases from one cut merged so far (`room.reseed.rebase`):
99
+ * the old document's state they covered (a state vector, and the deletes
100
+ * as an update without structs), so a second upload of the same edits
101
+ * (another tab's) merges nothing twice, and the merged maps: `maps[i]`,
102
+ * written while the `i`th cut's epoch was current, reads each unit those
103
+ * rebases wrote as the old unit it stands for, so an edit that builds on
104
+ * them still finds it. Starts as the cut's own state, with no map.
105
+ */
106
+ export type Merged = {
107
+ sv: Uint8Array;
108
+ ds: Uint8Array;
109
+ maps: Uint8Array[];
110
+ };
111
+ export declare const encodeMerged: (merged: Merged) => Uint8Array<ArrayBuffer>;
112
+ export declare const decodeMerged: (bytes: Uint8Array) => Merged;
113
+ /** Whether `upload` holds anything `merged` does not cover: what a rebase merges, if anything. */
114
+ export declare const hasEdits: (upload: Uint8Array, merged: Merged) => boolean;
115
+ /**
116
+ * Merge a client's unsaved edits, `upload` (its old document, whole),
117
+ * into `target`, the current document: `cuts` are the re-seeds since the
118
+ * client's epoch, oldest first (each one's map reads the next epoch's seed
119
+ * units as the previous epoch's), `merged` what its user's earlier rebases
120
+ * from the oldest cut merged (default: the cut's own state), `scratch`
121
+ * builds a document from an update. Writes `target` inside the caller's
122
+ * transaction; answers the report and `merged` grown by this rebase.
123
+ */
124
+ export declare const rebaseOnto: (target: Replica, cuts: readonly Cut[], upload: Uint8Array, scratch: (update: Uint8Array) => Replica, merged?: Merged) => {
125
+ report: RebaseReport;
126
+ merged: Merged;
127
+ };