@react-stately/data 3.11.3-nightly.4555 → 3.11.3-nightly.4560

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 CHANGED
@@ -1,26 +1,8 @@
1
- import {useReducer as $d70Aq$useReducer, useRef as $d70Aq$useRef, useEffect as $d70Aq$useEffect, useState as $d70Aq$useState, useMemo as $d70Aq$useMemo} from "react";
1
+ import {useAsyncList as $f86e6c1ec7da6ebb$export$bc3384a35de93d66} from "./useAsyncList.mjs";
2
+ import {useTreeData as $be2ea0343af54212$export$d14e1352e21f4a16} from "./useTreeData.mjs";
3
+ import {useListData as $0d86e9c8f07f9a7b$export$762f73dccccd255d} from "./useListData.mjs";
2
4
 
3
5
  /*
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
- * Copyright 2020 Adobe. All rights reserved.
15
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
16
- * you may not use this file except in compliance with the License. You may obtain a copy
17
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
18
- *
19
- * Unless required by applicable law or agreed to in writing, software distributed under
20
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
21
- * OF ANY KIND, either express or implied. See the License for the specific language
22
- * governing permissions and limitations under the License.
23
- */ /*
24
6
  * Copyright 2020 Adobe. All rights reserved.
25
7
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
26
8
  * you may not use this file except in compliance with the License. You may obtain a copy
@@ -31,677 +13,6 @@ import {useReducer as $d70Aq$useReducer, useRef as $d70Aq$useRef, useEffect as $
31
13
  * OF ANY KIND, either express or implied. See the License for the specific language
32
14
  * governing permissions and limitations under the License.
33
15
  */
