import { register } from "@antv/x6-react-shape"; import { EventArgs, Graph, Node, Path } from "@antv/x6"; import { topicData } from "@/config/data"; import { useSizeHook, useShapeProps } from "@/hooks"; import CustomInput from "../CustomInput"; import { useEffect, useState } from "react"; import { PlusOutlined } from "@ant-design/icons"; import { TopicType } from "@/enum"; import { addTopic } from "@/utils/mindMap"; const component = ({ node, graph }: { node: Node; graph: Graph }) => { const { fill, stroke, opacity, label, text, borderSize } = node.getData(); const { size, ref } = useSizeHook(); const { fillContent, strokeColor, strokeWidth, strokeDasharray } = useShapeProps(fill, size, stroke); const [selected, setSelected] = useState(true); const handleSelect = (_args: EventArgs["node:selected"]) => { const cells = graph.getSelectedCells(); setSelected(cells.length === 1 && cells[0].id === node.id); }; useEffect(() => { graph.createTransformWidget(node); graph.select(node); graph.on("node:selected", handleSelect); graph.on("node:unselected", handleSelect); return () => { graph.off("node:selected", handleSelect); graph.off("node:unselected", handleSelect); }; }, []); const handleAddBranch = () => { const data = node.getData(); if( data.type === TopicType.main) { addTopic(node, TopicType.branch); } else { addTopic(node, TopicType.sub); } } return ( <>
{selected && (
)}
); }; // 连接器 Graph.registerConnector( "mindmap", (sourcePoint, targetPoint, routerPoints, options) => { const midX = sourcePoint.x + 10; const midY = sourcePoint.y; const ctrX = (targetPoint.x - midX) / 5 + midX; const ctrY = targetPoint.y; const pathData = ` M ${sourcePoint.x} ${sourcePoint.y} L ${midX} ${midY} Q ${ctrX} ${ctrY} ${targetPoint.x} ${targetPoint.y} `; return options.raw ? Path.parse(pathData) : pathData; }, true ); // 注册思维导图边 Graph.registerEdge( "mindmap-edge", { inherit: "edge", connector: { name: "mindmap", }, attrs: { line: { targetMarker: "", stroke: "#A2B1C3", strokeWidth: 2, }, }, zIndex: 0, }, true ); // 主题 register({ shape: "mind-map-topic", width: 206, height: 70, effect: ["data"], component: component, }); const baseNode = { shape: "mind-map-topic", data: { label: "", ...topicData, }, }; export default baseNode;