simple two node dag
This commit is contained in:
1 parent
d8747a7ee2
commit
69edca7685
11 files changed
+431
-15
No files matched your search
@@ -27,10 +27,6 @@ export const DAGEdge = ({
|
||||
cx1 = x1 + 2 * l1 * Math.cos(theta)
|
||||
cy2 = y2 + 0.5 * l2 * Math.sin(theta)
|
||||
cx2 = x2 - 2 * l2 * Math.cos(theta)
|
||||
} else if (theta === Math.PI) {
|
||||
cy1 *= 1.5
|
||||
cx1 = x1 - 2 * l1 * Math.cos(theta)
|
||||
cy2 *= 0.9
|
||||
} else if ((theta > Math.PI / 2) && (theta < Math.PI)) {
|
||||
cy2 += l2 * Math.sin(theta)
|
||||
cx2 += 10 * l2 * Math.cos(theta)
|
||||
|
||||
@@ -25,19 +25,29 @@ export const Basic: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
<DAGNode {...args}/>
|
||||
</SVGBoard>
|
||||
|
||||
export const WithEdge: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
export const WithFromEdge: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
graphHeight={GRAPH_HEIGHT}
|
||||
>
|
||||
<DAGNode {...args} />
|
||||
</SVGBoard>
|
||||
WithEdge.args = {
|
||||
...(WithEdge.args ?? {}),
|
||||
WithFromEdge.args = {
|
||||
...(WithFromEdge.args ?? {}),
|
||||
edge: {
|
||||
color: 'white',
|
||||
loc: [ NODE_WIDTH * 4, NODE_HEIGHT * 2 ]
|
||||
loc: [NODE_WIDTH * 4, NODE_HEIGHT * 2],
|
||||
direction: '1->2',
|
||||
}
|
||||
}
|
||||
|
||||
export const WithToEdge: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
graphHeight={GRAPH_HEIGHT}
|
||||
>
|
||||
<DAGNode {...args} edge={{
|
||||
color: 'white',
|
||||
loc: [args.loc[0] - args.width, args.loc[1] + args.height / 2],
|
||||
direction: '1<-2',
|
||||
}}/>
|
||||
</SVGBoard>
|
||||
export const WithManyEdges: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
graphHeight={GRAPH_HEIGHT}
|
||||
>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import Color from "chroma-js";
|
||||
import { PencilIcon } from "components/Icons"
|
||||
import { useNodeStyles } from "./styles"
|
||||
import { Node, Point } from "types/graphs";
|
||||
import { Edge, Node, Point } from "types/graphs";
|
||||
import { DAGEdge } from "./DAGEdge";
|
||||
|
||||
|
||||
@@ -11,9 +11,11 @@ export interface NodeProp extends Node {
|
||||
width: number;
|
||||
onXClick?: () => void;
|
||||
onOpenClick?: () => void;
|
||||
debug?: boolean;
|
||||
edge?: {
|
||||
color: string;
|
||||
loc: Point;
|
||||
direction: Edge['direction'];
|
||||
}
|
||||
}
|
||||
export const DAGNode = ({
|
||||
@@ -25,6 +27,7 @@ export const DAGNode = ({
|
||||
onOpenClick = () => { },
|
||||
color,
|
||||
edge,
|
||||
debug=false,
|
||||
}: NodeProp) => {
|
||||
const { classes, theme } = useNodeStyles({
|
||||
color,
|
||||
@@ -51,11 +54,22 @@ export const DAGNode = ({
|
||||
</div>
|
||||
</div>
|
||||
</foreignObject>
|
||||
{edge && <DAGEdge
|
||||
p1={[x + width, y + height/ 2]}
|
||||
p2={edge.loc}
|
||||
direction="1->2"
|
||||
color={edge.color}
|
||||
/>}
|
||||
{
|
||||
edge && (edge.direction === '1->2' ?
|
||||
<DAGEdge
|
||||
p1={[x + width, y + height/ 2]}
|
||||
p2={edge.loc}
|
||||
direction={edge.direction}
|
||||
color={edge.color}
|
||||
debug={debug}
|
||||
/> :
|
||||
<DAGEdge
|
||||
p1={[x, y + height/ 2]}
|
||||
p2={edge.loc}
|
||||
direction={edge.direction}
|
||||
color={edge.color}
|
||||
debug={debug}
|
||||
/>)
|
||||
}
|
||||
</>
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import React from "react";
|
||||
import { ComponentMeta, ComponentStory } from '@storybook/react';
|
||||
import { SequenceNodes } from './index';
|
||||
import { SVGBoard } from '../DAGSvgs/Board';
|
||||
import { NODE_HEIGHT, NODE_WIDTH } from '../DAGSvgs/constants';
|
||||
|
||||
|
||||
export default {
|
||||
title: 'DAGSvgs/SequenceNodes',
|
||||
component: SequenceNodes,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {
|
||||
nodeHeight: NODE_HEIGHT,
|
||||
nodeWidth: NODE_WIDTH,
|
||||
distanceFactor: 1.5,
|
||||
startLoc: [50,50]
|
||||
}
|
||||
} as ComponentMeta<typeof SequenceNodes>
|
||||
|
||||
export const Simple: ComponentStory<typeof SequenceNodes> = args => <SVGBoard>
|
||||
<SequenceNodes
|
||||
{...args}
|
||||
/>
|
||||
</SVGBoard>
|
||||
|
||||
Simple.args = {
|
||||
...Simple.args,
|
||||
sequence: [
|
||||
{
|
||||
$smType: "condition",
|
||||
condition: 'and',
|
||||
condition_data: {
|
||||
conditions: [
|
||||
{
|
||||
$smType: 'condition',
|
||||
condition: 'numeric_state',
|
||||
condition_data: {
|
||||
entity_id: 'sensor.temperature_kitchen',
|
||||
below: '15',
|
||||
}
|
||||
},
|
||||
{
|
||||
$smType: "condition",
|
||||
condition: 'template',
|
||||
condition_data: {
|
||||
value_template: 'states(switch.kitchen_light) == "on"'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
$smType: "action",
|
||||
action: "service",
|
||||
action_data: {
|
||||
alias: "Start Music In Kitchen",
|
||||
service: 'media_player.play_media',
|
||||
target: {
|
||||
entity_id: "media_player.kitchen_dot"
|
||||
},
|
||||
data: {
|
||||
media_content_id: "Good Morning",
|
||||
media_content_type: "SPOTIFY",
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { FC } from "react";
|
||||
import { useTheme } from "styles/theme";
|
||||
import { Point } from "types/graphs";
|
||||
import { AutomationSequenceNode } from '../../types/automations/index';
|
||||
import { computeNodesEdges } from './location';
|
||||
|
||||
|
||||
export const SequenceNodes: FC<{
|
||||
startLoc: Point,
|
||||
sequence: AutomationSequenceNode[],
|
||||
nodeHeight: number,
|
||||
nodeWidth: number,
|
||||
distanceFactor: number,
|
||||
}> = ({
|
||||
startLoc,
|
||||
sequence,
|
||||
nodeHeight,
|
||||
nodeWidth,
|
||||
distanceFactor,
|
||||
}) => {
|
||||
const theme = useTheme();
|
||||
|
||||
return <>
|
||||
{Array.from(computeNodesEdges(
|
||||
startLoc,
|
||||
nodeWidth,
|
||||
nodeHeight,
|
||||
distanceFactor,
|
||||
sequence,
|
||||
theme.secondaryAccent,
|
||||
))}
|
||||
</>
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { DAGEdge } from "components/DAGSvgs/DAGEdge";
|
||||
import { AutomationSequenceNode } from "types/automations";
|
||||
import { Point } from "types/graphs";
|
||||
import { DAGNode } from '../DAGSvgs/DAGNode';
|
||||
|
||||
|
||||
export function* computeNodesEdges(
|
||||
startPoint: Point,
|
||||
nodeWidth: number,
|
||||
nodeHeight: number,
|
||||
distanceFactor: number,
|
||||
sequence: AutomationSequenceNode[],
|
||||
edgeColor: string,
|
||||
): Generator<JSX.Element, [Point, Point], never> {
|
||||
let lastStartPoint: Point = [...startPoint];
|
||||
let lastNodeLoc: Point | null = null;
|
||||
for (let i = 0; i < sequence.length; i++) {
|
||||
const node = sequence[i];
|
||||
const nodeLoc: Point = lastStartPoint = [lastStartPoint[0] + nodeWidth * distanceFactor, lastStartPoint[1]];
|
||||
// const curretEdgeLoc: Point = [nodeLoc[0], nodeLoc[1] + nodeHeight / 2];
|
||||
// const nextAddLoc: Point | null = i < sequence.length ? null : [nodeLoc[0] + nodeWidth * distanceFactor, nodeLoc[1] + nodeHeight / 2];
|
||||
yield <>
|
||||
<DAGNode
|
||||
key={`node.${i}`}
|
||||
height={nodeHeight}
|
||||
width={nodeWidth}
|
||||
loc={nodeLoc}
|
||||
text={node.$smType}
|
||||
color={node.$smType === 'action' ? 'green' : 'blue'}
|
||||
edge={lastNodeLoc === null ? undefined : {
|
||||
direction: '1<-2',
|
||||
loc: [lastNodeLoc[0] + nodeWidth, lastNodeLoc[1] + nodeHeight / 2],
|
||||
color: edgeColor,
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
lastNodeLoc = [...nodeLoc]
|
||||
}
|
||||
return [startPoint, lastStartPoint]
|
||||
}
|
||||
Reference in new issue
Block a user