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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
| import {Graph, FunctionExt, Shape} from '@antv/x6'
| import { Stencil } from "@antv/x6-plugin-stencil";
| import { Transform } from "@antv/x6-plugin-transform";
| import { Selection } from "@antv/x6-plugin-selection";
| import { Snapline } from "@antv/x6-plugin-snapline";
| import { Keyboard } from "@antv/x6-plugin-keyboard";
| import { Clipboard } from "@antv/x6-plugin-clipboard";
| import { History } from "@antv/x6-plugin-history";
| import { Export } from "@antv/x6-plugin-export";
| import './shape'
| import { customPorts, basicPorts } from './ports'
| import { getImg } from '@/utils/util'
| import graphData from './data/data'
| const insertCss = require('insert-css')
|
| insertCss(`
| @keyframes ant-line {
| to {
| stroke-dashoffset: -1000
| }
| }
| `)
|
|
|
|
| export default class FlowGraph {
| static graph = null
| static stencil = null
|
| /**
| * 初始化方法
| * @param {*} dom 画板容器
| * @param {*} width 容器宽度
| * @param {*} height 容器高度
| * @param {*} flag 默认为true,传入false只实例化画板
| * @returns
| */
| static init(dom, width = 1200, height = 900, flag = true) {
| // 初始化 流程图画板
| this.graph = new Graph({
| background: {
| color: '#e5e5e5' // 设置画布背景颜色
| },
| container: dom,
| width: width,
| height: height,
| autoResize: true,
| grid: {
| size: 10,
| visible: true,
| type: 'doubleMesh',
| args: [
| {
| color: '#cccccc',
| thickness: 1,
| },
| {
| color: '#5F95FF',
| thickness: 1,
| factor: 4,
| },
| ],
| },
| // 开启画布缩放
| mousewheel: {
| enabled: true,
| modifiers: ['ctrl', 'meta'],
| minScale: 0.5,
| maxScale: 2,
| },
| interacting:!flag?false: {
| nodeMovable: true, //节点是否可以被移动。
| edgeMovable: false, //边是否可以被移动。
| edgeLabelMovable: false, //边的标签是否可以被移动。
| arrowheadMovable: false, //边的起始/终止箭头是否可以被移动
| vertexMovable: true, //边的路径点是否可以被移动。
| vertexAddable: true, //是否可以添加边的路径点。
| vertexDeletable: true, //边的路径点是否可以被删除。
| },
| connecting: {
| anchor: 'center',
| connectionPoint: 'anchor',
| allowBlank: true,
| highlight: true,
| snap: true, // 是否自动吸附
| allowMulti: false, // 是否允许在相同的起始节点和终止之间创建多条边
| allowNode: false, // 是否允许边链接到节点(非节点上的链接桩)
| allowBlank: false, // 是否允许连接到空白点
| allowLoop: false, // 是否允许创建循环连线,即边的起始节点和终止节点为同一节点,
| allowEdge: false, // 是否允许边链接到另一个边
| highlight: true, // 拖动边时,是否高亮显示所有可用的连接桩或节点
| connectionPoint: "anchor", // 指定连接点
| anchor: "center", // 指定被连接的节点的锚点
| createEdge() {
| // X6 的 Shape 命名空间中内置 Edge、DoubleEdge、ShadowEdge 三种边
| return new Shape.Edge({
| attrs: {
| line: {
| strokeWidth: 2, //线宽
| stroke: '#5f95ff',//线色
| strokeDasharray: 0, //虚线间隔
| style: {
| animation: 'ant-line 0s infinite linear', //动画 虚线时有效
| },
| sourceMarker: null, //起始点 标记
| targetMarker: {//结束点 标记
| name: "block",//内置箭头
| width: 12,
| height: 8,
| },
| },
| },
| router: {
| name: 'orth',
| args: {
| offset: 32,
| direction: undefined
| }
| },
| zIndex: 0,
| })
| },
| validateConnection({
| sourceView,
| targetView,
| sourceMagnet,
| targetMagnet,
| }) {
| if (sourceView === targetView) {
| return false
| }
| if (!sourceMagnet) {
| return false
| }
| if (!targetMagnet) {
| return false
| }
| return true
| },
| },
| highlighting: {
| magnetAvailable: {
| name: 'stroke',
| args: {
| padding: 4,
| attrs: {
| strokeWidth: 4,
| stroke: 'rgba(223,234,255)',
| },
| },
| },
| },
| // 开启拖拽平移(防止冲突,按下修饰键并点击鼠标才能触发画布拖拽)
| panning: {
| enabled: true,
| modifiers: 'shift',
| },
| embedding: {
| enabled: true,
| findParent({ node }) {
| const bbox = node.getBBox()
| return this.getNodes().filter((node) => {
| // 只有 data.parent 为 true 的节点才是父节点
| const data = node.getData()
| if (data && data.parent) {
| const targetBBox = node.getBBox()
| return bbox.isIntersectWithRect(targetBBox)
| }
| return false
| })
| },
| },
| })
|
| // #region 使用插件
| this.graph
| .use(
| new Transform({
| resizing: true,
| rotating: true,
| })
| )
| .use(
| new Selection({
| enabled: true,
| rubberband: true,
| showNodeSelectionBox: true,
| })
| )
| .use(
| new Snapline({
| enabled: true,
| })
| )
| .use(
| new Keyboard({
| enabled: true,
| })
| )
| .use(
| new Clipboard({
| enabled: true,
| })
| )
| .use(
| new History({
| enabled: true,
| })
| ).use(new Export());;
| // #endregion
| if(!flag) {
| // this.graph.centerContent()
| this.graph.hideGrid() // 返显渲染的时候 隐藏网格
| return this.graph
| }
| this.initStencil()
| // this.initShape()
| // this.initGraphShape()
| this.initEvent()
| return this.graph
| }
| // 初始化根节点
| static initStencil(option) {
| let obj = {
| target: this.graph,
| stencilGraphWidth: 280,
| // search: { rect: true },
| collapsable: true,
| groups: [],
| }
| let newoption = Object.assign(obj,option)
| this.stencil = new Stencil(newoption)
| const stencilContainer = document.querySelector('#stencil')
| stencilContainer?.appendChild(this.stencil.container)
| return this.stencil
| }
| // 初始化具体每个根节点下不同类型节点
| static initShape() {
| const { graph } = this
| // 基础节点
| const r1 = graph.createNode({
| shape: 'flow-chart-rect',
| attrs: {
| body: {
| rx: 24,
| ry: 24,
| },
| text: {
| text: '起始节点',
| },
| },
| })
| const r2 = graph.createNode({
| shape: 'flow-chart-rect',
| attrs: {
| text: {
| text: '流程节点',
| },
| },
| })
| const r3 = graph.createNode({
| shape: 'flow-chart-rect',
| width: 52,
| height: 52,
| angle: 45,
| attrs: {
| 'edit-text': {
| style: {
| transform: 'rotate(-45deg)',
| },
| },
| text: {
| text: '判断节点',
| transform: 'rotate(-45deg)',
| },
| },
| ports: {
| groups: {
| top: {
| position: {
| name: 'top',
| args: {
| dx: -26,
| },
| },
| },
| right: {
| position: {
| name: 'right',
| args: {
| dy: -26,
| },
| },
| },
| bottom: {
| position: {
| name: 'bottom',
| args: {
| dx: 26,
| },
| },
| },
| left: {
| position: {
| name: 'left',
| args: {
| dy: 26,
| },
| },
| },
| },
| },
| })
| const r4 = graph.createNode({
| shape: 'flow-chart-rect',
| width: 70,
| height: 70,
| attrs: {
| body: {
| rx: 35,
| ry: 35,
| },
| text: {
| text: '链接节点',
| },
| },
| })
|
| // 组合节点
| const c1 = graph.createNode({
| shape: 'flow-chart-image-rect',
| })
| const c2 = graph.createNode({
| shape: 'flow-chart-title-rect',
| })
| const c3 = graph.createNode({
| shape: 'flow-chart-animate-text',
| })
| // 节点组
| const g1 = graph.createNode({
| shape: 'groupNode',
| attrs: {
| text: {
| text: 'Group Name',
| },
| },
| data: {
| parent: true,
| },
| })
| // 系统设计图
| const imgs = [
| {
| image: getImg('ldb.png')
| },
| {
| image: getImg('wft1.png')
| },
| {
| image: getImg('wft2.png')
| },
| {
| image: getImg('wft3.png')
| },
| {
| image: getImg('wft4.png')
| },
| {
| image: getImg('wft5.png')
| },
| {
| image: getImg('lqt.png')
| },
| {
| image: getImg('lqb-o.png')
| },
| {
| image: getImg('ldb-o.png')
| },
| {
| image: getImg('bh.png')
| },
| {
| image: getImg('f1.png')
| },
| {
| image: getImg('f2.png')
| },
| {
| image: getImg('f3.png')
| },
| ]
| const imgNodes = imgs.map(item => {
| return graph.createNode({
| // shape: 'flow-chart-image-rect-custom',
| // attrs: {
| // image: {
| // 'xlink:href': item.image,
| // }
| // }
| shape: 'image', //可选值:Rect Circle Ellipse Polygon Polyline Path Image HTML TextBlock BorderedImage EmbeddedImage InscribedImage Cylinder
| imageUrl: item.image,
| attrs: {
| image: {
| // fill: 'yellow',
| },
| },
| width: 52,
| height: 52,
| ports: { ...customPorts }
| })
| })
| this.stencil.load([r1, r2, r3, r4], 'basic')
| this.stencil.load(imgNodes, 'custom-image')
| }
|
| // 根据json渲染节点和边
| static initGraphShape(gd = graphData) {
| this.graph.fromJSON(gd)
| }
|
| // 连接桩显示时机
| static showPorts(ports, show) {
| for (let i = 0, len = ports.length; i < len; i = i + 1) {
| ports[i].style.visibility = show ? 'visible' : 'hidden'
| }
| }
|
| // 事件相关
| static initEvent() {
| const { graph } = this
| const container = document.getElementById('container')
| // 右键编辑文本
| graph.on('node:contextmenu', ({ cell, view }) => {
| // console.log(cell,view.container,436)
|
| const disableEdit = cell.data.disableEdit
| if(disableEdit)return
| const oldText = cell.attr('text/text')
| cell.attr('text/style/display', 'none')
| const elem = view.container.querySelector('.x6-edit-text')
| if (elem) {
| elem.innerText = oldText
| elem.focus()
| }
| const onBlur = () => {
| cell.attr('text/text', elem.innerText)
| cell.attr('text/style/display', 'inline-block')
| cell.attr('text/style/display', 'none')
| }
| if(elem){
| elem.addEventListener('blur', () => {
| onBlur()
| elem.removeEventListener('blur', onBlur)
| })
| }
| })
| // 鼠标移入 显示连接桩
| graph.on('node:mouseenter', FunctionExt.debounce(() => {
| const ports = container.querySelectorAll('.x6-port-body')
| this.showPorts(ports, true)
| }), 500,)
| // 鼠标移出 隐藏连接桩
| graph.on('node:mouseleave', () => {
| const ports = container.querySelectorAll('.x6-port-body')
| this.showPorts(ports, false)
| })
|
| graph.on('node:collapse', ({ node, e }) => {
| e.stopPropagation()
| node.toggleCollapse()
| const collapsed = node.isCollapsed()
| const cells = node.getDescendants()
| cells.forEach(n => {
| if (collapsed) {
| n.hide()
| } else {
| n.show()
| }
| })
| })
| // backspace delete
| graph.bindKey('delete', () => {
| const cells = graph.getSelectedCells()
| if (cells.length) {
| graph.removeCells(cells)
| }
| })
| // 鼠标动态添加/删除小工具。
| graph.on('edge:mouseenter', ({ cell }) => {
| /**
| * EdgeTool
| * vertices 路径点工具,在路径点位置渲染一个小圆点,拖动小圆点修改路径点位置,双击小圆点删除路径点,在边上单击添加路径点。
| * segments 线段工具。在边的每条线段的中心渲染一个工具条,可以拖动工具条调整线段两端的路径点的位置。
| * boundary 根据边的包围盒渲染一个包围边的矩形。注意,该工具仅仅渲染一个矩形,不带任何交互。
| * button 在指定位置处渲染一个按钮,支持自定义按钮的点击交互。
| * button-remove 在指定的位置处,渲染一个删除按钮,点击时删除对应的边。
| * source-arrowhead-和-target-arrowhead 在边的起点或终点渲染一个图形(默认是箭头),拖动该图形来修改边的起点或终点。
| * edge-editor 提供边上文本编辑功能。
| */
| cell.addTools([
| {
| name: 'vertices',
| args: {
| attrs: { fill: '#007acc' },
| // 移动路径点过程中的吸附半径。当路径点与邻近的路径点的某个坐标 (x, y) 距离在半径范围内时,将当前路径点的对应坐标 (x, y) 吸附到邻居路径的路径点。
| snapRadius: 20,
| // 在边上按下鼠标时,是否可以添加新的路径点。
| addable: true,
| // 是否可以通过双击移除路径点。
| removable: true,
| // 是否自动移除冗余的路径点。
| removeRedundancies: true,
| // 是否阻止工具上的鼠标事件冒泡到边视图上。阻止后鼠标与工具交互时将不会触发边的 mousedown、mousemove 和 mouseup 事件。
| stopPropagation: false
| }
| }
| ])
| })
| graph.on('edge:mouseleave', ({ cell }) => {
| cell.removeTools()
| })
| }
| }
|
|