import { CSSProperties } from "react"; import { CellElement } from "./attributes"; export type ElementAlignment = "left" | "center" | "right"; export const DEFAULT_ELEMENT_ALIGNMENT: ElementAlignment = "left"; function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null; } export function isElementAlignment(value: unknown): value is ElementAlignment { return value === "left" || value === "center" || value === "right"; } // Element BLOCKS keep their alignment in `align`, except the icon, which // keeps it in `styles.align` (as its renderer reads it). const ICON_BLOCK_NAME = "tableberg/icon"; interface ElementBlockLike { name?: string; attributes?: Record; } /** An element block's alignment; one never set still has the default. */ export function getElementBlockAlignment( block: ElementBlockLike | null | undefined ): ElementAlignment { const attrs = block?.attributes ?? {}; const styles = isRecord(attrs.styles) ? attrs.styles : {}; const align = block?.name === ICON_BLOCK_NAME ? styles.align : attrs.align; return isElementAlignment(align) ? align : DEFAULT_ELEMENT_ALIGNMENT; } /** The attributes that set an element block's alignment. */ export function elementBlockAlignmentAttrs( block: ElementBlockLike | null | undefined, alignment: ElementAlignment ): Record { if (block?.name === ICON_BLOCK_NAME) { const styles = isRecord(block.attributes?.styles) ? block.attributes.styles : {}; return { styles: { ...styles, align: alignment } }; } return { align: alignment }; } export function elementAlignmentToJustifyContent( alignment: ElementAlignment ): CSSProperties["justifyContent"] { if (alignment === "center") { return "center"; } if (alignment === "right") { return "flex-end"; } return "flex-start"; } export function getElementAlignment(element: CellElement): ElementAlignment { const elementName = element.name as string; const attrs: Record = isRecord(element.attributes) ? element.attributes : {}; if (elementName === "icon") { const rawStyles = attrs["styles"]; const styles = isRecord(rawStyles) ? rawStyles : {}; return isElementAlignment(styles.align) ? styles.align : DEFAULT_ELEMENT_ALIGNMENT; } return isElementAlignment(attrs.align) ? attrs.align : DEFAULT_ELEMENT_ALIGNMENT; } export function setElementAlignment( element: CellElement, alignment: ElementAlignment ): CellElement { const elementName = element.name as string; const attrs = isRecord(element.attributes) ? { ...element.attributes } : ({} as Record); if (elementName === "icon") { const rawStyles = attrs["styles"]; const styles = isRecord(rawStyles) ? { ...rawStyles } : ({} as Record); return { ...element, attributes: { ...attrs, styles: { ...styles, align: alignment, }, }, } as CellElement; } return { ...element, attributes: { ...attrs, align: alignment, }, } as CellElement; } export function getUniformElementsAlignment( elements: CellElement[] ): ElementAlignment | undefined { if (elements.length === 0) { return DEFAULT_ELEMENT_ALIGNMENT; } const first = getElementAlignment(elements[0]); for (let i = 1; i < elements.length; i++) { if (getElementAlignment(elements[i]) !== first) { return undefined; } } return first; }