[lexical] Bug Fix: Preserve pasted paragraph alignment when merging into destination block#8438
[lexical] Bug Fix: Preserve pasted paragraph alignment when merging into destination block#8438matingathani wants to merge 4 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
Fixes a Lexical paste/merge edge case where a pasted block’s alignment (element format) could be dropped when the paste operation merges only the pasted block’s children into an existing destination block, by transferring the pasted block’s format to the destination when the destination has no explicit format.
Changes:
- Preserve pasted block alignment by copying
formatTypeto the destination block during merge when the destination has no explicit format. - Expand block-DOM detection to include
details/summary. - Harden HTML import by wrapping hoisted inline/text children in paragraphs when there is no block ancestor, with new unit tests covering the behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/lexical/src/LexicalSelection.ts | Copies pasted block format to the destination block before merging children when the destination has no explicit format. |
| packages/lexical/src/tests/unit/HTMLCopyAndPaste.test.ts | Adds a copy/paste test intended to ensure centered alignment is preserved. |
| packages/lexical/src/LexicalUtils.ts | Treats details and summary as block DOM nodes. |
| packages/lexical-html/src/index.ts | Wraps hoisted inline/text children in paragraphs when importing unknown/unconverted DOM nodes without a block ancestor. |
| packages/lexical-html/src/tests/unit/LexicalHtml.test.ts | Adds tests for importDOM behavior around unknown block/nested elements and summary. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
10c4cac to
4f019a2
Compare
etrepum
left a comment
There was a problem hiding this comment.
These tests aren't really comprehensive enough to confirm that it doesn't have edge cases, there are failed PRs for this feature that you could look at for examples of what those cases are.
…g into destination block
Cover four additional edge cases beyond the existing center-into-empty test: - Right-aligned paragraph pasted into empty destination (right alignment preserved) - Center-aligned pasted into non-empty right-aligned destination (destination format not overridden) - Multiple paragraphs with mixed alignment each preserve their own format - Unaligned pasted paragraph adds no text-align to destination
4f019a2 to
7fea4f3
Compare
|
Thanks for the review @etrepum. Addressed both points: Merge conflicts / unrelated changes: Branch has been rebased on Tests: Expanded from 1 to 5 test cases covering the following scenarios:
Case 3 verifies the guard condition Let me know if there are additional edge cases from the previously failed PRs you'd like covered. |
Review: Preserve Pasted Paragraph Alignment When Merging Into Destination BlockReviewed by: Navi (AI review assistant for @potatowagon) SummaryWhen pasting a paragraph with explicit alignment (center, right, etc.) into an empty paragraph, the alignment is now preserved on the destination block. Previously, the pasted content's format was lost during the merge operation. What I Verified
NoteThis PR may overlap with #8310 which addresses the same issue (paste alignment for non-empty paragraphs). The approaches differ slightly — this one is more conservative (only applies to empty destination blocks). Both should be reviewed together to decide which approach to merge. Verdict✅ Safe to approve — clean, minimal change to core selection logic with comprehensive tests. The guard condition prevents unintended format overwriting. |
potatowagon
left a comment
There was a problem hiding this comment.
Review — Preserve Pasted Paragraph Alignment
Assessment: Looks good to land ✅
What I verified:
-
Bug fix logic: When pasting a paragraph with alignment (e.g., centered) into an empty/default-aligned destination paragraph, the format wasn't being preserved because the merge logic in
RangeSelection.insertNodesonly transferred children, not formatting. The fix checks if the source has a format and the destination has the default format (''), then copies it over. -
Condition correctness: The guard
firstToInsert.getFormatType() !== '' && firstBlock.getFormatType() === ''correctly avoids overwriting an explicitly-set destination alignment. This means: paste centered into default → preserves center; paste centered into right-aligned → keeps right-aligned. This is the expected UX. -
Test coverage: 6 test cases covering: centered into empty (preserves), right-aligned into empty (preserves), centered into already-aligned (no override), copy/paste round-trip with centered text, right-aligned round-trip, and mixed alignment blocks.
-
CI status: Full CI suite green (39 checks pass).
-
Duplicate PR note: PR #8310 addresses the same issue with a slightly different approach. Both PRs should not be merged together — one needs to be chosen. This PR's approach is cleaner (simpler condition).
— via Navi on behalf of potatowagon
Description
When pasting a paragraph with center/right alignment into a non-empty paragraph, the pasted block's format (alignment) was discarded because only the block's children (text nodes) were merged into the destination. This PR transfers the pasted block's format to the destination block before merging when the destination does not already have an explicit format.
Closes #8101
Test plan
Before
Copying a center-aligned paragraph and pasting into another paragraph would reset alignment to default (left).
After
The pasted content preserves its original alignment when the destination block does not have an explicit format set.