Skip to content

Commit 115b500

Browse files
committed
feat(drag-drop): add API to get/set current position of a standalone draggable
Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return. Fixes #14420. Fixes #14674.
1 parent e7b0e40 commit 115b500

File tree

4 files changed

+118
-12
lines changed

4 files changed

+118
-12
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,47 @@ describe('CdkDrag', () => {
680680
}).toThrowError(/^cdkDrag must be attached to an element node/);
681681
}));
682682

683+
it('should be able to get the current position', fakeAsync(() => {
684+
const fixture = createComponent(StandaloneDraggable);
685+
fixture.detectChanges();
686+
687+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
688+
const dragInstance = fixture.componentInstance.dragInstance;
689+
690+
expect(dragInstance.getFreeDragPosition()).toEqual({x: 0, y: 0});
691+
692+
dragElementViaMouse(fixture, dragElement, 50, 100);
693+
expect(dragInstance.getFreeDragPosition()).toEqual({x: 50, y: 100});
694+
695+
dragElementViaMouse(fixture, dragElement, 100, 200);
696+
expect(dragInstance.getFreeDragPosition()).toEqual({x: 150, y: 300});
697+
}));
698+
699+
it('should be able to set the current position', fakeAsync(() => {
700+
const fixture = createComponent(StandaloneDraggable);
701+
fixture.componentInstance.freeDragPosition = {x: 50, y: 100};
702+
fixture.detectChanges();
703+
704+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
705+
const dragInstance = fixture.componentInstance.dragInstance;
706+
707+
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');
708+
expect(dragInstance.getFreeDragPosition()).toEqual({x: 50, y: 100});
709+
}));
710+
711+
it('should be able to continue dragging after the current position was set', fakeAsync(() => {
712+
const fixture = createComponent(StandaloneDraggable);
713+
fixture.componentInstance.freeDragPosition = {x: 50, y: 100};
714+
fixture.detectChanges();
715+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
716+
717+
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');
718+
719+
dragElementViaMouse(fixture, dragElement, 100, 200);
720+
721+
expect(dragElement.style.transform).toBe('translate3d(150px, 300px, 0px)');
722+
}));
723+
683724
});
684725

