1 import {sizeToPixels} from "../../../utils/dom";
2 import {SerializedCommonBlockNode} from "lexical/nodes/CommonBlockNode";
4 export type CommonBlockAlignment = 'left' | 'right' | 'center' | 'justify' | '';
5 const validAlignments: CommonBlockAlignment[] = ['left', 'right', 'center', 'justify'];
7 type EditorNodeDirection = 'ltr' | 'rtl' | null;
9 export interface NodeHasAlignment {
10 readonly __alignment: CommonBlockAlignment;
11 setAlignment(alignment: CommonBlockAlignment): void;
12 getAlignment(): CommonBlockAlignment;
15 export interface NodeHasId {
16 readonly __id: string;
17 setId(id: string): void;
21 export interface NodeHasInset {
22 readonly __inset: number;
23 setInset(inset: number): void;
27 export interface NodeHasDirection {
28 readonly __dir: EditorNodeDirection;
29 setDirection(direction: EditorNodeDirection): void;
30 getDirection(): EditorNodeDirection;
33 export interface CommonBlockInterface extends NodeHasId, NodeHasAlignment, NodeHasInset, NodeHasDirection {}
35 export function extractAlignmentFromElement(element: HTMLElement): CommonBlockAlignment {
36 const textAlignStyle: string = element.style.textAlign || '';
37 if (validAlignments.includes(textAlignStyle as CommonBlockAlignment)) {
38 return textAlignStyle as CommonBlockAlignment;
41 if (element.classList.contains('align-left')) {
43 } else if (element.classList.contains('align-right')) {
45 } else if (element.classList.contains('align-center')) {
47 } else if (element.classList.contains('align-justify')) {
54 export function extractInsetFromElement(element: HTMLElement): number {
55 const elemPadding: string = element.style.paddingLeft || '0';
56 return sizeToPixels(elemPadding);
59 export function extractDirectionFromElement(element: HTMLElement): EditorNodeDirection {
60 const elemDir = (element.dir || '').toLowerCase();
61 if (elemDir === 'rtl' || elemDir === 'ltr') {
68 export function setCommonBlockPropsFromElement(element: HTMLElement, node: CommonBlockInterface): void {
70 node.setId(element.id);
73 node.setAlignment(extractAlignmentFromElement(element));
74 node.setInset(extractInsetFromElement(element));
75 node.setDirection(extractDirectionFromElement(element));
78 export function commonPropertiesDifferent(nodeA: CommonBlockInterface, nodeB: CommonBlockInterface): boolean {
79 return nodeA.__id !== nodeB.__id ||
80 nodeA.__alignment !== nodeB.__alignment ||
81 nodeA.__inset !== nodeB.__inset ||
82 nodeA.__dir !== nodeB.__dir;
85 export function updateElementWithCommonBlockProps(element: HTMLElement, node: CommonBlockInterface): void {
87 element.setAttribute('id', node.__id);
90 if (node.__alignment) {
91 element.classList.add('align-' + node.__alignment);
95 element.style.paddingLeft = `${node.__inset}px`;
99 element.dir = node.__dir;
103 export function deserializeCommonBlockNode(serializedNode: SerializedCommonBlockNode, node: CommonBlockInterface): void {
104 node.setId(serializedNode.id);
105 node.setAlignment(serializedNode.alignment);
106 node.setInset(serializedNode.inset);
107 node.setDirection(serializedNode.direction);
110 export interface NodeHasSize {
111 setHeight(height: number): void;
112 setWidth(width: number): void;