lexical icon indicating copy to clipboard operation
lexical copied to clipboard

[lexical][lexical-clipboard] Bug Fix: Preserve alignment when pasting selected text

Open Rohithmatham12 opened this issue 5 months ago • 4 comments

Description

When copying selected text from an aligned paragraph inside the same Lexical namespace, the application/x-lexical-editor clipboard payload took priority over text/html. That JSON payload only contained the selected TextNode, so pasting into another paragraph reused the destination paragraph's default alignment and discarded the source paragraph alignment.

This updates clipboard JSON export to retain the non-inline parent element when the selected child content covers the full element text. That preserves block-level formatting such as paragraph alignment for full-paragraph text selections while partial text selections remain text-only.

Closes #8101

Test plan

Before

Copying all visible text from an aligned paragraph and pasting it elsewhere lost the source paragraph alignment.

After

Full-paragraph text selections preserve block-level alignment on paste, including paragraphs that contain inline elements such as links, while partial text selections remain text-only.

Automated tests:

  • node node_modules/vitest/vitest.mjs --project unit packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts --run
  • node node_modules/vitest/vitest.mjs --project unit packages/lexical-clipboard/src/__tests__/unit/GetClipboardDataExtension.test.ts packages/lexical-clipboard/src/__tests__/unit/ClipboardImportExtension.test.ts --run
  • node node_modules/prettier/bin/prettier.cjs --check packages/lexical-clipboard/src/clipboard.ts packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts
  • node node_modules/eslint/bin/eslint.js packages/lexical-clipboard/src/clipboard.ts packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts

Rohithmatham12 avatar Jun 10 '26 19:06 Rohithmatham12

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

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

Request Review

vercel[bot] avatar Jun 10 '26 19:06 vercel[bot]

Title and description doesn't match the pull request template

etrepum avatar Jun 11 '26 04:06 etrepum

The approach makes sense, but $shouldIncludeElementWithSelectedChildren breaks on paragraphs containing inline elements (links, marks, etc.). selection.getNodes() includes intermediate element nodes, so their getTextContent() gets double-counted in the join — e.g. "Hello worldworld" vs "Hello world". Tested locally and confirmed the alignment isn't preserved when a link is present.

Details and a suggested fix inline. Also noted how HeadingNode handles the same problem via extractWithChild for reference.

mayrang avatar Jun 21 '26 15:06 mayrang

Addressed the review feedback in 752a47ecc:

  • filtered selected intermediate ElementNodes out of $shouldIncludeElementWithSelectedChildren() so inline elements like links are not double-counted
  • added regression coverage for preserving centered paragraph alignment when the selected paragraph contains a link
  • simplified the paste target selection to use the RangeSelection returned by target.select()

Re-ran:

node node_modules/vitest/vitest.mjs --project unit packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts --run
node node_modules/vitest/vitest.mjs --project unit packages/lexical-clipboard/src/__tests__/unit/GetClipboardDataExtension.test.ts packages/lexical-clipboard/src/__tests__/unit/ClipboardImportExtension.test.ts --run
node node_modules/prettier/bin/prettier.cjs --check packages/lexical-clipboard/src/clipboard.ts packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts
node node_modules/eslint/bin/eslint.js packages/lexical-clipboard/src/clipboard.ts packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts

Rohithmatham12 avatar Jun 21 '26 20:06 Rohithmatham12

Superseded by #9059

etrepum avatar Aug 29 '26 18:08 etrepum