initial graph view
This commit is contained in:
1 parent
12d086a5c0
commit
6d4fe7749a
8 files changed
+131
-48
No files matched your search
@@ -1,4 +1,3 @@
|
||||
import { NODE_HEIGHT, NODE_WIDTH } from "./constants"
|
||||
import { EdgeDirection, Point } from "./types"
|
||||
|
||||
|
||||
@@ -8,14 +7,17 @@ export interface EdgeProp {
|
||||
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]
|
||||
export default ({
|
||||
p1: [x1,y1],
|
||||
p2: [x2,y2],
|
||||
direction,
|
||||
}: EdgeProp) => {
|
||||
const [_,my] = [(x1+x2)/2, (y1+y2)/2]
|
||||
const theta = Math.atan2(y2 -y1, x2-x1) -Math.PI / 2
|
||||
const cx1 = x1 + NODE_WIDTH
|
||||
const theta = Math.atan2(y2 -y1, x2-x1) - Math.PI / 2
|
||||
console.log({theta}, Math.sin(theta))
|
||||
const cx1 = x1 - 45 * Math.sin(theta)
|
||||
const cy1 = my - 30 * Math.cos(theta)
|
||||
const cx2 = x2 - NODE_WIDTH
|
||||
const cx2 = x2 + 45 * Math.sin(theta)
|
||||
const cy2 = my + 30 * Math.cos(theta)
|
||||
|
||||
return <path className="edge"
|
||||
|
||||
@@ -18,15 +18,15 @@ export default ({
|
||||
onXClick=() => {},
|
||||
onOpenClick=() => {},
|
||||
isOpen=false,
|
||||
nodeType,
|
||||
color,
|
||||
}: NodeProp) => <foreignObject x={x} y={y} width={width} height={height}>
|
||||
<div className="automation-dag--node">
|
||||
<div className={`automation-dag--node-inner ${nodeType}`}>
|
||||
<div className="automation-dag--node-inner-edge left" onClick={onXClick}>
|
||||
<div className="automation-editor--node">
|
||||
<div className={`automation-editor--node-inner top-color ${color}`}>
|
||||
<div className="automation-editor--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-editor--node-inner-text">{text}</div>
|
||||
<div className="automation-editor--node-inner-edge right" onClick={onOpenClick}>
|
||||
<div className="automation-node-side-button">{isOpen ? "▲" : "▼"}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
import { AutomationData } from "~/automations/types";
|
||||
import { AutomationCondition } from "~/automations/types/conditions";
|
||||
import { AutomationTrigger } from "~/automations/types/triggers";
|
||||
import { getDescriptionFromAutomationNode } from "~/automations/utils";
|
||||
import useWindowSize from "~/utils/useWindowSize";
|
||||
import AutoInfoBox from "./AutoInfoBox";
|
||||
import { NODE_HEIGHT, NODE_WIDTH } from "./constants";
|
||||
import DAGEdge from "./DAGEdge";
|
||||
import DAGNode from "./DAGNode";
|
||||
import { Point } from "./types";
|
||||
|
||||
interface Props {
|
||||
automation: AutomationData;
|
||||
@@ -11,11 +18,47 @@ export default function AutomationEditor({
|
||||
onUpdate,
|
||||
}: Props) {
|
||||
|
||||
// state
|
||||
const {ratioWbh} = useWindowSize();
|
||||
|
||||
// alias
|
||||
const nHDF = 1.5 // node horizontal distance factor
|
||||
const graphHeight = 300;
|
||||
const graphWidth = Math.round(graphHeight * ratioWbh);
|
||||
console.log({ratioWbh, graphHeight, graphWidth})
|
||||
const pointTrigToCond: Point = [NODE_WIDTH*nHDF, NODE_HEIGHT/2]
|
||||
|
||||
// render funcs
|
||||
const renderTrigger = (trigger: AutomationTrigger, i: number) => {
|
||||
const nodeLoc: Point = [0 ,i*NODE_HEIGHT*1.25];
|
||||
return <>
|
||||
<DAGNode key={`node.${i}`} loc={nodeLoc} text={getDescriptionFromAutomationNode(trigger)} color="red"/>
|
||||
<DAGEdge
|
||||
key={`edge.${i}`}
|
||||
p1={[NODE_WIDTH , nodeLoc[1] + 0.5 * NODE_HEIGHT]}
|
||||
p2={pointTrigToCond}
|
||||
direction="1->2"
|
||||
/>
|
||||
</>
|
||||
}
|
||||
const renderMidCondition = (condition: AutomationCondition, i: number) => {
|
||||
const nodeLoc: Point = [pointTrigToCond[0] + NODE_WIDTH/2 , 0];
|
||||
let lastNodeLoc = pointTrigToCond;
|
||||
if (i > 0) {
|
||||
lastNodeLoc = [nodeLoc[0] + NODE_WIDTH*((i - 1)*nHDF+1), nodeLoc[1] + NODE_HEIGHT/2];
|
||||
nodeLoc[0] += NODE_WIDTH*i*nHDF
|
||||
}
|
||||
return <>
|
||||
<DAGNode key={`node.${i}`} loc={nodeLoc} text={getDescriptionFromAutomationNode(condition)} color="blue"/>
|
||||
<DAGEdge
|
||||
key={`edge.${i}`}
|
||||
p1={lastNodeLoc}
|
||||
p2={[nodeLoc[0], nodeLoc[1] + NODE_HEIGHT/2]}
|
||||
direction="1->2"
|
||||
/>
|
||||
</>
|
||||
}
|
||||
|
||||
// render
|
||||
return <div className="automation-editor" style={{
|
||||
flexDirection: ratioWbh >= 0.75 ? 'row' : 'column'
|
||||
}}>
|
||||
@@ -35,13 +78,15 @@ export default function AutomationEditor({
|
||||
<path d="M 0 0 L 10 5 L 0 10 z" />
|
||||
</marker>
|
||||
</defs>
|
||||
{/* <rect x="1" y="1" height={GRAPH_HEIGHT-1} width={GRAPH_WIDTH} stroke="white" fill="none"/> */}
|
||||
{/* <g className="automation-dag-edges">
|
||||
|
||||
</g>
|
||||
<g className="automation-dag-nodes">
|
||||
|
||||
</g> */}
|
||||
<g className="triggers">
|
||||
{automation.trigger.map(renderTrigger)}
|
||||
</g>
|
||||
<g className="conditions">
|
||||
{automation.condition.map(renderMidCondition)}
|
||||
</g>
|
||||
<circle className="blue" cx={pointTrigToCond[0]} cy={pointTrigToCond[1]} r={2}/>
|
||||
<g className="actions">
|
||||
</g>
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { AutomationNodeTypes } from "../../automations/types";
|
||||
|
||||
|
||||
export type Point = [number, number];
|
||||
export interface Node {
|
||||
loc: Point;
|
||||
text: JSX.Element | string;
|
||||
nodeType: AutomationNodeTypes;
|
||||
color: 'red' | 'blue' | 'green';
|
||||
}
|
||||
export interface Edge {
|
||||
from: string;
|
||||
|
||||
Reference in new issue
Block a user