react-arborist 2.3.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;
@@ -88,6 +88,8 @@ export declare class TreeApi<T> {
88
88
  selectAll(): void;
89
89
  get cursorParentId(): string | null;
90
90
  get cursorOverFolder(): boolean;
91
+ get dragNodes(): NodeApi<T>[];
92
+ canDrop(): boolean;
91
93
  hideCursor(): void;
92
94
  showCursor(cursor: Cursor): void;
93
95
  open(identity: Identity): void;
@@ -106,8 +108,8 @@ export declare class TreeApi<T> {
106
108
  get hasMultipleSelections(): boolean;
107
109
  isSelected(id?: string): boolean;
108
110
  isOpen(id?: string): boolean;
111
+ isEditable(data: T): boolean;
109
112
  isDraggable(data: T): boolean;
110
- isDroppable(data: T): boolean;
111
113
  isDragging(node: string | IdObj | null): boolean;
112
114
  isFocused(id: string): boolean;
113
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,
@@ -1397,26 +1368,28 @@ function $065a164934293bf2$export$ff4858a4110d9246() {
1397
1368
  return;
1398
1369
  }
1399
1370
  if (e.key === "Enter") {
1400
- if (!tree.props.onRename) return;
1371
+ const node3 = tree.focusedNode;
1372
+ if (!node3) return;
1373
+ if (!node3.isEditable || !tree.props.onRename) return;
1401
1374
  setTimeout(()=>{
1402
- if (tree.focusedNode) tree.edit(tree.focusedNode);
1375
+ if (node3) tree.edit(node3);
1403
1376
  });
1404
1377
  return;
1405
1378
  }
1406
1379
  if (e.key === " ") {
1407
1380
  e.preventDefault();
1408
- const node3 = tree.focusedNode;
1409
- if (!node3) return;
1410
- if (node3.isLeaf) {
1411
- node3.select();
1412
- node3.activate();
1413
- } 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();
1414
1387
  return;
1415
1388
  }
1416
1389
  if (e.key === "*") {
1417
- const node4 = tree.focusedNode;
1418
- if (!node4) return;
1419
- tree.openSiblings(node4);
1390
+ const node5 = tree.focusedNode;
1391
+ if (!node5) return;
1392
+ tree.openSiblings(node5);
1420
1393
  return;
1421
1394
  }
1422
1395
  if (e.key === "PageUp") {
@@ -1437,13 +1410,13 @@ function $065a164934293bf2$export$ff4858a4110d9246() {
1437
1410
  $065a164934293bf2$var$timeoutId = setTimeout(()=>{
1438
1411
  $065a164934293bf2$var$focusSearchTerm = "";
1439
1412
  }, 600);
1440
- const node5 = tree.visibleNodes.find((n)=>{
1413
+ const node6 = tree.visibleNodes.find((n)=>{
1441
1414
  // @ts-ignore
1442
1415
  const name = n.data.name;
1443
1416
  if (typeof name === "string") return name.toLowerCase().startsWith($065a164934293bf2$var$focusSearchTerm);
1444
1417
  else return false;
1445
1418
  });
1446
- if (node5) tree.focus(node5.id);
1419
+ if (node6) tree.focus(node6.id);
1447
1420
  },
1448
1421
  children: /*#__PURE__*/ (0, $g00cZ$jsx)((0, $g00cZ$FixedSizeList), {
1449
1422
  className: tree.props.className,
@@ -1810,6 +1783,30 @@ class $bfece7c4aed4e9c4$export$e2da3477247342d1 {
1810
1783
  get cursorOverFolder() {
1811
1784
  return this.state.dnd.cursor.type === "highlight";
1812
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
+ }
1813
1810
  hideCursor() {
1814
1811
  this.dispatch((0, $59f144a8dd651e5e$export$e324594224ef24da).cursor({
1815
1812
  type: "none"
@@ -1907,12 +1904,12 @@ class $bfece7c4aed4e9c4$export$e2da3477247342d1 {
1907
1904
  if (this.isFiltered) return this.state.nodes.open.filtered[id] ?? true; // Filtered folders are always opened by default
1908
1905
  else return this.state.nodes.open.unfiltered[id] ?? def;
1909
1906
  }
1910
- isDraggable(data) {
1911
- const check = this.props.disableDrag || (()=>false);
1907
+ isEditable(data) {
1908
+ const check = this.props.disableEdit || (()=>false);
1912
1909
  return !$0e6083160f4b36ed$exports.access(data, check) ?? true;
1913
1910
  }
1914
- isDroppable(data) {
1915
- const check = this.props.disableDrop || (()=>false);
1911
+ isDraggable(data) {
1912
+ const check = this.props.disableDrag || (()=>false);
1916
1913
  return !$0e6083160f4b36ed$exports.access(data, check) ?? true;
1917
1914
  }
1918
1915
  isDragging(node) {
@@ -2075,54 +2072,19 @@ function $e739455e59c6aed3$export$5a6c424b1725f44f() {
2075
2072
  // In case we drop an item at the bottom of the list
2076
2073
  const [, drop] = (0, $g00cZ$useDrop)(()=>({
2077
2074
  accept: "NODE",
2078
- hover: (item, m)=>{
2075
+ canDrop: (_item, m)=>{
2079
2076
  if (!m.isOver({
2080
2077
  shallow: true
2081
- })) return;
2082
- if (m.canDrop()) {
2083
- const offset = m.getClientOffset();
2084
- if (!tree.listEl.current || !offset) return;
2085
- const { cursor: cursor } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
2086
- element: tree.listEl.current,
2087
- offset: offset,
2088
- indent: tree.indent,
2089
- node: null,
2090
- prevNode: tree.visibleNodes[tree.visibleNodes.length - 1],
2091
- nextNode: null
2092
- });
2093
- if (cursor) tree.showCursor(cursor);
2094
- } else tree.hideCursor();
2078
+ })) return false;
2079
+ return tree.canDrop();
2095
2080
  },
2096
- canDrop: (item, m)=>{
2081
+ hover: (_item, m)=>{
2097
2082
  if (!m.isOver({
2098
2083
  shallow: true
2099
- })) return false;
2100
- if (tree.isFiltered) return false;
2101
- const offset = m.getClientOffset();
2102
- if (!tree.listEl.current || !offset) return false;
2103
- const { drop: drop } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
2104
- element: tree.listEl.current,
2105
- offset: offset,
2106
- indent: tree.indent,
2107
- node: null,
2108
- prevNode: tree.visibleNodes[tree.visibleNodes.length - 1],
2109
- nextNode: null
2110
- });
2111
- if (!drop) return false;
2112
- const dropParent = tree.get(drop.parentId) ?? tree.root;
2113
- for (let id of item.dragIds){
2114
- const drag = tree.get(id);
2115
- if (!drag) return false;
2116
- if (!dropParent) return false;
2117
- if (drag.isInternal && (0, $0e6083160f4b36ed$export$1e38f72c6c546f70)(dropParent, drag)) return false;
2118
- }
2119
- return true;
2120
- },
2121
- drop: (item, m)=>{
2122
- if (m.didDrop()) return;
2084
+ })) return;
2123
2085
  const offset = m.getClientOffset();
2124
2086
  if (!tree.listEl.current || !offset) return;
2125
- const { drop: drop } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
2087
+ const { cursor: cursor , drop: drop } = (0, $2db980bfed6822da$export$f502ca02ebb85a1c)({
2126
2088
  element: tree.listEl.current,
2127
2089
  offset: offset,
2128
2090
  indent: tree.indent,
@@ -2130,7 +2092,10 @@ function $e739455e59c6aed3$export$5a6c424b1725f44f() {
2130
2092
  prevNode: tree.visibleNodes[tree.visibleNodes.length - 1],
2131
2093
  nextNode: null
2132
2094
  });
2133
- 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();
2134
2099
  }
2135
2100
  }), [
2136
2101
  tree