@react-stately/data 3.15.1 → 3.16.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.
- package/dist/import.mjs +4 -4
- package/dist/main.js +6 -6
- package/dist/main.js.map +1 -1
- package/dist/module.js +4 -4
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +6 -0
- package/package.json +17 -13
- package/src/index.ts +6 -6
- package/dist/types.d.ts +0 -276
- package/dist/types.d.ts.map +0 -1
- package/dist/useAsyncList.main.js +0 -290
- package/dist/useAsyncList.main.js.map +0 -1
- package/dist/useAsyncList.mjs +0 -285
- package/dist/useAsyncList.module.js +0 -285
- package/dist/useAsyncList.module.js.map +0 -1
- package/dist/useListData.main.js +0 -256
- package/dist/useListData.main.js.map +0 -1
- package/dist/useListData.mjs +0 -250
- package/dist/useListData.module.js +0 -250
- package/dist/useListData.module.js.map +0 -1
- package/dist/useTreeData.main.js +0 -342
- package/dist/useTreeData.main.js.map +0 -1
- package/dist/useTreeData.mjs +0 -337
- package/dist/useTreeData.module.js +0 -337
- package/dist/useTreeData.module.js.map +0 -1
- package/src/useAsyncList.ts +0 -358
- package/src/useListData.ts +0 -430
- package/src/useTreeData.ts +0 -540
package/dist/useTreeData.mjs
DELETED
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
import {useState as $3pPTd$useState} from "react";
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
-
* governing permissions and limitations under the License.
|
|
13
|
-
*/
|
|
14
|
-
function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
|
|
15
|
-
let { initialItems: initialItems = [], initialSelectedKeys: initialSelectedKeys, getKey: getKey = (item)=>{
|
|
16
|
-
var _item_id;
|
|
17
|
-
return (_item_id = item.id) !== null && _item_id !== void 0 ? _item_id : item.key;
|
|
18
|
-
}, getChildren: getChildren = (item)=>item.children } = options;
|
|
19
|
-
// We only want to compute this on initial render.
|
|
20
|
-
let [tree, setItems] = (0, $3pPTd$useState)(()=>buildTree(initialItems, new Map()));
|
|
21
|
-
let { items: items, nodeMap: nodeMap } = tree;
|
|
22
|
-
let [selectedKeys, setSelectedKeys] = (0, $3pPTd$useState)(new Set(initialSelectedKeys || []));
|
|
23
|
-
function buildTree(initialItems = [], map, parentKey) {
|
|
24
|
-
if (initialItems == null) initialItems = [];
|
|
25
|
-
return {
|
|
26
|
-
items: initialItems.map((item)=>{
|
|
27
|
-
let node = {
|
|
28
|
-
key: getKey(item),
|
|
29
|
-
parentKey: parentKey !== null && parentKey !== void 0 ? parentKey : null,
|
|
30
|
-
value: item,
|
|
31
|
-
children: null
|
|
32
|
-
};
|
|
33
|
-
node.children = buildTree(getChildren(item), map, node.key).items;
|
|
34
|
-
map.set(node.key, node);
|
|
35
|
-
return node;
|
|
36
|
-
}),
|
|
37
|
-
nodeMap: map
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
function updateTree(items, key, update, originalMap) {
|
|
41
|
-
let node = key == null ? null : originalMap.get(key);
|
|
42
|
-
if (node == null) return {
|
|
43
|
-
items: items,
|
|
44
|
-
nodeMap: originalMap
|
|
45
|
-
};
|
|
46
|
-
let map = new Map(originalMap);
|
|
47
|
-
// Create a new node. If null, then delete the node, otherwise replace.
|
|
48
|
-
let newNode = update(node);
|
|
49
|
-
if (newNode == null) deleteNode(node, map);
|
|
50
|
-
else addNode(newNode, map);
|
|
51
|
-
// Walk up the tree and update each parent to refer to the new children.
|
|
52
|
-
while(node && node.parentKey){
|
|
53
|
-
let nextParent = map.get(node.parentKey);
|
|
54
|
-
let copy = {
|
|
55
|
-
key: nextParent.key,
|
|
56
|
-
parentKey: nextParent.parentKey,
|
|
57
|
-
value: nextParent.value,
|
|
58
|
-
children: null
|
|
59
|
-
};
|
|
60
|
-
let children = nextParent.children;
|
|
61
|
-
if (newNode == null && children) children = children.filter((c)=>c !== node);
|
|
62
|
-
var _children_map;
|
|
63
|
-
copy.children = (_children_map = children === null || children === void 0 ? void 0 : children.map((child)=>{
|
|
64
|
-
if (child === node) // newNode cannot be null here due to the above filter.
|
|
65
|
-
return newNode;
|
|
66
|
-
return child;
|
|
67
|
-
})) !== null && _children_map !== void 0 ? _children_map : null;
|
|
68
|
-
map.set(copy.key, copy);
|
|
69
|
-
newNode = copy;
|
|
70
|
-
node = nextParent;
|
|
71
|
-
}
|
|
72
|
-
if (newNode == null) items = items.filter((c)=>c !== node);
|
|
73
|
-
return {
|
|
74
|
-
items: items.map((item)=>{
|
|
75
|
-
if (item === node) // newNode cannot be null here due to the above filter.
|
|
76
|
-
return newNode;
|
|
77
|
-
return item;
|
|
78
|
-
}),
|
|
79
|
-
nodeMap: map
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
function addNode(node, map) {
|
|
83
|
-
map.set(node.key, node);
|
|
84
|
-
if (node.children) for (let child of node.children)addNode(child, map);
|
|
85
|
-
}
|
|
86
|
-
function deleteNode(node, map) {
|
|
87
|
-
map.delete(node.key);
|
|
88
|
-
if (node.children) for (let child of node.children)deleteNode(child, map);
|
|
89
|
-
}
|
|
90
|
-
return {
|
|
91
|
-
items: items,
|
|
92
|
-
selectedKeys: selectedKeys,
|
|
93
|
-
setSelectedKeys: setSelectedKeys,
|
|
94
|
-
getItem (key) {
|
|
95
|
-
return nodeMap.get(key);
|
|
96
|
-
},
|
|
97
|
-
insert (parentKey, index, ...values) {
|
|
98
|
-
setItems(({ items: items, nodeMap: originalMap })=>{
|
|
99
|
-
let { items: newNodes, nodeMap: newMap } = buildTree(values, originalMap, parentKey);
|
|
100
|
-
// If parentKey is null, insert into the root.
|
|
101
|
-
if (parentKey == null) return {
|
|
102
|
-
items: [
|
|
103
|
-
...items.slice(0, index),
|
|
104
|
-
...newNodes,
|
|
105
|
-
...items.slice(index)
|
|
106
|
-
],
|
|
107
|
-
nodeMap: newMap
|
|
108
|
-
};
|
|
109
|
-
// Otherwise, update the parent node and its ancestors.
|
|
110
|
-
return updateTree(items, parentKey, (parentNode)=>({
|
|
111
|
-
key: parentNode.key,
|
|
112
|
-
parentKey: parentNode.parentKey,
|
|
113
|
-
value: parentNode.value,
|
|
114
|
-
children: [
|
|
115
|
-
...parentNode.children.slice(0, index),
|
|
116
|
-
...newNodes,
|
|
117
|
-
...parentNode.children.slice(index)
|
|
118
|
-
]
|
|
119
|
-
}), newMap);
|
|
120
|
-
});
|
|
121
|
-
},
|
|
122
|
-
insertBefore (key, ...values) {
|
|
123
|
-
let node = nodeMap.get(key);
|
|
124
|
-
if (!node) return;
|
|
125
|
-
let parentNode = nodeMap.get(node.parentKey);
|
|
126
|
-
let nodes = parentNode ? parentNode.children : items;
|
|
127
|
-
let index = nodes.indexOf(node);
|
|
128
|
-
var _parentNode_key;
|
|
129
|
-
this.insert((_parentNode_key = parentNode === null || parentNode === void 0 ? void 0 : parentNode.key) !== null && _parentNode_key !== void 0 ? _parentNode_key : null, index, ...values);
|
|
130
|
-
},
|
|
131
|
-
insertAfter (key, ...values) {
|
|
132
|
-
let node = nodeMap.get(key);
|
|
133
|
-
if (!node) return;
|
|
134
|
-
let parentNode = nodeMap.get(node.parentKey);
|
|
135
|
-
let nodes = parentNode ? parentNode.children : items;
|
|
136
|
-
let index = nodes.indexOf(node);
|
|
137
|
-
var _parentNode_key;
|
|
138
|
-
this.insert((_parentNode_key = parentNode === null || parentNode === void 0 ? void 0 : parentNode.key) !== null && _parentNode_key !== void 0 ? _parentNode_key : null, index + 1, ...values);
|
|
139
|
-
},
|
|
140
|
-
prepend (parentKey, ...values) {
|
|
141
|
-
this.insert(parentKey, 0, ...values);
|
|
142
|
-
},
|
|
143
|
-
append (parentKey, ...values) {
|
|
144
|
-
if (parentKey == null) this.insert(null, items.length, ...values);
|
|
145
|
-
else {
|
|
146
|
-
let parentNode = nodeMap.get(parentKey);
|
|
147
|
-
if (!parentNode) return;
|
|
148
|
-
this.insert(parentKey, parentNode.children.length, ...values);
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
remove (...keys) {
|
|
152
|
-
if (keys.length === 0) return;
|
|
153
|
-
let newItems = items;
|
|
154
|
-
let prevMap = nodeMap;
|
|
155
|
-
let newTree;
|
|
156
|
-
for (let key of keys){
|
|
157
|
-
newTree = updateTree(newItems, key, ()=>null, prevMap);
|
|
158
|
-
prevMap = newTree.nodeMap;
|
|
159
|
-
newItems = newTree.items;
|
|
160
|
-
}
|
|
161
|
-
setItems(newTree);
|
|
162
|
-
let selection = new Set(selectedKeys);
|
|
163
|
-
for (let key of selectedKeys)if (!newTree.nodeMap.has(key)) selection.delete(key);
|
|
164
|
-
setSelectedKeys(selection);
|
|
165
|
-
},
|
|
166
|
-
removeSelectedItems () {
|
|
167
|
-
this.remove(...selectedKeys);
|
|
168
|
-
},
|
|
169
|
-
move (key, toParentKey, index) {
|
|
170
|
-
setItems(({ items: items, nodeMap: originalMap })=>{
|
|
171
|
-
let node = originalMap.get(key);
|
|
172
|
-
if (!node) return {
|
|
173
|
-
items: items,
|
|
174
|
-
nodeMap: originalMap
|
|
175
|
-
};
|
|
176
|
-
let { items: newItems, nodeMap: newMap } = updateTree(items, key, ()=>null, originalMap);
|
|
177
|
-
const movedNode = {
|
|
178
|
-
...node,
|
|
179
|
-
parentKey: toParentKey
|
|
180
|
-
};
|
|
181
|
-
// If parentKey is null, insert into the root.
|
|
182
|
-
if (toParentKey == null) {
|
|
183
|
-
addNode(movedNode, newMap);
|
|
184
|
-
return {
|
|
185
|
-
items: [
|
|
186
|
-
...newItems.slice(0, index),
|
|
187
|
-
movedNode,
|
|
188
|
-
...newItems.slice(index)
|
|
189
|
-
],
|
|
190
|
-
nodeMap: newMap
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
// Otherwise, update the parent node and its ancestors.
|
|
194
|
-
return updateTree(newItems, toParentKey, (parentNode)=>({
|
|
195
|
-
key: parentNode.key,
|
|
196
|
-
parentKey: parentNode.parentKey,
|
|
197
|
-
value: parentNode.value,
|
|
198
|
-
children: [
|
|
199
|
-
...parentNode.children.slice(0, index),
|
|
200
|
-
movedNode,
|
|
201
|
-
...parentNode.children.slice(index)
|
|
202
|
-
]
|
|
203
|
-
}), newMap);
|
|
204
|
-
});
|
|
205
|
-
},
|
|
206
|
-
moveBefore (key, keys) {
|
|
207
|
-
setItems((prevState)=>{
|
|
208
|
-
let { items: items, nodeMap: nodeMap } = prevState;
|
|
209
|
-
let node = nodeMap.get(key);
|
|
210
|
-
if (!node) return prevState;
|
|
211
|
-
var _node_parentKey;
|
|
212
|
-
let toParentKey = (_node_parentKey = node.parentKey) !== null && _node_parentKey !== void 0 ? _node_parentKey : null;
|
|
213
|
-
let parent = null;
|
|
214
|
-
var _nodeMap_get;
|
|
215
|
-
if (toParentKey != null) parent = (_nodeMap_get = nodeMap.get(toParentKey)) !== null && _nodeMap_get !== void 0 ? _nodeMap_get : null;
|
|
216
|
-
let toIndex = (parent === null || parent === void 0 ? void 0 : parent.children) ? parent.children.indexOf(node) : items.indexOf(node);
|
|
217
|
-
return $be2ea0343af54212$var$moveItems(prevState, keys, parent, toIndex, updateTree, addNode);
|
|
218
|
-
});
|
|
219
|
-
},
|
|
220
|
-
moveAfter (key, keys) {
|
|
221
|
-
setItems((prevState)=>{
|
|
222
|
-
let { items: items, nodeMap: nodeMap } = prevState;
|
|
223
|
-
let node = nodeMap.get(key);
|
|
224
|
-
if (!node) return prevState;
|
|
225
|
-
var _node_parentKey;
|
|
226
|
-
let toParentKey = (_node_parentKey = node.parentKey) !== null && _node_parentKey !== void 0 ? _node_parentKey : null;
|
|
227
|
-
let parent = null;
|
|
228
|
-
var _nodeMap_get;
|
|
229
|
-
if (toParentKey != null) parent = (_nodeMap_get = nodeMap.get(toParentKey)) !== null && _nodeMap_get !== void 0 ? _nodeMap_get : null;
|
|
230
|
-
let toIndex = (parent === null || parent === void 0 ? void 0 : parent.children) ? parent.children.indexOf(node) : items.indexOf(node);
|
|
231
|
-
toIndex++;
|
|
232
|
-
return $be2ea0343af54212$var$moveItems(prevState, keys, parent, toIndex, updateTree, addNode);
|
|
233
|
-
});
|
|
234
|
-
},
|
|
235
|
-
update (oldKey, newValue) {
|
|
236
|
-
setItems(({ items: items, nodeMap: originalMap })=>updateTree(items, oldKey, (oldNode)=>{
|
|
237
|
-
let node = {
|
|
238
|
-
key: oldNode.key,
|
|
239
|
-
parentKey: oldNode.parentKey,
|
|
240
|
-
value: newValue,
|
|
241
|
-
children: null
|
|
242
|
-
};
|
|
243
|
-
let tree = buildTree(getChildren(newValue), originalMap, node.key);
|
|
244
|
-
node.children = tree.items;
|
|
245
|
-
return node;
|
|
246
|
-
}, originalMap));
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
function $be2ea0343af54212$var$moveItems(state, keys, toParent, toIndex, updateTree, addNode) {
|
|
251
|
-
let { items: items, nodeMap: nodeMap } = state;
|
|
252
|
-
let parent = toParent;
|
|
253
|
-
let removeKeys = new Set(keys);
|
|
254
|
-
while((parent === null || parent === void 0 ? void 0 : parent.parentKey) != null){
|
|
255
|
-
if (removeKeys.has(parent.key)) throw new Error('Cannot move an item to be a child of itself.');
|
|
256
|
-
var _nodeMap_get;
|
|
257
|
-
parent = (_nodeMap_get = nodeMap.get(parent.parentKey)) !== null && _nodeMap_get !== void 0 ? _nodeMap_get : null;
|
|
258
|
-
}
|
|
259
|
-
let originalToIndex = toIndex;
|
|
260
|
-
let keyArray = Array.isArray(keys) ? keys : [
|
|
261
|
-
...keys
|
|
262
|
-
];
|
|
263
|
-
// depth first search to put keys in order
|
|
264
|
-
let inOrderKeys = new Map();
|
|
265
|
-
let removedItems = [];
|
|
266
|
-
let newItems = items;
|
|
267
|
-
let newMap = nodeMap;
|
|
268
|
-
let i = 0;
|
|
269
|
-
function traversal(node, { inorder: inorder, postorder: postorder }) {
|
|
270
|
-
inorder === null || inorder === void 0 ? void 0 : inorder(node);
|
|
271
|
-
var _node_children;
|
|
272
|
-
if (node != null) for (let child of (_node_children = node.children) !== null && _node_children !== void 0 ? _node_children : []){
|
|
273
|
-
traversal(child, {
|
|
274
|
-
inorder: inorder,
|
|
275
|
-
postorder: postorder
|
|
276
|
-
});
|
|
277
|
-
postorder === null || postorder === void 0 ? void 0 : postorder(child);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
function inorder(child) {
|
|
281
|
-
// in-order so we add items as we encounter them in the tree, then we can insert them in expected order later
|
|
282
|
-
if (keyArray.includes(child.key)) inOrderKeys.set(child.key, i++);
|
|
283
|
-
}
|
|
284
|
-
function postorder(child) {
|
|
285
|
-
// remove items and update the tree from the leaves and work upwards toward the root, this way
|
|
286
|
-
// we don't copy child node references from parents inadvertently
|
|
287
|
-
if (keyArray.includes(child.key)) {
|
|
288
|
-
var _toParent_key;
|
|
289
|
-
removedItems.push({
|
|
290
|
-
...newMap.get(child.key),
|
|
291
|
-
parentKey: (_toParent_key = toParent === null || toParent === void 0 ? void 0 : toParent.key) !== null && _toParent_key !== void 0 ? _toParent_key : null
|
|
292
|
-
});
|
|
293
|
-
let { items: nextItems, nodeMap: nextMap } = updateTree(newItems, child.key, ()=>null, newMap);
|
|
294
|
-
newItems = nextItems;
|
|
295
|
-
newMap = nextMap;
|
|
296
|
-
}
|
|
297
|
-
// decrement the index if the child being removed is in the target parent and before the target index
|
|
298
|
-
// the root node is special, it is null, and will not have a key, however, a parentKey can still point to it
|
|
299
|
-
if ((child.parentKey === toParent || child.parentKey === (toParent === null || toParent === void 0 ? void 0 : toParent.key)) && keyArray.includes(child.key) && ((toParent === null || toParent === void 0 ? void 0 : toParent.children) ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) toIndex--;
|
|
300
|
-
}
|
|
301
|
-
traversal({
|
|
302
|
-
children: items
|
|
303
|
-
}, {
|
|
304
|
-
inorder: inorder,
|
|
305
|
-
postorder: postorder
|
|
306
|
-
});
|
|
307
|
-
let inOrderItems = removedItems.sort((a, b)=>inOrderKeys.get(a.key) > inOrderKeys.get(b.key) ? 1 : -1);
|
|
308
|
-
// If parentKey is null, insert into the root.
|
|
309
|
-
if (!toParent || toParent.key == null) {
|
|
310
|
-
inOrderItems.forEach((movedNode)=>{
|
|
311
|
-
addNode(movedNode, newMap);
|
|
312
|
-
});
|
|
313
|
-
return {
|
|
314
|
-
items: [
|
|
315
|
-
...newItems.slice(0, toIndex),
|
|
316
|
-
...inOrderItems,
|
|
317
|
-
...newItems.slice(toIndex)
|
|
318
|
-
],
|
|
319
|
-
nodeMap: newMap
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
// Otherwise, update the parent node and its ancestors.
|
|
323
|
-
return updateTree(newItems, toParent.key, (parentNode)=>({
|
|
324
|
-
key: parentNode.key,
|
|
325
|
-
parentKey: parentNode.parentKey,
|
|
326
|
-
value: parentNode.value,
|
|
327
|
-
children: [
|
|
328
|
-
...parentNode.children.slice(0, toIndex),
|
|
329
|
-
...inOrderItems,
|
|
330
|
-
...parentNode.children.slice(toIndex)
|
|
331
|
-
]
|
|
332
|
-
}), newMap);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
export {$be2ea0343af54212$export$d14e1352e21f4a16 as useTreeData};
|
|
337
|
-
//# sourceMappingURL=useTreeData.module.js.map
|
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
import {useState as $3pPTd$useState} from "react";
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
-
* governing permissions and limitations under the License.
|
|
13
|
-
*/
|
|
14
|
-
function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
|
|
15
|
-
let { initialItems: initialItems = [], initialSelectedKeys: initialSelectedKeys, getKey: getKey = (item)=>{
|
|
16
|
-
var _item_id;
|
|
17
|
-
return (_item_id = item.id) !== null && _item_id !== void 0 ? _item_id : item.key;
|
|
18
|
-
}, getChildren: getChildren = (item)=>item.children } = options;
|
|
19
|
-
// We only want to compute this on initial render.
|
|
20
|
-
let [tree, setItems] = (0, $3pPTd$useState)(()=>buildTree(initialItems, new Map()));
|
|
21
|
-
let { items: items, nodeMap: nodeMap } = tree;
|
|
22
|
-
let [selectedKeys, setSelectedKeys] = (0, $3pPTd$useState)(new Set(initialSelectedKeys || []));
|
|
23
|
-
function buildTree(initialItems = [], map, parentKey) {
|
|
24
|
-
if (initialItems == null) initialItems = [];
|
|
25
|
-
return {
|
|
26
|
-
items: initialItems.map((item)=>{
|
|
27
|
-
let node = {
|
|
28
|
-
key: getKey(item),
|
|
29
|
-
parentKey: parentKey !== null && parentKey !== void 0 ? parentKey : null,
|
|
30
|
-
value: item,
|
|
31
|
-
children: null
|
|
32
|
-
};
|
|
33
|
-
node.children = buildTree(getChildren(item), map, node.key).items;
|
|
34
|
-
map.set(node.key, node);
|
|
35
|
-
return node;
|
|
36
|
-
}),
|
|
37
|
-
nodeMap: map
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
function updateTree(items, key, update, originalMap) {
|
|
41
|
-
let node = key == null ? null : originalMap.get(key);
|
|
42
|
-
if (node == null) return {
|
|
43
|
-
items: items,
|
|
44
|
-
nodeMap: originalMap
|
|
45
|
-
};
|
|
46
|
-
let map = new Map(originalMap);
|
|
47
|
-
// Create a new node. If null, then delete the node, otherwise replace.
|
|
48
|
-
let newNode = update(node);
|
|
49
|
-
if (newNode == null) deleteNode(node, map);
|
|
50
|
-
else addNode(newNode, map);
|
|
51
|
-
// Walk up the tree and update each parent to refer to the new children.
|
|
52
|
-
while(node && node.parentKey){
|
|
53
|
-
let nextParent = map.get(node.parentKey);
|
|
54
|
-
let copy = {
|
|
55
|
-
key: nextParent.key,
|
|
56
|
-
parentKey: nextParent.parentKey,
|
|
57
|
-
value: nextParent.value,
|
|
58
|
-
children: null
|
|
59
|
-
};
|
|
60
|
-
let children = nextParent.children;
|
|
61
|
-
if (newNode == null && children) children = children.filter((c)=>c !== node);
|
|
62
|
-
var _children_map;
|
|
63
|
-
copy.children = (_children_map = children === null || children === void 0 ? void 0 : children.map((child)=>{
|
|
64
|
-
if (child === node) // newNode cannot be null here due to the above filter.
|
|
65
|
-
return newNode;
|
|
66
|
-
return child;
|
|
67
|
-
})) !== null && _children_map !== void 0 ? _children_map : null;
|
|
68
|
-
map.set(copy.key, copy);
|
|
69
|
-
newNode = copy;
|
|
70
|
-
node = nextParent;
|
|
71
|
-
}
|
|
72
|
-
if (newNode == null) items = items.filter((c)=>c !== node);
|
|
73
|
-
return {
|
|
74
|
-
items: items.map((item)=>{
|
|
75
|
-
if (item === node) // newNode cannot be null here due to the above filter.
|
|
76
|
-
return newNode;
|
|
77
|
-
return item;
|
|
78
|
-
}),
|
|
79
|
-
nodeMap: map
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
function addNode(node, map) {
|
|
83
|
-
map.set(node.key, node);
|
|
84
|
-
if (node.children) for (let child of node.children)addNode(child, map);
|
|
85
|
-
}
|
|
86
|
-
function deleteNode(node, map) {
|
|
87
|
-
map.delete(node.key);
|
|
88
|
-
if (node.children) for (let child of node.children)deleteNode(child, map);
|
|
89
|
-
}
|
|
90
|
-
return {
|
|
91
|
-
items: items,
|
|
92
|
-
selectedKeys: selectedKeys,
|
|
93
|
-
setSelectedKeys: setSelectedKeys,
|
|
94
|
-
getItem (key) {
|
|
95
|
-
return nodeMap.get(key);
|
|
96
|
-
},
|
|
97
|
-
insert (parentKey, index, ...values) {
|
|
98
|
-
setItems(({ items: items, nodeMap: originalMap })=>{
|
|
99
|
-
let { items: newNodes, nodeMap: newMap } = buildTree(values, originalMap, parentKey);
|
|
100
|
-
// If parentKey is null, insert into the root.
|
|
101
|
-
if (parentKey == null) return {
|
|
102
|
-
items: [
|
|
103
|
-
...items.slice(0, index),
|
|
104
|
-
...newNodes,
|
|
105
|
-
...items.slice(index)
|
|
106
|
-
],
|
|
107
|
-
nodeMap: newMap
|
|
108
|
-
};
|
|
109
|
-
// Otherwise, update the parent node and its ancestors.
|
|
110
|
-
return updateTree(items, parentKey, (parentNode)=>({
|
|
111
|
-
key: parentNode.key,
|
|
112
|
-
parentKey: parentNode.parentKey,
|
|
113
|
-
value: parentNode.value,
|
|
114
|
-
children: [
|
|
115
|
-
...parentNode.children.slice(0, index),
|
|
116
|
-
...newNodes,
|
|
117
|
-
...parentNode.children.slice(index)
|
|
118
|
-
]
|
|
119
|
-
}), newMap);
|
|
120
|
-
});
|
|
121
|
-
},
|
|
122
|
-
insertBefore (key, ...values) {
|
|
123
|
-
let node = nodeMap.get(key);
|
|
124
|
-
if (!node) return;
|
|
125
|
-
let parentNode = nodeMap.get(node.parentKey);
|
|
126
|
-
let nodes = parentNode ? parentNode.children : items;
|
|
127
|
-
let index = nodes.indexOf(node);
|
|
128
|
-
var _parentNode_key;
|
|
129
|
-
this.insert((_parentNode_key = parentNode === null || parentNode === void 0 ? void 0 : parentNode.key) !== null && _parentNode_key !== void 0 ? _parentNode_key : null, index, ...values);
|
|
130
|
-
},
|
|
131
|
-
insertAfter (key, ...values) {
|
|
132
|
-
let node = nodeMap.get(key);
|
|
133
|
-
if (!node) return;
|
|
134
|
-
let parentNode = nodeMap.get(node.parentKey);
|
|
135
|
-
let nodes = parentNode ? parentNode.children : items;
|
|
136
|
-
let index = nodes.indexOf(node);
|
|
137
|
-
var _parentNode_key;
|
|
138
|
-
this.insert((_parentNode_key = parentNode === null || parentNode === void 0 ? void 0 : parentNode.key) !== null && _parentNode_key !== void 0 ? _parentNode_key : null, index + 1, ...values);
|
|
139
|
-
},
|
|
140
|
-
prepend (parentKey, ...values) {
|
|
141
|
-
this.insert(parentKey, 0, ...values);
|
|
142
|
-
},
|
|
143
|
-
append (parentKey, ...values) {
|
|
144
|
-
if (parentKey == null) this.insert(null, items.length, ...values);
|
|
145
|
-
else {
|
|
146
|
-
let parentNode = nodeMap.get(parentKey);
|
|
147
|
-
if (!parentNode) return;
|
|
148
|
-
this.insert(parentKey, parentNode.children.length, ...values);
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
remove (...keys) {
|
|
152
|
-
if (keys.length === 0) return;
|
|
153
|
-
let newItems = items;
|
|
154
|
-
let prevMap = nodeMap;
|
|
155
|
-
let newTree;
|
|
156
|
-
for (let key of keys){
|
|
157
|
-
newTree = updateTree(newItems, key, ()=>null, prevMap);
|
|
158
|
-
prevMap = newTree.nodeMap;
|
|
159
|
-
newItems = newTree.items;
|
|
160
|
-
}
|
|
161
|
-
setItems(newTree);
|
|
162
|
-
let selection = new Set(selectedKeys);
|
|
163
|
-
for (let key of selectedKeys)if (!newTree.nodeMap.has(key)) selection.delete(key);
|
|
164
|
-
setSelectedKeys(selection);
|
|
165
|
-
},
|
|
166
|
-
removeSelectedItems () {
|
|
167
|
-
this.remove(...selectedKeys);
|
|
168
|
-
},
|
|
169
|
-
move (key, toParentKey, index) {
|
|
170
|
-
setItems(({ items: items, nodeMap: originalMap })=>{
|
|
171
|
-
let node = originalMap.get(key);
|
|
172
|
-
if (!node) return {
|
|
173
|
-
items: items,
|
|
174
|
-
nodeMap: originalMap
|
|
175
|
-
};
|
|
176
|
-
let { items: newItems, nodeMap: newMap } = updateTree(items, key, ()=>null, originalMap);
|
|
177
|
-
const movedNode = {
|
|
178
|
-
...node,
|
|
179
|
-
parentKey: toParentKey
|
|
180
|
-
};
|
|
181
|
-
// If parentKey is null, insert into the root.
|
|
182
|
-
if (toParentKey == null) {
|
|
183
|
-
addNode(movedNode, newMap);
|
|
184
|
-
return {
|
|
185
|
-
items: [
|
|
186
|
-
...newItems.slice(0, index),
|
|
187
|
-
movedNode,
|
|
188
|
-
...newItems.slice(index)
|
|
189
|
-
],
|
|
190
|
-
nodeMap: newMap
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
// Otherwise, update the parent node and its ancestors.
|
|
194
|
-
return updateTree(newItems, toParentKey, (parentNode)=>({
|
|
195
|
-
key: parentNode.key,
|
|
196
|
-
parentKey: parentNode.parentKey,
|
|
197
|
-
value: parentNode.value,
|
|
198
|
-
children: [
|
|
199
|
-
...parentNode.children.slice(0, index),
|
|
200
|
-
movedNode,
|
|
201
|
-
...parentNode.children.slice(index)
|
|
202
|
-
]
|
|
203
|
-
}), newMap);
|
|
204
|
-
});
|
|
205
|
-
},
|
|
206
|
-
moveBefore (key, keys) {
|
|
207
|
-
setItems((prevState)=>{
|
|
208
|
-
let { items: items, nodeMap: nodeMap } = prevState;
|
|
209
|
-
let node = nodeMap.get(key);
|
|
210
|
-
if (!node) return prevState;
|
|
211
|
-
var _node_parentKey;
|
|
212
|
-
let toParentKey = (_node_parentKey = node.parentKey) !== null && _node_parentKey !== void 0 ? _node_parentKey : null;
|
|
213
|
-
let parent = null;
|
|
214
|
-
var _nodeMap_get;
|
|
215
|
-
if (toParentKey != null) parent = (_nodeMap_get = nodeMap.get(toParentKey)) !== null && _nodeMap_get !== void 0 ? _nodeMap_get : null;
|
|
216
|
-
let toIndex = (parent === null || parent === void 0 ? void 0 : parent.children) ? parent.children.indexOf(node) : items.indexOf(node);
|
|
217
|
-
return $be2ea0343af54212$var$moveItems(prevState, keys, parent, toIndex, updateTree, addNode);
|
|
218
|
-
});
|
|
219
|
-
},
|
|
220
|
-
moveAfter (key, keys) {
|
|
221
|
-
setItems((prevState)=>{
|
|
222
|
-
let { items: items, nodeMap: nodeMap } = prevState;
|
|
223
|
-
let node = nodeMap.get(key);
|
|
224
|
-
if (!node) return prevState;
|
|
225
|
-
var _node_parentKey;
|
|
226
|
-
let toParentKey = (_node_parentKey = node.parentKey) !== null && _node_parentKey !== void 0 ? _node_parentKey : null;
|
|
227
|
-
let parent = null;
|
|
228
|
-
var _nodeMap_get;
|
|
229
|
-
if (toParentKey != null) parent = (_nodeMap_get = nodeMap.get(toParentKey)) !== null && _nodeMap_get !== void 0 ? _nodeMap_get : null;
|
|
230
|
-
let toIndex = (parent === null || parent === void 0 ? void 0 : parent.children) ? parent.children.indexOf(node) : items.indexOf(node);
|
|
231
|
-
toIndex++;
|
|
232
|
-
return $be2ea0343af54212$var$moveItems(prevState, keys, parent, toIndex, updateTree, addNode);
|
|
233
|
-
});
|
|
234
|
-
},
|
|
235
|
-
update (oldKey, newValue) {
|
|
236
|
-
setItems(({ items: items, nodeMap: originalMap })=>updateTree(items, oldKey, (oldNode)=>{
|
|
237
|
-
let node = {
|
|
238
|
-
key: oldNode.key,
|
|
239
|
-
parentKey: oldNode.parentKey,
|
|
240
|
-
value: newValue,
|
|
241
|
-
children: null
|
|
242
|
-
};
|
|
243
|
-
let tree = buildTree(getChildren(newValue), originalMap, node.key);
|
|
244
|
-
node.children = tree.items;
|
|
245
|
-
return node;
|
|
246
|
-
}, originalMap));
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
function $be2ea0343af54212$var$moveItems(state, keys, toParent, toIndex, updateTree, addNode) {
|
|
251
|
-
let { items: items, nodeMap: nodeMap } = state;
|
|
252
|
-
let parent = toParent;
|
|
253
|
-
let removeKeys = new Set(keys);
|
|
254
|
-
while((parent === null || parent === void 0 ? void 0 : parent.parentKey) != null){
|
|
255
|
-
if (removeKeys.has(parent.key)) throw new Error('Cannot move an item to be a child of itself.');
|
|
256
|
-
var _nodeMap_get;
|
|
257
|
-
parent = (_nodeMap_get = nodeMap.get(parent.parentKey)) !== null && _nodeMap_get !== void 0 ? _nodeMap_get : null;
|
|
258
|
-
}
|
|
259
|
-
let originalToIndex = toIndex;
|
|
260
|
-
let keyArray = Array.isArray(keys) ? keys : [
|
|
261
|
-
...keys
|
|
262
|
-
];
|
|
263
|
-
// depth first search to put keys in order
|
|
264
|
-
let inOrderKeys = new Map();
|
|
265
|
-
let removedItems = [];
|
|
266
|
-
let newItems = items;
|
|
267
|
-
let newMap = nodeMap;
|
|
268
|
-
let i = 0;
|
|
269
|
-
function traversal(node, { inorder: inorder, postorder: postorder }) {
|
|
270
|
-
inorder === null || inorder === void 0 ? void 0 : inorder(node);
|
|
271
|
-
var _node_children;
|
|
272
|
-
if (node != null) for (let child of (_node_children = node.children) !== null && _node_children !== void 0 ? _node_children : []){
|
|
273
|
-
traversal(child, {
|
|
274
|
-
inorder: inorder,
|
|
275
|
-
postorder: postorder
|
|
276
|
-
});
|
|
277
|
-
postorder === null || postorder === void 0 ? void 0 : postorder(child);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
function inorder(child) {
|
|
281
|
-
// in-order so we add items as we encounter them in the tree, then we can insert them in expected order later
|
|
282
|
-
if (keyArray.includes(child.key)) inOrderKeys.set(child.key, i++);
|
|
283
|
-
}
|
|
284
|
-
function postorder(child) {
|
|
285
|
-
// remove items and update the tree from the leaves and work upwards toward the root, this way
|
|
286
|
-
// we don't copy child node references from parents inadvertently
|
|
287
|
-
if (keyArray.includes(child.key)) {
|
|
288
|
-
var _toParent_key;
|
|
289
|
-
removedItems.push({
|
|
290
|
-
...newMap.get(child.key),
|
|
291
|
-
parentKey: (_toParent_key = toParent === null || toParent === void 0 ? void 0 : toParent.key) !== null && _toParent_key !== void 0 ? _toParent_key : null
|
|
292
|
-
});
|
|
293
|
-
let { items: nextItems, nodeMap: nextMap } = updateTree(newItems, child.key, ()=>null, newMap);
|
|
294
|
-
newItems = nextItems;
|
|
295
|
-
newMap = nextMap;
|
|
296
|
-
}
|
|
297
|
-
// decrement the index if the child being removed is in the target parent and before the target index
|
|
298
|
-
// the root node is special, it is null, and will not have a key, however, a parentKey can still point to it
|
|
299
|
-
if ((child.parentKey === toParent || child.parentKey === (toParent === null || toParent === void 0 ? void 0 : toParent.key)) && keyArray.includes(child.key) && ((toParent === null || toParent === void 0 ? void 0 : toParent.children) ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) toIndex--;
|
|
300
|
-
}
|
|
301
|
-
traversal({
|
|
302
|
-
children: items
|
|
303
|
-
}, {
|
|
304
|
-
inorder: inorder,
|
|
305
|
-
postorder: postorder
|
|
306
|
-
});
|
|
307
|
-
let inOrderItems = removedItems.sort((a, b)=>inOrderKeys.get(a.key) > inOrderKeys.get(b.key) ? 1 : -1);
|
|
308
|
-
// If parentKey is null, insert into the root.
|
|
309
|
-
if (!toParent || toParent.key == null) {
|
|
310
|
-
inOrderItems.forEach((movedNode)=>{
|
|
311
|
-
addNode(movedNode, newMap);
|
|
312
|
-
});
|
|
313
|
-
return {
|
|
314
|
-
items: [
|
|
315
|
-
...newItems.slice(0, toIndex),
|
|
316
|
-
...inOrderItems,
|
|
317
|
-
...newItems.slice(toIndex)
|
|
318
|
-
],
|
|
319
|
-
nodeMap: newMap
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
// Otherwise, update the parent node and its ancestors.
|
|
323
|
-
return updateTree(newItems, toParent.key, (parentNode)=>({
|
|
324
|
-
key: parentNode.key,
|
|
325
|
-
parentKey: parentNode.parentKey,
|
|
326
|
-
value: parentNode.value,
|
|
327
|
-
children: [
|
|
328
|
-
...parentNode.children.slice(0, toIndex),
|
|
329
|
-
...inOrderItems,
|
|
330
|
-
...parentNode.children.slice(toIndex)
|
|
331
|
-
]
|
|
332
|
-
}), newMap);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
export {$be2ea0343af54212$export$d14e1352e21f4a16 as useTreeData};
|
|
337
|
-
//# sourceMappingURL=useTreeData.module.js.map
|