import React, { useRef, useEffect, Fragment, ReactNode } from 'react'; import { Rect, Transformer } from 'react-konva'; import { Html } from 'react-konva-utils'; import { BaseNode } from "./data"; /** * Html盒子组件 * 包装Html组件,以设置组件宽高和旋转 */ interface Props { node: BaseNode, onDragMove?: (id: string, position: { x: number, y: number }) => void; onChange?: (node: BaseNode) => void; // 更新组件配置 isSelected?: boolean; // 是否选中 onSelect?: () => void; // 设置选中 isDesignMode?: boolean; // 是否编辑模式 children: ReactNode; } const Index = (props: Props) => { const { node, onChange, isSelected, onSelect, children, onDragMove, isDesignMode = true } = props; const { position, size, draggable = true, selected = true, rotation } = node; const rectRef = useRef(null); const trRef = useRef(null); useEffect(() => { trRef.current.nodes(isDesignMode && selected && isSelected ? [rectRef.current] : []); // 选中设置可拖拽size }, [isSelected]); // 尺寸拖拽、旋转后执行 const onTransformEnd = () => { const nodeCur = rectRef.current; const scaleX = nodeCur.scaleX(); const scaleY = nodeCur.scaleY(); const rotation = nodeCur.rotation(); nodeCur.scaleX(1); nodeCur.scaleY(1); onChange({ ...node, position: { x: nodeCur.x(), y: nodeCur.y() }, size: { width: Math.max(5, nodeCur.width() * scaleX), height: Math.max(nodeCur.height() * scaleY) }, rotation }); } const handleOnDragMove = (e) => { if (onDragMove) { const nodeCur = e.target; onDragMove(node.id, { x: nodeCur.x(), y: nodeCur.y() }); } } return ( { e.stopPropagation(); onSelect(); } }} >{children} { // limit resize if (Math.abs(newBox.width) < 5 || Math.abs(newBox.height) < 5) { return oldBox; } return newBox; }} /> ); }; export default Index;