Skip to content

fix(cdk/drag-drop): expose pickup position in constrainPosition callback #25341

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

Merged
merged 1 commit into from
Jul 26, 2022
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
2 changes: 2 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/cdk/drag-drop/directives/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
userPointerPosition: Point,
dragRef: DragRef,
dimensions: ClientRect,
pickupPositionInElement: Point,
) => Point;

/** Class to be added to the preview element. */
Expand Down
3 changes: 2 additions & 1 deletion src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ export class DragRef<T = any> {
userPointerPosition: Point,
dragRef: DragRef,
dimensions: ClientRect,
pickupPositionInElement: Point,
) => Point;

constructor(
Expand Down Expand Up @@ -1239,7 +1240,7 @@ export class DragRef<T = any> {
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') {
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/cdk/drag-drop.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class CdkDrag<T = any> 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<any> | undefined);
boundaryElement: string | ElementRef<HTMLElement> | 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);
Expand Down Expand Up @@ -359,7 +359,7 @@ export class DragDropRegistry<I extends {
export class DragRef<T = any> {
constructor(element: ElementRef<HTMLElement> | HTMLElement, _config: DragRefConfig, _document: Document, _ngZone: NgZone, _viewportRuler: ViewportRuler, _dragDropRegistry: DragDropRegistry<DragRef, DropListRefInternal>);
readonly beforeStarted: Subject<void>;
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);
Expand Down