Skip to content

Commit 4952b85

Browse files
committed
add getHandlerId() to connection objects, for use with the test backend
1 parent 20bebee commit 4952b85

File tree

1 file changed

+117
-108
lines changed

1 file changed

+117
-108
lines changed

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

Lines changed: 117 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ 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, DragSourceOptions, DragPreviewOptions } from './connectors';
11+
import {
12+
DropTargetConnector,
13+
DragSourceConnector,
14+
DragSourceOptions,
15+
DragPreviewOptions
16+
} from './connectors';
1217
import { Subscription, SubscriptionLike } from 'rxjs';
1318

1419
/**
1520
* A base type to represent a DOM connection.
1621
*/
1722
export interface ConnectionBase<TMonitor> extends SubscriptionLike {
18-
19-
/**
23+
/**
2024
* A connection maintains a subscription to `dnd-core`'s drag state
2125
* changes. This function is how you are notified of those changes.
2226
*
@@ -47,41 +51,40 @@ export interface ConnectionBase<TMonitor> extends SubscriptionLike {
4751
4852
* You can also subscribe one-by-one, with `isDragging$ = listen(m => m.isDragging())`.
4953
*/
50-
listen<O>(mapTo: (monitor: TMonitor) => O): Observable<O>;
51-
52-
/**
53-
* This method **MUST** be called, however you choose to, when `ngOnDestroy()` fires.
54-
* If you don't, you will leave subscriptions hanging around that will fire
55-
* callbacks on components that no longer exist.
56-
*/
57-
unsubscribe(): void;
58-
59-
/**
60-
* Same as RxJS Subscription.add().
61-
* Useful, for example, for writing wrappers for the {@link SkyhookDndService} methods,
62-
* which might internally listen()/subscribe to {@link DropTargetSpec#hover} and provide
63-
* a convenient callback after you hover without dropping or exiting for a specified
64-
* duration. That would require the following pattern:
65-
*
66-
* ```typescript
67-
* function wrapper(dndService, types, spec, callback) {
68-
* let subj = new Subject();
69-
* let dt = dndService.dropTarget(types, {
70-
* ...spec,
71-
* hover: monitor => {
72-
* subj.next();
73-
* spec.hover && spec.hover(monitor);
74-
* }
75-
* });
76-
* // runs the callback until the returned connection
77-
* // is destroyed via unsubscribe()
78-
* dt.add(subj.pipe( ... ).subscribe(callback))
79-
* return dt;
80-
* }
81-
* ```
82-
*/
83-
add(teardown: TeardownLogic): Subscription;
84-
54+
listen<O>(mapTo: (monitor: TMonitor) => O): Observable<O>;
55+
56+
/**
57+
* This method **MUST** be called, however you choose to, when `ngOnDestroy()` fires.
58+
* If you don't, you will leave subscriptions hanging around that will fire
59+
* callbacks on components that no longer exist.
60+
*/
61+
unsubscribe(): void;
62+
63+
/**
64+
* Same as RxJS Subscription.add().
65+
* Useful, for example, for writing wrappers for the {@link SkyhookDndService} methods,
66+
* which might internally listen()/subscribe to {@link DropTargetSpec#hover} and provide
67+
* a convenient callback after you hover without dropping or exiting for a specified
68+
* duration. That would require the following pattern:
69+
*
70+
* ```typescript
71+
* function wrapper(dndService, types, spec, callback) {
72+
* let subj = new Subject();
73+
* let dt = dndService.dropTarget(types, {
74+
* ...spec,
75+
* hover: monitor => {
76+
* subj.next();
77+
* spec.hover && spec.hover(monitor);
78+
* }
79+
* });
80+
* // runs the callback until the returned connection
81+
* // is destroyed via unsubscribe()
82+
* dt.add(subj.pipe( ... ).subscribe(callback))
83+
* return dt;
84+
* }
85+
* ```
86+
*/
87+
add(teardown: TeardownLogic): Subscription;
8588
}
8689

