8
8
import { type Instance } from '../../setup'
9
9
import { getTreeDiff , isDisabled } from '../../utils'
10
10
import { Buttons , getMouseEventButton , MouseButton } from './buttons'
11
- import { type Pointer } from './pointer'
12
11
import { isDifferentPointerPosition , pointerKey , PointerPosition } from './shared'
13
12
14
13
/**
@@ -66,7 +65,12 @@ export class Mouse {
66
65
}
67
66
} ) ( )
68
67
69
- move ( instance : Instance , position : PointerPosition ) {
68
+ move (
69
+ instance : Instance ,
70
+ position : PointerPosition ,
71
+ /** Whether `preventDefault()` has been called on the `pointerdown` event */
72
+ isPrevented : boolean ,
73
+ ) {
70
74
const prevPosition = this . position
71
75
const prevTarget = this . getTarget ( instance )
72
76
@@ -96,14 +100,23 @@ export class Mouse {
96
100
}
97
101
} ,
98
102
move : ( ) => {
103
+ if ( isPrevented ) {
104
+ return
105
+ }
106
+
99
107
instance . dispatchUIEvent ( nextTarget , 'mousemove' , init )
100
108
101
109
this . modifySelecting ( instance )
102
110
} ,
103
111
}
104
112
}
105
113
106
- down ( instance : Instance , keyDef : pointerKey , pointer : Pointer ) {
114
+ down (
115
+ instance : Instance ,
116
+ keyDef : pointerKey ,
117
+ /** Whether `preventDefault()` has been called on the `pointerdown` event */
118
+ isPrevented : boolean ,
119
+ ) {
107
120
const button = this . buttons . down ( keyDef )
108
121
109
122
if ( button === undefined ) {
@@ -112,42 +125,49 @@ export class Mouse {
112
125
113
126
const target = this . getTarget ( instance )
114
127
this . buttonDownTarget [ button ] = target
115
- const disabled = isDisabled ( target )
116
128
const init = this . getEventInit ( 'mousedown' , keyDef . button )
117
- if ( disabled || instance . dispatchUIEvent ( target , 'mousedown' , init ) ) {
129
+ const disabled = isDisabled ( target )
130
+ if (
131
+ ! isPrevented &&
132
+ ( disabled || instance . dispatchUIEvent ( target , 'mousedown' , init ) )
133
+ ) {
118
134
this . startSelecting ( instance , init . detail as number )
119
135
focusElement ( target )
120
136
}
121
137
if ( ! disabled && getMouseEventButton ( keyDef . button ) === 2 ) {
122
138
instance . dispatchUIEvent (
123
139
target ,
124
140
'contextmenu' ,
125
- this . getEventInit ( 'contextmenu' , keyDef . button , pointer ) ,
141
+ this . getEventInit ( 'contextmenu' , keyDef . button ) ,
126
142
)
127
143
}
128
144
}
129
145
130
- up ( instance : Instance , keyDef : pointerKey , pointer : Pointer ) {
146
+ up (
147
+ instance : Instance ,
148
+ keyDef : pointerKey ,
149
+ /** Whether `preventDefault()` has been called on the `pointerdown` event */
150
+ isPrevented : boolean ,
151
+ ) {
131
152
const button = this . buttons . up ( keyDef )
132
153
133
154
if ( button === undefined ) {
134
155
return
135
156
}
136
157
const target = this . getTarget ( instance )
137
158
if ( ! isDisabled ( target ) ) {
138
- instance . dispatchUIEvent (
139
- target ,
140
- 'mouseup' ,
141
- this . getEventInit ( 'mouseup' , keyDef . button ) ,
142
- )
143
- this . endSelecting ( )
159
+ if ( ! isPrevented ) {
160
+ const mouseUpInit = this . getEventInit ( 'mouseup' , keyDef . button )
161
+ instance . dispatchUIEvent ( target , 'mouseup' , mouseUpInit )
162
+ this . endSelecting ( )
163
+ }
144
164
145
165
const clickTarget = getTreeDiff (
146
166
this . buttonDownTarget [ button ] ,
147
167
target ,
148
168
) [ 2 ] [ 0 ] as Element | undefined
149
169
if ( clickTarget ) {
150
- const init = this . getEventInit ( 'click' , keyDef . button , pointer )
170
+ const init = this . getEventInit ( 'click' , keyDef . button )
151
171
if ( init . detail ) {
152
172
instance . dispatchUIEvent (
153
173
clickTarget ,
@@ -169,21 +189,11 @@ export class Mouse {
169
189
this . clickCount . reset ( )
170
190
}
171
191
172
- private getEventInit (
173
- type : EventType ,
174
- button ?: MouseButton ,
175
- pointer ?: Pointer ,
176
- ) {
177
- const init : PointerEventInit = {
192
+ private getEventInit ( type : EventType , button ?: MouseButton ) {
193
+ const init : MouseEventInit = {
178
194
...this . position . coords ,
179
195
}
180
196
181
- if ( pointer ) {
182
- init . pointerId = pointer . pointerId
183
- init . pointerType = pointer . pointerType
184
- init . isPrimary = pointer . isPrimary
185
- }
186
-
187
197
init . button = getMouseEventButton ( button )
188
198
init . buttons = this . buttons . getButtons ( )
189
199
0 commit comments