Skip to content

Commit

Permalink
[lexical-list] Bug Fix: Prevent error when calling formatList when se…
Browse files Browse the repository at this point in the history
…lection is at root
  • Loading branch information
amanharwara committed Dec 24, 2024
1 parent e79c946 commit 6393d7a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/lexical-list/src/formatList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import {$getNearestNodeOfType} from '@lexical/utils';
import {
$createParagraphNode,
$getRoot,
$getSelection,
$isElementNode,
$isLeafNode,
$isRangeSelection,
$isRootOrShadowRoot,
$setSelection,
ElementNode,
LexicalEditor,
LexicalNode,
Expand Down Expand Up @@ -77,6 +79,20 @@ export function insertList(editor: LexicalEditor, listType: ListType): void {
const anchorNode = anchor.getNode();
const anchorNodeParent = anchorNode.getParent();

if (anchor.key === 'root') {
const root = $getRoot();
const firstChild = root.getFirstChild();
if (firstChild) {
$setSelection(firstChild.selectStart());
} else {
const paragraph = $createParagraphNode();
root.append(paragraph);
$setSelection(paragraph.select());
}
insertList(editor, listType);
return;
}

if ($isSelectingEmptyListItem(anchorNode, nodes)) {
const list = $createListNode(listType);

Expand Down

0 comments on commit 6393d7a

Please sign in to comment.