react-arborist 0.1.1 → 0.1.5

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.
Files changed (38) hide show
  1. package/README.md +16 -2
  2. package/dist/lib/components/drop-cursor.js +11 -7
  3. package/dist/lib/components/preview.d.ts +0 -1
  4. package/dist/lib/components/preview.js +12 -8
  5. package/dist/lib/components/row.d.ts +2 -2
  6. package/dist/lib/components/row.js +27 -24
  7. package/dist/lib/components/tree.d.ts +2 -0
  8. package/dist/lib/components/tree.js +37 -0
  9. package/dist/lib/context.d.ts +3 -4
  10. package/dist/lib/context.js +47 -37
  11. package/dist/lib/data/enrich-tree.d.ts +2 -0
  12. package/dist/lib/data/enrich-tree.js +34 -0
  13. package/dist/lib/data/flatten-tree.d.ts +2 -0
  14. package/dist/lib/data/flatten-tree.js +20 -0
  15. package/dist/lib/data/make-tree.d.ts +5 -0
  16. package/dist/lib/data/make-tree.js +40 -0
  17. package/dist/lib/data/visible-nodes-hook.d.ts +2 -0
  18. package/dist/lib/data/visible-nodes-hook.js +13 -0
  19. package/dist/lib/dnd/compute-drop.js +11 -7
  20. package/dist/lib/dnd/drag-hook.js +17 -13
  21. package/dist/lib/dnd/drop-hook.js +17 -13
  22. package/dist/lib/dnd/measure-hover.js +7 -3
  23. package/dist/lib/dnd/outer-drop-hook.d.ts +1 -0
  24. package/dist/lib/dnd/outer-drop-hook.js +49 -0
  25. package/dist/lib/index.d.ts +1 -1
  26. package/dist/lib/index.js +5 -1
  27. package/dist/lib/reducer.js +26 -14
  28. package/dist/lib/selection/range.js +4 -1
  29. package/dist/lib/selection/selection-hook.js +10 -6
  30. package/dist/lib/selection/selection.js +6 -3
  31. package/dist/lib/selection/selection.test.js +4 -2
  32. package/dist/lib/types.d.ts +47 -43
  33. package/dist/lib/types.js +2 -1
  34. package/dist/lib/utils.d.ts +1 -5
  35. package/dist/lib/utils.js +13 -46
  36. package/package.json +6 -6
  37. package/dist/lib/components/tree-list.d.ts +0 -3
  38. package/dist/lib/components/tree-list.js +0 -75
