2024-09-13 22:50:42 +08:00
|
|
|
import {$createCustomListItemNode, $isCustomListItemNode, CustomListItemNode} from "../nodes/custom-list-item";
|
|
|
|
import {$createCustomListNode, $isCustomListNode} from "../nodes/custom-list";
|
2024-10-04 22:11:09 +08:00
|
|
|
import {$getSelection, BaseSelection, LexicalEditor} from "lexical";
|
|
|
|
import {$getBlockElementNodesInSelection, $selectNodes, $toggleSelection} from "./selection";
|
2024-09-13 22:50:42 +08:00
|
|
|
import {nodeHasInset} from "./nodes";
|
|
|
|
|
|
|
|
|
2024-10-04 22:11:09 +08:00
|
|
|
export function $nestListItem(node: CustomListItemNode): CustomListItemNode {
|
2024-09-13 22:50:42 +08:00
|
|
|
const list = node.getParent();
|
|
|
|
if (!$isCustomListNode(list)) {
|
2024-10-04 22:11:09 +08:00
|
|
|
return node;
|
2024-09-13 22:50:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const listItems = list.getChildren() as CustomListItemNode[];
|
|
|
|
const nodeIndex = listItems.findIndex((n) => n.getKey() === node.getKey());
|
|
|
|
const isFirst = nodeIndex === 0;
|
|
|
|
|
|
|
|
const newListItem = $createCustomListItemNode();
|
|
|
|
const newList = $createCustomListNode(list.getListType());
|
|
|
|
newList.append(newListItem);
|
|
|
|
newListItem.append(...node.getChildren());
|
|
|
|
|
|
|
|
if (isFirst) {
|
|
|
|
node.append(newList);
|
|
|
|
} else {
|
|
|
|
const prevListItem = listItems[nodeIndex - 1];
|
|
|
|
prevListItem.append(newList);
|
|
|
|
node.remove();
|
|
|
|
}
|
2024-10-04 22:11:09 +08:00
|
|
|
|
|
|
|
return newListItem;
|
2024-09-13 22:50:42 +08:00
|
|
|
}
|
|
|
|
|
2024-10-04 22:11:09 +08:00
|
|
|
export function $unnestListItem(node: CustomListItemNode): CustomListItemNode {
|
2024-09-13 22:50:42 +08:00
|
|
|
const list = node.getParent();
|
|
|
|
const parentListItem = list?.getParent();
|
|
|
|
const outerList = parentListItem?.getParent();
|
|
|
|
if (!$isCustomListNode(list) || !$isCustomListNode(outerList) || !$isCustomListItemNode(parentListItem)) {
|
2024-10-04 22:11:09 +08:00
|
|
|
return node;
|
2024-09-13 22:50:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
parentListItem.insertAfter(node);
|
|
|
|
if (list.getChildren().length === 0) {
|
|
|
|
list.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parentListItem.getChildren().length === 0) {
|
|
|
|
parentListItem.remove();
|
|
|
|
}
|
2024-10-04 22:11:09 +08:00
|
|
|
|
|
|
|
return node;
|
2024-09-13 22:50:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function getListItemsForSelection(selection: BaseSelection|null): (CustomListItemNode|null)[] {
|
|
|
|
const nodes = selection?.getNodes() || [];
|
|
|
|
const listItemNodes = [];
|
|
|
|
|
|
|
|
outer: for (const node of nodes) {
|
|
|
|
if ($isCustomListItemNode(node)) {
|
|
|
|
listItemNodes.push(node);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parents = node.getParents();
|
|
|
|
for (const parent of parents) {
|
|
|
|
if ($isCustomListItemNode(parent)) {
|
|
|
|
listItemNodes.push(parent);
|
|
|
|
continue outer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
listItemNodes.push(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return listItemNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
function $reduceDedupeListItems(listItems: (CustomListItemNode|null)[]): CustomListItemNode[] {
|
|
|
|
const listItemMap: Record<string, CustomListItemNode> = {};
|
|
|
|
|
|
|
|
for (const item of listItems) {
|
|
|
|
if (item === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const key = item.getKey();
|
|
|
|
if (typeof listItemMap[key] === 'undefined') {
|
|
|
|
listItemMap[key] = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.values(listItemMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function $setInsetForSelection(editor: LexicalEditor, change: number): void {
|
2024-10-04 22:11:09 +08:00
|
|
|
const selection = $getSelection();
|
2024-09-13 22:50:42 +08:00
|
|
|
const listItemsInSelection = getListItemsForSelection(selection);
|
|
|
|
const isListSelection = listItemsInSelection.length > 0 && !listItemsInSelection.includes(null);
|
|
|
|
|
|
|
|
if (isListSelection) {
|
2024-10-04 22:11:09 +08:00
|
|
|
const alteredListItems = [];
|
2024-09-13 22:50:42 +08:00
|
|
|
const listItems = $reduceDedupeListItems(listItemsInSelection);
|
|
|
|
if (change > 0) {
|
|
|
|
for (const listItem of listItems) {
|
2024-10-04 22:11:09 +08:00
|
|
|
alteredListItems.push($nestListItem(listItem));
|
2024-09-13 22:50:42 +08:00
|
|
|
}
|
|
|
|
} else if (change < 0) {
|
|
|
|
for (const listItem of [...listItems].reverse()) {
|
2024-10-04 22:11:09 +08:00
|
|
|
alteredListItems.push($unnestListItem(listItem));
|
2024-09-13 22:50:42 +08:00
|
|
|
}
|
2024-10-04 22:11:09 +08:00
|
|
|
alteredListItems.reverse();
|
2024-09-13 22:50:42 +08:00
|
|
|
}
|
|
|
|
|
2024-10-04 22:11:09 +08:00
|
|
|
$selectNodes(alteredListItems);
|
2024-09-13 22:50:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const elements = $getBlockElementNodesInSelection(selection);
|
|
|
|
for (const node of elements) {
|
|
|
|
if (nodeHasInset(node)) {
|
|
|
|
const currentInset = node.getInset();
|
|
|
|
const newInset = Math.min(Math.max(currentInset + change, 0), 500);
|
|
|
|
node.setInset(newInset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$toggleSelection(editor);
|
|
|
|
}
|