-import {$isElementNode, BaseSelection, LexicalEditor} from "lexical";
+import {$isElementNode, BaseSelection} from "lexical";
import {EditorButtonDefinition} from "../../framework/buttons";
import alignLeftIcon from "@icons/editor/align-left.svg";
import {EditorUiContext} from "../../framework/core";
import {nodeHasAlignment} from "../../../utils/nodes";
-function setAlignmentForSelection(editor: LexicalEditor, alignment: CommonBlockAlignment): void {
- const selection = getLastSelection(editor);
+function setAlignmentForSelection(context: EditorUiContext, alignment: CommonBlockAlignment): void {
+ const selection = getLastSelection(context.editor);
const selectionNodes = selection?.getNodes() || [];
// Handle inline node selection alignment
if (selectionNodes.length === 1 && $isElementNode(selectionNodes[0]) && selectionNodes[0].isInline() && nodeHasAlignment(selectionNodes[0])) {
selectionNodes[0].setAlignment(alignment);
$selectSingleNode(selectionNodes[0]);
- $toggleSelection(editor);
+ context.manager.triggerFutureStateRefresh();
return;
}
// Handle normal block/range alignment
const elements = $getBlockElementNodesInSelection(selection);
- for (const node of elements) {
- if (nodeHasAlignment(node)) {
- node.setAlignment(alignment)
- }
+ const alignmentNodes = elements.filter(n => nodeHasAlignment(n));
+ const allAlreadyAligned = alignmentNodes.every(n => n.getAlignment() === alignment);
+ const newAlignment = allAlreadyAligned ? '' : alignment;
+ for (const node of alignmentNodes) {
+ node.setAlignment(newAlignment);
}
- $toggleSelection(editor);
+
+ context.manager.triggerFutureStateRefresh();
}
function setDirectionForSelection(context: EditorUiContext, direction: 'ltr' | 'rtl'): void {
label: 'Align left',
icon: alignLeftIcon,
action(context: EditorUiContext) {
- context.editor.update(() => setAlignmentForSelection(context.editor, 'left'));
+ context.editor.update(() => setAlignmentForSelection(context, 'left'));
},
isActive(selection: BaseSelection|null) {
return $selectionContainsAlignment(selection, 'left');
label: 'Align center',
icon: alignCenterIcon,
action(context: EditorUiContext) {
- context.editor.update(() => setAlignmentForSelection(context.editor, 'center'));
+ context.editor.update(() => setAlignmentForSelection(context, 'center'));
},
isActive(selection: BaseSelection|null) {
return $selectionContainsAlignment(selection, 'center');
label: 'Align right',
icon: alignRightIcon,
action(context: EditorUiContext) {
- context.editor.update(() => setAlignmentForSelection(context.editor, 'right'));
+ context.editor.update(() => setAlignmentForSelection(context, 'right'));
},
isActive(selection: BaseSelection|null) {
return $selectionContainsAlignment(selection, 'right');
label: 'Align justify',
icon: alignJustifyIcon,
action(context: EditorUiContext) {
- context.editor.update(() => setAlignmentForSelection(context.editor, 'justify'));
+ context.editor.update(() => setAlignmentForSelection(context, 'justify'));
},
isActive(selection: BaseSelection|null) {
return $selectionContainsAlignment(selection, 'justify');