@splicetree/adapter-vue 0.1.1 → 0.2.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 (3) hide show
  1. package/README.md +2 -2
  2. package/dist/index.js +35 -13
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # @splicetree/adapter-vue
2
2
 
3
+ 提供响应式 `items` 与操作方法,自动监听核心事件刷新视图。
4
+
3
5
  ## 简介
4
6
 
5
7
  SpliceTree Vue 适配层,将核心 `items()` 适配为 `shallowRef` 响应式数据,并复用核心 API。
6
8
 
7
- 提供响应式 `items` 与操作方法,自动监听核心事件刷新视图。
8
-
9
9
  [![version](https://img.shields.io/npm/v/@splicetree/adapter-vue.svg?label=version)](https://www.npmjs.com/package/@splicetree/adapter-vue)
10
10
  [![downloads](https://img.shields.io/npm/dm/@splicetree/adapter-vue.svg)](https://npmcharts.com/compare/%40splicetree%2Fadapter-vue?minimal=true)
11
11
  [![license](https://img.shields.io/npm/l/@splicetree/adapter-vue.svg)](https://www.npmjs.com/package/@splicetree/adapter-vue)
package/dist/index.js CHANGED
@@ -314,8 +314,11 @@ function moveNode(ctx, id, newParentId, beforeId) {
314
314
  * - 提供插件扩展点(setup/extendNode)
315
315
  */
316
316
  function createSpliceTree(data, options = {}) {
317
- const keyField = options.keyField ?? "id";
318
- const parentField = options.parentField ?? "parent";
317
+ const cfg = options.configuration ?? {};
318
+ const keyField = cfg.keyField ?? options.keyField ?? "id";
319
+ const parentField = cfg.parentField ?? options.parentField ?? "parent";
320
+ const defaultExpanded = cfg.defaultExpanded ?? options.defaultExpanded;
321
+ const defaultExpandedLevel = cfg.defaultExpandedLevel ?? options.defaultExpandedLevel;
319
322
  const events = createEmitter();
320
323
  const expandedKeys = createReactive(/* @__PURE__ */ new Set(), (payload) => {
321
324
  events.emit({
@@ -323,8 +326,8 @@ function createSpliceTree(data, options = {}) {
323
326
  keys: Array.from(payload.target)
324
327
  });
325
328
  });
326
- const { roots, map, parentCache, childrenCache } = buildTree(data, keyField, parentField, expandedKeys);
327
- initDefaultExpansion(map, expandedKeys, options.defaultExpanded, options.defaultExpandedLevel);
329
+ let { roots, map, parentCache, childrenCache } = buildTree(data, keyField, parentField, expandedKeys);
330
+ initDefaultExpansion(map, expandedKeys, defaultExpanded, defaultExpandedLevel);
328
331
  const emitVisibility = () => {
329
332
  events.emit({
330
333
  name: "visibility",
@@ -393,6 +396,22 @@ function createSpliceTree(data, options = {}) {
393
396
  expandedKeys,
394
397
  notify: emitVisibility
395
398
  }, id, newParentId, beforeId);
399
+ },
400
+ syncData(next) {
401
+ tree.data = next;
402
+ const built = buildTree(next, keyField, parentField, expandedKeys);
403
+ roots = built.roots;
404
+ map = built.map;
405
+ parentCache = built.parentCache;
406
+ childrenCache = built.childrenCache;
407
+ const validIds = new Set(map.keys());
408
+ const toDelete = [];
409
+ expandedKeys.forEach((id) => {
410
+ if (!validIds.has(id)) toDelete.push(id);
411
+ });
412
+ for (const id of toDelete) expandedKeys.delete(id);
413
+ applyNodeExtensions();
414
+ emitVisibility();
396
415
  }
397
416
  };
398
417
  const pluginCtx = {
@@ -404,15 +423,18 @@ function createSpliceTree(data, options = {}) {
404
423
  const api = plugin.setup?.(pluginCtx);
405
424
  Object.assign(tree, api);
406
425
  });
407
- if (options?.plugins?.length) for (const n of map.values()) for (const plugin of options.plugins) plugin.extendNode?.(n, pluginCtx);
408
- for (const node of map.values()) {
409
- node.isExpanded = () => tree.isExpanded(node.id);
410
- node.toggleExpand = (expand) => {
411
- if (expand === void 0) tree.toggleExpand(node.id);
412
- else if (expand) tree.expand(node.id);
413
- else tree.collapse(node.id);
414
- };
426
+ function applyNodeExtensions() {
427
+ if (options?.plugins?.length) for (const n of map.values()) for (const plugin of options.plugins) plugin.extendNode?.(n, pluginCtx);
428
+ for (const node of map.values()) {
429
+ node.isExpanded = () => tree.isExpanded(node.id);
430
+ node.toggleExpand = (expand) => {
431
+ if (expand === void 0) tree.toggleExpand(node.id);
432
+ else if (expand) tree.expand(node.id);
433
+ else tree.collapse(node.id);
434
+ };
435
+ }
415
436
  }
437
+ applyNodeExtensions();
416
438
  return tree;
417
439
  }
418
440
 
@@ -436,7 +458,7 @@ function useSpliceTree(data, options = {}) {
436
458
  };
437
459
  createTree();
438
460
  watch(() => toValue(data), () => {
439
- createTree();
461
+ api.value.syncData(toValue(data));
440
462
  }, {
441
463
  deep: true,
442
464
  immediate: false
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splicetree/adapter-vue",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.2.0",
5
5
  "author": {
6
6
  "email": "michael.cocova@gmail.com",
7
7
  "name": "Michael Cocova"
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "devDependencies": {
33
33
  "vue": "^3.0.0",
34
- "@splicetree/core": "0.1.1"
34
+ "@splicetree/core": "0.2.0"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"