-import {$getSelection, BaseSelection, LexicalEditor} from "lexical";
+import {$createTextNode, $getSelection, BaseSelection, LexicalEditor, TextNode} from "lexical";
import {$getBlockElementNodesInSelection, $selectNodes, $toggleSelection} from "./selection";
import {nodeHasInset} from "./nodes";
import {$createListItemNode, $createListNode, $isListItemNode, $isListNode, ListItemNode} from "@lexical/list";
return node;
}
+ const nodeChildList = node.getChildren().filter(n => $isListNode(n))[0] || null;
+ const nodeChildItems = nodeChildList?.getChildren() || [];
+
const listItems = list.getChildren() as ListItemNode[];
const nodeIndex = listItems.findIndex((n) => n.getKey() === node.getKey());
const isFirst = nodeIndex === 0;
node.remove();
}
+ if (nodeChildList) {
+ for (const child of nodeChildItems) {
+ newListItem.insertAfter(child);
+ }
+ nodeChildList.remove();
+ }
+
return newListItem;
}
return node;
}
+ const laterSiblings = node.getNextSiblings();
+
parentListItem.insertAfter(node);
if (list.getChildren().length === 0) {
list.remove();
parentListItem.remove();
}
+ if (laterSiblings.length > 0) {
+ const childList = $createListNode(list.getListType());
+ childList.append(...laterSiblings);
+ node.append(childList);
+ }
+
+ if (list.getChildrenSize() === 0) {
+ list.remove();
+ }
+
return node;
}
export function $setInsetForSelection(editor: LexicalEditor, change: number): void {
const selection = $getSelection();
+ const selectionBounds = selection?.getStartEndPoints();
const listItemsInSelection = getListItemsForSelection(selection);
const isListSelection = listItemsInSelection.length > 0 && !listItemsInSelection.includes(null);
alteredListItems.reverse();
}
- $selectNodes(alteredListItems);
+ if (alteredListItems.length === 1 && selectionBounds) {
+ // Retain selection range if moving just one item
+ const listItem = alteredListItems[0] as ListItemNode;
+ let child = listItem.getChildren()[0] as TextNode;
+ if (!child) {
+ child = $createTextNode('');
+ listItem.append(child);
+ }
+ child.select(selectionBounds[0].offset, selectionBounds[1].offset);
+ } else {
+ $selectNodes(alteredListItems);
+ }
+
return;
}