Skip to content

fix(link-dialog): don't crash when the selection rectangle is unavailable - #956

Open
simplydt wants to merge 1 commit into
mdx-editor:mainfrom
simplydt:fix/link-dialog-null-selection-rectangle
Open

fix(link-dialog): don't crash when the selection rectangle is unavailable#956
simplydt wants to merge 1 commit into
mdx-editor:mainfrom
simplydt:fix/link-dialog-null-selection-rectangle

Conversation

@simplydt

Copy link
Copy Markdown

Summary

Fixes #753 — clicking the Create Link toolbar button while an image is selected crashes the whole editor with TypeError: Cannot read properties of null (reading 'top') (reproducible on the official demo).

Root cause

getSelectionRectangle() returns null when the native DOM selection is not inside the editor root element — which is the case when an image (decorator node) holds the selection, since the Lexical selection and the native selection diverge there (as noted in #753). openLinkEditDialog$ force-asserts the result as non-null:

const rectangle = getSelectionRectangle(editor)!

so a state with rectangle: null reaches LinkDialog, which reads theRect.top and throws, unmounting the editor tree.

Fix

Remove the non-null assertion and fall back to the editor root element's bounding rectangle, so the dialog still opens (anchored to the editor) instead of crashing. This follows the graceful-degradation behavior requested in #753, and mirrors the null handling that the link preview path already does a few lines above (which bails to inactive when getSelectionRectangle returns null).

Testing

  • npm run typecheck — clean
  • npx eslint src/plugins/link-dialog/index.ts — clean
  • npm run test:once — 79 passed, 1 skipped
  • Manually verified the equivalent change against 3.52.1: insert an image → click Create Link → dialog opens anchored to the editor, no crash

🤖 Generated with Claude Code

…gle is null

getSelectionRectangle returns null when the native DOM selection is not
inside the editor root element, e.g. when an image (decorator node) is
selected. openLinkEditDialog$ force-asserted the result as non-null, so
clicking the Create Link toolbar button with an image selected published
a state with rectangle: null and LinkDialog crashed on theRect.top with
"Cannot read properties of null (reading 'top')", taking down the whole
editor tree.

Fall back to the editor root element's bounding rectangle so the dialog
still opens (anchored to the editor) instead of crashing.

Fixes mdx-editor#753

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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] “Create Link” button with image selected throws “Cannot read properties of null” error

1 participant