Skip to content

fix(core): reconcile nested history and mounted table cells - #1004

Merged
petyosi merged 4 commits into
mdx-editor:mainfrom
huyanxius:fix/nested-history-reconciliation-20261028
Nov 2, 2026
Merged

petyosi merged 4 commits into
mdx-editor:mainfrom
huyanxius:fix/nested-history-reconciliation-20261028

Conversation

@huyanxius

Copy link
Copy Markdown
Contributor

Scope

Separate core Undo/export/reconciliation repair requested in the review of #996. No table-region paste feature, spreadsheet UI, history-command routing, public API, or dependency changes are included here.

Changes

  • Treat nested exports between editors sharing history as derived updates, rather than additional user edits. Undo/Redo of the originating editor re-exports through directive/JSX ancestors.
  • Preserve committed nested EditorState snapshots by historical MDAST identity. Restoring an ancestor snapshot restores mounted descendants without recreating their keys or losing their history references.
  • Avoid redundant focus boundaries only when the ancestor snapshot contains the exact recorded nested edit; keep unrelated editor history boundaries intact.
  • Reconcile a changed persisted table-cell source, preserve unsaved sibling content/local history, and reject a stale blur before reconciliation. Only a cell whose authoritative content was replaced resets its own local history through Lexical's public command.
  • Preserve explicit property-edit boundaries and establish a first-edit baseline through public editor updates.

Regression evidence

On freshly fetched main e186ded343e87a790deeabfb253531099d82a9fb, ordinary edit → blur → Undo failed in root, real directive, real JSX, and multi-level nesting: root Markdown reverted while mounted cells stayed stale; nested exports also added history steps.

The new 28-case suite covers those paths, first-cell edits, root-origin restores, repeated edit/focus cycles, exact parent Undo counts, Redo after focus changes, stale blur, unsaved siblings, local history after authoritative replacement, and explicit JSX properties. Five new browser scenarios cover native typing, root keyboard Undo/Redo, repeated cycles, and unaffected-cell focus/caret/history across Chromium, Firefox, and WebKit.

Verification

Head: 9d56758d34382ea9f42cdbabad0a29eaa9139efd (published tree exactly matches the locally tested tree).

Passed locally:

  • Full Vitest: 268 passed; existing 1 skipped and 1 todo retained.
  • Typecheck, full lint, library/declaration build, git diff check.
  • Lexical graph: all 24 installed packages at 0.48.0.
  • Independent review and focused rerun: 64 passed, no remaining blocking finding within this scope.

Draft: exact-head CI browser/package/cross-version validation is pending. Local browser startup was blocked by the cloud sandbox before any page opened; this is not a browser pass. An initial resource-killed build was rerun successfully with a bounded Node heap. This PR is not marked ready for review pending the remaining CI gates.

Copy link
Copy Markdown
Contributor Author

The corrected head dd014f8a209e13d7c357224d2e29e318dfdfa871 now passes all four jobs in exact-head CI, including 156/156 browser tests across Chromium, Firefox, and WebKit, packed React 18/19 consumers, and cross-version Markdown replay.

The first browser run exposed two real gaps: a parent Redo's restored root selection could blur and commit an unrelated pending cell, and cleared cell-local history could bubble availability events into the root toolbar. The correction preserves surviving nested focus/caret during native history restoration, suppresses only the restoration-induced blur, and accepts toolbar availability only from the active editor or its actual shared history. It does not reroute Undo/Redo or change history stacks. Existing assertions were retained; the sibling test now also checks final Redo focus/caret.

Final local verification: 271 unit tests passed (existing 1 skip/1 todo retained), full lint, typecheck, library/declaration build, and the 24-package Lexical graph check. Independent corrective review reran 67 focused tests successfully. The published tree was fetched and matched the tested tree exactly. This remains a separate draft core repair; no merge or deployment was performed.

@petyosi petyosi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for splitting this out. The table-cell bug is real. On main, if you edit a cell, blur it and press Undo in the root, the markdown goes back but the cell still shows the edit. But this PR fixes three separate problems at once, and it changes history behaviour for every nested editor. I'd like it reduced to the table-cell fix.

What's in here:

  • (a) Stale cell after a parent Undo (TableEditor.tsx:475-519). An effect re-imports the cell when its stored contents change, and save() refuses stale writes. This is the part I asked for, and it is small and reasonable.
  • (b) Nested-editor saves no longer create root history steps (NestedLexicalEditor.tsx:122-150, 310-375, lexicalExtensions.ts:149-157, 278-325). This applies to every directive and JSX editor. It caused the browser regressions fixed in dd014f8. That fix adds high-priority UNDO/REDO handlers on every editor with history, which restore focus and the DOM range in a setTimeout, plus blur suppression during restores and per-editor filtering of CAN_UNDO/CAN_REDO in UndoRedo.tsx. That is a lot of global machinery, and every piece of it is a new way for focus and selection to go wrong.
  • (c) A history baseline for the first edit (ensureEditorHistoryBaseline). It forces an entry with a no-op markDirty(), can JSON.stringify the whole editor state, and runs on every cell save and property update.

Other problems:

  • useMdastNodeUpdater now calls getMdastNode() unconditionally (NestedLexicalEditor.tsx:133-142). The public NestedEditorsContextValue.lexicalNode contract only requires setMdastNode(), so a custom node that met the contract now throws getMdastNode is not a function.
  • undo-redo.test.tsx:254 changes the 1d7fa34 assertion from editedDepth + 1 to editedDepth. That is a deliberate change to how deep the history is, and it belongs with (b), not with the table fix.

A minimal version of (a) is about 40 lines in TableEditor.tsx:

  • an effect keyed on a persisted-contents ref, so uncommitted typing in other cells is not overwritten;
  • a re-import tagged with HISTORIC_TAG;
  • clearing the cell's local history after the re-import;
  • the stale-content check in save().

With that, the existing suite and your root-table Undo/Redo, sibling-typing and stale-blur cases pass. If you think (b) or (c) are worth fixing, please open issues with repros first, so we can agree on an approach before more code.

Integrate current main while removing global nested history, focus restoration,
and first-edit baseline changes. Retain only table persisted-content import,
local history clearing and stale-save protection, with root-table regressions.
Restore original nested editor contract and existing Undo history assertions.

308 unit tests and complete typecheck pass; changed-file typed lint passes.
Three root-table regressions fail on the old baseline and pass with the fix.
Native browser and complete declaration build await exact-head CI; local
browser restrictions were not bypassed and declaration rollup was resource-killed.

Copy link
Copy Markdown
Contributor Author

Addressed the requested reduction in 6a4c8d5, with the browser-discovered table-local availability correction in b6377fb8339a109671d12fdb0b1fa72abeb44f2c.

  • The production diff is now TableEditor only: persisted-contents comparison, HISTORIC_TAG re-import, clearing the restored cell's local history, and stale-save protection. Unsaved sibling text is preserved.
  • Removed the global NestedLexicalEditor/history/focus/baseline changes and the UndoRedo filtering. The public nested-node contract no longer gains an unconditional getMdastNode() call; NestedLexicalEditor.tsx, lexicalExtensions.ts, UndoRedo.tsx, and the existing undo-redo tests match current main, including the original history-depth assertion. I have not carried the broader (b)/(c) work into this fix.
  • Retained only the root-table Undo/Redo, sibling-typing, stale-blur coverage and its root-table browser example/spec. Integrated current main without dropping its table export/paragraph fixes.
  • The first narrowed CI run exposed two real WebKit availability regressions: CLEAR_HISTORY from the restored cell emitted false availability into the active parent toolbar. The correction scopes temporary notification boundaries to that synchronous cell reset (no global toolbar or Undo/Redo routing changes). A new unit regression failed before the correction and passes afterward; existing browser assertions were not weakened.

Exact-head CI for b6377fb is now successful in all four jobs, with 156/156 browser tests (52 each Chromium, Firefox, WebKit). Local follow-on checks: 12 focused tests, full typecheck and changed-file typed lint passed. The published files were read back and matched the tested candidate. This remains draft for review; no upstream merge was performed.

@petyosi
petyosi marked this pull request as ready for review November 2, 2026 14:57
petyosi added a commit that referenced this pull request Nov 2, 2026
After #1004 was narrowed to table cells, the nested-history names and the single-item loop in the browser spec no longer described what they cover.
@petyosi
petyosi merged commit ddef71e into mdx-editor:main Nov 2, 2026
4 checks passed
@petyosi

petyosi commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merged, thanks. This is the size I asked for. The effect only re-imports a cell when its stored contents change. The stale-write check in save() and the cleared local history after a restore cover Undo/Redo, and other cells keep their unsaved typing.

On top of it, 07daea0 renames the example and tests from nested-history to table-history, because they now cover only tables. It also removes the single-item for (const kind of ['root']) loop from the browser spec.

The temporary CAN_UNDO/CAN_REDO handlers around CLEAR_HISTORY_COMMAND depend on the order in which listeners were registered. The comment explains why, so I left them as they are.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 4.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants