-
Notifications
You must be signed in to change notification settings - Fork 1
/
stage.js
1453 lines (1275 loc) · 32.3 KB
/
stage.js
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import konva from 'konva'
import uniqid from 'uniqid'
var _ = require('lodash')
var stringhash = require('string-hash')
/**
* 编辑面板
*/
class Stage {
constructor(options) {
this.options = {
draggable: false,
canZoom: true,
canWheelZoom: true,
model: {
portName: 'name',
showDataType: true
}
}
_.merge(this.options, options)
this.stage = new konva.Stage(this.options)
this.layers = {}
// 创建图层
this.modelIndex = {}
this.layers.model = new konva.Layer()
this.stage.add(this.layers.model)
// 连线层
this.linkIndex = {}
this.layers.link = new konva.Layer()
this.stage.add(this.layers.link)
// 是否被修改
this.modified = false
// 当前选中连线
this.selectedLine = null
// 临时连线
this.tempLine = this.createTempLine()
this.layers.link.add(this.tempLine)
// 新连线端口
this.linePorts = []
// 当前鼠标位置
this.mousePos = {
x: 0,
y: 0
}
// 是否处于连线状态
this.isLinking = false
// 连线锚点(用于编辑连线)
this.anchors = []
// 是否允许缩放
this.zoom = 1.0
this.minZoom = 0.1
this.maxZoom = 10
this.zoomFactor = 1.25
// 操作列表(用于Undo, Redo)
this.maxAction = 50
this.actions = []
// 当前操作列表游标索引
this.actionIndex = -1
// 鼠标事件
let stage = this
this.stage.on('mousedown', (evt) => {
let shape = evt.target
// 如果点击编辑器空白区,则取消选中状态
if (shape instanceof konva.Stage) {
if (stage.selectedLine) {
let mod = this.selectedLine.getAttr('linkmod')
if (mod) {
stage.snapshot()
stage.selectedLine.setAttr('linkmod', false)
}
stage.hideAnchor()
stage.selectedLine = null
}
// 如果是连线状态,取消连线
if (stage.isLinking) {
stage.tempLine.setAttr('visible', false)
stage.isLinking = false
stage.linePorts = []
}
stage.layers.link.draw()
return
}
// 判断是否为Group
let group = stage.findGroupParent(shape, 'model')
if (group) {
// 判断是否为端口
if (shape.hasName('modelinport') || shape.hasName('modeloutport')) {
} else {
group.startDrag()
}
}
})
this.stage.on('mouseup', (evt) => {
})
this.stage.on('mousemove', (evt) => {
stage.mousePos = stage.stage.getPointerPosition();
if (stage.isLinking) {
// 更新临时连线
stage.updateTempLine()
stage.layers.link.draw()
}
})
this.stage.on('wheel', (evt) => {
if (!(this.options.canZoom && this.options.canWheelZoom)) {
return
}
let speed = 360
let multiple = Math.floor(evt.evt.wheelDelta / speed)
if (multiple === 0) {
return
}
if (multiple < 0) {
// 放大
this.zoom = _.clamp(this.zoom * Math.pow(this.zoomFactor, -multiple), this.minZoom, this.maxZoom)
} else if (multiple > 0) {
this.zoom = _.clamp(this.zoom / Math.pow(this.zoomFactor, multiple), this.minZoom, this.maxZoom)
}
this.stage.scale({x: this.zoom, y: this.zoom})
this.update()
})
this.stage.on('dragstart', (evt) => {
if (stage.isLinking) {
return
}
let target = evt.target
if (target instanceof konva.Group) {
let group = target
}
});
this.stage.on('dragmove', (evt) => {
if (stage.isLinking) {
return
}
let target = evt.target
if (target instanceof konva.Group) {
let group = target
let inports = group.find('.modelinport')
inports.each((shape) => {
let links = shape.getAttr('linkids')
let pos = shape.getAbsolutePosition(stage.stage)
if (links && links.length > 0) {
for (let lid of links) {
let link = stage.layers.link.find('#' + lid)[0]
let oldpt = link.getAttr('points').slice(-2)
let dx = pos.x - oldpt[0]
let dy = pos.y - oldpt[1]
let pts = link.getAttr('points').slice(0, -2)
pts[4] = pts[4] + dx
pts[5] = pts[5] + dy
pts.push(pos.x, pos.y)
link.setAttr('points', pts)
// 更新锚点
if (stage.selectedLine && link === stage.selectedLine) {
stage.anchors[0].setAttr('points', pts)
stage.anchors[2].setAttr('x', stage.anchors[2].attrs.x + dx)
stage.anchors[2].setAttr('y', stage.anchors[2].attrs.y + dy)
}
}
}
})
let outports = group.find('.modeloutport')
outports.each( (shape) => {
let links = shape.getAttr('linkids')
let pos = shape.getAbsolutePosition(stage.stage)
if (links && links.length > 0) {
for (let lid of links) {
let link = stage.layers.link.find('#' + lid)[0]
let oldpt = link.getAttr('points').slice(0, 2)
let dx = pos.x - oldpt[0]
let dy = pos.y - oldpt[1]
let pts = link.getAttr('points').slice(2)
pts[0] = pts[0] + dx
pts[1] = pts[1] + dy
pts.splice(0, 0, pos.x, pos.y)
link.setAttr('points', pts)
// 更新锚点
if (stage.selectedLine && link === stage.selectedLine) {
stage.anchors[0].setAttr('points', pts)
stage.anchors[1].setAttr('x', stage.anchors[1].attrs.x + dx)
stage.anchors[1].setAttr('y', stage.anchors[1].attrs.y + dy)
}
}
}
})
stage.layers.link.batchDraw()
}
})
this.stage.on('dragend', (evt) => {
if (stage.isLinking) {
return
}
let target = evt.target
if (target instanceof konva.Group) {
stage.snapshot()
}
})
}
/**
* 缓冲快照
*/
snapshot(){
if (this.actions.length >= this.maxAction) {
this.actions.splice(0,1)
}
// 一旦添加快照,删除当前undo队列后续缓存
if(this.actionIndex >= 0 && this.actionIndex < this.actions.length - 1){
this.actions.splice(this.actionIndex + 1, this.actions.length - this.actionIndex - 1)
}
let cache = this.saveToJson()
this.actions.push(cache)
this.actionIndex = this.actions.length - 1
}
/**
* 刷新显示
*/
update(){
this.stage.draw()
}
/**
* 查找所在Group对象
* @param shape
* @param type 类型
* @returns {*}
*/
findGroupParent(shape, type) {
let parent = null
// 如果自己满足条件,返回自己
if (shape instanceof konva.Group) {
if ( !type || (type.length > 0 && shape.hasName(type))){
parent = shape
return
}
}
let groups = this.stage.find('Group')
for (let i = 0; i < groups.length; i++) {
if (groups[i] === shape){
continue
}
if (type.length > 0 && !groups[i].hasName(type) ){
continue
}
if (groups[i].isAncestorOf(shape)) {
parent = groups[i]
break
}
}
return parent
}
/**
* 刷新临时连线
*/
updateTempLine() {
if (!this.tempLine) {
return
}
let offset = this.stage.getAbsolutePosition()
let pts = this.tempLine.getAttr('points').slice()
pts[2] = (this.mousePos.x - offset.x) / this.zoom
pts[3] = (this.mousePos.y - offset.y) / this.zoom
let dist = Math.sqrt((pts[2] - pts[0]) * (pts[2] - pts[0]) + (pts[3] - pts[1]) * (pts[3] - pts[1]))
if (dist < 4 ) {
pts[2] = pts[0]
pts[3] = pts[1]
} else {
pts[2] = pts[2] - (pts[2] - pts[0]) * 4.0 / dist
pts[3] = pts[3] - (pts[3] - pts[1]) * 4.0 / dist
}
this.tempLine.setAttr('points', pts)
}
/**
* 创建锚点
* @param x
* @param y
* @returns {konva.Circle}
*/
buildAnchor(x, y) {
const stage = this
let anchor = new konva.Circle({
x: x,
y: y,
radius: 6,
stroke: '#ccc',
fill: '#999',
strokeWidth: 1,
draggable: true
})
anchor.on('mouseover', function () {
document.body.style.cursor = 'pointer'
this.setStrokeWidth(2)
stage.layers.link.draw()
})
anchor.on('mouseout', function () {
document.body.style.cursor = 'default'
this.setStrokeWidth(1)
stage.layers.link.draw()
})
anchor.on('dragmove', function () {
// 更新曲线
let x = this.attrs.x
let y = this.attrs.y
let lid = this.getAttr('linkid')
let type = this.getAttr('linktype')
if (lid) {
let link = stage.layers.link.find('#' + lid)[0]
let pts = link.getAttr('points').slice()
if (type === 'start') {
pts[2] = x
pts[3] = y
} else if (type === 'end') {
pts[4] = x
pts[5] = y
}
link.setAttr('points', pts)
link.setAttr('linkmod', true) // 标记被修改过
stage.anchors[0].setAttr('points', pts)
}
stage.layers.link.draw()
})
stage.layers.link.add(anchor)
stage.anchors.push(anchor)
return anchor
}
/**
* 创建连线锚点
* @param line
*/
buildLineAnchor(line) {
let stage = this
let pts = line.points()
// 辅助线
let anchorline = new konva.Line({
dash: [10, 10, 0, 10],
strokeWidth: 2,
stroke: '#ccc',
lineCap: 'round',
id: 'anchorline',
points: pts.slice()
})
this.anchors.push(anchorline)
this.layers.link.add(anchorline)
// 显示锚点
let a1 = this.buildAnchor(pts[2], pts[3])
a1.setAttr('linkid', line.id())
a1.setAttr('linktype', 'start')
let a2 = this.buildAnchor(pts[4], pts[5])
a2.setAttr('linkid', line.id())
a2.setAttr('linktype', 'end')
// 显示删除按钮
let close = this.buildCloseButton({
x: (pts[2] + pts[4])/ 2,
y: (pts[3] + pts[5])/ 2,
size: 12,
background: '#406b95',
uid: line.id(),
action: function (evt) {
// 删除连线
let group = stage.findGroupParent(evt.target, 'modelclose')
let pid = group.getAttr('pid')
stage.removeLink(pid)
stage.layers.link.draw()
stage.snapshot()
}
})
this.anchors.push(close)
this.layers.link.add(close)
}
/**
*
*/
hideAnchor() {
for(let a of this.anchors){
if(a){
a.destroy()
}
}
this.anchors = []
}
/**
* 创建临时线
*/
createTempLine() {
return new konva.Line({
points: [0, 0, 0, 0],
dash: [10, 5],
stroke: '#333',
strokeWidth: 2,
lineCap: 'round',
lineJoin: 'round',
bezier: false,
id: 'templine',
visible: false
})
}
/**
* 判断是否已经存在连线
* @param start
* @param end
*/
hasLink(start, end){
if (!start || !end){
console.error('blueprint error: Stage.hasLink() param is null')
return false
}
let startlinks = start.getAttr('linkids')
if (!startlinks || startlinks.length === 0) {
return false
}
let endlinks = end.getAttr('linkids')
if (!endlinks || endlinks.length === 0) {
return false
}
// 检测是否有交集
let ids = _.intersection(startlinks, endlinks)
return ids.length > 0
}
/**
* 添加连线
* @param config
* {
* start: konva.Group 起始端口(必须是outport类型)
* end: konva.Group 终止端口(必须是inport类型)
* points: array[number] 连线的坐标点数组(共4个坐标点,连线为贝塞尔曲线,2个顶点 + 2个控制点)
* }
* @returns {konva.Line}
*/
addLink(config) {
const stage = this
let lid = uniqid()
let points = config.points
if (!points || points.length < 8) {
// 计算绝对坐标
let outpos = config.start.getAbsolutePosition(this.stage)
// 计算绝对坐标
let inpos = config.end.getAbsolutePosition(this.stage)
if (outpos.x < inpos.x) {
points = [outpos.x, outpos.y, outpos.x + 100, outpos.y, inpos.x - 100, inpos.y, inpos.x, inpos.y]
} else {
points = [outpos.x, outpos.y, outpos.x + 100, outpos.y + 100, inpos.x - 100, inpos.y + 100, inpos.x, inpos.y]
}
}
let line = new konva.Line({
points: points,
stroke: '#89abd2',
strokeWidth: 2,
lineCap: 'round',
lineJoin: 'round',
bezier: true,
id: lid
})
const bindPort = (port, type) => {
let links = port.getAttr('linkids')
if (!links) {
links = [lid]
} else {
links.push(lid)
}
port.setAttr('linkids', links)
let portDef = port.getAttr('portdef')
let model = this.findGroupParent(port, 'model')
line.setAttr('link' + type, {
mid: model.id(),
port: portDef.name
})
}
bindPort(config.start, 'start')
bindPort(config.end, 'end')
line.on('mouseover', function () {
this.setAttr('stroke', '#ccc')
stage.layers.link.draw()
})
line.on('mouseout', function (evt) {
this.setAttr('stroke', '#89abd2')
stage.layers.link.draw()
});
line.on('mouseup', function (evt) {
if (stage.selectedLine === evt.target) {
return
}
if (stage.selectedLine) {
let mod = stage.selectedLine.getAttr('linkmod')
if (mod) {
stage.snapshot()
stage.selectedLine.setAttr('linkmod', false)
}
stage.hideAnchor()
}
stage.selectedLine = evt.target
let line = evt.target
stage.buildLineAnchor(line)
stage.layers.link.draw()
})
stage.linkIndex[lid] = line
stage.layers.link.add(line)
return line
}
/**
*
* @param opt
*/
addLinkFromJson(opt){
let startModel = this.modelIndex[opt.start.mid]
let start = startModel.outport(opt.start.port)
let endModel = this.modelIndex[opt.end.mid]
let end = endModel.inport(opt.end.port)
return this.addLink({
start: start,
end: end,
points: opt.points
})
}
/**
* 创建close按钮
* @param opt
* {
* uid:
* x:
* y:
* size:
* background:
* action:
* }
*/
buildCloseButton(opt){
// 删除按钮
let close = new konva.Group({
x: opt.x,
y: opt.y,
rotation: 45,
name: 'modelclose'
})
let bg = new konva.Circle({
x: 0,
y: 0,
radius: opt.size / 2,
fill: opt.background ? opt.background : 'transparent',
stroke: opt.background ? opt.background : 'transparent'
})
close.add(bg)
let lineRadius = opt.size / 2 - 2
let hline = new konva.Line({
points: [-lineRadius, 0, lineRadius, 0],
stroke: '#ddd',
strokeWidth: 2,
lineCap: 'round',
lineJoin: 'round'
})
close.add(hline)
let vline = new konva.Line({
points: [0, -lineRadius, 0, lineRadius],
stroke: '#ddd',
strokeWidth: 2,
lineCap: 'round',
lineJoin: 'round'
})
close.add(vline)
close.setAttr('pid', opt.uid)
close.on('mousedown', opt.action)
return close
}
/**
* 创建模型
* @param config
* {
* uid: 模型唯一ID
* x: X坐标
* y: Y坐标
* title: 模型标题
* name: 模型ID名
* ports: [ 端口列表
* {
* orientation: in \ out 端口方向
* external: true 是否暴露给外部
* name: 端口ID
* title: 端口标题
* dataType: 数据类型
* }
* ]
* }
*
* @returns {konva.Group}
*/
addModel(config) {
const stage = this
const cornerRadius = 4
const shadowOffset = 4
const clipOffsetX = 20
const headHeight = 58
const minWidth = 140
const minContentHeight = 60
let cfg = { uid: uniqid() }
_.merge(cfg, config)
// 创建模型
let group = new konva.Group({
x: cfg.x,
y: cfg.y,
draggable: true,
name: 'model',
id: cfg.uid
})
group.on('mouseover', function () {
document.body.style.cursor = 'pointer'
})
group.on('mouseout', function () {
document.body.style.cursor = 'default'
})
group.setAttr('modeldef', cfg)
//头部
let head = new konva.Group({
x:0,
y:0,
name: 'modelhead',
clip: {
x : -clipOffsetX,
y : -cornerRadius,
width : minWidth + clipOffsetX * 2,
height : headHeight + cornerRadius
}
})
group.add(head)
// 背景图形
let headbg = new konva.Rect({
x: 0,
y: 0,
width: minWidth,
height: headHeight + cornerRadius,
fill: '#406b95',
stroke: '#d2e8ff',
strokeWidth: 1,
cornerRadius: cornerRadius,
shadowColor: 'black',
shadowBlur: 10,
shadowOffset: {
x: shadowOffset,
y: shadowOffset
},
shadowOpacity: 0.6,
name: 'modelbg'
})
head.add(headbg)
// 标题
let title = new konva.Text({
x: 14,
y: 12,
text: config.title + (config.version ? ' ' + config.version : ''),
fontSize: 14,
fill: '#fff'
})
head.add(title)
// Name
let name = new konva.Text({
x: 14,
y: 30,
text: config.name,
fontSize: 12,
fill: '#fff'
})
head.add(name)
// 删除按钮
let close = stage.buildCloseButton({
x: minWidth - 10,
y: 10,
size: 14,
uid: cfg.uid,
action: function (evt) {
// 删除当前模型
let group = stage.findGroupParent(evt.target, 'modelclose')
let pid = group.getAttr('pid')
stage.removeModel(pid)
stage.snapshot()
stage.update()
}
})
head.add(close)
// 内容
let content = new konva.Group({
x:0,
y:58,
name: 'modelhead',
clip: {
x : -clipOffsetX,
y : 0,
width : minWidth + clipOffsetX * 2,
height : minContentHeight + clipOffsetX
}
})
group.add(content)
// 背景图形
let contentbg = new konva.Rect({
x: 0,
y: -cornerRadius * 2,
width: minWidth,
height: minContentHeight + cornerRadius,
fill: '#f3faff',
stroke: '#d2e8ff',
strokeWidth: 1,
cornerRadius: cornerRadius,
shadowColor: 'black',
shadowBlur: 10,
shadowOffset: {
x: shadowOffset,
y: shadowOffset
},
shadowOpacity: 0.6,
name: 'modelbg'
})
content.add(contentbg)
// 绑定端口消息
const bindPortEvent = (port) => {
port.on('mouseover', function (evt) {
let shape = evt.target
shape.setAttr('fill', '#ccc')
stage.layers.model.draw()
})
port.on('mouseout', function (evt) {
let shape = evt.target
let fill = shape.getAttr('portfill')
shape.setAttr('fill', fill)
stage.layers.model.draw()
})
port.on('mouseup', function (evt) {
evt.cancelBubble = true
let shape = evt.target
let port = shape.getParent()
if (stage.isLinking) {
if (port === stage.linePorts[0]) {
return
}
// 检测类型
if (port.hasName('modelinport') && stage.linePorts[0].hasName('modelinport')) {
return
}
if (port.hasName('modeloutport') && stage.linePorts[0].hasName('modeloutport')) {
return
}
// 不能是同一个父模型
if( stage.findGroupParent(port, 'model') === stage.findGroupParent(stage.linePorts[0], 'model')){
return
}
let start = null
let end = null
if (port.hasName('modelinport')) {
start = stage.linePorts[0]
end = port
} else {
start = port
end = stage.linePorts[0]
}
// 不能重复添加
if(stage.hasLink(start, end)){
return
}
// 添加连线
stage.addLink({
start:start,
end: end
})
stage.tempLine.setAttr('visible', false)
stage.isLinking = false
stage.linePorts = []
stage.snapshot()
} else {
// 进入连线模式
stage.isLinking = true
stage.linePorts[0] = port
let pos = port.getAbsolutePosition(stage.stage)
let pts = [pos.x, pos.y, pos.x, pos.y]
stage.tempLine.setAttr('points', pts)
stage.tempLine.setAttr('visible', true)
stage.updateTempLine()
}
stage.layers.link.draw()
})
}
// 创建端口
let optionModel = stage.options.model
const createPort = (opt) => {
let port = new konva.Group({
x: 0,
y: 0,
name: opt.orientation === 'in' ? 'modelinport' : 'modeloutport',
})
port.setAttr('portdef', opt)
// 计算端口类型hash
let hashstr = opt.name + opt.datatype + ( opt.version ? opt.version : '')
port.setAttr('hash', stringhash(hashstr))
let portColor = opt.orientation === 'in' ? '#e17e1b' : '#00951b'
let rect = new konva.Rect({
x: 0,
y: 0,
width: 16,
height: 12,
fill: portColor,
stroke: portColor,
strokeWidth: 1
});
rect.setAttr('portfill', portColor)
port.add(rect)
let title = new konva.Text({
x: 0,
y: 0,
text: opt[optionModel.portName],
fontSize: 14,
fill: '#333'
})
port.add(title)
let ext = null
if (opt.external) {
ext = new konva.Wedge({
x: 0,
y: 0,
radius: 6,
angle: 180,
fill: portColor,
stroke: portColor,
strokeWidth: 1,
rotation: opt.orientation === 'in' ? 90 : -90
})
port.add(ext)
}
let datatype = new konva.Text({
x: 0,
y: 0,
text: opt.datatype + ( opt.version ? ' ' + opt.version : ''),
fontSize: 12,
fill: '#333',
visible: optionModel.showDataType
})
port.add(datatype)
// 调整位置
if (opt.orientation === 'in') {
ext && ext.position({
x: -1,
y: 0
})
rect.position({
x: 1,
y: -6
})
title.position({
x: 20,
y: -7
})
datatype && datatype.position({
x: 20,
y: 7
})
} else {
ext && ext.position({
x: 1,
y: 0
})
rect.position({
x: -17,
y: -6
})
title.position({
x: -20 - title.getWidth(),
y: -7
})
datatype.position({
x: -20 - datatype.getWidth(),
y: 7
})
}
// 记录最大宽度
port.setAttr('portwidth', Math.max(title.getWidth(), datatype.getWidth()))
bindPortEvent(rect)
return port
}
// 端口分类
let inports = []
let outports = []
let maxPortWidth = {
inport: 24,
outport: 24
}
if (cfg.ports && cfg.ports.length > 0){
for( let p of cfg.ports) {
let port = createPort(p)
let w = +port.getAttr('portwidth')
content.add(port)
if (p.orientation === 'in') {