@@ -0,0 +1,2 @@
1
+ import { Node } from "../types";
2
+ export declare function flattenTree<T>(root: Node<T>): Node<T>[];
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.flattenTree = void 0;
4
+ function flattenTree(root) {
5
+ var list = [];
6
+ var index = 0;
7
+ function collect(node) {
8
+ var _a;
9
+ if (node.level >= 0) {
10
+ node.rowIndex = index++;
11
+ list.push(node);
12
+ }
13
+ if (node.isOpen) {
14
+ (_a = node.children) === null || _a === void 0 ? void 0 : _a.forEach(collect);
15
+ }
16
+ }
17
+ collect(root);
18
+ return list;
19
+ }
20
+ exports.flattenTree = flattenTree;
@@ -0,0 +1,5 @@
1
+ export declare function makeTree(string: string): {
2
+ id: string;
3
+ name: string;
4
+ isOpen: boolean;
5
+ };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ // A function that turns a string of text into a tree
3
+ // Each line is a node
4
+ // The number of spaces at the beginning indicate the level
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.makeTree = void 0;
7
+ function makeTree(string) {
8
+ var root = { id: "ROOT", name: "ROOT", isOpen: true };
9
+ var prevNode = root;
10
+ var prevLevel = -1;
11
+ var id = 1;
12
+ string.split("\n").forEach(function (line) {
13
+ var name = line.trimStart();
14
+ var level = line.length - name.length;
15
+ var diff = level - prevLevel;
16
+ var node = { id: (id++).toString(), name: name, isOpen: true };
17
+ if (diff === 1) {
18
+ // First child
19
+ //@ts-ignore
20
+ node.parent = prevNode;
21
+ //@ts-ignore
22
+ prevNode.children = [node];
23
+ }
24
+ else {
25
+ // Find the parent and go up
26
+ //@ts-ignore
27
+ var parent_1 = prevNode.parent;
28
+ for (var i = diff; i < 0; i++) {
29
+ parent_1 = parent_1.parent;
30
+ }
31
+ //@ts-ignore
32
+ node.parent = parent_1;
33
+ parent_1.children.push(node);
34
+ }
35
+ prevNode = node;
36
+ prevLevel = level;
37
+ });
38
+ return root;
39
+ }
40
+ exports.makeTree = makeTree;
@@ -0,0 +1,2 @@
1
+ import { TreeProps, Node, IdObj } from "../types";
2
+ export declare function useVisibleNodes<T extends IdObj>(props: TreeProps<T>): Node<T>[];
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useVisibleNodes = void 0;
4
+ var react_1 = require("react");
5
+ var enrich_tree_1 = require("./enrich-tree");
6
+ var flatten_tree_1 = require("./flatten-tree");
7
+ function useVisibleNodes(props) {
8
+ var root = (0, react_1.useMemo)(function () {
9
+ return (0, enrich_tree_1.enrichTree)(props.data, props.hideRoot, props.getChildren, props.getIsOpen);
10
+ }, [props.data, props.hideRoot, props.getChildren, props.getIsOpen]);
11
+ return (0, react_1.useMemo)(function () { return (0, flatten_tree_1.flattenTree)(root); }, [root]);
12
+ }
13
+ exports.useVisibleNodes = useVisibleNodes;
@@ -1,4 +1,7 @@
1
- import { bound, indexOf, isFolder } from "../utils";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.computeDrop = void 0;
4
+ var utils_1 = require("../utils");
2
5
  function measureHover(el, offset) {
3
6
  var rect = el.getBoundingClientRect();
4
7
  var x = offset.x - Math.round(rect.x);
@@ -16,7 +19,7 @@ function getNodesAroundCursor(node, prev, next, hover) {
16
19
  // Put the cursor below the last item which is "prev"
17
20
  return [prev, null];
18
21
  }
19
- if (isFolder(node)) {
22
+ if ((0, utils_1.isFolder)(node)) {
20
23
  if (!hover.inMiddle && hover.inTopHalf) {
21
24
  return [prev, node];
22
25
  }
@@ -40,17 +43,17 @@ function getDropLocation(nodeAboveCursor, level, hover) {
40
43
  if (!drop) {
41
44
  return [null, 0];
42
45
  }
43
- if (isFolder(drop) && drop.isOpen) {
46
+ if ((0, utils_1.isFolder)(drop) && drop.isOpen) {
44
47
  // keep args the same
45
48
  }
46
- else if (isFolder(drop) && hover.inMiddle) {
49
+ else if ((0, utils_1.isFolder)(drop) && hover.inMiddle) {
47
50
  // keep args the same
48
51
  }
49
52
  else {
50
53
  while (drop.parent && drop.level > level) {
51
54
  drop = drop.parent;
52
55
  }
53
- dropIndex = indexOf(drop) + 1;
56
+ dropIndex = (0, utils_1.indexOf)(drop) + 1;
54
57
  dropParent = drop.parent;
55
58
  }
56
59
  return [dropParent, dropIndex];
@@ -70,9 +73,9 @@ function getDropLevel(hovering, aboveCursor, belowCursor, indent) {
70
73
  max = aboveCursor.level;
71
74
  min = belowCursor.level;
72
75
  }
73
- return bound(hoverLevel, min, max);
76
+ return (0, utils_1.bound)(hoverLevel, min, max);
74
77
  }
75
- export function computeDrop(_a) {
78
+ function computeDrop(_a) {
76
79
  var _b, _c;
77
80
  var element = _a.element, offset = _a.offset, indent = _a.indent, node = _a.node, prevNode = _a.prevNode, nextNode = _a.nextNode;
78
81
  var hovering = measureHover(element, offset);
@@ -103,3 +106,4 @@ export function computeDrop(_a) {
103
106
  },
104
107
  };
105
108
  }
109
+ exports.computeDrop = computeDrop;
@@ -1,13 +1,16 @@
1
- import { useEffect } from "react";
2
- import { useDrag } from "react-dnd";
3
- import { getEmptyImage } from "react-dnd-html5-backend";
4
- import { useIsSelected, useSelectedIds, useStaticContext } from "../context";
5
- import { setCursorLocation } from "../reducer";
6
- export function useDragHook(node) {
7
- var treeView = useStaticContext();
8
- var isSelected = useIsSelected();
9
- var ids = useSelectedIds();
10
- var _a = useDrag(function () { return ({
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useDragHook = void 0;
4
+ var react_1 = require("react");
5
+ var react_dnd_1 = require("react-dnd");
6
+ var react_dnd_html5_backend_1 = require("react-dnd-html5-backend");
7
+ var context_1 = require("../context");
8
+ var reducer_1 = require("../reducer");
9
+ function useDragHook(node) {
10
+ var treeView = (0, context_1.useStaticContext)();
11
+ var isSelected = (0, context_1.useIsSelected)();
12
+ var ids = (0, context_1.useSelectedIds)();
13
+ var _a = (0, react_dnd_1.useDrag)(function () { return ({
11
14
  type: "NODE",
12
15
  item: function () { return ({
13
16
  id: node.id,
@@ -17,7 +20,7 @@ export function useDragHook(node) {
17
20
  isDragging: m.isDragging(),
18
21
  }); },
19
22
  end: function (item, monitor) {
20
- treeView.dispatch(setCursorLocation(null));
23
+ treeView.dispatch((0, reducer_1.setCursorLocation)(null));
21
24
  var drop = monitor.getDropResult();
22
25
  if (drop && drop.parentId) {
23
26
  treeView.onMove(item.dragIds, drop.parentId, drop.index);
@@ -25,8 +28,9 @@ export function useDragHook(node) {
25
28
  }
26
29
  },
27
30
  }); }, [ids, node]), isDragging = _a[0].isDragging, ref = _a[1], preview = _a[2];
28
- useEffect(function () {
29
- preview(getEmptyImage());
31
+ (0, react_1.useEffect)(function () {
32
+ preview((0, react_dnd_html5_backend_1.getEmptyImage)());
30
33
  }, [preview]);
31
34
  return [{ isDragging: isDragging }, ref];
32
35
  }
36
+ exports.useDragHook = useDragHook;
@@ -1,11 +1,14 @@
1
- import { useDrop } from "react-dnd";
2
- import { useStaticContext } from "../context";
3
- import { setCursorLocation } from "../reducer";
4
- import { isDecendent, isFolder } from "../utils";
5
- import { computeDrop } from "./compute-drop";
6
- export function useDropHook(el, node, prev, next) {
7
- var treeView = useStaticContext();
8
- var _a = useDrop(function () { return ({
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useDropHook = void 0;
4
+ var react_dnd_1 = require("react-dnd");
5
+ var context_1 = require("../context");
6
+ var reducer_1 = require("../reducer");
7
+ var utils_1 = require("../utils");
8
+ var compute_drop_1 = require("./compute-drop");
9
+ function useDropHook(el, node, prev, next) {
10
+ var treeView = (0, context_1.useStaticContext)();
11
+ var _a = (0, react_dnd_1.useDrop)(function () { return ({
9
12
  accept: "NODE",
10
13
  canDrop: function (item) {
11
14
  for (var _i = 0, _a = item.dragIds; _i < _a.length; _i++) {
@@ -13,7 +16,7 @@ export function useDropHook(el, node, prev, next) {
13
16
  var drag = treeView.getNode(id);
14
17
  if (!drag)
15
18
  return false;
16
- if (isFolder(drag) && isDecendent(node, drag))
19
+ if ((0, utils_1.isFolder)(drag) && (0, utils_1.isDecendent)(node, drag))
17
20
  return false;
18
21
  }
19
22
  return true;
@@ -23,7 +26,7 @@ export function useDropHook(el, node, prev, next) {
23
26
  var offset = m.getClientOffset();
24
27
  if (!el.current || !offset)
25
28
  return;
26
- var cursor = computeDrop({
29
+ var cursor = (0, compute_drop_1.computeDrop)({
27
30
  element: el.current,
28
31
  offset: offset,
29
32
  indent: treeView.indent,
@@ -31,17 +34,17 @@ export function useDropHook(el, node, prev, next) {
31
34
  prevNode: prev,
32
35
  nextNode: next,
33
36
  }).cursor;
34
- treeView.dispatch(setCursorLocation(cursor));
37
+ treeView.dispatch((0, reducer_1.setCursorLocation)(cursor));
35
38
  }
36
39
  else {
37
- treeView.dispatch(setCursorLocation(null));
40
+ treeView.dispatch((0, reducer_1.setCursorLocation)(null));
38
41
  }
39
42
  },
40
43
  drop: function (item, m) {
41
44
  var offset = m.getClientOffset();
42
45
  if (!el.current || !offset)
43
46
  return;
44
- var _a = computeDrop({
47
+ var _a = (0, compute_drop_1.computeDrop)({
45
48
  element: el.current,
46
49
  offset: offset,
47
50
  indent: treeView.indent,
@@ -54,3 +57,4 @@ export function useDropHook(el, node, prev, next) {
54
57
  }); }, [node, prev, el, treeView]), ref = _a[1];
55
58
  return ref;
56
59
  }
60
+ exports.useDropHook = useDropHook;
@@ -1,5 +1,8 @@
1
- import { bound } from "../utils";
2
- export function measureHover(el, offset, indent) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.measureHover = void 0;
4
+ var utils_1 = require("../utils");
5
+ function measureHover(el, offset, indent) {
3
6
  var nextEl = el.nextElementSibling;
4
7
  var prevEl = el.previousElementSibling;
5
8
  var rect = el.getBoundingClientRect();
@@ -12,6 +15,7 @@ export function measureHover(el, offset, indent) {
12
15
  var inMiddle = y > pad && y < height - pad;
13
16
  var maxLevel = Number(inBottomHalf ? el.dataset.level : prevEl ? prevEl.dataset.level : 0);
14
17
  var minLevel = Number(inTopHalf ? el.dataset.level : nextEl ? nextEl.dataset.level : 0);
15
- var level = bound(Math.floor(x / indent), minLevel, maxLevel);
18
+ var level = (0, utils_1.bound)(Math.floor(x / indent), minLevel, maxLevel);
16
19
  return { level: level, inTopHalf: inTopHalf, inBottomHalf: inBottomHalf, inMiddle: inMiddle };
17
20
  }
21
+ exports.measureHover = measureHover;
@@ -0,0 +1 @@
1
+ export declare function useOuterDrop(): void;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useOuterDrop = void 0;
4
+ var react_dnd_1 = require("react-dnd");
5
+ var context_1 = require("../context");
6
+ var reducer_1 = require("../reducer");
7
+ var compute_drop_1 = require("./compute-drop");
8
+ function useOuterDrop() {
9
+ var tree = (0, context_1.useStaticContext)();
10
+ // In case we drop an item at the bottom of the list
11
+ var _a = (0, react_dnd_1.useDrop)(function () { return ({
12
+ accept: "NODE",
13
+ hover: function (item, m) {
14
+ if (!m.isOver({ shallow: true }))
15
+ return;
16
+ var offset = m.getClientOffset();
17
+ if (!tree.listRef.current || !offset)
18
+ return;
19
+ var cursor = (0, compute_drop_1.computeDrop)({
20
+ element: tree.listRef.current,
21
+ offset: offset,
22
+ indent: tree.indent,
23
+ node: null,
24
+ prevNode: tree.visibleNodes[tree.visibleNodes.length - 1],
25
+ nextNode: null,
26
+ }).cursor;
27
+ tree.dispatch((0, reducer_1.setCursorLocation)(cursor));
28
+ },
29
+ drop: function (item, m) {
30
+ if (m.didDrop())
31
+ return;
32
+ console.log("drop!!");
33
+ var offset = m.getClientOffset();
34
+ if (!tree.listRef.current || !offset)
35
+ return;
36
+ var _a = (0, compute_drop_1.computeDrop)({
37
+ element: tree.listRef.current,
38
+ offset: offset,
39
+ indent: tree.indent,
40
+ node: null,
41
+ prevNode: tree.visibleNodes[tree.visibleNodes.length - 1],
42
+ nextNode: null,
43
+ }), parentId = _a.parentId, index = _a.index;
44
+ return { parentId: parentId, index: index };
45
+ },
46
+ }); }, [tree]), drop = _a[1];
47
+ drop(tree.listRef);
48
+ }
49
+ exports.useOuterDrop = useOuterDrop;
@@ -1,2 +1,2 @@
1
- export { TreeList } from "./components/tree-list";
1
+ export { Tree } from "./components/tree";
2
2
  export type { NodeRenderer, NodeState, NodeHandlers } from "./types";
package/dist/lib/index.js CHANGED
@@ -1 +1,5 @@
1
- export { TreeList } from "./components/tree-list";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Tree = void 0;
4
+ var tree_1 = require("./components/tree");
5
+ Object.defineProperty(exports, "Tree", { enumerable: true, get: function () { return tree_1.Tree; } });
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __assign = (this && this.__assign) || function () {
2
3
  __assign = Object.assign || function(t) {
3
4
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -9,8 +10,10 @@ var __assign = (this && this.__assign) || function () {
9
10
  };
10
11
  return __assign.apply(this, arguments);
11
12
  };
12
- import { Selection } from "./selection/selection";
13
- export var initState = function () { return ({
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.reducer = exports.stepDown = exports.stepUp = exports.edit = exports.selectId = exports.select = exports.setVisibleIds = exports.setCursorLocation = exports.initState = void 0;
15
+ var selection_1 = require("./selection/selection");
16
+ var initState = function () { return ({
14
17
  visibleIds: [],
15
18
  cursorLocation: null,
16
19
  editingId: null,
@@ -19,40 +22,48 @@ export var initState = function () { return ({
19
22
  ids: [],
20
23
  },
21
24
  }); };
22
- export var setCursorLocation = function (location) { return ({
25
+ exports.initState = initState;
26
+ var setCursorLocation = function (location) { return ({
23
27
  type: "SET_CURSOR_LOCATION",
24
28
  location: location,
25
29
  }); };
26
- export var setVisibleIds = function (ids, // index to id
30
+ exports.setCursorLocation = setCursorLocation;
31
+ var setVisibleIds = function (ids, // index to id
27
32
  idMap // id to index
28
33
  ) { return ({
29
34
  type: "SET_VISIBLE_IDS",
30
35
  ids: ids,
31
36
  idMap: idMap,
32
37
  }); };
33
- export var select = function (index, meta, shift) { return ({
38
+ exports.setVisibleIds = setVisibleIds;
39
+ var select = function (index, meta, shift) { return ({
34
40
  type: "SELECT",
35
41
  index: index,
36
42
  meta: meta,
37
43
  shift: shift,
38
44
  }); };
39
- export var selectId = function (id) { return ({
45
+ exports.select = select;
46
+ var selectId = function (id) { return ({
40
47
  type: "SELECT_ID",
41
48
  id: id,
42
49
  }); };
43
- export var edit = function (id) { return ({
50
+ exports.selectId = selectId;
51
+ var edit = function (id) { return ({
44
52
  type: "EDIT",
45
53
  id: id,
46
54
  }); };
47
- export var stepUp = function (shift, ids) { return ({
55
+ exports.edit = edit;
56
+ var stepUp = function (shift, ids) { return ({
48
57
  type: "STEP_UP",
49
58
  shift: shift,
50
59
  }); };
51
- export var stepDown = function (shift, ids) { return ({
60
+ exports.stepUp = stepUp;
61
+ var stepDown = function (shift, ids) { return ({
52
62
  type: "STEP_DOWN",
53
63
  shift: shift,
54
64
  }); };
55
- export function reducer(state, action) {
65
+ exports.stepDown = stepDown;
66
+ function reducer(state, action) {
56
67
  switch (action.type) {
57
68
  case "EDIT":
58
69
  return __assign(__assign({}, state), { editingId: action.id });
@@ -64,7 +75,7 @@ export function reducer(state, action) {
64
75
  return __assign(__assign({}, state), { cursorLocation: action.location });
65
76
  }
66
77
  case "SELECT":
67
- var s = Selection.parse(state.selection.data, state.visibleIds);
78
+ var s = selection_1.Selection.parse(state.selection.data, state.visibleIds);
68
79
  if (action.meta) {
69
80
  if (s.contains(action.index)) {
70
81
  s.deselect(action.index);
@@ -86,7 +97,7 @@ export function reducer(state, action) {
86
97
  case "SELECT_ID":
87
98
  return __assign(__assign({}, state), { selection: __assign(__assign({}, state.selection), { ids: [action.id] }) });
88
99
  case "STEP_UP":
89
- var s3 = Selection.parse(state.selection.data, state.visibleIds);
100
+ var s3 = selection_1.Selection.parse(state.selection.data, state.visibleIds);
90
101
  var f = s3.getFocus();
91
102
  if (action.shift) {
92
103
  s3.extend(f - 1);
@@ -99,7 +110,7 @@ export function reducer(state, action) {
99
110
  ids: s3.getSelectedItems(),
100
111
  } });
101
112
  case "STEP_DOWN":
102
- var s6 = Selection.parse(state.selection.data, state.visibleIds);
113
+ var s6 = selection_1.Selection.parse(state.selection.data, state.visibleIds);
103
114
  var f2 = s6.getFocus();
104
115
  if (action.shift) {
105
116
  s6.extend(f2 + 1);
@@ -113,7 +124,7 @@ export function reducer(state, action) {
113
124
  } });
114
125
  case "SET_VISIBLE_IDS":
115
126
  var ids = state.selection.ids;
116
- var s2 = new Selection([], null, "none", state.visibleIds);
127
+ var s2 = new selection_1.Selection([], null, "none", state.visibleIds);
117
128
  for (var _i = 0, ids_1 = ids; _i < ids_1.length; _i++) {
118
129
  var id = ids_1[_i];
119
130
  if (id in action.idMap)
@@ -127,6 +138,7 @@ export function reducer(state, action) {
127
138
  return state;
128
139
  }
129
140
  }
141
+ exports.reducer = reducer;
130
142
  function equal(a, b) {
131
143
  if (a === b)
132
144
  return true;