Skip to content

fix(muya): don't crash or replace another block when picking a table size after undo (#5355) - #5384

Merged
Jocs merged 3 commits into
developfrom
fix/5355-stale-table-pick
Sep 16, 2026
Merged

Jocs merged 3 commits into
developfrom
fix/5355-stale-table-pick

Conversation

@Jocs

@Jocs Jocs commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

Summary

In the 0.20 engine the table size grid stays open while the document changes underneath it, and a pick then acts on whatever block holds the caret:

  • Pick after Undo removed the line: if Edit > Undo removes the / line that opened the grid, picking a size throws Cannot destructure property 'path' of 'this.parent' as it is null.
  • Earlier edits in the session: the pick throws nothing but replaces another paragraph, and its text, with the table.
  • Menu commands: the same Undo also makes Paragraph > Table… and the context menu's Insert Paragraph Before/After throw.

About the report. #5355 was filed from v0.19.1 with a stack from the legacy muyajs engine (ContentState.updateParagraph ← QuickInsert.selectItem) and no steps, so we don't know what the reporter actually did. An earlier triage run reproduced that stack on packaged v0.19.1 (open the @ insert menu, Undo, click Heading 1). This PR does not touch the legacy engine; it fixes the crash from the same user action — a pick from the insert menu after Undo — in the 0.20 engine, where it still reproduces on v0.20.0-rc.3 and develop.

Steps to reproduce

Desktop app, v0.20.0-rc.3 or a develop build.

  1. Open a file with two paragraphs, Hello and World. Make no other edits (the crash needs the undo to be the first edit group since the file loaded).
  2. Click at the end of Hello and press Enter.
  3. Type / and click Table (or type /table and press Enter). The row × column grid opens.
  4. Edit > Undo. The / line disappears, but the grid stays open.
  5. Click any grid cell, or the grid's OK button → Cannot destructure property 'path' of 'this.parent' as it is null., and the grid stays open.

Variants:

  • List item, block quote, or a one-paragraph file: same error.
  • The file already has its own / line below Hello: same error.
  • Earlier edit: type zz after World and wait a second, then do steps 2–5. No error, but World zz is replaced by the table.
  • Undo restores real text into the line: select abc, type /, pick Table, then Undo. The pick replaces abc.
  • Query replaced by typing while the grid is open: type /table + Enter, press Backspace ×6, type Important. The pick replaces Important.
  • Front menu, no Undo needed: choose Table from the front menu of an empty paragraph while the caret is in World. World is replaced instead of the empty paragraph.
  • Grid rows input: open the grid, click its rows input, type a number, then Edit > Undo. Pressing Enter in the input gives the same error.
  • Paragraph menu and context menu: after step 2, type abc and run Edit > Undo. Paragraph > Table… > OK logs TypeError: Cannot read properties of null (reading 'insertAfter') and inserts no table; right-click > Insert Paragraph Before/After throws reading 'insertBefore' / 'insertAfter'.

Root cause

1. Stale caret after undoing the first edit. Undoing the first edit after a file loads restores no caret, because that undo entry has no selection. If the undo removed the line that held the caret, editor.activeContentBlock and the cached selection still point at the removed block. Muya._immediateBlockAtCursor returned that detached block, and createTable / insertParagraph then called insertAfter/insertBefore on its null parent. _outmostBlockAtCursor, which the other paragraph commands use, already returned null in this state. setContent without autoFocus leaves the same stale caches.

2. The grid pick targeted the caret, not the line that opened the grid. The pick called createTable({ replace: true }), which replaces whichever block holds the caret at pick time. After Undo that block is either the detached line (the crash) or the paragraph Undo moved the caret to (the content loss). The pick also never checked that the line still held only its / query.

Fix

Block commands

  • _immediateBlockAtCursor returns null when the cached caret block is no longer in the document, so createTable and insertParagraph do nothing instead of throwing.
  • createTable takes an optional block (default: the block at the caret) and does nothing when that block is not in the document.
  • Undo and redo still do not move the caret or focus.

