@splicetree/adapter-vue 3.0.1 → 3.0.2

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 +53 -2
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -318,6 +318,36 @@ function moveNode(ctx, id, newParentId, beforeId) {
318
318
  setLevelRecursively(node, ctx.childrenCache, node.level);
319
319
  ctx.notify();
320
320
  }
321
+ function removeNodes(ctx, ids) {
322
+ const list = Array.isArray(ids) ? ids : [ids];
323
+ const toRemove = /* @__PURE__ */ new Set();
324
+ const collect = (id) => {
325
+ if (toRemove.has(id)) return;
326
+ toRemove.add(id);
327
+ const children = ctx.childrenCache.get(id) ?? [];
328
+ for (const c of children) collect(c.id);
329
+ };
330
+ for (const id of list) if (ctx.map.has(id)) collect(id);
331
+ for (const id of toRemove) {
332
+ const parent = ctx.parentCache.get(id);
333
+ if (parent) {
334
+ const arr = ctx.childrenCache.get(parent.id) ?? [];
335
+ const idx = arr.findIndex((n) => n.id === id);
336
+ if (idx >= 0) arr.splice(idx, 1);
337
+ } else {
338
+ const idx = ctx.roots.findIndex((n) => n.id === id);
339
+ if (idx >= 0) ctx.roots.splice(idx, 1);
340
+ }
341
+ }
342
+ for (const id of toRemove) {
343
+ ctx.childrenCache.delete(id);
344
+ ctx.parentCache.delete(id);
345
+ ctx.map.delete(id);
346
+ ctx.expandedKeys.delete(id);
347
+ }
348
+ for (const r of ctx.roots) setLevelRecursively(r, ctx.childrenCache, 0);
349
+ ctx.notify();
350
+ }
321
351
  /**
322
352
  * 创建 SpliceTree 树实例
323
353
  * - 构建缓存结构
@@ -325,6 +355,7 @@ function moveNode(ctx, id, newParentId, beforeId) {
325
355
  * - 提供插件扩展点(setup/extendNode)
326
356
  */
327
357
  function createSpliceTree(data, options = {}) {
358
+ let pluginCtx;
328
359
  const cfg = options.configuration ?? {};
329
360
  const keyField = cfg.keyField ?? "id";
330
361
  const parentField = cfg.parentField ?? "parent";
@@ -409,6 +440,18 @@ function createSpliceTree(data, options = {}) {
409
440
  notify: emitVisibility
410
441
  }, id, newParentId, beforeId);
411
442
  },
443
+ remove(ids) {
444
+ removeNodes({
445
+ map,
446
+ tree,
447
+ roots,
448
+ keyField,
449
+ parentCache,
450
+ childrenCache,
451
+ expandedKeys,
452
+ notify: emitVisibility
453
+ }, ids);
454
+ },
412
455
  syncData(next) {
413
456
  tree.data = next;
414
457
  const built = buildTree(next, keyField, parentField, expandedKeys);
@@ -423,13 +466,21 @@ function createSpliceTree(data, options = {}) {
423
466
  });
424
467
  for (const id of toDelete) expandedKeys.delete(id);
425
468
  applyNodeExtensions();
469
+ pluginCtx.roots = roots;
470
+ pluginCtx.map = map;
471
+ pluginCtx.parentCache = parentCache;
472
+ pluginCtx.childrenCache = childrenCache;
426
473
  emitVisibility();
427
474
  }
428
475
  };
429
- const pluginCtx = {
476
+ pluginCtx = {
430
477
  tree,
431
478
  options,
432
- events
479
+ events,
480
+ roots,
481
+ map,
482
+ parentCache,
483
+ childrenCache
433
484
  };
434
485
  options?.plugins?.forEach((plugin) => {
435
486
  const api = plugin.setup?.(pluginCtx);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splicetree/adapter-vue",
3
3
  "type": "module",
4
- "version": "3.0.1",
4
+ "version": "3.0.2",
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.1"
33
+ "@splicetree/core": "3.0.2"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public"