| 1 | "use client"; |
| 2 | |
| 3 | import { PlateElement, type PlateElementProps } from "platejs/react"; |
| 4 | |
| 5 | import { useForceUpdateChildrenOnLengthChange } from "@/hooks/presentation/useForceUpdateChildrenOnLengthChange"; |
| 6 | import { cn } from "@/lib/utils"; |
| 7 | import { type TConnectedCirclesGroupElement } from "../plugins/diagram-components-plugin"; |
| 8 | import { getAlignmentClasses } from "../utils"; |
| 9 | import { |
| 10 | CONNECTED_CIRCLE_GAP_PX, |
| 11 | CONNECTED_CIRCLE_SIZE_PX, |
| 12 | } from "./connected-circles-layout"; |
| 13 | import { getDiagramFitFrameStyle, useDiagramFitScale } from "./diagram-fit"; |
| 14 | |
| 15 | const CONNECTED_CIRCLES_LAYOUT_WIDTH_PX = |
| 16 | CONNECTED_CIRCLE_SIZE_PX * 2 + CONNECTED_CIRCLE_GAP_PX; |
| 17 | const CONNECTED_CIRCLES_MIN_LAYOUT_HEIGHT_PX = |
| 18 | CONNECTED_CIRCLE_SIZE_PX * 2 + CONNECTED_CIRCLE_GAP_PX + 32; |
| 19 | |
| 20 | export default function ConnectedCircles( |
| 21 | props: PlateElementProps<TConnectedCirclesGroupElement>, |
| 22 | ) { |
| 23 | const { alignment = "center" } = props.element; |
| 24 | const { containerRef, fitStyle, frameStyle, layoutRef } = |
| 25 | useDiagramFitScale<HTMLDivElement>( |
| 26 | CONNECTED_CIRCLES_LAYOUT_WIDTH_PX, |
| 27 | CONNECTED_CIRCLES_MIN_LAYOUT_HEIGHT_PX, |
| 28 | ); |
| 29 | |
| 30 | useForceUpdateChildrenOnLengthChange(props.editor, props.element); |
| 31 | |
| 32 | return ( |
| 33 | <PlateElement {...props} className="relative my-4"> |
| 34 | <div |
| 35 | ref={containerRef} |
| 36 | className={cn( |
| 37 | "w-full overflow-visible", |
| 38 | getAlignmentClasses(alignment), |
| 39 | )} |
| 40 | > |
| 41 | <div |
| 42 | style={{ |
| 43 | ...frameStyle, |
| 44 | ...getDiagramFitFrameStyle(alignment), |
| 45 | }} |
| 46 | > |
| 47 | <div |
| 48 | ref={layoutRef} |
| 49 | className="relative inline-grid place-items-center gap-1 overflow-visible py-4" |
| 50 | style={{ |
| 51 | ...fitStyle, |
| 52 | alignContent: "center", |
| 53 | gridAutoRows: CONNECTED_CIRCLE_SIZE_PX, |
| 54 | gridTemplateColumns: `repeat(2, ${CONNECTED_CIRCLE_SIZE_PX}px)`, |
| 55 | minHeight: CONNECTED_CIRCLES_MIN_LAYOUT_HEIGHT_PX, |
| 56 | }} |
| 57 | > |
| 58 | {props.children} |
| 59 | </div> |
| 60 | </div> |
| 61 | </div> |
| 62 | </PlateElement> |
| 63 | ); |
| 64 | } |
| 65 |