Skip to content

fix(text editor): make the code block toggle context-aware and preserve line breaks - #4227

Open
john-traas wants to merge 3 commits into
mainfrom
text-editor-code-block
Open

fix(text editor): make the code block toggle context-aware and preserve line breaks#4227
john-traas wants to merge 3 commits into
mainfrom
text-editor-code-block

Conversation

@john-traas

Copy link
Copy Markdown
Contributor

Summary

Rebuilds the code block command on the selection-context ladder introduced for the list commands in #4222, per #4225.

  • New code-block-utils.ts: resolveCodeBlockContext (in-code / no-code / mixed), a convertibility guard (paragraphs, headings and code blocks convert; blockquotes are transparent; lists, tables, rules and other nodes decline), a toggle-off transform that splits each code block on \n into one paragraph per line, and a unify transform that merges multi-block and mixed selections into a single code block.
  • createCodeBlockCommand: the command body and allowed() share the same applicability rules, so the toolbar button is hidden whenever the command cannot apply — no more enabled-looking button that silently does nothing.
  • exampleSetup's undocumented raw Shift-Ctrl-\ binding is suppressed via mapKeys, leaving Mod-Shift-C (which routes through the command) as the single code block shortcut.

Fixes #3179
Fixes #3314
Closes #4225

Behavior matrix

Selection Command allowed()
Inside one code block One paragraph per line true
Spanning only code blocks Each toggled off true
One paragraph/heading Converted in place true
Multiple paragraphs/headings Merged into one code block, one line per block true
Mixed code + text blocks Unified into one code block true
Paragraph inside a blockquote Converted in place true
Touches list / table / rule / other Declines, no dispatch false (button hidden)
NodeSelection / AllSelection Declines false

Tests

  • menu-code-block-commands.spec.ts: 23 behavior-matrix cases on a standalone schema, including no-dispatch assertions for every decline.
  • editor-commands.spec.ts: real-schema toggle-off, mixed unify, and list-decline cases.
  • editor-keymap.e2e.ts: mounted-view coverage for multi-block merge, toggle-off split, and the unbound Shift-Ctrl-\.

Notes

🤖 Generated with Claude Code

@john-traas
john-traas requested a lite review from Copilot August 13, 2026 20:37
@coderabbitai

coderabbitai Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@john-traas, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 16 minutes

Limit details: You’ve used all 1 included review currently available under your plan.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: fcca5729-a81a-46c0-85e9-9b04f90a95c6

📥 Commits

Reviewing files that changed from the base of the PR and between 4a73391 and e1db1d6.

📒 Files selected for processing (6)
  • src/components/text-editor/prosemirror-adapter/editor-commands.spec.ts
  • src/components/text-editor/prosemirror-adapter/editor-config.ts
  • src/components/text-editor/prosemirror-adapter/editor-keymap.e2e.ts
  • src/components/text-editor/prosemirror-adapter/menu/menu-code-block-commands.spec.ts
  • src/components/text-editor/prosemirror-adapter/menu/menu-command-utils/code-block-utils.ts
  • src/components/text-editor/prosemirror-adapter/menu/menu-commands.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown

Documentation has been published to https://lundalogik.github.io/lime-elements/versions/PR-4227/

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR rebuilds the text editor’s code block command to be selection-context aware (matching the list-command “ladder” approach) and to preserve line breaks when toggling code blocks off, aligning toolbar visibility (allowed()) with actual command applicability.

Changes:

  • Added code-block-utils.ts to classify selection context, detect non-convertible selections, toggle-off by splitting into one paragraph per line, and unify multi-block/mixed selections into a single code block.
  • Replaced the legacy code block toggle wiring with createCodeBlockCommand, sharing applicability rules between the command body and allowed() so toolbar and keymap behavior match.
  • Suppressed exampleSetup’s raw Shift-Ctrl-\ code block binding so Mod-Shift-C is the single supported shortcut path.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/components/text-editor/prosemirror-adapter/menu/menu-commands.ts Introduces createCodeBlockCommand and wires it into the command mapping so keymap/toolbar share applicability rules.
src/components/text-editor/prosemirror-adapter/menu/menu-command-utils/code-block-utils.ts Implements context resolution, convertibility guard, toggle-off (split per line), and unify-to-single-code-block transforms.
src/components/text-editor/prosemirror-adapter/menu/menu-code-block-commands.spec.ts Adds standalone-schema unit tests covering the behavior matrix and no-dispatch decline cases.
src/components/text-editor/prosemirror-adapter/editor-keymap.e2e.ts Adds mounted-view coverage for multi-block merge, toggle-off split, and the unbound Shift-Ctrl-\ case.
src/components/text-editor/prosemirror-adapter/editor-config.ts Disables exampleSetup’s Shift-Ctrl-\ binding via mapKeys to avoid bypassing the command ladder.
src/components/text-editor/prosemirror-adapter/editor-commands.spec.ts Extends real-schema tests to cover merge/unify/split behaviors and list-touch decline + allowed() behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@john-traas
john-traas force-pushed the text-editor-list-handling branch from b4b10fc to 337cf7f Compare August 17, 2026 12:23
@john-traas
john-traas marked this pull request as ready for review August 17, 2026 12:28
Base automatically changed from text-editor-list-handling to main August 17, 2026 12:33
@john-traas
john-traas force-pushed the text-editor-code-block branch from 26c805e to e1db1d6 Compare August 17, 2026 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants