@@ -83,6 +83,14 @@ export function CDK_DRAG_CONFIG_FACTORY(): CdkDragConfig {
83
83
const passiveEventListenerOptions = supportsPassiveEventListeners ( ) ?
84
84
{ passive : true } as EventListenerOptions : false ;
85
85
86
+ /**
87
+ * Time in milliseconds for which to ignore mouse events, after
88
+ * receiving a touch event. Used to avoid doing double work for
89
+ * touch devices where the browser fires fake mouse events, in
90
+ * addition to touch events.
91
+ */
92
+ const MOUSE_EVENT_IGNORE_TIME = 800 ;
93
+
86
94
/** Element that can be moved inside a CdkDropList container. */
87
95
@Directive ( {
88
96
selector : '[cdkDrag]' ,
@@ -175,6 +183,12 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
175
183
176
184
/** Subscription to the event that is dispatched when the user lifts their pointer. */
177
185
private _pointerUpSubscription = Subscription . EMPTY ;
186
+ /**
187
+ * Time at which the last touch event occurred. Used to avoid firing the same
188
+ * events multiple times on touch devices where the browser will fire a fake
189
+ * mouse event for each touch event, after a certain time.
190
+ */
191
+ private _lastTouchEventTime : number ;
178
192
179
193
/** Elements that can be used to drag the draggable item. */
180
194
@ContentChildren ( CdkDragHandle , { descendants : true } ) _handles : QueryList < CdkDragHandle > ;
@@ -330,9 +344,13 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
330
344
*/
331
345
private _initializeDragSequence ( referenceElement : HTMLElement , event : MouseEvent | TouchEvent ) {
332
346
const isDragging = this . _isDragging ( ) ;
347
+ const isTouchEvent = this . _isTouchEvent ( event ) ;
348
+ const isAuxiliaryMouseButton = ! isTouchEvent && ( event as MouseEvent ) . button !== 0 ;
349
+ const isSyntheticEvent = ! isTouchEvent && this . _lastTouchEventTime &&
350
+ this . _lastTouchEventTime + MOUSE_EVENT_IGNORE_TIME > Date . now ( ) ;
333
351
334
352
// Abort if the user is already dragging or is using a mouse button other than the primary one.
335
- if ( isDragging || ( ! this . _isTouchEvent ( event ) && event . button !== 0 ) ) {
353
+ if ( isDragging || isAuxiliaryMouseButton || isSyntheticEvent ) {
336
354
return ;
337
355
}
338
356
@@ -359,10 +377,14 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
359
377
}
360
378
361
379
/** Starts the dragging sequence. */
362
- private _startDragSequence ( ) {
380
+ private _startDragSequence ( event : MouseEvent | TouchEvent ) {
363
381
// Emit the event on the item before the one on the container.
364
382
this . started . emit ( { source : this } ) ;
365
383
384
+ if ( this . _isTouchEvent ( event ) ) {
385
+ this . _lastTouchEventTime = Date . now ( ) ;
386
+ }
387
+
366
388
if ( this . dropContainer ) {
367
389
const element = this . _rootElement ;
368
390
@@ -397,7 +419,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
397
419
// per pixel of movement (e.g. if the user moves their pointer quickly).
398
420
if ( distanceX + distanceY >= this . _config . dragStartThreshold ) {
399
421
this . _hasStartedDragging = true ;
400
- this . _ngZone . run ( ( ) => this . _startDragSequence ( ) ) ;
422
+ this . _ngZone . run ( ( ) => this . _startDragSequence ( event ) ) ;
401
423
}
402
424
403
425
return ;
@@ -457,10 +479,14 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
457
479
this . _passiveTransform . x = this . _activeTransform . x ;
458
480
this . _passiveTransform . y = this . _activeTransform . y ;
459
481
this . _ngZone . run ( ( ) => this . ended . emit ( { source : this } ) ) ;
482
+ this . _dragDropRegistry . stopDragging ( this ) ;
460
483
return ;
461
484
}
462
485
463
- this . _animatePreviewToPlaceholder ( ) . then ( ( ) => this . _cleanupDragArtifacts ( ) ) ;
486
+ this . _animatePreviewToPlaceholder ( ) . then ( ( ) => {
487
+ this . _cleanupDragArtifacts ( ) ;
488
+ this . _dragDropRegistry . stopDragging ( this ) ;
489
+ } ) ;
464
490
}
465
491
466
492
/** Cleans up the DOM artifacts that were added to facilitate the element being dragged. */
0 commit comments