34
- function $0d86e9c8f07f9a7b$export$762f73dccccd255d(options) {
35
- let { initialItems: initialItems = [], initialSelectedKeys: initialSelectedKeys, getKey: getKey = (item)=>item.id || item.key, filter: filter, initialFilterText: initialFilterText = "" } = options;
36
- // Store both items and filteredItems in state so we can go back to the unfiltered list
37
- let [state, setState] = (0, $d70Aq$useState)({
38
- items: initialItems,
39
- selectedKeys: initialSelectedKeys === "all" ? "all" : new Set(initialSelectedKeys || []),
40
- filterText: initialFilterText
41
- });
42
- let filteredItems = (0, $d70Aq$useMemo)(()=>filter ? state.items.filter((item)=>filter(item, state.filterText)) : state.items, [
43
- state.items,
44
- state.filterText,
45
- filter
46
- ]);
47
- return {
48
- ...state,
49
- items: filteredItems,
50
- ...$0d86e9c8f07f9a7b$export$79c0c687a5963b0a({
51
- getKey: getKey
52
- }, setState),
53
- getItem (key) {
54
- return state.items.find((item)=>getKey(item) === key);
55
- }
56
- };
57
- }
58
- function $0d86e9c8f07f9a7b$export$79c0c687a5963b0a(opts, dispatch) {
59
- let { cursor: cursor, getKey: getKey } = opts;
60
- return {
61
- setSelectedKeys (selectedKeys) {
62
- dispatch((state)=>({
63
- ...state,
64
- selectedKeys: selectedKeys
65
- }));
66
- },
67
- setFilterText (filterText) {
68
- dispatch((state)=>({
69
- ...state,
70
- filterText: filterText
71
- }));
72
- },
73
- insert (index, ...values) {
74
- dispatch((state)=>$0d86e9c8f07f9a7b$var$insert(state, index, ...values));
75
- },
76
- insertBefore (key, ...values) {
77
- dispatch((state)=>{
78
- let index = state.items.findIndex((item)=>getKey(item) === key);
79
- if (index === -1) {
80
- if (state.items.length === 0) index = 0;
81
- else return state;
82
- }
83
- return $0d86e9c8f07f9a7b$var$insert(state, index, ...values);
84
- });
85
- },
86
- insertAfter (key, ...values) {
87
- dispatch((state)=>{
88
- let index = state.items.findIndex((item)=>getKey(item) === key);
89
- if (index === -1) {
90
- if (state.items.length === 0) index = 0;
91
- else return state;
92
- }
93
- return $0d86e9c8f07f9a7b$var$insert(state, index + 1, ...values);
94
- });
95
- },
96
- prepend (...values) {
97
- dispatch((state)=>$0d86e9c8f07f9a7b$var$insert(state, 0, ...values));
98
- },
99
- append (...values) {
100
- dispatch((state)=>$0d86e9c8f07f9a7b$var$insert(state, state.items.length, ...values));
101
- },
102
- remove (...keys) {
103
- dispatch((state)=>{
104
- let keySet = new Set(keys);
105
- let items = state.items.filter((item)=>!keySet.has(getKey(item)));
106
- let selection = "all";
107
- if (state.selectedKeys !== "all") {
108
- selection = new Set(state.selectedKeys);
109
- for (let key of keys)selection.delete(key);
110
- }
111
- if (cursor == null && items.length === 0) selection = new Set();
112
- return {
113
- ...state,
114
- items: items,
115
- selectedKeys: selection
116
- };
117
- });
118
- },
119
- removeSelectedItems () {
120
- dispatch((state)=>{
121
- if (state.selectedKeys === "all") return {
122
- ...state,
123
- items: [],
124
- selectedKeys: new Set()
125
- };
126
- let selectedKeys = state.selectedKeys;
127
- let items = state.items.filter((item)=>!selectedKeys.has(getKey(item)));
128
- return {
129
- ...state,
130
- items: items,
131
- selectedKeys: new Set()
132
- };
133
- });
134
- },
135
- move (key, toIndex) {
136
- dispatch((state)=>{
137
- let index = state.items.findIndex((item)=>getKey(item) === key);
138
- if (index === -1) return state;
139
- let copy = state.items.slice();
140
- let [item] = copy.splice(index, 1);
141
- copy.splice(toIndex, 0, item);
142
- return {
143
- ...state,
144
- items: copy
145
- };
146
- });
147
- },
148
- moveBefore (key, keys) {
149
- dispatch((state)=>{
150
- let toIndex = state.items.findIndex((item)=>getKey(item) === key);
151
- if (toIndex === -1) return state;
152
- // Find indices of keys to move. Sort them so that the order in the list is retained.
153
- let keyArray = Array.isArray(keys) ? keys : [
154
- ...keys
155
- ];
156
- let indices = keyArray.map((key)=>state.items.findIndex((item)=>getKey(item) === key)).sort((a, b)=>a - b);
157
- return $0d86e9c8f07f9a7b$var$move(state, indices, toIndex);
158
- });
159
- },
160
- moveAfter (key, keys) {
161
- dispatch((state)=>{
162
- let toIndex = state.items.findIndex((item)=>getKey(item) === key);
163
- if (toIndex === -1) return state;
164
- let keyArray = Array.isArray(keys) ? keys : [
165
- ...keys
166
- ];
167
- let indices = keyArray.map((key)=>state.items.findIndex((item)=>getKey(item) === key)).sort((a, b)=>a - b);
168
- return $0d86e9c8f07f9a7b$var$move(state, indices, toIndex + 1);
169
- });
170
- },
171
- update (key, newValue) {
172
- dispatch((state)=>{
173
- let index = state.items.findIndex((item)=>getKey(item) === key);
174
- if (index === -1) return state;
175
- return {
176
- ...state,
177
- items: [
178
- ...state.items.slice(0, index),
179
- newValue,
180
- ...state.items.slice(index + 1)
181
- ]
182
- };
183
- });
184
- }
185
- };
186
- }
187
- function $0d86e9c8f07f9a7b$var$insert(state, index, ...values) {
188
- return {
189
- ...state,
190
- items: [
191
- ...state.items.slice(0, index),
192
- ...values,
193
- ...state.items.slice(index)
194
- ]
195
- };
196
- }
197
- function $0d86e9c8f07f9a7b$var$move(state, indices, toIndex) {
198
- // Shift the target down by the number of items being moved from before the target
199
- toIndex -= indices.filter((index)=>index < toIndex).length;
200
- let moves = indices.map((from)=>({
201
- from: from,
202
- to: toIndex++
203
- }));
204
- // Shift later from indices down if they have a larger index
205
- for(let i = 0; i < moves.length; i++){
206
- let a = moves[i].from;
207
- for(let j = i; j < moves.length; j++){
208
- let b = moves[j].from;
209
- if (b > a) moves[j].from--;
210
- }
211
- }
212
- // Interleave the moves so they can be applied one by one rather than all at once
213
- for(let i = 0; i < moves.length; i++){
214
- let a = moves[i];
215
- for(let j = moves.length - 1; j > i; j--){
216
- let b = moves[j];
217
- if (b.from < a.to) a.to++;
218
- else b.from++;
219
- }
220
- }
221
- let copy = state.items.slice();
222
- for (let move of moves){
223
- let [item] = copy.splice(move.from, 1);
224
- copy.splice(move.to, 0, item);
225
- }
226
- return {
227
- ...state,
228
- items: copy
229
- };
230
- }
231
-
232
-
233
-
234
- function $f86e6c1ec7da6ebb$var$reducer(data, action) {
235
- let selectedKeys;
236
- switch(data.state){
237
- case "idle":
238
- case "error":
239
- switch(action.type){
240
- case "loading":
241
- case "loadingMore":
242
- case "sorting":
243
- case "filtering":
244
- var _action_filterText, _action_sortDescriptor;
245
- return {
246
- ...data,
247
- filterText: (_action_filterText = action.filterText) !== null && _action_filterText !== void 0 ? _action_filterText : data.filterText,
248
- state: action.type,
249
- // Reset items to an empty list if loading, but not when sorting.
250
- items: action.type === "loading" ? [] : data.items,
251
- sortDescriptor: (_action_sortDescriptor = action.sortDescriptor) !== null && _action_sortDescriptor !== void 0 ? _action_sortDescriptor : data.sortDescriptor,
252
- abortController: action.abortController
253
- };
254
- case "update":
255
- return {
256
- ...data,
257
- ...action.updater(data)
258
- };
259
- case "success":
260
- case "error":
261
- return data;
262
- default:
263
- throw new Error(`Invalid action "${action.type}" in state "${data.state}"`);
264
- }
265
- case "loading":
266
- case "sorting":
267
- case "filtering":
268
- switch(action.type){
269
- case "success":
270
- // Ignore if there is a newer abortcontroller in state.
271
- // This means that multiple requests were going at once.
272
- // We want to take only the latest result.
273
- if (action.abortController !== data.abortController) return data;
274
- var _action_selectedKeys;
275
- selectedKeys = (_action_selectedKeys = action.selectedKeys) !== null && _action_selectedKeys !== void 0 ? _action_selectedKeys : data.selectedKeys;
276
- var _action_filterText1, _action_sortDescriptor1;
277
- return {
278
- ...data,
279
- filterText: (_action_filterText1 = action.filterText) !== null && _action_filterText1 !== void 0 ? _action_filterText1 : data.filterText,
280
- state: "idle",
281
- items: [
282
- ...action.items
283
- ],
284
- selectedKeys: selectedKeys === "all" ? "all" : new Set(selectedKeys),
285
- sortDescriptor: (_action_sortDescriptor1 = action.sortDescriptor) !== null && _action_sortDescriptor1 !== void 0 ? _action_sortDescriptor1 : data.sortDescriptor,
286
- abortController: null,
287
- cursor: action.cursor
288
- };
289
- case "error":
290
- if (action.abortController !== data.abortController) return data;
291
- return {
292
- ...data,
293
- state: "error",
294
- error: action.error,
295
- abortController: null
296
- };
297
- case "loading":
298
- case "loadingMore":
299
- case "sorting":
300
- case "filtering":
301
- // We're already loading, and another load was triggered at the same time.
302
- // We need to abort the previous load and start a new one.
303
- data.abortController.abort();
304
- var _action_filterText2;
305
- return {
306
- ...data,
307
- filterText: (_action_filterText2 = action.filterText) !== null && _action_filterText2 !== void 0 ? _action_filterText2 : data.filterText,
308
- state: action.type,
309
- // Reset items to an empty list if loading, but not when sorting.
310
- items: action.type === "loading" ? [] : data.items,
311
- abortController: action.abortController
312
- };
313
- case "update":
314
- // We're already loading, and an update happened at the same time (e.g. selectedKey changed).
315
- // Update data but don't abort previous load.
316
- return {
317
- ...data,
318
- ...action.updater(data)
319
- };
320
- default:
321
- throw new Error(`Invalid action "${action.type}" in state "${data.state}"`);
322
- }
323
- case "loadingMore":
324
- switch(action.type){
325
- case "success":
326
- var _action_selectedKeys1;
327
- selectedKeys = data.selectedKeys === "all" || action.selectedKeys === "all" ? "all" : new Set([
328
- ...data.selectedKeys,
329
- ...(_action_selectedKeys1 = action.selectedKeys) !== null && _action_selectedKeys1 !== void 0 ? _action_selectedKeys1 : []
330
- ]);
331
- var _action_sortDescriptor2;
332
- // Append the new items
333
- return {
334
- ...data,
335
- state: "idle",
336
- items: [
337
- ...data.items,
338
- ...action.items
339
- ],
340
- selectedKeys: selectedKeys,
341
- sortDescriptor: (_action_sortDescriptor2 = action.sortDescriptor) !== null && _action_sortDescriptor2 !== void 0 ? _action_sortDescriptor2 : data.sortDescriptor,
342
- abortController: null,
343
- cursor: action.cursor
344
- };
345
- case "error":
346
- if (action.abortController !== data.abortController) return data;
347
- return {
348
- ...data,
349
- state: "error",
350
- error: action.error
351
- };
352
- case "loading":
353
- case "sorting":
354
- case "filtering":
355
- // We're already loading more, and another load was triggered at the same time.
356
- // We need to abort the previous load more and start a new one.
357
- data.abortController.abort();
358
- var _action_filterText3;
359
- return {
360
- ...data,
361
- filterText: (_action_filterText3 = action.filterText) !== null && _action_filterText3 !== void 0 ? _action_filterText3 : data.filterText,
362
- state: action.type,
363
- // Reset items to an empty list if loading, but not when sorting.
364
- items: action.type === "loading" ? [] : data.items,
365
- abortController: action.abortController
366
- };
367
- case "loadingMore":
368
- // If already loading more and another loading more is triggered, abort the new load more since
369
- // it is a duplicate request since the cursor hasn't been updated.
370
- // Do not overwrite the data.abortController
371
- action.abortController.abort();
372
- return data;
373
- case "update":
374
- // We're already loading, and an update happened at the same time (e.g. selectedKey changed).
375
- // Update data but don't abort previous load.
376
- return {
377
- ...data,
378
- ...action.updater(data)
379
- };
380
- default:
381
- throw new Error(`Invalid action "${action.type}" in state "${data.state}"`);
382
- }
383
- default:
384
- throw new Error(`Invalid state "${data.state}"`);
385
- }
386
- }
387
- function $f86e6c1ec7da6ebb$export$bc3384a35de93d66(options) {
388
- const { load: load, sort: sort, initialSelectedKeys: initialSelectedKeys, initialSortDescriptor: initialSortDescriptor, getKey: getKey = (item)=>item.id || item.key, initialFilterText: initialFilterText = "" } = options;
389
- let [data, dispatch] = (0, $d70Aq$useReducer)($f86e6c1ec7da6ebb$var$reducer, {
390
- state: "idle",
391
- error: null,
392
- items: [],
393
- selectedKeys: initialSelectedKeys === "all" ? "all" : new Set(initialSelectedKeys),
394
- sortDescriptor: initialSortDescriptor,
395
- filterText: initialFilterText
396
- });
397
- const dispatchFetch = async (action, fn)=>{
398
- let abortController = new AbortController();
399
- try {
400
- dispatch({
401
- ...action,
402
- abortController: abortController
403
- });
404
- var _action_filterText;
405
- let previousFilterText = (_action_filterText = action.filterText) !== null && _action_filterText !== void 0 ? _action_filterText : data.filterText;
406
- var _action_sortDescriptor;
407
- let response = await fn({
408
- items: data.items.slice(),
409
- selectedKeys: data.selectedKeys,
410
- sortDescriptor: (_action_sortDescriptor = action.sortDescriptor) !== null && _action_sortDescriptor !== void 0 ? _action_sortDescriptor : data.sortDescriptor,
411
- signal: abortController.signal,
412
- cursor: action.type === "loadingMore" ? data.cursor : null,
413
- filterText: previousFilterText
414
- });
415
- var _response_filterText;
416
- let filterText = (_response_filterText = response.filterText) !== null && _response_filterText !== void 0 ? _response_filterText : previousFilterText;
417
- dispatch({
418
- type: "success",
419
- ...response,
420
- abortController: abortController
421
- });
422
- // Fetch a new filtered list if filterText is updated via `load` response func rather than list.setFilterText
423
- // Only do this if not aborted (e.g. user triggers another filter action before load completes)
424
- if (filterText && filterText !== previousFilterText && !abortController.signal.aborted) dispatchFetch({
425
- type: "filtering",
426
- filterText: filterText
427
- }, load);
428
- } catch (e) {
429
- dispatch({
430
- type: "error",
431
- error: e,
432
- abortController: abortController
433
- });
434
- }
435
- };
436
- let didDispatchInitialFetch = (0, $d70Aq$useRef)(false);
437
- (0, $d70Aq$useEffect)(()=>{
438
- if (!didDispatchInitialFetch.current) {
439
- dispatchFetch({
440
- type: "loading"
441
- }, load);
442
- didDispatchInitialFetch.current = true;
443
- }
444
- // eslint-disable-next-line react-hooks/exhaustive-deps
445
- }, []);
446
- return {
447
- items: data.items,
448
- selectedKeys: data.selectedKeys,
449
- sortDescriptor: data.sortDescriptor,
450
- isLoading: data.state === "loading" || data.state === "loadingMore" || data.state === "sorting" || data.state === "filtering",
451
- loadingState: data.state,
452
- error: data.error,
453
- filterText: data.filterText,
454
- getItem (key) {
455
- return data.items.find((item)=>getKey(item) === key);
456
- },
457
- reload () {
458
- dispatchFetch({
459
- type: "loading"
460
- }, load);
461
- },
462
- loadMore () {
463
- // Ignore if already loading more or if performing server side filtering.
464
- if (data.state === "loadingMore" || data.state === "filtering" || data.cursor == null) return;
465
- dispatchFetch({
466
- type: "loadingMore"
467
- }, load);
468
- },
469
- sort (sortDescriptor) {
470
- dispatchFetch({
471
- type: "sorting",
472
- sortDescriptor: sortDescriptor
473
- }, sort || load);
474
- },
475
- ...(0, $0d86e9c8f07f9a7b$export$79c0c687a5963b0a)({
476
- ...options,
477
- getKey: getKey,
478
- cursor: data.cursor
479
- }, (fn)=>{
480
- dispatch({
481
- type: "update",
482
- updater: fn
483
- });
484
- }),
485
- setFilterText (filterText) {
486
- dispatchFetch({
487
- type: "filtering",
488
- filterText: filterText
489
- }, load);
490
- }
491
- };
492
- }
493
-
494
-
495
- /*
496
- * Copyright 2020 Adobe. All rights reserved.
497
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
498
- * you may not use this file except in compliance with the License. You may obtain a copy
499
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
500
- *
501
- * Unless required by applicable law or agreed to in writing, software distributed under
502
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
503
- * OF ANY KIND, either express or implied. See the License for the specific language
504
- * governing permissions and limitations under the License.
505
- */
506
- function $be2ea0343af54212$export$d14e1352e21f4a16(options) {
507
- let { initialItems: initialItems = [], initialSelectedKeys: initialSelectedKeys, getKey: getKey = (item)=>item.id || item.key, getChildren: getChildren = (item)=>item.children } = options;
508
- // We only want to compute this on initial render.
509
- let [tree, setItems] = (0, $d70Aq$useState)(()=>buildTree(initialItems, new Map()));
510
- let { items: items, nodeMap: nodeMap } = tree;
511
- let [selectedKeys, setSelectedKeys] = (0, $d70Aq$useState)(new Set(initialSelectedKeys || []));
512
- function buildTree(initialItems = [], map, parentKey) {
513
- return {
514
- items: initialItems.map((item)=>{
515
- let node = {
516
- key: getKey(item),
517
- parentKey: parentKey,
518
- value: item,
519
- children: null
520
- };
521
- node.children = buildTree(getChildren(item), map, node.key).items;
522
- map.set(node.key, node);
523
- return node;
524
- }),
525
- nodeMap: map
526
- };
527
- }
528
- function updateTree(items, key, update, originalMap) {
529
- let node = originalMap.get(key);
530
- if (!node) return {
531
- items: items,
532
- nodeMap: originalMap
533
- };
534
- let map = new Map(originalMap);
535
- // Create a new node. If null, then delete the node, otherwise replace.
536
- let newNode = update(node);
537
- if (newNode == null) deleteNode(node, map);
538
- else addNode(newNode, map);
539
- // Walk up the tree and update each parent to refer to the new children.
540
- while(node.parentKey){
541
- let nextParent = map.get(node.parentKey);
542
- let copy = {
543
- key: nextParent.key,
544
- parentKey: nextParent.parentKey,
545
- value: nextParent.value,
546
- children: null
547
- };
548
- let children = nextParent.children;
549
- if (newNode == null) children = children.filter((c)=>c !== node);
550
- copy.children = children.map((child)=>{
551
- if (child === node) return newNode;
552
- return child;
553
- });
554
- map.set(copy.key, copy);
555
- newNode = copy;
556
- node = nextParent;
557
- }
558
- if (newNode == null) items = items.filter((c)=>c !== node);
559
- return {
560
- items: items.map((item)=>{
561
- if (item === node) return newNode;
562
- return item;
563
- }),
564
- nodeMap: map
565
- };
566
- }
567
- function addNode(node, map) {
568
- map.set(node.key, node);
569
- for (let child of node.children)addNode(child, map);
570
- }
571
- function deleteNode(node, map) {
572
- map.delete(node.key);
573
- for (let child of node.children)deleteNode(child, map);
574
- }
575
- return {
576
- items: items,
577
- selectedKeys: selectedKeys,
578
- setSelectedKeys: setSelectedKeys,
579
- getItem (key) {
580
- return nodeMap.get(key);
581
- },
582
- insert (parentKey, index, ...values) {
583
- setItems(({ items: items, nodeMap: originalMap })=>{
584
- let { items: newNodes, nodeMap: newMap } = buildTree(values, originalMap, parentKey);
585
- // If parentKey is null, insert into the root.
586
- if (parentKey == null) return {
587
- items: [
588
- ...items.slice(0, index),
589
- ...newNodes,
590
- ...items.slice(index)
591
- ],
592
- nodeMap: newMap
593
- };
594
- // Otherwise, update the parent node and its ancestors.
595
- return updateTree(items, parentKey, (parentNode)=>({
596
- key: parentNode.key,
597
- parentKey: parentNode.parentKey,
598
- value: parentNode.value,
599
- children: [
600
- ...parentNode.children.slice(0, index),
601
- ...newNodes,
602
- ...parentNode.children.slice(index)
603
- ]
604
- }), newMap);
605
- });
606
- },
607
- insertBefore (key, ...values) {
608
- let node = nodeMap.get(key);
609
- if (!node) return;
610
- let parentNode = nodeMap.get(node.parentKey);
611
- let nodes = parentNode ? parentNode.children : items;
612
- let index = nodes.indexOf(node);
613
- this.insert(parentNode === null || parentNode === void 0 ? void 0 : parentNode.key, index, ...values);
614
- },
615
- insertAfter (key, ...values) {
616
- let node = nodeMap.get(key);
617
- if (!node) return;
618
- let parentNode = nodeMap.get(node.parentKey);
619
- let nodes = parentNode ? parentNode.children : items;
620
- let index = nodes.indexOf(node);
621
- this.insert(parentNode === null || parentNode === void 0 ? void 0 : parentNode.key, index + 1, ...values);
622
- },
623
- prepend (parentKey, ...values) {
624
- this.insert(parentKey, 0, ...values);
625
- },
626
- append (parentKey, ...values) {
627
- if (parentKey == null) this.insert(null, items.length, ...values);
628
- else {
629
- let parentNode = nodeMap.get(parentKey);
630
- if (!parentNode) return;
631
- this.insert(parentKey, parentNode.children.length, ...values);
632
- }
633
- },
634
- remove (...keys) {
635
- if (keys.length === 0) return;
636
- let newItems = items;
637
- let prevMap = nodeMap;
638
- let newTree;
639
- for (let key of keys){
640
- newTree = updateTree(newItems, key, ()=>null, prevMap);
641
- prevMap = newTree.nodeMap;
642
- newItems = newTree.items;
643
- }
644
- setItems(newTree);
645
- let selection = new Set(selectedKeys);
646
- for (let key of selectedKeys)if (!newTree.nodeMap.has(key)) selection.delete(key);
647
- setSelectedKeys(selection);
648
- },
649
- removeSelectedItems () {
650
- this.remove(...selectedKeys);
651
- },
652
- move (key, toParentKey, index) {
653
- setItems(({ items: items, nodeMap: originalMap })=>{
654
- let node = originalMap.get(key);
655
- if (!node) return {
656
- items: items,
657
- nodeMap: originalMap
658
- };
659
- let { items: newItems, nodeMap: newMap } = updateTree(items, key, ()=>null, originalMap);
660
- const movedNode = {
661
- ...node,
662
- parentKey: toParentKey
663
- };
664
- // If parentKey is null, insert into the root.
665
- if (toParentKey == null) {
666
- newMap.set(movedNode.key, movedNode);
667
- return {
668
- items: [
669
- ...newItems.slice(0, index),
670
- movedNode,
671
- ...newItems.slice(index)
672
- ],
673
- nodeMap: newMap
674
- };
675
- }
676
- // Otherwise, update the parent node and its ancestors.
677
- return updateTree(newItems, toParentKey, (parentNode)=>({
678
- key: parentNode.key,
679
- parentKey: parentNode.parentKey,
680
- value: parentNode.value,
681
- children: [
682
- ...parentNode.children.slice(0, index),
683
- movedNode,
684
- ...parentNode.children.slice(index)
685
- ]
686
- }), newMap);
687
- });
688
- },
689
- update (oldKey, newValue) {
690
- setItems(({ items: items, nodeMap: originalMap })=>updateTree(items, oldKey, (oldNode)=>{
691
- let node = {
692
- key: oldNode.key,
693
- parentKey: oldNode.parentKey,
694
- value: newValue,
695
- children: null
696
- };
697
- let tree = buildTree(getChildren(newValue), originalMap, node.key);
698
- node.children = tree.items;
699
- return node;
700
- }, originalMap));
701
- }
702
- };
703
- }
704
-
705
16
 
706
17
 
707
18