import React, { useContext, useMemo, useRef, useState } from "react"; import { motion, MotionConfig } from "framer-motion"; import { ConfigContext } from "../Controller/ConfigController"; import { defaultEdgeConfig } from "../config/edgeConfig"; import useCalcEdge from "../hooks/Edge/useCalcEdge"; import { EdgeFrontProps } from "../typings/Edge"; import { BsCursorFill, BsFillEyeFill, BsFillEyeSlashFill, } from "react-icons/bs"; import useIsShowText from "../hooks/Graph/useIsShowText"; import { useGraphBounds } from "../Controller/GraphBoundsController"; function Edge(props: EdgeFrontProps) { const { id, fromNode, toNode, description, fromId, toId, visible, needHighlight, isMoving, } = props; const { config } = useContext(ConfigContext)!; const { edgeConfig } = config; const [opacity, setOpacity] = useState(1); const [isHovered, setIsHovered] = useState(false); const { calcD } = useCalcEdge(); const d = calcD(props); const isActive = needHighlight && !isMoving; const { descriptionColor, descriptionSize, hoveredColor, stroke, strokeWidth, flyLineEffect, } = defaultEdgeConfig; const isShowText = useIsShowText(); const { x1, x2, y1, y2 } = useGraphBounds(); const isInScreen = useMemo(() => { const fromX = fromNode?.position.x || 0; const fromY = fromNode?.position.y || 0; const toX = toNode?.position.x || 0; const toY = toNode?.position.y || 0; if ( (fromX >= x1 && fromX <= x2 && fromY >= y1 && fromY <= y2) || (toX >= x1 && toX <= x2 && toY >= y1 && toY <= y2) ) { return true; } else { return false; } }, [fromNode, toNode, x1, x2, y1, y2]); return ( {fromNode && toNode && visible && ( { setOpacity((value) => (value !== 1 ? 1 : 0.2)); }} > {(edgeConfig?.flyLineEffect || flyLineEffect) === "line" && isActive && ( )} {isShowText && !isHovered && !isMoving && opacity === 1 && ( {edgeConfig?.arrowIcon ? ( edgeConfig.arrowIcon ) : ( )} {description} )} {isShowText && isHovered && !isMoving && opacity === 1 && ( )} {isShowText && opacity !== 1 && !isMoving && ( )} { setIsHovered(true); }} onHoverEnd={() => { setIsHovered(false); }} /> )} ); } export default React.memo(Edge);