react-arborist 0.1.12 → 0.2.0-beta.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.
Files changed (86) hide show
  1. package/dist/{lib/components → components}/drop-cursor.d.ts +1 -0
  2. package/dist/{lib/components → components}/preview.d.ts +1 -0
  3. package/dist/{lib/components → components}/row.d.ts +0 -0
  4. package/dist/{lib/components → components}/tree.d.ts +0 -0
  5. package/dist/{lib/context.d.ts → context.d.ts} +4 -3
  6. package/dist/{lib/data → data}/enrich-tree.d.ts +1 -1
  7. package/dist/{lib/data → data}/flatten-tree.d.ts +0 -0
  8. package/dist/dnd/compute-drop.d.ts +37 -0
  9. package/dist/{lib/dnd → dnd}/drag-hook.d.ts +0 -0
  10. package/dist/{lib/dnd → dnd}/drop-hook.d.ts +0 -0
  11. package/dist/{lib/dnd → dnd}/outer-drop-hook.d.ts +0 -0
  12. package/dist/index.d.ts +4 -0
  13. package/dist/index.js +1354 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/module.js +1346 -0
  16. package/dist/module.js.map +1 -0
  17. package/dist/{lib/provider.d.ts → provider.d.ts} +1 -0
  18. package/dist/{lib/reducer.d.ts → reducer.d.ts} +4 -3
  19. package/dist/{lib/selection → selection}/range.d.ts +0 -0
  20. package/dist/{lib/selection → selection}/selection-hook.d.ts +0 -0
  21. package/dist/{lib/selection → selection}/selection.d.ts +0 -0
  22. package/dist/{lib/tree-api-hook.d.ts → tree-api-hook.d.ts} +0 -0
  23. package/dist/{lib/tree-api.d.ts → tree-api.d.ts} +4 -3
  24. package/dist/{lib/types.d.ts → types.d.ts} +8 -3
  25. package/dist/types.d.ts.map +1 -0
  26. package/dist/{lib/utils.d.ts → utils.d.ts} +2 -0
  27. package/package.json +16 -43
  28. package/src/components/drop-cursor.tsx +47 -0
  29. package/src/components/preview.tsx +108 -0
  30. package/src/components/row.tsx +119 -0
  31. package/src/components/tree.tsx +118 -0
  32. package/src/context.tsx +52 -0
  33. package/src/data/enrich-tree.ts +74 -0
  34. package/src/data/flatten-tree.ts +17 -0
  35. package/src/data/make-tree.ts +37 -0
  36. package/src/dnd/compute-drop.ts +184 -0
  37. package/src/dnd/drag-hook.ts +48 -0
  38. package/src/dnd/drop-hook.ts +66 -0
  39. package/src/dnd/measure-hover.ts +26 -0
  40. package/src/dnd/outer-drop-hook.ts +50 -0
  41. package/src/index.ts +5 -0
  42. package/src/provider.tsx +61 -0
  43. package/src/reducer.ts +161 -0
  44. package/src/selection/range.ts +41 -0
  45. package/src/selection/selection-hook.ts +24 -0
  46. package/src/selection/selection.test.ts +111 -0
  47. package/src/selection/selection.ts +186 -0
  48. package/src/tree-api-hook.ts +34 -0
  49. package/src/tree-api.ts +129 -0
  50. package/src/types.ts +147 -0
  51. package/src/utils.ts +35 -0
  52. package/tsconfig.json +28 -0
  53. package/README.md +0 -197
  54. package/dist/lib/components/drop-cursor.js +0 -53
  55. package/dist/lib/components/preview.js +0 -91
  56. package/dist/lib/components/row.js +0 -122
  57. package/dist/lib/components/tree.js +0 -76
  58. package/dist/lib/context.js +0 -57
  59. package/dist/lib/data/enrich-tree.js +0 -48
  60. package/dist/lib/data/flatten-tree.js +0 -20
  61. package/dist/lib/data/make-tree.d.ts +0 -5
  62. package/dist/lib/data/make-tree.js +0 -40
  63. package/dist/lib/data/visible-nodes-hook.d.ts +0 -2
  64. package/dist/lib/data/visible-nodes-hook.js +0 -19
  65. package/dist/lib/dnd/compute-drop.d.ts +0 -24
  66. package/dist/lib/dnd/compute-drop.js +0 -113
  67. package/dist/lib/dnd/drag-hook.js +0 -36
  68. package/dist/lib/dnd/drop-hook.js +0 -60
  69. package/dist/lib/dnd/measure-hover.d.ts +0 -8
  70. package/dist/lib/dnd/measure-hover.js +0 -21
  71. package/dist/lib/dnd/outer-drop-hook.js +0 -50
  72. package/dist/lib/index.d.ts +0 -3
  73. package/dist/lib/index.js +0 -7
  74. package/dist/lib/provider.js +0 -44
  75. package/dist/lib/reducer.js +0 -149
  76. package/dist/lib/selection/range.js +0 -45
  77. package/dist/lib/selection/selection-hook.js +0 -24
  78. package/dist/lib/selection/selection.js +0 -192
  79. package/dist/lib/selection/selection.test.d.ts +0 -1
  80. package/dist/lib/selection/selection.test.js +0 -102
  81. package/dist/lib/tree-api-hook.js +0 -26
  82. package/dist/lib/tree-api.js +0 -130
  83. package/dist/lib/tree-monitor.d.ts +0 -15
  84. package/dist/lib/tree-monitor.js +0 -32
  85. package/dist/lib/types.js +0 -2
  86. package/dist/lib/utils.js +0 -31
