Repository navigation
fix(muya): don't crash or replace another block when picking a table size after undo (#5355) - #5384
Merged
Merged
Conversation
…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
force-pushed
the
fix/5355-stale-table-pick
branch
from
September 16, 2026 02:11
80a261a to
e39aa1f
Compare
4 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
/line that opened the grid, picking a size throwsCannot destructure property 'path' of 'this.parent' as it is null.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.
HelloandWorld. Make no other edits (the crash needs the undo to be the first edit group since the file loaded).Helloand press Enter./and click Table (or type/tableand press Enter). The row × column grid opens./line disappears, but the grid stays open.Cannot destructure property 'path' of 'this.parent' as it is null., and the grid stays open.Variants:
/line belowHello: same error.zzafterWorldand wait a second, then do steps 2–5. No error, butWorld zzis replaced by the table.abc, type/, pick Table, then Undo. The pick replacesabc./table+ Enter, press Backspace ×6, typeImportant. The pick replacesImportant.World.Worldis replaced instead of the empty paragraph.abcand run Edit > Undo. Paragraph > Table… > OK logsTypeError: Cannot read properties of null (reading 'insertAfter')and inserts no table; right-click > Insert Paragraph Before/After throwsreading '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.activeContentBlockand the cached selection still point at the removed block.Muya._immediateBlockAtCursorreturned that detached block, andcreateTable/insertParagraphthen calledinsertAfter/insertBeforeon its null parent._outmostBlockAtCursor, which the other paragraph commands use, already returned null in this state.setContentwithout 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
_immediateBlockAtCursorreturns null when the cached caret block is no longer in the document, socreateTableandinsertParagraphdo nothing instead of throwing.createTabletakes an optionalblock(default: the block at the caret) and does nothing when that block is not in the document.Table grid
showTablePickerremembers 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
/line is still in the document and choosing Table again works.Known gaps
/menu focus is not in the editor (same as rc.3).Test plan
src/__tests__/blockCommandsDetachedCaret5355.spec.ts(6 tests) andsrc/__tests__/tablePickerTrigger5355.spec.ts(10 tests), plus the updatedreplaceBlockByLabel.spec.ts. They cover the trigger block rather than the caret block becoming the table, a same-text/line left alone,setContentof another document, real text kept, an ignored pick changing nothing at all (block tree, caret caches anddocument.activeElementcompared before and after), and the redo-recreated line being ignored. RED against develop'smuya.ts/blockTransforms.ts.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, desktoppnpm run typecheck.tests/uiin Chromium: the only failure is paragraph-front Turn Into, which also fails locally without this change.--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).Fixes #5355
🤖 Generated with Claude Code