Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(drag-drop): error if drag sequence is started while another one i…
…s finishing (#16081)
  • Loading branch information
crisbeto authored and josephperrott committed Jun 10, 2019
1 parent 4d0d080 commit 23df7aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Expand Up @@ -2640,6 +2640,23 @@ describe('CdkDrag', () => {
}).not.toThrow();
}));

it('should not be able to start a drag sequence while another one is still active',
fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone);
fixture.detectChanges();
const [item, otherItem] = fixture.componentInstance.dragItems.toArray();

startDraggingViaMouse(fixture, item.element.nativeElement);

expect(document.querySelectorAll('.cdk-drag-dragging').length)
.toBe(1, 'Expected one item to be dragged initially.');

startDraggingViaMouse(fixture, otherItem.element.nativeElement);

expect(document.querySelectorAll('.cdk-drag-dragging').length)
.toBe(1, 'Expected only one item to continue to be dragged.');
}));

});

describe('in a connected drop container', () => {
Expand Down
9 changes: 7 additions & 2 deletions src/cdk/drag-drop/drag-ref.ts
Expand Up @@ -510,8 +510,13 @@ export class DragRef<T = any> {
// in the `pointerMove` subscription, because we're not guaranteed to have one move event
// per pixel of movement (e.g. if the user moves their pointer quickly).
if (isOverThreshold && (Date.now() >= this._dragStartTime + (this.dragStartDelay || 0))) {
this._hasStartedDragging = true;
this._ngZone.run(() => this._startDragSequence(event));
// Prevent other drag sequences from starting while something in the container is still
// being dragged. This can happen while we're waiting for the drop animation to finish
// and can cause errors, because some elements might still be moving around.
if (!this._dropContainer || !this._dropContainer.isDragging()) {
this._hasStartedDragging = true;
this._ngZone.run(() => this._startDragSequence(event));
}
}

return;
Expand Down

0 comments on commit 23df7aa

Please sign in to comment.