2024-08-20 21:54:53 +08:00
|
|
|
import {$isListNode, ListNode, ListType} from "@lexical/list";
|
2024-07-21 22:11:24 +08:00
|
|
|
import {EditorButtonDefinition} from "../../framework/buttons";
|
|
|
|
import {EditorUiContext} from "../../framework/core";
|
2024-08-20 21:54:53 +08:00
|
|
|
import {BaseSelection, LexicalNode} from "lexical";
|
2024-07-21 22:11:24 +08:00
|
|
|
import listBulletIcon from "@icons/editor/list-bullet.svg";
|
|
|
|
import listNumberedIcon from "@icons/editor/list-numbered.svg";
|
|
|
|
import listCheckIcon from "@icons/editor/list-check.svg";
|
2024-08-04 01:14:01 +08:00
|
|
|
import {$selectionContainsNodeType} from "../../../utils/selection";
|
2024-08-20 21:54:53 +08:00
|
|
|
import {toggleSelectionAsList} from "../../../utils/formats";
|
2024-07-21 22:11:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
function buildListButton(label: string, type: ListType, icon: string): EditorButtonDefinition {
|
|
|
|
return {
|
|
|
|
label,
|
|
|
|
icon,
|
|
|
|
action(context: EditorUiContext) {
|
2024-08-20 21:54:53 +08:00
|
|
|
toggleSelectionAsList(context.editor, type);
|
2024-07-21 22:11:24 +08:00
|
|
|
},
|
|
|
|
isActive(selection: BaseSelection|null): boolean {
|
|
|
|
return $selectionContainsNodeType(selection, (node: LexicalNode | null | undefined): boolean => {
|
|
|
|
return $isListNode(node) && (node as ListNode).getListType() === type;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const bulletList: EditorButtonDefinition = buildListButton('Bullet list', 'bullet', listBulletIcon);
|
|
|
|
export const numberList: EditorButtonDefinition = buildListButton('Numbered list', 'number', listNumberedIcon);
|
|
|
|
export const taskList: EditorButtonDefinition = buildListButton('Task list', 'check', listCheckIcon);
|