@react-stately/data 3.12.3 → 3.13.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/types.d.ts CHANGED
@@ -244,6 +244,18 @@ export interface TreeData<T extends object> {
244
244
  * @param index - The index within the new parent to insert at.
245
245
  */
246
246
  move(key: Key, toParentKey: Key | null, index: number): void;
247
+ /**
248
+ * Moves one or more items before a given key.
249
+ * @param key - The key of the item to move the items before.
250
+ * @param keys - The keys of the items to move.
251
+ */
252
+ moveBefore(key: Key, keys: Iterable<Key>): void;
253
+ /**
254
+ * Moves one or more items after a given key.
255
+ * @param key - The key of the item to move the items after.
256
+ * @param keys - The keys of the items to move.
257
+ */
258
+ moveAfter(key: Key, keys: Iterable<Key>): void;
247
259
  /**
248
260
  * Updates an item in the tree.
249
261
  * @param key - The key of the item to update.
@@ -1 +1 @@
1
- {"mappings":";AAeA,6BAA6B,CAAC;IAC5B,iCAAiC;IACjC,YAAY,CAAC,EAAE,CAAC,EAAE,CAAC;IACnB,iDAAiD;IACjD,mBAAmB,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC5C,8CAA8C;IAC9C,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,+DAA+D;IAC/D,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,GAAG,CAAC;IAC1B,8EAA8E;IAC9E,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAA;CAClD;AAED,0BAA0B,CAAC;IACzB,6BAA6B;IAC7B,KAAK,EAAE,CAAC,EAAE,CAAC;IAEX,4DAA4D;IAC5D,YAAY,EAAE,SAAS,CAAC;IAExB,8BAA8B;IAC9B,eAAe,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI,CAAC;IAEvC,+BAA+B;IAC/B,UAAU,EAAE,MAAM,CAAC;IAEnB,4BAA4B;IAC5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B;;;OAGG;IACH,mBAAmB,IAAI,IAAI,CAAC;IAE5B;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,GAAG,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7D;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,CAAA;CACpC;AAED;;;GAGG;AACH,4BAA4B,CAAC,SAAS,MAAM,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CA6QlF","sources":["packages/@react-stately/data/src/packages/@react-stately/data/src/useListData.ts","packages/@react-stately/data/src/packages/@react-stately/data/src/useAsyncList.ts","packages/@react-stately/data/src/packages/@react-stately/data/src/useTreeData.ts","packages/@react-stately/data/src/packages/@react-stately/data/src/index.ts","packages/@react-stately/data/src/index.ts"],"sourcesContent":[null,null,null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useAsyncList} from './useAsyncList';\nexport {useTreeData} from './useTreeData';\nexport {useListData} from './useListData';\n\nexport type {ListOptions, ListData} from './useListData';\nexport type {AsyncListOptions, AsyncListData} from './useAsyncList';\nexport type {TreeOptions, TreeData} from './useTreeData';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
1
+ {"mappings":";AAeA,6BAA6B,CAAC;IAC5B,iCAAiC;IACjC,YAAY,CAAC,EAAE,CAAC,EAAE,CAAC;IACnB,iDAAiD;IACjD,mBAAmB,CAAC,EAAE,KAAK,GAAG,QAAQ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AEnE;;;;OAIG;IACH,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,MAAM,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC;IAE7C;;;;OAIG;IACH,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,MAAM,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC;IAE5C;;;;OAIG;IACH,MAAM,CAAC,SAAS,EAAE,GAAG,GAAG,IAAI,EAAE,GAAG,MAAM,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC;IAEpD;;;;OAIG;IACH,OAAO,CAAC,SAAS,EAAE,GAAG,GAAG,IAAI,EAAE,GAAG,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC;IAEpD;;;OAGG;IACH,MAAM,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAE7B;;;OAGG;IACH,mBAAmB,IAAI,IAAI,CAAC;IAE5B;;;;;OAKG;IACH,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,GAAG,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7D;;;;OAIG;IACH,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAEhD;;;;OAIG;IACH,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAE/C;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,CAAA;CACpC;AAOD;;;GAGG;AACH,4BAA4B,CAAC,SAAS,MAAM,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CA6SlF","sources":["packages/@react-stately/data/src/packages/@react-stately/data/src/useListData.ts","packages/@react-stately/data/src/packages/@react-stately/data/src/useAsyncList.ts","packages/@react-stately/data/src/packages/@react-stately/data/src/useTreeData.ts","packages/@react-stately/data/src/packages/@react-stately/data/src/index.ts","packages/@react-stately/data/src/index.ts"],"sourcesContent":[null,null,null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useAsyncList} from './useAsyncList';\nexport {useTreeData} from './useTreeData';\nexport {useListData} from './useListData';\n\nexport type {ListOptions, ListData} from './useListData';\nexport type {AsyncListOptions, AsyncListData} from './useAsyncList';\nexport type {TreeOptions, TreeData} from './useTreeData';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
@@ -32,7 +32,7 @@ function $2d16d1aab63a81f4$export$d14e1352e21f4a16(options) {
32
32
  items: initialItems.map((item)=>{
33
33
  let node = {
34
34
  key: getKey(item),
35
- parentKey: parentKey,
35
+ parentKey: parentKey !== null && parentKey !== void 0 ? parentKey : null,
36
36
  value: item,
37
37
  children: null
38
38
  };
@@ -44,8 +44,8 @@ function $2d16d1aab63a81f4$export$d14e1352e21f4a16(options) {
44
44
  };
45
45
  }
46
46
  function updateTree(items, key, update, originalMap) {
47
- let node = originalMap.get(key);
48
- if (!node) return {
47
+ let node = key == null ? null : originalMap.get(key);
48
+ if (node == null) return {
49
49
  items: items,
50
50
  nodeMap: originalMap
51
51
  };
@@ -186,7 +186,7 @@ function $2d16d1aab63a81f4$export$d14e1352e21f4a16(options) {
186
186
  };
187
187
  // If parentKey is null, insert into the root.
188
188
  if (toParentKey == null) {
189
- newMap.set(movedNode.key, movedNode);
189
+ addNode(movedNode, newMap);
190
190
  return {
191
191
  items: [
192
192
  ...newItems.slice(0, index),
@@ -209,6 +209,35 @@ function $2d16d1aab63a81f4$export$d14e1352e21f4a16(options) {
209
209
  }), newMap);
210
210
  });
211
211
  },
212
+ moveBefore (key, keys) {
213
+ setItems((prevState)=>{
214
+ let { items: items, nodeMap: nodeMap } = prevState;
215
+ let node = nodeMap.get(key);
216
+ if (!node) return prevState;
217
+ var _node_parentKey;
218
+ let toParentKey = (_node_parentKey = node.parentKey) !== null && _node_parentKey !== void 0 ? _node_parentKey : null;
219
+ let parent = null;
220
+ var _nodeMap_get;
221
+ if (toParentKey != null) parent = (_nodeMap_get = nodeMap.get(toParentKey)) !== null && _nodeMap_get !== void 0 ? _nodeMap_get : null;
222
+ let toIndex = (parent === null || parent === void 0 ? void 0 : parent.children) ? parent.children.indexOf(node) : items.indexOf(node);
223
+ return $2d16d1aab63a81f4$var$moveItems(prevState, keys, parent, toIndex, updateTree, addNode);
224
+ });
225
+ },
226
+ moveAfter (key, keys) {
227
+ setItems((prevState)=>{
228
+ let { items: items, nodeMap: nodeMap } = prevState;
229
+ let node = nodeMap.get(key);
230
+ if (!node) return prevState;
231
+ var _node_parentKey;
232
+ let toParentKey = (_node_parentKey = node.parentKey) !== null && _node_parentKey !== void 0 ? _node_parentKey : null;
233
+ let parent = null;
234
+ var _nodeMap_get;
235
+ if (toParentKey != null) parent = (_nodeMap_get = nodeMap.get(toParentKey)) !== null && _nodeMap_get !== void 0 ? _nodeMap_get : null;
236
+ let toIndex = (parent === null || parent === void 0 ? void 0 : parent.children) ? parent.children.indexOf(node) : items.indexOf(node);
237
+ toIndex++;
238
+ return $2d16d1aab63a81f4$var$moveItems(prevState, keys, parent, toIndex, updateTree, addNode);
239
+ });
240
+ },
212
241
  update (oldKey, newValue) {
213
242
  setItems(({ items: items, nodeMap: originalMap })=>updateTree(items, oldKey, (oldNode)=>{
214
243
  let node = {
@@ -224,6 +253,89 @@ function $2d16d1aab63a81f4$export$d14e1352e21f4a16(options) {
224
253
  }
225
254
  };
226
255
  }
256
+ function $2d16d1aab63a81f4$var$moveItems(state, keys, toParent, toIndex, updateTree, addNode) {
257
+ let { items: items, nodeMap: nodeMap } = state;
258
+ let parent = toParent;
259
+ let removeKeys = new Set(keys);
260
+ while((parent === null || parent === void 0 ? void 0 : parent.parentKey) != null){
261
+ if (removeKeys.has(parent.key)) throw new Error('Cannot move an item to be a child of itself.');
262
+ var _nodeMap_get;
263
+ parent = (_nodeMap_get = nodeMap.get(parent.parentKey)) !== null && _nodeMap_get !== void 0 ? _nodeMap_get : null;
264
+ }
265
+ let originalToIndex = toIndex;
266
+ let keyArray = Array.isArray(keys) ? keys : [
267
+ ...keys
268
+ ];
269
+ // depth first search to put keys in order
270
+ let inOrderKeys = new Map();
271
+ let removedItems = [];
272
+ let newItems = items;
273
+ let newMap = nodeMap;
274
+ let i = 0;
275
+ function traversal(node, { inorder: inorder, postorder: postorder }) {
276
+ inorder === null || inorder === void 0 ? void 0 : inorder(node);
277
+ var _node_children;
278
+ if (node != null) for (let child of (_node_children = node.children) !== null && _node_children !== void 0 ? _node_children : []){
279
+ traversal(child, {
280
+ inorder: inorder,
281
+ postorder: postorder
282
+ });
283
+ postorder === null || postorder === void 0 ? void 0 : postorder(child);
284
+ }
285
+ }
286
+ function inorder(child) {
287
+ // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later
288
+ if (keyArray.includes(child.key)) inOrderKeys.set(child.key, i++);
289
+ }
290
+ function postorder(child) {
291
+ // remove items and update the tree from the leaves and work upwards toward the root, this way
292
+ // we don't copy child node references from parents inadvertently
293
+ if (keyArray.includes(child.key)) {
294
+ var _toParent_key;
295
+ removedItems.push({
296
+ ...newMap.get(child.key),
297
+ parentKey: (_toParent_key = toParent === null || toParent === void 0 ? void 0 : toParent.key) !== null && _toParent_key !== void 0 ? _toParent_key : null
298
+ });
299
+ let { items: nextItems, nodeMap: nextMap } = updateTree(newItems, child.key, ()=>null, newMap);
300
+ newItems = nextItems;
301
+ newMap = nextMap;
302
+ }
303
+ // decrement the index if the child being removed is in the target parent and before the target index
304
+ if (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--;
305
+ }
306
+ traversal({
307
+ children: items
308
+ }, {
309
+ inorder: inorder,
310
+ postorder: postorder
311
+ });
312
+ let inOrderItems = removedItems.sort((a, b)=>inOrderKeys.get(a.key) > inOrderKeys.get(b.key) ? 1 : -1);
313
+ // If parentKey is null, insert into the root.
314
+ if (!toParent || toParent.key == null) {
315
+ inOrderItems.forEach((movedNode)=>{
316
+ addNode(movedNode, newMap);
317
+ });
318
+ return {
319
+ items: [
320
+ ...newItems.slice(0, toIndex),
321
+ ...inOrderItems,
322
+ ...newItems.slice(toIndex)
323
+ ],
324
+ nodeMap: newMap
325
+ };
326
+ }
327
+ // Otherwise, update the parent node and its ancestors.
328
+ return updateTree(newItems, toParent.key, (parentNode)=>({
329
+ key: parentNode.key,
330
+ parentKey: parentNode.parentKey,
331
+ value: parentNode.value,
332
+ children: [
333
+ ...parentNode.children.slice(0, toIndex),
334
+ ...inOrderItems,
335
+ ...parentNode.children.slice(toIndex)
336
+ ]
337
+ }), newMap);
338
+ }
227
339
 
228
340
 
229
341
  //# sourceMappingURL=useTreeData.main.js.map
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AA+GM,SAAS,0CAA8B,OAAuB;IACnE,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC;YAAc;eAAA,CAAA,WAAA,KAAK,EAAE,cAAP,sBAAA,WAAW,KAAK,GAAG;oBAC3C,cAAc,CAAC,OAAc,KAAK,QAAQ,EAC3C,GAAG;IAEJ,kDAAkD;IAClD,IAAI,CAAC,MAAM,SAAS,GAAG,CAAA,GAAA,qBAAO,EAA0D,IAAM,UAAU,cAAc,IAAI;IAC1H,IAAI,SAAC,KAAK,WAAE,O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,CAAC,SAAC,KAAK,EAAE,SAAS,WAAW,EAAC,GAAK,WAAW,OAAO,QAAQ,CAAA;oBACpE,IAAI,OAAoB;wBACtB,KAAK,QAAQ,GAAG;wBAChB,WAAW,QAAQ,SAAS;wBAC5B,OAAO;wBACP,UAAU;oBACZ;oBAEA,IAAI,OAAO,UAAU,YAAY,WAAW,aAAa,KAAK,GAAG;oBACjE,KAAK,QAAQ,GAAG,KAAK,KAAK;oBAC1B,OAAO;gBACT,GAAG;QACL;IACF;AACF","sources":["packages/@react-stately/data/src/useTreeData.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Key} from '@react-types/shared';\nimport {useState} from 'react';\n\nexport interface TreeOptions<T extends object> {\n /** Initial root items in the tree. */\n initialItems?: T[],\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>,\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key,\n /** A function that returns the children for an item object. */\n getChildren?: (item: T) => T[]\n}\n\ninterface TreeNode<T extends object> {\n /** A unique key for the tree node. */\n key: Key,\n /** The key of the parent node. */\n parentKey?: Key | null,\n /** The value object for the tree node. */\n value: T,\n /** Children of the tree node. */\n children: TreeNode<T>[] | null\n}\n\nexport interface TreeData<T extends object> {\n /** The root nodes in the tree. */\n items: TreeNode<T>[],\n\n /** The keys of the currently selected items in the tree. */\n selectedKeys: Set<Key>,\n\n /** Sets the selected keys. */\n setSelectedKeys(keys: Set<Key>): void,\n\n /**\n * Gets a node from the tree by key.\n * @param key - The key of the item to retrieve.\n */\n getItem(key: Key): TreeNode<T> | undefined,\n\n /**\n * Inserts an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param index - The index within the parent to insert into.\n * @param value - The value to insert.\n */\n insert(parentKey: Key | null, index: number, ...values: T[]): void,\n\n /**\n * Inserts items into the list before the item at the given key.\n * @param key - The key of the item to insert before.\n * @param values - The values to insert.\n */\n insertBefore(key: Key, ...values: T[]): void,\n\n /**\n * Inserts items into the list after the item at the given key.\n * @param key - The key of the item to insert after.\n * @param values - The values to insert.\n */\n insertAfter(key: Key, ...values: T[]): void,\n\n /**\n * Appends an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n append(parentKey: Key | null, ...values: T[]): void,\n\n /**\n * Prepends an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n prepend(parentKey: Key | null, ...value: T[]): void,\n\n /**\n * Removes an item from the tree by its key.\n * @param key - The key of the item to remove.\n */\n remove(...keys: Key[]): void,\n\n /**\n * Removes all items from the tree that are currently\n * in the set of selected items.\n */\n removeSelectedItems(): void,\n\n /**\n * Moves an item within the tree.\n * @param key - The key of the item to move.\n * @param toParentKey - The key of the new parent to insert into. `null` for the root.\n * @param index - The index within the new parent to insert at.\n */\n move(key: Key, toParentKey: Key | null, index: number): void,\n\n /**\n * Updates an item in the tree.\n * @param key - The key of the item to update.\n * @param newValue - The new value for the item.\n */\n update(key: Key, newValue: T): void\n}\n\n/**\n * Manages state for an immutable tree data structure, and provides convenience methods to\n * update the data over time.\n */\nexport function useTreeData<T extends object>(options: TreeOptions<T>): TreeData<T> {\n let {\n initialItems = [],\n initialSelectedKeys,\n getKey = (item: any) => item.id ?? item.key,\n getChildren = (item: any) => item.children\n } = options;\n\n // We only want to compute this on initial render.\n let [tree, setItems] = useState<{items: TreeNode<T>[], nodeMap: Map<Key, TreeNode<T>>}>(() => buildTree(initialItems, new Map()));\n let {items, nodeMap} = tree;\n\n let [selectedKeys, setSelectedKeys] = useState(new Set<Key>(initialSelectedKeys || []));\n\n function buildTree(initialItems: T[] | null = [], map: Map<Key, TreeNode<T>>, parentKey?: Key | null) {\n if (initialItems == null) {\n initialItems = [];\n }\n return {\n items: initialItems.map(item => {\n let node: TreeNode<T> = {\n key: getKey(item),\n parentKey: parentKey,\n value: item,\n children: null\n };\n\n node.children = buildTree(getChildren(item), map, node.key).items;\n map.set(node.key, node);\n return node;\n }),\n nodeMap: map\n };\n }\n\n function updateTree(items: TreeNode<T>[], key: Key, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n let map = new Map<Key, TreeNode<T>>(originalMap);\n\n // Create a new node. If null, then delete the node, otherwise replace.\n let newNode = update(node);\n if (newNode == null) {\n deleteNode(node, map);\n } else {\n addNode(newNode, map);\n }\n\n // Walk up the tree and update each parent to refer to the new children.\n while (node && node.parentKey) {\n let nextParent = map.get(node.parentKey)!;\n let copy: TreeNode<T> = {\n key: nextParent.key,\n parentKey: nextParent.parentKey,\n value: nextParent.value,\n children: null\n };\n\n let children = nextParent.children;\n if (newNode == null && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children = children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\n\n map.set(copy.key, copy);\n\n newNode = copy;\n node = nextParent;\n }\n\n if (newNode == null) {\n items = items.filter(c => c !== node);\n }\n\n return {\n items: items.map(item => {\n if (item === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return item;\n }),\n nodeMap: map\n };\n }\n\n function addNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.set(node.key, node);\n if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\n for (let child of node.children) {\n deleteNode(child, map);\n }\n }\n }\n\n return {\n items,\n selectedKeys,\n setSelectedKeys,\n getItem(key: Key) {\n return nodeMap.get(key);\n },\n insert(parentKey: Key | null, index: number, ...values: T[]) {\n setItems(({items, nodeMap: originalMap}) => {\n let {items: newNodes, nodeMap: newMap} = buildTree(values, originalMap, parentKey);\n\n // If parentKey is null, insert into the root.\n if (parentKey == null) {\n return {\n items: [\n ...items.slice(0, index),\n ...newNodes,\n ...items.slice(index)\n ],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(items, parentKey, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n ...newNodes,\n ...parentNode.children!.slice(index)\n ]\n }), newMap);\n });\n },\n insertBefore(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index, ...values);\n },\n insertAfter(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index + 1, ...values);\n },\n prepend(parentKey: Key | null, ...values: T[]) {\n this.insert(parentKey, 0, ...values);\n },\n append(parentKey: Key | null, ...values: T[]) {\n if (parentKey == null) {\n this.insert(null, items.length, ...values);\n } else {\n let parentNode = nodeMap.get(parentKey);\n if (!parentNode) {\n return;\n }\n\n this.insert(parentKey, parentNode.children!.length, ...values);\n }\n },\n remove(...keys: Key[]) {\n if (keys.length === 0) {\n return;\n }\n\n let newItems = items;\n let prevMap = nodeMap;\n let newTree;\n for (let key of keys) {\n newTree = updateTree(newItems, key, () => null, prevMap);\n prevMap = newTree.nodeMap;\n newItems = newTree.items;\n }\n\n setItems(newTree);\n\n let selection = new Set(selectedKeys);\n for (let key of selectedKeys) {\n if (!newTree.nodeMap.has(key)) {\n selection.delete(key);\n }\n }\n\n setSelectedKeys(selection);\n },\n removeSelectedItems() {\n this.remove(...selectedKeys);\n },\n move(key: Key, toParentKey: Key | null, index: number) {\n setItems(({items, nodeMap: originalMap}) => {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n\n let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);\n\n\n const movedNode = {\n ...node,\n parentKey: toParentKey\n };\n\n // If parentKey is null, insert into the root.\n if (toParentKey == null) {\n newMap.set(movedNode.key, movedNode);\n return {items: [\n ...newItems.slice(0, index),\n movedNode,\n ...newItems.slice(index)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParentKey, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n movedNode,\n ...parentNode.children!.slice(index)\n ]\n }), newMap);\n });\n },\n update(oldKey: Key, newValue: T) {\n setItems(({items, nodeMap: originalMap}) => updateTree(items, oldKey, oldNode => {\n let node: TreeNode<T> = {\n key: oldNode.key,\n parentKey: oldNode.parentKey,\n value: newValue,\n children: null\n };\n\n let tree = buildTree(getChildren(newValue), originalMap, node.key);\n node.children = tree.items;\n return node;\n }, originalMap));\n }\n };\n}\n"],"names":[],"version":3,"file":"useTreeData.main.js.map"}
1
+ {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AAkIM,SAAS,0CAA8B,OAAuB;IACnE,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC;YAAc;eAAA,CAAA,WAAA,KAAK,EAAE,cAAP,sBAAA,WAAW,KAAK,GAAG;oBAC3C,cAAc,CAAC,OAAc,KAAK,QAAQ,EAC3C,GAAG;IAEJ,kDAAkD;IAClD,IAAI,CA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,WAAW,SAAS;YAC/B,OAAO,WAAW,KAAK;YACvB,UAAU;mBACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;mBAC9B;mBACA,WAAW,QAAQ,CAAE,KAAK,CAAC;aAC/B;QACH,CAAA,GAAI;AACN","sources":["packages/@react-stately/data/src/useTreeData.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Key} from '@react-types/shared';\nimport {useState} from 'react';\n\nexport interface TreeOptions<T extends object> {\n /** Initial root items in the tree. */\n initialItems?: T[],\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>,\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key,\n /** A function that returns the children for an item object. */\n getChildren?: (item: T) => T[]\n}\n\ninterface TreeNode<T extends object> {\n /** A unique key for the tree node. */\n key: Key,\n /** The key of the parent node. */\n parentKey?: Key | null,\n /** The value object for the tree node. */\n value: T,\n /** Children of the tree node. */\n children: TreeNode<T>[] | null\n}\n\nexport interface TreeData<T extends object> {\n /** The root nodes in the tree. */\n items: TreeNode<T>[],\n\n /** The keys of the currently selected items in the tree. */\n selectedKeys: Set<Key>,\n\n /** Sets the selected keys. */\n setSelectedKeys(keys: Set<Key>): void,\n\n /**\n * Gets a node from the tree by key.\n * @param key - The key of the item to retrieve.\n */\n getItem(key: Key): TreeNode<T> | undefined,\n\n /**\n * Inserts an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param index - The index within the parent to insert into.\n * @param value - The value to insert.\n */\n insert(parentKey: Key | null, index: number, ...values: T[]): void,\n\n /**\n * Inserts items into the list before the item at the given key.\n * @param key - The key of the item to insert before.\n * @param values - The values to insert.\n */\n insertBefore(key: Key, ...values: T[]): void,\n\n /**\n * Inserts items into the list after the item at the given key.\n * @param key - The key of the item to insert after.\n * @param values - The values to insert.\n */\n insertAfter(key: Key, ...values: T[]): void,\n\n /**\n * Appends an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n append(parentKey: Key | null, ...values: T[]): void,\n\n /**\n * Prepends an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n prepend(parentKey: Key | null, ...value: T[]): void,\n\n /**\n * Removes an item from the tree by its key.\n * @param key - The key of the item to remove.\n */\n remove(...keys: Key[]): void,\n\n /**\n * Removes all items from the tree that are currently\n * in the set of selected items.\n */\n removeSelectedItems(): void,\n\n /**\n * Moves an item within the tree.\n * @param key - The key of the item to move.\n * @param toParentKey - The key of the new parent to insert into. `null` for the root.\n * @param index - The index within the new parent to insert at.\n */\n move(key: Key, toParentKey: Key | null, index: number): void,\n\n /**\n * Moves one or more items before a given key.\n * @param key - The key of the item to move the items before.\n * @param keys - The keys of the items to move.\n */\n moveBefore(key: Key, keys: Iterable<Key>): void,\n\n /**\n * Moves one or more items after a given key.\n * @param key - The key of the item to move the items after.\n * @param keys - The keys of the items to move.\n */\n moveAfter(key: Key, keys: Iterable<Key>): void,\n\n /**\n * Updates an item in the tree.\n * @param key - The key of the item to update.\n * @param newValue - The new value for the item.\n */\n update(key: Key, newValue: T): void\n}\n\ninterface TreeDataState<T extends object> {\n items: TreeNode<T>[],\n nodeMap: Map<Key, TreeNode<T>>\n}\n\n/**\n * Manages state for an immutable tree data structure, and provides convenience methods to\n * update the data over time.\n */\nexport function useTreeData<T extends object>(options: TreeOptions<T>): TreeData<T> {\n let {\n initialItems = [],\n initialSelectedKeys,\n getKey = (item: any) => item.id ?? item.key,\n getChildren = (item: any) => item.children\n } = options;\n\n // We only want to compute this on initial render.\n let [tree, setItems] = useState<TreeDataState<T>>(() => buildTree(initialItems, new Map()));\n let {items, nodeMap} = tree;\n\n let [selectedKeys, setSelectedKeys] = useState(new Set<Key>(initialSelectedKeys || []));\n\n function buildTree(initialItems: T[] | null = [], map: Map<Key, TreeNode<T>>, parentKey?: Key | null) {\n if (initialItems == null) {\n initialItems = [];\n }\n return {\n items: initialItems.map(item => {\n let node: TreeNode<T> = {\n key: getKey(item),\n parentKey: parentKey ?? null,\n value: item,\n children: null\n };\n\n node.children = buildTree(getChildren(item), map, node.key).items;\n map.set(node.key, node);\n return node;\n }),\n nodeMap: map\n };\n }\n\n function updateTree(items: TreeNode<T>[], key: Key | null, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {\n let node = key == null ? null : originalMap.get(key);\n if (node == null) {\n return {items, nodeMap: originalMap};\n }\n let map = new Map<Key, TreeNode<T>>(originalMap);\n\n // Create a new node. If null, then delete the node, otherwise replace.\n let newNode = update(node);\n if (newNode == null) {\n deleteNode(node, map);\n } else {\n addNode(newNode, map);\n }\n\n // Walk up the tree and update each parent to refer to the new children.\n while (node && node.parentKey) {\n let nextParent = map.get(node.parentKey)!;\n let copy: TreeNode<T> = {\n key: nextParent.key,\n parentKey: nextParent.parentKey,\n value: nextParent.value,\n children: null\n };\n\n let children = nextParent.children;\n if (newNode == null && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children = children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\n\n map.set(copy.key, copy);\n\n newNode = copy;\n node = nextParent;\n }\n\n if (newNode == null) {\n items = items.filter(c => c !== node);\n }\n\n return {\n items: items.map(item => {\n if (item === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return item;\n }),\n nodeMap: map\n };\n }\n\n function addNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.set(node.key, node);\n if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\n for (let child of node.children) {\n deleteNode(child, map);\n }\n }\n }\n return {\n items,\n selectedKeys,\n setSelectedKeys,\n getItem(key: Key) {\n return nodeMap.get(key);\n },\n insert(parentKey: Key | null, index: number, ...values: T[]) {\n setItems(({items, nodeMap: originalMap}) => {\n let {items: newNodes, nodeMap: newMap} = buildTree(values, originalMap, parentKey);\n\n // If parentKey is null, insert into the root.\n if (parentKey == null) {\n return {\n items: [\n ...items.slice(0, index),\n ...newNodes,\n ...items.slice(index)\n ],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(items, parentKey, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n ...newNodes,\n ...parentNode.children!.slice(index)\n ]\n }), newMap);\n });\n },\n insertBefore(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index, ...values);\n },\n insertAfter(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index + 1, ...values);\n },\n prepend(parentKey: Key | null, ...values: T[]) {\n this.insert(parentKey, 0, ...values);\n },\n append(parentKey: Key | null, ...values: T[]) {\n if (parentKey == null) {\n this.insert(null, items.length, ...values);\n } else {\n let parentNode = nodeMap.get(parentKey);\n if (!parentNode) {\n return;\n }\n\n this.insert(parentKey, parentNode.children!.length, ...values);\n }\n },\n remove(...keys: Key[]) {\n if (keys.length === 0) {\n return;\n }\n\n let newItems = items;\n let prevMap = nodeMap;\n let newTree;\n for (let key of keys) {\n newTree = updateTree(newItems, key, () => null, prevMap);\n prevMap = newTree.nodeMap;\n newItems = newTree.items;\n }\n\n setItems(newTree);\n\n let selection = new Set(selectedKeys);\n for (let key of selectedKeys) {\n if (!newTree.nodeMap.has(key)) {\n selection.delete(key);\n }\n }\n\n setSelectedKeys(selection);\n },\n removeSelectedItems() {\n this.remove(...selectedKeys);\n },\n move(key: Key, toParentKey: Key | null, index: number) {\n setItems(({items, nodeMap: originalMap}) => {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n\n let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);\n\n\n const movedNode = {\n ...node,\n parentKey: toParentKey\n };\n\n // If parentKey is null, insert into the root.\n if (toParentKey == null) {\n addNode(movedNode, newMap);\n return {items: [\n ...newItems.slice(0, index),\n movedNode,\n ...newItems.slice(index)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParentKey, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n movedNode,\n ...parentNode.children!.slice(index)\n ]\n }), newMap);\n });\n },\n moveBefore(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n toIndex++;\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n update(oldKey: Key, newValue: T) {\n setItems(({items, nodeMap: originalMap}) => updateTree(items, oldKey, oldNode => {\n let node: TreeNode<T> = {\n key: oldNode.key,\n parentKey: oldNode.parentKey,\n value: newValue,\n children: null\n };\n\n let tree = buildTree(getChildren(newValue), originalMap, node.key);\n node.children = tree.items;\n return node;\n }, originalMap));\n }\n };\n}\n\nfunction moveItems<T extends object>(\n state: TreeDataState<T>,\n keys: Iterable<Key>,\n toParent: TreeNode<T> | null,\n toIndex: number,\n updateTree: (\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) => TreeDataState<T>,\n addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void\n): TreeDataState<T> {\n let {items, nodeMap} = state;\n\n let parent = toParent;\n let removeKeys = new Set(keys);\n while (parent?.parentKey != null) {\n if (removeKeys.has(parent.key)) {\n throw new Error('Cannot move an item to be a child of itself.');\n }\n parent = nodeMap.get(parent.parentKey!) ?? null;\n }\n\n let originalToIndex = toIndex;\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n // depth first search to put keys in order\n let inOrderKeys: Map<Key, number> = new Map();\n let removedItems: Array<TreeNode<T>> = [];\n let newItems = items;\n let newMap = nodeMap;\n let i = 0;\n\n function traversal(node, {inorder, postorder}) {\n inorder?.(node);\n if (node != null) {\n for (let child of node.children ?? []) {\n traversal(child, {inorder, postorder});\n postorder?.(child);\n }\n }\n }\n\n function inorder(child) {\n // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later\n if (keyArray.includes(child.key)) {\n inOrderKeys.set(child.key, i++);\n }\n }\n\n function postorder(child) {\n // remove items and update the tree from the leaves and work upwards toward the root, this way\n // we don't copy child node references from parents inadvertently\n if (keyArray.includes(child.key)) {\n removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});\n let {items: nextItems, nodeMap: nextMap} = updateTree(newItems, child.key, () => null, newMap);\n newItems = nextItems;\n newMap = nextMap;\n }\n // decrement the index if the child being removed is in the target parent and before the target index\n if (child.parentKey === toParent?.key\n && keyArray.includes(child.key)\n && (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) {\n toIndex--;\n }\n }\n\n traversal({children: items}, {inorder, postorder});\n\n let inOrderItems = removedItems.sort((a, b) => inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1);\n // If parentKey is null, insert into the root.\n if (!toParent || toParent.key == null) {\n inOrderItems.forEach(movedNode => {\n addNode(movedNode, newMap); \n });\n return {items: [\n ...newItems.slice(0, toIndex),\n ...inOrderItems,\n ...newItems.slice(toIndex)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParent.key, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, toIndex),\n ...inOrderItems,\n ...parentNode.children!.slice(toIndex)\n ]\n }), newMap);\n}\n"],"names":[],"version":3,"file":"useTreeData.main.js.map"}
@@ -26,7 +26,7 @@ function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
26
26
  items: initialItems.map((item)=>{
27
27
  let node = {
28
28
  key: getKey(item),
29
- parentKey: parentKey,
29
+ parentKey: parentKey !== null && parentKey !== void 0 ? parentKey : null,
30
30
  value: item,
31
31
  children: null
32
32
  };
@@ -38,8 +38,8 @@ function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
38
38
  };
39
39
  }
40
40
  function updateTree(items, key, update, originalMap) {
41
- let node = originalMap.get(key);
42
- if (!node) return {
41
+ let node = key == null ? null : originalMap.get(key);
42
+ if (node == null) return {
43
43
  items: items,
44
44
  nodeMap: originalMap
45
45
  };
@@ -180,7 +180,7 @@ function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
180
180
  };
181
181
  // If parentKey is null, insert into the root.
182
182
  if (toParentKey == null) {
183
- newMap.set(movedNode.key, movedNode);
183
+ addNode(movedNode, newMap);
184
184
  return {
185
185
  items: [
186
186
  ...newItems.slice(0, index),
@@ -203,6 +203,35 @@ function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
203
203
  }), newMap);
204
204
  });
205
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
+ },
206
235
  update (oldKey, newValue) {
207
236
  setItems(({ items: items, nodeMap: originalMap })=>updateTree(items, oldKey, (oldNode)=>{
208
237
  let node = {
@@ -218,6 +247,89 @@ function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
218
247
  }
219
248
  };
220
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
+ if (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--;
299
+ }
300
+ traversal({
301
+ children: items
302
+ }, {
303
+ inorder: inorder,
304
+ postorder: postorder
305
+ });
306
+ let inOrderItems = removedItems.sort((a, b)=>inOrderKeys.get(a.key) > inOrderKeys.get(b.key) ? 1 : -1);
307
+ // If parentKey is null, insert into the root.
308
+ if (!toParent || toParent.key == null) {
309
+ inOrderItems.forEach((movedNode)=>{
310
+ addNode(movedNode, newMap);
311
+ });
312
+ return {
313
+ items: [
314
+ ...newItems.slice(0, toIndex),
315
+ ...inOrderItems,
316
+ ...newItems.slice(toIndex)
317
+ ],
318
+ nodeMap: newMap
319
+ };
320
+ }
321
+ // Otherwise, update the parent node and its ancestors.
322
+ return updateTree(newItems, toParent.key, (parentNode)=>({
323
+ key: parentNode.key,
324
+ parentKey: parentNode.parentKey,
325
+ value: parentNode.value,
326
+ children: [
327
+ ...parentNode.children.slice(0, toIndex),
328
+ ...inOrderItems,
329
+ ...parentNode.children.slice(toIndex)
330
+ ]
331
+ }), newMap);
332
+ }
221
333
 
222
334
 
223
335
  export {$be2ea0343af54212$export$d14e1352e21f4a16 as useTreeData};
@@ -26,7 +26,7 @@ function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
26
26
  items: initialItems.map((item)=>{
27
27
  let node = {
28
28
  key: getKey(item),
29
- parentKey: parentKey,
29
+ parentKey: parentKey !== null && parentKey !== void 0 ? parentKey : null,
30
30
  value: item,
31
31
  children: null
32
32
  };
@@ -38,8 +38,8 @@ function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
38
38
  };
39
39
  }
40
40
  function updateTree(items, key, update, originalMap) {
41
- let node = originalMap.get(key);
42
- if (!node) return {
41
+ let node = key == null ? null : originalMap.get(key);
42
+ if (node == null) return {
43
43
  items: items,
44
44
  nodeMap: originalMap
45
45
  };
@@ -180,7 +180,7 @@ function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
180
180
  };
181
181
  // If parentKey is null, insert into the root.
182
182
  if (toParentKey == null) {
183
- newMap.set(movedNode.key, movedNode);
183
+ addNode(movedNode, newMap);
184
184
  return {
185
185
  items: [
186
186
  ...newItems.slice(0, index),
@@ -203,6 +203,35 @@ function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
203
203
  }), newMap);
204
204
  });
205
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
+ },
206
235
  update (oldKey, newValue) {
207
236
  setItems(({ items: items, nodeMap: originalMap })=>updateTree(items, oldKey, (oldNode)=>{
208
237
  let node = {
@@ -218,6 +247,89 @@ function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
218
247
  }
219
248
  };
220
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
+ if (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--;
299
+ }
300
+ traversal({
301
+ children: items
302
+ }, {
303
+ inorder: inorder,
304
+ postorder: postorder
305
+ });
306
+ let inOrderItems = removedItems.sort((a, b)=>inOrderKeys.get(a.key) > inOrderKeys.get(b.key) ? 1 : -1);
307
+ // If parentKey is null, insert into the root.
308
+ if (!toParent || toParent.key == null) {
309
+ inOrderItems.forEach((movedNode)=>{
310
+ addNode(movedNode, newMap);
311
+ });
312
+ return {
313
+ items: [
314
+ ...newItems.slice(0, toIndex),
315
+ ...inOrderItems,
316
+ ...newItems.slice(toIndex)
317
+ ],
318
+ nodeMap: newMap
319
+ };
320
+ }
321
+ // Otherwise, update the parent node and its ancestors.
322
+ return updateTree(newItems, toParent.key, (parentNode)=>({
323
+ key: parentNode.key,
324
+ parentKey: parentNode.parentKey,
325
+ value: parentNode.value,
326
+ children: [
327
+ ...parentNode.children.slice(0, toIndex),
328
+ ...inOrderItems,
329
+ ...parentNode.children.slice(toIndex)
330
+ ]
331
+ }), newMap);
332
+ }
221
333
 
222
334
 
223
335
  export {$be2ea0343af54212$export$d14e1352e21f4a16 as useTreeData};
@@ -1 +1 @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ACrB;+BACG,SAAS,KAAK,CAAC;yBACnB;wBAAE,SAAS;oBAAM;gBACpB;gBAEA,uDAAuD;gBACvD,OAAO,WAAW,UAAU,aAAa,CAAA,aAAe,CAAA;wBACtD,KAAK,WAAW,GAAG;wBACnB,WAAW,WAAW,SAAS;wBAC/B,OAAO,WAAW,KAAK;wBACvB,UAAU;+BACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;4BACjC;+BACG,WAAW,QAAQ,CAAE,KAAK,CAAC;yBAC/B;oBACH,CAAA,GAAI;YACN;QACF;QACA,QAAO,MAAW,EAAE,QAAW;YAC7B,SAAS,CAAC,SAAC,KAAK,EAAE,SAAS,WAAW,EAAC,GAAK,WAAW,OAAO,QAAQ,CAAA;oBACpE,IAAI,OAAoB;wBACtB,KAAK,QAAQ,GAAG;wBAChB,WAAW,QAAQ,SAAS;wBAC5B,OAAO;wBACP,UAAU;oBACZ;oBAEA,IAAI,OAAO,UAAU,YAAY,WAAW,aAAa,KAAK,GAAG;oBACjE,KAAK,QAAQ,GAAG,KAAK,KAAK;oBAC1B,OAAO;gBACT,GAAG;QACL;IACF;AACF","sources":["packages/@react-stately/data/src/useTreeData.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Key} from '@react-types/shared';\nimport {useState} from 'react';\n\nexport interface TreeOptions<T extends object> {\n /** Initial root items in the tree. */\n initialItems?: T[],\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>,\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key,\n /** A function that returns the children for an item object. */\n getChildren?: (item: T) => T[]\n}\n\ninterface TreeNode<T extends object> {\n /** A unique key for the tree node. */\n key: Key,\n /** The key of the parent node. */\n parentKey?: Key | null,\n /** The value object for the tree node. */\n value: T,\n /** Children of the tree node. */\n children: TreeNode<T>[] | null\n}\n\nexport interface TreeData<T extends object> {\n /** The root nodes in the tree. */\n items: TreeNode<T>[],\n\n /** The keys of the currently selected items in the tree. */\n selectedKeys: Set<Key>,\n\n /** Sets the selected keys. */\n setSelectedKeys(keys: Set<Key>): void,\n\n /**\n * Gets a node from the tree by key.\n * @param key - The key of the item to retrieve.\n */\n getItem(key: Key): TreeNode<T> | undefined,\n\n /**\n * Inserts an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param index - The index within the parent to insert into.\n * @param value - The value to insert.\n */\n insert(parentKey: Key | null, index: number, ...values: T[]): void,\n\n /**\n * Inserts items into the list before the item at the given key.\n * @param key - The key of the item to insert before.\n * @param values - The values to insert.\n */\n insertBefore(key: Key, ...values: T[]): void,\n\n /**\n * Inserts items into the list after the item at the given key.\n * @param key - The key of the item to insert after.\n * @param values - The values to insert.\n */\n insertAfter(key: Key, ...values: T[]): void,\n\n /**\n * Appends an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n append(parentKey: Key | null, ...values: T[]): void,\n\n /**\n * Prepends an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n prepend(parentKey: Key | null, ...value: T[]): void,\n\n /**\n * Removes an item from the tree by its key.\n * @param key - The key of the item to remove.\n */\n remove(...keys: Key[]): void,\n\n /**\n * Removes all items from the tree that are currently\n * in the set of selected items.\n */\n removeSelectedItems(): void,\n\n /**\n * Moves an item within the tree.\n * @param key - The key of the item to move.\n * @param toParentKey - The key of the new parent to insert into. `null` for the root.\n * @param index - The index within the new parent to insert at.\n */\n move(key: Key, toParentKey: Key | null, index: number): void,\n\n /**\n * Updates an item in the tree.\n * @param key - The key of the item to update.\n * @param newValue - The new value for the item.\n */\n update(key: Key, newValue: T): void\n}\n\n/**\n * Manages state for an immutable tree data structure, and provides convenience methods to\n * update the data over time.\n */\nexport function useTreeData<T extends object>(options: TreeOptions<T>): TreeData<T> {\n let {\n initialItems = [],\n initialSelectedKeys,\n getKey = (item: any) => item.id ?? item.key,\n getChildren = (item: any) => item.children\n } = options;\n\n // We only want to compute this on initial render.\n let [tree, setItems] = useState<{items: TreeNode<T>[], nodeMap: Map<Key, TreeNode<T>>}>(() => buildTree(initialItems, new Map()));\n let {items, nodeMap} = tree;\n\n let [selectedKeys, setSelectedKeys] = useState(new Set<Key>(initialSelectedKeys || []));\n\n function buildTree(initialItems: T[] | null = [], map: Map<Key, TreeNode<T>>, parentKey?: Key | null) {\n if (initialItems == null) {\n initialItems = [];\n }\n return {\n items: initialItems.map(item => {\n let node: TreeNode<T> = {\n key: getKey(item),\n parentKey: parentKey,\n value: item,\n children: null\n };\n\n node.children = buildTree(getChildren(item), map, node.key).items;\n map.set(node.key, node);\n return node;\n }),\n nodeMap: map\n };\n }\n\n function updateTree(items: TreeNode<T>[], key: Key, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n let map = new Map<Key, TreeNode<T>>(originalMap);\n\n // Create a new node. If null, then delete the node, otherwise replace.\n let newNode = update(node);\n if (newNode == null) {\n deleteNode(node, map);\n } else {\n addNode(newNode, map);\n }\n\n // Walk up the tree and update each parent to refer to the new children.\n while (node && node.parentKey) {\n let nextParent = map.get(node.parentKey)!;\n let copy: TreeNode<T> = {\n key: nextParent.key,\n parentKey: nextParent.parentKey,\n value: nextParent.value,\n children: null\n };\n\n let children = nextParent.children;\n if (newNode == null && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children = children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\n\n map.set(copy.key, copy);\n\n newNode = copy;\n node = nextParent;\n }\n\n if (newNode == null) {\n items = items.filter(c => c !== node);\n }\n\n return {\n items: items.map(item => {\n if (item === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return item;\n }),\n nodeMap: map\n };\n }\n\n function addNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.set(node.key, node);\n if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\n for (let child of node.children) {\n deleteNode(child, map);\n }\n }\n }\n\n return {\n items,\n selectedKeys,\n setSelectedKeys,\n getItem(key: Key) {\n return nodeMap.get(key);\n },\n insert(parentKey: Key | null, index: number, ...values: T[]) {\n setItems(({items, nodeMap: originalMap}) => {\n let {items: newNodes, nodeMap: newMap} = buildTree(values, originalMap, parentKey);\n\n // If parentKey is null, insert into the root.\n if (parentKey == null) {\n return {\n items: [\n ...items.slice(0, index),\n ...newNodes,\n ...items.slice(index)\n ],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(items, parentKey, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n ...newNodes,\n ...parentNode.children!.slice(index)\n ]\n }), newMap);\n });\n },\n insertBefore(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index, ...values);\n },\n insertAfter(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index + 1, ...values);\n },\n prepend(parentKey: Key | null, ...values: T[]) {\n this.insert(parentKey, 0, ...values);\n },\n append(parentKey: Key | null, ...values: T[]) {\n if (parentKey == null) {\n this.insert(null, items.length, ...values);\n } else {\n let parentNode = nodeMap.get(parentKey);\n if (!parentNode) {\n return;\n }\n\n this.insert(parentKey, parentNode.children!.length, ...values);\n }\n },\n remove(...keys: Key[]) {\n if (keys.length === 0) {\n return;\n }\n\n let newItems = items;\n let prevMap = nodeMap;\n let newTree;\n for (let key of keys) {\n newTree = updateTree(newItems, key, () => null, prevMap);\n prevMap = newTree.nodeMap;\n newItems = newTree.items;\n }\n\n setItems(newTree);\n\n let selection = new Set(selectedKeys);\n for (let key of selectedKeys) {\n if (!newTree.nodeMap.has(key)) {\n selection.delete(key);\n }\n }\n\n setSelectedKeys(selection);\n },\n removeSelectedItems() {\n this.remove(...selectedKeys);\n },\n move(key: Key, toParentKey: Key | null, index: number) {\n setItems(({items, nodeMap: originalMap}) => {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n\n let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);\n\n\n const movedNode = {\n ...node,\n parentKey: toParentKey\n };\n\n // If parentKey is null, insert into the root.\n if (toParentKey == null) {\n newMap.set(movedNode.key, movedNode);\n return {items: [\n ...newItems.slice(0, index),\n movedNode,\n ...newItems.slice(index)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParentKey, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n movedNode,\n ...parentNode.children!.slice(index)\n ]\n }), newMap);\n });\n },\n update(oldKey: Key, newValue: T) {\n setItems(({items, nodeMap: originalMap}) => updateTree(items, oldKey, oldNode => {\n let node: TreeNode<T> = {\n key: oldNode.key,\n parentKey: oldNode.parentKey,\n value: newValue,\n children: null\n };\n\n let tree = buildTree(getChildren(newValue), originalMap, node.key);\n node.children = tree.items;\n return node;\n }, originalMap));\n }\n };\n}\n"],"names":[],"version":3,"file":"useTreeData.module.js.map"}
1
+ {"mappings":";;AAAA;;;;;;;;;;CAUC;AAkIM,SAAS,0CAA8B,OAAuB;IACnE,IAAI,gBACF,eAAe,EAAE,uBACjB,mBAAmB,UACnB,SAAS,CAAC;YAAc;eAAA,CAAA,WAAA,KAAK,EAAE,cAAP,sBAAA,WAAW,KAAK,GAAG;oBAC3C,cAAc,CAAC,OAAc,KAAK,QAAQ,EAC3C,GAAG;IAEJ,kDAAkD;IAClD,IAAI,CAAC,MAAM,SAAS,GAAG,CAAA,GAAA,eAAO,EAAoB,IAAM,UAAU,cAAc,IAAI;IACpF,IAAI,SAAC,KAAK,WAAE,OAAO,EAAC,GAAG;IAEvB,IAAI,CAAC,cAAc,gBAAgB,GAAG,CAAA,GAAA,eAAO,EAAE,IAAI,IAAS,uBAAuB,EAAE;IAErF,SAAS,UAAU,eAA2B,EAAE,EAAE,GAA0B,EAAE,SAAsB;QAClG,IAAI,gBAAgB,MAClB,eAAe,EAAE;QAEnB,OAAO;YACL,OAAO,aAAa,GAAG,CAAC,CAAA;gBACtB,IAAI,OAAoB;oBACtB,KAAK,OAAO;oBACZ,WAAW,sBAAA,uBAAA,YAAa;oBACxB,OAAO;oBACP,UAAU;gBACZ;gBAEA,KAAK,QAAQ,GAAG,UAAU,YAAY,OAAO,KAAK,KAAK,GAAG,EAAE,KAAK;gBACjE,IAAI,GAAG,CAAC,KAAK,GAAG,EAAE;gBAClB,OAAO;YACT;YACA,SAAS;QACX;IACF;IAEA,SAAS,WAAW,KAAoB,EAAE,GAAe,EAAE,MAAiD,EAAE,WAAkC;QAC9I,IAAI,OAAO,OAAO,OAAO,OAAO,YAAY,GAAG,CAAC;QAChD,IAAI,QAAQ,MACV,OAAO;mBAAC;YAAO,SAAS;QAAW;QAErC,IAAI,MAAM,IAAI,IAAsB;QAEpC,uEAAuE;QACvE,IAAI,UAAU,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,CAAC,EAAE,GAAG,IAAK,IAAI;IACvG,8CAA8C;IAC9C,IAAI,CAAC,YAAY,SAAS,GAAG,IAAI,MAAM;QACrC,aAAa,OAAO,CAAC,CAAA;YACnB,QAAQ,WAAW;QACrB;QACA,OAAO;YAAC,OAAO;mBACV,SAAS,KAAK,CAAC,GAAG;mBAClB;mBACA,SAAS,KAAK,CAAC;aACnB;YAAE,SAAS;QAAM;IACpB;IAEA,uDAAuD;IACvD,OAAO,WAAW,UAAU,SAAS,GAAG,EAAE,CAAA,aAAe,CAAA;YACvD,KAAK,WAAW,GAAG;YACnB,WAAW,WAAW,SAAS;YAC/B,OAAO,WAAW,KAAK;YACvB,UAAU;mBACL,WAAW,QAAQ,CAAE,KAAK,CAAC,GAAG;mBAC9B;mBACA,WAAW,QAAQ,CAAE,KAAK,CAAC;aAC/B;QACH,CAAA,GAAI;AACN","sources":["packages/@react-stately/data/src/useTreeData.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Key} from '@react-types/shared';\nimport {useState} from 'react';\n\nexport interface TreeOptions<T extends object> {\n /** Initial root items in the tree. */\n initialItems?: T[],\n /** The keys for the initially selected items. */\n initialSelectedKeys?: Iterable<Key>,\n /** A function that returns a unique key for an item object. */\n getKey?: (item: T) => Key,\n /** A function that returns the children for an item object. */\n getChildren?: (item: T) => T[]\n}\n\ninterface TreeNode<T extends object> {\n /** A unique key for the tree node. */\n key: Key,\n /** The key of the parent node. */\n parentKey?: Key | null,\n /** The value object for the tree node. */\n value: T,\n /** Children of the tree node. */\n children: TreeNode<T>[] | null\n}\n\nexport interface TreeData<T extends object> {\n /** The root nodes in the tree. */\n items: TreeNode<T>[],\n\n /** The keys of the currently selected items in the tree. */\n selectedKeys: Set<Key>,\n\n /** Sets the selected keys. */\n setSelectedKeys(keys: Set<Key>): void,\n\n /**\n * Gets a node from the tree by key.\n * @param key - The key of the item to retrieve.\n */\n getItem(key: Key): TreeNode<T> | undefined,\n\n /**\n * Inserts an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param index - The index within the parent to insert into.\n * @param value - The value to insert.\n */\n insert(parentKey: Key | null, index: number, ...values: T[]): void,\n\n /**\n * Inserts items into the list before the item at the given key.\n * @param key - The key of the item to insert before.\n * @param values - The values to insert.\n */\n insertBefore(key: Key, ...values: T[]): void,\n\n /**\n * Inserts items into the list after the item at the given key.\n * @param key - The key of the item to insert after.\n * @param values - The values to insert.\n */\n insertAfter(key: Key, ...values: T[]): void,\n\n /**\n * Appends an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n append(parentKey: Key | null, ...values: T[]): void,\n\n /**\n * Prepends an item into a parent node as a child.\n * @param parentKey - The key of the parent item to insert into. `null` for the root.\n * @param value - The value to insert.\n */\n prepend(parentKey: Key | null, ...value: T[]): void,\n\n /**\n * Removes an item from the tree by its key.\n * @param key - The key of the item to remove.\n */\n remove(...keys: Key[]): void,\n\n /**\n * Removes all items from the tree that are currently\n * in the set of selected items.\n */\n removeSelectedItems(): void,\n\n /**\n * Moves an item within the tree.\n * @param key - The key of the item to move.\n * @param toParentKey - The key of the new parent to insert into. `null` for the root.\n * @param index - The index within the new parent to insert at.\n */\n move(key: Key, toParentKey: Key | null, index: number): void,\n\n /**\n * Moves one or more items before a given key.\n * @param key - The key of the item to move the items before.\n * @param keys - The keys of the items to move.\n */\n moveBefore(key: Key, keys: Iterable<Key>): void,\n\n /**\n * Moves one or more items after a given key.\n * @param key - The key of the item to move the items after.\n * @param keys - The keys of the items to move.\n */\n moveAfter(key: Key, keys: Iterable<Key>): void,\n\n /**\n * Updates an item in the tree.\n * @param key - The key of the item to update.\n * @param newValue - The new value for the item.\n */\n update(key: Key, newValue: T): void\n}\n\ninterface TreeDataState<T extends object> {\n items: TreeNode<T>[],\n nodeMap: Map<Key, TreeNode<T>>\n}\n\n/**\n * Manages state for an immutable tree data structure, and provides convenience methods to\n * update the data over time.\n */\nexport function useTreeData<T extends object>(options: TreeOptions<T>): TreeData<T> {\n let {\n initialItems = [],\n initialSelectedKeys,\n getKey = (item: any) => item.id ?? item.key,\n getChildren = (item: any) => item.children\n } = options;\n\n // We only want to compute this on initial render.\n let [tree, setItems] = useState<TreeDataState<T>>(() => buildTree(initialItems, new Map()));\n let {items, nodeMap} = tree;\n\n let [selectedKeys, setSelectedKeys] = useState(new Set<Key>(initialSelectedKeys || []));\n\n function buildTree(initialItems: T[] | null = [], map: Map<Key, TreeNode<T>>, parentKey?: Key | null) {\n if (initialItems == null) {\n initialItems = [];\n }\n return {\n items: initialItems.map(item => {\n let node: TreeNode<T> = {\n key: getKey(item),\n parentKey: parentKey ?? null,\n value: item,\n children: null\n };\n\n node.children = buildTree(getChildren(item), map, node.key).items;\n map.set(node.key, node);\n return node;\n }),\n nodeMap: map\n };\n }\n\n function updateTree(items: TreeNode<T>[], key: Key | null, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {\n let node = key == null ? null : originalMap.get(key);\n if (node == null) {\n return {items, nodeMap: originalMap};\n }\n let map = new Map<Key, TreeNode<T>>(originalMap);\n\n // Create a new node. If null, then delete the node, otherwise replace.\n let newNode = update(node);\n if (newNode == null) {\n deleteNode(node, map);\n } else {\n addNode(newNode, map);\n }\n\n // Walk up the tree and update each parent to refer to the new children.\n while (node && node.parentKey) {\n let nextParent = map.get(node.parentKey)!;\n let copy: TreeNode<T> = {\n key: nextParent.key,\n parentKey: nextParent.parentKey,\n value: nextParent.value,\n children: null\n };\n\n let children = nextParent.children;\n if (newNode == null && children) {\n children = children.filter(c => c !== node);\n }\n\n copy.children = children?.map(child => {\n if (child === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return child;\n }) ?? null;\n\n map.set(copy.key, copy);\n\n newNode = copy;\n node = nextParent;\n }\n\n if (newNode == null) {\n items = items.filter(c => c !== node);\n }\n\n return {\n items: items.map(item => {\n if (item === node) {\n // newNode cannot be null here due to the above filter.\n return newNode!;\n }\n\n return item;\n }),\n nodeMap: map\n };\n }\n\n function addNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.set(node.key, node);\n if (node.children) {\n for (let child of node.children) {\n addNode(child, map);\n }\n }\n }\n\n function deleteNode(node: TreeNode<T>, map: Map<Key, TreeNode<T>>) {\n map.delete(node.key);\n if (node.children) {\n for (let child of node.children) {\n deleteNode(child, map);\n }\n }\n }\n return {\n items,\n selectedKeys,\n setSelectedKeys,\n getItem(key: Key) {\n return nodeMap.get(key);\n },\n insert(parentKey: Key | null, index: number, ...values: T[]) {\n setItems(({items, nodeMap: originalMap}) => {\n let {items: newNodes, nodeMap: newMap} = buildTree(values, originalMap, parentKey);\n\n // If parentKey is null, insert into the root.\n if (parentKey == null) {\n return {\n items: [\n ...items.slice(0, index),\n ...newNodes,\n ...items.slice(index)\n ],\n nodeMap: newMap\n };\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(items, parentKey, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n ...newNodes,\n ...parentNode.children!.slice(index)\n ]\n }), newMap);\n });\n },\n insertBefore(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index, ...values);\n },\n insertAfter(key: Key, ...values: T[]): void {\n let node = nodeMap.get(key);\n if (!node) {\n return;\n }\n\n let parentNode = nodeMap.get(node.parentKey!);\n let nodes = parentNode ? parentNode.children : items;\n let index = nodes!.indexOf(node);\n this.insert(parentNode?.key ?? null, index + 1, ...values);\n },\n prepend(parentKey: Key | null, ...values: T[]) {\n this.insert(parentKey, 0, ...values);\n },\n append(parentKey: Key | null, ...values: T[]) {\n if (parentKey == null) {\n this.insert(null, items.length, ...values);\n } else {\n let parentNode = nodeMap.get(parentKey);\n if (!parentNode) {\n return;\n }\n\n this.insert(parentKey, parentNode.children!.length, ...values);\n }\n },\n remove(...keys: Key[]) {\n if (keys.length === 0) {\n return;\n }\n\n let newItems = items;\n let prevMap = nodeMap;\n let newTree;\n for (let key of keys) {\n newTree = updateTree(newItems, key, () => null, prevMap);\n prevMap = newTree.nodeMap;\n newItems = newTree.items;\n }\n\n setItems(newTree);\n\n let selection = new Set(selectedKeys);\n for (let key of selectedKeys) {\n if (!newTree.nodeMap.has(key)) {\n selection.delete(key);\n }\n }\n\n setSelectedKeys(selection);\n },\n removeSelectedItems() {\n this.remove(...selectedKeys);\n },\n move(key: Key, toParentKey: Key | null, index: number) {\n setItems(({items, nodeMap: originalMap}) => {\n let node = originalMap.get(key);\n if (!node) {\n return {items, nodeMap: originalMap};\n }\n\n let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);\n\n\n const movedNode = {\n ...node,\n parentKey: toParentKey\n };\n\n // If parentKey is null, insert into the root.\n if (toParentKey == null) {\n addNode(movedNode, newMap);\n return {items: [\n ...newItems.slice(0, index),\n movedNode,\n ...newItems.slice(index)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParentKey, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, index),\n movedNode,\n ...parentNode.children!.slice(index)\n ]\n }), newMap);\n });\n },\n moveBefore(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n moveAfter(key: Key, keys: Iterable<Key>) {\n setItems((prevState) => {\n let {items, nodeMap} = prevState;\n let node = nodeMap.get(key);\n if (!node) {\n return prevState;\n }\n let toParentKey = node.parentKey ?? null;\n let parent: null | TreeNode<T> = null;\n if (toParentKey != null) {\n parent = nodeMap.get(toParentKey) ?? null;\n }\n let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);\n toIndex++;\n return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);\n });\n },\n update(oldKey: Key, newValue: T) {\n setItems(({items, nodeMap: originalMap}) => updateTree(items, oldKey, oldNode => {\n let node: TreeNode<T> = {\n key: oldNode.key,\n parentKey: oldNode.parentKey,\n value: newValue,\n children: null\n };\n\n let tree = buildTree(getChildren(newValue), originalMap, node.key);\n node.children = tree.items;\n return node;\n }, originalMap));\n }\n };\n}\n\nfunction moveItems<T extends object>(\n state: TreeDataState<T>,\n keys: Iterable<Key>,\n toParent: TreeNode<T> | null,\n toIndex: number,\n updateTree: (\n items: TreeNode<T>[],\n key: Key | null,\n update: (node: TreeNode<T>) => TreeNode<T> | null,\n originalMap: Map<Key, TreeNode<T>>\n ) => TreeDataState<T>,\n addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void\n): TreeDataState<T> {\n let {items, nodeMap} = state;\n\n let parent = toParent;\n let removeKeys = new Set(keys);\n while (parent?.parentKey != null) {\n if (removeKeys.has(parent.key)) {\n throw new Error('Cannot move an item to be a child of itself.');\n }\n parent = nodeMap.get(parent.parentKey!) ?? null;\n }\n\n let originalToIndex = toIndex;\n\n let keyArray = Array.isArray(keys) ? keys : [...keys];\n // depth first search to put keys in order\n let inOrderKeys: Map<Key, number> = new Map();\n let removedItems: Array<TreeNode<T>> = [];\n let newItems = items;\n let newMap = nodeMap;\n let i = 0;\n\n function traversal(node, {inorder, postorder}) {\n inorder?.(node);\n if (node != null) {\n for (let child of node.children ?? []) {\n traversal(child, {inorder, postorder});\n postorder?.(child);\n }\n }\n }\n\n function inorder(child) {\n // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later\n if (keyArray.includes(child.key)) {\n inOrderKeys.set(child.key, i++);\n }\n }\n\n function postorder(child) {\n // remove items and update the tree from the leaves and work upwards toward the root, this way\n // we don't copy child node references from parents inadvertently\n if (keyArray.includes(child.key)) {\n removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});\n let {items: nextItems, nodeMap: nextMap} = updateTree(newItems, child.key, () => null, newMap);\n newItems = nextItems;\n newMap = nextMap;\n }\n // decrement the index if the child being removed is in the target parent and before the target index\n if (child.parentKey === toParent?.key\n && keyArray.includes(child.key)\n && (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) {\n toIndex--;\n }\n }\n\n traversal({children: items}, {inorder, postorder});\n\n let inOrderItems = removedItems.sort((a, b) => inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1);\n // If parentKey is null, insert into the root.\n if (!toParent || toParent.key == null) {\n inOrderItems.forEach(movedNode => {\n addNode(movedNode, newMap); \n });\n return {items: [\n ...newItems.slice(0, toIndex),\n ...inOrderItems,\n ...newItems.slice(toIndex)\n ], nodeMap: newMap};\n }\n\n // Otherwise, update the parent node and its ancestors.\n return updateTree(newItems, toParent.key, parentNode => ({\n key: parentNode.key,\n parentKey: parentNode.parentKey,\n value: parentNode.value,\n children: [\n ...parentNode.children!.slice(0, toIndex),\n ...inOrderItems,\n ...parentNode.children!.slice(toIndex)\n ]\n }), newMap);\n}\n"],"names":[],"version":3,"file":"useTreeData.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/data",
3
- "version": "3.12.3",
3
+ "version": "3.13.0",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -22,7 +22,7 @@
22
22
  "url": "https://github.com/adobe/react-spectrum"
23
23
  },
24
24
  "dependencies": {
25
- "@react-types/shared": "^3.29.0",
25
+ "@react-types/shared": "^3.29.1",
26
26
  "@swc/helpers": "^0.5.0"
27
27
  },
28
28
  "peerDependencies": {
@@ -31,5 +31,5 @@
31
31
  "publishConfig": {
32
32
  "access": "public"
33
33
  },
34
- "gitHead": "9b66d270572f482948afee95622a85cdf68ed408"
34
+ "gitHead": "9c77d4e8267ed39469c65f65da94ece7be509874"
35
35
  }
@@ -107,6 +107,20 @@ export interface TreeData<T extends object> {
107
107
  */
108
108
  move(key: Key, toParentKey: Key | null, index: number): void,
109
109
 
110
+ /**
111
+ * Moves one or more items before a given key.
112
+ * @param key - The key of the item to move the items before.
113
+ * @param keys - The keys of the items to move.
114
+ */
115
+ moveBefore(key: Key, keys: Iterable<Key>): void,
116
+
117
+ /**
118
+ * Moves one or more items after a given key.
119
+ * @param key - The key of the item to move the items after.
120
+ * @param keys - The keys of the items to move.
121
+ */
122
+ moveAfter(key: Key, keys: Iterable<Key>): void,
123
+
110
124
  /**
111
125
  * Updates an item in the tree.
112
126
  * @param key - The key of the item to update.
@@ -115,6 +129,11 @@ export interface TreeData<T extends object> {
115
129
  update(key: Key, newValue: T): void
116
130
  }
117
131
 
132
+ interface TreeDataState<T extends object> {
133
+ items: TreeNode<T>[],
134
+ nodeMap: Map<Key, TreeNode<T>>
135
+ }
136
+
118
137
  /**
119
138
  * Manages state for an immutable tree data structure, and provides convenience methods to
120
139
  * update the data over time.
@@ -128,7 +147,7 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
128
147
  } = options;
129
148
 
130
149
  // We only want to compute this on initial render.
131
- let [tree, setItems] = useState<{items: TreeNode<T>[], nodeMap: Map<Key, TreeNode<T>>}>(() => buildTree(initialItems, new Map()));
150
+ let [tree, setItems] = useState<TreeDataState<T>>(() => buildTree(initialItems, new Map()));
132
151
  let {items, nodeMap} = tree;
133
152
 
134
153
  let [selectedKeys, setSelectedKeys] = useState(new Set<Key>(initialSelectedKeys || []));
@@ -141,7 +160,7 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
141
160
  items: initialItems.map(item => {
142
161
  let node: TreeNode<T> = {
143
162
  key: getKey(item),
144
- parentKey: parentKey,
163
+ parentKey: parentKey ?? null,
145
164
  value: item,
146
165
  children: null
147
166
  };
@@ -154,9 +173,9 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
154
173
  };
155
174
  }
156
175
 
157
- function updateTree(items: TreeNode<T>[], key: Key, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {
158
- let node = originalMap.get(key);
159
- if (!node) {
176
+ function updateTree(items: TreeNode<T>[], key: Key | null, update: (node: TreeNode<T>) => TreeNode<T> | null, originalMap: Map<Key, TreeNode<T>>) {
177
+ let node = key == null ? null : originalMap.get(key);
178
+ if (node == null) {
160
179
  return {items, nodeMap: originalMap};
161
180
  }
162
181
  let map = new Map<Key, TreeNode<T>>(originalMap);
@@ -233,7 +252,6 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
233
252
  }
234
253
  }
235
254
  }
236
-
237
255
  return {
238
256
  items,
239
257
  selectedKeys,
@@ -352,7 +370,7 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
352
370
 
353
371
  // If parentKey is null, insert into the root.
354
372
  if (toParentKey == null) {
355
- newMap.set(movedNode.key, movedNode);
373
+ addNode(movedNode, newMap);
356
374
  return {items: [
357
375
  ...newItems.slice(0, index),
358
376
  movedNode,
@@ -373,6 +391,39 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
373
391
  }), newMap);
374
392
  });
375
393
  },
394
+ moveBefore(key: Key, keys: Iterable<Key>) {
395
+ setItems((prevState) => {
396
+ let {items, nodeMap} = prevState;
397
+ let node = nodeMap.get(key);
398
+ if (!node) {
399
+ return prevState;
400
+ }
401
+ let toParentKey = node.parentKey ?? null;
402
+ let parent: null | TreeNode<T> = null;
403
+ if (toParentKey != null) {
404
+ parent = nodeMap.get(toParentKey) ?? null;
405
+ }
406
+ let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);
407
+ return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);
408
+ });
409
+ },
410
+ moveAfter(key: Key, keys: Iterable<Key>) {
411
+ setItems((prevState) => {
412
+ let {items, nodeMap} = prevState;
413
+ let node = nodeMap.get(key);
414
+ if (!node) {
415
+ return prevState;
416
+ }
417
+ let toParentKey = node.parentKey ?? null;
418
+ let parent: null | TreeNode<T> = null;
419
+ if (toParentKey != null) {
420
+ parent = nodeMap.get(toParentKey) ?? null;
421
+ }
422
+ let toIndex = parent?.children ? parent.children.indexOf(node) : items.indexOf(node);
423
+ toIndex++;
424
+ return moveItems(prevState, keys, parent, toIndex, updateTree, addNode);
425
+ });
426
+ },
376
427
  update(oldKey: Key, newValue: T) {
377
428
  setItems(({items, nodeMap: originalMap}) => updateTree(items, oldKey, oldNode => {
378
429
  let node: TreeNode<T> = {
@@ -389,3 +440,99 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
389
440
  }
390
441
  };
391
442
  }
443
+
444
+ function moveItems<T extends object>(
445
+ state: TreeDataState<T>,
446
+ keys: Iterable<Key>,
447
+ toParent: TreeNode<T> | null,
448
+ toIndex: number,
449
+ updateTree: (
450
+ items: TreeNode<T>[],
451
+ key: Key | null,
452
+ update: (node: TreeNode<T>) => TreeNode<T> | null,
453
+ originalMap: Map<Key, TreeNode<T>>
454
+ ) => TreeDataState<T>,
455
+ addNode: (node: TreeNode<T>, map: Map<Key, TreeNode<T>>) => void
456
+ ): TreeDataState<T> {
457
+ let {items, nodeMap} = state;
458
+
459
+ let parent = toParent;
460
+ let removeKeys = new Set(keys);
461
+ while (parent?.parentKey != null) {
462
+ if (removeKeys.has(parent.key)) {
463
+ throw new Error('Cannot move an item to be a child of itself.');
464
+ }
465
+ parent = nodeMap.get(parent.parentKey!) ?? null;
466
+ }
467
+
468
+ let originalToIndex = toIndex;
469
+
470
+ let keyArray = Array.isArray(keys) ? keys : [...keys];
471
+ // depth first search to put keys in order
472
+ let inOrderKeys: Map<Key, number> = new Map();
473
+ let removedItems: Array<TreeNode<T>> = [];
474
+ let newItems = items;
475
+ let newMap = nodeMap;
476
+ let i = 0;
477
+
478
+ function traversal(node, {inorder, postorder}) {
479
+ inorder?.(node);
480
+ if (node != null) {
481
+ for (let child of node.children ?? []) {
482
+ traversal(child, {inorder, postorder});
483
+ postorder?.(child);
484
+ }
485
+ }
486
+ }
487
+
488
+ function inorder(child) {
489
+ // in-order so we add items as we encounter them in the tree, then we can insert them in expected order later
490
+ if (keyArray.includes(child.key)) {
491
+ inOrderKeys.set(child.key, i++);
492
+ }
493
+ }
494
+
495
+ function postorder(child) {
496
+ // remove items and update the tree from the leaves and work upwards toward the root, this way
497
+ // we don't copy child node references from parents inadvertently
498
+ if (keyArray.includes(child.key)) {
499
+ removedItems.push({...newMap.get(child.key)!, parentKey: toParent?.key ?? null});
500
+ let {items: nextItems, nodeMap: nextMap} = updateTree(newItems, child.key, () => null, newMap);
501
+ newItems = nextItems;
502
+ newMap = nextMap;
503
+ }
504
+ // decrement the index if the child being removed is in the target parent and before the target index
505
+ if (child.parentKey === toParent?.key
506
+ && keyArray.includes(child.key)
507
+ && (toParent?.children ? toParent.children.indexOf(child) : items.indexOf(child)) < originalToIndex) {
508
+ toIndex--;
509
+ }
510
+ }
511
+
512
+ traversal({children: items}, {inorder, postorder});
513
+
514
+ let inOrderItems = removedItems.sort((a, b) => inOrderKeys.get(a.key)! > inOrderKeys.get(b.key)! ? 1 : -1);
515
+ // If parentKey is null, insert into the root.
516
+ if (!toParent || toParent.key == null) {
517
+ inOrderItems.forEach(movedNode => {
518
+ addNode(movedNode, newMap);
519
+ });
520
+ return {items: [
521
+ ...newItems.slice(0, toIndex),
522
+ ...inOrderItems,
523
+ ...newItems.slice(toIndex)
524
+ ], nodeMap: newMap};
525
+ }
526
+
527
+ // Otherwise, update the parent node and its ancestors.
528
+ return updateTree(newItems, toParent.key, parentNode => ({
529
+ key: parentNode.key,
530
+ parentKey: parentNode.parentKey,
531
+ value: parentNode.value,
532
+ children: [
533
+ ...parentNode.children!.slice(0, toIndex),
534
+ ...inOrderItems,
535
+ ...parentNode.children!.slice(toIndex)
536
+ ]
537
+ }), newMap);
538
+ }