Skip to content

Commit 33ceabf

Browse files
committed
Was able to get the nodes to render on the screen using d3, still working on adding edges and getting the positioning of the nodes to represent a tree shape.
1 parent 972639e commit 33ceabf

File tree

9 files changed

+462
-10
lines changed

9 files changed

+462
-10
lines changed

package-lock.json

Lines changed: 330 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"babel": "^6.23.0",
9999
"babel-loader": "^9.1.3",
100100
"css-loader": "^6.8.1",
101+
"d3": "^7.8.5",
101102
"react": "^18.2.0",
102103
"react-dom": "^18.2.0",
103104
"reactflow": "^11.10.1",

src/getNonce 2.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function getNonce() {
2+
let text = "";
3+
const possible =
4+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
5+
for (let i = 0; i < 32; i++) {
6+
text += possible.charAt(Math.floor(Math.random() * possible.length));
7+
}
8+
return text;
9+
}
10+
11+
module.exports = { getNonce }

src/parser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class Parser {
7171
reactRouter: false,
7272
reduxConnect: false,
7373
children: [],
74+
parent: null,
7475
parentList: [],
7576
props: {},
7677
error: '',
@@ -180,6 +181,9 @@ export class Parser {
180181
if (componentTree.parentList.includes(componentTree.filePath)) {
181182
return;
182183
}
184+
// if (typeof componentTree.parentList === 'string' && componentTree.parentList.includes(componentTree.filePath)) {
185+
// return;
186+
// }
183187

184188
// Create abstract syntax tree of current component tree file
185189
let ast: babel.ParseResult<File>;
@@ -468,6 +472,7 @@ export class Parser {
468472
count: 1,
469473
props: props,
470474
children: [],
475+
parent: parent.id,
471476
// consider adding the id to the parentList array somehow for D3 integration...
472477
parentList: [parent.filePath].concat(parent.parentList),
473478
error: '',

src/treeTemplates/tree 2.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Tree = {
2+
id: undefined,
3+
fileName: undefined,
4+
filePath: undefined,
5+
// children & parentList should be populated with other Tree objects
6+
children: [],
7+
parentList: [],
8+
isClientComponent: false
9+
}
10+
11+
module.exports = { Tree };

src/types/hierarchyData.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Tree } from "./tree"
2+
3+
4+
export interface hierarchyData {
5+
children?: hierarchyData[],
6+
data: Tree,
7+
depth: number,
8+
height: number,
9+
parent: hierarchyData | null
10+
}

src/types/tree.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type Tree = {
1111
reactRouter: boolean;
1212
reduxConnect: boolean;
1313
children: Tree[];
14+
parent: string;
1415
parentList: string[];
1516
props: { [key: string]: boolean; };
1617
error: string;

src/webview/Flow.tsx

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useEffect } from "react";
1+
import React, { useCallback, useEffect, useRef } from "react";
22
import ReactFlow, {
33
addEdge,
44
MiniMap,
@@ -12,13 +12,19 @@ import ReactFlow, {
1212
import "reactflow/dist/style.css";
1313
import { ConnectionLineType } from "../types/connection";
1414
import FlowBuilder from './flowBuilder';
15+
import * as d3 from 'd3';
16+
import { Tree } from "../types/tree";
17+
import { hierarchyData } from "../types/hierarchyData";
1518

1619
const onInit = (reactFlowInstance: ReactFlowInstance) =>
1720
console.log("flow loaded:", reactFlowInstance);
1821

1922
const OverviewFlow = () => {
23+
const reactFlowWrapper = useRef<HTMLDivElement>(null);
2024
const initialNodes = [];
2125
const initialEdges = [];
26+
const elements = [];
27+
const edge = [];
2228

2329
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
2430
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
@@ -33,26 +39,74 @@ const OverviewFlow = () => {
3339
const msg = e.data; // object containing type prop and value prop
3440
switch (msg.type) {
3541
case 'parsed-data': {
36-
const results = new FlowBuilder(msg.value);
37-
results.build(msg.settings)
38-
setNodes(results.initialNodes);
39-
setEdges(results.initialEdges);
42+
// const results = new FlowBuilder(msg.value);
43+
// results.build(msg.settings)
44+
let data : object | undefined = msg.value;
45+
console.log('data', data)
46+
// setNodes(results.initialNodes);
47+
// setEdges(results.initialEdges);
48+
49+
// create a holder for the heirarchical data (msg.value), data comes in an object of all the Trees
50+
const root : any = d3.hierarchy(data)
51+
console.log('root', root)
52+
53+
54+
//create tree layout and give nodes their positions
55+
const treeLayout = d3.tree()
56+
treeLayout(root);
57+
58+
root.each((node: any) : void => {
59+
// console.log('node name',Object.keys(node))
60+
elements.push({
61+
id: node.data.name,
62+
type: 'default',
63+
data: { label: node.data.name },
64+
position: { x: node.x || 0, y: node.y || 0 },
65+
});
66+
67+
// figure out how to get edges to connect
68+
if (node.data.parent) {
69+
// elements.push(addEdge({
70+
// id: node.data.id,
71+
// source: node.data.id,
72+
// target: node.data.parent,
73+
// // sourceHandle: null,
74+
// // targetHandle: null,
75+
// }))
76+
}
77+
})
4078
break;
4179
}
4280
}
4381
});
82+
83+
setNodes(elements);
84+
85+
//Rendering reactFlow
86+
if (reactFlowWrapper.current) {
87+
// Set initial position for the nodes
88+
const initialPosition = { x: 0, y: 0 };
89+
// reactFlowWrapper.current?.fitView(initialPosition);
90+
}
91+
// assign root variable using d3.heirarchy(data[0]), this will use d3 to format our data in a easy way to visualize the heirarchy
92+
// const elements: Tree[];
93+
4494
}, []);
4595

4696
return (
97+
<div style={{ height: '600px', width: '100%' }}>
4798
<ReactFlow
4899
nodes={nodes}
49-
edges={edges}
100+
// edges={edges}
101+
// nodes={elements}
102+
// edges={edge}
50103
onNodesChange={onNodesChange}
51104
onEdgesChange={onEdgesChange}
52105
onConnect={onConnect}
53106
onInit={onInit}
54107
fitView
55108
attributionPosition="top-right"
109+
style={{ width: '100%', height: '100%' }}
56110
>
57111
<MiniMap
58112
nodeStrokeColor={(n): string => {
@@ -80,6 +134,7 @@ const OverviewFlow = () => {
80134
<Controls />
81135
<Background color="#aaa" gap={16} />
82136
</ReactFlow >
137+
</div>
83138
);
84139
};
85140

webpack.config 2.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const path = require('path')
2+
3+
4+
module.exports = {
5+
entry: './src/webview/index.jsx',
6+
output: {
7+
filename: 'bundle.js',
8+
path: path.resolve(__dirname, './build'),
9+
},
10+
resolve: {
11+
extensions: ['.js', '.jsx', '.css'],
12+
},
13+
module: {
14+
rules: [
15+
{
16+
test: /\.jsx?/,
17+
exclude: /node_modules/,
18+
use: {
19+
loader: "babel-loader",
20+
options: {
21+
presets: ['@babel/preset-env', '@babel/preset-react']
22+
}
23+
}
24+
},
25+
{
26+
test: /\.(scss|css)$/,
27+
use: ["style-loader", "css-loader"],
28+
},
29+
]
30+
},
31+
mode: "development"
32+
}

0 commit comments

Comments
 (0)