@splicetree/core 0.1.1 → 0.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.
- package/README.md +0 -2
- package/dist/index.d.ts +13 -11
- package/dist/index.js +34 -12
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
* - 若传入 `D`,表示函数可选择性接收该参数类型
|
|
6
6
|
*/
|
|
7
7
|
type Fn<T, D = undefined> = [D] extends [undefined] ? () => T : (data?: D) => T;
|
|
8
|
+
/** 可能是数组的类型 */
|
|
9
|
+
type MaybeArray<T> = T | T[];
|
|
10
|
+
/** 可能是函数的类型 */
|
|
11
|
+
type MaybeFn<T, Args extends any[] = []> = T | ((...args: Args) => T);
|
|
8
12
|
/**
|
|
9
13
|
* 输入数据的通用结构
|
|
10
14
|
* - 表示原始树数据的任意键值对对象
|
|
@@ -97,15 +101,10 @@ interface SpliceTreeEvents {
|
|
|
97
101
|
}
|
|
98
102
|
/**
|
|
99
103
|
* `createSpliceTree` 的选项
|
|
100
|
-
* - `keyField`:主键字段名,默认 `'id'`
|
|
101
|
-
* - `parentField`:父级字段名,默认 `'parent'`
|
|
102
104
|
* - `plugins`:插件列表
|
|
103
|
-
* - `defaultExpanded`:默认展开的节点 id 列表(或设为 true 表示全部展开)
|
|
104
|
-
* - `defaultExpandedLevel`:默认展开的层级(或 `'deepest'` 表示展开到最深层)
|
|
105
105
|
* - 其余键允许被插件进行选项扩展
|
|
106
106
|
*/
|
|
107
|
-
interface SpliceTreeConfiguration {
|
|
108
|
-
interface UseSpliceTreeOptions<T = SpliceTreeData> {
|
|
107
|
+
interface SpliceTreeConfiguration {
|
|
109
108
|
/**
|
|
110
109
|
* 主键字段名
|
|
111
110
|
* @default 'id'
|
|
@@ -116,10 +115,6 @@ interface UseSpliceTreeOptions<T = SpliceTreeData> {
|
|
|
116
115
|
* @default 'parent'
|
|
117
116
|
*/
|
|
118
117
|
parentField?: string;
|
|
119
|
-
/**
|
|
120
|
-
* 插件列表
|
|
121
|
-
*/
|
|
122
|
-
plugins?: SpliceTreePlugin<T>[];
|
|
123
118
|
/**
|
|
124
119
|
* 默认展开的节点 ID 列表
|
|
125
120
|
* 设为 true 表示默认展开所有节点
|
|
@@ -130,6 +125,12 @@ interface UseSpliceTreeOptions<T = SpliceTreeData> {
|
|
|
130
125
|
* 设为 'deepest' 表示默认展开所有层级
|
|
131
126
|
*/
|
|
132
127
|
defaultExpandedLevel?: number | 'deepest';
|
|
128
|
+
}
|
|
129
|
+
interface UseSpliceTreeOptions<T = SpliceTreeData> {
|
|
130
|
+
/**
|
|
131
|
+
* 插件列表
|
|
132
|
+
*/
|
|
133
|
+
plugins?: SpliceTreePlugin<T>[];
|
|
133
134
|
/**
|
|
134
135
|
* 插件配置聚合入口(按插件名分类)
|
|
135
136
|
*/
|
|
@@ -214,6 +215,7 @@ interface SpliceTreeInstance<T = SpliceTreeData> {
|
|
|
214
215
|
* @param beforeId 插入到指定兄弟节点之前(不传表示末尾)
|
|
215
216
|
*/
|
|
216
217
|
moveNode: (id: string, newParentId: string | undefined, beforeId?: string) => void;
|
|
218
|
+
syncData: (next: T[]) => void;
|
|
217
219
|
}
|
|
218
220
|
/**
|
|
219
221
|
* 插件上下文
|
|
@@ -281,4 +283,4 @@ type CreateSpliceTree = <T = SpliceTreeData>(data: T[], options?: UseSpliceTreeO
|
|
|
281
283
|
*/
|
|
282
284
|
declare function createSpliceTree<T extends SpliceTreeData = SpliceTreeData>(data: T[], options?: UseSpliceTreeOptions<T>): SpliceTreeInstance<T>;
|
|
283
285
|
//#endregion
|
|
284
|
-
export { CreateSpliceTree, Fn, SpliceTreeConfiguration, SpliceTreeData, SpliceTreeEventName, SpliceTreeEventPayload, SpliceTreeEventPayloadMap, SpliceTreeEvents, SpliceTreeInstance, SpliceTreeNode, SpliceTreePlugin, SpliceTreePluginContext, UseSpliceTreeOptions, createSpliceTree };
|
|
286
|
+
export { CreateSpliceTree, Fn, MaybeArray, MaybeFn, SpliceTreeConfiguration, SpliceTreeData, SpliceTreeEventName, SpliceTreeEventPayload, SpliceTreeEventPayloadMap, SpliceTreeEvents, SpliceTreeInstance, SpliceTreeNode, SpliceTreePlugin, SpliceTreePluginContext, UseSpliceTreeOptions, createSpliceTree };
|
package/dist/index.js
CHANGED
|
@@ -327,8 +327,11 @@ function moveNode(ctx, id, newParentId, beforeId) {
|
|
|
327
327
|
* - 提供插件扩展点(setup/extendNode)
|
|
328
328
|
*/
|
|
329
329
|
function createSpliceTree(data, options = {}) {
|
|
330
|
-
const
|
|
331
|
-
const
|
|
330
|
+
const cfg = options.configuration ?? {};
|
|
331
|
+
const keyField = cfg.keyField ?? "id";
|
|
332
|
+
const parentField = cfg.parentField ?? "parent";
|
|
333
|
+
const defaultExpanded = cfg.defaultExpanded;
|
|
334
|
+
const defaultExpandedLevel = cfg.defaultExpandedLevel;
|
|
332
335
|
const events = createEmitter();
|
|
333
336
|
const expandedKeys = createReactive(/* @__PURE__ */ new Set(), (payload) => {
|
|
334
337
|
events.emit({
|
|
@@ -336,8 +339,8 @@ function createSpliceTree(data, options = {}) {
|
|
|
336
339
|
keys: Array.from(payload.target)
|
|
337
340
|
});
|
|
338
341
|
});
|
|
339
|
-
|
|
340
|
-
initDefaultExpansion(map, expandedKeys,
|
|
342
|
+
let { roots, map, parentCache, childrenCache } = buildTree(data, keyField, parentField, expandedKeys);
|
|
343
|
+
initDefaultExpansion(map, expandedKeys, defaultExpanded, defaultExpandedLevel);
|
|
341
344
|
const emitVisibility = () => {
|
|
342
345
|
events.emit({
|
|
343
346
|
name: "visibility",
|
|
@@ -406,6 +409,22 @@ function createSpliceTree(data, options = {}) {
|
|
|
406
409
|
expandedKeys,
|
|
407
410
|
notify: emitVisibility
|
|
408
411
|
}, id, newParentId, beforeId);
|
|
412
|
+
},
|
|
413
|
+
syncData(next) {
|
|
414
|
+
tree.data = next;
|
|
415
|
+
const built = buildTree(next, keyField, parentField, expandedKeys);
|
|
416
|
+
roots = built.roots;
|
|
417
|
+
map = built.map;
|
|
418
|
+
parentCache = built.parentCache;
|
|
419
|
+
childrenCache = built.childrenCache;
|
|
420
|
+
const validIds = new Set(map.keys());
|
|
421
|
+
const toDelete = [];
|
|
422
|
+
expandedKeys.forEach((id) => {
|
|
423
|
+
if (!validIds.has(id)) toDelete.push(id);
|
|
424
|
+
});
|
|
425
|
+
for (const id of toDelete) expandedKeys.delete(id);
|
|
426
|
+
applyNodeExtensions();
|
|
427
|
+
emitVisibility();
|
|
409
428
|
}
|
|
410
429
|
};
|
|
411
430
|
const pluginCtx = {
|
|
@@ -417,15 +436,18 @@ function createSpliceTree(data, options = {}) {
|
|
|
417
436
|
const api = plugin.setup?.(pluginCtx);
|
|
418
437
|
Object.assign(tree, api);
|
|
419
438
|
});
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
439
|
+
function applyNodeExtensions() {
|
|
440
|
+
if (options?.plugins?.length) for (const n of map.values()) for (const plugin of options.plugins) plugin.extendNode?.(n, pluginCtx);
|
|
441
|
+
for (const node of map.values()) {
|
|
442
|
+
node.isExpanded = () => tree.isExpanded(node.id);
|
|
443
|
+
node.toggleExpand = (expand) => {
|
|
444
|
+
if (expand === void 0) tree.toggleExpand(node.id);
|
|
445
|
+
else if (expand) tree.expand(node.id);
|
|
446
|
+
else tree.collapse(node.id);
|
|
447
|
+
};
|
|
448
|
+
}
|
|
428
449
|
}
|
|
450
|
+
applyNodeExtensions();
|
|
429
451
|
return tree;
|
|
430
452
|
}
|
|
431
453
|
|