Skip to content

[lexical-rich-text] Feat: add pasteFileContentCheck config to let files win over incidental HTML on paste - #8902

Merged
etrepum merged 3 commits into
meta:mainfrom
Sa-Te:fix/image-paste-file-transfer
Aug 5, 2026
Merged

etrepum merged 3 commits into
meta:mainfrom
Sa-Te:fix/image-paste-file-transfer

Conversation

@Sa-Te

@Sa-Te Sa-Te commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #8681

Problem

Browsers put both a Files entry and a text/html fallback on the clipboard when an image is copied via the right-click context menu. eventFiles()'s hasContent check treats any text/html/text/plain entry as "this is a content paste", which unconditionally blocks DRAG_DROP_PASTE from firing in registerRichText's PASTE_COMMAND handler — so a browser-copied image is always routed through the HTML importer instead of being handed to the app as a file, even when the app would prefer the file.

Fix

  • eventFiles() now takes an optional hasContent predicate (packages/lexical-utils), defaulting to the exact historical text/html/text/plain check, so every existing caller (drag/drop, dragstart, dragover) is unaffected.
  • registerRichText threads a new pasteFileContentCheck signal parameter through to that call in the PASTE_COMMAND handler, following the same signal-parameter pattern already used for escapeFormatTriggers.
  • RichTextExtension's config gains pasteFileContentCheck (LexicalRichTextExtension.ts), reactive via the existing namedSignals/effect wiring — no new plumbing needed.
  • Default behavior is unchanged; apps opt in explicitly, e.g.:
  configExtension(RichTextExtension, {
    pasteFileContentCheck: (dataTransfer) =>
      dataTransfer.types.includes('text/plain'),
  })

This follows the API shape suggested by @etrepum in the issue thread.

  • Updated the matching Flow declaration in packages/lexical-rich-text/flow/LexicalRichText.js.flow and the extension list in the website docs.

Test plan

  • New unit tests in PasteFileContentCheck.test.ts: default behavior preserved, override lets files win, override doesn't hijack a file-less HTML paste, file-only paste still works under the default, and the config is reactive via signal mutation.
  • Full pnpm run test-unit passes (excluding a pre-existing, unrelated timeout in FastPathCrossParent.test.ts).
  • pnpm run tsc, flow, lint, prettier all clean.
  • pnpm run test-e2e-chromium on the full CopyAndPaste e2e suite
    (52 tests) passes, confirming default paste/drop behavior across
    rich text, lists, tables, and HTML import is unaffected.

@vercel

vercel Bot commented Aug 1, 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 3, 2026 8:22am
lexical-playground Ready Ready Preview Aug 3, 2026 8:22am

Request Review

Comment on lines +133 to +134
state.getOutput().escapeFormatTriggers,
state.getOutput().pasteFileContentCheck,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be refactored to call getOutput once

Comment thread packages/lexical-utils/src/index.ts Outdated
Comment on lines +849 to +851
hasContent: (dataTransfer: DataTransfer) => boolean = dataTransfer =>
dataTransfer.types.includes('text/html') ||
dataTransfer.types.includes('text/plain'),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to define this default multiple times, it should be defined once and re-used

Comment thread packages/lexical-rich-text/src/index.ts Outdated
event,
pasteFileContentCheck.peek(),
);
if (files.length > 0 && !hasTextContent) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the real predicate that matters for this use case, maybe this PR's approach is more complicated than it needs to be.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. you're right, this was more machinery than the problem needed. I've pushed a simpler version: instead of parameterizing eventFiles's internal hasContent check (which touches a shared utility used by 5 other call sites that don't care about it), I moved the actual decision up to registerRichText/RichTextExtension as a single shouldHandlePasteAsFiles(files, hasTextContent) predicate, with one default definition. lexical-utils is untouched now. Also fixed the double getOutput() call.
One tradeoff worth flagging: this predicate gets a precomputed boolean rather than the raw DataTransfer, so it can't distinguish text/html from text/plain specifically — that covers the issue's actual repro (browser copy-image), but let me know if you want the finer-grained version instead.

@etrepum
etrepum added this pull request to the merge queue Aug 5, 2026
Merged via the queue into meta:main with commit 0561e5f Aug 5, 2026
78 of 81 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — b8a64b7a Deployed Aug 3, 2026 by vercel[bot]
Preview – lexical-playground — b8a64b7a Deployed Aug 3, 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.

Feature: Insert the copied image from the browser as a file

2 participants