|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | +import LogicFlow from '@logicflow/core'; |
| 3 | +import '@logicflow/core/dist/index.css'; |
| 4 | +import StatusButtons from './statusButton'; |
| 5 | +import { StatusEdge } from './statusEdge'; |
| 6 | +import { HoverEdge } from './hoverEdge'; |
| 7 | +const SilentConfig = { |
| 8 | + stopScrollGraph: true, |
| 9 | + stopMoveGraph: true, |
| 10 | + stopZoomGraph: true, |
| 11 | +}; |
| 12 | +const App: React.FC = () => { |
| 13 | + const containerRef = useRef<HTMLDivElement>(null); |
| 14 | + const [lf, setLf] = useState<LogicFlow | null>(null); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + if (!containerRef.current) return; |
| 18 | + |
| 19 | + const lfInstance = new LogicFlow({ |
| 20 | + container: containerRef.current, |
| 21 | + grid: true, |
| 22 | + ...SilentConfig, |
| 23 | + }); |
| 24 | + lfInstance.register(StatusEdge); |
| 25 | + lfInstance.register(HoverEdge); |
| 26 | + lfInstance.render({ |
| 27 | + nodes: [ |
| 28 | + { |
| 29 | + id: 'n1', |
| 30 | + type: 'rect', |
| 31 | + x: 100, |
| 32 | + y: 200, |
| 33 | + }, |
| 34 | + { |
| 35 | + id: 'n2', |
| 36 | + type: 'rect', |
| 37 | + x: 500, |
| 38 | + y: 200, |
| 39 | + }, |
| 40 | + { |
| 41 | + id: 'n3', |
| 42 | + type: 'rect', |
| 43 | + x: 100, |
| 44 | + y: 300, |
| 45 | + }, |
| 46 | + { |
| 47 | + id: 'n4', |
| 48 | + type: 'rect', |
| 49 | + x: 500, |
| 50 | + y: 300, |
| 51 | + }, |
| 52 | + { |
| 53 | + id: 'n5', |
| 54 | + type: 'rect', |
| 55 | + x: 100, |
| 56 | + y: 400, |
| 57 | + }, |
| 58 | + { |
| 59 | + id: 'n6', |
| 60 | + type: 'rect', |
| 61 | + x: 500, |
| 62 | + y: 400, |
| 63 | + }, |
| 64 | + ], |
| 65 | + edges: [ |
| 66 | + { |
| 67 | + id: 'e1', |
| 68 | + type: 'status-edge', |
| 69 | + sourceNodeId: 'n1', |
| 70 | + targetNodeId: 'n2', |
| 71 | + text: '根据状态渲染的边颜色,先选中这条边', |
| 72 | + properties: { status: 'todo' }, |
| 73 | + }, |
| 74 | + { |
| 75 | + id: 'e2', |
| 76 | + type: 'polyline', |
| 77 | + text: '内置样式边', |
| 78 | + sourceNodeId: 'n3', |
| 79 | + targetNodeId: 'n4', |
| 80 | + }, |
| 81 | + { |
| 82 | + id: 'e3', |
| 83 | + type: 'hover-edge', |
| 84 | + text: '鼠标移入hover变化', |
| 85 | + sourceNodeId: 'n5', |
| 86 | + targetNodeId: 'n6', |
| 87 | + }, |
| 88 | + ], |
| 89 | + }); |
| 90 | + lfInstance.translateCenter(); |
| 91 | + //直接调用model修改style样式 但是不支持 因为属于直接越权操作了 建议还是继承getEdgeStyle方法 |
| 92 | + const edgeModel = lfInstance.graphModel.getEdgeModelById('e2'); |
| 93 | + edgeModel?.setStyles({ |
| 94 | + stroke: '#722ed1', |
| 95 | + strokeWidth: 2, |
| 96 | + strokeDasharray: '5 5', |
| 97 | + }); |
| 98 | + |
| 99 | + setLf(lfInstance); |
| 100 | + }, []); |
| 101 | + lf?.on('edge:mouseenter', ({ data }) => { |
| 102 | + if (data.type !== 'hover-edge') { |
| 103 | + return; |
| 104 | + } |
| 105 | + lf.setProperties(data.id, { |
| 106 | + isHover: true, |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + lf?.on('edge:mouseleave', ({ data }) => { |
| 111 | + if (data.type !== 'hover-edge') { |
| 112 | + return; |
| 113 | + } |
| 114 | + lf.setProperties(data.id, { |
| 115 | + isHover: false, |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + return ( |
| 120 | + <div style={{ padding: 16 }}> |
| 121 | + {lf && <StatusButtons lf={lf} />} |
| 122 | + <div |
| 123 | + ref={containerRef} |
| 124 | + style={{ height: 600, border: '1px solid #eee', marginTop: 12 }} |
| 125 | + /> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +export default App; |
0 commit comments