import React, { useCallback, useEffect, useState } from 'react'; import ReactFlow, { MiniMap, Controls, Background, useNodesState, useEdgesState, } from 'reactflow'; import 'reactflow/dist/style.css'; import { CustomNode, SHAPES, getNodeStyle } from './MindMapNode'; import { LINE_TYPES, getEdgeStyle } from './MindMapEdge'; import { SPACING, SNAP_GRID, calculateNodePosition, getDescendants } from './MindMapLayout'; import { getNodeType, extractIcon, processChartContent } from './MindMapUtils'; import { NodeEditModal, handleNodeDoubleClick, handleEditSave, handleNodeDragStop } from './MindMapEditor'; function MindMap({ chart }) { const [nodes, setNodes, onNodesChange] = useNodesState([]); const [edges, setEdges, onEdgesChange] = useEdgesState([]); const [error, setError] = useState(''); const [debugInfo, setDebugInfo] = useState(''); const [expandedNodes, setExpandedNodes] = useState(new Set(['node-0'])); const [nodeShapes, setNodeShapes] = useState({}); const [edgeTypes, setEdgeTypes] = useState({}); const [isEditing, setIsEditing] = useState(false); const [editingNode, setEditingNode] = useState(null); const [editValue, setEditValue] = useState(''); const handleNodeClick = (event, node) => { if (event.button === 2) { // Right click event.preventDefault(); const currentShape = nodeShapes[node.id] || SHAPES.RECTANGLE; const shapes = Object.values(SHAPES); const nextShapeIndex = (shapes.indexOf(currentShape) + 1) % shapes.length; setNodeShapes(prev => ({ ...prev, [node.id]: shapes[nextShapeIndex] })); } else { const nodeId = node.id; setExpandedNodes(prev => { const newExpanded = new Set(prev); if (newExpanded.has(nodeId)) { const descendants = getDescendants(nodeId, edges); descendants.forEach(id => newExpanded.delete(id)); } else { newExpanded.add(nodeId); } return newExpanded; }); } }; const handleEdgeContextMenu = (event, edge) => { event.preventDefault(); const currentType = edgeTypes[edge.id] || LINE_TYPES.SMOOTHSTEP; const types = Object.values(LINE_TYPES); const nextTypeIndex = (types.indexOf(currentType) + 1) % types.length; setEdgeTypes(prev => ({ ...prev, [edge.id]: types[nextTypeIndex] })); }; useEffect(() => { if (!chart) return; const { nodes: processedNodes, edges: processedEdges } = processChartContent(chart, nodeShapes, expandedNodes); setNodes(processedNodes); setEdges(processedEdges); }, [chart, nodeShapes, expandedNodes, setNodes, setEdges]); const nodeTypes = { custom: CustomNode, }; return (
{debugInfo}