-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathDropTargetKeyboardNavigation.ts
More file actions
294 lines (266 loc) · 9.02 KB
/
DropTargetKeyboardNavigation.ts
File metadata and controls
294 lines (266 loc) · 9.02 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
import {Collection, DropTarget, Key, KeyboardDelegate, Node} from '@react-types/shared';
export function navigate(
keyboardDelegate: KeyboardDelegate,
collection: Collection<Node<unknown>>,
target: DropTarget | null | undefined,
direction: 'left' | 'right' | 'up' | 'down',
rtl = false,
wrap = false
): DropTarget | null {
switch (direction) {
case 'left':
return rtl
? nextDropTarget(keyboardDelegate, collection, target, wrap, 'left')
: previousDropTarget(keyboardDelegate, collection, target, wrap, 'left');
case 'right':
return rtl
? previousDropTarget(keyboardDelegate, collection, target, wrap, 'right')
: nextDropTarget(keyboardDelegate, collection, target, wrap, 'right');
case 'up':
return previousDropTarget(keyboardDelegate, collection, target, wrap);
case 'down':
return nextDropTarget(keyboardDelegate, collection, target, wrap);
}
}
function nextDropTarget(
keyboardDelegate: KeyboardDelegate,
collection: Collection<Node<unknown>>,
target: DropTarget | null | undefined,
wrap = false,
horizontal: 'left' | 'right' | null = null
): DropTarget | null {
if (!target) {
return {
type: 'root'
};
}
if (target.type === 'root') {
let nextKey = keyboardDelegate.getFirstKey?.() ?? null;
if (nextKey != null) {
return {
type: 'item',
key: nextKey,
dropPosition: 'before'
};
}
return null;
}
if (target.type === 'item') {
let nextKey: Key | null | undefined = null;
if (horizontal) {
nextKey = horizontal === 'right' ? keyboardDelegate.getKeyRightOf?.(target.key) : keyboardDelegate.getKeyLeftOf?.(target.key);
} else {
nextKey = keyboardDelegate.getKeyBelow?.(target.key);
}
let nextCollectionKey = getNextItem(collection, target.key, key => collection.getKeyAfter(key));
// If the keyboard delegate did not move to the next key in the collection,
// jump to that key with the same drop position. Otherwise, try the other
// drop positions on the current key first.
if (nextKey != null && nextKey !== nextCollectionKey) {
return {
type: 'item',
key: nextKey,
dropPosition: target.dropPosition
};
}
switch (target.dropPosition) {
case 'before': {
return {
type: 'item',
key: target.key,
dropPosition: 'on'
};
}
case 'on': {
// If there are nested items, traverse to them prior to the "after" position of this target.
// If the next key is on the same level, then its "before" position is equivalent to this item's "after" position.
let targetNode = collection.getItem(target.key);
let nextNode = nextKey != null ? collection.getItem(nextKey) : null;
if (targetNode && nextNode && nextNode.level >= targetNode.level) {
return {
type: 'item',
key: nextNode.key,
dropPosition: 'before'
};
}
return {
type: 'item',
key: target.key,
dropPosition: 'after'
};
}
case 'after': {
// If this is the last sibling in a level, traverse to the parent.
let targetNode = collection.getItem(target.key);
let nextItemInSameLevel = targetNode?.nextKey != null ? collection.getItem(targetNode.nextKey) : null;
while (nextItemInSameLevel != null && nextItemInSameLevel.type !== 'item') {
nextItemInSameLevel = nextItemInSameLevel.nextKey != null ? collection.getItem(nextItemInSameLevel.nextKey) : null;
}
if (targetNode && nextItemInSameLevel == null && targetNode.parentKey != null) {
// If the parent item has an item after it, use the "before" position.
let parentNode = collection.getItem(targetNode.parentKey);
const nextNode = parentNode?.nextKey != null ? collection.getItem(parentNode.nextKey) : null;
if (nextNode?.type === 'item') {
return {
type: 'item',
key: nextNode.key,
dropPosition: 'before'
};
}
if (parentNode?.type === 'item') {
return {
type: 'item',
key: parentNode.key,
dropPosition: 'after'
};
}
}
if (nextItemInSameLevel) {
return {
type: 'item',
key: nextItemInSameLevel.key,
dropPosition: 'on'
};
}
}
}
}
if (wrap) {
return {
type: 'root'
};
}
return null;
}
function previousDropTarget(
keyboardDelegate: KeyboardDelegate,
collection: Collection<Node<unknown>>,
target: DropTarget | null | undefined,
wrap = false,
horizontal: 'left' | 'right' | null = null
): DropTarget | null {
// Start after the last root-level item.
if (!target || (wrap && target.type === 'root')) {
// Keyboard delegate gets the deepest item but we want the shallowest.
let prevKey: Key | null = null;
let lastKey = keyboardDelegate.getLastKey?.();
while (lastKey != null) {
let node = collection.getItem(lastKey);
if (node?.type !== 'item') {
break;
}
prevKey = lastKey;
lastKey = node?.parentKey;
}
if (prevKey != null) {
return {
type: 'item',
key: prevKey,
dropPosition: 'after'
};
}
return null;
}
if (target.type === 'item') {
let prevKey: Key | null | undefined = null;
if (horizontal) {
prevKey = horizontal === 'left' ? keyboardDelegate.getKeyLeftOf?.(target.key) : keyboardDelegate.getKeyRightOf?.(target.key);
} else {
prevKey = keyboardDelegate.getKeyAbove?.(target.key);
}
let prevCollectionKey = getNextItem(collection, target.key, key => collection.getKeyBefore(key));
// If the keyboard delegate did not move to the next key in the collection,
// jump to that key with the same drop position. Otherwise, try the other
// drop positions on the current key first.
if (prevKey != null && prevKey !== prevCollectionKey) {
return {
type: 'item',
key: prevKey,
dropPosition: target.dropPosition
};
}
switch (target.dropPosition) {
case 'before': {
// Move after the last child of the previous item.
let targetNode = collection.getItem(target.key);
if (targetNode && targetNode.prevKey != null) {
let lastChild = getLastChild(collection, targetNode.prevKey);
if (lastChild) {
return lastChild;
}
}
if (prevKey != null) {
return {
type: 'item',
key: prevKey,
dropPosition: 'on'
};
}
return {
type: 'root'
};
}
case 'on': {
return {
type: 'item',
key: target.key,
dropPosition: 'before'
};
}
case 'after': {
// Move after the last child of this item.
let lastChild = getLastChild(collection, target.key);
if (lastChild) {
return lastChild;
}
return {
type: 'item',
key: target.key,
dropPosition: 'on'
};
}
}
}
if (target.type !== 'root') {
return {
type: 'root'
};
}
return null;
}
function getLastChild(collection: Collection<Node<unknown>>, key: Key): DropTarget | null {
// getChildNodes still returns child tree items even when the item is collapsed.
// Checking if the next item has a greater level is a silly way to determine if the item is expanded.
let targetNode = collection.getItem(key);
let nextKey = getNextItem(collection, key, key => collection.getKeyAfter(key));
let nextNode = nextKey != null ? collection.getItem(nextKey) : null;
if (targetNode && nextNode && nextNode.level > targetNode.level) {
let lastChild: Node<unknown> | null = null;
if ('lastChildKey' in targetNode) {
lastChild = targetNode.lastChildKey != null ? collection.getItem(targetNode.lastChildKey) : null;
while (lastChild && lastChild.type !== 'item' && lastChild.prevKey != null) {
lastChild = collection.getItem(lastChild.prevKey)!;
}
} else {
lastChild = Array.from(targetNode.childNodes).findLast(item => item.type === 'item') || null;
}
if (lastChild) {
return {
type: 'item',
key: lastChild.key,
dropPosition: 'after'
};
}
}
return null;
}
// Find the next or previous item in a collection, skipping over other types of nodes (e.g. content).
function getNextItem(collection: Collection<Node<unknown>>, key: Key, getNextKey: (key: Key) => Key | null): Key | null {
let nextCollectionKey = getNextKey(key);
let nextCollectionNode = nextCollectionKey != null ? collection.getItem(nextCollectionKey) : null;
while (nextCollectionNode && nextCollectionNode.type !== 'item') {
nextCollectionKey = getNextKey(nextCollectionNode.key);
nextCollectionNode = nextCollectionKey != null ? collection.getItem(nextCollectionKey) : null;
}
return nextCollectionKey;
}