Skip to content

Commit

Permalink
feat:add walk utils and fix tree method
Browse files Browse the repository at this point in the history
  • Loading branch information
pomelo-nwu committed Feb 6, 2021
1 parent 65c8a6f commit d1949ba
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
29 changes: 18 additions & 11 deletions packages/graphin/src/utils/Tree.ts
@@ -1,10 +1,16 @@
export interface Node {
id: string;
import { GraphinTreeData } from '../typings/type';

// export interface Node {
// id: string;
// parent?: Node;
// data?: any; // eslint-disable-line
// children: Node[];
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
// [key: string]: any;
// }

export interface Node extends GraphinTreeData {
parent?: Node;
data?: any; // eslint-disable-line
children: Node[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}

export default class Tree {
Expand All @@ -30,7 +36,7 @@ export default class Tree {
if (cb(node)) {
return node;
}
if (node.children.length) {
if (node?.children?.length) {
queue.push(...node.children);
}
}
Expand All @@ -52,7 +58,6 @@ export default class Tree {
private addRoot = (id: string, data?: any) => {
this.root = {
id,
data,
children: [],
};
this.nodeIds.push(id);
Expand All @@ -66,7 +71,7 @@ export default class Tree {
return;
}

let parent;
let parent: Node | undefined;

if (!parentId) {
// If parentId was not given, pick a random node as parent
Expand All @@ -82,9 +87,11 @@ export default class Tree {
}

this.nodeIds.push(id);
parent.children.push({
// @ts-ignore
(parent as Node).children.push({
id,
data,
// @ts-ignore
parent,
children: [],
});
};
Expand Down
2 changes: 2 additions & 0 deletions packages/graphin/src/utils/index.ts
Expand Up @@ -8,6 +8,7 @@ import getComboStyleByTheme from '../theme/combo-style';

import { deepMix } from '@antv/util';
import uuid from './uuid';
import walk from './walk';

export default {
hexToRgba,
Expand All @@ -20,4 +21,5 @@ export default {
getComboStyleByTheme,
deepMerge: deepMix,
uuid,
walk,
};
18 changes: 13 additions & 5 deletions packages/graphin/src/utils/mock.ts
@@ -1,5 +1,7 @@
import Tree from './Tree';
import { IUserNode, IUserEdge, GraphinData } from '../typings/type';
import walk from './walk';

import { IUserNode, IUserEdge, GraphinData, GraphinTreeData } from '../typings/type';

const defaultOptions = {
/** 节点 */
Expand Down Expand Up @@ -146,9 +148,8 @@ export class Mock {
this.treeData.bfs(node => {
if (node.id !== rootId) {
this.edges.push({
source: (node.parent && node.parent.id) as string,
source: node?.parent?.id as string,
target: node.id,
label: `edge-${node.parent && node.parent.id}_${node.id}`,
properties: [],
});
}
Expand Down Expand Up @@ -212,9 +213,16 @@ export class Mock {
};
};

graphinTree = (): GraphinData => {
graphinTree = (): GraphinTreeData => {
const tree = this.treeData.getRoot();
// @ts-ignore
walk(tree, node => {
// @ts-ignore
delete node.parent;
});
// @ts-ignore
return this.treeData.getRoot();
console.log(tree);
return tree as GraphinTreeData;
};
}

Expand Down
12 changes: 12 additions & 0 deletions packages/graphin/src/utils/walk.ts
@@ -0,0 +1,12 @@
import { GraphinTreeData } from '../typings/type';

const walk = (node: GraphinTreeData, callback: (node: GraphinTreeData) => void) => {
callback(node);
if (node.children && node.children.length > 0) {
node.children.forEach(child => {
walk(child, callback);
});
}
};

export default walk;

0 comments on commit d1949ba

Please sign in to comment.