mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-12-16 16:23:36 +08:00
e5b6d28bca
Changed from using a decorator to using a helper that watches for image selections to then display a resize helper. Also changes resizer to use a ghost and apply changes on end instead of continuosly during resize.
81 lines
2.9 KiB
TypeScript
81 lines
2.9 KiB
TypeScript
import {$isElementNode, BaseSelection, LexicalEditor} from "lexical";
|
|
import {EditorButtonDefinition} from "../../framework/buttons";
|
|
import alignLeftIcon from "@icons/editor/align-left.svg";
|
|
import {EditorUiContext} from "../../framework/core";
|
|
import alignCenterIcon from "@icons/editor/align-center.svg";
|
|
import alignRightIcon from "@icons/editor/align-right.svg";
|
|
import alignJustifyIcon from "@icons/editor/align-justify.svg";
|
|
import {
|
|
$getBlockElementNodesInSelection,
|
|
$selectionContainsAlignment, $selectSingleNode, $toggleSelection, getLastSelection
|
|
} from "../../../utils/selection";
|
|
import {CommonBlockAlignment} from "../../../nodes/_common";
|
|
import {nodeHasAlignment} from "../../../utils/nodes";
|
|
|
|
|
|
function setAlignmentForSection(editor: LexicalEditor, alignment: CommonBlockAlignment): void {
|
|
const selection = getLastSelection(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);
|
|
return;
|
|
}
|
|
|
|
// Handle normal block/range alignment
|
|
const elements = $getBlockElementNodesInSelection(selection);
|
|
for (const node of elements) {
|
|
if (nodeHasAlignment(node)) {
|
|
node.setAlignment(alignment)
|
|
}
|
|
}
|
|
$toggleSelection(editor);
|
|
}
|
|
|
|
export const alignLeft: EditorButtonDefinition = {
|
|
label: 'Align left',
|
|
icon: alignLeftIcon,
|
|
action(context: EditorUiContext) {
|
|
context.editor.update(() => setAlignmentForSection(context.editor, 'left'));
|
|
},
|
|
isActive(selection: BaseSelection|null) {
|
|
return $selectionContainsAlignment(selection, 'left');
|
|
}
|
|
};
|
|
|
|
export const alignCenter: EditorButtonDefinition = {
|
|
label: 'Align center',
|
|
icon: alignCenterIcon,
|
|
action(context: EditorUiContext) {
|
|
context.editor.update(() => setAlignmentForSection(context.editor, 'center'));
|
|
},
|
|
isActive(selection: BaseSelection|null) {
|
|
return $selectionContainsAlignment(selection, 'center');
|
|
}
|
|
};
|
|
|
|
export const alignRight: EditorButtonDefinition = {
|
|
label: 'Align right',
|
|
icon: alignRightIcon,
|
|
action(context: EditorUiContext) {
|
|
context.editor.update(() => setAlignmentForSection(context.editor, 'right'));
|
|
},
|
|
isActive(selection: BaseSelection|null) {
|
|
return $selectionContainsAlignment(selection, 'right');
|
|
}
|
|
};
|
|
|
|
export const alignJustify: EditorButtonDefinition = {
|
|
label: 'Align justify',
|
|
icon: alignJustifyIcon,
|
|
action(context: EditorUiContext) {
|
|
context.editor.update(() => setAlignmentForSection(context.editor, 'justify'));
|
|
},
|
|
isActive(selection: BaseSelection|null) {
|
|
return $selectionContainsAlignment(selection, 'justify');
|
|
}
|
|
};
|