| 1 | import { type TColumnElement } from "platejs"; |
| 2 | import { SlateElement, type SlateElementProps } from "platejs/static"; |
| 3 | |
| 4 | function parseColumnWidth(width: unknown): number | null { |
| 5 | if (width === undefined || width === null) return null; |
| 6 | |
| 7 | const parsed = Number.parseFloat(String(width)); |
| 8 | |
| 9 | if (!Number.isFinite(parsed) || parsed <= 0) return null; |
| 10 | |
| 11 | return Math.round(parsed * 100) / 100; |
| 12 | } |
| 13 | |
| 14 | export function ColumnElementStatic(props: SlateElementProps<TColumnElement>) { |
| 15 | const width = parseColumnWidth(props.element.width); |
| 16 | const style = |
| 17 | width === null |
| 18 | ? { flex: "1 1 0", minWidth: 0 } |
| 19 | : { |
| 20 | flex: `0 0 ${width}%`, |
| 21 | maxWidth: `${width}%`, |
| 22 | minWidth: 0, |
| 23 | }; |
| 24 | |
| 25 | return ( |
| 26 | <div className="group/column relative" style={style}> |
| 27 | <SlateElement |
| 28 | className="px-2 pt-2 group-first/column:pl-0 group-last/column:pr-0" |
| 29 | {...props} |
| 30 | > |
| 31 | <div className="relative border border-transparent p-1.5"> |
| 32 | {props.children} |
| 33 | </div> |
| 34 | </SlateElement> |
| 35 | </div> |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | export function ColumnGroupElementStatic(props: SlateElementProps) { |
| 40 | return ( |
| 41 | <SlateElement className="mb-2" {...props}> |
| 42 | <div className="flex w-full rounded">{props.children}</div> |
| 43 | </SlateElement> |
| 44 | ); |
| 45 | } |
| 46 |