8790
/**
@@ -90,30 +93,31 @@ export interface ConnectionBase<TMonitor> extends SubscriptionLike {
9093
*
9194
* To create one, refer to {@link SkyhookDndService#dropTarget}.
9295
*/
93-
export interface DropTarget<
94-
Item = {},
95-
DropResult = {}
96-
> extends ConnectionBase<DropTargetMonitor<Item, DropResult>> {
97-
98-
/** Use this method to have a dynamically typed target. If no type has
99-
* previously been set, it creates the subscription and allows the
100-
* `[dragSource]` DOM element to be connected. If you do not need to
101-
* dynamically update the type, you can set it once via the
102-
* {@link DropTargetSpec#types} property.
103-
*
104-
* See {@link DragSource#setType} for an example of how to set
105-
* a dynamic type, for it is very similar here.
106-
*/
107-
setTypes(type: TypeOrTypeArray): void;
108-
109-
/** This function allows you to connect a DOM node to your `DropTarget`.
110-
* You will not usually need to call this directly;
111-
* it is more easily handled by the directives.
112-
*
113-
* The subscription returned is automatically unsubscribed when the connection is made.
114-
* This may be immediate if the `DropTarget` already has a type.
115-
*/
116-
connectDropTarget(elementOrNode: Node): Subscription;
96+
export interface DropTarget<Item = {}, DropResult = {}>
97+
extends ConnectionBase<DropTargetMonitor<Item, DropResult>> {
98+
/** Use this method to have a dynamically typed target. If no type has
99+
* previously been set, it creates the subscription and allows the
100+
* `[dragSource]` DOM element to be connected. If you do not need to
101+
* dynamically update the type, you can set it once via the
102+
* {@link DropTargetSpec#types} property.
103+
*
104+
* See {@link DragSource#setType} for an example of how to set
105+
* a dynamic type, for it is very similar here.
106+
*/
107+
setTypes(type: TypeOrTypeArray): void;
108+
109+
/** This function allows you to connect a DOM node to your `DropTarget`.
110+
* You will not usually need to call this directly;
111+
* it is more easily handled by the directives.
112+
*
113+
* The subscription returned is automatically unsubscribed when the connection is made.
114+
* This may be immediate if the `DropTarget` already has a type.
115+
*/
116+
connectDropTarget(elementOrNode: Node): Subscription;
117+
118+
/**
119+
* Returns the drop target ID that can be used to simulate the drag and drop events with the testing backend. */
120+
getHandlerId(): any;
117121
}
118122

119123
/**
@@ -123,12 +127,9 @@ You do not have to connect it to a DOM element if that's all you want.
123127
124128
To create one, refer to {@link SkyhookDndService#dragSource}.
125129
*/
126-
export interface DragSource<
127-
Item,
128-
DropResult = {}
129-
> extends ConnectionBase<DragSourceMonitor<Item, DropResult>> {
130-
131-
/** Use this method to have a dynamically typed source. If no type has
130+
export interface DragSource<Item, DropResult = {}>
131+
extends ConnectionBase<DragSourceMonitor<Item, DropResult>> {
132+
/** Use this method to have a dynamically typed source. If no type has
132133
* previously been set, it creates the subscription and allows the
133134
* `[dragSource]` DOM element to be connected. If you do not need to
134135
* dynamically update the type, you can set it once via the
@@ -160,32 +161,42 @@ export interface DragSource<
160161
* });
161162
162163
*/
163-
setType(type: string | symbol): void;
164-
165-
/** This function allows you to connect a DOM node to your `DragSource`.
166-
* You will not usually need to call this directly;
167-
* it is more easily handled by the directives.
168-
*
169-
* The subscription returned is automatically unsubscribed when the connection is made.
170-
* This may be immediate if the `DragSource` already has a type.
171-
*/
172-
connectDragSource(elementOrNode: Node, options?: DragSourceOptions): Subscription;
173-
174-
/** This function allows you to connect a DOM node to your `DragSource` as a **preview**.
175-
* You will not usually need to call this directly;
176-
* it is more easily handled by the directives.
177-
*
178-
* You might use an `ElementRef.nativeElement`, or even an
179-
* [`Image`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image).
180-
*
181-
* const img = new Image();
182-
* img.onload = this.source.connectDragPreview(img);
183-
* img.src = '...';
184-
*
185-
* The subscription returned is automatically unsubscribed when the connection is made.
186-
* This may be immediate if the `DragSource` already has a type.
187-
*/
188-
connectDragPreview(elementOrNode: Node, options?: DragPreviewOptions): Subscription;
164+
setType(type: string | symbol): void;
165+
166+
/** This function allows you to connect a DOM node to your `DragSource`.
167+
* You will not usually need to call this directly;
168+
* it is more easily handled by the directives.
169+
*
170+
* The subscription returned is automatically unsubscribed when the connection is made.
171+
* This may be immediate if the `DragSource` already has a type.
172+
*/
173+
connectDragSource(
174+
elementOrNode: Node,
175+
options?: DragSourceOptions
176+
): Subscription;
177+
178+
/** This function allows you to connect a DOM node to your `DragSource` as a **preview**.
179+
* You will not usually need to call this directly;
180+
* it is more easily handled by the directives.
181+
*
182+
* You might use an `ElementRef.nativeElement`, or even an
183+
* [`Image`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image).
184+
*
185+
* const img = new Image();
186+
* img.onload = this.source.connectDragPreview(img);
187+
* img.src = '...';
188+
*
189+
* The subscription returned is automatically unsubscribed when the connection is made.
190+
* This may be immediate if the `DragSource` already has a type.
191+
*/
192+
connectDragPreview(
193+
elementOrNode: Node,
194+
options?: DragPreviewOptions
195+
): Subscription;
196+
197+
/**
198+
* Returns the drag source ID that can be used to simulate the drag and drop events with the testing backend. */
199+
getHandlerId(): any;
189200
}
190201

191202
/**
@@ -241,18 +252,16 @@ you must handle every draggable `type` in a drag layer to have any previews at
241252
all.
242253
243254
*/
244-
export interface DragLayer<Item = any> extends ConnectionBase<DragLayerMonitor<Item>> {
245-
246-
/** For listen functions in general, see {@link DragSource.listen}.
247-
*
248-
* This listen function is called any time the global drag state
249-
* changes, including the coordinate changes, so that your component can
250-
* provide a timely updated custom drag preview. You can ask the monitor for
251-
* the client coordinates of the dragged item. Read the {@link DragLayerMonitor}
252-
* docs to see all the different possibile coordinates you might subscribe
253-
* to.
254-
*/
255-
listen<O>(mapTo: (monitor: DragLayerMonitor<Item>) => O): Observable<O>;
256-
255+
export interface DragLayer<Item = any>
256+
extends ConnectionBase<DragLayerMonitor<Item>> {
257+
/** For listen functions in general, see {@link DragSource.listen}.
258+
*
259+
* This listen function is called any time the global drag state
260+
* changes, including the coordinate changes, so that your component can
261+
* provide a timely updated custom drag preview. You can ask the monitor for
262+
* the client coordinates of the dragged item. Read the {@link DragLayerMonitor}
263+
* docs to see all the different possibile coordinates you might subscribe
264+
* to.
265+
*/
266+
listen<O>(mapTo: (monitor: DragLayerMonitor<Item>) => O): Observable<O>;
257267
}
258-

0 commit comments

Comments
 (0)