forked from keman5/welabx-g6
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdelete-item.js
More file actions
65 lines (58 loc) · 1.86 KB
/
delete-item.js
File metadata and controls
65 lines (58 loc) · 1.86 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
export default G6 => {
G6.registerBehavior('delete-item', {
getEvents () {
return {
'keydown': 'onKeydown',
};
},
shouldBegin (e) {
return true;
},
onKeydown (e) {
const { graph } = this;
// 实例化时监听了键盘事件, 防止在画布外仍然能监听到画布内的事件
if (graph.cfg.canvas.cfg.el.getAttribute('isFocused') !== 'true') return;
if (!this.shouldBegin(e)) return;
/**
* TODO:
* 删除节点时, 将与该节点连接的后代节点也删除
*/
if (e.keyCode === 8 || e.keyCode === 46) {
const nodes = graph.findAllByState('node', 'nodeState:selected');
if (nodes && nodes.length) {
const $node = nodes[0].getContainer().get('item');
graph.emit('before-node-removed', {
target: $node,
callback (confirm) {
// 内部this已改变
if (confirm) {
graph.remove($node);
graph.set('after-node-selected', []);
// 提交事件
graph.emit('after-node-selected');
graph.emit('after-node-removed', $node);
}
},
});
}
// 删除选中的边
const edges = graph.findAllByState('edge', 'edgeState:selected');
if (edges && edges.length) {
const $edge = edges[0].getContainer().get('item');
graph.emit('before-edge-removed', {
target: $edge,
callback (confirm) {
if (confirm) {
graph.remove($edge);
graph.set('after-edge-selected', []);
// 提交事件
graph.emit('after-edge-selected');
graph.emit('after-edge-removed', $edge);
}
},
});
}
}
},
});
};