wujingjing
2025-04-16 692c19940b25bedd5b3d0e2cf2b8e73fe440f5c6
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import G6 from '@antv/g6'
 
export default (g6: typeof G6) => {
  g6.registerBehavior('drag-shadow-node', {
    getDefaultCfg() {
      return {
        isGragging: false,
        sourceAnchorIndex: 0,
        // 记录当前拖拽模式(拖拽目标可能是节点也可能是锚点)
        dragTarget: 'node',
        dragStartNode: {},
        distance: [] // 鼠标距离节点中心位置的距离
      }
    },
    getEvents() {
      return {
        'node:mousedown': 'onMousedown',
        'node:mouseup': 'onMouseup',
        'node:dragstart': 'onDragStart',
        'node:drag': 'onDrag',
        'node:dragend': 'onDragEnd',
        'node:drop': 'onDrop'
      }
    },
    shouldBegin(e) {
      return true
    },
    // 鼠标按下显示锚点光圈
    onMousedown(e) {
      if (!this.shouldBegin(e)) return
      this._clearSelected(e)
      if (e.target.cfg.isAnchor) {
        // 拖拽锚点
        this.dragTarget = 'anchor'
        this.dragStartNode = {
          ...e.item._cfg,
          anchorIndex: e.target.cfg.index
        }
        const nodes = this.graph.findAll('node', (node) => node)
 
        nodes.forEach((node) => {
          node.setState('anchorActived', true)
        })
      }
      this.graph.emit('on-node-mousedown', e)
    },
    onMouseup(e) {
      if (!this.shouldBegin(e)) return
      if (this.dragTarget === 'anchor') {
        const nodes = this.graph.findAll('node', (node) => node)
 
        nodes.forEach((node) => {
          node.clearStates('anchorActived')
        })
      }
      this.graph.emit('on-node-mouseup', e)
    },
    // 拖拽开始
    onDragStart(e) {
      if (!this.shouldBegin(e)) return
      const group = e.item.getContainer()
 
      this.isGragging = true
      this.origin = {
        x: e.x,
        y: e.y
      }
      if (e.target.get('isAnchor')) {
        // 拖拽锚点, 记录当前点击的锚点 index
        this.sourceAnchorIndex = e.target.get('index')
      } else if (group.getFirst().cfg.xShapeNode) {
        // 拖拽自定义节点
        e.item.toFront()
        this.dragTarget = 'node'
        this._nodeOnDragStart(e, group)
      }
      this.graph.emit('on-node-dragstart', e)
    },
    // 拖拽中
    onDrag(e) {
      if (!this.shouldBegin(e)) return
      if (this.isGragging) {
        const group = e.item.getContainer()
 
        if (this.dragTarget === 'node' && group.getFirst().cfg.xShapeNode) {
          this._nodeOnDrag(e, e.item.getContainer())
        }
        this.graph.emit('on-node-drag', e)
      }
    },
    // 拖拽结束
    onDragEnd(e) {
      if (!this.shouldBegin(e)) return
      const group = e.item.getContainer()
 
      this.isGragging = false
      if (this.dragTarget === 'anchor') {
        const nodes = this.graph.findAll('node', (node) => node)
 
        nodes.forEach((node) => {
          node.clearStates('anchorActived')
        })
      } else if (this.dragTarget === 'node' && group.getFirst().cfg.xShapeNode) {
        this._nodeOnDragEnd(e, group)
      }
      this.graph.emit('on-node-dragend', e)
    },
    // 锚点拖拽结束添加边
    onDrop(e) {
      if (!this.shouldBegin(e)) return
      // e.item 当前拖拽节点 | e.target 当前释放节点
      if (
        this.dragStartNode.id &&
        e.target.cfg.isAnchor &&
        this.dragStartNode.id !== e.target.cfg.nodeId
      ) {
        const sourceNode = this.dragStartNode.group.get('item')
        const { singleEdge } = sourceNode.getModel() // 同个source和同个target只能有1条线
        const targetAnchorIndex = e.target.get('index')
        const edges = sourceNode.getOutEdges()
 
        const hasLinked = edges.find((edge) => {
          // sourceAnchorIndex === targetAnchorIndex, edge.source.id === source.id, edget.target.id === target.id
          if (
            (edge.get('source').get('id') === sourceNode.get('id') &&
              edge.get('target').get('id') === e.target.cfg.nodeId &&
              edge.get('sourceAnchorIndex') === this.sourceAnchorIndex &&
              edge.get('targetAnchorIndex') === targetAnchorIndex) ||
            (singleEdge && edge.get('target').get('id') === e.target.cfg.nodeId)
          ) {
            return true
          }
        })
 
        if (!hasLinked) {
          this.graph.emit('before-edge-add', {
            source: sourceNode,
            target: e.item.getContainer().get('item'),
            sourceAnchor: this.dragStartNode.anchorIndex,
            targetAnchor: e.target.cfg.index
          })
        }
      }
      this.graph.emit('on-node-drop', e)
    },
 
    /**
     * @description 判断当前画布模式是否启用了内置的 drag-node, 因为有冲突
     */
    _dragNodeModeCheck() {
      const currentMode = this.graph.get('modes')[this.graph.getCurrentMode()]
 
      if (currentMode.includes('drag-node')) {
        console.warn(
          'Behavior drag-shadow-node 与内置 Behavior drag-node 在行为上有冲突, 请考虑改用 setMode 来分开两种错误, 或在以上两种行为的入参 shouldBegin 方法中添加逻辑处理来避免冲突'
        )
        return true
      }
      return false
    },
 
    /**
     * @description 节点拖拽开始事件
     */
    _nodeOnDragStart(e, group) {
      this._dragNodeModeCheck()
      this._addShadowNode(e, group)
    },
 
    /**
     * @description 添加虚拟节点
     */
    _addShadowNode(e, group) {
      const item = group.get('item')
      const model = item.get('model')
      const { radius } = item.get('originStyle')
      const currentShape = item.get('currentShape')
      const { width, height, centerX, centerY } = item.getBBox()
      const shapes = item.get('shapeFactory')[currentShape]
 
      let { shapeType } = shapes || {},
        attrs = {
          fillOpacity: 0.1,
          fill: '#1890FF',
          stroke: '#1890FF',
          cursor: 'move',
          lineDash: [4, 4],
          width,
          height,
          x: model.x,
          y: model.y
        } as any
 
      switch (shapeType) {
        case 'circle':
          this.distance = [e.x - centerX, e.y - centerY]
          attrs = {
            ...attrs,
            r: width / 2
          }
          break
        case 'rect':
          this.distance = [e.x - centerX + width / 2, e.y - centerY + height / 2]
          attrs = {
            ...attrs,
            x: -width / 2,
            y: -height / 2,
            r: width / 2
          }
          if (radius) attrs.radius = radius
          break
        case 'ellipse':
          this.distance = [e.x - centerX, e.y - centerY]
          attrs = {
            ...attrs,
            rx: width / 2,
            ry: height / 2
          }
          break
        case 'path':
          this.distance = [e.x - centerX, e.y - centerY]
          attrs.path = item.get('keyShape').attrs.path
          attrs.size = [width, height]
          break
        case 'modelRect':
          this.distance = [e.x - centerX + width / 2, e.y - centerY + height / 2]
          attrs = {
            ...attrs,
            x: -width / 2,
            y: -height / 2,
            r: width / 2
          }
          shapeType = 'rect'
          break
        default:
          shapeType = 'circle'
          break
      }
 
      const shadowNode = group.addShape(shapeType, {
        className: 'shadow-node',
        attrs
      })
 
      shadowNode.toFront()
    },
 
    /**
     * @description 节点拖拽事件
     */
    _nodeOnDrag(e, group) {
      // 记录鼠标拖拽时与图形中心点坐标的距离
      const item = group.get('item')
      const pathAttrs = group.getFirst()
      const { width, height, centerX, centerY } = item.getBBox()
      const shadowNode = pathAttrs.cfg.xShapeNode ? group.$getItem('shadow-node') : null
      const shapes = item.get('shapeFactory')[item.get('currentShape')]
      const { shapeType } = shapes || {}
 
      if (!shadowNode) {
        return console.warn('暂未支持拖拽内置节点')
      }
      if (shapeType === 'path') {
        const { type, direction } = pathAttrs.get('attrs')
        const dx = e.x - centerX - this.distance[0]
        const dy = e.y - centerY - this.distance[1]
        const path = [['Z']] as any[]
 
        switch (type) {
          case 'diamond-node':
            path.unshift(
              ['M', dx, dy - height / 2], // 上顶点
              ['L', dx + width / 2, dy], // 右侧顶点
              ['L', dx, dy + height / 2], // 下顶点
              ['L', dx - width / 2, dy] // 左侧顶点
            )
            break
          case 'triangle-node':
            path.unshift(
              ['L', dx + width / 2, dy], // 右侧顶点
              ['L', dx - width / 2, dy] // 左侧顶点
            )
            if (direction === 'up') {
              path.unshift(
                ['M', dx, dy - height] // 上顶点
              )
            } else {
              path.unshift(
                ['M', dx, dy + height] // 下顶点
              )
            }
            break
        }
 
        shadowNode.attr({
          path
        })
      } else {
        shadowNode.attr({
          x: e.x - centerX - this.distance[0],
          y: e.y - centerY - this.distance[1]
        })
      }
 
      shadowNode.toFront()
    },
 
    /**
     * @description 节点拖拽结束事件
     */
    _nodeOnDragEnd(e, group) {
      const { graph } = this
      const model = e.item.getModel()
      const shadowNode = group.getFirst().cfg.xShapeNode ? group.$getItem('shadow-node') : null
 
      if (shadowNode) {
        const x = e.x - this.origin.x + model.x
        const y = e.y - this.origin.y + model.y
        const pos = {
          x,
          y
        }
 
        shadowNode.remove()
 
        if (!this._dragNodeModeCheck()) {
          // 如果当前模式中没有使用内置的 drag-node 则让画布更新节点位置
          graph.updateItem(e.item, pos)
        }
      }
    },
    // 清空已选的边
    _clearSelected(e) {
      const selectedEdges = this.graph.findAllByState('edge', 'edgeState:selected')
 
      selectedEdges.forEach((edge) => {
        edge.clearStates(['edgeState:selected', 'edgeState:hover'])
      })
    }
  })
}