Skip to content

Bug: [lexical-y] Undoing a format leaves it on the text after a collaborator's plain text inside the run, with no concurrent edits #9682

Open

Description

@zurfyx

Lexical version: main at 4a13388 (@lexical/y).

Steps To Reproduce

  1. Three clients bind one document holding a paragraph with "one two".
  2. Client A bolds "one two". Every client syncs.
  3. Client F types plain " mid" after "one". Every client syncs, so A has seen it.
  4. Client A undoes its bold. The clients sync.

Link to code example: the test below. Save it as packages/lexical-y/src/__tests__/unit/Repro.test.ts on main and run pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/Repro.test.ts. It fails on main with gc off and with LEXICAL_Y_GC=1.

Repro test (fails on main)
/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

// @vitest-environment node

import {
  $createParagraphNode,
  $createTextNode,
  $getRoot,
  type TextNode,
} from 'lexical';
import {expect, test} from 'vitest';

import {expectConverged, setup} from '../utils/harness';

// Each text node: its text, then `*` when bold.
const $show = () =>
  $getRoot()
    .getAllTextNodes()
    .map(n => n.getTextContent() + (n.hasFormat('bold') ? '*' : ''))
    .join('|');

for (const ids of [
  [1, 2, 3],
  [3, 2, 1],
]) {
  test(`A's undo of its bold takes it off all of "one two" after F typed plain text inside, which A has seen (ids ${ids})`, async () => {
    const t = await setup(
      () =>
        $getRoot().append(
          $createParagraphNode().append($createTextNode('one two')),
        ),
      {ids},
    );
    const [a, , f] = t.peers;
    a.update(() => {
      const selection = ($getRoot().getAllTextNodes()[0] as TextNode).select(
        0,
        7,
      );
      selection.format = 0;
      selection.formatText('bold');
    });
    await t.sync();
    // F types plain " mid" after "one"; A receives it before undoing.
    f.update(() => {
      const selection = ($getRoot().getAllTextNodes()[0] as TextNode).select(
        3,
        3,
      );
      selection.format = 0;
      selection.insertText(' mid');
    });
    await t.sync();
    a.undo();
    await t.sync();
    await expectConverged(t.peers);
    // A's undo removes the bold it added, from " two" too.
    expect(t.a.read($show)).toBe('one mid two');
  });
}

The current behavior

Every client shows one mid plain and two still bold. There are no concurrent edits: A's undo has seen F's text.

F's write of plain " mid" inside A's bold run ended the bold before " mid" and wrote a restore after it that gives the bold back to " two". A's undo deletes A's own format items but not F's restore, so " two" keeps the bold A is undoing.

The same mechanism gives these shapes (in packages/lexical-y/src/__tests__/unit/UndoRunEnds.test.ts, pinned as test.fails):

  • F deletes " mid" again before A undoes (the delete keeps F's run ends, D86): A's undo, redo and undo again leave " two" bold;
  • E types italic " PEER" right after F's " mid" or right before " two", F deletes " mid", and A undoes its italic: " two" keeps the italic.

The expected behavior

A's undo takes its bold off all of "one two" (one mid two, all plain), and a redo gives it back (one*| mid| two*).

Relation to the documented limits. y.md's Limits (Undo) documents this behavior today, for both orders: "The same holds for text a collaborator types or pastes inside the range without that format, whether you saw it before you undo or it arrives as you undo: bold one two, let a collaborator type plain mid after one, then undo the bold, and two stays bold." The decision register tracks it as D64 (OPEN). This issue tracks the case where the undo has seen the restore, which #9561 (parked) fixes; the concurrent case, where it has not, has no fix in sight and would stay a documented limit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions