Skip to content

Commit 5cd1b28

Browse files
committed
fix(cdk/drag-drop): not stopping drag if page is blurred
Currently the only way to stop a drag sequence is via a `mouseup`/`touchend` event or by destroying the instance, however if the page loses focus while dragging the events won't be dispatched anymore and user will have to click again to stop dragging. These changes add some extra code that listens for `blur` events on the `window` and stops dragging. Fixes #17537.
1 parent 4f8e87e commit 5cd1b28

File tree

5 files changed

+87
-13
lines changed

5 files changed

+87
-13
lines changed

src/cdk/drag-drop/directives/drag.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -4331,6 +4331,27 @@ describe('CdkDrag', () => {
43314331
}).toThrowError(/^cdkDropList must be attached to an element node/);
43324332
}));
43334333

4334+
it('should stop dragging if the page is blurred', fakeAsync(() => {
4335+
const fixture = createComponent(DraggableInDropZone);
4336+
fixture.detectChanges();
4337+
const dragItems = fixture.componentInstance.dragItems;
4338+
4339+
expect(fixture.componentInstance.droppedSpy).not.toHaveBeenCalled();
4340+
4341+
const item = dragItems.first;
4342+
const targetRect = dragItems.toArray()[2].element.nativeElement.getBoundingClientRect();
4343+
4344+
startDraggingViaMouse(fixture, item.element.nativeElement);
4345+
dispatchMouseEvent(document, 'mousemove', targetRect.left + 1, targetRect.top + 1);
4346+
fixture.detectChanges();
4347+
4348+
dispatchFakeEvent(window, 'blur');
4349+
fixture.detectChanges();
4350+
flush();
4351+
4352+
expect(fixture.componentInstance.droppedSpy).toHaveBeenCalledTimes(1);
4353+
}));
4354+
43344355
});
43354356

