@headless-tree/core 1.2.1 → 1.3.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 (189) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/index.d.mts +577 -0
  3. package/dist/index.d.ts +577 -0
  4. package/dist/index.js +2321 -0
  5. package/dist/index.mjs +2276 -0
  6. package/package.json +18 -10
  7. package/src/core/create-tree.ts +26 -15
  8. package/src/features/async-data-loader/feature.ts +5 -0
  9. package/src/features/async-data-loader/types.ts +2 -0
  10. package/src/features/checkboxes/checkboxes.spec.ts +20 -5
  11. package/src/features/checkboxes/feature.ts +31 -16
  12. package/src/features/checkboxes/types.ts +1 -0
  13. package/src/features/drag-and-drop/drag-and-drop.spec.ts +11 -2
  14. package/src/features/drag-and-drop/feature.ts +51 -16
  15. package/src/features/drag-and-drop/types.ts +17 -0
  16. package/src/features/keyboard-drag-and-drop/feature.ts +8 -1
  17. package/src/features/keyboard-drag-and-drop/keyboard-drag-and-drop.spec.ts +34 -3
  18. package/src/features/main/types.ts +0 -2
  19. package/src/features/sync-data-loader/feature.ts +5 -1
  20. package/src/features/tree/feature.ts +4 -3
  21. package/src/features/tree/tree.spec.ts +14 -4
  22. package/src/test-utils/test-tree-do.ts +2 -0
  23. package/src/test-utils/test-tree.ts +1 -0
  24. package/tsconfig.json +1 -4
  25. package/vitest.config.ts +3 -1
  26. package/lib/cjs/core/build-proxified-instance.d.ts +0 -2
  27. package/lib/cjs/core/build-proxified-instance.js +0 -58
  28. package/lib/cjs/core/build-static-instance.d.ts +0 -2
  29. package/lib/cjs/core/build-static-instance.js +0 -26
  30. package/lib/cjs/core/create-tree.d.ts +0 -2
  31. package/lib/cjs/core/create-tree.js +0 -191
  32. package/lib/cjs/features/async-data-loader/feature.d.ts +0 -2
  33. package/lib/cjs/features/async-data-loader/feature.js +0 -135
  34. package/lib/cjs/features/async-data-loader/types.d.ts +0 -47
  35. package/lib/cjs/features/async-data-loader/types.js +0 -2
  36. package/lib/cjs/features/checkboxes/feature.d.ts +0 -2
  37. package/lib/cjs/features/checkboxes/feature.js +0 -94
  38. package/lib/cjs/features/checkboxes/types.d.ts +0 -26
  39. package/lib/cjs/features/checkboxes/types.js +0 -9
  40. package/lib/cjs/features/drag-and-drop/feature.d.ts +0 -2
  41. package/lib/cjs/features/drag-and-drop/feature.js +0 -205
  42. package/lib/cjs/features/drag-and-drop/types.d.ts +0 -71
  43. package/lib/cjs/features/drag-and-drop/types.js +0 -9
  44. package/lib/cjs/features/drag-and-drop/utils.d.ts +0 -27
  45. package/lib/cjs/features/drag-and-drop/utils.js +0 -182
  46. package/lib/cjs/features/expand-all/feature.d.ts +0 -2
  47. package/lib/cjs/features/expand-all/feature.js +0 -70
  48. package/lib/cjs/features/expand-all/types.d.ts +0 -19
  49. package/lib/cjs/features/expand-all/types.js +0 -2
  50. package/lib/cjs/features/hotkeys-core/feature.d.ts +0 -2
  51. package/lib/cjs/features/hotkeys-core/feature.js +0 -107
  52. package/lib/cjs/features/hotkeys-core/types.d.ts +0 -27
  53. package/lib/cjs/features/hotkeys-core/types.js +0 -2
  54. package/lib/cjs/features/keyboard-drag-and-drop/feature.d.ts +0 -2
  55. package/lib/cjs/features/keyboard-drag-and-drop/feature.js +0 -206
  56. package/lib/cjs/features/keyboard-drag-and-drop/types.d.ts +0 -27
  57. package/lib/cjs/features/keyboard-drag-and-drop/types.js +0 -11
  58. package/lib/cjs/features/main/types.d.ts +0 -47
  59. package/lib/cjs/features/main/types.js +0 -2
  60. package/lib/cjs/features/prop-memoization/feature.d.ts +0 -2
  61. package/lib/cjs/features/prop-memoization/feature.js +0 -70
  62. package/lib/cjs/features/prop-memoization/types.d.ts +0 -15
  63. package/lib/cjs/features/prop-memoization/types.js +0 -2
  64. package/lib/cjs/features/renaming/feature.d.ts +0 -2
  65. package/lib/cjs/features/renaming/feature.js +0 -86
  66. package/lib/cjs/features/renaming/types.d.ts +0 -27
  67. package/lib/cjs/features/renaming/types.js +0 -2
  68. package/lib/cjs/features/search/feature.d.ts +0 -2
  69. package/lib/cjs/features/search/feature.js +0 -119
  70. package/lib/cjs/features/search/types.d.ts +0 -32
  71. package/lib/cjs/features/search/types.js +0 -2
  72. package/lib/cjs/features/selection/feature.d.ts +0 -2
  73. package/lib/cjs/features/selection/feature.js +0 -132
  74. package/lib/cjs/features/selection/types.d.ts +0 -21
  75. package/lib/cjs/features/selection/types.js +0 -2
  76. package/lib/cjs/features/sync-data-loader/feature.d.ts +0 -2
  77. package/lib/cjs/features/sync-data-loader/feature.js +0 -49
  78. package/lib/cjs/features/sync-data-loader/types.d.ts +0 -28
  79. package/lib/cjs/features/sync-data-loader/types.js +0 -2
  80. package/lib/cjs/features/tree/feature.d.ts +0 -2
  81. package/lib/cjs/features/tree/feature.js +0 -244
  82. package/lib/cjs/features/tree/types.d.ts +0 -63
  83. package/lib/cjs/features/tree/types.js +0 -2
  84. package/lib/cjs/index.d.ts +0 -33
  85. package/lib/cjs/index.js +0 -51
  86. package/lib/cjs/mddocs-entry.d.ts +0 -121
  87. package/lib/cjs/mddocs-entry.js +0 -17
  88. package/lib/cjs/test-utils/test-tree-do.d.ts +0 -23
  89. package/lib/cjs/test-utils/test-tree-do.js +0 -112
  90. package/lib/cjs/test-utils/test-tree-expect.d.ts +0 -17
  91. package/lib/cjs/test-utils/test-tree-expect.js +0 -66
  92. package/lib/cjs/test-utils/test-tree.d.ts +0 -48
  93. package/lib/cjs/test-utils/test-tree.js +0 -207
  94. package/lib/cjs/types/core.d.ts +0 -84
  95. package/lib/cjs/types/core.js +0 -2
  96. package/lib/cjs/types/deep-merge.d.ts +0 -13
  97. package/lib/cjs/types/deep-merge.js +0 -2
  98. package/lib/cjs/utilities/create-on-drop-handler.d.ts +0 -3
  99. package/lib/cjs/utilities/create-on-drop-handler.js +0 -20
  100. package/lib/cjs/utilities/errors.d.ts +0 -2
  101. package/lib/cjs/utilities/errors.js +0 -9
  102. package/lib/cjs/utilities/insert-items-at-target.d.ts +0 -3
  103. package/lib/cjs/utilities/insert-items-at-target.js +0 -40
  104. package/lib/cjs/utilities/remove-items-from-parents.d.ts +0 -2
  105. package/lib/cjs/utilities/remove-items-from-parents.js +0 -32
  106. package/lib/cjs/utils.d.ts +0 -6
  107. package/lib/cjs/utils.js +0 -53
  108. package/lib/esm/core/build-proxified-instance.d.ts +0 -2
  109. package/lib/esm/core/build-proxified-instance.js +0 -54
  110. package/lib/esm/core/build-static-instance.d.ts +0 -2
  111. package/lib/esm/core/build-static-instance.js +0 -22
  112. package/lib/esm/core/create-tree.d.ts +0 -2
  113. package/lib/esm/core/create-tree.js +0 -187
  114. package/lib/esm/features/async-data-loader/feature.d.ts +0 -2
  115. package/lib/esm/features/async-data-loader/feature.js +0 -132
  116. package/lib/esm/features/async-data-loader/types.d.ts +0 -47
  117. package/lib/esm/features/async-data-loader/types.js +0 -1
  118. package/lib/esm/features/checkboxes/feature.d.ts +0 -2
  119. package/lib/esm/features/checkboxes/feature.js +0 -91
  120. package/lib/esm/features/checkboxes/types.d.ts +0 -26
  121. package/lib/esm/features/checkboxes/types.js +0 -6
  122. package/lib/esm/features/drag-and-drop/feature.d.ts +0 -2
  123. package/lib/esm/features/drag-and-drop/feature.js +0 -202
  124. package/lib/esm/features/drag-and-drop/types.d.ts +0 -71
  125. package/lib/esm/features/drag-and-drop/types.js +0 -6
  126. package/lib/esm/features/drag-and-drop/utils.d.ts +0 -27
  127. package/lib/esm/features/drag-and-drop/utils.js +0 -172
  128. package/lib/esm/features/expand-all/feature.d.ts +0 -2
  129. package/lib/esm/features/expand-all/feature.js +0 -67
  130. package/lib/esm/features/expand-all/types.d.ts +0 -19
  131. package/lib/esm/features/expand-all/types.js +0 -1
  132. package/lib/esm/features/hotkeys-core/feature.d.ts +0 -2
  133. package/lib/esm/features/hotkeys-core/feature.js +0 -104
  134. package/lib/esm/features/hotkeys-core/types.d.ts +0 -27
  135. package/lib/esm/features/hotkeys-core/types.js +0 -1
  136. package/lib/esm/features/keyboard-drag-and-drop/feature.d.ts +0 -2
  137. package/lib/esm/features/keyboard-drag-and-drop/feature.js +0 -203
  138. package/lib/esm/features/keyboard-drag-and-drop/types.d.ts +0 -27
  139. package/lib/esm/features/keyboard-drag-and-drop/types.js +0 -8
  140. package/lib/esm/features/main/types.d.ts +0 -47
  141. package/lib/esm/features/main/types.js +0 -1
  142. package/lib/esm/features/prop-memoization/feature.d.ts +0 -2
  143. package/lib/esm/features/prop-memoization/feature.js +0 -67
  144. package/lib/esm/features/prop-memoization/types.d.ts +0 -15
  145. package/lib/esm/features/prop-memoization/types.js +0 -1
  146. package/lib/esm/features/renaming/feature.d.ts +0 -2
  147. package/lib/esm/features/renaming/feature.js +0 -83
  148. package/lib/esm/features/renaming/types.d.ts +0 -27
  149. package/lib/esm/features/renaming/types.js +0 -1
  150. package/lib/esm/features/search/feature.d.ts +0 -2
  151. package/lib/esm/features/search/feature.js +0 -116
  152. package/lib/esm/features/search/types.d.ts +0 -32
  153. package/lib/esm/features/search/types.js +0 -1
  154. package/lib/esm/features/selection/feature.d.ts +0 -2
  155. package/lib/esm/features/selection/feature.js +0 -129
  156. package/lib/esm/features/selection/types.d.ts +0 -21
  157. package/lib/esm/features/selection/types.js +0 -1
  158. package/lib/esm/features/sync-data-loader/feature.d.ts +0 -2
  159. package/lib/esm/features/sync-data-loader/feature.js +0 -46
  160. package/lib/esm/features/sync-data-loader/types.d.ts +0 -28
  161. package/lib/esm/features/sync-data-loader/types.js +0 -1
  162. package/lib/esm/features/tree/feature.d.ts +0 -2
  163. package/lib/esm/features/tree/feature.js +0 -241
  164. package/lib/esm/features/tree/types.d.ts +0 -63
  165. package/lib/esm/features/tree/types.js +0 -1
  166. package/lib/esm/index.d.ts +0 -33
  167. package/lib/esm/index.js +0 -32
  168. package/lib/esm/mddocs-entry.d.ts +0 -121
  169. package/lib/esm/mddocs-entry.js +0 -1
  170. package/lib/esm/test-utils/test-tree-do.d.ts +0 -23
  171. package/lib/esm/test-utils/test-tree-do.js +0 -108
  172. package/lib/esm/test-utils/test-tree-expect.d.ts +0 -17
  173. package/lib/esm/test-utils/test-tree-expect.js +0 -62
  174. package/lib/esm/test-utils/test-tree.d.ts +0 -48
  175. package/lib/esm/test-utils/test-tree.js +0 -203
  176. package/lib/esm/types/core.d.ts +0 -84
  177. package/lib/esm/types/core.js +0 -1
  178. package/lib/esm/types/deep-merge.d.ts +0 -13
  179. package/lib/esm/types/deep-merge.js +0 -1
  180. package/lib/esm/utilities/create-on-drop-handler.d.ts +0 -3
  181. package/lib/esm/utilities/create-on-drop-handler.js +0 -16
  182. package/lib/esm/utilities/errors.d.ts +0 -2
  183. package/lib/esm/utilities/errors.js +0 -4
  184. package/lib/esm/utilities/insert-items-at-target.d.ts +0 -3
  185. package/lib/esm/utilities/insert-items-at-target.js +0 -36
  186. package/lib/esm/utilities/remove-items-from-parents.d.ts +0 -2
  187. package/lib/esm/utilities/remove-items-from-parents.js +0 -28
  188. package/lib/esm/utils.d.ts +0 -6
  189. package/lib/esm/utils.js +0 -46
