@splicetree/adapter-vue 3.0.2 → 3.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.
Files changed (2) hide show
  1. package/dist/index.js +68 -7
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -425,7 +425,10 @@ function createSpliceTree(data, options = {}) {
425
425
  parentCache,
426
426
  childrenCache,
427
427
  expandedKeys,
428
- notify: emitVisibility
428
+ notify: () => {
429
+ applyNodeExtensions();
430
+ emitVisibility();
431
+ }
429
432
  }, parentId, children);
430
433
  },
431
434
  moveNode(id, newParentId, beforeId) {
@@ -452,6 +455,19 @@ function createSpliceTree(data, options = {}) {
452
455
  notify: emitVisibility
453
456
  }, ids);
454
457
  },
458
+ updateOriginal(id, patch) {
459
+ const node = map.get(id);
460
+ if (!node) return false;
461
+ if (Object.prototype.hasOwnProperty.call(patch, keyField)) return false;
462
+ if (Object.prototype.hasOwnProperty.call(patch, parentField)) {
463
+ const nextParent = patch[parentField];
464
+ if (nextParent !== node.getParent()?.id) tree.moveNode(id, nextParent);
465
+ delete patch[parentField];
466
+ }
467
+ Object.assign(node.original, patch);
468
+ emitVisibility();
469
+ return true;
470
+ },
455
471
  syncData(next) {
456
472
  tree.data = next;
457
473
  const built = buildTree(next, keyField, parentField, expandedKeys);
@@ -495,12 +511,56 @@ function createSpliceTree(data, options = {}) {
495
511
  else if (expand) tree.expand(node.id);
496
512
  else tree.collapse(node.id);
497
513
  };
514
+ node.updateOriginal = (patch) => {
515
+ tree.updateOriginal(node.id, patch);
516
+ };
498
517
  }
499
518
  }
500
519
  applyNodeExtensions();
501
520
  return tree;
502
521
  }
503
522
 
523
+ //#endregion
524
+ //#region src/utils.ts
525
+ function isStructuralChange(prev, next, keyField, parentField) {
526
+ const prevMap = /* @__PURE__ */ new Map();
527
+ for (const it of prev ?? []) {
528
+ const id = String(Reflect.get(it, keyField));
529
+ prevMap.set(id, it);
530
+ }
531
+ let structural = false;
532
+ const nextMap = /* @__PURE__ */ new Map();
533
+ for (const it of next ?? []) {
534
+ const id = String(Reflect.get(it, keyField));
535
+ nextMap.set(id, it);
536
+ const old = prevMap.get(id);
537
+ if (!old) {
538
+ structural = true;
539
+ continue;
540
+ }
541
+ if (Reflect.get(it, parentField) !== Reflect.get(old, parentField)) structural = true;
542
+ }
543
+ for (const id of prevMap.keys()) if (!nextMap.has(id)) {
544
+ structural = true;
545
+ break;
546
+ }
547
+ return structural;
548
+ }
549
+ function applyFieldPatches(api, next, keyField, parentField) {
550
+ for (const it of next ?? []) {
551
+ const id = String(Reflect.get(it, keyField));
552
+ const node = api.getNode(id);
553
+ if (!node) continue;
554
+ const patch = {};
555
+ for (const k of Object.keys(it)) {
556
+ if (k === keyField || k === parentField) continue;
557
+ const nv = it[k];
558
+ if (nv !== node.original?.[k]) patch[k] = nv;
559
+ }
560
+ if (Object.keys(patch).length) api.updateOriginal(id, patch);
561
+ }
562
+ }
563
+
504
564
  //#endregion
505
565
  //#region src/index.ts
506
566
  /**
@@ -539,13 +599,14 @@ function useSpliceTree(data, options = {}) {
539
599
  applyExpandedKeys(options?.expandedKeys ? toValue(options.expandedKeys) : void 0);
540
600
  };
541
601
  createTree();
542
- watch(() => toValue(data), () => {
543
- api.value.syncData(toValue(data));
602
+ watch(() => toValue(data), (next) => {
603
+ const cfg = options?.configuration ?? {};
604
+ const keyField = cfg.keyField ?? "id";
605
+ const parentField = cfg.parentField ?? "parent";
606
+ if (isStructuralChange(api.value?.data ?? [], next, keyField, parentField)) api.value.syncData(next);
607
+ else applyFieldPatches(api.value, next, keyField, parentField);
544
608
  applyExpandedKeys(options?.expandedKeys ? toValue(options.expandedKeys) : void 0);
545
- }, {
546
- deep: true,
547
- immediate: false
548
- });
609
+ }, { immediate: false });
549
610
  watch(() => options?.expandedKeys ? toValue(options.expandedKeys) : void 0, (val) => {
550
611
  applyExpandedKeys(val);
551
612
  }, {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splicetree/adapter-vue",
3
3
  "type": "module",
4
- "version": "3.0.2",
4
+ "version": "3.1.0",
5
5
  "author": {
6
6
  "email": "michael.cocova@gmail.com",
7
7
  "name": "Michael Cocova"
@@ -30,7 +30,7 @@
30
30
  "unplugin-vue": "^7.1.0",
31
31
  "vue": "^3.0.0",
32
32
  "vue-tsc": "^3.1.5",
33
- "@splicetree/core": "3.0.2"
33
+ "@splicetree/core": "3.1.0"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public"