[lexical-list] Bug Fix: empty list item type change (#7380)

This commit is contained in:
Maksim Horbachevsky
2025-03-25 16:39:15 -04:00
committed by GitHub
parent fc58b1325b
commit 8c14af5d75
2 changed files with 61 additions and 25 deletions

View File

@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*
*/
import {
$createTableCellNode,
$createTableNode,
@ -14,11 +13,18 @@ import {
TableNode,
TableRowNode,
} from '@lexical/table';
import {$createParagraphNode, $createTextNode, $getRoot} from 'lexical';
import {
$createParagraphNode,
$createTextNode,
$getRoot,
$nodesOfType,
$selectAll,
} from 'lexical';
import {initializeUnitTest} from 'lexical/src/__tests__/utils';
import {$insertList} from '../../formatList';
import {$isListNode} from '../../LexicalListNode';
import {$createListItemNode} from '../../LexicalListItemNode';
import {$createListNode, $isListNode, ListNode} from '../../LexicalListNode';
describe('insertList', () => {
initializeUnitTest((testEnv) => {
@ -97,5 +103,32 @@ describe('insertList', () => {
expect($isListNode(firstChild)).toBe(true);
});
});
test('formatting empty list items', async () => {
const {editor} = testEnv;
await editor.update(() => {
$getRoot().append(
$createListNode('bullet').append(
$createListItemNode().append($createTextNode('Level 1')),
$createListItemNode().append(
$createListNode('bullet').append($createListItemNode()),
),
),
);
});
await editor.update(() => {
$selectAll();
$insertList('number');
});
editor.read(() => {
const lists = $nodesOfType(ListNode).filter(
(node) => node.getListType() === 'number',
);
expect(lists.length).toBe(2);
});
});
});
});

View File

@ -121,31 +121,34 @@ export function $insertList(listType: ListType): void {
continue;
}
if ($isLeafNode(node)) {
let parent = node.getParent();
while (parent != null) {
const parentKey = parent.getKey();
let parent = $isLeafNode(node)
? node.getParent()
: $isListItemNode(node) && node.isEmpty()
? node
: null;
if ($isListNode(parent)) {
if (!handled.has(parentKey)) {
const newListNode = $createListNode(listType);
append(newListNode, parent.getChildren());
parent.replace(newListNode);
handled.add(parentKey);
}
while (parent != null) {
const parentKey = parent.getKey();
break;
} else {
const nextParent = parent.getParent();
if ($isRootOrShadowRoot(nextParent) && !handled.has(parentKey)) {
handled.add(parentKey);
$createListOrMerge(parent, listType);
break;
}
parent = nextParent;
if ($isListNode(parent)) {
if (!handled.has(parentKey)) {
const newListNode = $createListNode(listType);
append(newListNode, parent.getChildren());
parent.replace(newListNode);
handled.add(parentKey);
}
break;
} else {
const nextParent = parent.getParent();
if ($isRootOrShadowRoot(nextParent) && !handled.has(parentKey)) {
handled.add(parentKey);
$createListOrMerge(parent, listType);
break;
}
parent = nextParent;
}
}
}