Skip to content

[lexical-clipboard] Bug Fix: Update Lexical Clipboard with Empty Selection#7835

Merged
etrepum merged 3 commits intofacebook:mainfrom
jkjk822:patch-3
Sep 20, 2025
Merged

[lexical-clipboard] Bug Fix: Update Lexical Clipboard with Empty Selection#7835
etrepum merged 3 commits intofacebook:mainfrom
jkjk822:patch-3

Conversation

@jkjk822
Copy link
Copy Markdown
Contributor

@jkjk822 jkjk822 commented Sep 19, 2025

Description

$copyToClipboardEvent updates a ClipboardEvent based on the current selection - however returning null for an absent or empty selection will prevent the Lexical content from getting cleared for an empty selection. This means the 'text/plain' and 'text/html' contents will update to empty strings, but the 'application/x-lexical-editor' will still have whatever stale value was present previously.

An alternative fix to the one committed already is to delete the content when null, rather than skipping, i.e.

const v = $editorFn(editor, selection);
if (v !== null) {
  clipboardData[mimeType] = v;
} else {
  delete clipboardData[mimeType]
}

This would preserve the null return for $getLexicalContent, though it doesn't look like anything in the repo depends on it.

Test plan

No affected tests - though I could probably add one if desired.

@vercel
Copy link
Copy Markdown

vercel bot commented Sep 19, 2025

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

Project Deployment Preview Comments Updated (UTC)
lexical Ready Ready Preview Comment Sep 20, 2025 1:42am
lexical-playground Ready Ready Preview Comment Sep 20, 2025 1:42am

@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 Sep 19, 2025
@jkjk822 jkjk822 marked this pull request as ready for review September 19, 2025 22:26
Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

I don't think changing the semantics of $getLexicalContent is the correct fix. This is part of the public API and it's not clear why it should change.

The fix should probably be closer to the problem, probably in setLexicalClipboardDataTransfer. Maybe just a call to clipboardData.clearData() before the updates.

The clipboard often has data from the prior selection. We should clear it as we might not be overwriting all the same data types there were present previously, which would leave stale data in the clipboard.
@jkjk822
Copy link
Copy Markdown
Contributor Author

jkjk822 commented Sep 19, 2025

Ah, good idea. That is much simpler and handles other stale data cases outside Lexical as well. I confirmed it still fixes the issue in my repo as well.

If this looks good I can try to write a quick test.

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Sep 19, 2025
@jkjk822 jkjk822 changed the title [lexical-playground] Bug Fix: Update Lexical Clipboard with Empty Selection [lexical-clipboard] Bug Fix: Update Lexical Clipboard with Empty Selection Sep 19, 2025
@etrepum etrepum added this pull request to the merge queue Sep 20, 2025
Merged via the queue into facebook:main with commit ddf44a3 Sep 20, 2025
67 of 70 checks passed
@jkjk822
Copy link
Copy Markdown
Contributor Author

jkjk822 commented Sep 22, 2025

Looks good, thanks for cleaning up and merging.

@jkjk822 jkjk822 deleted the patch-3 branch September 22, 2025 17:51
This was referenced Sep 25, 2025
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