-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathTreeDropTargetDelegate.ts
More file actions
306 lines (261 loc) · 10.5 KB
/
TreeDropTargetDelegate.ts
File metadata and controls
306 lines (261 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import {Direction, DropTarget, DropTargetDelegate, ItemDropTarget, Key, Node, RootDropTarget} from '@react-types/shared';
interface TreeCollection<T> extends Iterable<Node<T>> {
getItem(key: Key): Node<T> | null,
getChildren?(key: Key): Iterable<Node<T>>,
getKeyAfter(key: Key): Key | null,
getKeyBefore(key: Key): Key | null
}
interface TreeState<T> {
collection: TreeCollection<T>,
expandedKeys: Set<Key>
}
interface PointerTracking {
lastY: number,
lastX: number,
yDirection: 'up' | 'down' | null,
xDirection: 'left' | 'right' | null,
boundaryContext: {
parentKey: Key,
lastSwitchY: number,
lastSwitchX: number,
preferredTargetIndex?: number
} | null
}
const X_SWITCH_THRESHOLD = 10;
const Y_SWITCH_THRESHOLD = 5;
export class TreeDropTargetDelegate<T> {
private delegate: DropTargetDelegate | null = null;
private state: TreeState<T> | null = null;
private direction: Direction = 'ltr';
private pointerTracking: PointerTracking = {
lastY: 0,
lastX: 0,
yDirection: null,
xDirection: null,
boundaryContext: null
};
setup(delegate: DropTargetDelegate, state: TreeState<T>, direction: Direction): void {
this.delegate = delegate;
this.state = state;
this.direction = direction;
}
getDropTargetFromPoint(x: number, y: number, isValidDropTarget: (target: DropTarget) => boolean): DropTarget | null {
let baseTarget = this.delegate!.getDropTargetFromPoint(x, y, isValidDropTarget);
if (!baseTarget || baseTarget.type === 'root') {
return baseTarget;
}
return this.resolveDropTarget(baseTarget, x, y, isValidDropTarget);
}
private resolveDropTarget(
target: ItemDropTarget,
x: number,
y: number,
isValidDropTarget: (target: DropTarget) => boolean
): RootDropTarget | ItemDropTarget | null {
let tracking = this.pointerTracking;
// Calculate movement directions
let deltaY = y - tracking.lastY;
let deltaX = x - tracking.lastX;
let currentYMovement: 'up' | 'down' | null = tracking.yDirection;
let currentXMovement: 'left' | 'right' | null = tracking.xDirection;
if (Math.abs(deltaY) > Y_SWITCH_THRESHOLD) {
currentYMovement = deltaY > 0 ? 'down' : 'up';
tracking.yDirection = currentYMovement;
tracking.lastY = y;
}
if (Math.abs(deltaX) > X_SWITCH_THRESHOLD) {
currentXMovement = deltaX > 0 ? 'right' : 'left';
tracking.xDirection = currentXMovement;
tracking.lastX = x;
}
// Normalize to 'after'
if (target.dropPosition === 'before') {
let keyBefore = this.state!.collection.getKeyBefore(target.key);
if (keyBefore != null) {
let convertedTarget = {
type: 'item',
key: keyBefore,
dropPosition: 'after'
} as const;
if (isValidDropTarget(convertedTarget)) {
target = convertedTarget;
}
}
}
let potentialTargets = this.getPotentialTargets(target, isValidDropTarget);
if (potentialTargets.length === 0) {
return {type: 'root'};
}
let resolvedItemTarget: ItemDropTarget;
if (potentialTargets.length > 1) {
resolvedItemTarget = this.selectTarget(potentialTargets, target, x, y, currentYMovement, currentXMovement);
} else {
resolvedItemTarget = potentialTargets[0];
// Reset boundary context since we're not in a boundary case
tracking.boundaryContext = null;
}
return resolvedItemTarget;
}
// Returns potential targets for an ambiguous drop position (e.g. after the last child of a parent, or after the parent itself)
// Ordered by level, from innermost to outermost.
private getPotentialTargets(originalTarget: ItemDropTarget, isValidDropTarget: (target: DropTarget) => boolean): ItemDropTarget[] {
if (originalTarget.dropPosition === 'on') {
return [originalTarget];
}
let target = originalTarget;
let collection = this.state!.collection;
let currentItem = collection.getItem(target.key);
while (currentItem && currentItem?.type !== 'item' && currentItem.nextKey != null) {
target.key = currentItem.nextKey;
currentItem = collection.getItem(currentItem.nextKey);
}
let potentialTargets = [target];
// If target has children and is expanded, use "before first child"
if (currentItem && currentItem.hasChildNodes && this.state!.expandedKeys.has(currentItem.key) && collection.getChildren && target.dropPosition === 'after') {
// Find the first item child (traverse keys directly instead of using collection.getChildren, which may only include cells).
let firstChildItemNode = currentItem.firstChildKey != null ? collection.getItem(currentItem.firstChildKey) : null;
while (firstChildItemNode && firstChildItemNode.type !== 'item') {
firstChildItemNode = firstChildItemNode.nextKey != null ? collection.getItem(firstChildItemNode.nextKey) : null;
}
if (firstChildItemNode?.type === 'item') {
const beforeFirstChildTarget = {
type: 'item',
key: firstChildItemNode.key,
dropPosition: 'before'
} as const;
if (isValidDropTarget(beforeFirstChildTarget)) {
return [beforeFirstChildTarget];
} else {
return [];
}
}
}
if (currentItem?.nextKey != null) {
return [originalTarget];
}
// Walk up the parent chain to find ancestors that are the last child at their level
let parentKey = currentItem?.parentKey;
let ancestorTargets: ItemDropTarget[] = [];
while (parentKey) {
let parentItem = collection.getItem(parentKey);
let nextItem = parentItem?.nextKey ? collection.getItem(parentItem.nextKey) : null;
let isLastChildAtLevel = !nextItem || nextItem.parentKey !== parentKey;
if (isLastChildAtLevel) {
let afterParentTarget = {
type: 'item',
key: parentKey,
dropPosition: 'after'
} as const;
if (isValidDropTarget(afterParentTarget)) {
ancestorTargets.push(afterParentTarget);
}
if (nextItem) {
break;
}
}
parentKey = parentItem?.parentKey;
}
if (ancestorTargets.length > 0) {
potentialTargets.push(...ancestorTargets);
}
// Handle converting "after" to "before next" for non-ambiguous cases
if (potentialTargets.length === 1) {
let nextKey = collection.getKeyAfter(target.key);
let nextNode = nextKey ? collection.getItem(nextKey) : null;
if (nextKey != null && nextNode && currentItem && nextNode.type === 'item' && nextNode.level != null && currentItem.level != null && nextNode.level > currentItem.level) {
let beforeTarget = {
type: 'item',
key: nextKey,
dropPosition: 'before'
} as const;
if (isValidDropTarget(beforeTarget)) {
return [beforeTarget];
}
}
}
return potentialTargets.filter(isValidDropTarget);
}
private selectTarget(
potentialTargets: ItemDropTarget[],
originalTarget: ItemDropTarget,
x: number,
y: number,
currentYMovement: 'up' | 'down' | null,
currentXMovement: 'left' | 'right' | null
): ItemDropTarget {
if (potentialTargets.length < 2) {
return potentialTargets[0];
}
let tracking = this.pointerTracking;
let currentItem = this.state!.collection.getItem(originalTarget.key);
let parentKey = currentItem?.parentKey;
if (!parentKey) {
return potentialTargets[0];
}
// More than 1 potential target - use Y for initial target, then X for switching levels
// Initialize boundary context if needed
if (!tracking.boundaryContext || tracking.boundaryContext.parentKey !== parentKey) {
// If entering from below, start with outer-most
let initialTargetIndex = tracking.yDirection === 'up' ? potentialTargets.length - 1 : 0;
tracking.boundaryContext = {
parentKey,
preferredTargetIndex: initialTargetIndex,
lastSwitchY: y,
lastSwitchX: x
};
}
let boundaryContext = tracking.boundaryContext;
let distanceFromLastXSwitch = Math.abs(x - boundaryContext.lastSwitchX);
let distanceFromLastYSwitch = Math.abs(y - boundaryContext.lastSwitchY);
// Switch between targets based on Y movement
if (distanceFromLastYSwitch > Y_SWITCH_THRESHOLD && currentYMovement) {
let currentIndex = boundaryContext.preferredTargetIndex || 0;
if (currentYMovement === 'down' && currentIndex === 0) {
// Moving down from inner-most, switch to outer-most
boundaryContext.preferredTargetIndex = potentialTargets.length - 1;
} else if (currentYMovement === 'up' && currentIndex === potentialTargets.length - 1) {
// Moving up from outer-most, switch to inner-most
boundaryContext.preferredTargetIndex = 0;
}
// Reset x tracking so that moving diagonally doesn't cause flickering.
tracking.xDirection = null;
}
// X movement controls level selection
if (distanceFromLastXSwitch > X_SWITCH_THRESHOLD && currentXMovement) {
let currentTargetIndex = boundaryContext.preferredTargetIndex || 0;
if (currentXMovement === 'left') {
if (this.direction === 'ltr') {
// LTR: left = move to higher level in tree (increase index)
if (currentTargetIndex < potentialTargets.length - 1) {
boundaryContext.preferredTargetIndex = currentTargetIndex + 1;
boundaryContext.lastSwitchX = x;
}
} else {
// RTL: left = move to lower level in tree (decrease index)
if (currentTargetIndex > 0) {
boundaryContext.preferredTargetIndex = currentTargetIndex - 1;
boundaryContext.lastSwitchX = x;
}
}
} else if (currentXMovement === 'right') {
if (this.direction === 'ltr') {
// LTR: right = move to lower level in tree (decrease index)
if (currentTargetIndex > 0) {
boundaryContext.preferredTargetIndex = currentTargetIndex - 1;
boundaryContext.lastSwitchX = x;
}
} else {
// RTL: right = move to higher level in tree (increase index)
if (currentTargetIndex < potentialTargets.length - 1) {
boundaryContext.preferredTargetIndex = currentTargetIndex + 1;
boundaryContext.lastSwitchX = x;
}
}
}
// Reset y tracking so that moving diagonally doesn't cause flickering.
tracking.yDirection = null;
}
let targetIndex = Math.max(0, Math.min(boundaryContext.preferredTargetIndex || 0, potentialTargets.length - 1));
return potentialTargets[targetIndex];
}
}