1 import {$isListNode, ListNode, ListType} from "@lexical/list";
2 import {EditorButtonDefinition} from "../../framework/buttons";
3 import {EditorUiContext} from "../../framework/core";
9 import listBulletIcon from "@icons/editor/list-bullet.svg";
10 import listNumberedIcon from "@icons/editor/list-numbered.svg";
11 import listCheckIcon from "@icons/editor/list-check.svg";
12 import indentIncreaseIcon from "@icons/editor/indent-increase.svg";
13 import indentDecreaseIcon from "@icons/editor/indent-decrease.svg";
15 $getBlockElementNodesInSelection,
16 $selectionContainsNodeType,
19 } from "../../../utils/selection";
20 import {toggleSelectionAsList} from "../../../utils/formats";
21 import {nodeHasInset} from "../../../utils/nodes";
24 function buildListButton(label: string, type: ListType, icon: string): EditorButtonDefinition {
28 action(context: EditorUiContext) {
29 toggleSelectionAsList(context.editor, type);
31 isActive(selection: BaseSelection|null): boolean {
32 return $selectionContainsNodeType(selection, (node: LexicalNode | null | undefined): boolean => {
33 return $isListNode(node) && (node as ListNode).getListType() === type;
39 export const bulletList: EditorButtonDefinition = buildListButton('Bullet list', 'bullet', listBulletIcon);
40 export const numberList: EditorButtonDefinition = buildListButton('Numbered list', 'number', listNumberedIcon);
41 export const taskList: EditorButtonDefinition = buildListButton('Task list', 'check', listCheckIcon);
44 function setInsetForSelection(editor: LexicalEditor, change: number): void {
45 const selection = getLastSelection(editor);
47 const elements = $getBlockElementNodesInSelection(selection);
48 for (const node of elements) {
49 if (nodeHasInset(node)) {
50 const currentInset = node.getInset();
51 const newInset = Math.min(Math.max(currentInset + change, 0), 500);
52 node.setInset(newInset)
56 $toggleSelection(editor);
59 export const indentIncrease: EditorButtonDefinition = {
60 label: 'Increase indent',
61 icon: indentIncreaseIcon,
62 action(context: EditorUiContext) {
63 context.editor.update(() => {
64 setInsetForSelection(context.editor, 40);
72 export const indentDecrease: EditorButtonDefinition = {
73 label: 'Decrease indent',
74 icon: indentDecreaseIcon,
75 action(context: EditorUiContext) {
76 context.editor.update(() => {
77 setInsetForSelection(context.editor, -40);