Skip to content

fix(muya): guard checkNeedRender against a null cursor (image-preview reclick crash) - #4588

Merged
Jocs merged 1 commit into
developfrom
fix/image-preview-blur-crash
Jun 20, 2026
Merged

Jocs merged 1 commit into
developfrom
fix/image-preview-blur-crash

Conversation

@Jocs

@Jocs Jocs commented Jun 20, 2026

Copy link
Copy Markdown
Member

Summary

Fixes a renderer crash when clicking an inline image again after closing its full-screen preview.

Repro: select an inline image → press Space (opens SimpleImageViewer) → press Esc → click the same image again → TypeError: Cannot read properties of undefined (reading 'offset').

Root cause: Format.checkNeedRender (packages/muya/src/block/base/format.ts) resolved its anchor/focus offsets with non-null assertions (anchor!.offset / focus!.offset). blurHandler() invokes it with the default cursor derived from this.selection.anchor/focus. The image-preview path leaves the image's paragraph as the active content block with a null selection (no text caret is seated, and Esc doesn't restore one), so the next click's blurHandler() dereferenced undefined.offset.

Fix: when no cursor offset is resolvable there is nothing for the cursor-proximity check to compare against, so return false (no re-render needed) instead of dereferencing.

Test

Adds packages/muya/src/block/base/__tests__/blurHandlerNullCursor.spec.ts (TDD — written failing first): blurHandler() / checkNeedRender() no longer throw with a null selection and return false, while a real cursor inside inline markup still triggers a re-render.

Verification

  • New spec: 3/3 pass
  • Full muya unit suite: 1158 passed (148 files)
  • CommonMark + GFM conformance: pass

🤖 Generated with Claude Code

Format.checkNeedRender resolved its offsets via `anchor!.offset` /
`focus!.offset`, but blurHandler() calls it with the default cursor built
from this.selection.anchor/focus. After the image-preview path (select
image -> Space opens SimpleImageViewer -> Esc), ImageSelection never seats
a text caret, so the image's paragraph stays the active content block with
a null selection. The next click fires blurHandler() on that paragraph and
the undefined offsets threw "Cannot read properties of undefined (reading
'offset')".

Return false (nothing to re-render) when no cursor offset is resolvable.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@Jocs
Jocs force-pushed the fix/image-preview-blur-crash branch from 2c4af81 to dd333a9 Compare June 20, 2026 07:46
@Jocs
Jocs merged commit 727736a into develop Jun 20, 2026
7 checks passed
@Jocs
Jocs deleted the fix/image-preview-blur-crash branch October 23, 2026 08:32
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.

1 participant