Skip to content

Commit d5f9792

Browse files
committed
refactor(cdk/drag-drop): remove circular dependencies
Uses type-only imports to resolve the circular dependencies in the drag-drop module. (cherry picked from commit 81e6ead)
1 parent 197b16d commit d5f9792

File tree

8 files changed

+48
-71
lines changed

8 files changed

+48
-71
lines changed

goldens/ts-circular-deps.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
[
2-
["src/cdk/drag-drop/directives/drag.ts", "src/cdk/drag-drop/directives/drop-list.ts"],
32
[
4-
"src/cdk/drag-drop/directives/drag.ts",
5-
"src/cdk/drag-drop/directives/drop-list.ts",
6-
"src/cdk/drag-drop/drag-events.ts"
3+
"src/cdk/scrolling/scroll-dispatcher.ts",
4+
"src/cdk/scrolling/scrollable.ts"
75
],
8-
["src/cdk/drag-drop/directives/drag.ts", "src/cdk/drag-drop/drag-events.ts"],
9-
["src/cdk/drag-drop/drag-ref.ts", "src/cdk/drag-drop/drop-list-ref.ts"],
10-
["src/cdk/scrolling/scroll-dispatcher.ts", "src/cdk/scrolling/scrollable.ts"],
11-
["src/cdk/scrolling/virtual-scroll-strategy.ts", "src/cdk/scrolling/virtual-scroll-viewport.ts"]
12-
]
6+
[
7+
"src/cdk/scrolling/virtual-scroll-strategy.ts",
8+
"src/cdk/scrolling/virtual-scroll-viewport.ts"
9+
]
10+
]

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
SimpleChanges,
2929
ChangeDetectorRef,
3030
Self,
31+
InjectionToken,
3132
} from '@angular/core';
3233
import {
3334
coerceBooleanProperty,
@@ -37,7 +38,7 @@ import {
3738
} from '@angular/cdk/coercion';
3839
import {Observable, Observer, Subject, merge} from 'rxjs';
3940
import {startWith, take, map, takeUntil, switchMap, tap} from 'rxjs/operators';
40-
import {
41+
import type {
4142
CdkDragDrop,
4243
CdkDragEnd,
4344
CdkDragEnter,
@@ -51,13 +52,20 @@ import {CDK_DRAG_PLACEHOLDER, CdkDragPlaceholder} from './drag-placeholder';
5152
import {CDK_DRAG_PREVIEW, CdkDragPreview} from './drag-preview';
5253
import {CDK_DRAG_PARENT} from '../drag-parent';
5354
import {DragRef, Point, PreviewContainer} from '../drag-ref';
54-
import {CDK_DROP_LIST, CdkDropListInternal as CdkDropList} from './drop-list';
55+
import type {CdkDropList} from './drop-list';
5556
import {DragDrop} from '../drag-drop';
5657
import {CDK_DRAG_CONFIG, DragDropConfig, DragStartDelay, DragAxis} from './config';
5758
import {assertElementNode} from './assertions';
5859

5960
const DRAG_HOST_CLASS = 'cdk-drag';
6061

62+
/**
63+
* Injection token that can be used to reference instances of `CdkDropList`. It serves as
64+
* alternative token to the actual `CdkDropList` class which could cause unnecessary
65+
* retention of the class and its directive metadata.
66+
*/
67+
export const CDK_DROP_LIST = new InjectionToken<CdkDropList>('CdkDropList');
68+
6169
/** Element that can be moved inside a CdkDropList container. */
6270
@Directive({
6371
selector: '[cdkDrag]',

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ import {
2424
ChangeDetectorRef,
2525
SkipSelf,
2626
Inject,
27-
InjectionToken,
2827
} from '@angular/core';
2928
import {Directionality} from '@angular/cdk/bidi';
3029
import {ScrollDispatcher} from '@angular/cdk/scrolling';
31-
import {CdkDrag} from './drag';
30+
import {CDK_DROP_LIST, CdkDrag} from './drag';
3231
import {CdkDragDrop, CdkDragEnter, CdkDragExit, CdkDragSortEvent} from '../drag-events';
3332
import {CDK_DROP_LIST_GROUP, CdkDropListGroup} from './drop-list-group';
3433
import {DropListRef} from '../drop-list-ref';
@@ -42,20 +41,6 @@ import {assertElementNode} from './assertions';
4241
/** Counter used to generate unique ids for drop zones. */
4342
let _uniqueIdCounter = 0;
4443

45-
/**
46-
* Internal compile-time-only representation of a `CdkDropList`.
47-
* Used to avoid circular import issues between the `CdkDropList` and the `CdkDrag`.
48-
* @docs-private
49-
*/
50-
export interface CdkDropListInternal extends CdkDropList {}
51-
52-
/**
53-
* Injection token that can be used to reference instances of `CdkDropList`. It serves as
54-
* alternative token to the actual `CdkDropList` class which could cause unnecessary
55-
* retention of the class and its directive metadata.
56-
*/
57-
export const CDK_DROP_LIST = new InjectionToken<CdkDropList>('CdkDropList');
58-
5944
/** Container that wraps a set of draggable items. */
6045
@Directive({
6146
selector: '[cdkDropList], cdk-drop-list',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {CdkDrag} from './directives/drag';
10-
import {CdkDropList} from './directives/drop-list';
9+
import type {CdkDrag} from './directives/drag';
10+
import type {CdkDropList} from './directives/drop-list';
1111

1212
/** Event emitted when the user starts dragging a draggable. */
1313
export interface CdkDragStart<T = any> {

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import {coerceBooleanProperty, coerceElement} from '@angular/cdk/coercion';
1818
import {isFakeMousedownFromScreenReader, isFakeTouchstartFromScreenReader} from '@angular/cdk/a11y';
1919
import {Subscription, Subject, Observable} from 'rxjs';
20-
import {DropListRefInternal as DropListRef} from './drop-list-ref';
20+
import type {DropListRef} from './drop-list-ref';
2121
import {DragDropRegistry} from './drag-drop-registry';
2222
import {
2323
combineTransforms,
@@ -69,13 +69,6 @@ const MOUSE_EVENT_IGNORE_TIME = 800;
6969
// TODO(crisbeto): add an API for moving a draggable up/down the
7070
// list programmatically. Useful for keyboard controls.
7171

72-
/**
73-
* Internal compile-time-only representation of a `DragRef`.
74-
* Used to avoid circular import issues between the `DragRef` and the `DropListRef`.
75-
* @docs-private
76-
*/
77-
export interface DragRefInternal extends DragRef {}
78-
7972
/** Template that can be used to create a drag helper element (e.g. a preview or a placeholder). */
8073
interface DragHelperTemplate<T = any> {
8174
template: TemplateRef<T> | null;

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {_getShadowRoot} from '@angular/cdk/platform';
1414
import {Subject, Subscription, interval, animationFrameScheduler} from 'rxjs';
1515
import {takeUntil} from 'rxjs/operators';
1616
import {DragDropRegistry} from './drag-drop-registry';
17-
import {DragRefInternal as DragRef, Point} from './drag-ref';
17+
import type {DragRef, Point} from './drag-ref';
1818
import {isPointerNearClientRect, isInsideClientRect} from './dom/client-rect';
1919
import {ParentPositionTracker} from './dom/parent-position-tracker';
2020
import {DragCSSStyleDeclaration} from './dom/styling';
@@ -47,13 +47,6 @@ const enum AutoScrollHorizontalDirection {
4747
RIGHT,
4848
}
4949

50-
/**
51-
* Internal compile-time-only representation of a `DropListRef`.
52-
* Used to avoid circular import issues between the `DropListRef` and the `DragRef`.
53-
* @docs-private
54-
*/
55-
export interface DropListRefInternal extends DropListRef {}
56-
5750
type RootNode = DocumentOrShadowRoot & {
5851
// As of TS 4.4 the built in DOM typings don't include `elementFromPoint` on `ShadowRoot`,
5952
// even though it exists (see https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot).

src/cdk/drag-drop/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export * from './drag-utils';
1616
export * from './drag-drop-module';
1717
export * from './drag-drop-registry';
1818

19-
export {CdkDropList, CDK_DROP_LIST} from './directives/drop-list';
19+
export {CdkDropList} from './directives/drop-list';
2020
export * from './directives/config';
2121
export * from './directives/drop-list-group';
2222
export * from './directives/drag';

tools/public_api_guard/cdk/drag-drop.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const CDK_DROP_LIST_GROUP: InjectionToken<CdkDropListGroup<unknown>>;
5252
export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
5353
constructor(
5454
element: ElementRef<HTMLElement>,
55-
dropContainer: CdkDropListInternal,
55+
dropContainer: CdkDropList,
5656
_document: any, _ngZone: NgZone, _viewContainerRef: ViewContainerRef, config: DragDropConfig, _dir: Directionality, dragDrop: DragDrop, _changeDetectorRef: ChangeDetectorRef, _selfHandle?: CdkDragHandle | undefined, _parentDrag?: CdkDrag<any> | undefined);
5757
boundaryElement: string | ElementRef<HTMLElement> | HTMLElement;
5858
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, pickupPositionInElement: Point) => Point;
@@ -61,7 +61,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
6161
set disabled(value: BooleanInput);
6262
_dragRef: DragRef<CdkDrag<T>>;
6363
dragStartDelay: DragStartDelay;
64-
dropContainer: CdkDropListInternal;
64+
dropContainer: CdkDropList;
6565
readonly dropped: EventEmitter<CdkDragDrop<any>>;
6666
element: ElementRef<HTMLElement>;
6767
readonly ended: EventEmitter<CdkDragEnd>;
@@ -353,7 +353,7 @@ export class DragDropRegistry<I extends {
353353

354354
// @public
355355
export class DragRef<T = any> {
356-
constructor(element: ElementRef<HTMLElement> | HTMLElement, _config: DragRefConfig, _document: Document, _ngZone: NgZone, _viewportRuler: ViewportRuler, _dragDropRegistry: DragDropRegistry<DragRef, DropListRefInternal>);
356+
constructor(element: ElementRef<HTMLElement> | HTMLElement, _config: DragRefConfig, _document: Document, _ngZone: NgZone, _viewportRuler: ViewportRuler, _dragDropRegistry: DragDropRegistry<DragRef, DropListRef>);
357357
readonly beforeStarted: Subject<void>;
358358
constrainPosition?: (userPointerPosition: Point, dragRef: DragRef, dimensions: ClientRect, pickupPositionInElement: Point) => Point;
359359
data: T;
@@ -369,8 +369,8 @@ export class DragRef<T = any> {
369369
previousIndex: number;
370370
currentIndex: number;
371371
item: DragRef;
372-
container: DropListRefInternal;
373-
previousContainer: DropListRefInternal;
372+
container: DropListRef;
373+
previousContainer: DropListRef;
374374
distance: Point;
375375
dropPoint: Point;
376376
isPointerOverContainer: boolean;
@@ -384,12 +384,12 @@ export class DragRef<T = any> {
384384
event: MouseEvent | TouchEvent;
385385
}>;
386386
readonly entered: Subject<{
387-
container: DropListRefInternal;
387+
container: DropListRef;
388388
item: DragRef;
389389
currentIndex: number;
390390
}>;
391391
readonly exited: Subject<{
392-
container: DropListRefInternal;
392+
container: DropListRef;
393393
item: DragRef;
394394
}>;
395395
getFreeDragPosition(): Readonly<Point>;
@@ -425,7 +425,7 @@ export class DragRef<T = any> {
425425
}>;
426426
withBoundaryElement(boundaryElement: ElementRef<HTMLElement> | HTMLElement | null): this;
427427
withDirection(direction: Direction): this;
428-
_withDropContainer(container: DropListRefInternal): void;
428+
_withDropContainer(container: DropListRef): void;
429429
withHandles(handles: (HTMLElement | ElementRef<HTMLElement>)[]): this;
430430
withParent(parent: DragRef<unknown> | null): this;
431431
withPlaceholderTemplate(template: DragHelperTemplate | null): this;
@@ -453,18 +453,18 @@ export type DropListOrientation = 'horizontal' | 'vertical';
453453

454454
// @public
455455
export class DropListRef<T = any> {
456-
constructor(element: ElementRef<HTMLElement> | HTMLElement, _dragDropRegistry: DragDropRegistry<DragRefInternal, DropListRef>, _document: any, _ngZone: NgZone, _viewportRuler: ViewportRuler);
456+
constructor(element: ElementRef<HTMLElement> | HTMLElement, _dragDropRegistry: DragDropRegistry<DragRef, DropListRef>, _document: any, _ngZone: NgZone, _viewportRuler: ViewportRuler);
457457
autoScrollDisabled: boolean;
458458
autoScrollStep: number;
459459
readonly beforeStarted: Subject<void>;
460-
_canReceive(item: DragRefInternal, x: number, y: number): boolean;
460+
_canReceive(item: DragRef, x: number, y: number): boolean;
461461
connectedTo(connectedTo: DropListRef[]): this;
462462
data: T;
463463
disabled: boolean;
464464
dispose(): void;
465-
drop(item: DragRefInternal, currentIndex: number, previousIndex: number, previousContainer: DropListRef, isPointerOverContainer: boolean, distance: Point, dropPoint: Point, event?: MouseEvent | TouchEvent): void;
465+
drop(item: DragRef, currentIndex: number, previousIndex: number, previousContainer: DropListRef, isPointerOverContainer: boolean, distance: Point, dropPoint: Point, event?: MouseEvent | TouchEvent): void;
466466
readonly dropped: Subject<{
467-
item: DragRefInternal;
467+
item: DragRef;
468468
currentIndex: number;
469469
previousIndex: number;
470470
container: DropListRef;
@@ -475,29 +475,29 @@ export class DropListRef<T = any> {
475475
event: MouseEvent | TouchEvent;
476476
}>;
477477
element: HTMLElement | ElementRef<HTMLElement>;
478-
enter(item: DragRefInternal, pointerX: number, pointerY: number, index?: number): void;
478+
enter(item: DragRef, pointerX: number, pointerY: number, index?: number): void;
479479
readonly entered: Subject<{
480-
item: DragRefInternal;
480+
item: DragRef;
481481
container: DropListRef;
482482
currentIndex: number;
483483
}>;
484-
enterPredicate: (drag: DragRefInternal, drop: DropListRef) => boolean;
485-
exit(item: DragRefInternal): void;
484+
enterPredicate: (drag: DragRef, drop: DropListRef) => boolean;
485+
exit(item: DragRef): void;
486486
readonly exited: Subject<{
487-
item: DragRefInternal;
487+
item: DragRef;
488488
container: DropListRef;
489489
}>;
490-
getItemIndex(item: DragRefInternal): number;
490+
getItemIndex(item: DragRef): number;
491491
getScrollableParents(): readonly HTMLElement[];
492-
_getSiblingContainerFromPosition(item: DragRefInternal, x: number, y: number): DropListRef | undefined;
492+
_getSiblingContainerFromPosition(item: DragRef, x: number, y: number): DropListRef | undefined;
493493
isDragging(): boolean;
494494
_isOverContainer(x: number, y: number): boolean;
495495
isReceiving(): boolean;
496496
lockAxis: 'x' | 'y';
497497
readonly receivingStarted: Subject<{
498498
receiver: DropListRef;
499499
initiator: DropListRef;
500-
items: DragRefInternal[];
500+
items: DragRef[];
501501
}>;
502502
readonly receivingStopped: Subject<{
503503
receiver: DropListRef;
@@ -507,21 +507,21 @@ export class DropListRef<T = any> {
507507
previousIndex: number;
508508
currentIndex: number;
509509
container: DropListRef;
510-
item: DragRefInternal;
510+
item: DragRef;
511511
}>;
512512
sortingDisabled: boolean;
513-
_sortItem(item: DragRefInternal, pointerX: number, pointerY: number, pointerDelta: {
513+
_sortItem(item: DragRef, pointerX: number, pointerY: number, pointerDelta: {
514514
x: number;
515515
y: number;
516516
}): void;
517-
sortPredicate: (index: number, drag: DragRefInternal, drop: DropListRef) => boolean;
517+
sortPredicate: (index: number, drag: DragRef, drop: DropListRef) => boolean;
518518
start(): void;
519-
_startReceiving(sibling: DropListRef, items: DragRefInternal[]): void;
519+
_startReceiving(sibling: DropListRef, items: DragRef[]): void;
520520
_startScrollingIfNecessary(pointerX: number, pointerY: number): void;
521521
_stopReceiving(sibling: DropListRef): void;
522522
_stopScrolling(): void;
523523
withDirection(direction: Direction): this;
524-
withItems(items: DragRefInternal[]): this;
524+
withItems(items: DragRef[]): this;
525525
withOrientation(orientation: 'vertical' | 'horizontal'): this;
526526
withScrollableParents(elements: HTMLElement[]): this;
527527
}

0 commit comments

Comments
 (0)