Table grid

showTablePicker remembers the block it was opened on and the text of its first content leaf. A pick acts on that block only while the block is still in the document and its text is unchanged or blank; any other pick returns without touching anything — no table, no caret change, and the grid closes as before.

That single identity check covers every case: the line removed by Undo, a line whose text Undo restored, a query the user replaced with real text, a different document loaded with setContent, and a pre-existing / line that moved into the removed line's place.

Behaviour changes

  • Stale picks do nothing. A pick whose line is gone, or whose line now holds real text, no longer inserts a table; before, it crashed or replaced that text. The grid closes and nothing else changes — including the caret, which after such an Undo is already outside the document (typing lands nowhere until you click, with or without the pick).
  • Undo → Redo → pick no longer inserts a table. Redo re-creates the line as a new block, which the pick no longer recognises, so it is ignored; rc.3 inserts the table there. Nothing is lost or misplaced — the / line is still in the document and choosing Table again works.
  • Front menu. Table from the front menu replaces the hovered empty paragraph, not the block holding the caret.
  • Menu commands after the first-edit Undo. Until the caret is placed again, Paragraph > Table… and Insert Paragraph Before/After do nothing. Before, they threw.

Known gaps

  • The grid stays visible after Undo. It closes on the next pick or a click outside. Escape closes it only while focus is in the editor, because the grid listens for Escape on the editor element; after clicking Table in the / menu focus is not in the editor (same as rc.3).
  • Right-click does not place the caret. Right-clicking a word selects it but does not update the cached caret, so after the first-edit Undo, Insert Paragraph Before/After still does nothing until you click or type in the document. Typing then replaces the selected word, as on develop.
  • Typed characters can miss the document (not changed here). Typing right after undoing the first edit next to a table or task list puts the characters in the DOM but not in the document. Same as develop.

