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 cx2 = x2 - NODE_WIDTH
const cy2 = my + 30 * Math.cos(theta) const cy2 = my + 30 * Math.cos(theta)
return <> return <path className="edge"
<circle cx={cx1} cy={cy1} stroke="red" r="1"/>
<circle cx={cx2} cy={cy2} stroke="blue" r="1"/>
<path className="edge"
d={ `M${x1} ${y1} C${cx1} ${cy1}, ${cx2} ${cy2}, ${x2} ${y2}`} d={ `M${x1} ${y1} C${cx1} ${cy1}, ${cx2} ${cy2}, ${x2} ${y2}`}
fill="none" fill="none"
markerStart={ markerStart={
@@ -33,5 +30,4 @@ export default ({p1, p2, direction}: EdgeProp) => {
"url(#arrow)" : '' "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 { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_WIDTH } from "./constants";
import DAGEdge from "./DAGEdge"; import DAGEdge from "./DAGEdge";
import DAGNode from "./DAGNode"; import DAGNode from "./DAGNode";
import { DAG, Point } from "./types"; import { DAG, Node } from "./types";
import { normalizeDAG } from "./utils"; 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 interface Props extends DAG {
} }
export default function AutomationDAG({ export default function AutomationDAG({
...dag ...dag
}: Props) { }: Props) {
// const [mx, my] = [10, 100]; // state
// const [points, setPoints] = useState<Array<Point>>([ const [{nodes, edges}, _setDAG] = useState(normalizeDAG(dag));
// [10+1.25*NODE_WIDTH, 25],
// [10+2*NODE_WIDTH, 100],
// [25+1.5*NODE_WIDTH, 170],
// [25+2*NODE_WIDTH, 170],
// ])
// useEffect(()=>{ // set state
// window.setTimeout(() => { const setDAG = (dag: DAG) => _setDAG(normalizeDAG(dag));
// setPoints(points.map(randomWalk)) const removeNode = (nodeId: string) => () => {
// },1000) const newNodes = {...nodes}
// }); delete newNodes[nodeId];
_setDAG({
const {nodes, edges} = normalizeDAG(dag); nodes: newNodes,
edges: edges.filter(e => e.from !== nodeId && e.to !== nodeId),
})
}
// render
return <div className="automation-dag" > return <div className="automation-dag" >
<svg <svg
viewBox={[0,0,GRAPH_WIDTH, GRAPH_HEIGHT].join(" ")} 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" /> <path d="M 0 0 L 10 5 L 0 10 z" />
</marker> </marker>
</defs> </defs>
<circle stroke="red" r="1" cx="0" cy="0"/> <rect x="1" y="1" height={GRAPH_HEIGHT-1} width={GRAPH_WIDTH} stroke="white" fill="none"/>
{nodes.map((node, i) => <> <g className="automation-dag-edges">
<DAGNode key={i} {...node}/> {edges.map(({from, to, direction}, i) => <DAGEdge
</>)} key={i}
{edges.map(({from, to, direction}) => <> p1={nodes[from].loc}
<DAGEdge p2={nodes[to].loc}
key={`${from}-${to}`} direction={direction}
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> </svg>
</div> </div>
} }
+3 -3
View File
@@ -6,12 +6,12 @@ export interface Node {
text: string; text: string;
} }
export interface Edge { export interface Edge {
from: number; from: string;
to: number; to: string;
direction: '1->2' | '1<->2' | '1<-2'; direction: '1->2' | '1<->2' | '1<-2';
} }
export type EdgeDirection = Edge['direction']; export type EdgeDirection = Edge['direction'];
export interface DAG { export interface DAG {
nodes: Array<Node>; nodes: Record<string,Node>;
edges: Array<Edge>; edges: Array<Edge>;
} }
+8 -5
View File
@@ -1,6 +1,6 @@
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants"; 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 => { export const clip = (p: Point) : Point => {
@@ -12,10 +12,13 @@ export interface NormalizedDag extends DAG {
} }
export const normalizeDAG = (dag: DAG): NormalizedDag => { export const normalizeDAG = (dag: DAG): NormalizedDag => {
// let overflow: bo // let overflow: bo
const nodes = dag.nodes.map(n => ({ const nodes = Object.keys(dag.nodes).map<[string, Node]>(nodeId => [nodeId, {
...n, ...dag.nodes[nodeId],
loc: clip(n.loc), loc: clip(dag.nodes[nodeId].loc),
})); }]).reduce((all, [nodeId, next]) => ({
[nodeId]: next,
...all,
}),{});
// dag.edges.forEach(edge => { // dag.edges.forEach(edge => {
// const n1 = nodes[edge.from]; // const n1 = nodes[edge.from];
// const n2 = nodes[edge.to]; // const n2 = nodes[edge.to];
+23 -9
View File
@@ -1,23 +1,37 @@
import AutomationDAG from "../components/AutomationDAG"; import AutomationDAG from "../components/AutomationDAG";
import AutomationEditor from "../components/AutomationEditor"; // import AutomationEditor from "../components/AutomationEditor";
import sample from "../sample/auto_01.yaml"; // import sample from "../sample/auto_01.yaml";
export function Home() { export function Home() {
return ( return (
<div id="page--home" className="page"> <div id="page--home" className="page">
<AutomationDAG <AutomationDAG
nodes={[ nodes={{
{text: '1', loc:[-10,-50]}, 0: {text: '1', loc:[-10,-50]},
{text: '2', loc:[0,50]}, 1: {text: '2', loc:[0,50]},
{text: '3', loc:[-10,200]}, 2: {text: '3', loc:[-10,200]},
]} 3: {text: '4', loc:[100,200]},
4: {text: '6', loc:[200,200]},
}}
edges={[ edges={[
{ {
from: 0, to: 1, from: "0", to: "1",
direction: '1->2', direction: '1->2',
}, },
{ {
from: 0, to: 2, from: "0", to: "2",
direction: '1->2',
},
{
from: "2", to: "3",
direction: '1->2',
},
{
from: "1", to: "4",
direction: '1->2',
},
{
from: "0", to: "3",
direction: '1->2', direction: '1->2',
}, },
]} ]}
+39
View File
@@ -0,0 +1,39 @@
import { useState, useEffect } from "react";
// Define general type for useWindowSize hook, which includes width and height
interface Size {
width: number;
height: number;
}
// Hook
export default function useWindowSize(): Size {
// Initialize state with undefined width/height so server and client renders match
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
const [windowSize, setWindowSize] = useState<Size>({
width: window.innerWidth,
height: window.innerHeight,
});
useEffect(() => {
// Handler to call on window resize
function handleResize() {
// Set window width/height to state
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
// Add event listener
window.addEventListener("resize", handleResize);
// Call handler right away so state gets updated with initial window size
handleResize();
// Remove event listener on cleanup
return () => window.removeEventListener("resize", handleResize);
}, []); // Empty array ensures that effect is only run on mount
return windowSize;
}