react-arborist 3.1.0 → 3.3.0-rc.1

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.
@@ -51,6 +51,7 @@ export declare class NodeApi<T = any> {
51
51
  get next(): NodeApi<T> | null;
52
52
  get prev(): NodeApi<T> | null;
53
53
  get nextSibling(): NodeApi<T> | null;
54
+ isAncestorOf(node: NodeApi<T> | null): boolean;
54
55
  select(): void;
55
56
  deselect(): void;
56
57
  selectMulti(): void;
@@ -87,9 +87,17 @@ export declare class TreeApi<T> {
87
87
  selectContiguous(identity: Identity): void;
88
88
  deselectAll(): void;
89
89
  selectAll(): void;
90
+ setSelection(args: {
91
+ ids: (IdObj | string)[] | null;
92
+ anchor: IdObj | string | null;
93
+ mostRecent: IdObj | string | null;
94
+ }): void;
90
95
  get cursorParentId(): string | null;
91
96
  get cursorOverFolder(): boolean;
92
97
  get dragNodes(): NodeApi<T>[];
98
+ get dragNode(): NodeApi<T> | null;
99
+ get dragDestinationParent(): NodeApi<T> | null;
100
+ get dragDestinationIndex(): number | null;
93
101
  canDrop(): boolean;
94
102
  hideCursor(): void;
95
103
  showCursor(cursor: Cursor): void;
package/dist/module.js CHANGED
@@ -47,7 +47,7 @@ var $0e6083160f4b36ed$exports = {};
47
47
  $parcel$export($0e6083160f4b36ed$exports, "bound", () => $0e6083160f4b36ed$export$adf7c0fe6059d774);
48
48
  $parcel$export($0e6083160f4b36ed$exports, "isItem", () => $0e6083160f4b36ed$export$5318634f2ee07019);
49
49
  $parcel$export($0e6083160f4b36ed$exports, "isClosed", () => $0e6083160f4b36ed$export$4210f5ea57fbae57);
50
- $parcel$export($0e6083160f4b36ed$exports, "isDecendent", () => $0e6083160f4b36ed$export$1e38f72c6c546f70);
50
+ $parcel$export($0e6083160f4b36ed$exports, "isDescendant", () => $0e6083160f4b36ed$export$8503f9d2928f9a64);
51
51
  $parcel$export($0e6083160f4b36ed$exports, "indexOf", () => $0e6083160f4b36ed$export$305f7d4e9d4624f2);
52
52
  $parcel$export($0e6083160f4b36ed$exports, "noop", () => $0e6083160f4b36ed$export$8793edee2d425525);
53
53
  $parcel$export($0e6083160f4b36ed$exports, "dfs", () => $0e6083160f4b36ed$export$51b654aff22fc5a6);
@@ -71,7 +71,7 @@ function $0e6083160f4b36ed$export$5318634f2ee07019(node) {
71
71
  function $0e6083160f4b36ed$export$4210f5ea57fbae57(node) {
72
72
  return node && node.isInternal && !node.isOpen;
73
73
  }
74
- const $0e6083160f4b36ed$export$1e38f72c6c546f70 = (a, b)=>{
74
+ const $0e6083160f4b36ed$export$8503f9d2928f9a64 = (a, b)=>{
75
75
  let n = a;
76
76
  while(n){
77
77
  if (n.id === b.id) return true;
@@ -428,6 +428,15 @@ class $096e74084443e9a3$export$d4b903da0f522dc8 {
428
428
  const i = this.childIndex;
429
429
  return this.parent?.children[i + 1] ?? null;
430
430
  }
431
+ isAncestorOf(node) {
432
+ if (!node) return false;
433
+ let ancestor = node;
434
+ while(ancestor){
435
+ if (ancestor.id === this.id) return true;
436
+ ancestor = ancestor.parent;
437
+ }
438
+ return false;
439
+ }
431
440
  select() {
432
441
  this.tree.select(this);
433
442
  }
@@ -602,7 +611,9 @@ const $6ad32e02250c922e$export$d4c72bab9d6cc13a = (props)=>({
602
611
  },
603
612
  drag: {
604
613
  id: null,
605
- idWillReceiveDrop: null
614
+ selectedIds: [],
615
+ destinationParentId: null,
616
+ destinationIndex: null
606
617
  },
607
618
  selection: {
608
619
  ids: new Set(),
@@ -642,9 +653,9 @@ const $37bc167debff36d2$export$e324594224ef24da = {
642
653
  id
643
654
  ]).map((0, $0e6083160f4b36ed$export$65e5b62a4c490288))
644
655
  }),
645
- set: (ids)=>({
656
+ set: (args)=>({
646
657
  type: "SELECTION_SET",
647
- ids: ids
658
+ ...args
648
659
  }),
649
660
  mostRecent: (id)=>({
650
661
  type: "SELECTION_MOST_RECENT",
@@ -687,7 +698,9 @@ function $37bc167debff36d2$export$1650419e431d3ba3(state = (0, $6ad32e02250c922e
687
698
  case "SELECTION_SET":
688
699
  return {
689
700
  ...state,
690
- ids: new Set(action.ids)
701
+ ids: action.ids,
702
+ mostRecent: action.mostRecent,
703
+ anchor: action.anchor
691
704
  };
692
705
  case "SELECTION_MOST_RECENT":
693
706
  return {
@@ -959,7 +972,7 @@ function $907e707a330ef23a$export$715c0d031ede7907(node) {
959
972
  (0, $0e6083160f4b36ed$export$c6d63370cef03886)(tree.props.onMove, {
960
973
  dragIds: dragIds,
961
974
  parentId: parentId === (0, $81080a351c006222$export$ec71a3379b43ae5c) ? null : parentId,
962
- index: index,
975
+ index: index === null ? 0 : index,
963
976
  dragNodes: tree.dragNodes,
964
977
  parentNode: tree.get(parentId)
965
978
  });
@@ -1004,7 +1017,7 @@ function $2db980bfed6822da$var$measureHover(el, offset) {
1004
1017
  };
1005
1018
  }
1006
1019
  function $2db980bfed6822da$var$getNodesAroundCursor(node, prev, next, hover) {
1007
- if (!node) // We're hoving over the empty part of the list, not over an item,
1020
+ if (!node) // We're hovering over the empty part of the list, not over an item,
1008
1021
  // Put the cursor below the last item which is "prev"
1009
1022
  return [
1010
1023
  prev,
@@ -1088,7 +1101,7 @@ function $2db980bfed6822da$export$f502ca02ebb85a1c(args) {
1088
1101
  const { node: node , nextNode: nextNode , prevNode: prevNode } = args;
1089
1102
  const [above, below] = $2db980bfed6822da$var$getNodesAroundCursor(node, prevNode, nextNode, hover);
1090
1103
  /* Hovering over the middle of a folder */ if (node && node.isInternal && hover.inMiddle) return {
1091
- drop: $2db980bfed6822da$var$dropAt(node.id, 0),
1104
+ drop: $2db980bfed6822da$var$dropAt(node.id, null),
1092
1105
  cursor: $2db980bfed6822da$var$highlightCursor(node.id)
1093
1106
  };
1094
1107
  /* At the top of the list */ if (!above) return {
@@ -1765,19 +1778,34 @@ class $bfece7c4aed4e9c4$export$e2da3477247342d1 {
1765
1778
  $bfece7c4aed4e9c4$var$safeRun(this.props.onSelect, this.selectedNodes);
1766
1779
  }
1767
1780
  deselectAll() {
1768
- this.dispatch((0, $37bc167debff36d2$export$e324594224ef24da).clear());
1769
- this.dispatch((0, $37bc167debff36d2$export$e324594224ef24da).anchor(null));
1770
- this.dispatch((0, $37bc167debff36d2$export$e324594224ef24da).mostRecent(null));
1781
+ this.setSelection({
1782
+ ids: [],
1783
+ anchor: null,
1784
+ mostRecent: null
1785
+ });
1771
1786
  $bfece7c4aed4e9c4$var$safeRun(this.props.onSelect, this.selectedNodes);
1772
1787
  }
1773
1788
  selectAll() {
1774
- this.dispatch((0, $37bc167debff36d2$export$e324594224ef24da).set(new Set(Object.keys(this.idToIndex))));
1789
+ this.setSelection({
1790
+ ids: Object.keys(this.idToIndex),
1791
+ anchor: this.firstNode,
1792
+ mostRecent: this.lastNode
1793
+ });
1775
1794
  this.dispatch((0, $c27b8e9863235052$export$d7ddd398f22d79ef)(this.lastNode?.id));
1776
- this.dispatch((0, $37bc167debff36d2$export$e324594224ef24da).anchor(this.firstNode));
1777
- this.dispatch((0, $37bc167debff36d2$export$e324594224ef24da).mostRecent(this.lastNode));
1778
1795
  if (this.focusedNode) $bfece7c4aed4e9c4$var$safeRun(this.props.onFocus, this.focusedNode);
1779
1796
  $bfece7c4aed4e9c4$var$safeRun(this.props.onSelect, this.selectedNodes);
1780
1797
  }
1798
+ setSelection(args) {
1799
+ const ids = new Set(args.ids?.map($bfece7c4aed4e9c4$var$identify));
1800
+ const anchor = $bfece7c4aed4e9c4$var$identifyNull(args.anchor);
1801
+ const mostRecent = $bfece7c4aed4e9c4$var$identifyNull(args.mostRecent);
1802
+ this.dispatch((0, $37bc167debff36d2$export$e324594224ef24da).set({
1803
+ ids: ids,
1804
+ anchor: anchor,
1805
+ mostRecent: mostRecent
1806
+ }));
1807
+ $bfece7c4aed4e9c4$var$safeRun(this.props.onSelect, this.selectedNodes);
1808
+ }
1781
1809
  /* Drag and Drop */ get cursorParentId() {
1782
1810
  const { cursor: cursor } = this.state.dnd;
1783
1811
  switch(cursor.type){
@@ -1793,6 +1821,15 @@ class $bfece7c4aed4e9c4$export$e2da3477247342d1 {
1793
1821
  get dragNodes() {
1794
1822
  return this.state.dnd.dragIds.map((id)=>this.get(id)).filter((n)=>!!n);
1795
1823
  }
1824
+ get dragNode() {
1825
+ return this.get(this.state.nodes.drag.id);
1826
+ }
1827
+ get dragDestinationParent() {
1828
+ return this.get(this.state.nodes.drag.destinationParentId);
1829
+ }
1830
+ get dragDestinationIndex() {
1831
+ return this.state.nodes.drag.destinationIndex;
1832
+ }
1796
1833
  canDrop() {
1797
1834
  if (this.isFiltered) return false;
1798
1835
  const parentNode = this.get(this.state.dnd.parentId) ?? this.root;
@@ -1801,13 +1838,13 @@ class $bfece7c4aed4e9c4$export$e2da3477247342d1 {
1801
1838
  for (const drag of dragNodes){
1802
1839
  if (!drag) return false;
1803
1840
  if (!parentNode) return false;
1804
- if (drag.isInternal && $0e6083160f4b36ed$exports.isDecendent(parentNode, drag)) return false;
1841
+ if (drag.isInternal && $0e6083160f4b36ed$exports.isDescendant(parentNode, drag)) return false;
1805
1842
  }
1806
1843
  // Allow the user to insert their own logic
1807
1844
  if (typeof isDisabled == "function") return !isDisabled({
1808
1845
  parentNode: parentNode,
1809
1846
  dragNodes: this.dragNodes,
1810
- index: this.state.dnd.index
1847
+ index: this.state.dnd.index || 0
1811
1848
  });
1812
1849
  else if (typeof isDisabled == "string") // @ts-ignore
1813
1850
  return !parentNode.data[isDisabled];
@@ -1933,7 +1970,8 @@ class $bfece7c4aed4e9c4$export$e2da3477247342d1 {
1933
1970
  willReceiveDrop(node) {
1934
1971
  const id = $bfece7c4aed4e9c4$var$identifyNull(node);
1935
1972
  if (!id) return false;
1936
- return id === this.state.nodes.drag.idWillReceiveDrop;
1973
+ const { destinationParentId: destinationParentId , destinationIndex: destinationIndex } = this.state.nodes.drag;
1974
+ return id === destinationParentId && destinationIndex === null;
1937
1975
  }
1938
1976
  /* Tree Event Handlers */ onFocus() {
1939
1977
  const node = this.focusedNode || this.firstNode;
@@ -1971,30 +2009,28 @@ class $bfece7c4aed4e9c4$export$e2da3477247342d1 {
1971
2009
 
1972
2010
 
1973
2011
 
1974
- function $5c35ee13c124a8cc$export$1650419e431d3ba3(state = {
1975
- id: null,
1976
- idWillReceiveDrop: null
1977
- }, action) {
2012
+
2013
+ function $5c35ee13c124a8cc$export$1650419e431d3ba3(state = (0, $6ad32e02250c922e$export$d4c72bab9d6cc13a)().nodes.drag, action) {
1978
2014
  switch(action.type){
1979
2015
  case "DND_DRAG_START":
1980
2016
  return {
1981
2017
  ...state,
1982
- id: action.id
2018
+ id: action.id,
2019
+ selectedIds: action.dragIds
1983
2020
  };
1984
2021
  case "DND_DRAG_END":
1985
2022
  return {
1986
2023
  ...state,
1987
- id: null
1988
- };
1989
- case "DND_CURSOR":
1990
- const c = action.cursor;
1991
- if (c.type === "highlight" && c.id !== state.idWillReceiveDrop) return {
1992
- ...state,
1993
- idWillReceiveDrop: c.id
2024
+ id: null,
2025
+ destinationParentId: null,
2026
+ destinationIndex: null,
2027
+ selectedIds: []
1994
2028
  };
1995
- else if (c.type !== "highlight" && state.idWillReceiveDrop !== null) return {
2029
+ case "DND_HOVERING":
2030
+ if (action.parentId !== state.destinationParentId || action.index != state.destinationIndex) return {
1996
2031
  ...state,
1997
- idWillReceiveDrop: null
2032
+ destinationParentId: action.parentId,
2033
+ destinationIndex: action.index
1998
2034
  };
1999
2035
  else return state;
2000
2036
  default: