Fix multi-line selection creating separate code blocks per line#912
Merged
samuelpecher merged 12 commits intomainfrom Apr 8, 2026
Merged
Fix multi-line selection creating separate code blocks per line#912samuelpecher merged 12 commits intomainfrom
samuelpecher merged 12 commits intomainfrom
Conversation
`tag` can be an array, at which point PASTE_TAG would not be detected with simple equality
Move DOM insertion via insertAtCursor
Th class will respond to Selection's insertNodes for interchangeability. Then, the inserters can self-declare if they handle a selection scenario
Allows RangeSelection insertion to fall back to default insertion
When pasting non-inline nodes, Lexical will split the receiving node and insert them. For QuoteNode, we want to simply insert the nodes into the QuoteNode after the selection.
5efadf9 to
118fbe1
Compare
Selecting multiple lines and pressing the Code block button was creating one code block per line instead of a single code block containing all selected lines. The old implementation used $setBlocksType which wraps each block individually. Rewrite toggleCodeBlock() to collect all top-level elements in the selection and merge them into a single CodeNode separated by line breaks, mirroring how toggleBlockquote() already works. Toggle-off splits the code block content at line breaks back into separate paragraphs.
When toggling a code block on a blockquote (or list) containing multiple paragraphs, the content was collapsed into a single line because #applyCodeBlockFormat only iterated top-level elements without descending into containers. Two fixes: - In toggleCodeBlock, unwrap non-paragraph/non-code container elements (using the existing #unwrap method) and re-gather top-level elements before merging into a code block. - In hasSelectedWordsInSingleLine, detect when anchor and focus are in different block-level children of the same top-level element (e.g. two paragraphs inside a blockquote) so the code button correctly triggers code block mode instead of inline code.
Essentially, don't make a code node out of top-level decorators
118fbe1 to
975ddd4
Compare
Theory: toggling format is simply moving existing nodes into a new node of the target format... no difference from pasting them etc. This makes the code toggle do just that: use a CodeNodeInserter and just insert the block-level selected nodes into a new CodeNode
975ddd4 to
6c61a2f
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This is powered by a new theory: toggling block format can reuse the same code path as pasting/inserting etc. This introduces a
CodeNodeInserterand simply extracts and reinserts the selected block-level nodes into a newCodeNodeFixes Fizzy card #5029: Making multiple lines a code block not working as expected