[lexical] Bug Fix: Fix forward line deletion when using control+K (#7412)

This commit is contained in:
Daniel Teo
2025-04-02 01:32:59 +08:00
committed by GitHub
parent 46f8113073
commit c0f3008534
3 changed files with 66 additions and 3 deletions

View File

@ -259,6 +259,62 @@ test.describe.parallel('Selection', () => {
await assertHTML(page, lines(''));
});
test('can delete text by line forwards with control+K', async ({
page,
isPlainText,
}) => {
const deleteLineForwardWithControlK = async () => {
await page.keyboard.down('Control');
await page.keyboard.press('k');
await page.keyboard.up('Control');
};
test.skip(isPlainText || !IS_MAC);
await focusEditor(page);
await page.keyboard.type('One');
await page.keyboard.press('Enter');
await page.keyboard.type('Two');
await page.keyboard.press('Enter');
await page.keyboard.press('Enter');
await page.keyboard.type('Three');
const p = (text) =>
text
? html`
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">${text}</span>
</p>
`
: html`
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
`;
const lines = (...args) => html`
${args.map(p).join('')}
`;
await assertHTML(page, lines('One', 'Two', '', 'Three'));
// Move to the end of the line of 'Two'
await moveUp(page, 2);
await deleteLineForwardWithControlK(page);
await assertHTML(page, lines('One', 'Two', 'Three'));
await deleteLineForwardWithControlK(page);
await assertHTML(page, lines('One', 'TwoThree'));
await deleteLineForwardWithControlK(page);
await assertHTML(page, lines('One', 'Two'));
await deleteLineForwardWithControlK(page);
await assertHTML(page, lines('One', 'Two'));
await moveToEditorBeginning(page);
await deleteLineForwardWithControlK(page);
await assertHTML(page, lines('', 'Two'));
await deleteLineForwardWithControlK(page);
await assertHTML(page, lines('Two'));
await deleteLineForwardWithControlK(page);
await assertHTML(page, lines(''));
await deleteLineForwardWithControlK(page);
await assertHTML(page, lines(''));
});
test('can delete line which ends with element backwards with CMD+delete', async ({
page,
isPlainText,

View File

@ -1178,7 +1178,7 @@ function onKeyDown(event: KeyboardEvent, editor: LexicalEditor): void {
} else if (isDeleteLineBackward(key, metaKey)) {
event.preventDefault();
dispatchCommand(editor, DELETE_LINE_COMMAND, true);
} else if (isDeleteLineForward(key, metaKey)) {
} else if (isDeleteLineForward(key, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, DELETE_LINE_COMMAND, false);
} else if (isBold(key, altKey, metaKey, ctrlKey)) {

View File

@ -911,8 +911,15 @@ export function isDeleteLineBackward(key: string, metaKey: boolean): boolean {
return IS_APPLE && metaKey && isBackspace(key);
}
export function isDeleteLineForward(key: string, metaKey: boolean): boolean {
return IS_APPLE && metaKey && isDelete(key);
export function isDeleteLineForward(
key: string,
metaKey: boolean,
ctrlKey: boolean,
): boolean {
return (
IS_APPLE &&
((metaKey && isDelete(key)) || (ctrlKey && key.toLowerCase() === 'k'))
);
}
export function isDeleteBackward(