@@ -17,9 +17,6 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
17
17
capture : true
18
18
} ) ;
19
19
20
- /** Handler for a pointer event callback. */
21
- type PointerEventHandler = ( event : TouchEvent | MouseEvent ) => void ;
22
-
23
20
/**
24
21
* Service that keeps track of all the drag item and drop container
25
22
* instances, and manages global event listeners on the `document`.
@@ -42,8 +39,8 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
42
39
private _activeDragInstances = new Set < I > ( ) ;
43
40
44
41
/** Keeps track of the event listeners that we've bound to the `document`. */
45
- private _globalListeners = new Map < 'touchmove' | 'mousemove' | 'touchend' | 'mouseup' | 'wheel' , {
46
- handler : PointerEventHandler ,
42
+ private _globalListeners = new Map < string , {
43
+ handler : ( event : Event ) => void ,
47
44
options ?: AddEventListenerOptions | boolean
48
45
} > ( ) ;
49
46
@@ -87,7 +84,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
87
84
this . _ngZone . runOutsideAngular ( ( ) => {
88
85
// The event handler has to be explicitly active,
89
86
// because newer browsers make it passive by default.
90
- this . _document . addEventListener ( 'touchmove' , this . _preventScrollListener ,
87
+ this . _document . addEventListener ( 'touchmove' , this . _preventDefaultWhileDragging ,
91
88
activeCapturingEventOptions ) ;
92
89
} ) ;
93
90
}
@@ -104,7 +101,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
104
101
this . stopDragging ( drag ) ;
105
102
106
103
if ( this . _dragInstances . size === 0 ) {
107
- this . _document . removeEventListener ( 'touchmove' , this . _preventScrollListener ,
104
+ this . _document . removeEventListener ( 'touchmove' , this . _preventDefaultWhileDragging ,
108
105
activeCapturingEventOptions ) ;
109
106
}
110
107
}
@@ -127,19 +124,27 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
127
124
// use `preventDefault` to prevent the page from scrolling while the user is dragging.
128
125
this . _globalListeners
129
126
. set ( moveEvent , {
130
- handler : e => this . pointerMove . next ( e ) ,
127
+ handler : ( e : Event ) => this . pointerMove . next ( e as TouchEvent | MouseEvent ) ,
131
128
options : activeCapturingEventOptions
132
129
} )
133
130
. set ( upEvent , {
134
- handler : e => this . pointerUp . next ( e ) ,
131
+ handler : ( e : Event ) => this . pointerUp . next ( e as TouchEvent | MouseEvent ) ,
135
132
options : true
133
+ } )
134
+ // Preventing the default action on `mousemove` isn't enough to disable text selection
135
+ // on Safari so we need to prevent the selection event as well. Alternatively this can
136
+ // be done by setting `user-select: none` on the `body`, however it has causes a style
137
+ // recalculation which can be expensive on pages with a lot of elements.
138
+ . set ( 'selectstart' , {
139
+ handler : this . _preventDefaultWhileDragging ,
140
+ options : activeCapturingEventOptions
136
141
} ) ;
137
142
138
143
// TODO(crisbeto): prevent mouse wheel scrolling while
139
144
// dragging until we've set up proper scroll handling.
140
145
if ( ! isTouchEvent ) {
141
146
this . _globalListeners . set ( 'wheel' , {
142
- handler : this . _preventScrollListener ,
147
+ handler : this . _preventDefaultWhileDragging ,
143
148
options : activeCapturingEventOptions
144
149
} ) ;
145
150
}
@@ -184,9 +189,10 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
184
189
}
185
190
186
191
/**
187
- * Listener used to prevent `touchmove` and `wheel` events while the element is being dragged.
192
+ * Event listener that will prevent the default browser action while the user is dragging.
193
+ * @param event Event whose default action should be prevented.
188
194
*/
189
- private _preventScrollListener = ( event : Event ) => {
195
+ private _preventDefaultWhileDragging = ( event : Event ) => {
190
196
if ( this . _activeDragInstances . size ) {
191
197
event . preventDefault ( ) ;
192
198
}
0 commit comments