Skip to content

[lexical-rich-text] Bug Fix: forward dropped files when the drop point has no caret - #9076

Merged
etrepum merged 4 commits into
meta:mainfrom
nithin42:fix-drop-files-without-caret
Aug 29, 2026
Merged

etrepum merged 4 commits into
meta:mainfrom
nithin42:fix-drop-files-without-caret

Conversation

@nithin42

Copy link
Copy Markdown
Contributor

Description

registerRichText's DROP_COMMAND handler only dispatches DRAG_DROP_PASTE when caretFromPoint() manages to resolve a caret for the drop coordinates:

const eventRange = caretFromPoint(x, y, editor.getRootElement());
if (eventRange !== null) {
  ...set the selection...
  editor.dispatchCommand(DRAG_DROP_PASTE, files);
}
event.preventDefault();
return true;

The preventDefault() and return true are outside that if, so when caretFromPoint() returns null the handler still swallows the drop but never forwards the files. From the user's point of view they drop an image onto the editor and absolutely nothing happens, with no error.

caretFromPoint() returns null in real situations, not just theoretical ones: the browser gives back no caret position for points that do not land on text (dropping onto the editor's padding or a gutter is the easy one to hit), and the final fallback in that function returns null outright when neither caretRangeFromPoint nor caretPositionFromPoint is available.

The PASTE_COMMAND handler a bit further down in the same file already dispatches DRAG_DROP_PASTE unconditionally once it has files, so the two paths disagree today. This change makes drop behave like paste.

Fix

Hoist the dispatchCommand(DRAG_DROP_PASTE, files) call out of the if (eventRange !== null) block. Setting the selection from the drop point stays conditional, since that genuinely needs a resolved caret; when there is none the files are inserted at the current selection instead of being thrown away.

Test plan

Added packages/lexical-rich-text/src/__tests__/unit/RichTextDropFiles.test.ts with two cases:

  • a drop with files where the point resolves to no caret must still dispatch DRAG_DROP_PASTE (fails on main, passes with this change)
  • a drop with files where the point does resolve to a caret still dispatches it (guards the path that already worked)
npx vitest --project unit --no-watch packages/lexical-rich-text

Before the change the first test fails with expected null not to be null. After it, both pass. The lexical-rich-text, lexical-clipboard and lexical-plain-text unit suites are green (21 files, 170 tests). prettier and eslint are clean on both files.

…t has no caret

The DROP_COMMAND handler only dispatched DRAG_DROP_PASTE when caretFromPoint returned a range, but it called preventDefault and returned true either way. When the browser cannot resolve a caret for the drop coordinates the dropped files were silently discarded.

PASTE_COMMAND already dispatches DRAG_DROP_PASTE unconditionally, so this also makes the two paths agree.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 19, 2026
@vercel

vercel Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 28, 2026 5:31pm
lexical-playground Ready Ready Preview Aug 28, 2026 5:31pm

Request Review

@etrepum

etrepum commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

A lint rule changed since this PR was opened so you'll need to apply pnpm run lint:fix to this in order to pass integrity checks

@etrepum
etrepum added this pull request to the merge queue Aug 29, 2026
Merged via the queue into meta:main with commit c653233 Aug 29, 2026
44 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 4cec8cb3 Deployed Aug 28, 2026 by vercel[bot]
Preview – lexical-playground — 4cec8cb3 Deployed Aug 28, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants