initial svg

This commit is contained in:
Ari Sosnovsky committed 2021-11-08 15:21:39 -05:00
1 parent c933589998
commit a8c83afaff
22 files changed
+452 -21

No files matched your search

@@ -0,0 +1,37 @@
import { NODE_HEIGHT, NODE_WIDTH } from "./constants"
import { EdgeDirection, Point } from "./types"
export interface EdgeProp {
p1: Point,
p2: Point,
direction: EdgeDirection,
}
export default ({p1, p2, direction}: EdgeProp) => {
const [x1,y1] = [p1[0] + NODE_WIDTH, p1[1]+NODE_HEIGHT/2]
const [x2,y2] = [p2[0], p2[1]+NODE_HEIGHT/2]
const [mx,my] = [(x1+x2)/2, (y1+y2)/2]
const theta = Math.atan2(y2 -y1, x2-x1) -Math.PI / 2
const cx1 = x1 + NODE_WIDTH
const cy1 = my - 30 * Math.cos(theta)
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"
d={ `M${x1} ${y1} C${cx1} ${cy1}, ${cx2} ${cy2}, ${x2} ${y2}`}
fill="none"
markerStart={
(direction === '1<-2' || direction === '1<->2') ?
"url(#arrow)" : ''
}
markerEnd={
(direction === '1->2' || direction === '1<->2') ?
"url(#arrow)" : ''
}
/>
</>
}
@@ -0,0 +1,33 @@
import { NODE_HEIGHT, NODE_WIDTH } from "./constants"
import { Node } from "./types"
export interface NodeProp extends Node {
height?: number;
width?: number;
isOpen?: boolean;
onXClick?: () => void;
onOpenClick?: () => void;
}
export default ({
loc: [x,y],
text,
height=NODE_HEIGHT,
width=NODE_WIDTH,
onXClick=() => {},
onOpenClick=() => {},
isOpen=false,
}: NodeProp) => <foreignObject x={x} y={y} width={width} height={height}>
<div className="automation-dag--node">
<div className="automation-dag--node-inner">
<div className="automation-dag--node-inner-edge left" onClick={onXClick}>
<div className="automation-node-side-button">X</div>
</div>
<div className="automation-dag--node-inner-text">{text}</div>
<div className="automation-dag--node-inner-edge right" onClick={onOpenClick}>
<div className="automation-node-side-button">{isOpen ? "▲" : "▼"}</div>
</div>
</div>
</div>
</foreignObject>
@@ -0,0 +1,7 @@
export const NODE_WIDTH = 50 as const;
export const NODE_HEIGHT = 25 as const;
export const GRAPH_HEIGHT = 300 as const;
export const GRAPH_WIDTH = 300 as const;
@@ -0,0 +1,60 @@
import { useEffect, 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 { 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],
// ])
// useEffect(()=>{
// window.setTimeout(() => {
// setPoints(points.map(randomWalk))
// },1000)
// });
const {nodes, edges} = normalizeDAG(dag);
return <div className="automation-dag" >
<svg
viewBox={[0,0,GRAPH_WIDTH, GRAPH_HEIGHT].join(" ")}
xmlns="http://www.w3.org/2000/svg"
>
<defs>
<marker id="arrow" viewBox="0 0 10 10" refX="10" refY="5"
markerWidth="6" markerHeight="6"
orient="auto-start-reverse">
<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}
/>
</>)}
</svg>
</div>
}
+17
View File
@@ -0,0 +1,17 @@
export type Point = [number, number];
export interface Node {
loc: Point;
text: string;
}
export interface Edge {
from: number;
to: number;
direction: '1->2' | '1<->2' | '1<-2';
}
export type EdgeDirection = Edge['direction'];
export interface DAG {
nodes: Array<Node>;
edges: Array<Edge>;
}
@@ -0,0 +1,16 @@
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
import {clip} from "./utils";
test('it clips overflowing values', () => {
expect(clip([GRAPH_WIDTH, GRAPH_HEIGHT])).toEqual([
GRAPH_WIDTH-NODE_WIDTH,
GRAPH_HEIGHT-NODE_HEIGHT
])
expect(clip([-10, -10])).toEqual([
0, 0
])
expect(clip([0, 0])).toEqual([
0, 0
])
})
+29
View File
@@ -0,0 +1,29 @@
import { GRAPH_HEIGHT, GRAPH_WIDTH, NODE_HEIGHT, NODE_WIDTH } from "./constants";
import {DAG, Point} from "./types";
export const clip = (p: Point) : Point => {
const [x,y] = p.map(c => Math.max(0, c));
return [Math.min(GRAPH_WIDTH-NODE_WIDTH, x), Math.min(GRAPH_HEIGHT-NODE_HEIGHT, y)]
}
export interface NormalizedDag extends DAG {
// overflow: boolean;
}
export const normalizeDAG = (dag: DAG): NormalizedDag => {
// let overflow: bo
const nodes = dag.nodes.map(n => ({
...n,
loc: clip(n.loc),
}));
// dag.edges.forEach(edge => {
// const n1 = nodes[edge.from];
// const n2 = nodes[edge.to];
// n2.loc[0] = Math.max(n2.loc[0], n1.loc[0] + 1.25*NODE_WIDTH)
// })
return {
nodes,
edges: dag.edges,
}
}
@@ -0,0 +1,20 @@
import { useState } from "react";
import { AutomationCondition } from "../../automation/types/condition";
import InputList from "../InputList";
export interface Props {
condition: AutomationCondition
}
export default function ConditionNode({
condition
}: Props) {
return <div className="condition-node">
{/* <InputList
current={condition.condition}
options={}
/> */}
</div>
}
@@ -1,7 +1,7 @@
import StateManager from "../../automation/StateManager";
import { AutomationAction } from "../../automation/types";
import { determineNodeTypes, getNameFromAction } from "../../automation/utils";
import AutomationNode from "../Node";
import AutomationNode from "../AutomationNode";
export interface Props {
@@ -1,7 +1,7 @@
import { useState } from "react";
import * as types from "../../automation/types";
import { getNameFromAction } from "../../automation/utils";
import AutomationNode from "../Node";
import AutomationNode from "../AutomationNode";
import NewNodeModal from "./NewNodeModal";
import ScriptNode from "./ScriptNode";
+1 -1
View File
@@ -23,7 +23,7 @@ export default function InputYaml<T>({
delayId.current = null;
}
delayId.current = window.setTimeout(() => {
onChange(yaml.load(update))
onChange(yaml.load(update) as any)
}, 1000);
}
return <InputWrapper label={label}>