drag-edge.ts
5.9 KB
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
import {GraphStyle} from "@/utils/styles";
interface OptionType {
beforeAdd: (model: object, type: string) => Promise<any> | undefined,
afterAdd: (model: object, type: string) => Promise<any> | undefined,
}
export function dragEdge(G6, option: OptionType) {
G6.registerBehavior('drag-edge', {
getDefaultCfg() {
return {
updateEdge: true,
delegate: true,
delegateStyle: {},
dragEdge: false,
};
},
getEvents() {
return {
'anchor:dragstart': 'onDragStart',
'anchor:drag': 'onDrag',
'anchor:dragenter': 'onDragEnter',
'anchor:dragleave': 'onDragLeave',
'anchor:dragend': 'onDragEnd',
};
},
onDragStart(e) {
const node = e.target.getParent().getParent().get('item');
const anchor = e.item
const anchorIndex = anchor.index
const point = node.getAnchorPoints()[anchorIndex];
this.target = e.item;
this.origin = {
x: point.x,
y: point.y,
sourceNode: node,
sourceAnchor: anchorIndex
};
this.graph.getNodes().forEach(node => {
node.showAnchor(this.graph)
node.getContainer().anchorShapes.forEach(a => {
a.get('item').showHotpot()
})
});
this.graph.set('onDragEdge', true);
},
onDrag(e) {
if (!this.origin) {
return;
}
this._updateEdge(this.target, e);
},
onDragEnter(e) {
if (!this.origin) {
return;
}
if (!this.sameNode(e)) {
e.item.setHotspotActived(true);
this.origin.targetNode = e.target.getParent().getParent().get('item');
this.origin.targetAnchor = e.item.get('index');
}
},
onDragLeave(e) {
if (!this.origin) {
return;
}
if (!this.sameNode(e)) {
e.item.setHotspotActived(false);
this.origin.targetNode = null;
this.origin.targetAnchor = null;
}
},
onDragEnd(e) {
if (!this.origin) {
return;
}
const delegateShape = e.item.get('edgeDelegate');
if (delegateShape) {
delegateShape.remove();
this.target.set('edgeDelegate', null);
}
this._updateEdge(this.target, e, true);
this.target = null;
this.origin = null;
this.graph.set('onDragEdge', false);
this.graph.getNodes().forEach(node => {
node.hideAnchor(this.graph)
node.getContainer().anchorShapes.forEach(a => {
a.get('item').hideHotpot()
})
});
},
sameNode(e) {
return e.target.type === 'marker' && e.target.getParent() && e.target.getParent().getParent().get('item').get('id') === this.origin.sourceNode.get('id')
},
_updateEdge(item, e, force) {
const x = e.x;
const y = e.y;
if (this.delegate && !force) {
this._updateEdgeDelegate(item, x, y);
return;
}
this._addEdge(e);
this.graph.paint();
},
_updateEdgeDelegate(item, x, y) {
const self = this;
let edgeShape = item.get('edgeDelegate');
if (!edgeShape) {
const parent = self.graph.get('group');
edgeShape = parent.addShape('line', {
attrs: {
x1: this.origin.x,
y1: this.origin.y,
x2: x,
y2: y,
...GraphStyle.edgeDelegationStyle,
}
});
edgeShape.set('capture', false);
item.set('edgeDelegate', edgeShape);
}
edgeShape.attr({x2: x, y2: y});
this.graph.paint();
},
async _addEdge() {
if (this.origin.targetNode) {
const addModel = {
class: 'flow',
shape: "flow-polyline-round1",
source: this.origin.sourceNode.get('id'),
target: this.origin.targetNode.get('id'),
sourceAnchor: this.origin.sourceAnchor,
targetAnchor: this.origin.targetAnchor,
}
// TAG: 修改连接线颜色 开始/结束 相关的线和连接到抄送节点的颜色是灰色
if (this.origin.sourceNode.get('id') === 'start-node' || this.origin.targetNode.get('model').control ==='cc' || this.origin.targetNode.get('id') === 'end-node') {
addModel.shape = 'flow-polyline-round'
}
if (this.graph.executeCommand) {
this.graph.executeCommand('add', {
type: 'edge',
addModel: addModel
});
} else {
try {
if (!!option.beforeAdd) {
await option.beforeAdd(addModel, 'edge')
}
this.graph.emit('add-edge:before')
this.graph.add('edge', addModel);
this.graph.emit('add-edge:after')
if (!!option.afterAdd) {
await option.afterAdd(addModel, 'edge')
}
} catch (e) {
console.error(e)
}
}
}
}
});
}