685726
describe('draggable with a handle', () => {
@@ -2804,6 +2845,7 @@ describe('CdkDrag', () => {
28042845
<div
28052846
cdkDrag
28062847
[cdkDragBoundary]="boundarySelector"
2848+
[cdkDragFreeDragPosition]="freeDragPosition"
28072849
(cdkDragStarted)="startedSpy($event)"
28082850
(cdkDragReleased)="releasedSpy($event)"
28092851
(cdkDragEnded)="endedSpy($event)"
@@ -2819,6 +2861,7 @@ class StandaloneDraggable {
28192861
endedSpy = jasmine.createSpy('ended spy');
28202862
releasedSpy = jasmine.createSpy('released spy');
28212863
boundarySelector: string;
2864+
freeDragPosition?: {x: number, y: number};
28222865
}
28232866

28242867
@Component({

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
108108
*/
109109
@Input('cdkDragBoundary') boundaryElementSelector: string;
110110

111+
/**
112+
* Sets the position of a `CdkDrag` that is outside of a drop container.
113+
* Can be used to restore the element's position for a returning user.
114+
*/
115+
@Input('cdkDragFreeDragPosition') freeDragPosition: {x: number, y: number};
116+
111117
/** Whether starting to drag this element is disabled. */
112118
@Input('cdkDragDisabled')
113119
get disabled(): boolean {
@@ -212,6 +218,13 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
212218
this._dragRef.reset();
213219
}
214220

221+
/**
222+
* Gets the pixel coordinates of the draggable outside of a drop container.
223+
*/
224+
getFreeDragPosition(): {readonly x: number, readonly y: number} {
225+
return this._dragRef.getFreeDragPosition();
226+
}
227+
215228
ngAfterViewInit() {
216229
// We need to wait for the zone to stabilize, in order for the reference
217230
// element to be in the proper place in the DOM. This is mostly relevant
@@ -243,17 +256,27 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
243256
const handle = handleInstance.element.nativeElement;
244257
handleInstance.disabled ? dragRef.disableHandle(handle) : dragRef.enableHandle(handle);
245258
});
259+
260+
if (this.freeDragPosition) {
261+
this._dragRef.setFreeDragPosition(this.freeDragPosition);
262+
}
246263
});
247264
}
248265

249266
ngOnChanges(changes: SimpleChanges) {
250267
const rootSelectorChange = changes.rootElementSelector;
268+
const positionChange = changes.positionChange;
251269

252270
// We don't have to react to the first change since it's being
253271
// handled in `ngAfterViewInit` where it needs to be deferred.
254272
if (rootSelectorChange && !rootSelectorChange.firstChange) {
255273
this._updateRootElement();
256274
}
275+
276+
// Skip the first change since it's being handled in `ngAfterViewInit`.
277+
if (positionChange && !positionChange.firstChange && this.freeDragPosition) {
278+
this._dragRef.setFreeDragPosition(this.freeDragPosition);
279+
}
257280
}
258281

259282
ngOnDestroy() {

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

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,29 @@ export class DragRef<T = any> {
417417
this._dropContainer = container;
418418
}
419419

420+
/**
421+
* Gets the current position in pixels the draggable outside of a drop container.
422+
*/
423+
getFreeDragPosition(): Readonly<Point> {
424+
return {x: this._passiveTransform.x, y: this._passiveTransform.y};
425+
}
426+
427+
/**
428+
* Sets the current position in pixels the draggable outside of a drop container.
429+
* @param value New position to be set.
430+
*/
431+
setFreeDragPosition(value: Point): this {
432+
this._activeTransform = {x: 0, y: 0};
433+
this._passiveTransform.x = value.x;
434+
this._passiveTransform.y = value.y;
435+
436+
if (!this._dropContainer) {
437+
this._applyRootElementTransform(value.x, value.y);
438+
}
439+
440+
return this;
441+
}
442+
420443
/** Unsubscribes from the global subscriptions. */
421444
private _removeSubscriptions() {
422445
this._pointerMoveSubscription.unsubscribe();
@@ -509,13 +532,8 @@ export class DragRef<T = any> {
509532
constrainedPointerPosition.x - this._pickupPositionOnPage.x + this._passiveTransform.x;
510533
activeTransform.y =
511534
constrainedPointerPosition.y - this._pickupPositionOnPage.y + this._passiveTransform.y;
512-
const transform = getTransform(activeTransform.x, activeTransform.y);
513535

514-
// Preserve the previous `transform` value, if there was one. Note that we apply our own
515-
// transform before the user's, because things like rotation can affect which direction
516-
// the element will be translated towards.
517-
this._rootElement.style.transform = this._initialTransform ?
518-
transform + ' ' + this._initialTransform : transform;
536+
this._applyRootElementTransform(activeTransform.x, activeTransform.y);
519537

520538
// Apply transform as attribute if dragging and svg element to work for IE
521539
if (typeof SVGElement !== 'undefined' && this._rootElement instanceof SVGElement) {
@@ -642,12 +660,6 @@ export class DragRef<T = any> {
642660
return;
643661
}
644662

645-
// Cache the previous transform amount only after the first drag sequence, because
646-
// we don't want our own transforms to stack on top of each other.
647-
if (this._initialTransform == null) {
648-
this._initialTransform = this._rootElement.style.transform || '';
649-
}
650-
651663
// If we've got handles, we need to disable the tap highlight on the entire root element,
652664
// otherwise iOS will still add it, even though all the drag interactions on the handle
653665
// are disabled.
@@ -970,6 +982,26 @@ export class DragRef<T = any> {
970982
element.removeEventListener('touchstart', this._pointerDown, passiveEventListenerOptions);
971983
}
972984

985+
/**
986+
* Applies a `transform` to the root element, taking into account any existing transforms on it.
987+
* @param x New transform value along the X axis.
988+
* @param y New transform value along the Y axis.
989+
*/
990+
private _applyRootElementTransform(x: number, y: number) {
991+
const transform = getTransform(x, y);
992+
993+
// Cache the previous transform amount only after the first drag sequence, because
994+
// we don't want our own transforms to stack on top of each other.
995+
if (this._initialTransform == null) {
996+
this._initialTransform = this._rootElement.style.transform || '';
997+
}
998+
999+
// Preserve the previous `transform` value, if there was one. Note that we apply our own
1000+
// transform before the user's, because things like rotation can affect which direction
1001+
// the element will be translated towards.
1002+
this._rootElement.style.transform = this._initialTransform ?
1003+
transform + ' ' + this._initialTransform : transform;
1004+
}
9731005
}
9741006

9751007
/** Point on the page or within an element. */

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export declare class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDes
2020
ended: EventEmitter<CdkDragEnd>;
2121
entered: EventEmitter<CdkDragEnter<any>>;
2222
exited: EventEmitter<CdkDragExit<any>>;
23+
freeDragPosition: {
24+
x: number;
25+
y: number;
26+
};
2327
lockAxis: 'x' | 'y';
2428
moved: Observable<CdkDragMove<T>>;
2529
released: EventEmitter<CdkDragRelease>;
@@ -29,6 +33,10 @@ export declare class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDes
2933
element: ElementRef<HTMLElement>,
3034
dropContainer: CdkDropList, _document: any, _ngZone: NgZone, _viewContainerRef: ViewContainerRef, viewportRuler: ViewportRuler, dragDropRegistry: DragDropRegistry<DragRef, DropListRef>, config: DragRefConfig, _dir: Directionality,
3135
dragDrop?: DragDrop);
36+
getFreeDragPosition(): {
37+
readonly x: number;
38+
readonly y: number;
39+
};
3240
getPlaceholderElement(): HTMLElement;
3341
getRootElement(): HTMLElement;
3442
ngAfterViewInit(): void;

0 commit comments

Comments
 (0)