Test plan

  • Unit: src/__tests__/blockCommandsDetachedCaret5355.spec.ts (6 tests) and src/__tests__/tablePickerTrigger5355.spec.ts (10 tests), plus the updated replaceBlockByLabel.spec.ts. They cover the trigger block rather than the caret block becoming the table, a same-text / line left alone, setContent of another document, real text kept, an ignored pick changing nothing at all (block tree, caret caches and document.activeElement compared before and after), and the redo-recreated line being ignored. RED against develop's muya.ts / blockTransforms.ts.
  • e2e: e2e/tests/ui/quick-insert-table-after-undo-5355.spec.ts — fails on develop, passes in Chromium and WebKit.
  • pnpm -C packages/muya test: 236 files, 1679 tests passed.
  • lint:types, eslint on the changed files (the e2e spec with --no-ignore), check-circular, desktop pnpm run typecheck.
  • muya e2e tests/ui in Chromium: the only failure is paragraph-front Turn Into, which also fails locally without this change.
  • Desktop app, Playwright-driven with a fresh --user-data-dir, real mouse and keyboard, Edit menu items clicked from the application menu, and a renderer-error sink with a positive control on every launch. Measured on packaged v0.20.0-rc.3 and on a build of this branch: all the steps and variants above, plus the untouched flows (/table + Enter → pick, undo/redo of a real insertion, the grid's row/column inputs, click-away) and an 80-line document (the view does not move on an ignored pick).
  • Windows / Linux, Firefox

Fixes #5355

🤖 Generated with Claude Code

Jocs and others added 3 commits September 15, 2026 20:08
…ck (#5355)

Undoing the first edit after setContent (e.g. Enter and typing in a file
that was just opened) restores no caret, because that undo entry has no
selection. When the undo removed the paragraph the caret was in,
activeContentBlock and the cached selection kept pointing at the removed
paragraph. setContent without autoFocus leaves the same stale caches.

createTable and insertParagraph('before'|'after') find their target
through _immediateBlockAtCursor, which returned that detached paragraph,
and then threw on its null parent: Paragraph > Table... threw "reading
'insertAfter'" and Insert Paragraph Before/After threw "reading
'insertBefore'/'insertAfter'".

_outmostBlockAtCursor already returns null for such a caret, since a
detached block has no outmost block, so duplicate, deleteParagraph and
updateParagraph did nothing there. Give _immediateBlockAtCursor the same
check, so the commands built on it do nothing too.

Undo still does not move the caret or focus: placing one would pull
focus out of an input the user is typing in, such as the search box.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…#5355)

The row x column grid opened from "/" Table, "/table" + Enter or the
front menu stays open while typing, undo and redo change the document.
Its pick called createTable({ replace: true }), which replaced whatever
block held the caret at pick time:

- after Undo removed the "/" line as the first edit of a file, the caret
  caches still held that line and the pick threw "Cannot destructure
  property 'path' of 'this.parent' as it is null";
- with earlier edits, Undo put the caret into another paragraph and the
  pick replaced that paragraph, and its text, with the table;
- when Undo restored real text into the line, or the user replaced the
  query with real text while the grid was open, the pick replaced that
  text;
- the front menu's Table entry on an empty paragraph replaced the
  paragraph holding the caret instead of the hovered one.

showTablePicker now keeps the block it opened on and the text of that
block's first content leaf. A pick turns that block into the table,
passing it to createTable through a new optional `block` that defaults to
the block at the caret, and only while the block is still in the document
and still holds that text, or nothing. createTable does nothing once its
block has left the document.

Any other pick changes nothing at all: no table, no caret placement, no
focus change, no scroll. The grid closes as it always does and the
document is left exactly as it was, which is also the state the user was
already in: after Undo removed the line, the caret caches point at a
block that is gone and typing reaches nothing until the user clicks back
into the text.

A pick after Undo then Redo therefore inserts nothing where it used to
insert the table, because redo re-creates the line as a different block.
Recognising it would mean trusting whatever block now sits at the old
path, which replaces a pre-existing line of the same text, unless the
whole document is compared against a snapshot taken when the grid opened.

The grid does not open from a block that is not in the document: a pick
from it could only be ignored, and when the caret has no coords the float
falls back to that block's DOM node, which is no longer laid out.

The grid is deliberately not hidden when Undo runs: json-change fires
before an undo patches the block tree, so a listener there would see the
old tree. The pick is the one moment the grid changes the document, so
that is where the check lives.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…5355)

Drives the reported flows in Chromium:

- "/" Table, "/table" + Enter and a list item: after Undo a grid pick
  changes nothing - not the document, not the caret, not the focused
  element - and a click back into the text types as usual;
- with the caret moved into the next paragraph before Undo, and with
  earlier edits in the session, that paragraph keeps its text;
- in an 80-line document the pick leaves the scroll position and the
  line's position on screen untouched;
- Undo then Redo re-creates the line as another block, so the pick
  inserts nothing;
- with a "/" line directly below the trigger, a pick after Undo keeps
  the line that moved into the removed line's place;
- a pick keeps real text that Undo restored into the line, or that
  replaced the query while the grid was open, and turns a line whose
  "/" alone was undone into the table;
- Undo with focus in the search box or in the grid's row input leaves
  that focus and the document alone;
- createTable and Insert Paragraph Before / After after Undo removed the
  caret line do nothing instead of throwing;
- the front menu's Table entry replaces the hovered empty paragraph,
  not the paragraph holding the caret.

Checks that the markdown did not change wait two animation frames first,
because block edits reach the markdown on the next frame.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@Jocs
Jocs force-pushed the fix/5355-stale-table-pick branch from 80a261a to e39aa1f Compare September 16, 2026 02:11
@Jocs
Jocs merged commit 83b2685 into develop Sep 16, 2026
7 checks passed
@Jocs
Jocs deleted the fix/5355-stale-table-pick branch September 16, 2026 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected error with message Cannot destructure property 'text' of 'block2' as it is null.

1 participant