Skip to content

[lexical-playground][lexical-react] Bug Fix: only one collab client bootstraps a nested editor's document - #9107

Merged
etrepum merged 6 commits into
meta:mainfrom
etrepum:claude/nested-editor-selection-test-g0qr23
Aug 31, 2026
Merged

etrepum merged 6 commits into
meta:mainfrom
etrepum:claude/nested-editor-selection-test-g0qr23

Conversation

@etrepum

@etrepum etrepum commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

Description

ImageComponent (image caption) and StickyComponent (sticky note) mount CollaborationPlugin with shouldBootstrap={true} unconditionally, while the main document uses shouldBootstrap={!skipCollaborationInit} so that only the left frame of the /split/ two-client view seeds it.

That is unsafe for a nested editor. syncPropertiesToYjs gives the nested editor a sub-Doc and uses that doc's guid as the editor key, so every client resolves the same collab room -- and unlike the main document, every client mounts the nested editor at the same moment the node itself syncs. Bootstrap is an ordinary Yjs insert rather than a compare-and-set, so when both clients' sync arrives before either's paragraph has propagated, each inserts an empty paragraph and Yjs keeps both. The caption ends up with a leading empty paragraph and the user's text in the second one, which is what made SelectBlock › Select within nested editor fail intermittently in the collab e2e job:

Array [
  3, 1, 1, 0,
-   0,
+   1,
  0, 0,
]

skipCollaborationInit moves from App.tsx to collaboration.ts so both nested editors can share it, and CollaborationPlugin's shouldBootstrap prop gains a doc comment recording that at most one client may set it.

Test plan

Before

SelectBlock › Select within nested editor, collab mode, 40 repeats over 8 workers on a 4-core box (oversubscribed to widen the race):

Error: expect(received).toEqual(expected) // deep equality

- Expected  - 1
+ Received  + 1

  Array [ 3, 1, 1, 0,
-   0,
+   1,
    0,
    0,
  ]
   at assertSelectionOnPageOrFrame (packages/lexical-playground/__tests__/utils/index.mjs:660:32)

1 failed
39 passed (3.3m)

A reduced spec asserting the caption editor's HTML directly, 24 repeats over 8 workers, caught the extra paragraph itself:

Expected: "<p class=\"PlaygroundEditorTheme__paragraph\" dir=\"auto\"><span data-lexical-text=\"true\">caption text</span></p>"
Received: "<p class=\"PlaygroundEditorTheme__paragraph\" dir=\"auto\"><br data-lexical-managed-linebreak=\"true\"></p><p class=\"PlaygroundEditorTheme__paragraph\" dir=\"auto\"><span data-lexical-text=\"true\">caption text</span></p>"

Instrumenting onBootstrap showed both frames on the same caption document, with the second frame only just losing the race:

DBG onBootstrap {"_f":"left","id":"a421fdae-b83b-4706-8283-b7761fdff588","shouldBootstrap":true,"yEmpty":true,"len":0}
DBG bootstrap:initializeEditor {"_f":"left","empty":true,"hasSel":true,"active":true,"size":0}
DBG onBootstrap {"_f":"right","id":"a421fdae-b83b-4706-8283-b7761fdff588","shouldBootstrap":true,"yEmpty":false,"len":1}

After

Same test, same oversubscribed configuration, 58 repeats completed with no failures before the run was stopped:

Running 100 tests using 8 workers
  ✓  ... SelectBlock › Select within nested editor
  (58 completed, 0 failed)

Full SelectBlock.spec.mjs and Images.spec.mjs in collab mode:

4 skipped
23 passed (1.5m)

pnpm run tsc, npx prettier --list-different and npx eslint are clean on the changed files. Only chromium was exercised; firefox and webkit were not run in this environment.

…ootstraps a nested editor's document

## Description

`ImageComponent` (image caption) and `StickyComponent` (sticky note) mount
`CollaborationPlugin` with `shouldBootstrap={true}` unconditionally, while the
main document uses `shouldBootstrap={!skipCollaborationInit}` so that only the
`left` frame of the `/split/` two-client view seeds it.

That is unsafe for a nested editor. `syncPropertiesToYjs` gives the nested
editor a sub-`Doc` and uses that doc's `guid` as the editor key, so every client
resolves the same collab room -- and unlike the main document, every client
mounts the nested editor at the same moment the node itself syncs. Bootstrap is
an ordinary Yjs insert rather than a compare-and-set, so when both clients'
`sync` arrives before either's paragraph has propagated, each inserts an empty
paragraph and Yjs keeps both. The caption ends up with a leading empty
paragraph and the user's text in the second one, which is what made
`SelectBlock › Select within nested editor` fail intermittently in the collab
e2e job:

    Array [
      3, 1, 1, 0,
    -   0,
    +   1,
      0, 0,
    ]

`skipCollaborationInit` moves from `App.tsx` to `collaboration.ts` so both
nested editors can share it, and `CollaborationPlugin`'s `shouldBootstrap` prop
gains a doc comment recording that at most one client may set it.

## Test plan

### Before

