@splicetree/core 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.
package/dist/index.d.ts CHANGED
@@ -216,6 +216,11 @@ interface SpliceTreeInstance<T = SpliceTreeData> {
216
216
  * @param beforeId 插入到指定兄弟节点之前(不传表示末尾)
217
217
  */
218
218
  moveNode: (id: string, newParentId: string | undefined, beforeId?: string) => void;
219
+ /**
220
+ * 移除指定节点或节点列表(包含其子孙)
221
+ * @param id 单个 id 或 id 数组
222
+ */
223
+ remove: (id: string | string[]) => void;
219
224
  syncData: (next: T[]) => void;
220
225
  }
221
226
  /**
@@ -237,6 +242,13 @@ interface SpliceTreePluginContext<T = SpliceTreeData> {
237
242
  * 事件总线
238
243
  */
239
244
  events: SpliceTreeEvents;
245
+ /**
246
+ * 插件可访问的内部缓存
247
+ */
248
+ roots: SpliceTreeNode<T>[];
249
+ map: Map<string, SpliceTreeNode<T>>;
250
+ parentCache: Map<string, SpliceTreeNode<T> | undefined>;
251
+ childrenCache: Map<string, SpliceTreeNode<T>[]>;
240
252
  }
241
253
  /**
242
254
  * 插件定义接口
package/dist/index.js CHANGED
@@ -328,6 +328,36 @@ function moveNode(ctx, id, newParentId, beforeId) {
328
328
  setLevelRecursively(node, ctx.childrenCache, node.level);
329
329
  ctx.notify();
330
330
  }
331
+ function removeNodes(ctx, ids) {
332
+ const list = Array.isArray(ids) ? ids : [ids];
333
+ const toRemove = /* @__PURE__ */ new Set();
334
+ const collect = (id) => {
335
+ if (toRemove.has(id)) return;
336
+ toRemove.add(id);
337
+ const children = ctx.childrenCache.get(id) ?? [];
338
+ for (const c of children) collect(c.id);
339
+ };
340
+ for (const id of list) if (ctx.map.has(id)) collect(id);
341
+ for (const id of toRemove) {
342
+ const parent = ctx.parentCache.get(id);
343
+ if (parent) {
344
+ const arr = ctx.childrenCache.get(parent.id) ?? [];
345
+ const idx = arr.findIndex((n) => n.id === id);
346
+ if (idx >= 0) arr.splice(idx, 1);
347
+ } else {
348
+ const idx = ctx.roots.findIndex((n) => n.id === id);
349
+ if (idx >= 0) ctx.roots.splice(idx, 1);
350
+ }
351
+ }
352
+ for (const id of toRemove) {
353
+ ctx.childrenCache.delete(id);
354
+ ctx.parentCache.delete(id);
355
+ ctx.map.delete(id);
356
+ ctx.expandedKeys.delete(id);
357
+ }
358
+ for (const r of ctx.roots) setLevelRecursively(r, ctx.childrenCache, 0);
359
+ ctx.notify();
360
+ }
331
361
 
332
362
  //#endregion
333
363
  //#region src/use.ts
@@ -338,6 +368,7 @@ function moveNode(ctx, id, newParentId, beforeId) {
338
368
  * - 提供插件扩展点(setup/extendNode)
339
369
  */
340
370
  function createSpliceTree(data, options = {}) {
371
+ let pluginCtx;
341
372
  const cfg = options.configuration ?? {};
342
373
  const keyField = cfg.keyField ?? "id";
343
374
  const parentField = cfg.parentField ?? "parent";
@@ -422,6 +453,18 @@ function createSpliceTree(data, options = {}) {
422
453
  notify: emitVisibility
423
454
  }, id, newParentId, beforeId);
424
455
  },
456
+ remove(ids) {
457
+ removeNodes({
458
+ map,
459
+ tree,
460
+ roots,
461
+ keyField,
462
+ parentCache,
463
+ childrenCache,
464
+ expandedKeys,
465
+ notify: emitVisibility
466
+ }, ids);
467
+ },
425
468
  syncData(next) {
426
469
  tree.data = next;
427
470
  const built = buildTree(next, keyField, parentField, expandedKeys);
@@ -436,13 +479,21 @@ function createSpliceTree(data, options = {}) {
436
479
  });
437
480
  for (const id of toDelete) expandedKeys.delete(id);
438
481
  applyNodeExtensions();
482
+ pluginCtx.roots = roots;
483
+ pluginCtx.map = map;
484
+ pluginCtx.parentCache = parentCache;
485
+ pluginCtx.childrenCache = childrenCache;
439
486
  emitVisibility();
440
487
  }
441
488
  };
442
- const pluginCtx = {
489
+ pluginCtx = {
443
490
  tree,
444
491
  options,
445
- events
492
+ events,
493
+ roots,
494
+ map,
495
+ parentCache,
496
+ childrenCache
446
497
  };
447
498
  options?.plugins?.forEach((plugin) => {
448
499
  const api = plugin.setup?.(pluginCtx);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splicetree/core",
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"