@headless-tree/core 1.0.0 → 1.1.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.
- package/CHANGELOG.md +23 -0
- package/lib/cjs/features/async-data-loader/feature.js +78 -68
- package/lib/cjs/features/async-data-loader/types.d.ts +12 -7
- package/lib/cjs/features/drag-and-drop/feature.js +1 -0
- package/lib/cjs/features/expand-all/feature.js +2 -2
- package/lib/cjs/features/hotkeys-core/feature.js +50 -18
- package/lib/cjs/features/hotkeys-core/types.d.ts +3 -0
- package/lib/cjs/features/keyboard-drag-and-drop/feature.js +1 -1
- package/lib/cjs/features/selection/feature.js +3 -3
- package/lib/cjs/features/sync-data-loader/feature.js +13 -9
- package/lib/cjs/features/sync-data-loader/types.d.ts +11 -2
- package/lib/cjs/features/tree/feature.js +17 -8
- package/lib/cjs/test-utils/test-tree-expect.js +1 -0
- package/lib/cjs/utilities/errors.d.ts +1 -0
- package/lib/cjs/utilities/errors.js +6 -2
- package/lib/esm/features/async-data-loader/feature.js +78 -68
- package/lib/esm/features/async-data-loader/types.d.ts +12 -7
- package/lib/esm/features/drag-and-drop/feature.js +1 -0
- package/lib/esm/features/expand-all/feature.js +2 -2
- package/lib/esm/features/hotkeys-core/feature.js +50 -18
- package/lib/esm/features/hotkeys-core/types.d.ts +3 -0
- package/lib/esm/features/keyboard-drag-and-drop/feature.js +1 -1
- package/lib/esm/features/selection/feature.js +3 -3
- package/lib/esm/features/sync-data-loader/feature.js +13 -9
- package/lib/esm/features/sync-data-loader/types.d.ts +11 -2
- package/lib/esm/features/tree/feature.js +17 -8
- package/lib/esm/test-utils/test-tree-expect.js +1 -0
- package/lib/esm/utilities/errors.d.ts +1 -0
- package/lib/esm/utilities/errors.js +4 -1
- package/package.json +1 -1
- package/src/features/async-data-loader/async-data-loader.spec.ts +82 -0
- package/src/features/async-data-loader/feature.ts +92 -67
- package/src/features/async-data-loader/types.ts +14 -7
- package/src/features/drag-and-drop/feature.ts +1 -0
- package/src/features/expand-all/feature.ts +2 -2
- package/src/features/hotkeys-core/feature.ts +56 -17
- package/src/features/hotkeys-core/types.ts +4 -0
- package/src/features/keyboard-drag-and-drop/feature.ts +1 -1
- package/src/features/search/types.ts +1 -1
- package/src/features/selection/feature.ts +3 -3
- package/src/features/sync-data-loader/feature.ts +16 -9
- package/src/features/sync-data-loader/types.ts +11 -4
- package/src/features/tree/feature.ts +22 -8
- package/src/test-utils/test-tree-expect.ts +1 -0
- package/src/utilities/errors.ts +6 -2
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { FeatureImplementation, ItemInstance } from "../../types/core";
|
|
2
2
|
import { ItemMeta } from "./types";
|
|
3
3
|
import { makeStateUpdater, poll } from "../../utils";
|
|
4
|
+
import { logWarning } from "../../utilities/errors";
|
|
4
5
|
|
|
5
6
|
export const treeFeature: FeatureImplementation<any> = {
|
|
6
7
|
key: "tree",
|
|
@@ -31,16 +32,21 @@ export const treeFeature: FeatureImplementation<any> = {
|
|
|
31
32
|
|
|
32
33
|
const recursiveAdd = (
|
|
33
34
|
itemId: string,
|
|
34
|
-
|
|
35
|
+
path: string[],
|
|
35
36
|
level: number,
|
|
36
37
|
setSize: number,
|
|
37
38
|
posInSet: number,
|
|
38
39
|
) => {
|
|
40
|
+
if (path.includes(itemId)) {
|
|
41
|
+
logWarning(`Circular reference for ${path.join(".")}`);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
39
45
|
flatItems.push({
|
|
40
46
|
itemId,
|
|
41
47
|
level,
|
|
42
48
|
index: flatItems.length,
|
|
43
|
-
parentId,
|
|
49
|
+
parentId: path.at(-1) as string,
|
|
44
50
|
setSize,
|
|
45
51
|
posInSet,
|
|
46
52
|
});
|
|
@@ -49,7 +55,13 @@ export const treeFeature: FeatureImplementation<any> = {
|
|
|
49
55
|
const children = tree.retrieveChildrenIds(itemId) ?? [];
|
|
50
56
|
let i = 0;
|
|
51
57
|
for (const childId of children) {
|
|
52
|
-
recursiveAdd(
|
|
58
|
+
recursiveAdd(
|
|
59
|
+
childId,
|
|
60
|
+
path.concat(itemId),
|
|
61
|
+
level + 1,
|
|
62
|
+
children.length,
|
|
63
|
+
i++,
|
|
64
|
+
);
|
|
53
65
|
}
|
|
54
66
|
}
|
|
55
67
|
};
|
|
@@ -57,7 +69,7 @@ export const treeFeature: FeatureImplementation<any> = {
|
|
|
57
69
|
const children = tree.retrieveChildrenIds(rootItemId);
|
|
58
70
|
let i = 0;
|
|
59
71
|
for (const itemId of children) {
|
|
60
|
-
recursiveAdd(itemId, rootItemId, 0, children.length, i++);
|
|
72
|
+
recursiveAdd(itemId, [rootItemId], 0, children.length, i++);
|
|
61
73
|
}
|
|
62
74
|
|
|
63
75
|
return flatItems;
|
|
@@ -71,14 +83,16 @@ export const treeFeature: FeatureImplementation<any> = {
|
|
|
71
83
|
},
|
|
72
84
|
|
|
73
85
|
focusNextItem: ({ tree }) => {
|
|
74
|
-
const
|
|
75
|
-
|
|
86
|
+
const focused = tree.getFocusedItem().getItemMeta();
|
|
87
|
+
if (!focused) return;
|
|
88
|
+
const nextIndex = Math.min(focused.index + 1, tree.getItems().length - 1);
|
|
76
89
|
tree.getItems()[nextIndex]?.setFocused();
|
|
77
90
|
},
|
|
78
91
|
|
|
79
92
|
focusPreviousItem: ({ tree }) => {
|
|
80
|
-
const
|
|
81
|
-
|
|
93
|
+
const focused = tree.getFocusedItem().getItemMeta();
|
|
94
|
+
if (!focused) return;
|
|
95
|
+
const nextIndex = Math.max(focused.index - 1, 0);
|
|
82
96
|
tree.getItems()[nextIndex]?.setFocused();
|
|
83
97
|
},
|
|
84
98
|
|
package/src/utilities/errors.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const prefix = "Headless Tree: ";
|
|
2
|
+
|
|
3
|
+
export const throwError = (message: string) => Error(prefix + message);
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line no-console
|
|
6
|
+
export const logWarning = (message: string) => console.warn(prefix + message);
|