From 636dd60ee1b162245537dd97964970cb88b68d83 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 26 Jul 2022 11:56:57 +0200 Subject: [PATCH] fix(cdk/drag-drop): expose pickup position in constrainPosition callback (#25341) Exposes the pickup position within the element to the `constrainPosition` callback so that users can account for it in their calculations. Relates to #25154. (cherry picked from commit 73c0c6032f1da5b345d2d091998d8ac782179cdd) --- src/cdk/drag-drop/directives/drag.spec.ts | 2 ++ src/cdk/drag-drop/directives/drag.ts | 1 + src/cdk/drag-drop/drag-ref.ts | 3 ++- tools/public_api_guard/cdk/drag-drop.md | 4 ++-- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/cdk/drag-drop/directives/drag.spec.ts b/src/cdk/drag-drop/directives/drag.spec.ts index 314deae4c929..a77582994059 100644 --- a/src/cdk/drag-drop/directives/drag.spec.ts +++ b/src/cdk/drag-drop/directives/drag.spec.ts @@ -1082,6 +1082,7 @@ describe('CdkDrag', () => { jasmine.objectContaining({x: 300, y: 300}), jasmine.any(DragRef), jasmine.anything(), + jasmine.objectContaining({x: jasmine.any(Number), y: jasmine.any(Number)}), ); const elementRect = dragElement.getBoundingClientRect(); @@ -3685,6 +3686,7 @@ describe('CdkDrag', () => { jasmine.objectContaining({x: 200, y: 200}), jasmine.any(DragRef), jasmine.anything(), + jasmine.objectContaining({x: jasmine.any(Number), y: jasmine.any(Number)}), ); expect(Math.floor(previewRect.top)).toBe(50); expect(Math.floor(previewRect.left)).toBe(50); diff --git a/src/cdk/drag-drop/directives/drag.ts b/src/cdk/drag-drop/directives/drag.ts index 5a767fe73bd1..043ac5306b44 100644 --- a/src/cdk/drag-drop/directives/drag.ts +++ b/src/cdk/drag-drop/directives/drag.ts @@ -139,6 +139,7 @@ export class CdkDrag implements AfterViewInit, OnChanges, OnDestroy { userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, + pickupPositionInElement: Point, ) => Point; /** Class to be added to the preview element. */ diff --git a/src/cdk/drag-drop/drag-ref.ts b/src/cdk/drag-drop/drag-ref.ts index 7cedeabe9afa..4b0b125bbb39 100644 --- a/src/cdk/drag-drop/drag-ref.ts +++ b/src/cdk/drag-drop/drag-ref.ts @@ -365,6 +365,7 @@ export class DragRef { userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, + pickupPositionInElement: Point, ) => Point; constructor( @@ -1239,7 +1240,7 @@ export class DragRef { private _getConstrainedPointerPosition(point: Point): Point { const dropContainerLock = this._dropContainer ? this._dropContainer.lockAxis : null; let {x, y} = this.constrainPosition - ? this.constrainPosition(point, this, this._initialClientRect!) + ? this.constrainPosition(point, this, this._initialClientRect!, this._pickupPositionInElement) : point; if (this.lockAxis === 'x' || dropContainerLock === 'x') { diff --git a/tools/public_api_guard/cdk/drag-drop.md b/tools/public_api_guard/cdk/drag-drop.md index 631a2ea61882..cb8c983fedfd 100644 --- a/tools/public_api_guard/cdk/drag-drop.md +++ b/tools/public_api_guard/cdk/drag-drop.md @@ -55,7 +55,7 @@ export class CdkDrag implements AfterViewInit, OnChanges, OnDestroy { dropContainer: CdkDropListInternal, _document: any, _ngZone: NgZone, _viewContainerRef: ViewContainerRef, config: DragDropConfig, _dir: Directionality, dragDrop: DragDrop, _changeDetectorRef: ChangeDetectorRef, _selfHandle?: CdkDragHandle | undefined, _parentDrag?: CdkDrag | undefined); boundaryElement: string | ElementRef | HTMLElement; - constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect) => Point; + constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, pickupPositionInElement: Point) => Point; data: T; get disabled(): boolean; set disabled(value: BooleanInput); @@ -359,7 +359,7 @@ export class DragDropRegistry { constructor(element: ElementRef | HTMLElement, _config: DragRefConfig, _document: Document, _ngZone: NgZone, _viewportRuler: ViewportRuler, _dragDropRegistry: DragDropRegistry); readonly beforeStarted: Subject; - constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect) => Point; + constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, pickupPositionInElement: Point) => Point; data: T; get disabled(): boolean; set disabled(value: boolean);