@@ -1,203 +0,0 @@
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
- /* eslint-disable import/no-extraneous-dependencies */
11
- import { beforeEach, describe, vi } from "vitest";
12
- import { createTree } from "../core/create-tree";
13
- import { TestTreeDo } from "./test-tree-do";
14
- import { TestTreeExpect } from "./test-tree-expect";
15
- import { syncDataLoaderFeature } from "../features/sync-data-loader/feature";
16
- import { asyncDataLoaderFeature } from "../features/async-data-loader/feature";
17
- import { buildProxiedInstance } from "../core/build-proxified-instance";
18
- vi.useFakeTimers({ shouldAdvanceTime: true });
19
- export class TestTree {
20
- forSuits(runSuite) {
21
- describe.for([
22
- this.suits.sync(),
23
- this.suits.async(),
24
- this.suits.proxifiedSync(),
25
- this.suits.proxifiedAsync(),
26
- ])("$title", ({ tree }) => {
27
- tree.resetBeforeEach();
28
- runSuite(tree);
29
- });
30
- }
31
- get instance() {
32
- if (!this.treeInstance) {
33
- this.treeInstance = createTree(this.config);
34
- this.treeInstance.rebuildTree();
35
- }
36
- return this.treeInstance;
37
- }
38
- constructor(config) {
39
- this.config = config;
40
- this.do = new TestTreeDo(this);
41
- this.expect = new TestTreeExpect(this);
42
- this.treeInstance = null;
43
- this.asyncDataLoaderImp = {
44
- getItem: (id) => __awaiter(this, void 0, void 0, function* () {
45
- yield new Promise((r) => {
46
- r.debugName = `Loading getItem ${id}`;
47
- TestTree.asyncLoaderResolvers.push(r);
48
- });
49
- return id;
50
- }),
51
- getChildren: (id) => __awaiter(this, void 0, void 0, function* () {
52
- yield new Promise((r) => {
53
- r.debugName = `Loading getChildren ${id}`;
54
- TestTree.asyncLoaderResolvers.push(r);
55
- });
56
- return [`${id}1`, `${id}2`, `${id}3`, `${id}4`];
57
- }),
58
- };
59
- this.suits = {
60
- sync: () => ({
61
- tree: this.withFeatures(syncDataLoaderFeature),
62
- title: "Synchronous Data Loader",
63
- }),
64
- async: () => ({
65
- tree: this.withFeatures(asyncDataLoaderFeature).with({
66
- dataLoader: this.asyncDataLoaderImp,
67
- }),
68
- title: "Asynchronous Data Loader",
69
- }),
70
- proxifiedSync: () => ({
71
- tree: this.withFeatures(syncDataLoaderFeature).with({
72
- instanceBuilder: buildProxiedInstance,
73
- }),
74
- title: "Proxified Synchronous Data Loader",
75
- }),
76
- proxifiedAsync: () => ({
77
- tree: this.withFeatures(asyncDataLoaderFeature).with({
78
- instanceBuilder: buildProxiedInstance,
79
- }),
80
- title: "Proxified Asynchronous Data Loader",
81
- }),
82
- };
83
- }
84
- static resolveAsyncLoaders() {
85
- return __awaiter(this, void 0, void 0, function* () {
86
- var _a;
87
- do {
88
- (_a = TestTree.asyncLoaderResolvers.shift()) === null || _a === void 0 ? void 0 : _a();
89
- yield new Promise((r) => {
90
- setTimeout(r);
91
- });
92
- } while (TestTree.asyncLoaderResolvers.length);
93
- });
94
- }
95
- resolveAsyncVisibleItems() {
96
- return __awaiter(this, void 0, void 0, function* () {
97
- this.instance.getItems();
98
- yield TestTree.resolveAsyncLoaders();
99
- this.instance.getItems().forEach((i) => i.getItemName());
100
- yield TestTree.resolveAsyncLoaders();
101
- });
102
- }
103
- static default(config) {
104
- return new TestTree(Object.assign({ rootItemId: "x", createLoadingItemData: () => "loading", dataLoader: {
105
- getItem: (id) => id,
106
- getChildren: (id) => [`${id}1`, `${id}2`, `${id}3`, `${id}4`],
107
- }, getItemName: (item) => item.getItemData(), indent: 20, isItemFolder: (item) => item.getItemMeta().level < 2, initialState: {
108
- expandedItems: ["x1", "x11"],
109
- }, features: [] }, config));
110
- }
111
- with(config) {
112
- return new TestTree(Object.assign(Object.assign({}, this.config), config));
113
- }
114
- resetBeforeEach() {
115
- beforeEach(() => __awaiter(this, void 0, void 0, function* () {
116
- yield this.createTestCaseTree();
117
- }));
118
- }
119
- createTestCaseTree() {
120
- return __awaiter(this, void 0, void 0, function* () {
121
- this.reset();
122
- vi.clearAllMocks();
123
- // trigger instance creation
124
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
125
- this.instance;
126
- yield this.resolveAsyncVisibleItems();
127
- return this;
128
- });
129
- }
130
- withFeatures(...features) {
131
- var _a;
132
- return this.with({
133
- features: [...((_a = this.config.features) !== null && _a !== void 0 ? _a : []), ...features],
134
- });
135
- }
136
- mockedHandler(handlerName) {
137
- var _a;
138
- const mock = vi.fn();
139
- if (this.treeInstance) {
140
- (_a = this.treeInstance) === null || _a === void 0 ? void 0 : _a.setConfig((prev) => (Object.assign(Object.assign({}, prev), { [handlerName]: mock })));
141
- }
142
- else {
143
- this.config[handlerName] = mock;
144
- }
145
- return mock;
146
- }
147
- item(itemId) {
148
- return this.instance.getItemInstance(itemId);
149
- }
150
- reset() {
151
- this.treeInstance = null;
152
- TestTree.asyncLoaderResolvers = [];
153
- }
154
- debug() {
155
- console.log(this.instance
156
- .getItems()
157
- .map((item) => [
158
- " ".repeat(item.getItemMeta().level),
159
- '"',
160
- item.getItemName(),
161
- '"',
162
- ].join(""))
163
- .join("\n"));
164
- }
165
- setElementBoundingBox(itemId, bb = {
166
- left: 0,
167
- width: 100,
168
- top: 0,
169
- height: 20,
170
- }) {
171
- this.instance.registerElement({
172
- getBoundingClientRect: () => ({
173
- left: 0,
174
- width: 100,
175
- top: 0,
176
- height: 10000,
177
- }),
178
- });
179
- this.instance.getItemInstance(itemId).registerElement({
180
- getBoundingClientRect: () => bb,
181
- });
182
- }
183
- static dragEvent(clientX = 1000, clientY = 0) {
184
- return {
185
- preventDefault: vi.fn(),
186
- stopPropagation: vi.fn(),
187
- dataTransfer: {
188
- setData: vi.fn(),
189
- getData: vi.fn(),
190
- dropEffect: "unchaged-from-test",
191
- },
192
- clientX,
193
- clientY,
194
- };
195
- }
196
- createTopDragEvent(indent = 0) {
197
- return TestTree.dragEvent(indent * 20, 1);
198
- }
199
- createBottomDragEvent(indent = 0) {
200
- return TestTree.dragEvent(indent * 20, 19);
201
- }
202
- }
203
- TestTree.asyncLoaderResolvers = [];
@@ -1,84 +0,0 @@
1
- import { DragAndDropFeatureDef } from "../features/drag-and-drop/types";
2
- import { MainFeatureDef } from "../features/main/types";
3
- import { SelectionFeatureDef } from "../features/selection/types";
4
- import { TreeFeatureDef } from "../features/tree/types";
5
- import { HotkeyConfig, HotkeysCoreFeatureDef } from "../features/hotkeys-core/types";
6
- import { SyncDataLoaderFeatureDef } from "../features/sync-data-loader/types";
7
- import { AsyncDataLoaderFeatureDef } from "../features/async-data-loader/types";
8
- import { SearchFeatureDef } from "../features/search/types";
9
- import { RenamingFeatureDef } from "../features/renaming/types";
10
- import { ExpandAllFeatureDef } from "../features/expand-all/types";
11
- import { PropMemoizationFeatureDef } from "../features/prop-memoization/types";
12
- import { KeyboardDragAndDropFeatureDef } from "../features/keyboard-drag-and-drop/types";
13
- import { CheckboxesFeatureDef } from "../features/checkboxes/types";
14
- export type Updater<T> = T | ((old: T) => T);
15
- export type SetStateFn<T> = (updaterOrValue: Updater<T>) => void;
16
- export type FeatureDef = {
17
- state: any;
18
- config: any;
19
- treeInstance: any;
20
- itemInstance: any;
21
- hotkeys: string;
22
- };
23
- export type EmptyFeatureDef = {
24
- state: {};
25
- config: {};
26
- treeInstance: {};
27
- itemInstance: {};
28
- hotkeys: never;
29
- };
30
- type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
31
- type MergedFeatures<F extends FeatureDef> = {
32
- state: UnionToIntersection<F["state"]>;
33
- config: UnionToIntersection<F["config"]>;
34
- treeInstance: UnionToIntersection<F["treeInstance"]>;
35
- itemInstance: UnionToIntersection<F["itemInstance"]>;
36
- hotkeys: F["hotkeys"];
37
- };
38
- export type RegisteredFeatures<T> = MainFeatureDef<T> | TreeFeatureDef<T> | SelectionFeatureDef<T> | CheckboxesFeatureDef<T> | DragAndDropFeatureDef<T> | KeyboardDragAndDropFeatureDef<T> | HotkeysCoreFeatureDef<T> | SyncDataLoaderFeatureDef<T> | AsyncDataLoaderFeatureDef<T> | SearchFeatureDef<T> | RenamingFeatureDef<T> | ExpandAllFeatureDef | PropMemoizationFeatureDef;
39
- type TreeStateType<T> = MergedFeatures<RegisteredFeatures<T>>["state"];
40
- export interface TreeState<T> extends TreeStateType<T> {
41
- }
42
- type TreeConfigType<T> = MergedFeatures<RegisteredFeatures<T>>["config"];
43
- export interface TreeConfig<T> extends TreeConfigType<T> {
44
- }
45
- type TreeInstanceType<T> = MergedFeatures<RegisteredFeatures<T>>["treeInstance"];
46
- export interface TreeInstance<T> extends TreeInstanceType<T> {
47
- }
48
- type ItemInstanceType<T> = MergedFeatures<RegisteredFeatures<T>>["itemInstance"];
49
- export interface ItemInstance<T> extends ItemInstanceType<T> {
50
- }
51
- export type HotkeyName = MergedFeatures<RegisteredFeatures<any>>["hotkeys"];
52
- export type HotkeysConfig<T> = Record<HotkeyName, HotkeyConfig<T>>;
53
- export type CustomHotkeysConfig<T> = Partial<Record<HotkeyName | `custom${string}`, Partial<HotkeyConfig<T>>>>;
54
- type MayReturnNull<T extends (...x: any[]) => any> = (...args: Parameters<T>) => ReturnType<T> | null;
55
- export type ItemInstanceOpts<Key extends keyof ItemInstance<any>> = {
56
- item: ItemInstance<any>;
57
- tree: TreeInstance<any>;
58
- itemId: string;
59
- prev?: MayReturnNull<ItemInstance<any>[Key]>;
60
- };
61
- export type TreeInstanceOpts<Key extends keyof TreeInstance<any>> = {
62
- tree: TreeInstance<any>;
63
- prev?: MayReturnNull<TreeInstance<any>[Key]>;
64
- };
65
- export type FeatureImplementation<T = any> = {
66
- key?: string;
67
- deps?: string[];
68
- overwrites?: string[];
69
- stateHandlerNames?: Partial<Record<keyof TreeState<T>, keyof TreeConfig<T>>>;
70
- getInitialState?: (initialState: Partial<TreeState<T>>, tree: TreeInstance<T>) => Partial<TreeState<T>>;
71
- getDefaultConfig?: (defaultConfig: Partial<TreeConfig<T>>, tree: TreeInstance<T>) => Partial<TreeConfig<T>>;
72
- treeInstance?: {
73
- [key in keyof TreeInstance<T>]?: (opts: TreeInstanceOpts<key>, ...args: Parameters<TreeInstance<T>[key]>) => void;
74
- };
75
- itemInstance?: {
76
- [key in keyof ItemInstance<T>]?: (opts: ItemInstanceOpts<key>, ...args: Parameters<ItemInstance<T>[key]>) => void;
77
- };
78
- onTreeMount?: (instance: TreeInstance<T>, treeElement: HTMLElement) => void;
79
- onTreeUnmount?: (instance: TreeInstance<T>, treeElement: HTMLElement) => void;
80
- onItemMount?: (instance: ItemInstance<T>, itemElement: HTMLElement, tree: TreeInstance<T>) => void;
81
- onItemUnmount?: (instance: ItemInstance<T>, itemElement: HTMLElement, tree: TreeInstance<T>) => void;
82
- hotkeys?: Partial<HotkeysConfig<T>>;
83
- };
84
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,13 +0,0 @@
1
- type TAllKeys<T> = T extends any ? keyof T : never;
2
- type TIndexValue<T, K extends PropertyKey, D = never> = T extends any ? K extends keyof T ? T[K] : D : never;
3
- type TPartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>> extends infer O ? {
4
- [P in keyof O]: O[P];
5
- } : never;
6
- type TFunction = (...a: any[]) => any;
7
- type TPrimitives = string | number | boolean | bigint | symbol | Date | TFunction;
8
- export type TMerged<T> = [T] extends [Array<any>] ? {
9
- [K in keyof T]: TMerged<T[K]>;
10
- } : [T] extends [TPrimitives] ? T : [T] extends [object] ? TPartialKeys<{
11
- [K in TAllKeys<T>]: TMerged<TIndexValue<T, K>>;
12
- }, never> : T;
13
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import { ItemInstance } from "../types/core";
2
- import { DragTarget } from "../features/drag-and-drop/types";
3
- export declare const createOnDropHandler: <T>(onChangeChildren: (item: ItemInstance<T>, newChildren: string[]) => void) => (items: ItemInstance<T>[], target: DragTarget<T>) => Promise<void>;
@@ -1,16 +0,0 @@
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
- import { removeItemsFromParents } from "./remove-items-from-parents";
11
- import { insertItemsAtTarget } from "./insert-items-at-target";
12
- export const createOnDropHandler = (onChangeChildren) => (items, target) => __awaiter(void 0, void 0, void 0, function* () {
13
- const itemIds = items.map((item) => item.getId());
14
- yield removeItemsFromParents(items, onChangeChildren);
15
- yield insertItemsAtTarget(itemIds, target, onChangeChildren);
16
- });
@@ -1,2 +0,0 @@
1
- export declare const throwError: (message: string) => Error;
2
- export declare const logWarning: (message: string) => void;
@@ -1,4 +0,0 @@
1
- const prefix = "Headless Tree: ";
2
- export const throwError = (message) => Error(prefix + message);
3
- // eslint-disable-next-line no-console
4
- export const logWarning = (message) => console.warn(prefix + message);
@@ -1,3 +0,0 @@
1
- import { ItemInstance } from "../types/core";
2
- import { DragTarget } from "../features/drag-and-drop/types";
3
- export declare const insertItemsAtTarget: <T>(itemIds: string[], target: DragTarget<T>, onChangeChildren: (item: ItemInstance<T>, newChildrenIds: string[]) => Promise<void> | void) => Promise<void>;
@@ -1,36 +0,0 @@
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());
15
- // add moved items to new common parent, if dropped onto parent
16
- if (!("childIndex" in target)) {
17
- const newChildren = [...oldChildrenIds, ...itemIds];
18
- yield onChangeChildren(target.item, newChildren);
19
- if (target.item && "updateCachedChildrenIds" in target.item) {
20
- target.item.updateCachedChildrenIds(newChildren);
21
- }
22
- target.item.getTree().rebuildTree();
23
- return;
24
- }
25
- // add moved items to new common parent, if dropped between siblings
26
- const newChildren = [
27
- ...oldChildrenIds.slice(0, target.insertionIndex),
28
- ...itemIds,
29
- ...oldChildrenIds.slice(target.insertionIndex),
30
- ];
31
- yield onChangeChildren(target.item, newChildren);
32
- if (target.item && "updateCachedChildrenIds" in target.item) {
33
- target.item.updateCachedChildrenIds(newChildren);
34
- }
35
- target.item.getTree().rebuildTree();
36
- });
@@ -1,2 +0,0 @@
1
- import { ItemInstance } from "../types/core";
2
- export declare const removeItemsFromParents: <T>(movedItems: ItemInstance<T>[], onChangeChildren: (item: ItemInstance<T>, newChildrenIds: string[]) => void | Promise<void>) => Promise<void>;
@@ -1,28 +0,0 @@
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* () {
11
- const movedItemsIds = movedItems.map((item) => item.getId());
12
- const uniqueParents = [
13
- ...new Set(movedItems.map((item) => item.getParent())),
14
- ];
15
- for (const parent of uniqueParents) {
16
- const siblings = parent === null || parent === void 0 ? void 0 : parent.getChildren();
17
- if (siblings && parent) {
18
- const newChildren = siblings
19
- .filter((sibling) => !movedItemsIds.includes(sibling.getId()))
20
- .map((i) => i.getId());
21
- yield onChangeChildren(parent, newChildren);
22
- if (parent && "updateCachedChildrenIds" in parent) {
23
- parent === null || parent === void 0 ? void 0 : parent.updateCachedChildrenIds(newChildren);
24
- }
25
- }
26
- }
27
- movedItems[0].getTree().rebuildTree();
28
- });
@@ -1,6 +0,0 @@
1
- import { SetStateFn, TreeState, Updater } from "./types/core";
2
- export type NoInfer<T> = [T][T extends any ? 0 : never];
3
- export declare const memo: <D extends readonly any[], P extends readonly any[], R>(deps: (...args: [...P]) => [...D], fn: (...args: [...D]) => R) => (...a: P) => R;
4
- export declare function functionalUpdate<T>(updater: Updater<T>, input: T): T;
5
- export declare function makeStateUpdater<K extends keyof TreeState<any>>(key: K, instance: unknown): SetStateFn<TreeState<any>[K]>;
6
- export declare const poll: (fn: () => boolean, interval?: number, timeout?: number) => Promise<void>;
package/lib/esm/utils.js DELETED
@@ -1,46 +0,0 @@
1
- export const memo = (deps, fn) => {
2
- let value;
3
- let oldDeps = null;
4
- return (...a) => {
5
- const newDeps = deps(...a);
6
- if (!value) {
7
- value = fn(...newDeps);
8
- oldDeps = newDeps;
9
- return value;
10
- }
11
- const match = oldDeps &&
12
- oldDeps.length === newDeps.length &&
13
- !oldDeps.some((dep, i) => dep !== newDeps[i]);
14
- if (match) {
15
- return value;
16
- }
17
- value = fn(...newDeps);
18
- oldDeps = newDeps;
19
- return value;
20
- };
21
- };
22
- export function functionalUpdate(updater, input) {
23
- return typeof updater === "function"
24
- ? updater(input)
25
- : updater;
26
- }
27
- export function makeStateUpdater(key, instance) {
28
- return (updater) => {
29
- instance.setState((old) => {
30
- return Object.assign(Object.assign({}, old), { [key]: functionalUpdate(updater, old[key]) });
31
- });
32
- };
33
- }
34
- export const poll = (fn, interval = 100, timeout = 1000) => new Promise((resolve) => {
35
- let clear;
36
- const i = setInterval(() => {
37
- if (fn()) {
38
- resolve();
39
- clearInterval(i);
40
- clearTimeout(clear);
41
- }
42
- }, interval);
43
- clear = setTimeout(() => {
44
- clearInterval(i);
45
- }, timeout);
46
- });