Skip to content

[Breaking Change][lexical-code] Bug Fix: move code block escape logic to KEY_ENTER_COMMAND listener#8360

Merged
etrepum merged 8 commits intofacebook:mainfrom
anikakpillai:fix/8351-code-block-paste-newlines
Apr 17, 2026
Merged

[Breaking Change][lexical-code] Bug Fix: move code block escape logic to KEY_ENTER_COMMAND listener#8360
etrepum merged 8 commits intofacebook:mainfrom
anikakpillai:fix/8351-code-block-paste-newlines

Conversation

@anikakpillai
Copy link
Copy Markdown
Contributor

@anikakpillai anikakpillai commented Apr 15, 2026

Breaking Changes

The "press enter three times to exit the CodeNode" logic has been moved from CodeNode.insertNewAfter to a KEY_ENTER_COMMAND listener registered by CodeExtension.

Using CodeNode without CodeExtension is now deprecated to allow this functionality to be moved to a command listener. There is a shim in the implementation that will provide backwards compatible behavior and log a warning (in dev) when this scenario is encountered, but you should expect that the node will eventually stop working correctly in a future version if your editor uses CodeNode without CodeExtension.

Description

When pasting text containing consecutive blank lines into a code block,
content after the blank lines incorrectly escapes the code block and
appears as top-level paragraphs.

The root cause was that the escape-to-paragraph logic in
CodeNode.insertNewAfter ran on all content changes, including paste.

Per maintainer feedback, this fix takes the preferred approach of moving
the escape logic out of insertNewAfter entirely and into a
KEY_ENTER_COMMAND listener registered in CodeExtension. This ensures
the escape behavior only triggers on keyboard input, not paste or other
programmatic changes.

Closes #8351

Test plan

Before

Paste text with consecutive blank lines into a code block — content
after the blank lines escapes to a top-level paragraph.

After

  • Pasted content stays inside the code block
  • Pressing Enter at the end of a code block after two blank lines
    still correctly exits to a new paragraph
  • Four unit tests added covering: escape on Enter, no escape on paste,
    no escape with single blank line, no escape when cursor is not at end

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Apr 17, 2026 4:45am
lexical-playground Ready Ready Preview, Comment Apr 17, 2026 4:45am

Request Review

@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 15, 2026

Hi @anikakpillai!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 15, 2026
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 15, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

Generally speaking it's better if PRs add tests to demonstrate that they work as intended

Comment thread packages/lexical-code-core/src/CodeNode.ts Outdated
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Apr 16, 2026
@anikakpillai anikakpillai changed the title [lexical-code] Bug Fix: prevent code block escape when pasting consecutive blank lines [lexical-code] Bug Fix: move code block escape logic to KEY_ENTER_COMMAND listener Apr 16, 2026
@anikakpillai
Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've updated the approach per your original suggestion on the issue. The escape logic is now moved out of insertNewAfter entirely and into a KEY_ENTER_COMMAND listener in CodeExtension, so it only fires on keyboard input. Added 4 unit tests as well.

Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

Your latest changes fail integrity checks (looks like type errors) and existing end-to-end tests

@etrepum etrepum marked this pull request as draft April 16, 2026 22:56
@anikakpillai anikakpillai marked this pull request as ready for review April 17, 2026 00:26
Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

This looks good, but I think we will have to think about the compatibility scenarios here since it is a breaking change, if you are using CodeNode in a legacy app that is not using CodeExtension then you will lose the functionality to exit the CodeNode by pressing return three times.

@etrepum etrepum changed the title [lexical-code] Bug Fix: move code block escape logic to KEY_ENTER_COMMAND listener [Breaking Change][lexical-code] Bug Fix: move code block escape logic to KEY_ENTER_COMMAND listener Apr 17, 2026
@etrepum etrepum added this pull request to the merge queue Apr 17, 2026
Merged via the queue into facebook:main with commit 9588e94 Apr 17, 2026
38 checks passed
@etrepum etrepum mentioned this pull request Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Pasting text with consecutive blank lines into a code block results in text outside of the code block

2 participants