Skip to content

fix(Code Block):Code Block formatting applies to unintended adjacent lines #7699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Jynx2004
Copy link

This pull request updates the formatCode function in formatCode.ts to improve the handling of code block insertion for non-collapsed selections. The changes modify the else condition to achieve the following:

New Behavior: When a non-collapsed selection is detected, the function now:
Creates a new code node.
Inserts the code node as a sibling immediately below the selected node (using anchorNode.insertAfter(codeNode)).
Moves the selected text content into the new code node.
Removes the original selected content.

Rationale: This ensures the code block is placed directly after the selected element in the editor's node structure, maintaining a cleaner and more intuitive document hierarchy compared to the previous approach.

Impact: The change improves the user experience by correctly positioning code blocks relative to the selection, avoiding nested or misplaced nodes.

Closes #7691

Copy link

vercel bot commented Jul 17, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 17, 2025 6:20pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 17, 2025 6:20pm

@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 Jul 17, 2025
Copy link
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 PR needs tests to demonstrate that it works. You should also test that it works when multiple lines are selected.

I can confirm that it doesn't work correctly when two lines separated by a linebreak are selected and then converted to a code block.

@Jynx2004
Copy link
Author

This PR needs tests to demonstrate that it works. You should also test that it works when multiple lines are selected.

I can confirm that it doesn't work correctly when two lines separated by a linebreak are selected and then converted to a code block.

Sure will do the testing considering all the cases

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Code Block formatting applies to unintended adjacent lines
2 participants