react-arborist 2.2.0 → 3.0.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.
@@ -7,7 +7,6 @@ declare type Params<T> = {
7
7
  children: NodeApi<T>[] | null;
8
8
  parent: NodeApi<T> | null;
9
9
  isDraggable: boolean;
10
- isDroppable: boolean;
11
10
  rowIndex: number | null;
12
11
  tree: TreeApi<T>;
13
12
  };
@@ -19,7 +18,6 @@ export declare class NodeApi<T = any> {
19
18
  children: NodeApi<T>[] | null;
20
19
  parent: NodeApi<T> | null;
21
20
  isDraggable: boolean;
22
- isDroppable: boolean;
23
21
  rowIndex: number | null;
24
22
  constructor(params: Params<T>);
25
23
  get isRoot(): boolean;
@@ -27,6 +25,7 @@ export declare class NodeApi<T = any> {
27
25
  get isInternal(): boolean;
28
26
  get isOpen(): boolean;
29
27
  get isClosed(): boolean;
28
+ get isEditable(): boolean;
30
29
  get isEditing(): boolean;
31
30
  get isSelected(): boolean;
32
31
  get isOnlySelection(): boolean;
@@ -42,6 +42,7 @@ export declare class TreeApi<T> {
42
42
  get height(): number;
43
43
  get indent(): number;
44
44
  get rowHeight(): number;
45
+ get overscanCount(): number;
45
46
  get searchTerm(): string;
46
47
  get matchFn(): (node: NodeApi<T>) => boolean;
47
48
  accessChildren(data: T): T[] | null;
@@ -87,6 +88,8 @@ export declare class TreeApi<T> {
87
88
  selectAll(): void;
88
89
  get cursorParentId(): string | null;
89
90
  get cursorOverFolder(): boolean;
91
+ get dragNodes(): NodeApi<T>[];
92
+ canDrop(): boolean;
90
93
  hideCursor(): void;
91
94
  showCursor(cursor: Cursor): void;
92
95
  open(identity: Identity): void;
@@ -105,8 +108,8 @@ export declare class TreeApi<T> {
105
108
  get hasMultipleSelections(): boolean;
106
109
  isSelected(id?: string): boolean;
107
110
  isOpen(id?: string): boolean;
111
+ isEditable(data: T): boolean;
108
112
  isDraggable(data: T): boolean;
109
- isDroppable(data: T): boolean;
110
113
  isDragging(node: string | IdObj | null): boolean;
111
114
  isFocused(id: string): boolean;
112
115
  isMatch(node: NodeApi<T>): boolean;
package/dist/module.js CHANGED
@@ -180,7 +180,7 @@ function $0e6083160f4b36ed$export$58fe32731f07ed56(tree) {
180
180
  const focus = tree.focusedNode;
181
181
  if (!focus) return null;
182
182
  if (focus.isOpen) return focus.id;
183
- if (focus.parent) return focus.parent.id;
183
+ if (focus.parent && !focus.parent.isRoot) return focus.parent.id;
184
184
  return null;
185
185
  }
186
186
 
@@ -353,7 +353,6 @@ class $096e74084443e9a3$export$d4b903da0f522dc8 {
353
353
  this.children = params.children;
354
354
  this.parent = params.parent;
355
355
  this.isDraggable = params.isDraggable;
356
- this.isDroppable = params.isDroppable;
357
356
  this.rowIndex = params.rowIndex;
358
357
  }
359
358
  get isRoot() {
@@ -371,6 +370,9 @@ class $096e74084443e9a3$export$d4b903da0f522dc8 {
371
370
  get isClosed() {
372
371
  return this.isLeaf ? false : !this.tree.isOpen(this.id);
373
372
  }
373
+ get isEditable() {
374
+ return this.tree.isEditable(this.data);
375
+ }
374
376
  get isEditing() {
375
377
  return this.tree.editingId === this.id;
376
378
  }
@@ -493,7 +495,6 @@ function $81080a351c006222$export$882461b6382ed46c(tree) {
493
495
  id: id,
494
496
  children: null,
495
497
  isDraggable: tree.isDraggable(data),
496
- isDroppable: tree.isDroppable(data),
497
498
  rowIndex: null
498
499
  });
499
500
  const children = tree.accessChildren(data);
@@ -511,7 +512,6 @@ function $81080a351c006222$export$882461b6382ed46c(tree) {
511
512
  parent: null,
512
513
  children: null,
513
514
  isDraggable: true,
514
- isDroppable: true,
515
515
  rowIndex: null
516
516
  });
517
517
  const data = tree.props.data ?? [];
@@ -614,7 +614,10 @@ const $6ad32e02250c922e$export$d4c72bab9d6cc13a = (props)=>({
614
614
  cursor: {
615
615
  type: "none"
616
616
  },
617
- dragId: null
617
+ dragId: null,
618
+ dragIds: [],
619
+ parentId: null,
620
+ index: -1
618
621
  }
619
622
  });
620
623
 
@@ -710,16 +713,24 @@ const $59f144a8dd651e5e$export$e324594224ef24da = {
710
713
  cursor: cursor
711
714
  };
712
715
  },
713
- dragStart (id) {
716
+ dragStart (id, dragIds) {
714
717
  return {
715
718
  type: "DND_DRAG_START",
716
- id: id
719
+ id: id,
720
+ dragIds: dragIds
717
721
  };
718
722
  },
719
723
  dragEnd () {
720
724
  return {
721
725
  type: "DND_DRAG_END"
722
726
  };
727
+ },
728
+ hovering (parentId, index) {
729
+ return {
730
+ type: "DND_HOVERING",
731
+ parentId: parentId,
732
+ index: index
733
+ };
723
734
  }
724
735
  };
725
736
  function $59f144a8dd651e5e$export$1650419e431d3ba3(state = (0, $6ad32e02250c922e$export$d4c72bab9d6cc13a)()["dnd"], action) {
@@ -732,12 +743,16 @@ function $59f144a8dd651e5e$export$1650419e431d3ba3(state = (0, $6ad32e02250c922e
732
743
  case "DND_DRAG_START":
733
744
  return {
734
745
  ...state,
735
- dragId: action.id
746
+ dragId: action.id,
747
+ dragIds: action.dragIds
736
748
  };
737
749
  case "DND_DRAG_END":
750
+ return (0, $6ad32e02250c922e$export$d4c72bab9d6cc13a)()["dnd"];
751
+ case "DND_HOVERING":
738
752
  return {
739
753
  ...state,
740
- dragId: null
754
+ parentId: action.parentId,
755
+ index: action.index
741
756
  };
742
757
  default:
743
758
  return state;
@@ -925,32 +940,32 @@ function $907e707a330ef23a$export$715c0d031ede7907(node) {
925
940
  const [_, ref, preview] = (0, $g00cZ$useDrag)(()=>({
926
941
  canDrag: ()=>node.isDraggable,
927
942
  type: "NODE",
928
- item: ()=>({
929
- id: node.id,
930
- dragIds: tree.isSelected(node.id) ? Array.from(ids) : [
931
- node.id
932
- ]
933
- }),
934
- start: ()=>{
935
- tree.dispatch((0, $59f144a8dd651e5e$export$e324594224ef24da).dragStart(node.id));
943
+ item: ()=>{
944
+ // This is fired once at the begging of a drag operation
945
+ const dragIds = tree.isSelected(node.id) ? Array.from(ids) : [
946
+ node.id
947
+ ];
948
+ tree.dispatch((0, $59f144a8dd651e5e$export$e324594224ef24da).dragStart(node.id, dragIds));
949
+ return {
950
+ id: node.id
951
+ };
936
952
  },
937
- end: (item, monitor)=>{
938
- tree.dispatch((0, $59f144a8dd651e5e$export$e324594224ef24da).dragEnd());
953
+ end: ()=>{
939
954
  tree.hideCursor();
940
- const drop = monitor.getDropResult();
955
+ let { parentId: parentId , index: index , dragIds: dragIds } = tree.state.dnd;
941
956
  // If they held down meta, we need to create a copy
942
957
  // if (drop.dropEffect === "copy")
943
- if (drop && drop.parentId) {
944
- const parentId = drop.parentId === (0, $81080a351c006222$export$ec71a3379b43ae5c) ? null : drop.parentId;
958
+ if (tree.canDrop()) {
945
959
  (0, $0e6083160f4b36ed$export$c6d63370cef03886)(tree.props.onMove, {
946
- dragIds: item.dragIds,
947
- parentId: parentId,
948
- index: drop.index,
949
- dragNodes: item.dragIds.map((id)=>tree.get(id)),
950
- parentNode: tree.get(drop.parentId)
960
+ dragIds: dragIds,
961
+ parentId: parentId === (0, $81080a351c006222$export$ec71a3379b43ae5c) ? null : parentId,
962
+ index: index,
963
+ dragNodes: tree.dragNodes,
964
+ parentNode: tree.get(parentId)
951
965
  });
952
- tree.open(drop.parentId);
966
+ tree.open(parentId);
953
967
  }
968
+ tree.dispatch((0, $59f144a8dd651e5e$export$e324594224ef24da).dragEnd());
954
969
  }
955
970
  }), [
956
971
  ids,
@@ -968,7 +983,6 @@ function $907e707a330ef23a$export$715c0d031ede7907(node) {
968
983
 
969
984
 
970
985
 
971
-
972
986
  function $2db980bfed6822da$var$measureHover(el, offset) {
973
987
  const rect = el.getBoundingClientRect();
974
988
  const x = offset.x - Math.round(rect.x);
@@ -1035,16 +1049,6 @@ function $2db980bfed6822da$var$getDropLevel(hovering, aboveCursor, belowCursor,
1035
1049
  }
1036
1050
  return (0, $0e6083160f4b36ed$export$adf7c0fe6059d774)(hoverLevel, min, max);
1037
1051
  }
1038
- function $2db980bfed6822da$var$canDrop(above, below) {
1039
- if (!above) return true;
1040
- let n = above;
1041
- if ((0, $0e6083160f4b36ed$export$4210f5ea57fbae57)(above) && above !== below) n = above.parent;
1042
- while(n){
1043
- if (!n.isDroppable) return false;
1044
- n = n.parent;
1045
- }
1046
- return true;
1047
- }
1048
1052
  function $2db980bfed6822da$var$dropAt(parentId, index) {
1049
1053
  return {
1050
1054
  parentId: parentId || null,
@@ -1083,10 +1087,6 @@ function $2db980bfed6822da$export$f502ca02ebb85a1c(args) {
1083
1087
  const hover = $2db980bfed6822da$var$measureHover(args.element, args.offset);
1084
1088
  const { node: node , nextNode: nextNode , prevNode: prevNode } = args;
1085
1089
  const [above, below] = $2db980bfed6822da$var$getNodesAroundCursor(node, prevNode, nextNode, hover);
1086
- if (!$2db980bfed6822da$var$canDrop(above, below)) return {
1087
- drop: null,
1088
- cursor: $2db980bfed6822da$var$noCursor()
1089
- };
1090
1090
  /* Hovering over the middle of a folder */ if (node && node.isInternal && hover.inMiddle) return {
1091
1091
  drop: $2db980bfed6822da$var$dropAt(node.id, 0),
1092
1092
  cursor: $2db980bfed6822da$var$highlightCursor(node.id)
@@ -1109,15 +1109,16 @@ function $2db980bfed6822da$export$f502ca02ebb85a1c(args) {
1109
1109
  }
1110
1110
 
1111
1111
 
1112
+
1112
1113
  function $d38aa53467160173$export$57afafec4637d997(el, node) {
1113
1114
  const tree = (0, $89e93131aae74bd9$export$367b0f2231a90ba0)();
1114
1115
  const [_, dropRef] = (0, $g00cZ$useDrop)(()=>({
1115
1116
  accept: "NODE",
1116
- canDrop: (item, m)=>{
1117
- if (node.tree.isFiltered) return false;
1117
+ canDrop: ()=>tree.canDrop(),
1118
+ hover: (_item, m)=>{
1118
1119
  const offset = m.getClientOffset();
1119
- if (!el.current || !offset) return false;
1120
- const { drop: drop } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
1120
+ if (!el.current || !offset) return;
1121
+ const { cursor: cursor , drop: drop } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
1121
1122
  element: el.current,
1122
1123
  offset: offset,
1123
1124
  indent: tree.indent,
@@ -1125,43 +1126,13 @@ function $d38aa53467160173$export$57afafec4637d997(el, node) {
1125
1126
  prevNode: node.prev,
1126
1127
  nextNode: node.next
1127
1128
  });
1128
- if (!drop) return false;
1129
- const dropParent = tree.get(drop.parentId) ?? tree.root;
1130
- for (let id of item.dragIds){
1131
- const drag = tree.get(id);
1132
- if (!drag) return false;
1133
- if (!dropParent) return false;
1134
- if (drag.isInternal && (0, $0e6083160f4b36ed$export$1e38f72c6c546f70)(dropParent, drag)) return false;
1135
- }
1136
- return true;
1137
- },
1138
- hover: (item, m)=>{
1129
+ if (drop) tree.dispatch((0, $59f144a8dd651e5e$export$e324594224ef24da).hovering(drop.parentId, drop.index));
1139
1130
  if (m.canDrop()) {
1140
- const offset = m.getClientOffset();
1141
- if (!el.current || !offset) return;
1142
- const { cursor: cursor } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
1143
- element: el.current,
1144
- offset: offset,
1145
- indent: tree.indent,
1146
- node: node,
1147
- prevNode: node.prev,
1148
- nextNode: node.next
1149
- });
1150
1131
  if (cursor) tree.showCursor(cursor);
1151
1132
  } else tree.hideCursor();
1152
1133
  },
1153
- drop: (item, m)=>{
1154
- const offset = m.getClientOffset();
1155
- if (!el.current || !offset) return;
1156
- const { drop: drop } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
1157
- element: el.current,
1158
- offset: offset,
1159
- indent: tree.indent,
1160
- node: node,
1161
- prevNode: node.prev,
1162
- nextNode: node.next
1163
- });
1164
- return drop;
1134
+ drop: (_, m)=>{
1135
+ if (!m.canDrop()) return null;
1165
1136
  }
1166
1137
  }), [
1167
1138
  node,
@@ -1375,8 +1346,7 @@ function $065a164934293bf2$export$ff4858a4110d9246() {
1375
1346
  tree.selectAll();
1376
1347
  return;
1377
1348
  }
1378
- if (e.key === "a" && !e.metaKey) {
1379
- if (!tree.props.onCreate) return;
1349
+ if (e.key === "a" && !e.metaKey && tree.props.onCreate) {
1380
1350
  tree.createLeaf();
1381
1351
  return;
1382
1352
  }
@@ -1398,26 +1368,28 @@ function $065a164934293bf2$export$ff4858a4110d9246() {
1398
1368
  return;
1399
1369
  }
1400
1370
  if (e.key === "Enter") {
1401
- if (!tree.props.onRename) return;
1371
+ const node3 = tree.focusedNode;
1372
+ if (!node3) return;
1373
+ if (!node3.isEditable || !tree.props.onRename) return;
1402
1374
  setTimeout(()=>{
1403
- if (tree.focusedNode) tree.edit(tree.focusedNode);
1375
+ if (node3) tree.edit(node3);
1404
1376
  });
1405
1377
  return;
1406
1378
  }
1407
1379
  if (e.key === " ") {
1408
1380
  e.preventDefault();
1409
- const node3 = tree.focusedNode;
1410
- if (!node3) return;
1411
- if (node3.isLeaf) {
1412
- node3.select();
1413
- node3.activate();
1414
- } else node3.toggle();
1381
+ const node4 = tree.focusedNode;
1382
+ if (!node4) return;
1383
+ if (node4.isLeaf) {
1384
+ node4.select();
1385
+ node4.activate();
1386
+ } else node4.toggle();
1415
1387
  return;
1416
1388
  }
1417
1389
  if (e.key === "*") {
1418
- const node4 = tree.focusedNode;
1419
- if (!node4) return;
1420
- tree.openSiblings(node4);
1390
+ const node5 = tree.focusedNode;
1391
+ if (!node5) return;
1392
+ tree.openSiblings(node5);
1421
1393
  return;
1422
1394
  }
1423
1395
  if (e.key === "PageUp") {
@@ -1438,13 +1410,13 @@ function $065a164934293bf2$export$ff4858a4110d9246() {
1438
1410
  $065a164934293bf2$var$timeoutId = setTimeout(()=>{
1439
1411
  $065a164934293bf2$var$focusSearchTerm = "";
1440
1412
  }, 600);
1441
- const node5 = tree.visibleNodes.find((n)=>{
1413
+ const node6 = tree.visibleNodes.find((n)=>{
1442
1414
  // @ts-ignore
1443
1415
  const name = n.data.name;
1444
1416
  if (typeof name === "string") return name.toLowerCase().startsWith($065a164934293bf2$var$focusSearchTerm);
1445
1417
  else return false;
1446
1418
  });
1447
- if (node5) tree.focus(node5.id);
1419
+ if (node6) tree.focus(node6.id);
1448
1420
  },
1449
1421
  children: /*#__PURE__*/ (0, $g00cZ$jsx)((0, $g00cZ$FixedSizeList), {
1450
1422
  className: tree.props.className,
@@ -1453,6 +1425,7 @@ function $065a164934293bf2$export$ff4858a4110d9246() {
1453
1425
  height: tree.height,
1454
1426
  width: tree.width,
1455
1427
  itemSize: tree.rowHeight,
1428
+ overscanCount: tree.overscanCount,
1456
1429
  itemKey: (index)=>tree.visibleNodes[index]?.id || index,
1457
1430
  outerElementType: (0, $05f64c7ebcbad8b5$export$70c2b8898b86d3ad),
1458
1431
  innerElementType: (0, $da9a6b47b6fff922$export$a9af0da3ae60cd00),
@@ -1550,6 +1523,9 @@ class $bfece7c4aed4e9c4$export$e2da3477247342d1 {
1550
1523
  get rowHeight() {
1551
1524
  return this.props.rowHeight ?? 24;
1552
1525
  }
1526
+ get overscanCount() {
1527
+ return this.props.overscanCount ?? 1;
1528
+ }
1553
1529
  get searchTerm() {
1554
1530
  return (this.props.searchTerm || "").trim();
1555
1531
  }
@@ -1807,6 +1783,30 @@ class $bfece7c4aed4e9c4$export$e2da3477247342d1 {
1807
1783
  get cursorOverFolder() {
1808
1784
  return this.state.dnd.cursor.type === "highlight";
1809
1785
  }
1786
+ get dragNodes() {
1787
+ return this.state.dnd.dragIds.map((id)=>this.get(id)).filter((n)=>!!n);
1788
+ }
1789
+ canDrop() {
1790
+ if (this.isFiltered) return false;
1791
+ const parentNode = this.get(this.state.dnd.parentId) ?? this.root;
1792
+ const dragNodes = this.dragNodes;
1793
+ const check = this.props.disableDrop;
1794
+ for (const drag of dragNodes){
1795
+ if (!drag) return false;
1796
+ if (!parentNode) return false;
1797
+ if (drag.isInternal && $0e6083160f4b36ed$exports.isDecendent(parentNode, drag)) return false;
1798
+ }
1799
+ // Allow the user to insert their own logic
1800
+ if (typeof check == "function") return check({
1801
+ parentNode: parentNode,
1802
+ dragNodes: this.dragNodes,
1803
+ index: this.state.dnd.index
1804
+ });
1805
+ else if (typeof check == "string") // @ts-ignore
1806
+ return !!parentNode.data[check];
1807
+ else if (typeof check === "boolean") return check;
1808
+ else return true;
1809
+ }
1810
1810
  hideCursor() {
1811
1811
  this.dispatch((0, $59f144a8dd651e5e$export$e324594224ef24da).cursor({
1812
1812
  type: "none"
@@ -1904,12 +1904,12 @@ class $bfece7c4aed4e9c4$export$e2da3477247342d1 {
1904
1904
  if (this.isFiltered) return this.state.nodes.open.filtered[id] ?? true; // Filtered folders are always opened by default
1905
1905
  else return this.state.nodes.open.unfiltered[id] ?? def;
1906
1906
  }
1907
- isDraggable(data) {
1908
- const check = this.props.disableDrag || (()=>false);
1907
+ isEditable(data) {
1908
+ const check = this.props.disableEdit || (()=>false);
1909
1909
  return !$0e6083160f4b36ed$exports.access(data, check) ?? true;
1910
1910
  }
1911
- isDroppable(data) {
1912
- const check = this.props.disableDrop || (()=>false);
1911
+ isDraggable(data) {
1912
+ const check = this.props.disableDrag || (()=>false);
1913
1913
  return !$0e6083160f4b36ed$exports.access(data, check) ?? true;
1914
1914
  }
1915
1915
  isDragging(node) {
@@ -2072,54 +2072,19 @@ function $e739455e59c6aed3$export$5a6c424b1725f44f() {
2072
2072
  // In case we drop an item at the bottom of the list
2073
2073
  const [, drop] = (0, $g00cZ$useDrop)(()=>({
2074
2074
  accept: "NODE",
2075
- hover: (item, m)=>{
2075
+ canDrop: (_item, m)=>{
2076
2076
  if (!m.isOver({
2077
2077
  shallow: true
2078
- })) return;
2079
- if (m.canDrop()) {
2080
- const offset = m.getClientOffset();
2081
- if (!tree.listEl.current || !offset) return;
2082
- const { cursor: cursor } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
2083
- element: tree.listEl.current,
2084
- offset: offset,
2085
- indent: tree.indent,
2086
- node: null,
2087
- prevNode: tree.visibleNodes[tree.visibleNodes.length - 1],
2088
- nextNode: null
2089
- });
2090
- if (cursor) tree.showCursor(cursor);
2091
- } else tree.hideCursor();
2078
+ })) return false;
2079
+ return tree.canDrop();
2092
2080
  },
2093
- canDrop: (item, m)=>{
2081
+ hover: (_item, m)=>{
2094
2082
  if (!m.isOver({
2095
2083
  shallow: true
2096
- })) return false;
2097
- if (tree.isFiltered) return false;
2098
- const offset = m.getClientOffset();
2099
- if (!tree.listEl.current || !offset) return false;
2100
- const { drop: drop } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
2101
- element: tree.listEl.current,
2102
- offset: offset,
2103
- indent: tree.indent,
2104
- node: null,
2105
- prevNode: tree.visibleNodes[tree.visibleNodes.length - 1],
2106
- nextNode: null
2107
- });
2108
- if (!drop) return false;
2109
- const dropParent = tree.get(drop.parentId) ?? tree.root;
2110
- for (let id of item.dragIds){
2111
- const drag = tree.get(id);
2112
- if (!drag) return false;
2113
- if (!dropParent) return false;
2114
- if (drag.isInternal && (0, $0e6083160f4b36ed$export$1e38f72c6c546f70)(dropParent, drag)) return false;
2115
- }
2116
- return true;
2117
- },
2118
- drop: (item, m)=>{
2119
- if (m.didDrop()) return;
2084
+ })) return;
2120
2085
  const offset = m.getClientOffset();
2121
2086
  if (!tree.listEl.current || !offset) return;
2122
- const { drop: drop } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
2087
+ const { cursor: cursor , drop: drop } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
2123
2088
  element: tree.listEl.current,
2124
2089
  offset: offset,
2125
2090
  indent: tree.indent,
@@ -2127,7 +2092,10 @@ function $e739455e59c6aed3$export$5a6c424b1725f44f() {
2127
2092
  prevNode: tree.visibleNodes[tree.visibleNodes.length - 1],
2128
2093
  nextNode: null
2129
2094
  });
2130
- return drop;
2095
+ if (drop) tree.dispatch((0, $59f144a8dd651e5e$export$e324594224ef24da).hovering(drop.parentId, drop.index));
2096
+ if (m.canDrop()) {
2097
+ if (cursor) tree.showCursor(cursor);
2098
+ } else tree.hideCursor();
2131
2099
  }
2132
2100
  }), [
2133
2101
  tree