import React, { Fragment, useRef, useEffect } from 'react'; import { Circle, Transformer } from 'react-konva'; import { theme } from "antd"; import { BaseNode } from "./data"; const { useToken } = theme; interface Props { node: BaseNode; onDragMove?: (e, id: string) => void; isSelected?: boolean; // 是否选中 onSelect?: () => void; // 设置选中 onChange?: (node: BaseNode) => void; // 更新组件配置 style?: { fill: string, stroke: string }; isDesignMode?: boolean; // 是否编辑模式 } // 热点组件 const Index = (props: Props) => { const { node, onDragMove, style, isSelected, onSelect, onChange, isDesignMode = true } = props; const { position, size, draggable = true, selected = true, rotation } = node; const nodeRef = useRef(null); const trRef = useRef(null); const { token } = useToken(); useEffect(() => { trRef.current.nodes(isDesignMode && selected && isSelected ? [nodeRef.current] : []); // 选中设置可拖拽size }, [isSelected]); // 尺寸拖拽、旋转后执行 const onTransformEnd = () => { const nodeCur = nodeRef.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 }); } return ( onDragMove && onDragMove(e, node.id)} onClick={onSelect} onTransformEnd={onTransformEnd} rotation={rotation} /> { // limit resize if (Math.abs(newBox.width) < 5 || Math.abs(newBox.height) < 5) { return oldBox; } return newBox; }} /> ); }; export default Index;