UNPKG

7.01 kBTypeScriptView Raw
1export declare type Identifier = string | symbol;
2export declare type SourceType = Identifier;
3export declare type TargetType = Identifier | Identifier[];
4export declare type Unsubscribe = () => void;
5export declare type Listener = () => void;
6export interface XYCoord {
7 x: number;
8 y: number;
9}
10export declare enum HandlerRole {
11 SOURCE = "SOURCE",
12 TARGET = "TARGET"
13}
14export interface Backend {
15 setup(): void;
16 teardown(): void;
17 connectDragSource(sourceId: any, node?: any, options?: any): Unsubscribe;
18 connectDragPreview(sourceId: any, node?: any, options?: any): Unsubscribe;
19 connectDropTarget(targetId: any, node?: any, options?: any): Unsubscribe;
20 profile(): Record<string, number>;
21}
22export interface DragDropMonitor {
23 subscribeToStateChange(listener: Listener, options?: {
24 handlerIds: Identifier[] | undefined;
25 }): Unsubscribe;
26 subscribeToOffsetChange(listener: Listener): Unsubscribe;
27 canDragSource(sourceId: Identifier | undefined): boolean;
28 canDropOnTarget(targetId: Identifier | undefined): boolean;
29 /**
30 * Returns true if a drag operation is in progress, and either the owner initiated the drag, or its isDragging()
31 * is defined and returns true.
32 */
33 isDragging(): boolean;
34 isDraggingSource(sourceId: Identifier | undefined): boolean;
35 isOverTarget(targetId: Identifier | undefined, options?: {
36 shallow?: boolean;
37 }): boolean;
38 /**
39 * Returns a string or a symbol identifying the type of the current dragged item. Returns null if no item is being dragged.
40 */
41 getItemType(): Identifier | null;
42 /**
43 * Returns a plain object representing the currently dragged item. Every drag source must specify it by returning an object
44 * from its beginDrag() method. Returns null if no item is being dragged.
45 */
46 getItem(): any;
47 getSourceId(): Identifier | null;
48 getTargetIds(): Identifier[];
49 /**
50 * Returns a plain object representing the last recorded drop result. The drop targets may optionally specify it by returning an
51 * object from their drop() methods. When a chain of drop() is dispatched for the nested targets, bottom up, any parent that
52 * explicitly returns its own result from drop() overrides the child drop result previously set by the child. Returns null if
53 * called outside endDrag().
54 */
55 getDropResult(): any;
56 /**
57 * Returns true if some drop target has handled the drop event, false otherwise. Even if a target did not return a drop result,
58 * didDrop() returns true. Use it inside endDrag() to test whether any drop target has handled the drop. Returns false if called
59 * outside endDrag().
60 */
61 didDrop(): boolean;
62 isSourcePublic(): boolean | null;
63 /**
64 * Returns the { x, y } client offset of the pointer at the time when the current drag operation has started.
65 * Returns null if no item is being dragged.
66 */
67 getInitialClientOffset(): XYCoord | null;
68 /**
69 * Returns the { x, y } client offset of the drag source component's root DOM node at the time when the current drag
70 * operation has started. Returns null if no item is being dragged.
71 */
72 getInitialSourceClientOffset(): XYCoord | null;
73 /**
74 * Returns the last recorded { x, y } client offset of the pointer while a drag operation is in progress.
75 * Returns null if no item is being dragged.
76 */
77 getClientOffset(): XYCoord | null;
78 /**
79 * Returns the projected { x, y } client offset of the drag source component's root DOM node, based on its position at the time
80 * when the current drag operation has started, and the movement difference. Returns null if no item is being dragged.
81 */
82 getSourceClientOffset(): XYCoord | null;
83 /**
84 * Returns the { x, y } difference between the last recorded client offset of the pointer and the client offset when the current
85 * drag operation has started. Returns null if no item is being dragged.
86 */
87 getDifferenceFromInitialOffset(): XYCoord | null;
88}
89export interface HandlerRegistry {
90 addSource(type: SourceType, source: DragSource): Identifier;
91 addTarget(type: TargetType, target: DropTarget): Identifier;
92 containsHandler(handler: DragSource | DropTarget): boolean;
93 getSource(sourceId: Identifier, includePinned?: boolean): DragSource;
94 getSourceType(sourceId: Identifier): SourceType;
95 getTargetType(targetId: Identifier): TargetType;
96 getTarget(targetId: Identifier): DropTarget;
97 isSourceId(handlerId: Identifier): boolean;
98 isTargetId(handlerId: Identifier): boolean;
99 removeSource(sourceId: Identifier): void;
100 removeTarget(targetId: Identifier): void;
101 pinSource(sourceId: Identifier): void;
102 unpinSource(): void;
103}
104export interface Action<Payload> {
105 type: Identifier;
106 payload: Payload;
107}
108export interface SentinelAction {
109 type: Identifier;
110}
111export declare type ActionCreator<Payload> = (args: any[]) => Action<Payload>;
112export interface BeginDragOptions {
113 publishSource?: boolean;
114 clientOffset?: XYCoord;
115 getSourceClientOffset?: (sourceId: Identifier) => XYCoord;
116}
117export interface InitCoordsPayload {
118 clientOffset: XYCoord | null;
119 sourceClientOffset: XYCoord | null;
120}
121export interface BeginDragPayload {
122 itemType: Identifier;
123 item: any;
124 sourceId: Identifier;
125 clientOffset: XYCoord | null;
126 sourceClientOffset: XYCoord | null;
127 isSourcePublic: boolean;
128}
129export interface HoverPayload {
130 targetIds: Identifier[];
131 clientOffset: XYCoord | null;
132}
133export interface HoverOptions {
134 clientOffset?: XYCoord;
135}
136export interface DropPayload {
137 dropResult: any;
138}
139export interface TargetIdPayload {
140 targetId: Identifier;
141}
142export interface SourceIdPayload {
143 sourceId: Identifier;
144}
145export interface DragDropActions {
146 beginDrag(sourceIds?: Identifier[], options?: any): Action<BeginDragPayload> | undefined;
147 publishDragSource(): SentinelAction | undefined;
148 hover(targetIds: Identifier[], options?: any): Action<HoverPayload>;
149 drop(options?: any): void;
150 endDrag(): SentinelAction;
151}
152export interface DragDropManager {
153 getMonitor(): DragDropMonitor;
154 getBackend(): Backend;
155 getRegistry(): HandlerRegistry;
156 getActions(): DragDropActions;
157 dispatch(action: any): void;
158}
159export declare type BackendFactory = (manager: DragDropManager, globalContext?: any, configuration?: any) => Backend;
160export interface DragSource {
161 beginDrag(monitor: DragDropMonitor, targetId: Identifier): void;
162 endDrag(monitor: DragDropMonitor, targetId: Identifier): void;
163 canDrag(monitor: DragDropMonitor, targetId: Identifier): boolean;
164 isDragging(monitor: DragDropMonitor, targetId: Identifier): boolean;
165}
166export interface DropTarget {
167 canDrop(monitor: DragDropMonitor, targetId: Identifier): boolean;
168 hover(monitor: DragDropMonitor, targetId: Identifier): void;
169 drop(monitor: DragDropMonitor, targetId: Identifier): any;
170}