Skip to content

Fix multi-line selection creating separate code blocks per line#912

Merged
samuelpecher merged 12 commits intomainfrom
fix-multiline-code-block
Apr 8, 2026
Merged

Fix multi-line selection creating separate code blocks per line#912
samuelpecher merged 12 commits intomainfrom
fix-multiline-code-block

Conversation

@samuelpecher
Copy link
Copy Markdown
Collaborator

@samuelpecher samuelpecher commented Mar 23, 2026

Summary

  • Fixed selecting multiple lines and applying code block format — now creates a single multiline code block instead of separate blocks per line
  • Toggle-off splits code block content back into separate paragraphs
  • Added tests for both applying and toggling code blocks on multi-line selections

This is powered by a new theory: toggling block format can reuse the same code path as pasting/inserting etc. This introduces a CodeNodeInserter and simply extracts and reinserts the selected block-level nodes into a new CodeNode

Fixes Fizzy card #5029: Making multiple lines a code block not working as expected

Copilot AI review requested due to automatic review settings March 23, 2026 16:16
@samuelpecher samuelpecher self-assigned this Mar 23, 2026

This comment was marked as outdated.

Copy link
Copy Markdown
Member

@jorgemanrubia jorgemanrubia left a comment

Choose a reason for hiding this comment

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

👍

`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.
Copilot AI review requested due to automatic review settings April 7, 2026 18:04
@samuelpecher samuelpecher force-pushed the fix-multiline-code-block branch from 5efadf9 to 118fbe1 Compare April 7, 2026 18:04

This comment was marked as outdated.

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
@samuelpecher samuelpecher force-pushed the fix-multiline-code-block branch from 118fbe1 to 975ddd4 Compare April 8, 2026 14:20
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
Copilot AI review requested due to automatic review settings April 8, 2026 14:28
@samuelpecher samuelpecher force-pushed the fix-multiline-code-block branch from 975ddd4 to 6c61a2f Compare April 8, 2026 14:28
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

@samuelpecher samuelpecher merged commit ed2bcaf into main Apr 8, 2026
16 checks passed
@samuelpecher samuelpecher deleted the fix-multiline-code-block branch April 8, 2026 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants