Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(drag-drop): unable to drop into connected sibling that was scrolled into view #16681

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3927,6 +3927,50 @@ describe('CdkDrag', () => {
expect(itemEnterEvent).toEqual(expectedEvent);
}));

it('should be able to drop into a new container after scrolling into view', fakeAsync(() => {
const fixture = createComponent(ConnectedDropZones);
fixture.detectChanges();

// Make the page scrollable and scroll the items out of view.
const cleanup = makePageScrollable();
scrollTo(0, 4000);
dispatchFakeEvent(document, 'scroll');
fixture.detectChanges();
flush();
fixture.detectChanges();

const groups = fixture.componentInstance.groupedDragItems;
const item = groups[0][1];

// Start dragging and then scroll the elements back into view.
startDraggingViaMouse(fixture, item.element.nativeElement);
scrollTo(0, 0);
dispatchFakeEvent(document, 'scroll');

const targetRect = groups[1][2].element.nativeElement.getBoundingClientRect();
dispatchMouseEvent(document, 'mousemove', targetRect.left + 1, targetRect.top + 1);
dispatchMouseEvent(document, 'mouseup', targetRect.left + 1, targetRect.top + 1);
fixture.detectChanges();
flush();
fixture.detectChanges();

expect(fixture.componentInstance.droppedSpy).toHaveBeenCalledTimes(1);

const event = fixture.componentInstance.droppedSpy.calls.mostRecent().args[0];

expect(event).toEqual({
previousIndex: 1,
currentIndex: 3,
item,
container: fixture.componentInstance.dropInstances.toArray()[1],
previousContainer: fixture.componentInstance.dropInstances.first,
isPointerOverContainer: true,
distance: {x: jasmine.any(Number), y: jasmine.any(Number)}
});

cleanup();
}));

});

describe('with nested drags', () => {
Expand Down
28 changes: 20 additions & 8 deletions src/cdk/drag-drop/drop-list-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,7 @@ export class DropListRef<T = any> {

// @breaking-change 9.0.0 Remove check for _viewportRuler once it's marked as a required param.
if (this._viewportRuler) {
this._viewportScrollPosition = this._viewportRuler.getViewportScrollPosition();
this._viewportScrollSubscription = this._dragDropRegistry.scroll.subscribe(() => {
if (this.isDragging()) {
const newPosition = this._viewportRuler!.getViewportScrollPosition();
this._updateAfterScroll(this._viewportScrollPosition, newPosition.top, newPosition.left,
this._clientRect);
}
});
this._listenToScrollEvents();
}
}

Expand Down Expand Up @@ -854,6 +847,7 @@ export class DropListRef<T = any> {
if (!activeSiblings.has(sibling)) {
activeSiblings.add(sibling);
this._cacheOwnPosition();
this._listenToScrollEvents();
}
}

Expand All @@ -863,6 +857,24 @@ export class DropListRef<T = any> {
*/
_stopReceiving(sibling: DropListRef) {
this._activeSiblings.delete(sibling);
this._viewportScrollSubscription.unsubscribe();
}

/**
* Starts listening to scroll events on the viewport.
* Used for updating the internal state of the list.
*/
private _listenToScrollEvents() {
this._viewportScrollPosition = this._viewportRuler!.getViewportScrollPosition();
this._viewportScrollSubscription = this._dragDropRegistry.scroll.subscribe(() => {
if (this.isDragging()) {
const newPosition = this._viewportRuler!.getViewportScrollPosition();
this._updateAfterScroll(this._viewportScrollPosition, newPosition.top, newPosition.left,
this._clientRect);
} else if (this.isReceiving()) {
this._cacheOwnPosition();
}
});
}
}

Expand Down