@react-stately/dnd 3.7.3 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,226 +0,0 @@
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
-
13
- import {Collection, DragTypes, DropOperation, DroppableCollectionProps, DropTarget, ItemDropTarget, Key, Node} from '@react-types/shared';
14
- import {MultipleSelectionManager} from '@react-stately/selection';
15
- import {useCallback, useRef, useState} from 'react';
16
-
17
- interface DropOperationEvent {
18
- target: DropTarget,
19
- types: DragTypes,
20
- allowedOperations: DropOperation[],
21
- isInternal: boolean,
22
- draggingKeys: Set<Key>
23
- }
24
-
25
- export interface DroppableCollectionStateOptions extends Omit<DroppableCollectionProps, 'onDropMove' | 'onDropActivate'> {
26
- /** A collection of items. */
27
- collection: Collection<Node<unknown>>,
28
- /** An interface for reading and updating multiple selection state. */
29
- selectionManager: MultipleSelectionManager,
30
- /** Whether the drop events should be disabled. */
31
- isDisabled?: boolean
32
- }
33
-
34
- export interface DroppableCollectionState {
35
- /** A collection of items. */
36
- collection: Collection<Node<unknown>>,
37
- /** An interface for reading and updating multiple selection state. */
38
- selectionManager: MultipleSelectionManager,
39
- /** The current drop target. */
40
- target: DropTarget | null,
41
- /** Whether drop events are disabled. */
42
- isDisabled?: boolean,
43
- /** Sets the current drop target. */
44
- setTarget(target: DropTarget | null): void,
45
- /** Returns whether the given target is equivalent to the current drop target. */
46
- isDropTarget(target: DropTarget | null): boolean,
47
- /** Returns the drop operation for the given parameters. */
48
- getDropOperation(e: DropOperationEvent): DropOperation
49
- }
50
-
51
- /**
52
- * Manages state for a droppable collection.
53
- */
54
- export function useDroppableCollectionState(props: DroppableCollectionStateOptions): DroppableCollectionState {
55
- let {
56
- acceptedDragTypes = 'all',
57
- isDisabled,
58
- onInsert,
59
- onRootDrop,
60
- onItemDrop,
61
- onReorder,
62
- onMove,
63
- shouldAcceptItemDrop,
64
- collection,
65
- selectionManager,
66
- onDropEnter,
67
- getDropOperation,
68
- onDrop
69
- } = props;
70
- let [target, setTarget] = useState<DropTarget | null>(null);
71
- let targetRef = useRef<DropTarget | null>(null);
72
-
73
- let getOppositeTarget = (target: ItemDropTarget): ItemDropTarget | null => {
74
- if (target.dropPosition === 'before') {
75
- let node = collection.getItem(target.key);
76
- return node && node.prevKey != null
77
- ? {type: 'item', key: node.prevKey, dropPosition: 'after'}
78
- : null;
79
- } else if (target.dropPosition === 'after') {
80
- let node = collection.getItem(target.key);
81
- return node && node.nextKey != null
82
- ? {type: 'item', key: node.nextKey, dropPosition: 'before'}
83
- : null;
84
- }
85
- return null;
86
- };
87
-
88
- let defaultGetDropOperation = useCallback((e: DropOperationEvent) => {
89
- let {
90
- target,
91
- types,
92
- allowedOperations,
93
- isInternal,
94
- draggingKeys
95
- } = e;
96
-
97
- if (isDisabled || !target) {
98
- return 'cancel';
99
- }
100
-
101
- if (acceptedDragTypes === 'all' || acceptedDragTypes.some(type => types.has(type))) {
102
- let isValidInsert = onInsert && target.type === 'item' && !isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after');
103
- let isValidReorder = onReorder
104
- && target.type === 'item'
105
- && isInternal
106
- && (target.dropPosition === 'before' || target.dropPosition === 'after')
107
- && isDraggingWithinParent(collection, target, draggingKeys);
108
-
109
- let isItemDropAllowed = target.type !== 'item'
110
- || target.dropPosition !== 'on'
111
- || (!shouldAcceptItemDrop || shouldAcceptItemDrop(target, types));
112
-
113
- let isValidMove = onMove
114
- && target.type === 'item'
115
- && isInternal
116
- && isItemDropAllowed;
117
-
118
- // Feedback was that internal root drop was weird so preventing that from happening
119
- let isValidRootDrop = onRootDrop && target.type === 'root' && !isInternal;
120
-
121
- // Automatically prevent items (i.e. folders) from being dropped on themselves.
122
- let isValidOnItemDrop = onItemDrop
123
- && target.type === 'item'
124
- && target.dropPosition === 'on'
125
- && !(isInternal && target.key != null && draggingKeys.has(target.key))
126
- && isItemDropAllowed;
127
-
128
- if (onDrop || isValidInsert || isValidReorder || isValidMove || isValidRootDrop || isValidOnItemDrop) {
129
- if (getDropOperation) {
130
- return getDropOperation(target, types, allowedOperations);
131
- } else {
132
- return allowedOperations[0];
133
- }
134
- }
135
- }
136
-
137
- return 'cancel';
138
- }, [isDisabled, collection, acceptedDragTypes, getDropOperation, onInsert, onRootDrop, onItemDrop, shouldAcceptItemDrop, onReorder, onMove, onDrop]);
139
-
140
- return {
141
- collection,
142
- selectionManager,
143
- isDisabled,
144
- target,
145
- setTarget(newTarget) {
146
- if (this.isDropTarget(newTarget)) {
147
- return;
148
- }
149
-
150
- let target = targetRef.current;
151
- if (target && typeof props.onDropExit === 'function') {
152
- props.onDropExit({
153
- type: 'dropexit',
154
- x: 0, // todo
155
- y: 0,
156
- target
157
- });
158
- }
159
-
160
- if (newTarget && typeof onDropEnter === 'function') {
161
- onDropEnter({
162
- type: 'dropenter',
163
- x: 0, // todo
164
- y: 0,
165
- target: newTarget
166
- });
167
- }
168
-
169
- targetRef.current = newTarget ?? null;
170
- setTarget(newTarget ?? null);
171
- },
172
- isDropTarget(dropTarget) {
173
- let target = targetRef.current;
174
- if (!target || !dropTarget) {
175
- return false;
176
- }
177
- if (isEqualDropTarget(dropTarget, target)) {
178
- return true;
179
- }
180
-
181
- // Check if the targets point at the same point between two items, one referring before, and the other after.
182
- if (
183
- dropTarget?.type === 'item' &&
184
- target?.type === 'item' &&
185
- dropTarget.key !== target.key &&
186
- dropTarget.dropPosition !== target.dropPosition &&
187
- dropTarget.dropPosition !== 'on' &&
188
- target.dropPosition !== 'on'
189
- ) {
190
- return isEqualDropTarget(getOppositeTarget(dropTarget), target) ||
191
- isEqualDropTarget(dropTarget, getOppositeTarget(target));
192
- }
193
-
194
- return false;
195
- },
196
- getDropOperation(e) {
197
- return defaultGetDropOperation(e);
198
- }
199
- };
200
- }
201
-
202
- function isEqualDropTarget(a?: DropTarget | null, b?: DropTarget | null) {
203
- if (!a) {
204
- return !b;
205
- }
206
-
207
- switch (a.type) {
208
- case 'root':
209
- return b?.type === 'root';
210
- case 'item':
211
- return b?.type === 'item' && b?.key === a.key && b?.dropPosition === a.dropPosition;
212
- }
213
- }
214
-
215
- function isDraggingWithinParent(collection: Collection<Node<unknown>>, target: ItemDropTarget, draggingKeys: Set<Key>) {
216
- let targetNode = collection.getItem(target.key);
217
-
218
- for (let key of draggingKeys) {
219
- let node = collection.getItem(key);
220
- if (node?.parentKey !== targetNode?.parentKey) {
221
- return false;
222
- }
223
- }
224
-
225
- return true;
226
- }