Skip to content

Commit 5fd5ffa

Browse files
committed
API change: connect(c => ...) => connectDragPreview etc
1 parent abea81f commit 5fd5ffa

File tree

12 files changed

+64
-30
lines changed

12 files changed

+64
-30
lines changed

docs/Connecting-to-DOM.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ touching the inputs / selecting text / making unwanted changes.
7272

7373
1. Create an image element with `const img = new Image(); img.src = "...";`
7474
2. Use `img.onload = () => { ... }` to wait for it to load. Inside the onload
75-
callback, run `someDragSourceConnection.connect(c => c.dragPreview(img))`.
75+
callback, run `someDragSourceConnection.connectDragPreview(img)`.
7676

77-
See [[DragSource.connect]], and [[DragSourceConnector.dragPreview]] for options.
77+
See [[DragSource.connectDragPreview]] and [[DragPreviewOptions]] for options.
7878

7979

docs/Tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ export class KnightComponent {
733733
const img = new Image();
734734
img.src = // ... long 'data:image/png;base64' url
735735
// regular 'https://' URLs work here too
736-
img.onload = () => this.knightSource.connect(c => c.dragPreview(img));
736+
img.onload = () => this.knightSource.connectDragPreview(img);
737737
}
738738
}
739739
```

packages/angular-skyhook/src/connection-types.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { DragSourceMonitor } from './source-monitor';
88
import { TypeOrTypeArray } from './type-ish';
99
import { Observable, TeardownLogic } from 'rxjs';
1010
import { DragLayerMonitor } from './layer-monitor';
11-
import { DropTargetConnector, DragSourceConnector } from './connectors';
11+
import { DropTargetConnector, DragSourceConnector, DragSourceOptions, DragPreviewOptions } from './connectors';
1212
import { Subscription, SubscriptionLike } from 'rxjs';
1313

1414
/** @private */
@@ -84,17 +84,6 @@ export interface ConnectionBase<TMonitor> extends SubscriptionLike {
8484

8585
/** @private */
8686
export interface Connection<TMonitor, TConnector> extends ConnectionBase<TMonitor> {
87-
88-
/** This function allows you to connect a DOM node to your `DragSource`. It
89-
* is formulated as a callback so that connecting may be deferred until the
90-
* connection has a type. You will not usually need to call this directly;
91-
* it is more easily handled by the directives.
92-
*
93-
* To connect a DOM node, you must use one of the methods provided by the
94-
* `connector` object in the callback.
95-
*/
96-
connect(fn: (connector: TConnector) => void): Subscription;
97-
9887
}
9988

10089
/**
@@ -116,6 +105,14 @@ export interface DropTarget extends Connection<DropTargetMonitor, DropTargetConn
116105
*/
117106
setTypes(type: TypeOrTypeArray): void;
118107

108+
/** This function allows you to connect a DOM node to your `DropTarget`.
109+
* You will not usually need to call this directly;
110+
* it is more easily handled by the directives.
111+
*
112+
* The subscription returned is automatically unsubscribed when the connection is made.
113+
* This may be immediate if the `DropTarget` already has a type.
114+
*/
115+
connectDropTarget(elementOrNode: Node): Subscription;
119116
}
120117

121118
/**
@@ -157,6 +154,31 @@ export interface DragSource extends Connection<DragSourceMonitor,
157154
158155
*/
159156
setType(type: string|symbol): void;
157+
158+
/** This function allows you to connect a DOM node to your `DragSource`.
159+
* You will not usually need to call this directly;
160+
* it is more easily handled by the directives.
161+
*
162+
* The subscription returned is automatically unsubscribed when the connection is made.
163+
* This may be immediate if the `DragSource` already has a type.
164+
*/
165+
connectDragSource(elementOrNode: Node, options?: DragSourceOptions): Subscription;
166+
167+
/** This function allows you to connect a DOM node to your `DragSource` as a **preview**.
168+
* You will not usually need to call this directly;
169+
* it is more easily handled by the directives.
170+
*
171+
* You might use an `ElementRef.nativeElement`, or even an
172+
* [`Image`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image).
173+
*
174+
* const img = new Image();
175+
* img.onload = this.source.connectDragPreview(img);
176+
* img.src = '...';
177+
*
178+
* The subscription returned is automatically unsubscribed when the connection is made.
179+
* This may be immediate if the `DragSource` already has a type.
180+
*/
181+
connectDragPreview(elementOrNode: Node, options?: DragPreviewOptions): Subscription;
160182
}
161183

162184
/**

packages/angular-skyhook/src/connectors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ export interface DragPreviewOptions {
6363

6464
}
6565

66-
/** Connects a drop target to a DOM element */
66+
/** @private Connects a drop target to a DOM element */
6767
export interface DropTargetConnector {
6868
dropTarget ( elementOrNode: any): void;
6969
}
7070

71-
/** Connects a drag source to a DOM element, either as the source itself or as
71+
/** @private Connects a drag source to a DOM element, either as the source itself or as
7272
* a drag preview */
7373
export interface DragSourceConnector {
7474

packages/angular-skyhook/src/dnd.directive.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class DropTargetDirective extends DndDirective {
9191
}
9292

9393
protected callHooks(conn: DropTarget): Subscription {
94-
return conn.connect(c => c.dropTarget(this.elRef.nativeElement));
94+
return conn.connectDropTarget(this.elRef.nativeElement);
9595
}
9696
}
9797

@@ -118,7 +118,7 @@ export class DragSourceDirective extends DndDirective {
118118
}
119119

120120
protected callHooks(conn: DragSource): Subscription {
121-
return conn.connect(c => c.dragSource(this.elRef.nativeElement, this.dragSourceOptions));
121+
return conn.connectDragSource(this.elRef.nativeElement, this.dragSourceOptions);
122122
}
123123

124124
}
@@ -138,7 +138,7 @@ export class DragPreviewDirective extends DndDirective {
138138
}
139139

140140
protected callHooks(conn: DragSource) {
141-
return conn.connect(c => c.dragPreview(this.elRef.nativeElement, this.dragPreviewOptions));
141+
return conn.connectDragPreview(this.elRef.nativeElement, this.dragPreviewOptions);
142142
}
143143
}
144144

packages/angular-skyhook/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export {
2121
} from './connection-types';
2222

2323
export {
24-
DropTargetConnector, DragSourceConnector, DragSourceOptions, DragPreviewOptions
24+
DragSourceOptions, DragPreviewOptions
2525
} from './connectors';
2626

2727
export { DRAG_DROP_BACKEND, DRAG_DROP_MANAGER } from './tokens';

packages/angular-skyhook/src/internal/connection-factory.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { areCollectsEqual } from '../utils/areCollectsEqual';
1616
import { DropTargetMonitor } from '../target-monitor';
1717
import { DragSourceMonitor } from '../source-monitor';
1818
import * as t from '../connection-types';
19-
import { DropTargetConnector, DragSourceConnector } from '../connectors';
19+
import { DropTargetConnector, DragSourceConnector, DragSourceOptions, DragPreviewOptions } from '../connectors';
2020
import { scheduleMicroTaskAfter } from './scheduleMicroTaskAfter';
2121

2222
export interface FactoryArgs<TMonitor, TConnector> {
@@ -139,6 +139,18 @@ function connectionFactory<TMonitor extends DragSourceMonitor | DropTargetMonito
139139
});
140140
}
141141

142+
connectDropTarget(node: Node): Subscription {
143+
return this.connect(c => (c as any as DropTargetConnector).dropTarget(node));
144+
}
145+
146+
connectDragSource(node: Node, options: DragSourceOptions): Subscription {
147+
return this.connect(c => (c as any as DragSourceConnector).dragSource(node, options));
148+
}
149+
150+
connectDragPreview(node: Node, options: DragPreviewOptions): Subscription {
151+
return this.connect(c => (c as any as DragSourceConnector).dragPreview(node, options));
152+
}
153+
142154
setTypes(type: TypeOrTypeArray) {
143155
// must run inside skyhookZone, so things like timers firing after a long hover with touch backend
144156
// will cause change detection (via executing a macro or event task)

packages/examples/src/app/chessboard/knight.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class KnightComponent {
3131
ngOnInit() {
3232
const img = new Image();
3333
img.src = horseImage;
34-
img.onload = () => this.knightSource.connect(c => c.dragPreview(img));
34+
img.onload = () => this.knightSource.connectDragPreview(img);
3535
}
3636

3737
ngOnDestroy() {

packages/examples/src/app/customize/handles-previews/custom-preview.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class CustomPreview {
3535
var img = new Image();
3636
img.onload = () => {
3737
console.log("connecting image");
38-
this.source.connect(c => c.dragPreview(img));
38+
this.source.connectDragPreview(img);
3939
}
4040
// img.src = "https://angular.io/assets/images/logos/angular/angular.png";
4141
img.src =

packages/examples/src/app/drag-layer/draggable-box/draggable-box.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ export class DraggableBoxComponent {
3232
constructor(private dnd: SkyhookDndService) { }
3333

3434
ngOnInit() {
35-
this.source.connect(c => c.dragPreview(getEmptyImage(), {
35+
this.source.connectDragPreview(getEmptyImage(), {
3636
// for ie11 compat with DragLayer
3737
captureDraggingState: true
38-
}));
38+
});
3939
}
4040

4141
ngOnDestroy() {

0 commit comments

Comments
 (0)