fix(link-dialog): don't crash when the selection rectangle is unavailable - #956
Open
simplydt wants to merge 1 commit into
Open
fix(link-dialog): don't crash when the selection rectangle is unavailable#956simplydt wants to merge 1 commit into
simplydt wants to merge 1 commit into
Conversation
…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>
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
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()returnsnullwhen 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:so a state with
rectangle: nullreachesLinkDialog, which readstheRect.topand 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
inactivewhengetSelectionRectanglereturns null).Testing
npm run typecheck— cleannpx eslint src/plugins/link-dialog/index.ts— cleannpm run test:once— 79 passed, 1 skipped🤖 Generated with Claude Code