@headless-tree/core 0.0.0-20250224211405 → 0.0.0-20250322153940
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 +7 -1
- package/lib/cjs/core/create-tree.js +13 -4
- package/lib/cjs/features/async-data-loader/feature.js +73 -48
- package/lib/cjs/features/async-data-loader/types.d.ts +17 -14
- package/lib/cjs/features/drag-and-drop/feature.js +89 -84
- package/lib/cjs/features/drag-and-drop/types.d.ts +8 -20
- package/lib/cjs/features/drag-and-drop/utils.js +8 -21
- package/lib/cjs/features/expand-all/feature.js +26 -3
- package/lib/cjs/features/expand-all/types.d.ts +3 -1
- package/lib/cjs/features/hotkeys-core/feature.js +6 -3
- package/lib/cjs/features/hotkeys-core/types.d.ts +4 -5
- package/lib/cjs/features/prop-memoization/feature.js +2 -2
- package/lib/cjs/features/prop-memoization/types.d.ts +2 -2
- package/lib/cjs/features/renaming/feature.js +1 -1
- package/lib/cjs/features/search/feature.js +2 -0
- package/lib/cjs/features/search/types.d.ts +2 -2
- package/lib/cjs/features/selection/feature.js +4 -4
- package/lib/cjs/features/selection/types.d.ts +1 -1
- package/lib/cjs/features/sync-data-loader/feature.js +31 -5
- package/lib/cjs/features/sync-data-loader/types.d.ts +5 -5
- package/lib/cjs/features/tree/feature.js +4 -7
- package/lib/cjs/features/tree/types.d.ts +7 -5
- package/lib/cjs/test-utils/test-tree-do.d.ts +2 -2
- package/lib/cjs/test-utils/test-tree-do.js +19 -6
- package/lib/cjs/test-utils/test-tree.d.ts +2 -1
- package/lib/cjs/test-utils/test-tree.js +24 -21
- package/lib/cjs/utilities/create-on-drop-handler.d.ts +1 -1
- package/lib/cjs/utilities/create-on-drop-handler.js +13 -4
- package/lib/cjs/utilities/insert-items-at-target.d.ts +1 -1
- package/lib/cjs/utilities/insert-items-at-target.js +21 -12
- package/lib/cjs/utilities/remove-items-from-parents.d.ts +1 -1
- package/lib/cjs/utilities/remove-items-from-parents.js +12 -3
- package/lib/esm/core/create-tree.js +13 -4
- package/lib/esm/features/async-data-loader/feature.js +73 -48
- package/lib/esm/features/async-data-loader/types.d.ts +17 -14
- package/lib/esm/features/drag-and-drop/feature.js +89 -84
- package/lib/esm/features/drag-and-drop/types.d.ts +8 -20
- package/lib/esm/features/drag-and-drop/utils.js +8 -21
- package/lib/esm/features/expand-all/feature.js +26 -3
- package/lib/esm/features/expand-all/types.d.ts +3 -1
- package/lib/esm/features/hotkeys-core/feature.js +6 -3
- package/lib/esm/features/hotkeys-core/types.d.ts +4 -5
- package/lib/esm/features/prop-memoization/feature.js +2 -2
- package/lib/esm/features/prop-memoization/types.d.ts +2 -2
- package/lib/esm/features/renaming/feature.js +1 -1
- package/lib/esm/features/search/feature.js +2 -0
- package/lib/esm/features/search/types.d.ts +2 -2
- package/lib/esm/features/selection/feature.js +4 -4
- package/lib/esm/features/selection/types.d.ts +1 -1
- package/lib/esm/features/sync-data-loader/feature.js +31 -5
- package/lib/esm/features/sync-data-loader/types.d.ts +5 -5
- package/lib/esm/features/tree/feature.js +4 -7
- package/lib/esm/features/tree/types.d.ts +7 -5
- package/lib/esm/test-utils/test-tree-do.d.ts +2 -2
- package/lib/esm/test-utils/test-tree-do.js +19 -6
- package/lib/esm/test-utils/test-tree.d.ts +2 -1
- package/lib/esm/test-utils/test-tree.js +24 -21
- package/lib/esm/utilities/create-on-drop-handler.d.ts +1 -1
- package/lib/esm/utilities/create-on-drop-handler.js +13 -4
- package/lib/esm/utilities/insert-items-at-target.d.ts +1 -1
- package/lib/esm/utilities/insert-items-at-target.js +21 -12
- package/lib/esm/utilities/remove-items-from-parents.d.ts +1 -1
- package/lib/esm/utilities/remove-items-from-parents.js +12 -3
- package/package.json +2 -2
- package/src/core/core.spec.ts +31 -0
- package/src/core/create-tree.ts +15 -5
- package/src/features/async-data-loader/async-data-loader.spec.ts +10 -6
- package/src/features/async-data-loader/feature.ts +76 -48
- package/src/features/async-data-loader/types.ts +18 -11
- package/src/features/drag-and-drop/drag-and-drop.spec.ts +69 -83
- package/src/features/drag-and-drop/feature.ts +9 -10
- package/src/features/drag-and-drop/types.ts +15 -27
- package/src/features/drag-and-drop/utils.ts +7 -20
- package/src/features/expand-all/feature.ts +29 -5
- package/src/features/expand-all/types.ts +3 -1
- package/src/features/hotkeys-core/feature.ts +3 -0
- package/src/features/hotkeys-core/types.ts +4 -13
- package/src/features/prop-memoization/feature.ts +2 -2
- package/src/features/prop-memoization/prop-memoization.spec.ts +2 -2
- package/src/features/prop-memoization/types.ts +2 -2
- package/src/features/renaming/feature.ts +8 -2
- package/src/features/search/feature.ts +2 -0
- package/src/features/search/types.ts +2 -2
- package/src/features/selection/feature.ts +4 -4
- package/src/features/selection/types.ts +1 -1
- package/src/features/sync-data-loader/feature.ts +26 -7
- package/src/features/sync-data-loader/types.ts +5 -5
- package/src/features/tree/feature.ts +8 -11
- package/src/features/tree/types.ts +7 -5
- package/src/test-utils/test-tree-do.ts +3 -3
- package/src/test-utils/test-tree.ts +26 -22
- package/src/utilities/create-on-drop-handler.ts +3 -3
- package/src/utilities/insert-items-at-target.ts +16 -12
- package/src/utilities/remove-items-from-parents.ts +6 -3
|
@@ -7,7 +7,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { poll } from "../../utils";
|
|
11
10
|
export const expandAllFeature = {
|
|
12
11
|
key: "expand-all",
|
|
13
12
|
treeInstance: {
|
|
@@ -28,17 +27,41 @@ export const expandAllFeature = {
|
|
|
28
27
|
return;
|
|
29
28
|
}
|
|
30
29
|
item.expand();
|
|
31
|
-
yield
|
|
30
|
+
yield tree.waitForItemChildrenLoaded(item.getId());
|
|
32
31
|
yield Promise.all(item.getChildren().map((child) => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
|
-
yield
|
|
32
|
+
yield tree.waitForItemChildrenLoaded(item.getId());
|
|
34
33
|
yield (child === null || child === void 0 ? void 0 : child.expandAll(cancelToken));
|
|
35
34
|
})));
|
|
36
35
|
}),
|
|
37
36
|
collapseAll: ({ item }) => {
|
|
37
|
+
if (!item.isExpanded())
|
|
38
|
+
return;
|
|
38
39
|
for (const child of item.getChildren()) {
|
|
39
40
|
child === null || child === void 0 ? void 0 : child.collapseAll();
|
|
40
41
|
}
|
|
41
42
|
item.collapse();
|
|
42
43
|
},
|
|
43
44
|
},
|
|
45
|
+
hotkeys: {
|
|
46
|
+
expandSelected: {
|
|
47
|
+
hotkey: "Control+Shift+Plus",
|
|
48
|
+
handler: (_, tree) => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
+
const cancelToken = { current: false };
|
|
50
|
+
const cancelHandler = (e) => {
|
|
51
|
+
if (e.key === "Escape") {
|
|
52
|
+
cancelToken.current = true;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
document.addEventListener("keydown", cancelHandler);
|
|
56
|
+
yield Promise.all(tree.getSelectedItems().map((item) => item.expandAll(cancelToken)));
|
|
57
|
+
document.removeEventListener("keydown", cancelHandler);
|
|
58
|
+
}),
|
|
59
|
+
},
|
|
60
|
+
collapseSelected: {
|
|
61
|
+
hotkey: "Control+Shift+-",
|
|
62
|
+
handler: (_, tree) => {
|
|
63
|
+
tree.getSelectedItems().forEach((item) => item.collapseAll());
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
},
|
|
44
67
|
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export interface ExpandAllDataRef {
|
|
2
|
+
}
|
|
1
3
|
export type ExpandAllFeatureDef = {
|
|
2
4
|
state: {};
|
|
3
5
|
config: {};
|
|
@@ -13,5 +15,5 @@ export type ExpandAllFeatureDef = {
|
|
|
13
15
|
}) => Promise<void>;
|
|
14
16
|
collapseAll: () => void;
|
|
15
17
|
};
|
|
16
|
-
hotkeys:
|
|
18
|
+
hotkeys: "expandSelected" | "collapseSelected";
|
|
17
19
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const specialKeys = {
|
|
2
2
|
Letter: /^[a-z]$/,
|
|
3
3
|
LetterOrNumber: /^[a-z0-9]$/,
|
|
4
|
+
Plus: /^\+$/,
|
|
5
|
+
Space: /^ $/,
|
|
4
6
|
};
|
|
5
7
|
const testHotkeyMatch = (pressedKeys, tree, hotkey) => {
|
|
6
8
|
const supposedKeys = hotkey.hotkey.split("+");
|
|
@@ -20,9 +22,9 @@ export const hotkeysCoreFeature = {
|
|
|
20
22
|
onTreeMount: (tree, element) => {
|
|
21
23
|
const data = tree.getDataRef();
|
|
22
24
|
const keydown = (e) => {
|
|
23
|
-
var _a, _b;
|
|
24
|
-
var
|
|
25
|
-
(_a = (
|
|
25
|
+
var _a, _b, _c, _d;
|
|
26
|
+
var _e;
|
|
27
|
+
(_a = (_e = data.current).pressedKeys) !== null && _a !== void 0 ? _a : (_e.pressedKeys = new Set());
|
|
26
28
|
const newMatch = !data.current.pressedKeys.has(e.key);
|
|
27
29
|
data.current.pressedKeys.add(e.key);
|
|
28
30
|
const hotkeyName = findHotkeyMatch(data.current.pressedKeys, tree, tree.getHotkeyPresets(), tree.getConfig().hotkeys);
|
|
@@ -39,6 +41,7 @@ export const hotkeysCoreFeature = {
|
|
|
39
41
|
if (hotkeyConfig.preventDefault)
|
|
40
42
|
e.preventDefault();
|
|
41
43
|
hotkeyConfig.handler(e, tree);
|
|
44
|
+
(_d = (_c = tree.getConfig()).onTreeHotkey) === null || _d === void 0 ? void 0 : _d.call(_c, hotkeyName, e);
|
|
42
45
|
};
|
|
43
46
|
const keyup = (e) => {
|
|
44
47
|
var _a;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CustomHotkeysConfig,
|
|
1
|
+
import { CustomHotkeysConfig, TreeInstance } from "../../types/core";
|
|
2
2
|
export interface HotkeyConfig<T> {
|
|
3
3
|
hotkey: string;
|
|
4
4
|
canRepeat?: boolean;
|
|
@@ -7,17 +7,16 @@ export interface HotkeyConfig<T> {
|
|
|
7
7
|
preventDefault?: boolean;
|
|
8
8
|
handler: (e: KeyboardEvent, tree: TreeInstance<T>) => void;
|
|
9
9
|
}
|
|
10
|
-
export
|
|
10
|
+
export interface HotkeysCoreDataRef {
|
|
11
11
|
keydownHandler?: (e: KeyboardEvent) => void;
|
|
12
12
|
keyupHandler?: (e: KeyboardEvent) => void;
|
|
13
13
|
pressedKeys: Set<string>;
|
|
14
|
-
}
|
|
14
|
+
}
|
|
15
15
|
export type HotkeysCoreFeatureDef<T> = {
|
|
16
16
|
state: {};
|
|
17
17
|
config: {
|
|
18
18
|
hotkeys?: CustomHotkeysConfig<T>;
|
|
19
|
-
onTreeHotkey?: (name: string,
|
|
20
|
-
onItemHotkey?: (name: string, item: ItemInstance<T>, element: HTMLElement) => void;
|
|
19
|
+
onTreeHotkey?: (name: string, e: KeyboardEvent) => void;
|
|
21
20
|
};
|
|
22
21
|
treeInstance: {};
|
|
23
22
|
itemInstance: {};
|
|
@@ -27,10 +27,10 @@ export const propMemoizationFeature = {
|
|
|
27
27
|
"selection",
|
|
28
28
|
],
|
|
29
29
|
treeInstance: {
|
|
30
|
-
getContainerProps: ({ tree, prev }) => {
|
|
30
|
+
getContainerProps: ({ tree, prev }, treeLabel) => {
|
|
31
31
|
var _a;
|
|
32
32
|
const dataRef = tree.getDataRef();
|
|
33
|
-
const props = (_a = prev === null || prev === void 0 ? void 0 : prev()) !== null && _a !== void 0 ? _a : {};
|
|
33
|
+
const props = (_a = prev === null || prev === void 0 ? void 0 : prev(treeLabel)) !== null && _a !== void 0 ? _a : {};
|
|
34
34
|
return memoize(props, dataRef.current);
|
|
35
35
|
},
|
|
36
36
|
},
|
|
@@ -37,11 +37,11 @@ export const renamingFeature = {
|
|
|
37
37
|
tree.applySubStateUpdate("renamingValue", item.getItemName());
|
|
38
38
|
},
|
|
39
39
|
getRenameInputProps: ({ tree }) => ({
|
|
40
|
+
ref: (r) => r === null || r === void 0 ? void 0 : r.focus(),
|
|
40
41
|
onBlur: () => tree.abortRenaming(),
|
|
41
42
|
value: tree.getRenamingValue(),
|
|
42
43
|
onChange: (e) => {
|
|
43
44
|
var _a;
|
|
44
|
-
// TODO custom type with e.target.value
|
|
45
45
|
tree.applySubStateUpdate("renamingValue", (_a = e.target) === null || _a === void 0 ? void 0 : _a.value);
|
|
46
46
|
},
|
|
47
47
|
}),
|
|
@@ -40,10 +40,12 @@ export const searchFeature = {
|
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
42
|
getSearchInputElement: ({ tree }) => { var _a; return (_a = tree.getDataRef().current.searchInput) !== null && _a !== void 0 ? _a : null; },
|
|
43
|
+
// TODO memoize with propMemoizationFeature
|
|
43
44
|
getSearchInputElementProps: ({ tree }) => ({
|
|
44
45
|
value: tree.getSearchValue(),
|
|
45
46
|
onChange: (e) => tree.setSearch(e.target.value),
|
|
46
47
|
onBlur: () => tree.closeSearch(),
|
|
48
|
+
ref: tree.registerSearchInputElement,
|
|
47
49
|
}),
|
|
48
50
|
getSearchMatchingItems: memo(({ tree }) => [
|
|
49
51
|
tree.getSearchValue(),
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ItemInstance, SetStateFn } from "../../types/core";
|
|
2
2
|
import { HotkeysCoreDataRef } from "../hotkeys-core/types";
|
|
3
|
-
export
|
|
3
|
+
export interface SearchFeatureDataRef<T = any> extends HotkeysCoreDataRef {
|
|
4
4
|
matchingItems: ItemInstance<T>[];
|
|
5
5
|
searchInput: HTMLInputElement | null;
|
|
6
|
-
}
|
|
6
|
+
}
|
|
7
7
|
export type SearchFeatureDef<T> = {
|
|
8
8
|
state: {
|
|
9
9
|
search: string | null;
|
|
@@ -10,7 +10,6 @@ export const selectionFeature = {
|
|
|
10
10
|
setSelectedItems: ({ tree }, selectedItems) => {
|
|
11
11
|
tree.applySubStateUpdate("selectedItems", selectedItems);
|
|
12
12
|
},
|
|
13
|
-
// TODO memo
|
|
14
13
|
getSelectedItems: ({ tree }) => {
|
|
15
14
|
return tree.getState().selectedItems.map(tree.getItemInstance);
|
|
16
15
|
},
|
|
@@ -78,9 +77,10 @@ export const selectionFeature = {
|
|
|
78
77
|
// tree.setSelectedItems([tree.getFocusedItem().getId()]);
|
|
79
78
|
// },
|
|
80
79
|
// },
|
|
81
|
-
|
|
82
|
-
hotkey: "
|
|
83
|
-
|
|
80
|
+
toggleSelectedItem: {
|
|
81
|
+
hotkey: "Control+Space",
|
|
82
|
+
preventDefault: true,
|
|
83
|
+
handler: (_, tree) => {
|
|
84
84
|
tree.getFocusedItem().toggleSelect();
|
|
85
85
|
},
|
|
86
86
|
},
|
|
@@ -17,5 +17,5 @@ export type SelectionFeatureDef<T> = {
|
|
|
17
17
|
isSelected: () => boolean;
|
|
18
18
|
selectUpTo: (ctrl: boolean) => void;
|
|
19
19
|
};
|
|
20
|
-
hotkeys: "
|
|
20
|
+
hotkeys: "toggleSelectedItem" | "selectUpwards" | "selectDownwards" | "selectAll";
|
|
21
21
|
};
|
|
@@ -1,14 +1,40 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
1
10
|
import { makeStateUpdater } from "../../utils";
|
|
11
|
+
import { throwError } from "../../utilities/errors";
|
|
12
|
+
const promiseErrorMessage = "sync dataLoader returned promise";
|
|
2
13
|
export const syncDataLoaderFeature = {
|
|
3
14
|
key: "sync-data-loader",
|
|
4
|
-
getInitialState: (initialState) => (Object.assign({
|
|
5
|
-
getDefaultConfig: (defaultConfig, tree) => (Object.assign({
|
|
15
|
+
getInitialState: (initialState) => (Object.assign({ loadingItemData: [], loadingItemChildrens: [] }, initialState)),
|
|
16
|
+
getDefaultConfig: (defaultConfig, tree) => (Object.assign({ setLoadingItemData: makeStateUpdater("loadingItemData", tree), setLoadingItemChildrens: makeStateUpdater("loadingItemChildrens", tree) }, defaultConfig)),
|
|
6
17
|
stateHandlerNames: {
|
|
7
|
-
|
|
18
|
+
loadingItemData: "setLoadingItemData",
|
|
19
|
+
loadingItemChildrens: "setLoadingItemChildrens",
|
|
8
20
|
},
|
|
9
21
|
treeInstance: {
|
|
10
|
-
|
|
11
|
-
|
|
22
|
+
waitForItemDataLoaded: () => __awaiter(void 0, void 0, void 0, function* () { }),
|
|
23
|
+
waitForItemChildrenLoaded: () => __awaiter(void 0, void 0, void 0, function* () { }),
|
|
24
|
+
retrieveItemData: ({ tree }, itemId) => {
|
|
25
|
+
const data = tree.getConfig().dataLoader.getItem(itemId);
|
|
26
|
+
if (typeof data === "object" && "then" in data) {
|
|
27
|
+
throw throwError(promiseErrorMessage);
|
|
28
|
+
}
|
|
29
|
+
return data;
|
|
30
|
+
},
|
|
31
|
+
retrieveChildrenIds: ({ tree }, itemId) => {
|
|
32
|
+
const data = tree.getConfig().dataLoader.getChildren(itemId);
|
|
33
|
+
if (typeof data === "object" && "then" in data) {
|
|
34
|
+
throw throwError(promiseErrorMessage);
|
|
35
|
+
}
|
|
36
|
+
return data;
|
|
37
|
+
},
|
|
12
38
|
},
|
|
13
39
|
itemInstance: {
|
|
14
40
|
isLoading: () => false,
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export
|
|
2
|
-
getItem: (itemId: string) => T
|
|
3
|
-
getChildren: (itemId: string) => string[]
|
|
4
|
-
}
|
|
1
|
+
export interface TreeDataLoader<T> {
|
|
2
|
+
getItem: (itemId: string) => T | Promise<T>;
|
|
3
|
+
getChildren: (itemId: string) => string[] | Promise<string[]>;
|
|
4
|
+
}
|
|
5
5
|
export type SyncDataLoaderFeatureDef<T> = {
|
|
6
6
|
state: {};
|
|
7
7
|
config: {
|
|
8
8
|
rootItemId: string;
|
|
9
|
-
dataLoader
|
|
9
|
+
dataLoader: TreeDataLoader<T>;
|
|
10
10
|
};
|
|
11
11
|
treeInstance: {
|
|
12
12
|
retrieveItemData: (itemId: string) => T;
|
|
@@ -21,7 +21,7 @@ export const treeFeature = {
|
|
|
21
21
|
const { rootItemId } = tree.getConfig();
|
|
22
22
|
const { expandedItems } = tree.getState();
|
|
23
23
|
const flatItems = [];
|
|
24
|
-
const expandedItemsSet = new Set(expandedItems);
|
|
24
|
+
const expandedItemsSet = new Set(expandedItems); // TODO support setting state expandedItems as set instead of array
|
|
25
25
|
const recursiveAdd = (itemId, parentId, level, setSize, posInSet) => {
|
|
26
26
|
var _a;
|
|
27
27
|
flatItems.push({
|
|
@@ -33,7 +33,6 @@ export const treeFeature = {
|
|
|
33
33
|
posInSet,
|
|
34
34
|
});
|
|
35
35
|
if (expandedItemsSet.has(itemId)) {
|
|
36
|
-
// TODO THIS MADE A HUGE DIFFERENCE!
|
|
37
36
|
const children = (_a = tree.retrieveChildrenIds(itemId)) !== null && _a !== void 0 ? _a : [];
|
|
38
37
|
let i = 0;
|
|
39
38
|
for (const childId of children) {
|
|
@@ -79,7 +78,7 @@ export const treeFeature = {
|
|
|
79
78
|
}));
|
|
80
79
|
},
|
|
81
80
|
// TODO add label parameter
|
|
82
|
-
getContainerProps: ({ prev, tree }) => (Object.assign(Object.assign({}, prev === null || prev === void 0 ? void 0 : prev()), { role: "tree", "aria-label": "", ref: tree.registerElement })),
|
|
81
|
+
getContainerProps: ({ prev, tree }, treeLabel) => (Object.assign(Object.assign({}, prev === null || prev === void 0 ? void 0 : prev()), { role: "tree", "aria-label": treeLabel !== null && treeLabel !== void 0 ? treeLabel : "", ref: tree.registerElement })),
|
|
83
82
|
// relevant for hotkeys of this feature
|
|
84
83
|
isSearchOpen: () => false,
|
|
85
84
|
},
|
|
@@ -115,7 +114,7 @@ export const treeFeature = {
|
|
|
115
114
|
if (!item.isFolder()) {
|
|
116
115
|
return;
|
|
117
116
|
}
|
|
118
|
-
if ((_a = tree.getState().
|
|
117
|
+
if ((_a = tree.getState().loadingItemChildrens) === null || _a === void 0 ? void 0 : _a.includes(itemId)) {
|
|
119
118
|
return;
|
|
120
119
|
}
|
|
121
120
|
tree.applySubStateUpdate("expandedItems", (expandedItems) => [
|
|
@@ -154,9 +153,7 @@ export const treeFeature = {
|
|
|
154
153
|
? tree.getItemInstance(item.getItemMeta().parentId)
|
|
155
154
|
: undefined,
|
|
156
155
|
getIndexInParent: ({ item }) => item.getItemMeta().posInSet,
|
|
157
|
-
getChildren: ({ tree,
|
|
158
|
-
.retrieveChildrenIds(item.getItemMeta().itemId)
|
|
159
|
-
.map((id) => tree.getItemInstance(id)),
|
|
156
|
+
getChildren: ({ tree, itemId }) => tree.retrieveChildrenIds(itemId).map((id) => tree.getItemInstance(id)),
|
|
160
157
|
getTree: ({ tree }) => tree,
|
|
161
158
|
getItemAbove: ({ tree, item }) => tree.getItems()[item.getItemMeta().index - 1],
|
|
162
159
|
getItemBelow: ({ tree, item }) => tree.getItems()[item.getItemMeta().index + 1],
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { ItemInstance, SetStateFn, TreeInstance } from "../../types/core";
|
|
2
|
-
export
|
|
2
|
+
export interface ItemMeta {
|
|
3
3
|
itemId: string;
|
|
4
4
|
parentId: string;
|
|
5
5
|
level: number;
|
|
6
6
|
index: number;
|
|
7
7
|
setSize: number;
|
|
8
8
|
posInSet: number;
|
|
9
|
-
}
|
|
10
|
-
export
|
|
9
|
+
}
|
|
10
|
+
export interface TreeItemDataRef {
|
|
11
11
|
memoizedValues: Record<string, any>;
|
|
12
12
|
memoizedDeps: Record<string, any[] | undefined>;
|
|
13
|
-
}
|
|
13
|
+
}
|
|
14
14
|
export type TreeFeatureDef<T> = {
|
|
15
15
|
state: {
|
|
16
16
|
expandedItems: string[];
|
|
@@ -31,7 +31,9 @@ export type TreeFeatureDef<T> = {
|
|
|
31
31
|
focusNextItem: () => void;
|
|
32
32
|
focusPreviousItem: () => void;
|
|
33
33
|
updateDomFocus: () => void;
|
|
34
|
-
|
|
34
|
+
/** Pass to the container rendering the tree children. The `treeLabel` parameter
|
|
35
|
+
* will be passed as `aria-label` parameter, and is recommended to be set. */
|
|
36
|
+
getContainerProps: (treeLabel?: string) => Record<string, any>;
|
|
35
37
|
};
|
|
36
38
|
itemInstance: {
|
|
37
39
|
getId: () => string;
|
|
@@ -17,7 +17,7 @@ export declare class TestTreeDo<T> {
|
|
|
17
17
|
dragOverNotAllowed(itemId: string, event?: DragEvent): DragEvent<Element>;
|
|
18
18
|
dragLeave(itemId: string): void;
|
|
19
19
|
dragEnd(itemId: string, event?: DragEvent): DragEvent<Element>;
|
|
20
|
-
drop(itemId: string, event?: DragEvent): DragEvent<Element
|
|
21
|
-
dragOverAndDrop(itemId: string, event?: DragEvent): DragEvent<Element
|
|
20
|
+
drop(itemId: string, event?: DragEvent): Promise<DragEvent<Element>>;
|
|
21
|
+
dragOverAndDrop(itemId: string, event?: DragEvent): Promise<DragEvent<Element>>;
|
|
22
22
|
private consistentCalls;
|
|
23
23
|
}
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
1
10
|
import { expect, vi } from "vitest";
|
|
2
11
|
import { TestTree } from "./test-tree";
|
|
3
12
|
export class TestTreeDo {
|
|
@@ -76,14 +85,18 @@ export class TestTreeDo {
|
|
|
76
85
|
return e;
|
|
77
86
|
}
|
|
78
87
|
drop(itemId, event) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
const e = event !== null && event !== void 0 ? event : TestTree.dragEvent();
|
|
90
|
+
yield this.itemProps(itemId).onDrop(e);
|
|
91
|
+
return e;
|
|
92
|
+
});
|
|
82
93
|
}
|
|
83
94
|
dragOverAndDrop(itemId, event) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
const e = event !== null && event !== void 0 ? event : TestTree.dragEvent();
|
|
97
|
+
this.dragOver(itemId, e);
|
|
98
|
+
return this.drop(itemId, e);
|
|
99
|
+
});
|
|
87
100
|
}
|
|
88
101
|
consistentCalls(fn) {
|
|
89
102
|
if (!vi.isMockFunction(fn)) {
|
|
@@ -8,6 +8,7 @@ export declare class TestTree<T = string> {
|
|
|
8
8
|
readonly expect: TestTreeExpect<T>;
|
|
9
9
|
private treeInstance;
|
|
10
10
|
private static asyncLoaderResolvers;
|
|
11
|
+
private asyncDataLoaderImp;
|
|
11
12
|
suits: {
|
|
12
13
|
sync: () => {
|
|
13
14
|
tree: TestTree<T>;
|
|
@@ -41,7 +42,7 @@ export declare class TestTree<T = string> {
|
|
|
41
42
|
reset(): void;
|
|
42
43
|
debug(): void;
|
|
43
44
|
setElementBoundingBox(itemId: string, bb?: Partial<DOMRect>): void;
|
|
44
|
-
static dragEvent(
|
|
45
|
+
static dragEvent(clientX?: number, clientY?: number): DragEvent;
|
|
45
46
|
createTopDragEvent(indent?: number): DragEvent<Element>;
|
|
46
47
|
createBottomDragEvent(indent?: number): DragEvent<Element>;
|
|
47
48
|
}
|
|
@@ -39,13 +39,31 @@ export class TestTree {
|
|
|
39
39
|
this.do = new TestTreeDo(this);
|
|
40
40
|
this.expect = new TestTreeExpect(this);
|
|
41
41
|
this.treeInstance = null;
|
|
42
|
+
this.asyncDataLoaderImp = {
|
|
43
|
+
getItem: (id) => __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
yield new Promise((r) => {
|
|
45
|
+
r.debugName = `Loading getItem ${id}`;
|
|
46
|
+
TestTree.asyncLoaderResolvers.push(r);
|
|
47
|
+
});
|
|
48
|
+
return id;
|
|
49
|
+
}),
|
|
50
|
+
getChildren: (id) => __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
yield new Promise((r) => {
|
|
52
|
+
r.debugName = `Loading getChildren ${id}`;
|
|
53
|
+
TestTree.asyncLoaderResolvers.push(r);
|
|
54
|
+
});
|
|
55
|
+
return [`${id}1`, `${id}2`, `${id}3`, `${id}4`];
|
|
56
|
+
}),
|
|
57
|
+
};
|
|
42
58
|
this.suits = {
|
|
43
59
|
sync: () => ({
|
|
44
60
|
tree: this.withFeatures(syncDataLoaderFeature),
|
|
45
61
|
title: "Synchronous Data Loader",
|
|
46
62
|
}),
|
|
47
63
|
async: () => ({
|
|
48
|
-
tree: this.withFeatures(asyncDataLoaderFeature)
|
|
64
|
+
tree: this.withFeatures(asyncDataLoaderFeature).with({
|
|
65
|
+
dataLoader: this.asyncDataLoaderImp,
|
|
66
|
+
}),
|
|
49
67
|
title: "Asynchronous Data Loader",
|
|
50
68
|
}),
|
|
51
69
|
proxifiedSync: () => ({
|
|
@@ -65,12 +83,12 @@ export class TestTree {
|
|
|
65
83
|
static resolveAsyncLoaders() {
|
|
66
84
|
return __awaiter(this, void 0, void 0, function* () {
|
|
67
85
|
var _a;
|
|
68
|
-
|
|
86
|
+
do {
|
|
69
87
|
(_a = TestTree.asyncLoaderResolvers.shift()) === null || _a === void 0 ? void 0 : _a();
|
|
70
88
|
yield new Promise((r) => {
|
|
71
89
|
setTimeout(r);
|
|
72
90
|
});
|
|
73
|
-
}
|
|
91
|
+
} while (TestTree.asyncLoaderResolvers.length);
|
|
74
92
|
});
|
|
75
93
|
}
|
|
76
94
|
resolveAsyncVisibleItems() {
|
|
@@ -85,21 +103,6 @@ export class TestTree {
|
|
|
85
103
|
return new TestTree(Object.assign({ rootItemId: "x", createLoadingItemData: () => "loading", dataLoader: {
|
|
86
104
|
getItem: (id) => id,
|
|
87
105
|
getChildren: (id) => [`${id}1`, `${id}2`, `${id}3`, `${id}4`],
|
|
88
|
-
}, asyncDataLoader: {
|
|
89
|
-
getItem: (id) => __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
yield new Promise((r) => {
|
|
91
|
-
r.debugName = `Loading getItem ${id}`;
|
|
92
|
-
TestTree.asyncLoaderResolvers.push(r);
|
|
93
|
-
});
|
|
94
|
-
return id;
|
|
95
|
-
}),
|
|
96
|
-
getChildren: (id) => __awaiter(this, void 0, void 0, function* () {
|
|
97
|
-
yield new Promise((r) => {
|
|
98
|
-
r.debugName = `Loading getChildren ${id}`;
|
|
99
|
-
TestTree.asyncLoaderResolvers.push(r);
|
|
100
|
-
});
|
|
101
|
-
return [`${id}1`, `${id}2`, `${id}3`, `${id}4`];
|
|
102
|
-
}),
|
|
103
106
|
}, getItemName: (item) => item.getItemData(), indent: 20, isItemFolder: (item) => item.getItemMeta().level < 2, initialState: {
|
|
104
107
|
expandedItems: ["x1", "x11"],
|
|
105
108
|
}, features: [] }, config));
|
|
@@ -176,7 +179,7 @@ export class TestTree {
|
|
|
176
179
|
getBoundingClientRect: () => bb,
|
|
177
180
|
});
|
|
178
181
|
}
|
|
179
|
-
static dragEvent(
|
|
182
|
+
static dragEvent(clientX = 1000, clientY = 0) {
|
|
180
183
|
return {
|
|
181
184
|
preventDefault: vi.fn(),
|
|
182
185
|
stopPropagation: vi.fn(),
|
|
@@ -185,8 +188,8 @@ export class TestTree {
|
|
|
185
188
|
getData: vi.fn(),
|
|
186
189
|
dropEffect: "unchaged-from-test",
|
|
187
190
|
},
|
|
188
|
-
|
|
189
|
-
|
|
191
|
+
clientX,
|
|
192
|
+
clientY,
|
|
190
193
|
};
|
|
191
194
|
}
|
|
192
195
|
createTopDragEvent(indent = 0) {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { ItemInstance } from "../types/core";
|
|
2
2
|
import { DropTarget } from "../features/drag-and-drop/types";
|
|
3
|
-
export declare const createOnDropHandler: <T>(onChangeChildren: (item: ItemInstance<T>, newChildren: string[]) => void) => (items: ItemInstance<T>[], target: DropTarget<T>) => void
|
|
3
|
+
export declare const createOnDropHandler: <T>(onChangeChildren: (item: ItemInstance<T>, newChildren: string[]) => void) => (items: ItemInstance<T>[], target: DropTarget<T>) => Promise<void>;
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
1
10
|
import { removeItemsFromParents } from "./remove-items-from-parents";
|
|
2
11
|
import { insertItemsAtTarget } from "./insert-items-at-target";
|
|
3
|
-
export const createOnDropHandler = (onChangeChildren) => (items, target) => {
|
|
12
|
+
export const createOnDropHandler = (onChangeChildren) => (items, target) => __awaiter(void 0, void 0, void 0, function* () {
|
|
4
13
|
const itemIds = items.map((item) => item.getId());
|
|
5
|
-
removeItemsFromParents(items, onChangeChildren);
|
|
6
|
-
insertItemsAtTarget(itemIds, target, onChangeChildren);
|
|
7
|
-
};
|
|
14
|
+
yield removeItemsFromParents(items, onChangeChildren);
|
|
15
|
+
yield insertItemsAtTarget(itemIds, target, onChangeChildren);
|
|
16
|
+
});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { ItemInstance } from "../types/core";
|
|
2
2
|
import { DropTarget } from "../features/drag-and-drop/types";
|
|
3
|
-
export declare const insertItemsAtTarget: <T>(itemIds: string[], target: DropTarget<T>, onChangeChildren: (item: ItemInstance<T>, newChildrenIds: string[]) => void) => void
|
|
3
|
+
export declare const insertItemsAtTarget: <T>(itemIds: string[], target: DropTarget<T>, onChangeChildren: (item: ItemInstance<T>, newChildrenIds: string[]) => Promise<void> | void) => Promise<void>;
|
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export const insertItemsAtTarget = (itemIds, target, onChangeChildren) => __awaiter(void 0, void 0, void 0, function* () {
|
|
11
|
+
yield target.item.getTree().waitForItemChildrenLoaded(target.item.getId());
|
|
12
|
+
const oldChildrenIds = target.item
|
|
13
|
+
.getTree()
|
|
14
|
+
.retrieveChildrenIds(target.item.getId());
|
|
2
15
|
// add moved items to new common parent, if dropped onto parent
|
|
3
|
-
if (
|
|
4
|
-
const newChildren = [
|
|
5
|
-
|
|
6
|
-
...itemIds,
|
|
7
|
-
];
|
|
8
|
-
onChangeChildren(target.item, newChildren);
|
|
16
|
+
if (!("childIndex" in target)) {
|
|
17
|
+
const newChildren = [...oldChildrenIds, ...itemIds];
|
|
18
|
+
yield onChangeChildren(target.item, newChildren);
|
|
9
19
|
if (target.item && "updateCachedChildrenIds" in target.item) {
|
|
10
20
|
target.item.updateCachedChildrenIds(newChildren);
|
|
11
21
|
}
|
|
@@ -13,15 +23,14 @@ export const insertItemsAtTarget = (itemIds, target, onChangeChildren) => {
|
|
|
13
23
|
return;
|
|
14
24
|
}
|
|
15
25
|
// add moved items to new common parent, if dropped between siblings
|
|
16
|
-
const oldChildren = target.item.getChildren();
|
|
17
26
|
const newChildren = [
|
|
18
|
-
...
|
|
27
|
+
...oldChildrenIds.slice(0, target.insertionIndex),
|
|
19
28
|
...itemIds,
|
|
20
|
-
...
|
|
29
|
+
...oldChildrenIds.slice(target.insertionIndex),
|
|
21
30
|
];
|
|
22
|
-
onChangeChildren(target.item, newChildren);
|
|
31
|
+
yield onChangeChildren(target.item, newChildren);
|
|
23
32
|
if (target.item && "updateCachedChildrenIds" in target.item) {
|
|
24
33
|
target.item.updateCachedChildrenIds(newChildren);
|
|
25
34
|
}
|
|
26
35
|
target.item.getTree().rebuildTree();
|
|
27
|
-
};
|
|
36
|
+
});
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { ItemInstance } from "../types/core";
|
|
2
|
-
export declare const removeItemsFromParents: <T>(movedItems: ItemInstance<T>[], onChangeChildren: (item: ItemInstance<T>, newChildrenIds: string[]) => void) => void
|
|
2
|
+
export declare const removeItemsFromParents: <T>(movedItems: ItemInstance<T>[], onChangeChildren: (item: ItemInstance<T>, newChildrenIds: string[]) => void | Promise<void>) => Promise<void>;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export const removeItemsFromParents = (movedItems, onChangeChildren) => __awaiter(void 0, void 0, void 0, function* () {
|
|
2
11
|
const movedItemsIds = movedItems.map((item) => item.getId());
|
|
3
12
|
const uniqueParents = [
|
|
4
13
|
...new Set(movedItems.map((item) => item.getParent())),
|
|
@@ -9,11 +18,11 @@ export const removeItemsFromParents = (movedItems, onChangeChildren) => {
|
|
|
9
18
|
const newChildren = siblings
|
|
10
19
|
.filter((sibling) => !movedItemsIds.includes(sibling.getId()))
|
|
11
20
|
.map((i) => i.getId());
|
|
12
|
-
onChangeChildren(parent, newChildren);
|
|
21
|
+
yield onChangeChildren(parent, newChildren);
|
|
13
22
|
if (parent && "updateCachedChildrenIds" in parent) {
|
|
14
23
|
parent === null || parent === void 0 ? void 0 : parent.updateCachedChildrenIds(newChildren);
|
|
15
24
|
}
|
|
16
25
|
}
|
|
17
26
|
}
|
|
18
27
|
movedItems[0].getTree().rebuildTree();
|
|
19
|
-
};
|
|
28
|
+
});
|