react-arborist 0.1.13 → 0.2.0-beta.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.
- package/dist/{lib/components → components}/drop-cursor.d.ts +1 -0
- package/dist/{lib/components → components}/preview.d.ts +1 -0
- package/dist/{lib/components → components}/row.d.ts +0 -0
- package/dist/{lib/components → components}/tree.d.ts +0 -0
- package/dist/{lib/context.d.ts → context.d.ts} +0 -0
- package/dist/{lib/data → data}/enrich-tree.d.ts +0 -0
- package/dist/{lib/data → data}/flatten-tree.d.ts +0 -0
- package/dist/{lib/dnd → dnd}/compute-drop.d.ts +0 -0
- package/dist/{lib/dnd → dnd}/drag-hook.d.ts +0 -0
- package/dist/{lib/dnd → dnd}/drop-hook.d.ts +0 -0
- package/dist/{lib/dnd → dnd}/outer-drop-hook.d.ts +0 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1353 -0
- package/dist/index.js.map +1 -0
- package/dist/module.js +1345 -0
- package/dist/module.js.map +1 -0
- package/dist/{lib/provider.d.ts → provider.d.ts} +1 -0
- package/dist/{lib/reducer.d.ts → reducer.d.ts} +0 -0
- package/dist/{lib/selection → selection}/range.d.ts +0 -0
- package/dist/{lib/selection → selection}/selection-hook.d.ts +0 -0
- package/dist/{lib/selection → selection}/selection.d.ts +0 -0
- package/dist/{lib/tree-api-hook.d.ts → tree-api-hook.d.ts} +0 -0
- package/dist/{lib/tree-api.d.ts → tree-api.d.ts} +1 -1
- package/dist/{lib/types.d.ts → types.d.ts} +4 -0
- package/dist/{lib/utils.d.ts → utils.d.ts} +0 -0
- package/package.json +18 -45
- package/src/components/drop-cursor.tsx +47 -0
- package/src/components/preview.tsx +108 -0
- package/src/components/row.tsx +119 -0
- package/src/components/tree.tsx +118 -0
- package/src/context.tsx +52 -0
- package/src/data/enrich-tree.ts +74 -0
- package/src/data/flatten-tree.ts +17 -0
- package/src/data/make-tree.ts +37 -0
- package/src/dnd/compute-drop.ts +184 -0
- package/src/dnd/drag-hook.ts +48 -0
- package/src/dnd/drop-hook.ts +66 -0
- package/src/dnd/measure-hover.ts +26 -0
- package/src/dnd/outer-drop-hook.ts +50 -0
- package/src/index.ts +5 -0
- package/src/provider.tsx +61 -0
- package/src/reducer.ts +161 -0
- package/src/selection/range.ts +41 -0
- package/src/selection/selection-hook.ts +24 -0
- package/src/selection/selection.test.ts +111 -0
- package/src/selection/selection.ts +186 -0
- package/src/tree-api-hook.ts +34 -0
- package/src/tree-api.ts +129 -0
- package/src/types.ts +147 -0
- package/src/utils.ts +35 -0
- package/tsconfig.json +28 -0
- package/README.md +0 -197
- package/dist/lib/components/drop-cursor.js +0 -53
- package/dist/lib/components/preview.js +0 -91
- package/dist/lib/components/row.js +0 -122
- package/dist/lib/components/tree.js +0 -76
- package/dist/lib/context.js +0 -57
- package/dist/lib/data/enrich-tree.js +0 -48
- package/dist/lib/data/flatten-tree.js +0 -20
- package/dist/lib/data/make-tree.d.ts +0 -5
- package/dist/lib/data/make-tree.js +0 -40
- package/dist/lib/data/visible-nodes-hook.d.ts +0 -2
- package/dist/lib/data/visible-nodes-hook.js +0 -19
- package/dist/lib/dnd/compute-drop.js +0 -143
- package/dist/lib/dnd/drag-hook.js +0 -36
- package/dist/lib/dnd/drop-hook.js +0 -61
- package/dist/lib/dnd/measure-hover.d.ts +0 -8
- package/dist/lib/dnd/measure-hover.js +0 -21
- package/dist/lib/dnd/outer-drop-hook.js +0 -51
- package/dist/lib/index.d.ts +0 -3
- package/dist/lib/index.js +0 -7
- package/dist/lib/provider.js +0 -46
- package/dist/lib/reducer.js +0 -147
- package/dist/lib/selection/range.js +0 -45
- package/dist/lib/selection/selection-hook.js +0 -24
- package/dist/lib/selection/selection.js +0 -192
- package/dist/lib/selection/selection.test.d.ts +0 -1
- package/dist/lib/selection/selection.test.js +0 -102
- package/dist/lib/tree-api-hook.js +0 -26
- package/dist/lib/tree-api.js +0 -130
- package/dist/lib/tree-monitor.d.ts +0 -15
- package/dist/lib/tree-monitor.js +0 -32
- package/dist/lib/types.js +0 -2
- package/dist/lib/utils.js +0 -39
|
@@ -1,51 +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
|
-
if (cursor)
|
|
27
|
-
tree.api.showCursor(cursor);
|
|
28
|
-
},
|
|
29
|
-
canDrop: function (item, m) {
|
|
30
|
-
return m.isOver({ shallow: true });
|
|
31
|
-
},
|
|
32
|
-
drop: function (item, m) {
|
|
33
|
-
if (m.didDrop())
|
|
34
|
-
return;
|
|
35
|
-
var offset = m.getClientOffset();
|
|
36
|
-
if (!tree.listEl.current || !offset)
|
|
37
|
-
return;
|
|
38
|
-
var drop = (0, compute_drop_1.computeDrop)({
|
|
39
|
-
element: tree.listEl.current,
|
|
40
|
-
offset: offset,
|
|
41
|
-
indent: tree.indent,
|
|
42
|
-
node: null,
|
|
43
|
-
prevNode: tree.api.visibleNodes[tree.api.visibleNodes.length - 1],
|
|
44
|
-
nextNode: null,
|
|
45
|
-
}).drop;
|
|
46
|
-
return drop;
|
|
47
|
-
},
|
|
48
|
-
}); }, [tree]), drop = _a[1];
|
|
49
|
-
drop(tree.listEl);
|
|
50
|
-
}
|
|
51
|
-
exports.useOuterDrop = useOuterDrop;
|
package/dist/lib/index.d.ts
DELETED
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; } });
|
package/dist/lib/provider.js
DELETED
|
@@ -1,46 +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 = (0, react_1.useReducer)(reducer_1.reducer, (0, reducer_1.initState)()), state = _a[0], dispatch = _a[1];
|
|
23
|
-
var list = (0, react_1.useRef)();
|
|
24
|
-
var api = (0, tree_api_hook_1.useTreeApi)(state, dispatch, props, list.current);
|
|
25
|
-
(0, react_1.useImperativeHandle)(props.imperativeHandle, function () { return api; });
|
|
26
|
-
(0, selection_hook_1.useSelectionKeys)(props.listEl, api);
|
|
27
|
-
var staticValue = (0, react_1.useMemo)(function () { return (__assign(__assign({}, props), { api: api, list: list })); }, [props, api, list]);
|
|
28
|
-
/**
|
|
29
|
-
* This context pattern is ridiculous, next time use redux.
|
|
30
|
-
*/
|
|
31
|
-
return (
|
|
32
|
-
// @ts-ignore
|
|
33
|
-
(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: getParentId(state.cursor) }, { 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.cursor }, { children: props.children }), void 0) }), void 0) }), void 0) }), void 0) }), void 0) }), void 0));
|
|
34
|
-
}
|
|
35
|
-
exports.TreeViewProvider = TreeViewProvider;
|
|
36
|
-
function getParentId(cursor) {
|
|
37
|
-
switch (cursor.type) {
|
|
38
|
-
case "highlight":
|
|
39
|
-
return cursor.id;
|
|
40
|
-
default:
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function isOverFolder(state) {
|
|
45
|
-
return state.cursor.type === "highlight";
|
|
46
|
-
}
|
package/dist/lib/reducer.js
DELETED
|
@@ -1,147 +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
|
-
cursor: { type: "none" },
|
|
19
|
-
editingId: null,
|
|
20
|
-
selection: {
|
|
21
|
-
data: null,
|
|
22
|
-
ids: [],
|
|
23
|
-
},
|
|
24
|
-
}); };
|
|
25
|
-
exports.initState = initState;
|
|
26
|
-
exports.actions = {
|
|
27
|
-
setCursorLocation: function (cursor) { return ({
|
|
28
|
-
type: "SET_CURSOR_LOCATION",
|
|
29
|
-
cursor: cursor,
|
|
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.cursor, action.cursor)) {
|
|
67
|
-
return state;
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
return __assign(__assign({}, state), { cursor: action.cursor });
|
|
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 === null || b === null)
|
|
145
|
-
return false;
|
|
146
|
-
return JSON.stringify(a) === JSON.stringify(b);
|
|
147
|
-
}
|
|
@@ -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;
|
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Selection = void 0;
|
|
4
|
-
var range_1 = require("./range");
|
|
5
|
-
var Selection = /** @class */ (function () {
|
|
6
|
-
function Selection(ranges, currentIndex, direction, items) {
|
|
7
|
-
var _this = this;
|
|
8
|
-
if (ranges === void 0) { ranges = []; }
|
|
9
|
-
if (currentIndex === void 0) { currentIndex = ranges.length ? ranges.length - 1 : null; }
|
|
10
|
-
if (direction === void 0) { direction = "none"; }
|
|
11
|
-
if (items === void 0) { items = []; }
|
|
12
|
-
this.ranges = [];
|
|
13
|
-
this.direction = "none";
|
|
14
|
-
ranges.forEach(function (_a) {
|
|
15
|
-
var s = _a[0], e = _a[1];
|
|
16
|
-
return _this.addRange(s, e);
|
|
17
|
-
});
|
|
18
|
-
this.currentIndex = currentIndex;
|
|
19
|
-
this.direction = direction;
|
|
20
|
-
this.items = items;
|
|
21
|
-
}
|
|
22
|
-
Selection.parse = function (data, items) {
|
|
23
|
-
if (data) {
|
|
24
|
-
return new Selection(data.ranges, data.currentIndex, data.direction, items);
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
return new Selection();
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
Object.defineProperty(Selection.prototype, "current", {
|
|
31
|
-
get: function () {
|
|
32
|
-
if (this.currentIndex === null)
|
|
33
|
-
return null;
|
|
34
|
-
var range = this.ranges[this.currentIndex];
|
|
35
|
-
if (!range) {
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
return range;
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
enumerable: false,
|
|
43
|
-
configurable: true
|
|
44
|
-
});
|
|
45
|
-
Selection.prototype.select = function (n) {
|
|
46
|
-
if (n < 0 || n >= this.items.length)
|
|
47
|
-
return;
|
|
48
|
-
this.clear();
|
|
49
|
-
this.currentIndex = this.addRange(n, n);
|
|
50
|
-
};
|
|
51
|
-
Selection.prototype.multiSelect = function (n) {
|
|
52
|
-
if (n < 0 || n >= this.items.length)
|
|
53
|
-
return;
|
|
54
|
-
if (this.contains(n))
|
|
55
|
-
return;
|
|
56
|
-
this.currentIndex = this.addRange(n, n);
|
|
57
|
-
this.compact(n);
|
|
58
|
-
};
|
|
59
|
-
Selection.prototype.deselect = function (n) {
|
|
60
|
-
if (n < 0 || n >= this.items.length)
|
|
61
|
-
return;
|
|
62
|
-
var r = this.ranges.find(function (r) { return r.contains(n); });
|
|
63
|
-
if (!r)
|
|
64
|
-
return;
|
|
65
|
-
else if (r.size === 1)
|
|
66
|
-
this.removeRange(r);
|
|
67
|
-
else if (r.start === n)
|
|
68
|
-
r.start++;
|
|
69
|
-
else if (r.end === n)
|
|
70
|
-
r.end--;
|
|
71
|
-
else {
|
|
72
|
-
this.removeRange(r);
|
|
73
|
-
this.addRange(r.start, n - 1);
|
|
74
|
-
this.currentIndex = this.addRange(n + 1, r.end);
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
Selection.prototype.getSelectedItems = function () {
|
|
78
|
-
var _this = this;
|
|
79
|
-
return this.ranges.flatMap(function (range) {
|
|
80
|
-
return range.map(function (index) { return _this.items[index]; });
|
|
81
|
-
});
|
|
82
|
-
};
|
|
83
|
-
Selection.prototype.extend = function (n) {
|
|
84
|
-
if (n < 0 || n >= this.items.length)
|
|
85
|
-
return;
|
|
86
|
-
if (this.isEmpty()) {
|
|
87
|
-
this.select(n);
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
var anchor = this.getAnchor();
|
|
91
|
-
if (anchor !== null && this.current) {
|
|
92
|
-
var _a = [n, anchor].sort(function (a, b) { return a - b; }), start = _a[0], end = _a[1];
|
|
93
|
-
this.current.start = start;
|
|
94
|
-
this.current.end = end;
|
|
95
|
-
this.compact(n);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
Selection.prototype.contains = function (n) {
|
|
100
|
-
if (n === null)
|
|
101
|
-
return false;
|
|
102
|
-
return this.ranges.some(function (r) { return r.contains(n); });
|
|
103
|
-
};
|
|
104
|
-
Selection.prototype.getRanges = function () {
|
|
105
|
-
return this.ranges.map(function (r) { return r.serialize(); });
|
|
106
|
-
};
|
|
107
|
-
Selection.prototype.clear = function () {
|
|
108
|
-
this.ranges = [];
|
|
109
|
-
this.currentIndex = null;
|
|
110
|
-
this.direction = "none";
|
|
111
|
-
};
|
|
112
|
-
Selection.prototype.serialize = function () {
|
|
113
|
-
return {
|
|
114
|
-
ranges: this.getRanges(),
|
|
115
|
-
currentIndex: this.currentIndex,
|
|
116
|
-
direction: this.direction,
|
|
117
|
-
};
|
|
118
|
-
};
|
|
119
|
-
Selection.prototype.isEqual = function (other) {
|
|
120
|
-
if (other.ranges.length !== this.ranges.length)
|
|
121
|
-
return false;
|
|
122
|
-
for (var i = 0; i < this.ranges.length; ++i) {
|
|
123
|
-
if (!this.ranges[i].isEqual(other.ranges[i]))
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
return true;
|
|
127
|
-
};
|
|
128
|
-
Selection.prototype.addRange = function (start, end) {
|
|
129
|
-
var r = new range_1.Range(start, end);
|
|
130
|
-
// Keep ranges sorted by start
|
|
131
|
-
var index = this.ranges.findIndex(function (r) { return r.start >= start; });
|
|
132
|
-
if (index === -1)
|
|
133
|
-
this.ranges.push(r);
|
|
134
|
-
else
|
|
135
|
-
this.ranges.splice(index, 0, r);
|
|
136
|
-
return index === -1 ? this.ranges.length - 1 : index;
|
|
137
|
-
};
|
|
138
|
-
Selection.prototype.removeRange = function (r) {
|
|
139
|
-
var index = this.ranges.indexOf(r);
|
|
140
|
-
this.ranges.splice(index, 1);
|
|
141
|
-
if (this.isEmpty()) {
|
|
142
|
-
this.currentIndex = null;
|
|
143
|
-
}
|
|
144
|
-
else if (index === this.currentIndex) {
|
|
145
|
-
this.currentIndex = this.ranges.length - 1;
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
Selection.prototype.isEmpty = function () {
|
|
149
|
-
return this.ranges.length === 0;
|
|
150
|
-
};
|
|
151
|
-
Selection.prototype.getAnchor = function () {
|
|
152
|
-
if (!this.current)
|
|
153
|
-
return null;
|
|
154
|
-
return this.direction === "backward"
|
|
155
|
-
? this.current.end
|
|
156
|
-
: this.current.start;
|
|
157
|
-
};
|
|
158
|
-
Selection.prototype.getFocus = function () {
|
|
159
|
-
if (!this.current)
|
|
160
|
-
return -1;
|
|
161
|
-
return this.direction === "backward"
|
|
162
|
-
? this.current.start
|
|
163
|
-
: this.current.end;
|
|
164
|
-
};
|
|
165
|
-
Selection.prototype.compact = function (focus) {
|
|
166
|
-
var _this = this;
|
|
167
|
-
var removals = [];
|
|
168
|
-
var current = this.current;
|
|
169
|
-
for (var _i = 0, _a = this.ranges; _i < _a.length; _i++) {
|
|
170
|
-
var r = _a[_i];
|
|
171
|
-
if (!this.current || r === this.current)
|
|
172
|
-
continue;
|
|
173
|
-
if (this.current.overlaps(r)) {
|
|
174
|
-
this.current.combine(r);
|
|
175
|
-
removals.push(r);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
removals.forEach(function (r) { return _this.removeRange(r); });
|
|
179
|
-
if (current)
|
|
180
|
-
this.currentIndex = this.ranges.indexOf(current);
|
|
181
|
-
if (!this.current)
|
|
182
|
-
return;
|
|
183
|
-
if (this.current.start < focus)
|
|
184
|
-
this.direction = "forward";
|
|
185
|
-
else if (this.current.end > focus)
|
|
186
|
-
this.direction = "backward";
|
|
187
|
-
else
|
|
188
|
-
this.direction = "none";
|
|
189
|
-
};
|
|
190
|
-
return Selection;
|
|
191
|
-
}());
|
|
192
|
-
exports.Selection = Selection;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var selection_1 = require("./selection");
|
|
4
|
-
var createSelection = function () {
|
|
5
|
-
var ranges = [];
|
|
6
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
7
|
-
ranges[_i] = arguments[_i];
|
|
8
|
-
}
|
|
9
|
-
return new selection_1.Selection(ranges);
|
|
10
|
-
};
|
|
11
|
-
describe("select", function () {
|
|
12
|
-
test("select one after end", function () {
|
|
13
|
-
var s = createSelection([0, 0]);
|
|
14
|
-
s.multiSelect(1);
|
|
15
|
-
expect(s.getRanges()).toEqual([[0, 1]]);
|
|
16
|
-
expect(s.direction).toEqual("forward");
|
|
17
|
-
});
|
|
18
|
-
test("select one before start", function () {
|
|
19
|
-
var s = createSelection([1, 1]);
|
|
20
|
-
s.multiSelect(0);
|
|
21
|
-
expect(s.getRanges()).toEqual([[0, 1]]);
|
|
22
|
-
expect(s.direction).toEqual("backward");
|
|
23
|
-
});
|
|
24
|
-
test("select between two ranges", function () {
|
|
25
|
-
var s = createSelection([0, 0], [2, 2]);
|
|
26
|
-
s.multiSelect(1);
|
|
27
|
-
expect(s.getRanges()).toEqual([[0, 2]]);
|
|
28
|
-
expect(s.direction).toEqual("forward");
|
|
29
|
-
});
|
|
30
|
-
test("select new spot", function () {
|
|
31
|
-
var s = createSelection([0, 0]);
|
|
32
|
-
s.multiSelect(5);
|
|
33
|
-
expect(s.getRanges()).toEqual([
|
|
34
|
-
[0, 0],
|
|
35
|
-
[5, 5],
|
|
36
|
-
]);
|
|
37
|
-
expect(s.direction).toEqual("none");
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
describe("deselect", function () {
|
|
41
|
-
test("one", function () {
|
|
42
|
-
var s = createSelection([0, 0]);
|
|
43
|
-
s.deselect(0);
|
|
44
|
-
expect(s.getRanges()).toEqual([]);
|
|
45
|
-
});
|
|
46
|
-
test("start of a range", function () {
|
|
47
|
-
var s = createSelection([0, 5]);
|
|
48
|
-
s.deselect(0);
|
|
49
|
-
expect(s.getRanges()).toEqual([[1, 5]]);
|
|
50
|
-
});
|
|
51
|
-
test("end of a range", function () {
|
|
52
|
-
var s = createSelection([0, 5]);
|
|
53
|
-
s.deselect(5);
|
|
54
|
-
expect(s.getRanges()).toEqual([[0, 4]]);
|
|
55
|
-
});
|
|
56
|
-
test("between a range", function () {
|
|
57
|
-
var s = createSelection([0, 5]);
|
|
58
|
-
s.deselect(3);
|
|
59
|
-
expect(s.getRanges()).toEqual([
|
|
60
|
-
[0, 2],
|
|
61
|
-
[4, 5],
|
|
62
|
-
]);
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
describe("extend", function () {
|
|
66
|
-
test("up", function () {
|
|
67
|
-
var s = createSelection();
|
|
68
|
-
s.multiSelect(5);
|
|
69
|
-
s.extend(6);
|
|
70
|
-
expect(s.getRanges()).toEqual([[5, 6]]);
|
|
71
|
-
});
|
|
72
|
-
test("down", function () {
|
|
73
|
-
var s = createSelection();
|
|
74
|
-
s.multiSelect(5);
|
|
75
|
-
s.extend(4);
|
|
76
|
-
expect(s.getRanges()).toEqual([[4, 5]]);
|
|
77
|
-
});
|
|
78
|
-
test("around anchor", function () {
|
|
79
|
-
var s = createSelection([5, 10]);
|
|
80
|
-
s.extend(1);
|
|
81
|
-
expect(s.getRanges()).toEqual([[1, 5]]);
|
|
82
|
-
});
|
|
83
|
-
test("through other ranges", function () {
|
|
84
|
-
var s = createSelection([0, 0], [5, 5], [9, 10]);
|
|
85
|
-
s.multiSelect(2);
|
|
86
|
-
s.extend(20);
|
|
87
|
-
expect(s.getRanges()).toEqual([
|
|
88
|
-
[0, 0],
|
|
89
|
-
[2, 20],
|
|
90
|
-
]);
|
|
91
|
-
});
|
|
92
|
-
test("clicking backward", function () {
|
|
93
|
-
var s = createSelection([15, 15]);
|
|
94
|
-
s.extend(3);
|
|
95
|
-
expect(s.getRanges()).toEqual([[3, 15]]);
|
|
96
|
-
});
|
|
97
|
-
test("split range then extend", function () {
|
|
98
|
-
var s = createSelection([5, 10]);
|
|
99
|
-
s.deselect(8);
|
|
100
|
-
expect(s.currentIndex).toEqual(1);
|
|
101
|
-
});
|
|
102
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useTreeApi = void 0;
|
|
4
|
-
var react_1 = require("react");
|
|
5
|
-
var reducer_1 = require("./reducer");
|
|
6
|
-
var tree_api_1 = require("./tree-api");
|
|
7
|
-
function useTreeApi(state, dispatch, props, list) {
|
|
8
|
-
/**
|
|
9
|
-
* We only ever want one instance of the api object
|
|
10
|
-
* It will get updated as the props change, but the
|
|
11
|
-
* reference will not.
|
|
12
|
-
*/
|
|
13
|
-
var api = (0, react_1.useMemo)(function () { return new tree_api_1.TreeApi(dispatch, state, props, list); },
|
|
14
|
-
// eslint-disable-next-line
|
|
15
|
-
[]);
|
|
16
|
-
api.assign(dispatch, state, props, list);
|
|
17
|
-
/**
|
|
18
|
-
* This ensures that the selection remains correct even
|
|
19
|
-
* after opening and closing a folders
|
|
20
|
-
*/
|
|
21
|
-
(0, react_1.useLayoutEffect)(function () {
|
|
22
|
-
dispatch(reducer_1.actions.setVisibleIds(api.visibleIds, api.idToIndex));
|
|
23
|
-
}, [dispatch, api, props.root]);
|
|
24
|
-
return api;
|
|
25
|
-
}
|
|
26
|
-
exports.useTreeApi = useTreeApi;
|