`SelectBlock › Select within nested editor`, collab mode, 40 repeats over 8
workers on a 4-core box (oversubscribed to widen the race):

    Error: expect(received).toEqual(expected) // deep equality

    - Expected  - 1
    + Received  + 1

      Array [
        3,
        1,
        1,
        0,
    -   0,
    +   1,
        0,
        0,
      ]
       at assertSelectionOnPageOrFrame (packages/lexical-playground/__tests__/utils/index.mjs:660:32)

    1 failed
    39 passed (3.3m)

A reduced spec asserting the caption editor's HTML directly, 24 repeats over 8
workers, caught the extra paragraph itself:

    Expected: "<p class=\"PlaygroundEditorTheme__paragraph\" dir=\"auto\"><span data-lexical-text=\"true\">caption text</span></p>"
    Received: "<p class=\"PlaygroundEditorTheme__paragraph\" dir=\"auto\"><br data-lexical-managed-linebreak=\"true\"></p><p class=\"PlaygroundEditorTheme__paragraph\" dir=\"auto\"><span data-lexical-text=\"true\">caption text</span></p>"

Instrumenting `onBootstrap` showed both frames on the same caption document,
with the second frame only just losing the race:

    DBG onBootstrap {"_f":"left","id":"a421fdae-b83b-4706-8283-b7761fdff588","shouldBootstrap":true,"yEmpty":true,"len":0}
    DBG bootstrap:initializeEditor {"_f":"left","empty":true,"hasSel":true,"active":true,"size":0}
    DBG onBootstrap {"_f":"right","id":"a421fdae-b83b-4706-8283-b7761fdff588","shouldBootstrap":true,"yEmpty":false,"len":1}

### After

Same test, same oversubscribed configuration, 58 repeats completed with no
failures before the run was stopped:

    Running 100 tests using 8 workers
      ✓  ... SelectBlock › Select within nested editor
      (58 completed, 0 failed)

Full `SelectBlock.spec.mjs` and `Images.spec.mjs` in collab mode:

    4 skipped
    23 passed (1.5m)

`pnpm run tsc`, `npx prettier --list-different` and `npx eslint` are clean on
the changed files. Only chromium was exercised; firefox and webkit were not run
in this environment.
@vercel

vercel Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 31, 2026 5:55pm
lexical-playground Ready Ready Preview Aug 31, 2026 5:55pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 31, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Aug 31, 2026
claude added 2 commits August 31, 2026 17:40
…sted editor selection test

## Description

`Selection › keeps single active selection for nested editors` reads both
editors' `_selection` once, immediately after `focusEditor()`. Focusing an
editor only moves the DOM selection; ownership moves when Lexical handles the
`selectionchange` the browser dispatches for it, and that dispatch is
asynchronous. The one-shot read races the handover, so the test can observe the
moment where the DOM selection already sits inside the newly focused editor
while both editors still hold their previous Lexical state -- the caption
editor's selection still `null` and the root editor's still set.

Instrumenting the failing step shows the DOM is already correct when the
assertion fails, and that the handover lands on the very next round trip:

    P5 after focus caption {"activeEl":"<div.ImageNode__contentEditable>",
      "activeIsCaption":true,"caption":{"hasSel":false},
      "domAnchor":"#text(\"Hello world\")","domAnchorInCaption":true,
      "root":{"hasSel":true,"key":"49","offset":0}}
    P6 ms until caption selection 0 immediate false

The race is only wide enough to lose in collab mode, where the split view runs
two clients and the extra work delays the event past the read; the same test on
the same browser never failed in non-collab mode.

Both assertions in each pair now go through one `expect.poll` over the pair, so
the editor losing the selection is checked against the same handover the
winning editor waits for. No source change -- this is a test synchronization
fix.

## Test plan

### Before

`Selection › keeps single active selection for nested editors`, webkit, 32
repeats over 8 workers on a 4-core box (oversubscribed to widen the race):

    Error: expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: false

      136 |     expect(await hasSelection('.image-caption-container')).toBe(true);
          |                                                            ^

    14 failed
    18 passed (3.4m)

Failures split 13 at line 136 (caption never takes the selection) and 1 at line
126 (root editor has not taken it back yet) -- the same race in both
directions. The identical run in non-collab mode passed 32/32, which is what
pinned the cause to timing rather than to WebKit focus handling.

### After

Same test, same oversubscribed configuration:

    32 passed (3.1m)

Across every browser and mode that runs this test, 8 repeats each:

    === webkit rich-text              8 passed (20.9s)
    === webkit rich-text-with-collab  8 passed (41.7s)
    === chromium rich-text            8 passed (18.2s)
    === chromium rich-text-with-collab 8 passed (25.8s)
    === firefox rich-text             8 passed (27.8s)
    === firefox rich-text-with-collab 8 passed (38.2s)

Full `Selection.spec.mjs` in webkit collab:

    31 skipped
    27 passed (2.2m)

`npx prettier --list-different` and `npx eslint` are clean on the changed file.
Linux WebKit was used here rather than the macOS WebKit the CI job runs.
…est-g0qr23' into claude/nested-editor-selection-test-g0qr23
@etrepum
etrepum added this pull request to the merge queue Aug 31, 2026
Merged via the queue into meta:main with commit 2907387 Aug 31, 2026
46 checks passed

This branch was successfully deployed

2 active deployments
Preview – lexical — ef11a641 Deployed Aug 31, 2026 by vercel[bot]
Preview – lexical-playground — ef11a641 Deployed Aug 31, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants