@@ -13,6 +13,12 @@ export class SelectionSelect {
13
13
private disabled = true
14
14
private isWholeNode = true
15
15
private isWholeEdge = true
16
+ // 用于区分选区和点击事件
17
+ private mouseDownInfo : {
18
+ x : number
19
+ y : number
20
+ time : number
21
+ } | null = null
16
22
17
23
constructor ( { lf } : LogicFlow . IExtensionProps ) {
18
24
this . lf = lf
@@ -23,18 +29,22 @@ export class SelectionSelect {
23
29
lf . closeSelectionSelect = ( ) => {
24
30
this . closeSelectionSelect ( )
25
31
}
26
- this . onToolContainerMouseDown = this . onToolContainerMouseDown . bind ( this )
27
32
}
28
33
29
34
render ( _ : LogicFlow , domContainer : HTMLElement ) {
30
35
this . container = domContainer
31
36
}
32
37
33
- onToolContainerMouseDown ( e : MouseEvent ) {
38
+ onToolContainerMouseDown = ( e : MouseEvent ) => {
34
39
// 避免在其他插件元素上点击时开启选区
35
40
if ( e . target !== this . container ) {
36
41
return
37
42
}
43
+ this . mouseDownInfo = {
44
+ x : e . clientX ,
45
+ y : e . clientY ,
46
+ time : Date . now ( ) ,
47
+ }
38
48
const lf = this . lf
39
49
const domContainer = this . container
40
50
if ( ! domContainer ) {
@@ -72,6 +82,22 @@ export class SelectionSelect {
72
82
document . addEventListener ( 'mouseup' , this . drawOff )
73
83
}
74
84
85
+ onToolContainerMouseUp = ( e : MouseEvent ) => {
86
+ if ( this . mouseDownInfo ) {
87
+ const { x, y, time } = this . mouseDownInfo
88
+ const now = Date . now ( )
89
+ // 用 mouseDown 和 mouseUp 的位置偏移及时间间隔来判断是否是点击事件
90
+ const isClickEvent =
91
+ Math . abs ( e . clientX - x ) < 10 &&
92
+ Math . abs ( e . clientY - y ) < 10 &&
93
+ now - time < 100
94
+ if ( isClickEvent ) {
95
+ this . lf . clearSelectElements ( )
96
+ }
97
+ this . mouseDownInfo = null
98
+ }
99
+ }
100
+
75
101
/**
76
102
* 设置选中的灵敏度
77
103
* @param isWholeEdge 是否要边的起点终点都在选区范围才算选中。默认true
@@ -92,7 +118,9 @@ export class SelectionSelect {
92
118
if ( ! this . container ) {
93
119
return
94
120
}
121
+ this . mouseDownInfo = null
95
122
this . container . addEventListener ( 'mousedown' , this . onToolContainerMouseDown )
123
+ this . container . addEventListener ( 'mouseup' , this . onToolContainerMouseUp )
96
124
// 取消点击事件的穿透,只让 ToolOverlay 接收事件,避免与图形元素的事件冲突
97
125
this . container . style . pointerEvents = 'auto'
98
126
this . open ( )
@@ -106,10 +134,12 @@ export class SelectionSelect {
106
134
return
107
135
}
108
136
this . container . style . pointerEvents = 'none'
137
+ this . mouseDownInfo = null
109
138
this . container . removeEventListener (
110
139
'mousedown' ,
111
140
this . onToolContainerMouseDown ,
112
141
)
142
+ this . container . removeEventListener ( 'mouseup' , this . onToolContainerMouseUp )
113
143
this . close ( )
114
144
}
115
145
0 commit comments