@@ -1,40 +0,0 @@
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;
@@ -1,2 +0,0 @@
1
- import { TreeProps, Node, IdObj } from "../types";
2
- export declare function useVisibleNodes<T extends IdObj>(props: TreeProps<T>): Node<T>[];
@@ -1,19 +0,0 @@
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.childrenAccessor, props.isOpenAccessor, props.openByDefault);
10
- }, [
11
- props.data,
12
- props.hideRoot,
13
- props.childrenAccessor,
14
- props.isOpenAccessor,
15
- props.openByDefault,
16
- ]);
17
- return (0, react_1.useMemo)(function () { return (0, flatten_tree_1.flattenTree)(root); }, [root]);
18
- }
19
- exports.useVisibleNodes = useVisibleNodes;
@@ -1,24 +0,0 @@
1
- import { XYCoord } from "react-dnd";
2
- import { Node } from "../types";
3
- declare type Args = {
4
- element: HTMLElement;
5
- offset: XYCoord;
6
- indent: number;
7
- node: Node | null;
8
- prevNode: Node | null;
9
- nextNode: Node | null;
10
- };
11
- export declare function computeDrop({ element, offset, indent, node, prevNode, nextNode, }: Args): {
12
- parentId: string | null;
13
- index: number;
14
- cursor: {
15
- parentId: string | null;
16
- index: null;
17
- level: null;
18
- } | {
19
- parentId: string | null;
20
- index: number;
21
- level: number;
22
- };
23
- };
24
- export {};
@@ -1,113 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.computeDrop = void 0;
4
- var utils_1 = require("../utils");
5
- function measureHover(el, offset) {
6
- var rect = el.getBoundingClientRect();
7
- var x = offset.x - Math.round(rect.x);
8
- var y = offset.y - Math.round(rect.y);
9
- var height = rect.height;
10
- var inTopHalf = y < height / 2;
11
- var inBottomHalf = !inTopHalf;
12
- var pad = height / 4;
13
- var inMiddle = y > pad && y < height - pad;
14
- return { x: x, inTopHalf: inTopHalf, inBottomHalf: inBottomHalf, inMiddle: inMiddle };
15
- }
16
- function getNodesAroundCursor(node, prev, next, hover) {
17
- if (!node) {
18
- // We're hoving over the empty part of the list, not over an item,
19
- // Put the cursor below the last item which is "prev"
20
- return [prev, null];
21
- }
22
- if ((0, utils_1.isFolder)(node)) {
23
- if (!hover.inMiddle && hover.inTopHalf) {
24
- return [prev, node];
25
- }
26
- else {
27
- return [node, next];
28
- }
29
- }
30
- else {
31
- if (hover.inTopHalf) {
32
- return [prev, node];
33
- }
34
- else {
35
- return [node, next];
36
- }
37
- }
38
- }
39
- function getDropLocation(nodeAboveCursor, level, hover) {
40
- var _a;
41
- var dropIndex = 0;
42
- var drop = nodeAboveCursor;
43
- var dropParent = drop;
44
- if (!drop || !drop.isDroppable) {
45
- return [null, 0];
46
- }
47
- if ((0, utils_1.isFolder)(drop) && drop.isOpen && ((_a = drop.children) === null || _a === void 0 ? void 0 : _a.length)) {
48
- // keep args the same
49
- }
50
- else if ((0, utils_1.isFolder)(drop) && hover.inMiddle) {
51
- // keep args the same
52
- }
53
- else {
54
- while (drop.parent && drop.level > level) {
55
- drop = drop.parent;
56
- }
57
- dropIndex = (0, utils_1.indexOf)(drop) + 1;
58
- dropParent = drop.parent;
59
- }
60
- return [dropParent, dropIndex];
61
- }
62
- function getDropLevel(hovering, aboveCursor, belowCursor, indent) {
63
- var hoverLevel = Math.round(Math.max(0, hovering.x - indent) / indent);
64
- var min, max;
65
- if (!aboveCursor) {
66
- max = 0;
67
- min = 0;
68
- }
69
- else if (!belowCursor) {
70
- max = aboveCursor.level;
71
- min = 0;
72
- }
73
- else {
74
- max = aboveCursor.level;
75
- min = belowCursor.level;
76
- }
77
- return (0, utils_1.bound)(hoverLevel, min, max);
78
- }
79
- function computeDrop(_a) {
80
- var _b, _c;
81
- var element = _a.element, offset = _a.offset, indent = _a.indent, node = _a.node, prevNode = _a.prevNode, nextNode = _a.nextNode;
82
- var hovering = measureHover(element, offset);
83
- var _d = getNodesAroundCursor(node, prevNode, nextNode, hovering), above = _d[0], below = _d[1];
84
- if (!above) {
85
- return {
86
- parentId: ((_b = below === null || below === void 0 ? void 0 : below.parent) === null || _b === void 0 ? void 0 : _b.id) || null,
87
- index: 0,
88
- cursor: {
89
- parentId: ((_c = below === null || below === void 0 ? void 0 : below.parent) === null || _c === void 0 ? void 0 : _c.id) || null,
90
- index: 0,
91
- level: 0,
92
- },
93
- };
94
- }
95
- var level = getDropLevel(hovering, above, below, indent);
96
- var _e = getDropLocation(above, level, hovering), parent = _e[0], index = _e[1];
97
- // @ts-ignore
98
- var ret = {
99
- parentId: parent ? parent.id : null,
100
- index: index,
101
- cursor: parent && index === 0
102
- ? { parentId: parent ? parent.id : null, index: null, level: null }
103
- : {
104
- parentId: parent ? parent.id : null,
105
- index: above.rowIndex + 1,
106
- level: level,
107
- },
108
- };
109
- if (node !== null && !node.isDroppable)
110
- ret.cursor = { parentId: null, index: null, level: null };
111
- return ret;
112
- }
113
- exports.computeDrop = computeDrop;
@@ -1,36 +0,0 @@
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
- function useDragHook(node) {
9
- var tree = (0, context_1.useStaticContext)();
10
- var isSelected = (0, context_1.useIsSelected)();
11
- var ids = (0, context_1.useSelectedIds)();
12
- var _a = (0, react_dnd_1.useDrag)(function () { return ({
13
- canDrag: function () { return node.isDraggable; },
14
- type: "NODE",
15
- item: function () { return ({
16
- id: node.id,
17
- dragIds: isSelected(node.rowIndex) ? ids : [node.id],
18
- }); },
19
- collect: function (m) { return ({
20
- isDragging: m.isDragging(),
21
- }); },
22
- end: function (item, monitor) {
23
- tree.api.hideCursor();
24
- var drop = monitor.getDropResult();
25
- if (drop && drop.parentId) {
26
- tree.onMove(item.dragIds, drop.parentId, drop.index);
27
- tree.onToggle(drop.parentId, true);
28
- }
29
- },
30
- }); }, [ids, node]), isDragging = _a[0].isDragging, ref = _a[1], preview = _a[2];
31
- (0, react_1.useEffect)(function () {
32
- preview((0, react_dnd_html5_backend_1.getEmptyImage)());
33
- }, [preview]);
34
- return [{ isDragging: isDragging }, ref];
35
- }
36
- exports.useDragHook = useDragHook;
@@ -1,60 +0,0 @@
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 utils_1 = require("../utils");
7
- var compute_drop_1 = require("./compute-drop");
8
- function useDropHook(el, node, prev, next) {
9
- var tree = (0, context_1.useStaticContext)();
10
- return (0, react_dnd_1.useDrop)(function () { return ({
11
- accept: "NODE",
12
- canDrop: function (item) {
13
- if (!node.isDroppable)
14
- return false;
15
- for (var _i = 0, _a = item.dragIds; _i < _a.length; _i++) {
16
- var id = _a[_i];
17
- var drag = tree.api.getNode(id);
18
- if (!drag)
19
- return false;
20
- if ((0, utils_1.isFolder)(drag) && (0, utils_1.isDecendent)(node, drag))
21
- return false;
22
- }
23
- return true;
24
- },
25
- hover: function (item, m) {
26
- if (m.canDrop()) {
27
- var offset = m.getClientOffset();
28
- if (!el.current || !offset)
29
- return;
30
- var cursor = (0, compute_drop_1.computeDrop)({
31
- element: el.current,
32
- offset: offset,
33
- indent: tree.indent,
34
- node: node,
35
- prevNode: prev,
36
- nextNode: next,
37
- }).cursor;
38
- tree.api.showCursor(cursor);
39
- }
40
- else {
41
- tree.api.hideCursor();
42
- }
43
- },
44
- drop: function (item, m) {
45
- var offset = m.getClientOffset();
46
- if (!el.current || !offset)
47
- return;
48
- var _a = (0, compute_drop_1.computeDrop)({
49
- element: el.current,
50
- offset: offset,
51
- indent: tree.indent,
52
- node: node,
53
- prevNode: prev,
54
- nextNode: next,
55
- }), parentId = _a.parentId, index = _a.index;
56
- return { parentId: parentId, index: index };
57
- },
58
- }); }, [node, prev, el, tree]);
59
- }
60
- exports.useDropHook = useDropHook;
@@ -1,8 +0,0 @@
1
- import { XYCoord } from "react-dnd";
2
- export declare function measureHover(el: HTMLElement, offset: XYCoord, indent: number): {
3
- level: number;
4
- inTopHalf: boolean;
5
- inBottomHalf: boolean;
6
- inMiddle: boolean;
7
- };
8
- export declare type HoverData = ReturnType<typeof measureHover>;
@@ -1,21 +0,0 @@
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) {
6
- var nextEl = el.nextElementSibling;
7
- var prevEl = el.previousElementSibling;
8
- var rect = el.getBoundingClientRect();
9
- var x = offset.x - Math.round(rect.x);
10
- var y = offset.y - Math.round(rect.y);
11
- var height = rect.height;
12
- var inTopHalf = y < height / 2;
13
- var inBottomHalf = !inTopHalf;
14
- var pad = height / 4;
15
- var inMiddle = y > pad && y < height - pad;
16
- var maxLevel = Number(inBottomHalf ? el.dataset.level : prevEl ? prevEl.dataset.level : 0);
17
- var minLevel = Number(inTopHalf ? el.dataset.level : nextEl ? nextEl.dataset.level : 0);
18
- var level = (0, utils_1.bound)(Math.floor(x / indent), minLevel, maxLevel);
19
- return { level: level, inTopHalf: inTopHalf, inBottomHalf: inBottomHalf, inMiddle: inMiddle };
20
- }
21
- exports.measureHover = measureHover;
@@ -1,50 +0,0 @@
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 compute_drop_1 = require("./compute-drop");
7
- function useOuterDrop() {
8
- var tree = (0, context_1.useStaticContext)();
9
- // In case we drop an item at the bottom of the list
10
- var _a = (0, react_dnd_1.useDrop)(function () { return ({
11
- accept: "NODE",
12
- hover: function (item, m) {
13
- if (!m.isOver({ shallow: true }))
14
- return;
15
- var offset = m.getClientOffset();
16
- if (!tree.listEl.current || !offset)
17
- return;
18
- var cursor = (0, compute_drop_1.computeDrop)({
19
- element: tree.listEl.current,
20
- offset: offset,
21
- indent: tree.indent,
22
- node: null,
23
- prevNode: tree.api.visibleNodes[tree.api.visibleNodes.length - 1],
24
- nextNode: null,
25
- }).cursor;
26
- tree.api.showCursor(cursor);
27
- },
28
- canDrop: function (item, m) {
29
- return m.isOver({ shallow: true });
30
- },
31
- drop: function (item, m) {
32
- if (m.didDrop())
33
- return;
34
- var offset = m.getClientOffset();
35
- if (!tree.listEl.current || !offset)
36
- return;
37
- var _a = (0, compute_drop_1.computeDrop)({
38
- element: tree.listEl.current,
39
- offset: offset,
40
- indent: tree.indent,
41
- node: null,
42
- prevNode: tree.api.visibleNodes[tree.api.visibleNodes.length - 1],
43
- nextNode: null,
44
- }), parentId = _a.parentId, index = _a.index;
45
- return { parentId: parentId, index: index };
46
- },
47
- }); }, [tree]), drop = _a[1];
48
- drop(tree.listEl);
49
- }
50
- exports.useOuterDrop = useOuterDrop;
@@ -1,3 +0,0 @@
1
- export { Tree } from "./components/tree";
2
- export type { NodeRenderer, NodeState, NodeHandlers } from "./types";
3
- export { TreeApi } from "./tree-api";
package/dist/lib/index.js DELETED
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TreeApi = exports.Tree = void 0;
4
- var tree_1 = require("./components/tree");
5
- Object.defineProperty(exports, "Tree", { enumerable: true, get: function () { return tree_1.Tree; } });
6
- var tree_api_1 = require("./tree-api");
7
- Object.defineProperty(exports, "TreeApi", { enumerable: true, get: function () { return tree_api_1.TreeApi; } });
@@ -1,44 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.TreeViewProvider = void 0;
15
- var jsx_runtime_1 = require("react/jsx-runtime");
16
- var react_1 = require("react");
17
- var context_1 = require("./context");
18
- var reducer_1 = require("./reducer");
19
- var selection_hook_1 = require("./selection/selection-hook");
20
- var tree_api_hook_1 = require("./tree-api-hook");
21
- function TreeViewProvider(props) {
22
- var _a;
23
- var _b = (0, react_1.useReducer)(reducer_1.reducer, (0, reducer_1.initState)()), state = _b[0], dispatch = _b[1];
24
- var list = (0, react_1.useRef)();
25
- var api = (0, tree_api_hook_1.useTreeApi)(state, dispatch, props, list.current);
26
- (0, react_1.useImperativeHandle)(props.imperativeHandle, function () { return api; });
27
- (0, selection_hook_1.useSelectionKeys)(props.listEl, api);
28
- var staticValue = (0, react_1.useMemo)(function () { return (__assign(__assign({}, props), { api: api, list: list })); }, [props, api, list]);
29
- /**
30
- * This context pattern is ridiculous, next time use redux.
31
- */
32
- return (
33
- // @ts-ignore
34
- (0, jsx_runtime_1.jsx)(context_1.Static.Provider, __assign({ value: staticValue }, { children: (0, jsx_runtime_1.jsx)(context_1.EditingIdContext.Provider, __assign({ value: state.editingId }, { children: (0, jsx_runtime_1.jsx)(context_1.SelectionContext.Provider, __assign({ value: state.selection }, { children: (0, jsx_runtime_1.jsx)(context_1.CursorParentId.Provider, __assign({ value: ((_a = state.cursorLocation) === null || _a === void 0 ? void 0 : _a.parentId) || null }, { children: (0, jsx_runtime_1.jsx)(context_1.IsCursorOverFolder.Provider, __assign({ value: isOverFolder(state) }, { children: (0, jsx_runtime_1.jsx)(context_1.CursorLocationContext.Provider, __assign({ value: state.cursorLocation }, { children: props.children }), void 0) }), void 0) }), void 0) }), void 0) }), void 0) }), void 0));
35
- }
36
- exports.TreeViewProvider = TreeViewProvider;
37
- function isOverFolder(state) {
38
- if (state.cursorLocation) {
39
- return (!!state.cursorLocation.parentId && state.cursorLocation.index === null);
40
- }
41
- else {
42
- return false;
43
- }
44
- }
@@ -1,149 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.reducer = exports.actions = exports.initState = void 0;
15
- var selection_1 = require("./selection/selection");
16
- var initState = function () { return ({
17
- visibleIds: [],
18
- cursorLocation: null,
19
- editingId: null,
20
- selection: {
21
- data: null,
22
- ids: [],
23
- },
24
- }); };
25
- exports.initState = initState;
26
- exports.actions = {
27
- setCursorLocation: function (location) { return ({
28
- type: "SET_CURSOR_LOCATION",
29
- location: location,
30
- }); },
31
- setVisibleIds: function (ids, // index to id
32
- idMap // id to index
33
- ) { return ({
34
- type: "SET_VISIBLE_IDS",
35
- ids: ids,
36
- idMap: idMap,
37
- }); },
38
- select: function (index, meta, shift) { return ({
39
- type: "SELECT",
40
- index: index,
41
- meta: meta,
42
- shift: shift,
43
- }); },
44
- selectId: function (id) { return ({
45
- type: "SELECT_ID",
46
- id: id,
47
- }); },
48
- edit: function (id) { return ({
49
- type: "EDIT",
50
- id: id,
51
- }); },
52
- stepUp: function (shift, ids) { return ({
53
- type: "STEP_UP",
54
- shift: shift,
55
- }); },
56
- stepDown: function (shift, ids) { return ({
57
- type: "STEP_DOWN",
58
- shift: shift,
59
- }); },
60
- };
61
- function reducer(state, action) {
62
- switch (action.type) {
63
- case "EDIT":
64
- return __assign(__assign({}, state), { editingId: action.id });
65
- case "SET_CURSOR_LOCATION":
66
- if (equal(state.cursorLocation, action.location)) {
67
- return state;
68
- }
69
- else {
70
- return __assign(__assign({}, state), { cursorLocation: action.location });
71
- }
72
- case "SELECT":
73
- var s = selection_1.Selection.parse(state.selection.data, state.visibleIds);
74
- if (action.index === null) {
75
- s.clear();
76
- }
77
- else if (action.meta) {
78
- if (s.contains(action.index)) {
79
- s.deselect(action.index);
80
- }
81
- else {
82
- s.multiSelect(action.index);
83
- }
84
- }
85
- else if (action.shift) {
86
- s.extend(action.index);
87
- }
88
- else {
89
- s.select(action.index);
90
- }
91
- return __assign(__assign({}, state), { selection: {
92
- data: s.serialize(),
93
- ids: s.getSelectedItems(),
94
- } });
95
- case "SELECT_ID":
96
- return __assign(__assign({}, state), { selection: __assign(__assign({}, state.selection), { ids: [action.id] }) });
97
- case "STEP_UP":
98
- var s3 = selection_1.Selection.parse(state.selection.data, state.visibleIds);
99
- var f = s3.getFocus();
100
- if (action.shift) {
101
- s3.extend(f - 1);
102
- }
103
- else {
104
- s3.select(f - 1);
105
- }
106
- return __assign(__assign({}, state), { selection: {
107
- data: s3.serialize(),
108
- ids: s3.getSelectedItems(),
109
- } });
110
- case "STEP_DOWN":
111
- var s6 = selection_1.Selection.parse(state.selection.data, state.visibleIds);
112
- var f2 = s6.getFocus();
113
- if (action.shift) {
114
- s6.extend(f2 + 1);
115
- }
116
- else {
117
- s6.select(f2 + 1);
118
- }
119
- return __assign(__assign({}, state), { selection: {
120
- data: s6.serialize(),
121
- ids: s6.getSelectedItems(),
122
- } });
123
- case "SET_VISIBLE_IDS":
124
- // The visible ids changed
125
- var ids = state.selection.ids;
126
- // Start with a blank selection
127
- var s2 = new selection_1.Selection([], null, "none", state.visibleIds);
128
- // Add each of the old selected ids to this new selection
129
- for (var _i = 0, ids_1 = ids; _i < ids_1.length; _i++) {
130
- var id = ids_1[_i];
131
- if (id in action.idMap)
132
- s2.multiSelect(action.idMap[id]);
133
- }
134
- return __assign(__assign({}, state), { visibleIds: action.ids, selection: {
135
- ids: ids,
136
- data: s2.serialize(),
137
- } });
138
- default:
139
- return state;
140
- }
141
- }
142
- exports.reducer = reducer;
143
- function equal(a, b) {
144
- if (a === b)
145
- return true;
146
- if (a === null || b === null)
147
- return false;
148
- return (a.parentId === b.parentId && a.index === b.index && a.level === b.level);
149
- }
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Range = void 0;
4
- var Range = /** @class */ (function () {
5
- function Range(start, end) {
6
- this.start = start;
7
- this.end = end;
8
- if (this.start > this.end)
9
- throw new Error("Invalid range: start larger than end");
10
- }
11
- Range.prototype.serialize = function () {
12
- return [this.start, this.end];
13
- };
14
- Range.prototype.contains = function (n) {
15
- return n >= this.start && n <= this.end;
16
- };
17
- Range.prototype.overlaps = function (r) {
18
- return this.contains(r.start - 1) || this.contains(r.end + 1);
19
- };
20
- Range.prototype.combine = function (r) {
21
- this.start = Math.min(r.start, this.start);
22
- this.end = Math.max(r.end, this.end);
23
- };
24
- Object.defineProperty(Range.prototype, "size", {
25
- get: function () {
26
- return this.end - this.start + 1;
27
- },
28
- enumerable: false,
29
- configurable: true
30
- });
31
- Range.prototype.clone = function () {
32
- return new Range(this.start, this.end);
33
- };
34
- Range.prototype.map = function (fn) {
35
- var returns = [];
36
- for (var i = this.start; i <= this.end; i++)
37
- returns.push(fn(i));
38
- return returns;
39
- };
40
- Range.prototype.isEqual = function (other) {
41
- return this.start === other.start && this.end === other.end;
42
- };
43
- return Range;
44
- }());
45
- exports.Range = Range;
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useSelectionKeys = void 0;
4
- var react_1 = require("react");
5
- function useSelectionKeys(ref, api) {
6
- (0, react_1.useEffect)(function () {
7
- var el = ref.current;
8
- var cb = function (e) {
9
- if (e.code === "ArrowDown") {
10
- e.preventDefault();
11
- api.selectDownwards(e.shiftKey);
12
- }
13
- else if (e.code === "ArrowUp") {
14
- e.preventDefault();
15
- api.selectUpwards(e.shiftKey);
16
- }
17
- };
18
- el === null || el === void 0 ? void 0 : el.addEventListener("keydown", cb);
19
- return function () {
20
- el === null || el === void 0 ? void 0 : el.removeEventListener("keydown", cb);
21
- };
22
- }, [ref, api]);
23
- }
24
- exports.useSelectionKeys = useSelectionKeys;