schangxiang@126.com
2025-05-07 cace264ad9d86a7831099810b079da1141957add
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
342
343
// @ts-nocheck
import { DagreLayout, DagreLayoutOptions } from '@antv/layout'
import * as d3 from 'd3-hierarchy'
export default class Dagre {
  static pluginName = 'dagre'
  lf: any
  option: DagreLayoutOptions
  nodeMap: Record<string, any>
  checkHasSourceNode(node, sourceId, Name) {
    let current = node
    let check = false
    while (current && current?.parent?.data?.id !== sourceId) {
      current = current?.parent
      // console.log(
      //   current?.parent,
      //   current?.data?.name,
      //   sourceId,
      //   Name,
      //   '---===='
      // )
      if (current?.parent === sourceId) {
        check = true
      }
    }
    return check
  }
  convertToTree(nodes, edges) {
    this.nodeMap = {}
    // 创建一个空对象,用于将节点ID映射到其自身对象
    const nodeMap = {}
 
    // 遍历节点列表,将每个节点ID映射到其自身对象,并初始化children属性为空数组
    nodes.forEach((node) => {
      nodeMap[node.id] = { ...node, children: [] }
    })
 
    // 遍历边列表,将每个边的源节点连接到目标节点
    edges.forEach((edge) => {
      const sourceNode = nodeMap[edge.source]
      const targetNode = nodeMap[edge.target]
      sourceNode.children.push(targetNode)
    })
    const rootId = nodes.find((node) => node.type === 'Activity')?.id
    const treeData = nodeMap[rootId]
    let treemap = d3.tree().size([1066, 1145])
    const root = d3.hierarchy(treeData, function (d) {
      return d.children
    })
    treemap(root)
    root.each((d) => {
      this.nodeMap[d.data.id] = d
    })
    // 返回根节点对象
    // return this.nodeMap
  }
  render(lf: any) {
    this.lf = lf
  }
  getBytesLength(word: string): number {
    if (!word) {
      return 0
    }
    let totalLength = 0
    for (let i = 0; i < word.length; i++) {
      const c = word.charCodeAt(i)
      if (word.match(/[A-Z]/)) {
        totalLength += 1.5
      } else if ((c >= 0x0001 && c <= 0x007e) || (c >= 0xff60 && c <= 0xff9f)) {
        totalLength += 1
      } else {
        totalLength += 2
      }
    }
    return totalLength
  }
  /**
   * option: {
   *   rankdir: "TB", // layout 方向, 可选 TB, BT, LR, RL
   *   align: undefined, // 节点对齐方式,可选 UL, UR, DL, DR
   *   nodeSize: undefined, // 节点大小
   *   nodesepFunc: undefined, // 节点水平间距(px)
   *   ranksepFunc: undefined, // 每一层节点之间间距
   *   nodesep: 40, // 节点水平间距(px) 注意:如果有grid,需要保证nodesep为grid的偶数倍
   *   ranksep: 40, // 每一层节点之间间距 注意:如果有grid,需要保证ranksep为grid的偶数倍
   *   controlPoints: false, // 是否保留布局连线的控制点
   *   radial: false, // 是否基于 dagre 进行辐射布局
   *   focusNode: null, // radial 为 true 时生效,关注的节点
   * };
   */
  layout(option = {}) {
    const { nodes, edges, gridSize } = this.lf.graphModel
    // 为了保证生成的节点在girdSize上,需要处理一下。
    let nodesep = 40
    let ranksep = 40
    if (gridSize > 20) {
      nodesep = gridSize * 2
      ranksep = gridSize * 2
    }
    this.option = {
      type: 'dagre',
      rankdir: 'TB',
      // align: 'UL',
      // align: 'UR',
      preventOverlap: true,
      begin: [120, 120],
      controlPoints: true,
      // nodesepFunc: (node) => {
      //   return node
      // },
      ...option,
    }
    const layoutInstance = new DagreLayout(this.option)
    const newNodes = nodes.map((node) => ({
      id: node.id,
      size: {
        width: node.width,
        height: node.height,
      },
      model: node,
      type: node.type,
      name: node.properties.Name,
    }))
    const newEdges = edges.map((edge) => ({
      source: edge.sourceNodeId,
      target: edge.targetNodeId,
      model: edge,
      type: edge.type,
    }))
    const layoutData = layoutInstance.layout({
      nodes: newNodes,
      edges: newEdges,
    })
    this.convertToTree(newNodes, newEdges)
    const newGraphData = {
      nodes: [],
      edges: [],
    }
    layoutData.nodes.forEach((node) => {
      // @ts-ignore: pass node data
      const { model } = node
      const data = model.getData()
      // @ts-ignore: pass node data
      data.x = node.x
      // @ts-ignore: pass node data
      data.y = node.y
 
      if (data.text) {
        data.text = {
          x: node.x + 10,
          y: node.y - this.getBytesLength(data.text) * 6 - 10,
          value: data.text.value,
        }
      }
      newGraphData.nodes.push(data)
    })
    layoutData.edges.forEach((edge) => {
      // @ts-ignore: pass edge data
      const { model } = edge
      const data = model.getData()
      data.pointsList = this.calcPointsList(model, newGraphData.nodes)
      if (data.pointsList) {
        const first = data.pointsList[0]
        const last = data.pointsList[data.pointsList.length - 1]
        data.startPoint = { x: first.x, y: first.y }
        data.endPoint = { x: last.x, y: last.y }
        if (data.text && data.text.value) {
          data.text = {
            x: last.x,
            y: last.y - 80,
            value: data.text.value,
          }
        }
      } else {
        data.startPoint = undefined
        data.endPoint = undefined
        if (data.text && data.text.value) {
          data.text = data.text.value
        }
      }
      newGraphData.edges.push(data)
    })
    this.lf.render(newGraphData)
  }
  pointFilter(points) {
    const allPoints = points
    let i = 1
    while (i < allPoints.length - 1) {
      const pre = allPoints[i - 1]
      const current = allPoints[i]
      const next = allPoints[i + 1]
      if (
        (pre.x === current.x && current.x === next.x) ||
        (pre.y === current.y && current.y === next.y)
      ) {
        allPoints.splice(i, 1)
      } else {
        i++
      }
    }
    return allPoints
  }
  calcPointsList(model, nodes) {
    // 在节点确认从左向右后,通过计算来保证节点连线清晰。
    // TODO: 避障
    const pointsList = []
    if (this.option.rankdir === 'TB') {
      const sourceNodeModel = this.lf.getNodeModelById(model.sourceNodeId)
      const targetNodeModel = this.lf.getNodeModelById(model.targetNodeId)
      const newSourceNodeData = nodes.find(
        (node) => node.id === model.sourceNodeId
      )
      const newTargetNodeData = nodes.find(
        (node) => node.id === model.targetNodeId
      )
      if (newSourceNodeData.x < newTargetNodeData.x) {
        pointsList.push({
          x: newSourceNodeData.x + sourceNodeModel.width / 2,
          y: newSourceNodeData.y,
        })
        pointsList.push({
          x:
            newSourceNodeData.x +
            sourceNodeModel.width / 2 +
            (model.offset || 50),
          y: newSourceNodeData.y,
        })
        pointsList.push({
          x:
            newSourceNodeData.x +
            sourceNodeModel.width / 2 +
            (model.offset || 50),
          y: newTargetNodeData.y,
        })
        pointsList.push({
          x:
            newTargetNodeData.x -
            targetNodeModel.width / 2 +
            (model.offset || 50) -
            10,
          y: newTargetNodeData.y,
        })
        return this.pointFilter(pointsList)
      }
      // 向回连线
      if (newSourceNodeData.x > newTargetNodeData.x) {
        if (newSourceNodeData.y >= newTargetNodeData.y) {
          pointsList.push({
            x: newSourceNodeData.x,
            y: newSourceNodeData.y + sourceNodeModel.height / 2,
          })
          pointsList.push({
            x: newSourceNodeData.x,
            y:
              newSourceNodeData.y +
              sourceNodeModel.height / 2 +
              (model.offset || 50),
          })
          pointsList.push({
            x: newTargetNodeData.x,
            y:
              newSourceNodeData.y +
              sourceNodeModel.height / 2 +
              (model.offset || 50),
          })
          pointsList.push({
            x: newTargetNodeData.x,
            y: newTargetNodeData.y + targetNodeModel.height / 2,
          })
        } else {
          pointsList.push({
            x: newSourceNodeData.x,
            y:
              newSourceNodeData.y -
              sourceNodeModel.height / 2 +
              (model.offset || 50) +
              30,
          })
          pointsList.push({
            x: newSourceNodeData.x,
            y:
              newSourceNodeData.y -
              sourceNodeModel.height / 2 -
              (model.offset || 50) +
              100,
          })
          pointsList.push({
            x: newTargetNodeData.x,
            y:
              newSourceNodeData.y -
              sourceNodeModel.height / 2 -
              (model.offset || 50) +
              100,
          })
          pointsList.push({
            x: newTargetNodeData.x,
            y: newTargetNodeData.y - targetNodeModel.height / 2,
          })
        }
 
        return this.pointFilter(pointsList)
      } else if (newSourceNodeData.x == newTargetNodeData.x) {
        if (newSourceNodeData.y < newTargetNodeData.y) {
          const sourceNodeId = model.sourceNodeId
          const targetNodeId = model.targetNodeId
 
          const sourceNode = this.nodeMap[sourceNodeId]
          const targetNode = this.nodeMap[targetNodeId]
 
          if (
            sourceNode.children?.length > 1 &&
            targetNode.parent.data.id !== sourceNodeId
          ) {
            pointsList.push({
              x: newSourceNodeData.x,
              y: newSourceNodeData.y - sourceNodeModel.height / 2,
            })
            pointsList.push({
              x: newSourceNodeData.x + 200,
              y: newSourceNodeData.y,
            })
            pointsList.push({
              x: newTargetNodeData.x - 200,
              y: newSourceNodeData.y,
            })
            pointsList.push({
              x: newTargetNodeData.x,
              y: newTargetNodeData.y - targetNodeModel.height / 2,
            })
            console.log(targetNode.data.name, targetNode, '====-11')
          }
          //     const checked = this.checkHasSourceNode(
          //       targetNode,
          //       sourceNodeId,
          //       newSourceNodeData.properties.Name
          //     )
          //     console.log(checked, '---===--0')
        }
        return undefined
      }
    }
    return undefined
  }
}