Skip to content
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

[WIP][lexical-rich-text][lexical-playground] Bug Fix: Prevent unnecessary history entries on DELETE_CHARACTER_COMMAND #7222

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,85 @@ test('Headings - stays as a heading when you backspace at the start of a heading
`,
);
});

test('Headings - converts to paragraph when backspace at start of empty heading', async ({
page,
isPlainText,
isCollab,
}) => {
test.skip(isPlainText);
await initialize({isCollab, page});
await focusEditor(page);

await click(page, '.block-controls');
await click(page, '.dropdown .icon.h1');

await assertHTML(
page,
html`
<h1 class="PlaygroundEditorTheme__h1">
<br />
</h1>
`,
);

await page.keyboard.press('Backspace');

await assertHTML(
page,
html`
<p class="PlaygroundEditorTheme__paragraph">
<br />
</p>
`,
);
});

test('Headings - does not create unnecessary history entries when backspace at start of heading with content', async ({
page,
isPlainText,
isCollab,
}) => {
test.skip(isPlainText);
await initialize({isCollab, page});
await focusEditor(page);

// Create an empty heading
await click(page, '.block-controls');
await click(page, '.dropdown .icon.h1');

// This is our meaningful change that should be undoable
await page.keyboard.type('Welcome to the playground');

await moveToEditorBeginning(page);

// These backspace operations should not create history entries
await page.keyboard.press('Backspace');
await page.keyboard.press('Backspace');
await page.keyboard.press('Backspace');

// Verify content is unchanged after backspace attempts
await assertHTML(
page,
html`
<h1
class="PlaygroundEditorTheme__h1 PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">Welcome to the playground</span>
</h1>
`,
);

// Press undo - should undo the typing since backspace didn't create history entries
await page.keyboard.press('Meta+z');

// Should revert to empty heading since that was the state before typing
await assertHTML(
page,
html`
<h1 class="PlaygroundEditorTheme__h1">
<br />
</h1>
`,
);
});
7 changes: 4 additions & 3 deletions packages/lexical-rich-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,10 @@ export class HeadingNode extends ElementNode {
}

collapseAtStart(): true {
const newElement = !this.isEmpty()
? $createHeadingNode(this.getTag())
: $createParagraphNode();
if (!this.isEmpty()) {
return true;
}
const newElement = $createParagraphNode();
const children = this.getChildren();
children.forEach((child) => newElement.append(child));
this.replace(newElement);
Expand Down
Loading