43364357
describe('in a connected drop container', () => {

src/cdk/drag-drop/drag-drop-registry.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,18 @@ describe('DragDropRegistry', () => {
244244
subscription.unsubscribe();
245245
});
246246

247+
it('should dispatch an event if the window is blurred while scrolling', () => {
248+
const spy = jasmine.createSpy('blur spy');
249+
const subscription = registry.pageBlurred.subscribe(spy);
250+
const item = new DragItem();
251+
252+
registry.startDragging(item, createMouseEvent('mousedown'));
253+
dispatchFakeEvent(window, 'blur');
254+
255+
expect(spy).toHaveBeenCalled();
256+
subscription.unsubscribe();
257+
});
258+
247259
class DragItem {
248260
isDragging() { return this.shouldBeDragging; }
249261
constructor(public shouldBeDragging = false) {

src/cdk/drag-drop/drag-drop-registry.ts

+31-9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
2828
@Injectable({providedIn: 'root'})
2929
export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements OnDestroy {
3030
private _document: Document;
31+
private _window: Window | null;
3132

3233
/** Registered drop container instances. */
3334
private _dropInstances = new Set<C>();
@@ -41,6 +42,10 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
4142
/** Keeps track of the event listeners that we've bound to the `document`. */
4243
private _globalListeners = new Map<string, {
4344
handler: (event: Event) => void,
45+
// The target needs to be `| null` because we bind either to `window` or `document` which
46+
// aren't available during SSR. There's an injection token for the document, but not one for
47+
// window so we fall back to not binding events to it.
48+
target: EventTarget | null,
4449
options?: AddEventListenerOptions | boolean
4550
}>();
4651

@@ -54,21 +59,25 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
5459
* Emits the `touchmove` or `mousemove` events that are dispatched
5560
* while the user is dragging a drag item instance.
5661
*/
57-
readonly pointerMove: Subject<TouchEvent | MouseEvent> = new Subject<TouchEvent | MouseEvent>();
62+
pointerMove: Subject<TouchEvent | MouseEvent> = new Subject<TouchEvent | MouseEvent>();
5863

5964
/**
6065
* Emits the `touchend` or `mouseup` events that are dispatched
6166
* while the user is dragging a drag item instance.
6267
*/
63-
readonly pointerUp: Subject<TouchEvent | MouseEvent> = new Subject<TouchEvent | MouseEvent>();
68+
pointerUp: Subject<TouchEvent | MouseEvent> = new Subject<TouchEvent | MouseEvent>();
6469

6570
/** Emits when the viewport has been scrolled while the user is dragging an item. */
66-
readonly scroll: Subject<Event> = new Subject<Event>();
71+
scroll: Subject<Event> = new Subject<Event>();
72+
73+
/** Emits when the page has been blurred while the user is dragging an item. */
74+
pageBlurred: Subject<void> = new Subject<void>();
6775

6876
constructor(
6977
private _ngZone: NgZone,
7078
@Inject(DOCUMENT) _document: any) {
7179
this._document = _document;
80+
this._window = (typeof window !== 'undefined' && window.addEventListener) ? window : null;
7281
}
7382

7483
/** Adds a drop container to the registry. */
@@ -133,35 +142,45 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
133142
this._globalListeners
134143
.set(isTouchEvent ? 'touchend' : 'mouseup', {
135144
handler: (e: Event) => this.pointerUp.next(e as TouchEvent | MouseEvent),
136-
options: true
145+
options: true,
146+
target: this._document
137147
})
138148
.set('scroll', {
139149
handler: (e: Event) => this.scroll.next(e),
140150
// Use capturing so that we pick up scroll changes in any scrollable nodes that aren't
141151
// the document. See https://github.com/angular/components/issues/17144.
142-
options: true
152+
options: true,
153+
target: this._document
143154
})
144155
// Preventing the default action on `mousemove` isn't enough to disable text selection
145156
// on Safari so we need to prevent the selection event as well. Alternatively this can
146157
// be done by setting `user-select: none` on the `body`, however it has causes a style
147158
// recalculation which can be expensive on pages with a lot of elements.
148159
.set('selectstart', {
149160
handler: this._preventDefaultWhileDragging,
150-
options: activeCapturingEventOptions
161+
options: activeCapturingEventOptions,
162+
target: this._document
163+
})
164+
.set('blur', {
165+
handler: () => this.pageBlurred.next(),
166+
target: this._window // Note that this event can only be bound on the window, not document
151167
});
152168

153169
// We don't have to bind a move event for touch drag sequences, because
154170
// we already have a persistent global one bound from `registerDragItem`.
155171
if (!isTouchEvent) {
156172
this._globalListeners.set('mousemove', {
157173
handler: (e: Event) => this.pointerMove.next(e as MouseEvent),
158-
options: activeCapturingEventOptions
174+
options: activeCapturingEventOptions,
175+
target: this._document
159176
});
160177
}
161178

162179
this._ngZone.runOutsideAngular(() => {
163180
this._globalListeners.forEach((config, name) => {
164-
this._document.addEventListener(name, config.handler, config.options);
181+
if (config.target) {
182+
config.target.addEventListener(name, config.handler, config.options);
183+
}
165184
});
166185
});
167186
}
@@ -191,6 +210,7 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
191210
this._clearGlobalListeners();
192211
this.pointerMove.complete();
193212
this.pointerUp.complete();
213+
this.pageBlurred.complete();
194214
}
195215

196216
/**
@@ -220,7 +240,9 @@ export class DragDropRegistry<I extends {isDragging(): boolean}, C> implements O
220240
/** Clears out the global event listeners from the `document`. */
221241
private _clearGlobalListeners() {
222242
this._globalListeners.forEach((config, name) => {
223-
this._document.removeEventListener(name, config.handler, config.options);
243+
if (config.target) {
244+
config.target.removeEventListener(name, config.handler, config.options);
245+
}
224246
});
225247

226248
this._globalListeners.clear();

src/cdk/drag-drop/drag-ref.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,19 @@ export class DragRef<T = any> {
207207
/** Subscription to the viewport being resized. */
208208
private _resizeSubscription = Subscription.EMPTY;
209209

210+
/** Subscription to the page being blurred. */
211+
private _blurSubscription = Subscription.EMPTY;
212+
210213
/**
211214
* Time at which the last touch event occurred. Used to avoid firing the same
212215
* events multiple times on touch devices where the browser will fire a fake
213216
* mouse event for each touch event, after a certain time.
214217
*/
215218
private _lastTouchEventTime: number;
216219

220+
/** Last pointer move event that was captured. */
221+
private _lastPointerMove: MouseEvent | TouchEvent | null;
222+
217223
/** Time at which the last dragging sequence was started. */
218224
private _dragStartTime: number;
219225

@@ -488,7 +494,7 @@ export class DragRef<T = any> {
488494
this._resizeSubscription.unsubscribe();
489495
this._parentPositions.clear();
490496
this._boundaryElement = this._rootElement = this._ownerSVGElement = this._placeholderTemplate =
491-
this._previewTemplate = this._anchor = this._parentDragRef = null!;
497+
this._previewTemplate = this._anchor = this._parentDragRef = this._lastPointerMove = null!;
492498
}
493499

494500
/** Checks whether the element is currently being dragged. */
@@ -583,6 +589,7 @@ export class DragRef<T = any> {
583589
this._pointerMoveSubscription.unsubscribe();
584590
this._pointerUpSubscription.unsubscribe();
585591
this._scrollSubscription.unsubscribe();
592+
this._blurSubscription.unsubscribe();
586593
}
587594

588595
/** Destroys the preview element and its ViewRef. */
@@ -684,6 +691,7 @@ export class DragRef<T = any> {
684691
const constrainedPointerPosition = this._getConstrainedPointerPosition(pointerPosition);
685692
this._hasMoved = true;
686693
this._lastKnownPointerPosition = pointerPosition;
694+
this._lastPointerMove = event;
687695
this._updatePointerDirectionDelta(constrainedPointerPosition);
688696

689697
if (this._dropContainer) {
@@ -866,6 +874,7 @@ export class DragRef<T = any> {
866874
}
867875

868876
this._hasStartedDragging = this._hasMoved = false;
877+
this._lastPointerMove = null;
869878

870879
// Avoid multiple subscriptions and memory leaks when multi touch
871880
// (isDragging check above isn't enough because of possible temporal and/or dimensional delays)
@@ -876,6 +885,15 @@ export class DragRef<T = any> {
876885
this._updateOnScroll(scrollEvent);
877886
});
878887

888+
// If the page is blurred while dragging (e.g. there was an `alert` or the browser window was
889+
// minimized) we won't get a mouseup/touchend so we need to use a different event to stop the
890+
// drag sequence. Use the last known location to figure out where the element should be dropped.
891+
this._blurSubscription = this._dragDropRegistry.pageBlurred.subscribe(() => {
892+
if (this._lastPointerMove) {
893+
this._endDragSequence(this._lastPointerMove);
894+
}
895+
});
896+
879897
if (this._boundaryElement) {
880898
this._boundaryRect = getMutableClientRect(this._boundaryElement);
881899
}

tools/public_api_guard/cdk/drag-drop.d.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,10 @@ export declare class DragDropModule {
244244
export declare class DragDropRegistry<I extends {
245245
isDragging(): boolean;
246246
}, C> implements OnDestroy {
247-
readonly pointerMove: Subject<TouchEvent | MouseEvent>;
248-
readonly pointerUp: Subject<TouchEvent | MouseEvent>;
249-
readonly scroll: Subject<Event>;
247+
pageBlurred: Subject<void>;
248+
pointerMove: Subject<TouchEvent | MouseEvent>;
249+
pointerUp: Subject<TouchEvent | MouseEvent>;
250+
scroll: Subject<Event>;
250251
constructor(_ngZone: NgZone, _document: any);
251252
isDragging(drag: I): boolean;
252253
ngOnDestroy(): void;

0 commit comments

Comments
 (0)