import { Group, Rect, Text, Transformer } from "react-konva"; import { useRef, useEffect } from "react"; import useEditorStore from "../useEditorStore"; export default function TableElement({ element }) { const updateElement = useEditorStore((state) => state.updateElement); const setSelectedId = useEditorStore((state) => state.setSelectedId); const selectedId = useEditorStore((state) => state.selectedId); const groupRef = useRef(); const trRef = useRef(); const isSelected = selectedId === element.id; useEffect(() => { if (isSelected && trRef.current && groupRef.current) { trRef.current.nodes([groupRef.current]); trRef.current.getLayer().batchDraw(); } }, [isSelected]); const visibleColumns = element.columns.filter((col) => col.visible); const totalWidth = visibleColumns.reduce((sum, col) => sum + col.width, 0); return ( <> setSelectedId(element.id)} onDragEnd={(e) => { const snap = (v) => Math.round(v / 10) * 10; updateElement(element.id, { x: snap(e.target.x()), y: snap(e.target.y()), }); }} onTransformEnd={() => { const node = groupRef.current; updateElement(element.id, { rotation: node.rotation(), }); }} > {/* Header Row */} {visibleColumns.map((col, i) => { const xPos = visibleColumns .slice(0, i) .reduce((sum, c) => sum + c.width, 0); return ( ); })} {/* Sample Data Rows */} {element.showSampleData && element.sampleData && element.sampleData.map((row, rowIdx) => ( {visibleColumns.map((col, colIdx) => { const xPos = visibleColumns .slice(0, colIdx) .reduce((sum, c) => sum + c.width, 0); const yPos = element.rowHeight * (rowIdx + 1); return ( ); })} ))} {isSelected && ( { if (newBox.width < 100 || newBox.height < 50) { return oldBox; } return newBox; }} /> )} ); }