UNPKG

9.35 kBTypeScriptView Raw
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13import {Key, RefObject} from 'react';
14
15export interface DragDropEvent {
16 /** The x coordinate of the event, relative to the target element. */
17 x: number,
18 /** The y coordinate of the event, relative to the target element. */
19 y: number
20}
21
22export type DropOperation = 'copy' | 'link' | 'move' | 'cancel';
23
24export interface DragItem {
25 [type: string]: string
26}
27
28export interface DragStartEvent extends DragDropEvent {
29 /** The event type. */
30 type: 'dragstart'
31}
32
33export interface DragMoveEvent extends DragDropEvent {
34 /** The event type. */
35 type: 'dragmove'
36}
37
38export interface DragEndEvent extends DragDropEvent {
39 /** The event type. */
40 type: 'dragend',
41 /** The drop operation that occurred. */
42 dropOperation: DropOperation
43}
44
45export interface DropEnterEvent extends DragDropEvent {
46 /** The event type. */
47 type: 'dropenter'
48}
49
50export interface DropMoveEvent extends DragDropEvent {
51 /** The event type. */
52 type: 'dropmove'
53}
54
55export interface DropActivateEvent extends DragDropEvent {
56 /** The event type. */
57 type: 'dropactivate'
58}
59
60export interface DropExitEvent extends DragDropEvent {
61 /** The event type. */
62 type: 'dropexit'
63}
64
65export interface TextDropItem {
66 /** The item kind. */
67 kind: 'text',
68 /**
69 * The drag types available for this item.
70 * These are often mime types, but may be custom app-specific types.
71 */
72 types: Set<string>,
73 /** Returns the data for the given type as a string. */
74 getText(type: string): Promise<string>
75}
76
77export interface FileDropItem {
78 /** The item kind. */
79 kind: 'file',
80 /** The file type (usually a mime type). */
81 type: string,
82 /** The file name. */
83 name: string,
84 /** Returns the contents of the file as a blob. */
85 getFile(): Promise<File>,
86 /** Returns the contents of the file as a string. */
87 getText(): Promise<string>
88}
89
90export interface DirectoryDropItem {
91 /** The item kind. */
92 kind: 'directory',
93 /** The directory name. */
94 name: string,
95 /** Returns the entries contained within the directory. */
96 getEntries(): AsyncIterable<FileDropItem | DirectoryDropItem>
97}
98
99export type DropItem = TextDropItem | FileDropItem | DirectoryDropItem;
100
101export interface DropEvent extends DragDropEvent {
102 /** The event type. */
103 type: 'drop',
104 /** The drop operation that should occur. */
105 dropOperation: DropOperation,
106 /** The dropped items. */
107 items: DropItem[]
108}
109
110export type DropPosition = 'on' | 'before' | 'after';
111export interface RootDropTarget {
112 /** The event type. */
113 type: 'root'
114}
115
116export interface ItemDropTarget {
117 /** The drop target type. */
118 type: 'item',
119 /** The item key. */
120 key: Key,
121 /** The drop position relative to the item. */
122 dropPosition: DropPosition
123}
124
125export type DropTarget = RootDropTarget | ItemDropTarget;
126
127export interface DroppableCollectionEnterEvent extends DropEnterEvent {
128 /** The drop target. */
129 target: DropTarget
130}
131
132export interface DroppableCollectionMoveEvent extends DropMoveEvent {
133 /** The drop target. */
134 target: DropTarget
135}
136
137export interface DroppableCollectionActivateEvent extends DropActivateEvent {
138 /** The drop target. */
139 target: DropTarget
140}
141
142export interface DroppableCollectionExitEvent extends DropExitEvent {
143 /** The drop target. */
144 target: DropTarget
145}
146
147export interface DroppableCollectionDropEvent extends DropEvent {
148 /** The drop target. */
149 target: DropTarget
150}
151
152export interface DroppableCollectionInsertDropEvent {
153 /** The dropped items. */
154 items: DropItem[],
155 /** The drop operation that should occur. */
156 dropOperation: DropOperation,
157 /** The drop target. */
158 target: ItemDropTarget
159}
160
161export interface DroppableCollectionRootDropEvent {
162 /** The dropped items. */
163 items: DropItem[],
164 /** The drop operation that should occur. */
165 dropOperation: DropOperation
166}
167
168export interface DroppableCollectionOnItemDropEvent {
169 /** The dropped items. */
170 items: DropItem[],
171 /** The drop operation that should occur. */
172 dropOperation: DropOperation,
173 /** Whether the drag originated within the same collection as the drop. */
174 isInternal: boolean,
175 /** The drop target. */
176 target: ItemDropTarget
177}
178
179export interface DroppableCollectionReorderEvent {
180 /** The keys of the items that were reordered. */
181 keys: Set<Key>,
182 /** The drop operation that should occur. */
183 dropOperation: DropOperation,
184 /** The drop target. */
185 target: ItemDropTarget
186}
187
188export interface DragTypes {
189 /** Returns whether the drag contains data of the given type. */
190 has(type: string | symbol): boolean
191}
192
193export interface DropTargetDelegate {
194 /**
195 * Returns a drop target within a collection for the given x and y coordinates.
196 * The point is provided relative to the top left corner of the collection container.
197 * A drop target can be checked to see if it is valid using the provided `isValidDropTarget` function.
198 */
199 getDropTargetFromPoint(x: number, y: number, isValidDropTarget: (target: DropTarget) => boolean): DropTarget | null
200}
201
202export interface DroppableCollectionUtilityOptions {
203 /**
204 * The drag types that the droppable collection accepts. If the collection accepts directories, include `DIRECTORY_DRAG_TYPE` in your array of allowed types.
205 * @default 'all'
206 */
207 acceptedDragTypes?: 'all' | Array<string | symbol>,
208 /**
209 * Handler that is called when external items are dropped "between" items.
210 */
211 onInsert?: (e: DroppableCollectionInsertDropEvent) => void,
212 /**
213 * Handler that is called when external items are dropped on the droppable collection's root.
214 */
215 onRootDrop?: (e: DroppableCollectionRootDropEvent) => void,
216 /**
217 * Handler that is called when items are dropped "on" an item.
218 */
219 onItemDrop?: (e: DroppableCollectionOnItemDropEvent) => void,
220 /**
221 * Handler that is called when items are reordered via drag in the source collection.
222 */
223 onReorder?: (e: DroppableCollectionReorderEvent) => void,
224 /**
225 * A function returning whether a given target in the droppable collection is a valid "on" drop target for the current drag types.
226 */
227 shouldAcceptItemDrop?: (target: ItemDropTarget, types: DragTypes) => boolean
228}
229
230export interface DroppableCollectionBaseProps {
231 /** Handler that is called when a valid drag enters a drop target. */
232 onDropEnter?: (e: DroppableCollectionEnterEvent) => void,
233 /**
234 * Handler that is called after a valid drag is held over a drop target for a period of time.
235 * @private
236 */
237 onDropActivate?: (e: DroppableCollectionActivateEvent) => void,
238 /** Handler that is called when a valid drag exits a drop target. */
239 onDropExit?: (e: DroppableCollectionExitEvent) => void,
240 /**
241 * Handler that is called when a valid drag is dropped on a drop target. When defined, this overrides other
242 * drop handlers such as `onInsert`, and `onItemDrop`.
243 */
244 onDrop?: (e: DroppableCollectionDropEvent) => void,
245 /**
246 * A function returning the drop operation to be performed when items matching the given types are dropped
247 * on the drop target.
248 */
249 getDropOperation?: (target: DropTarget, types: DragTypes, allowedOperations: DropOperation[]) => DropOperation
250}
251
252export interface DroppableCollectionProps extends DroppableCollectionUtilityOptions, DroppableCollectionBaseProps {}
253
254export interface DraggableCollectionStartEvent extends DragStartEvent {
255 /** The keys of the items that were dragged. */
256 keys: Set<Key>
257}
258
259export interface DraggableCollectionMoveEvent extends DragMoveEvent {
260 /** The keys of the items that were dragged. */
261 keys: Set<Key>
262}
263
264export interface DraggableCollectionEndEvent extends DragEndEvent {
265 /** The keys of the items that were dragged. */
266 keys: Set<Key>,
267 /** Whether the drop ended within the same collection as it originated. */
268 isInternal: boolean
269}
270
271export type DragPreviewRenderer = (items: DragItem[], callback: (node: HTMLElement) => void) => void;
272
273export interface DraggableCollectionProps {
274 /** Handler that is called when a drag operation is started. */
275 onDragStart?: (e: DraggableCollectionStartEvent) => void,
276 /** Handler that is called when the drag is moved. */
277 onDragMove?: (e: DraggableCollectionMoveEvent) => void,
278 /** Handler that is called when the drag operation is ended, either as a result of a drop or a cancellation. */
279 onDragEnd?: (e: DraggableCollectionEndEvent) => void,
280 /** A function that returns the items being dragged. */
281 getItems: (keys: Set<Key>) => DragItem[],
282 /** The ref of the element that will be rendered as the drag preview while dragging. */
283 preview?: RefObject<DragPreviewRenderer>,
284 /** Function that returns the drop operations that are allowed for the dragged items. If not provided, all drop operations are allowed. */
285 getAllowedDropOperations?: () => DropOperation[]
286}