Fix pasting into blockquote splitting on line breaks#911
Fix pasting into blockquote splitting on line breaks#911samuelpecher wants to merge 2 commits intomainfrom
Conversation
When pasting text containing newlines into a blockquote, the pasted content would break out of the quote at each line break. This happened because $generateNodesFromDOM produced multiple ParagraphNode objects that selection.insertNodes() placed as top-level siblings outside the blockquote. The fix detects when pasting inside a QuoteNode and flattens paragraph wrappers into inline children (text nodes separated by line breaks) before inserting, keeping all content within the quote.
There was a problem hiding this comment.
Pull request overview
This pull request fixes an issue where pasting text with line breaks into a blockquote would cause the content to break out of the quote at each newline. The fix adds special handling for paste operations inside quote nodes by flattening paragraph wrappers and separating them with line breaks instead.
Changes:
- Added a check in the
insertDOMmethod to detect when pasting into a quote node and invoke special handling - Added a new private method
#$insertNodesIntoQuotethat flattens paragraph wrappers from pasted content into inline children separated by line breaks within the quote
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/editor/contents.js
Outdated
|
|
||
| #$insertNodesIntoQuote(selection, nodes) { | ||
| const anchorNode = selection.anchor.getNode() | ||
| const quoteNode = anchorNode.getParents().find($isQuoteNode) |
There was a problem hiding this comment.
The method getParents() does not exist in Lexical. Based on the codebase patterns (e.g., selection.js:510-516), you should traverse parent nodes using a loop with getParent(). The code should collect parents in an array or use a while loop to find the quote node ancestor.
| const quoteNode = anchorNode.getParents().find($isQuoteNode) | |
| let quoteNode = null | |
| let parent = anchorNode.getParent() | |
| while (parent !== null) { | |
| if ($isQuoteNode(parent)) { | |
| quoteNode = parent | |
| break | |
| } | |
| parent = parent.getParent() | |
| } |
There was a problem hiding this comment.
For the readers, LexicalNode does expose getParents(), but $getNearestNodeOfType(node, CodeNode) is the best approach here.
…de blocks when pasting into blockquotes Replace getParents().find($isQuoteNode) with $getNearestNodeOfType(anchorNode, QuoteNode) for consistency with the rest of the codebase. Extend the paste-into-blockquote flattening logic to also handle heading and code block elements, preventing them from escaping the blockquote on paste.
Summary
Fixes Fizzy card #4979: Pasting as quoted text doesn't always work as expected