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

-1
View File
@@ -46,7 +46,6 @@ export interface GenericCondition extends BaseCondition {
condition: string;
}
export type AutomationCondition =
| string // <-- this is the same as "TemplateCondition"
| LogicCondition
| NumericCondition
| StateCondition
@@ -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}>
+20 -2
View File
@@ -1,10 +1,28 @@
import AutomationDAG from "../components/AutomationDAG";
import AutomationEditor from "../components/AutomationEditor";
import sample from "../sample/auto_01.yaml";
export function Home() {
return (
<div id="page--home" className="page">
<div
<AutomationDAG
nodes={[
{text: '1', loc:[-10,-50]},
{text: '2', loc:[0,50]},
{text: '3', loc:[-10,200]},
]}
edges={[
{
from: 0, to: 1,
direction: '1->2',
},
{
from: 0, to: 2,
direction: '1->2',
},
]}
/>
{/* <div
style={{
display: "flex",
flexDirection: "row",
@@ -14,7 +32,7 @@ export function Home() {
}}
>
<AutomationEditor automation={sample}/>
</div>
</div> */}
</div>
);
}
-5
View File
@@ -19,11 +19,6 @@ export default function App() {
<Switch>
<Route path="/">
<Home />
{/* <NewNodeModal
node_type="action"
onClose={()=>{}}
onCreate={()=>{}}
/> */}
</Route>
</Switch>
</div>
+9 -1
View File
@@ -6,7 +6,15 @@ trigger:
device_id: bad6b55ae91b7ced810fd5310c0a0fce
entity_id: binary_sensor.door_balcony
domain: binary_sensor
condition: []
condition:
- condition: and
conditions:
- condition: not
conditions:
- condition: template
value_template: "{{ states('switch.plug_out_bug_lamp') == 'unavailable' }}"
- condition: time
after: "10:00:00"
action:
- choose:
- conditions:
+1 -2
View File
@@ -1,3 +1,2 @@
import 'jest-extended'
import 'jest-dom/extend-expect'
import 'react-testing-library/cleanup-after-each'
import '@testing-library/jest-dom'
@@ -0,0 +1,69 @@
.automation-dag {
height: 99%;
width: 99%;
border: 1px solid $secondary-accent;
svg {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
.edge {
stroke: $secondary-accent;
}
#arrow > path {
fill: $secondary-accent;
}
&--node {
display: flex;
flex-direction: row;
background-color: $secondary;
border-radius: 1em;
height: 100%;
max-height: 100%;
max-width: 100%;
width: 100%;
font-size: 0.5em;
&-inner {
display: flex;
flex-direction: row;
flex: 1;
&-text {
flex: 2;
display: flex;
text-align: center;
justify-content: center;
align-items: center;
}
&-edge {
flex: 1;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
border-radius: 1em;
justify-content: space-around;
align-items: center;
&.left {
border-left: 1px $secondary-accent solid;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
&.right {
border-right: 1px $secondary-accent solid;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
&:hover {
background-color: $primary-accent;
cursor: pointer;
}
}
}
}
}
+1
View File
@@ -2,6 +2,7 @@
@import "./AutomationEditor.scss";
@import "./NewNodeModal.scss";
@import "./InputWrapper.scss";
@import "./AutomationDAG.scss";
* {
transition: all 200ms ease-in;
+2
View File
@@ -6,6 +6,8 @@
overflow: hidden;
font-size: 16px;
color: $font-color-primary;
max-width: 100vw;
max-height: 100vh;
display: flex;
flex-direction: column;