fix(muya): select the document when Select All's cached caret block was removed by undo (#5387) by Jocs · Pull Request #5425 · marktext/marktext · GitHub
Skip to content

fix(muya): select the document when Select All's cached caret block was removed by undo (#5387) - #5425

Merged
Jocs merged 2 commits into
developfrom
fix/5387-select-all-after-undo
Sep 17, 2026
Merged

Jocs merged 2 commits into
developfrom
fix/5387-select-all-after-undo

Conversation

@Jocs

@Jocs Jocs commented Sep 16, 2026

Copy link
Copy Markdown
Member

Summary

After undoing a new paragraph as the first edit since the file loaded, Edit > Select All (or Cmd+A) selected nothing and logged Cannot destructure property 'path' of 'this.parent' as it is null. Typing then inserted at the caret instead of replacing the document (a one-line file a was saved as aX), and Backspace deleted nothing. Select All now selects the whole document in that state.

Steps to reproduce

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

  1. Open a file whose only content is a. Make no other edits.
  2. Click at the end of a, press Enter and type b.
  3. Edit > Undo. The b line disappears.
  4. Edit > Select All, type X, File > Save → the file is aX and the console shows the error above.

With a, a blank line and c, the file is saved as a, a blank line, Xc.

Root cause

  1. History records each undo entry's selection from the previous edit, so the first edit group after load is stored with selection: null.
  2. Undoing it calls _restoreSelection(null), which returns without placing a caret. Removing the b paragraph leaves the DOM caret on the editor container, and the cached selection still points at the removed paragraph.
  3. Selection.selectAll reads the live DOM selection, gets null, and falls back to the cached anchor block. Reading that block's path throws, because its paragraph no longer has a parent.

A separate edit before step 2 (for example typing z and waiting a second) gives the undo entry a selection, and Select All works.

Fix

selectAll only grows a selection inside a block that is still in the document (outMostBlock is non-null, the same check #5384 uses for block commands). Otherwise it falls through to selecting the whole document.

This fix is limited to Select All. Undo and redo still do not place a caret, same as #5384.

Behaviour notes

  • a / c variant: the first Select All after the undo selects the whole document. Without the undo it first selects the current paragraph, but after this undo the caret is not in any paragraph.
  • Not changed: typing right after this undo, without Select All, still inserts where the DOM caret landed (aX, Xc), same as develop.

Test plan

  • Unit: src/selection/__tests__/selectAllAfterUndo5387.spec.ts (a and a/c, real undo with the null undo-entry precondition pinned). Fails on develop with the reported error, passes with the fix.

  • e2e: e2e/tests/editing/select-all-after-undo-5387.spec.ts (Enter + type, Undo, Select All, then type X or Backspace, no page errors). Fails on develop, passes 30/30 with --repeat-each=5 in Chromium and WebKit.

  • pnpm -C packages/muya test: 247 files, 1812 tests passed. lint:types, check-circular, eslint on the changed files (e2e spec with --no-ignore).

  • Desktop app driven by Playwright with a fresh --user-data-dir and Edit menu items clicked from the application menu, packaged v0.20.0-rc.3 vs a build:unpack of this branch:

    Scenario rc.3 / develop saved this branch saved
    a: Undo, Select All, type X aX + 1 error X, 0 errors
    a: Undo, Select All ×3, type X aX + 3 errors X, 0 errors
    a: Undo, Select All, Backspace a + 1 error empty, like the no-undo control
    a/c: Undo, Select All ×2, type X a, blank line, Xc + 2 errors X, 0 errors
    a: Undo, Cmd+A, type X aX + 1 error X, 0 errors
    Controls: no Undo, or an earlier separate edit unchanged unchanged
  • Windows / Linux, Firefox

Fixes #5387

🤖 Generated with Claude Code

Jocs and others added 2 commits September 16, 2026 22:02
…as removed by undo (#5387)

Undoing the first edit after a file loads restores no caret, because that undo entry has no selection. When the undo removed the paragraph holding the caret, the DOM caret ends up on the editor container, so `selectAll` fell back to the cached anchor block, which was the removed paragraph, and threw `Cannot destructure property 'path' of 'this.parent' as it is null.` before selecting anything. Typing then inserted at the caret and Backspace deleted nothing.

`selectAll` now only grows a selection inside a block that is still in the document; otherwise it selects the whole document. Undo still does not move the caret.

Fixes #5387

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Enter + typing, Undo and Select All in a real browser, then typing or Backspace replaces the whole document without page errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Jocs
Jocs merged commit de6c2ff into develop Sep 17, 2026
7 checks passed
@Jocs
Jocs deleted the fix/5387-select-all-after-undo branch September 17, 2026 07:51
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.

[Bug] Select All does nothing after undoing a newly created paragraph

1 participant