1 import {$isElementNode, BaseSelection, LexicalEditor} from "lexical";
2 import {EditorButtonDefinition} from "../../framework/buttons";
3 import alignLeftIcon from "@icons/editor/align-left.svg";
4 import {EditorUiContext} from "../../framework/core";
5 import alignCenterIcon from "@icons/editor/align-center.svg";
6 import alignRightIcon from "@icons/editor/align-right.svg";
7 import alignJustifyIcon from "@icons/editor/align-justify.svg";
9 $getBlockElementNodesInSelection,
10 $selectionContainsAlignment, $selectSingleNode, $toggleSelection, getLastSelection
11 } from "../../../utils/selection";
12 import {CommonBlockAlignment} from "../../../nodes/_common";
13 import {nodeHasAlignment} from "../../../utils/nodes";
16 function setAlignmentForSection(editor: LexicalEditor, alignment: CommonBlockAlignment): void {
17 const selection = getLastSelection(editor);
18 const selectionNodes = selection?.getNodes() || [];
20 // Handle inline node selection alignment
21 if (selectionNodes.length === 1 && $isElementNode(selectionNodes[0]) && selectionNodes[0].isInline() && nodeHasAlignment(selectionNodes[0])) {
22 selectionNodes[0].setAlignment(alignment);
23 $selectSingleNode(selectionNodes[0]);
24 $toggleSelection(editor);
28 // Handle normal block/range alignment
29 const elements = $getBlockElementNodesInSelection(selection);
30 for (const node of elements) {
31 if (nodeHasAlignment(node)) {
32 node.setAlignment(alignment)
35 $toggleSelection(editor);
38 export const alignLeft: EditorButtonDefinition = {
41 action(context: EditorUiContext) {
42 context.editor.update(() => setAlignmentForSection(context.editor, 'left'));
44 isActive(selection: BaseSelection|null) {
45 return $selectionContainsAlignment(selection, 'left');
49 export const alignCenter: EditorButtonDefinition = {
50 label: 'Align center',
51 icon: alignCenterIcon,
52 action(context: EditorUiContext) {
53 context.editor.update(() => setAlignmentForSection(context.editor, 'center'));
55 isActive(selection: BaseSelection|null) {
56 return $selectionContainsAlignment(selection, 'center');
60 export const alignRight: EditorButtonDefinition = {
63 action(context: EditorUiContext) {
64 context.editor.update(() => setAlignmentForSection(context.editor, 'right'));
66 isActive(selection: BaseSelection|null) {
67 return $selectionContainsAlignment(selection, 'right');
71 export const alignJustify: EditorButtonDefinition = {
72 label: 'Align justify',
73 icon: alignJustifyIcon,
74 action(context: EditorUiContext) {
75 context.editor.update(() => setAlignmentForSection(context.editor, 'justify'));
77 isActive(selection: BaseSelection|null) {
78 return $selectionContainsAlignment(selection, 'justify');