luxiaotao1123
2024-03-02 0c009a44f7cd35489a56450d0aa2715b00b23c32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React, { useEffect, useState,useRef } from 'react';
import ReactDOM from 'react-dom';
import { data } from './data';
import G6 from '@antv/g6';
import "./index.css"
import { Modal, Input } from 'antd';
 
export default function() {
  const ref = useRef(null);
  const graphRef = useRef(null);
 
  useEffect(() => {
    if (!graphRef.current) {
      // // 注册自定义行为
      // G6.registerBehavior('drag-line', {
      //   getEvents() {
      //     return {
      //       'edge:mousedown': 'onDragStart',
      //       'canvas:mousemove': 'onDrag',
      //       'canvas:mouseup': 'onDragEnd',
      //     }
      //   },
      //   onDragStart(ev) {
      //     const edge = ev.item;
      //     this.edge = edge;
      //     this.dragging = true;
      //     console.log(ev)
      //   },
      //   onDrag(ev) {
      //     if (!this.dragging) {
      //       return;
      //     }
      //     const { x, y } = ev;
 
      //     // 更新边的 target(终点)
      //     this.edge.update({
      //       target: { x, y },
      //     });
      //   },
      //   onDragEnd() {
      //     this.edge = null;
      //     this.dragging = false;
      //   }
      // });
 
      G6.registerBehavior('click-add-edge', {
        getEvents() {
          return {
            'node:click': 'onClick',
            mousemove: 'onMousemove',
            'edge:click': 'onEdgeClick' // 点击空白处,取消边
          };
        },
        onClick(ev) {
          const node = ev.item;
          const graph = this.graph;
          const point = {
            x: ev.x,
            y: ev.y
          };
          const model = node.getModel();
          if (this.addingEdge && this.edge) {
            graph.updateItem(this.edge, {
              target: model.id
            });
            // graph.setItemState(this.edge, 'selected', true);
            this.edge = null;
            this.addingEdge = false;
          } else {
            this.edge = graph.addItem('edge', {
              source: model.id,
              target: point
            });
            this.addingEdge = true;
          }
        },
        onMousemove(ev) {
          const point = {
            x: ev.x,
            y: ev.y
          };
          if (this.addingEdge && this.edge) {
            this.graph.updateItem(this.edge, {
              target: point
            });
          }
        },
        onEdgeClick(ev) {
          const currentEdge = ev.item;
          // 拖拽过程中,点击会点击到新增的边上
          if (this.addingEdge && this.edge == currentEdge) {
            graph.removeItem(this.edge);
            this.edge = null;
            this.addingEdge = false;
          }
        }
      });
 
 
      graphRef.current = new G6.Graph({
        container: ReactDOM.findDOMNode(ref.current),
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight,
        modes: {
          default: [
            'drag-canvas', 
            'zoom-canvas',
            'drag-node',
            'drag-line',
            'click-add-edge'
          ],
          addEdge: ['click-add-edge', 'click-select']
        },
        layout: {
          type: 'dagre',
          direction: 'LR',
        },
        defaultNode: {
          shape: 'node',
          type: 'rect',
          labelCfg: {
            style: {
              fill: '#000000A6',
              fontSize: 14,
            },
          },
          style: {
            // 仅在 keyShape 上生效
            fill: 'lightblue',
            stroke: '#888',
            lineWidth: 1,
            radius: 7,
          },
          // linkPoints: {
          //   top: true,
          //   bottom: true,
          //   left: true,
          //   right: true,
          //   // ... 四个圆的样式可以在这里指定
          // },
        },
        defaultEdge: {
          shape: 'polyline',
        },
        nodeStateStyles: {
          // 各状态下的样式,平铺的配置项仅在 keyShape 上生效。需要在其他 shape 样式上响应状态变化则写法不同,参见上文提到的 配置状态样式 链接
          hover: {
            fillOpacity: 0.1,
            lineWidth: 1,
          },
        },
      });
    }
 
    const graph = graphRef.current;
 
    graph.data(data);
    graph.render();
 
    graph.setMode("edit")
    
    // 监听鼠标进入节点事件
    graph.on('node:mouseenter', (evt) => {
      const node = evt.item;
      // 激活该节点的 hover 状态
      graph.setItemState(node, 'hover', true);
    });
 
    // 监听鼠标离开节点事件
    graph.on('node:mouseleave', (evt) => {
      const node = evt.item;
      // 关闭该节点的 hover 状态
      graph.setItemState(node, 'hover', false);
    });
 
    graph.on('node:dblclick', (evt) => {
      const node = evt.item;
      const model = node.getModel();
      // 在此处执行显示一个模态框或输入框的操作,将model.id传入用来确定哪个节点,model.label传入用来显示当前节点文字
      showModal(model.id, model.label);
    });
 
    // 添加行为
    graph.on('edge:click', (evt) => {
      const { item } = evt;
  
      // 获取边的模型数据
      const model = item.getModel();
      
      // 切换选中状态
      if (model.style?.stroke === '#f00') {
        // 如果边处于选中状态,则取消选中
        graph.updateItem(item, {
          style: {
            ...model.style,
            stroke: '#000',
          },
        });
      } else {
        // 如果边未被选中,则选中当前边
        graph.updateItem(item, {
          style: {
            ...model.style,
            stroke: '#f00',
          },
        });
      }
    });
  }, []);
 
  const [modalVisible, setModalVisible] = useState(false);
  const [currentNode, setCurrentNode] = useState(null);
  const [currentLabel, setCurrentLabel] = useState('');
 
  const showModal = (id, label) => {
    setCurrentNode(id);        // 设置当前节点
    setCurrentLabel(label);    // 设置当前节点文字
    setModalVisible(true);     // 打开模态框
  };
 
  const updateLabel = (nodeId, newLabel) => {
    if(graphRef.current){
      const node = graphRef.current.findById(nodeId);
      graphRef.current.update(node, {
        label: newLabel,
      });
      graphRef.current.refresh();
    }
  };
 
  const handleOk = () => {
    // 点击确定后更新节点
    updateLabel(currentNode, currentLabel);
    setModalVisible(false);
  };
 
  const handleCancel = () => {
    // 关闭模态框
    setModalVisible(false);
  };
 
  return (
    <>
      <div ref={ref} />  {/* g6图表的容器 */}
      <Modal
        open={modalVisible}
        onOk={handleOk}
        onCancel={handleCancel}
      >
        <div className='modalInput'>
        <Input value={currentLabel} onChange={e => setCurrentLabel(e.target.value)} />
        </div>
      </Modal>
    </>
  );
}