@peerkit-dev/doc-lexical 0.1.0 → 0.1.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.
package/README.md CHANGED
@@ -10,7 +10,7 @@ Uses the official **`@lexical/yjs`** bindings over an existing `@peerkit-dev/doc
10
10
 
11
11
  | Package | Version |
12
12
  | --- | --- |
13
- | `@peerkit-dev/doc-lexical` | **0.1.0** |
13
+ | `@peerkit-dev/doc-lexical` | **0.1.1** |
14
14
  | `@peerkit-dev/doc-core` | `^0.1.1` (minimum for `waitForInitialSync`) |
15
15
  | `lexical` / `@lexical/yjs` | `^0.50.0` (Tirage Editorial uses `^0.50.0`) |
16
16
  | `yjs` | `^13.6.33` |
@@ -88,7 +88,7 @@ handle.setReadOnly(true);
88
88
  // After transport restore — DocSession starts a new sync epoch
89
89
  handle.recover(); // handleReconnect + provider reconnect waits for waitForInitialSync again
90
90
 
91
- // Optional: pull Yjs → Lexical when the editor is still empty after catch-up
91
+ // Optional: pull Yjs → Lexical when the editor is empty or shell-only after catch-up
92
92
  handle.resyncFromYjs();
93
93
 
94
94
  // Unmount binding only — host still owns DocSession leave / persistence
@@ -113,10 +113,11 @@ await docSession.leave();
113
113
  2. Custom nodes registered on the host editor participate in sync.
114
114
  3. Cursors/selections use DocSession awareness (bridged `name` ↔ `userName`, `peerId`).
115
115
  4. `shouldBootstrap` writes **only** when the shared root is empty — joiners / reconnects do not duplicate hydration.
116
- 5. Hosts must call `waitForInitialSync()` (or rely on the provider’s sync event) **before** setting `shouldBootstrap` from canonical state, so concurrent participants cannot independently bootstrap conflicting seeds.
117
- 6. Collaboration tags / binding origin prevent sync feedback loops.
118
- 7. `setReadOnly` / `dispose` are safe; `dispose` does **not** call `session.leave()`.
119
- 8. Host persistence of canonical manuscripts stays outside this package.
116
+ 5. Catch-up hydration runs for empty *and* shell-only Lexical roots (composer empty paragraphs). Non-empty local content is never overwritten by `resyncFromYjs` / mount hydrate.
117
+ 6. Hosts must call `waitForInitialSync()` (or rely on the provider’s sync event) **before** setting `shouldBootstrap` from canonical state, so concurrent participants cannot independently bootstrap conflicting seeds.
118
+ 7. Collaboration tags / binding origin prevent sync feedback loops.
119
+ 8. `setReadOnly` / `dispose` are safe; `dispose` does **not** call `session.leave()`.
120
+ 9. Host persistence of canonical manuscripts stays outside this package.
120
121
 
121
122
  ### Non-goals
122
123
 
package/dist/index.d.ts CHANGED
@@ -61,7 +61,8 @@ type DocLexicalHandle = {
61
61
  /**
62
62
  * Pull current shared Yjs Lexical root into the editor (idempotent).
63
63
  * Used after join catch-up, remount, or recover when observeDeep did not
64
- * replay already-applied updates.
64
+ * replay already-applied updates. Shell-only empty paragraphs do not block
65
+ * hydration; non-empty local content is left untouched.
65
66
  */
66
67
  resyncFromYjs(): void;
67
68
  /**
package/dist/index.js CHANGED
@@ -1,8 +1,37 @@
1
1
  import { createBinding, syncLexicalUpdateToYjs, initLocalState, syncCursorPositions, syncYjsChangesToLexical } from '@lexical/yjs';
2
- import { SKIP_COLLAB_TAG, $getRoot, $addUpdateTag, SKIP_SCROLL_INTO_VIEW_TAG, COLLABORATION_TAG, HISTORY_MERGE_TAG, $createParagraphNode, $getSelection } from 'lexical';
3
- import { XmlText } from 'yjs';
2
+ import { SKIP_COLLAB_TAG, $getRoot, $addUpdateTag, SKIP_SCROLL_INTO_VIEW_TAG, COLLABORATION_TAG, $isParagraphNode, $isTextNode, HISTORY_MERGE_TAG, $createParagraphNode, $getSelection } from 'lexical';
3
+ import { XmlText, XmlElement } from 'yjs';
4
4
 
5
5
  // src/mount.ts
6
+ function clearCollabNodeTree(sharedType) {
7
+ if (Object.prototype.hasOwnProperty.call(sharedType, "_collabNode")) {
8
+ Reflect.deleteProperty(sharedType, "_collabNode");
9
+ }
10
+ if (sharedType instanceof XmlText) {
11
+ for (const delta of sharedType.toDelta()) {
12
+ const insert = delta.insert;
13
+ if (insert != null && typeof insert === "object") {
14
+ clearCollabNodeTree(insert);
15
+ }
16
+ }
17
+ return;
18
+ }
19
+ if (sharedType instanceof XmlElement) {
20
+ for (const child of sharedType.toArray()) {
21
+ if (typeof child !== "string") {
22
+ clearCollabNodeTree(child);
23
+ }
24
+ }
25
+ }
26
+ }
27
+ function clearDescendantCollabCaches(sharedRoot) {
28
+ for (const delta of sharedRoot.toDelta()) {
29
+ const insert = delta.insert;
30
+ if (insert != null && typeof insert === "object") {
31
+ clearCollabNodeTree(insert);
32
+ }
33
+ }
34
+ }
6
35
 
7
36
  // src/errors.ts
8
37
  var DocLexicalError = class extends Error {
@@ -263,6 +292,7 @@ function mountDocLexical(options) {
263
292
  provider.disconnect();
264
293
  } catch {
265
294
  }
295
+ clearCollabNodeTree(binding.root.getSharedType());
266
296
  binding.root.destroy(binding);
267
297
  docMap.delete(id);
268
298
  status = "disposed";
@@ -278,12 +308,19 @@ function maybeBootstrap(binding, editor, shouldBootstrap, initialEditorState) {
278
308
  function hydrateEditorFromExistingYjs(binding, provider) {
279
309
  const shared = binding.root.getSharedType();
280
310
  if (shared.length === 0) return;
281
- const lexicalEmpty = binding.editor.getEditorState().read(() => $getRoot().isEmpty());
282
- if (!lexicalEmpty) return;
311
+ const hydratable = binding.editor.getEditorState().read(() => $isHydratableEditorRoot());
312
+ if (!hydratable) return;
283
313
  binding.editor.update(
284
314
  () => {
285
- binding.root.syncPropertiesFromYjs(binding, null);
286
- binding.root.applyChildrenYjsDelta(binding, shared.toDelta());
315
+ if (!$isHydratableEditorRoot()) return;
316
+ $getRoot().clear();
317
+ if (binding.root.isEmpty()) {
318
+ clearDescendantCollabCaches(shared);
319
+ binding.root.syncPropertiesFromYjs(binding, null);
320
+ binding.root.applyChildrenYjsDelta(binding, shared.toDelta());
321
+ } else {
322
+ binding.root.syncPropertiesFromYjs(binding, null);
323
+ }
287
324
  binding.root.syncChildrenFromYjs(binding);
288
325
  $addUpdateTag(SKIP_SCROLL_INTO_VIEW_TAG);
289
326
  },
@@ -296,6 +333,21 @@ function hydrateEditorFromExistingYjs(binding, provider) {
296
333
  }
297
334
  );
298
335
  }
336
+ function $isHydratableEditorRoot() {
337
+ const root = $getRoot();
338
+ if (root.isEmpty()) return true;
339
+ const children = root.getChildren();
340
+ if (children.length === 0) return true;
341
+ return children.every($isShellParagraph);
342
+ }
343
+ function $isShellParagraph(node) {
344
+ if (!$isParagraphNode(node)) return false;
345
+ const kids = node.getChildren();
346
+ if (kids.length === 0) return true;
347
+ return kids.every(
348
+ (child) => $isTextNode(child) && child.getTextContentSize() === 0
349
+ );
350
+ }
299
351
  function initializeEditor(editor, initialEditorState) {
300
352
  editor.update(
301
353
  () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peerkit-dev/doc-lexical",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "MIT",
5
5
  "description": "Reusable Lexical ↔ PeerKit DocSession collaboration adapter. Preserves structured Lexical state via @lexical/yjs over DocSession's Yjs lifecycle. Not an authoritative content store.",
6
6
  "type": "module",