brush-select.ts
7.83 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
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
const min = Math.min;
const max = Math.max;
const abs = Math.abs;
const DEFAULT_TRIGGER = 'shift';
const ALLOW_EVENTS = ['drag', 'shift', 'ctrl', 'alt', 'control'];
export function brushSelecct(G6) {
G6.registerBehavior('brush-select', {
getDefaultCfg() {
return {
brushStyle: {
fill: '#EEF6FF',
fillOpacity: 0.4,
stroke: '#DDEEFE',
lineWidth: 1
},
onSelect() {},
onDeselect() {},
selectedState: 'selected',
trigger: DEFAULT_TRIGGER,
includeEdges: true,
selectedEdges: [],
selectedNodes: []
};
},
getEvents() {
let trigger;
// 检测输入是否合法
if (ALLOW_EVENTS.indexOf(this.trigger.toLowerCase()) > -1) {
trigger = this.trigger;
} else {
trigger = DEFAULT_TRIGGER;
console.warn('Behavior brush-select的trigger参数不合法,请输入drag、shift、ctrl或alt');
}
if (trigger === 'drag') {
return {
mousedown: 'onMouseDown',
mousemove: 'onMouseMove',
mouseup: 'onMouseUp',
'canvas:click': 'clearStates'
};
}
return {
mousedown: 'onMouseDown',
mousemove: 'onMouseMove',
mouseup: 'onMouseUp',
'canvas:click': 'clearStates',
keyup: 'onKeyUp',
keydown: 'onKeyDown'
};
},
onMouseDown(e) {
// 按在node上面拖动时候不应该是框选
const {item} = e;
if (item) {
return;
}
if (this.trigger !== 'drag' && !this.keydown) {
return;
}
if (this.selectedNodes && this.selectedNodes.length !== 0) {
this.clearStates();
}
let brush = this.brush;
if (!brush) {
brush = this._createBrush();
}
this.originPoint = {x: e.canvasX, y: e.canvasY};
brush.attr({width: 0, height: 0});
brush.show();
this.dragging = true;
},
onMouseMove(e) {
if (!this.dragging) {
return;
}
if (this.trigger !== 'drag' && !this.keydown) {
return;
}
this._updateBrush(e);
this.graph.paint();
},
onMouseUp(e) {
if (!this.brush && !this.dragging) {
return;
}
if (this.trigger !== 'drag' && !this.keydown) {
return;
}
const graph = this.graph;
const autoPaint = graph.get('autoPaint');
graph.setAutoPaint(false);
this.brush.destroy();
this.brush = null;
this._getSelectedNodes(e);
this.dragging = false;
this.graph.paint();
graph.setAutoPaint(autoPaint);
},
clearStates() {
if (this.keydown) {
return
}
const graph = this.graph;
const autoPaint = graph.get('autoPaint');
graph.setAutoPaint(false);
const selectedState = this.selectedState;
const nodes = graph.findAllByState('node', selectedState);
const edges = graph.findAllByState('edge', selectedState);
nodes.forEach(node => graph.setItemState(node, selectedState, false));
edges.forEach(edge => graph.setItemState(edge, selectedState, false));
this.selectedNodes = [];
this.selectedEdges = [];
this.onDeselect && this.onDeselect(this.selectedNodes, this.selectedEdges);
graph.emit('nodeselectchange', {
targets: {
nodes: [],
edges: []
}, select: false
});
graph.paint();
graph.setAutoPaint(autoPaint);
},
_getSelectedNodes(e) {
const graph = this.graph;
const state = this.selectedState;
const originPoint = this.originPoint;
const p1 = {x: e.x, y: e.y};
const p2 = graph.getPointByCanvas(originPoint.x, originPoint.y);
const left = min(p1.x, p2.x);
const right = max(p1.x, p2.x);
const top = min(p1.y, p2.y);
const bottom = max(p1.y, p2.y);
const selectedNodes = [];
const shouldUpdate = this.shouldUpdate;
const selectedIds = [];
graph.getNodes().forEach(node => {
const bbox = node.getBBox();
if (bbox.centerX >= left
&& bbox.centerX <= right
&& bbox.centerY >= top
&& bbox.centerY <= bottom
) {
if (shouldUpdate(node, 'select')) {
selectedNodes.push(node);
const model = node.getModel();
selectedIds.push(model.id);
graph.setItemState(node, state, true);
}
}
});
const selectedEdges = [];
if (this.includeEdges) {
// 选中边,边的source和target都在选中的节点中时才选中
selectedNodes.forEach(node => {
const edges = node.getEdges();
edges.forEach(edge => {
const model = edge.getModel();
const {source, target} = model;
if (selectedIds.includes(source)
&& selectedIds.includes(target)
&& shouldUpdate(edge, 'select')) {
selectedEdges.push(edge);
graph.setItemState(edge, this.selectedState, true);
}
});
});
}
this.selectedEdges = selectedEdges;
this.selectedNodes = selectedNodes;
this.onSelect && this.onSelect(selectedNodes, selectedEdges);
graph.emit('nodeselectchange', {
targets: {
nodes: selectedNodes,
edges: selectedEdges
}, select: true
});
},
_createBrush() {
const self = this;
const brush = self.graph.get('canvas').addShape('rect', {
attrs: self.brushStyle,
capture: false
});
this.brush = brush;
return brush;
},
_updateBrush(e) {
const originPoint = this.originPoint;
this.brush.attr({
width: abs(e.canvasX - originPoint.x),
height: abs(e.canvasY - originPoint.y),
x: min(e.canvasX, originPoint.x),
y: min(e.canvasY, originPoint.y)
});
},
onKeyDown(e) {
const code = e.key;
// 按住control键时,允许用户设置trigger为ctrl
if (code && (code.toLowerCase() === this.trigger.toLowerCase())
|| code.toLowerCase() === 'control') {
this.keydown = true;
} else {
this.keydown = false;
}
},
onKeyUp() {
if (this.brush) {
// 清除所有选中状态后,设置拖得动状态为false,并清除框选的brush
this.brush.destroy();
this.brush = null;
this.dragging = false;
}
this.keydown = false;
}
})
}