Skip to content

[lexical-playground] Fix : Clear block formatting if all of its content is selected and handle the case of partial selection. - #8237

Closed
Jynx2004 wants to merge 34 commits into
meta:mainfrom
Jynx2004:format_issue_fix
Closed

Jynx2004 wants to merge 34 commits into
meta:mainfrom
Jynx2004:format_issue_fix

Conversation

@Jynx2004

Copy link
Copy Markdown
Contributor

Description

Updated clear-formatting to detect the “no formatting” case by checking selection.extract() instead of comparing anchor/focus. This ensures alignment is cleared even when the selection is collapsed and no text formatting is present.

Closes #7383

lexical-format.mp4

Test plan

All the existing unit tests passed . No new tests created.

@vercel

vercel Bot commented Mar 18, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Apr 11, 2026 8:13pm
lexical-playground Ready Ready Preview, Comment Apr 11, 2026 8:13pm

Request Review

@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 Mar 18, 2026

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

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 solves the issue

@Jynx2004

Copy link
Copy Markdown
Contributor Author

This PR needs tests to demonstrate that it solves the issue

I have added the unit test to check this functionality

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think this is quite a complete solution, it's a bit greedy in that it will always clear formatting from the parent even if you've only selected some of the text. I think the block formatting should only be cleared if the block is selected, or all of its contents are selected.

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Mar 19, 2026

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think this is working correctly because if I create an empty ParagraphNode, set some formatting (e.g. center alignment) and then clear formatting then it stays center aligned

Comment on lines +368 to 385
if (!$isRangeSelection(postExtractSelection)) {
return;
}

const postExtractCaretRange =
$caretRangeFromSelection(postExtractSelection);
const anchorNext = $getCaretInDirection(
postExtractCaretRange.anchor,
'next',
);
const focusNext = $getCaretInDirection(
postExtractCaretRange.focus,
'next',
);

if ($comparePointCaretNext(anchorNext, focusNext) === 0) {
return;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not quite sure this is the correct logic but it looks like you were just trying to check if the selection was collapsed

Suggested change
if (!$isRangeSelection(postExtractSelection)) {
return;
}
const postExtractCaretRange =
$caretRangeFromSelection(postExtractSelection);
const anchorNext = $getCaretInDirection(
postExtractCaretRange.anchor,
'next',
);
const focusNext = $getCaretInDirection(
postExtractCaretRange.focus,
'next',
);
if ($comparePointCaretNext(anchorNext, focusNext) === 0) {
return;
}
if (!$isRangeSelection(postExtractSelection) || postExtractSelection.isCollapsed()) {
return;
}

@etrepum
etrepum marked this pull request as draft April 22, 2026 04:21
@etrepum etrepum added the stale-pr PRs that are closed due to staleness. Please reopen the PR if there are new updates label May 11, 2026
@etrepum

etrepum commented May 11, 2026

Copy link
Copy Markdown
Collaborator

Closing due to inactivity, re-open or create a new PR when/if you intend to continue working on this

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 061f4095 Deployed Apr 11, 2026 by vercel[bot]
Preview – lexical — 061f4095 Deployed Apr 11, 2026 by vercel[bot]
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 stale-pr PRs that are closed due to staleness. Please reopen the PR if there are new updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: [lexical-playground] clear-formatting doesn't clear text-left/right/center alignment when no formatting to text

2 participants