[lexical-playground] Feature: clear blockelement formatting along with textNode (#7384)

This commit is contained in:
Harshkumar Metkel
2025-03-27 20:57:37 +05:30
committed by GitHub
parent 7dd022053c
commit 283a8fa4b1
2 changed files with 56 additions and 1 deletions

View File

@ -7,6 +7,8 @@
*/
import {
centerAlign,
rightAlign,
selectAll,
toggleBold,
toggleItalic,
@ -192,4 +194,53 @@ test.describe('Clear All Formatting', () => {
);
},
);
test(`Can clear left/center/right alignment when BIU formatting already applied`, async ({
page,
}) => {
await focusEditor(page);
await page.keyboard.type('Hello');
await toggleBold(page);
await page.keyboard.type(' World');
await rightAlign(page);
await page.keyboard.type(' Test');
await selectAll(page);
await selectFromAdditionalStylesDropdown(page, '.clear');
await assertHTML(
page,
html`
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr"
style="">
<span data-lexical-text="true">Hello World Test</span>
</p>
`,
);
});
test(`Can clear left/center/right alignment when BIU formatting not applied`, async ({
page,
}) => {
await focusEditor(page);
await page.keyboard.type('Hello World');
await rightAlign(page);
await page.keyboard.type(' Test');
await centerAlign(page);
await selectAll(page);
await selectFromAdditionalStylesDropdown(page, '.clear');
await assertHTML(
page,
html`
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr"
style="">
<span data-lexical-text="true">Hello World Test</span>
</p>
`,
);
});
});

View File

@ -276,7 +276,11 @@ export const clearFormatting = (editor: LexicalEditor) => {
}
if (textNode.__format !== 0) {
textNode.setFormat(0);
$getNearestBlockElementAncestorOrThrow(textNode).setFormat('');
}
const nearestBlockElement =
$getNearestBlockElementAncestorOrThrow(textNode);
if (nearestBlockElement.__format !== 0) {
nearestBlockElement.setFormat('');
}
node = textNode;
} else if ($isHeadingNode(node) || $isQuoteNode(node)) {