@zhongguo168a/yxeditor-common 0.0.78 → 0.0.79

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
@@ -454,10 +454,18 @@ declare class TreeNode<T = any> {
454
454
  */
455
455
  walkRemove(f: (node: TreeNode) => boolean): void;
456
456
  private _walkRemove;
457
+ flat(): TreeNode[];
458
+ /**
459
+ * 广度优先遍历(BFS):从当前节点开始,逐层遍历子树
460
+ * @param f - 节点处理函数,返回 false 表示停止遍历
461
+ * @param inChild - 决定是否将子节点加入队列:若提供,调用 inChild(node),返回 true 才加入其子节点
462
+ */
463
+ walkBFS(f: (node: TreeNode) => boolean, inChild?: (node: TreeNode) => boolean): boolean;
457
464
  /**
458
465
  * 如果返回true则继续,包含当前节点
466
+ * 深度优先遍历(DFS)
459
467
  * @param f
460
- * @param inChild
468
+ * @param inChild 如果返回true,则继续遍历, 否则不进入节点的子节点
461
469
  */
462
470
  walk(f: (node: TreeNode) => boolean, inChild?: (node: TreeNode) => boolean): void;
463
471
  private _walk;
package/dist/index.esm.js CHANGED
@@ -2036,10 +2036,53 @@ class TreeNode {
2036
2036
  }
2037
2037
  return true;
2038
2038
  }
2039
+ flat() {
2040
+ let result = [];
2041
+ this.walkBFS((node) => {
2042
+ result.push(node);
2043
+ return true;
2044
+ });
2045
+ return result;
2046
+ }
2047
+ /**
2048
+ * 广度优先遍历(BFS):从当前节点开始,逐层遍历子树
2049
+ * @param f - 节点处理函数,返回 false 表示停止遍历
2050
+ * @param inChild - 决定是否将子节点加入队列:若提供,调用 inChild(node),返回 true 才加入其子节点
2051
+ */
2052
+ walkBFS(f, inChild) {
2053
+ var _a;
2054
+ const queue = [this]; // 初始化队列
2055
+ while (queue.length > 0) {
2056
+ const node = queue.shift(); // 取出队首节点
2057
+ try {
2058
+ // 1. 处理当前节点
2059
+ if (!f(node)) {
2060
+ return false; // 停止遍历
2061
+ }
2062
+ }
2063
+ catch (e) {
2064
+ console.warn('walkBFS error:', e);
2065
+ return false;
2066
+ }
2067
+ // 2. 决定是否展开子节点
2068
+ const shouldEnqueueChildren = inChild ? inChild(node) : true;
2069
+ if (!shouldEnqueueChildren || node.isLeaf) {
2070
+ continue;
2071
+ }
2072
+ // 3. 将子节点加入队列(从左到右)
2073
+ if ((_a = node.children) === null || _a === void 0 ? void 0 : _a.length) {
2074
+ for (const child of node.children) {
2075
+ queue.push(child);
2076
+ }
2077
+ }
2078
+ }
2079
+ return true; // 遍历完成
2080
+ }
2039
2081
  /**
2040
2082
  * 如果返回true则继续,包含当前节点
2083
+ * 深度优先遍历(DFS)
2041
2084
  * @param f
2042
- * @param inChild
2085
+ * @param inChild 如果返回true,则继续遍历, 否则不进入节点的子节点
2043
2086
  */
2044
2087
  walk(f, inChild) {
2045
2088
  this._walk(f, inChild);