1 import {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 $getDecoratorNodesInSelection,
11 $selectionContainsAlignment, getLastSelection
12 } from "../../../utils/selection";
13 import {CommonBlockAlignment} from "../../../nodes/_common";
14 import {nodeHasAlignment} from "../../../utils/nodes";
17 function setAlignmentForSection(editor: LexicalEditor, alignment: CommonBlockAlignment): void {
18 const selection = getLastSelection(editor);
19 const selectionNodes = selection?.getNodes() || [];
20 const decorators = $getDecoratorNodesInSelection(selection);
22 // Handle decorator node selection alignment
23 if (selectionNodes.length === 1 && decorators.length === 1 && nodeHasAlignment(decorators[0])) {
24 decorators[0].setAlignment(alignment);
25 console.log('setting for decorator!');
29 // Handle normal block/range alignment
30 const elements = $getBlockElementNodesInSelection(selection);
31 for (const node of elements) {
32 if (nodeHasAlignment(node)) {
33 node.setAlignment(alignment)
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');