This commit is contained in:
Ari Sosnovsky committed 2021-11-08 19:46:06 -05:00
1 parent a8c83afaff
commit 8b4c8aec22
6 files changed
+104 -56

No files matched your search

+1 -5
View File
@@ -18,10 +18,7 @@ export default ({p1, p2, direction}: EdgeProp) => {
const cx2 = x2 - NODE_WIDTH
const cy2 = my + 30 * Math.cos(theta)
return <>
<circle cx={cx1} cy={cy1} stroke="red" r="1"/>
<circle cx={cx2} cy={cy2} stroke="blue" r="1"/>
<path className="edge"
return <path className="edge"
d={ `M${x1} ${y1} C${cx1} ${cy1}, ${cx2} ${cy2}, ${x2} ${y2}`}
fill="none"
markerStart={
@@ -33,5 +30,4 @@ export default ({p1, p2, direction}: EdgeProp) => {
"url(#arrow)" : ''
}
/>
</>
}
+30 -34
View File
@@ -1,36 +1,29 @@
import { useEffect, useState } from "react"
import { useEffect, useRef, useState } from "react"
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_WIDTH } from "./constants";
import DAGEdge from "./DAGEdge";
import DAGNode from "./DAGNode";
import { DAG, Point } from "./types";
import { DAG, Node } from "./types";
import { normalizeDAG } from "./utils";
const randomWalk = ([x,y]:Point) : Point => [
Math.round(Math.max(0, Math.min(x + (Math.random() < 0.5 ? -1 : 1)* 10*Math.random(), 300))),
Math.round(Math.max(0, Math.min(y + (Math.random() < 0.5 ? -1 : 1)* 10*Math.random(), 300))),
]
export interface Props extends DAG {
}
export default function AutomationDAG({
...dag
}: Props) {
// const [mx, my] = [10, 100];
// const [points, setPoints] = useState<Array<Point>>([
// [10+1.25*NODE_WIDTH, 25],
// [10+2*NODE_WIDTH, 100],
// [25+1.5*NODE_WIDTH, 170],
// [25+2*NODE_WIDTH, 170],
// ])
// state
const [{nodes, edges}, _setDAG] = useState(normalizeDAG(dag));
// useEffect(()=>{
// window.setTimeout(() => {
// setPoints(points.map(randomWalk))
// },1000)
// });
const {nodes, edges} = normalizeDAG(dag);
// set state
const setDAG = (dag: DAG) => _setDAG(normalizeDAG(dag));
const removeNode = (nodeId: string) => () => {
const newNodes = {...nodes}
delete newNodes[nodeId];
_setDAG({
nodes: newNodes,
edges: edges.filter(e => e.from !== nodeId && e.to !== nodeId),
})
}
// render
return <div className="automation-dag" >
<svg
viewBox={[0,0,GRAPH_WIDTH, GRAPH_HEIGHT].join(" ")}
@@ -43,18 +36,21 @@ export default function AutomationDAG({
<path d="M 0 0 L 10 5 L 0 10 z" />
</marker>
</defs>
<circle stroke="red" r="1" cx="0" cy="0"/>
{nodes.map((node, i) => <>
<DAGNode key={i} {...node}/>
</>)}
{edges.map(({from, to, direction}) => <>
<DAGEdge
key={`${from}-${to}`}
p1={nodes[from].loc}
p2={nodes[to].loc}
direction={direction}
/>
</>)}
<rect x="1" y="1" height={GRAPH_HEIGHT-1} width={GRAPH_WIDTH} stroke="white" fill="none"/>
<g className="automation-dag-edges">
{edges.map(({from, to, direction}, i) => <DAGEdge
key={i}
p1={nodes[from].loc}
p2={nodes[to].loc}
direction={direction}
/>
)}
</g>
<g className="automation-dag-nodes">
{Object.keys(nodes).map((nodeId) =>
<DAGNode key={nodeId} {...nodes[nodeId]} onXClick={removeNode(nodeId)}/>
)}
</g>
</svg>
</div>
}
+3 -3
View File
@@ -6,12 +6,12 @@ export interface Node {
text: string;
}
export interface Edge {
from: number;
to: number;
from: string;
to: string;
direction: '1->2' | '1<->2' | '1<-2';
}
export type EdgeDirection = Edge['direction'];
export interface DAG {
nodes: Array<Node>;
nodes: Record<string,Node>;
edges: Array<Edge>;
}
+8 -5
View File
@@ -1,6 +1,6 @@
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
import {DAG, Point} from "./types";
import {DAG, Node, Point} from "./types";
export const clip = (p: Point) : Point => {
@@ -12,10 +12,13 @@ export interface NormalizedDag extends DAG {
}
export const normalizeDAG = (dag: DAG): NormalizedDag => {
// let overflow: bo
const nodes = dag.nodes.map(n => ({
...n,
loc: clip(n.loc),
}));
const nodes = Object.keys(dag.nodes).map<[string, Node]>(nodeId => [nodeId, {
...dag.nodes[nodeId],
loc: clip(dag.nodes[nodeId].loc),
}]).reduce((all, [nodeId, next]) => ({
[nodeId]: next,
...all,
}),{});
// dag.edges.forEach(edge => {
// const n1 = nodes[edge.from];
// const n2 = nodes[edge.to];