styling for auto editor
This commit is contained in:
1 parent
fe1b66d2d9
commit
5b14210f95
12 files changed
+357
-93
No files matched your search
@@ -4,12 +4,14 @@ import InputText from "../Inputs/InputText";
|
||||
import InputTextArea from "../Inputs/InputTextArea";
|
||||
|
||||
interface Props {
|
||||
className: string;
|
||||
metadata: AutomationMetadata;
|
||||
onUpdate: (m: AutomationMetadata) => void;
|
||||
}
|
||||
export default function AutoInfoBox({
|
||||
metadata,
|
||||
onUpdate,
|
||||
className,
|
||||
}: Props) {
|
||||
|
||||
// aliases
|
||||
@@ -19,7 +21,7 @@ export default function AutoInfoBox({
|
||||
})
|
||||
|
||||
// render
|
||||
return <div className="automation-editor-info-box">
|
||||
return <div className={className}>
|
||||
<InputText label="ID" value={metadata.id} onChange={onUpdateMetadata('id')} />
|
||||
<InputText label="Name" value={metadata.alias} onChange={onUpdateMetadata('alias')} />
|
||||
<InputTextArea label="Description" value={metadata.description} onChange={onUpdateMetadata('description')} />
|
||||
|
||||
@@ -6,11 +6,13 @@ export interface EdgeProp {
|
||||
p1: Point,
|
||||
p2: Point,
|
||||
direction: EdgeDirection,
|
||||
className: string,
|
||||
}
|
||||
export default ({
|
||||
p1: [x1,y1],
|
||||
p2: [x2,y2],
|
||||
direction,
|
||||
className,
|
||||
}: EdgeProp) => {
|
||||
const [_,my] = [(x1+x2)/2, (y1+y2)/2]
|
||||
const theta = Math.atan2(y2 -y1, x2-x1) - Math.PI / 2
|
||||
@@ -19,7 +21,7 @@ export default ({
|
||||
const cx2 = x2 + 45 * Math.sin(theta)
|
||||
const cy2 = my + 30 * Math.cos(theta)
|
||||
|
||||
return <path className="edge"
|
||||
return <path className={className}
|
||||
d={ `M${x1} ${y1} C${cx1} ${cy1}, ${cx2} ${cy2}, ${x2} ${y2}`}
|
||||
fill="none"
|
||||
markerStart={
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { FC } from "react"
|
||||
import Color from "chroma-js";
|
||||
import { PencilIcon } from "~/icons/icons"
|
||||
import { NODE_HEIGHT, NODE_WIDTH } from "./constants"
|
||||
import { useNodeStyles } from "./styles"
|
||||
import { Node } from "./types"
|
||||
|
||||
|
||||
@@ -20,43 +22,27 @@ export default ({
|
||||
onOpenClick=() => {},
|
||||
isOpen=false,
|
||||
color,
|
||||
}: NodeProp) => <foreignObject x={x} y={y} width={width} height={height}>
|
||||
<div className="automation-editor--node">
|
||||
<div className={`automation-editor--node-inner top-color ${color}`}>
|
||||
<div className="automation-editor--node-inner-edge left" onClick={onXClick}>
|
||||
<span className="automation-node-side-button">X</span>
|
||||
</div>
|
||||
<div className="automation-editor--node-inner-text">
|
||||
<span>{text}</span>
|
||||
</div>
|
||||
<div className="automation-editor--node-inner-edge right" onClick={onOpenClick}>
|
||||
<span className="automation-node-side-button">{isOpen ? "▲" : "▼"}</span>
|
||||
}: NodeProp) => {
|
||||
const {classes, theme} = useNodeStyles({
|
||||
color,
|
||||
nodeHeight: height,
|
||||
nodeWidth: width,
|
||||
});
|
||||
return <foreignObject x={x} y={y} width={width} height={height}>
|
||||
<div className={classes.root}>
|
||||
<div className={classes.inner}>
|
||||
<div className={classes.leftEdge} onClick={onXClick}>
|
||||
<button className={classes.buttonDelete}>X</button>
|
||||
</div>
|
||||
<div className={classes.text}>
|
||||
{text}
|
||||
</div>
|
||||
<div className={classes.rightEdge} onClick={onOpenClick}>
|
||||
<button className={classes.buttonEdit}>
|
||||
<PencilIcon size={1.1} color={Color(theme.primary).set('rgb.g', 200).hex()}/>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</foreignObject>
|
||||
|
||||
export interface ConditionNodeProps extends NodeProp {
|
||||
conditionType: string;
|
||||
</foreignObject>
|
||||
}
|
||||
export const ConditionNode: FC<ConditionNodeProps> = ({
|
||||
loc: [x,y],
|
||||
text,
|
||||
height=NODE_HEIGHT,
|
||||
width=NODE_WIDTH,
|
||||
onXClick=() => {},
|
||||
onOpenClick=() => {},
|
||||
isOpen=false,
|
||||
color,
|
||||
conditionType,
|
||||
children,
|
||||
}) => <foreignObject x={x} y={y} width={width} height={height}>
|
||||
<div className="automation-editor--condition-node">
|
||||
<div className="automation-editor--condition-node-type">
|
||||
{conditionType}
|
||||
</div>
|
||||
<div>
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
</foreignObject>
|
||||
@@ -1,7 +1,5 @@
|
||||
|
||||
|
||||
|
||||
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;
|
||||
export const NODE_WIDTH = 90 as const;
|
||||
export const NODE_HEIGHT = 40 as const;
|
||||
@@ -0,0 +1,90 @@
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { useState } from "react";
|
||||
import { AutomationData } from "~/automations/types";
|
||||
import { usePageTheme } from "~/styles/page";
|
||||
import AutomationEditor from ".";
|
||||
|
||||
export default {
|
||||
title: 'AutomationEditor',
|
||||
component: AutomationEditor,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
} as ComponentMeta<typeof AutomationEditor>;
|
||||
|
||||
|
||||
const Template: ComponentStory<typeof AutomationEditor> = ({automation, ...args}) => {
|
||||
const {classes} = usePageTheme({});
|
||||
const [data, setData] = useState(automation);
|
||||
return <div className={classes.page}>
|
||||
<AutomationEditor {...args}
|
||||
automation={data}
|
||||
onUpdate={data => {
|
||||
setData(data)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
|
||||
export const ServiceAction = Template.bind({})
|
||||
ServiceAction.args = {
|
||||
automation: {
|
||||
metadata: {
|
||||
id: "X12314akx",
|
||||
alias: "root",
|
||||
description: 'Really Long Description',
|
||||
mode: 'single',
|
||||
},
|
||||
trigger: [
|
||||
{
|
||||
platform: 'time',
|
||||
at: '10:00:00'
|
||||
},
|
||||
{
|
||||
platform: "device",
|
||||
device_id: "34749",
|
||||
domain: "zha",
|
||||
type: "remote_button_double_press",
|
||||
subtype: "remote_button_double_press"
|
||||
}
|
||||
],
|
||||
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",
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
} as AutomationData
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { AutomationData } from "~/automations/types";
|
||||
import { AutomationData, AutomationSequenceNode } from "~/automations/types";
|
||||
import { AutomationCondition } from "~/automations/types/conditions";
|
||||
import { AutomationTrigger } from "~/automations/types/triggers";
|
||||
import { getDescriptionFromAutomationNode } from "~/automations/utils";
|
||||
@@ -6,7 +6,8 @@ import useWindowSize from "~/utils/useWindowSize";
|
||||
import AutoInfoBox from "./AutoInfoBox";
|
||||
import { NODE_HEIGHT, NODE_WIDTH } from "./constants";
|
||||
import DAGEdge from "./DAGEdge";
|
||||
import DAGNode, { ConditionNode } from "./DAGNode";
|
||||
import DAGNode from "./DAGNode";
|
||||
import { useEditorStyles } from "./styles";
|
||||
import { Point } from "./types";
|
||||
|
||||
interface Props {
|
||||
@@ -20,6 +21,7 @@ export default function AutomationEditor({
|
||||
|
||||
// state
|
||||
const {ratioWbh} = useWindowSize();
|
||||
const {classes, theme} = useEditorStyles({});
|
||||
|
||||
// alias
|
||||
const nHDF = 1.5 // node horizontal distance factor
|
||||
@@ -36,70 +38,64 @@ export default function AutomationEditor({
|
||||
key={`edge.${i}`}
|
||||
p1={[NODE_WIDTH , nodeLoc[1] + 0.5 * NODE_HEIGHT]}
|
||||
p2={pointTrigToCond}
|
||||
className={classes.dagEdge}
|
||||
direction="1->2"
|
||||
/>
|
||||
</>
|
||||
}
|
||||
const renderMidCondition = (condition: AutomationCondition, i: number) => {
|
||||
const renderSequence = (node: AutomationSequenceNode, 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
|
||||
}
|
||||
let children = <></>;
|
||||
if (
|
||||
(condition.condition === 'and') ||
|
||||
(condition.condition === 'or') ||
|
||||
(condition.condition === 'not')
|
||||
) {
|
||||
// children = condition.condition_data.conditions.map((c,i) => renderMidCondition(c,i))
|
||||
}
|
||||
return <>
|
||||
<ConditionNode
|
||||
conditionType={condition.condition}
|
||||
<DAGNode
|
||||
key={`node.${i}`}
|
||||
loc={nodeLoc}
|
||||
text={getDescriptionFromAutomationNode(condition)} color="blue"
|
||||
>
|
||||
{children}
|
||||
</ConditionNode>
|
||||
text={getDescriptionFromAutomationNode(node)}
|
||||
color={node.$smType === 'action' ? 'green' : 'blue'}
|
||||
/>
|
||||
<DAGEdge
|
||||
key={`edge.${i}`}
|
||||
p1={lastNodeLoc}
|
||||
p2={[nodeLoc[0], nodeLoc[1] + NODE_HEIGHT/2]}
|
||||
direction="1->2"
|
||||
className={classes.dagEdge}
|
||||
/>
|
||||
</>
|
||||
}
|
||||
|
||||
// render
|
||||
return <div className="automation-editor" style={{
|
||||
return <div className={classes.root} style={{
|
||||
flexDirection: ratioWbh >= 0.75 ? 'row' : 'column'
|
||||
}}>
|
||||
<AutoInfoBox
|
||||
className={classes.infoBox}
|
||||
metadata={automation.metadata}
|
||||
onUpdate={metadata => onUpdate({...automation, metadata})}
|
||||
/>
|
||||
<div className="automation-editor-dag">
|
||||
<div className={classes.dag}>
|
||||
<svg
|
||||
className={classes.svg}
|
||||
viewBox={[0,0,graphWidth, graphHeight].join(" ")}
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<defs>
|
||||
<marker id="arrow" viewBox="0 0 10 10" refX="10" refY="5"
|
||||
<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" />
|
||||
<path d="M 0 0 L 10 5 L 0 10 z" fill={theme.secondaryAccent} />
|
||||
</marker>
|
||||
</defs>
|
||||
<g className="triggers">
|
||||
{automation.trigger.map(renderTrigger)}
|
||||
</g>
|
||||
<g className="sequence">
|
||||
{automation.sequence.map(renderMidCondition)}
|
||||
{automation.sequence.map(renderSequence)}
|
||||
</g>
|
||||
<circle className="blue" cx={pointTrigToCond[0]} cy={pointTrigToCond[1]} r={2}/>
|
||||
<circle className={classes.circle} cx={pointTrigToCond[0]} cy={pointTrigToCond[1]} r={2}/>
|
||||
<g className="actions">
|
||||
</g>
|
||||
</svg>
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import { Styles } from "jss";
|
||||
import Color from 'chroma-js';
|
||||
import { createAppUseStyles } from "~/styles/theme";
|
||||
import { Node } from "./types";
|
||||
|
||||
|
||||
export const useEditorStyles = createAppUseStyles(theme => ({
|
||||
root: {
|
||||
height: '99%',
|
||||
width: '99%',
|
||||
color: theme.primaryAccent,
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
},
|
||||
dag: {
|
||||
borderWidth: 1,
|
||||
borderStyle: 'solid',
|
||||
borderColor: theme.secondaryAccent,
|
||||
width: '100%',
|
||||
},
|
||||
svg: {
|
||||
height: '99%',
|
||||
width: '99%',
|
||||
padding: 0,
|
||||
margin: 0,
|
||||
},
|
||||
circle: {
|
||||
fill: Color(theme.secondary).set('rgb.b', 125).hex(),
|
||||
},
|
||||
infoBox: {
|
||||
paddingLeft: '10px',
|
||||
paddingRight: '10px',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'flex-start',
|
||||
overflow: 'auto',
|
||||
},
|
||||
dagEdge: {
|
||||
stroke: theme.secondaryAccent,
|
||||
},
|
||||
}))
|
||||
|
||||
export const useNodeStyles = createAppUseStyles<{
|
||||
color: Node['color'],
|
||||
nodeHeight: number,
|
||||
nodeWidth: number,
|
||||
}>(theme => {
|
||||
const edgeStyle: Styles = {
|
||||
flex: '1',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
flexWrap: 'nowrap',
|
||||
borderRadius: '1em',
|
||||
justifyContent: 'space-around',
|
||||
alignItems: 'center',
|
||||
'&:hover': {
|
||||
backgroundColor: theme.primaryAccent,
|
||||
cursor: 'pointer',
|
||||
}
|
||||
}
|
||||
const buttonStyle: Styles = {
|
||||
fontSize: '8px',
|
||||
backgroundColor: "rgba(0,0,0,0)",
|
||||
color: theme.primaryAccent,
|
||||
borderStyle: 'none',
|
||||
height: '15px',
|
||||
width: '15px',
|
||||
cursor: 'pointer',
|
||||
transition: "500ms east-out",
|
||||
}
|
||||
return {
|
||||
root: {
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'flex-end',
|
||||
height: `100%`,
|
||||
maxHeight: `100%`,
|
||||
maxWidth: '100%',
|
||||
width: '100%',
|
||||
fontSize: 10,
|
||||
},
|
||||
inner: ({color}) => ({
|
||||
position: 'relative',
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
backgroundColor: theme.secondary,
|
||||
borderRadius: '1em',
|
||||
flex: 1,
|
||||
height: `calc(100% - 10px)`,
|
||||
maxHeight: `calc(100% - 10px)`,
|
||||
maxWidth: '100%',
|
||||
borderTopWidth: '1px',
|
||||
borderTopStyle: 'solid',
|
||||
borderTopLeftRadius: '10px',
|
||||
borderTopRightRadius: '10px',
|
||||
borderTopColor: color === 'red' ? Color(theme.secondary).set('rgb.r', 125).hex() :
|
||||
color === 'blue' ? Color(theme.secondary).set('rgb.b', 125).hex() :
|
||||
color === 'green' ? Color(theme.secondary).set('rgb.g', 125).hex() :
|
||||
'none'
|
||||
}),
|
||||
text: {
|
||||
flex: 4,
|
||||
display: 'flex',
|
||||
textAlign: 'center',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
height: '100%',
|
||||
maxHeight: '100%',
|
||||
},
|
||||
buttonDelete: {
|
||||
...buttonStyle,
|
||||
"&:hover": {
|
||||
color: theme.primary,
|
||||
}
|
||||
},
|
||||
buttonEdit: {
|
||||
...buttonStyle,
|
||||
"&:hover": {
|
||||
transform: "rotate(45deg)"
|
||||
}
|
||||
},
|
||||
leftEdge: {
|
||||
...edgeStyle,
|
||||
borderLeft: `1px ${theme.secondaryAccent} solid`,
|
||||
borderTopRightRadius: 0,
|
||||
borderBottomRightRadius: 0,
|
||||
},
|
||||
rightEdge: {
|
||||
...edgeStyle,
|
||||
borderRight: `1px ${theme.secondaryAccent} solid`,
|
||||
borderTopLeftRadius: 0,
|
||||
borderBottomLeftRadius: 0,
|
||||
},
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,38 @@
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { useState } from "react";
|
||||
import { AutomationAction } from "~/automations/types/actions";
|
||||
import { usePageTheme } from "~/styles/page";
|
||||
import { SequenceEditor } from ".";
|
||||
|
||||
export default {
|
||||
title: 'SequenceEditor',
|
||||
component: SequenceEditor,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
} as ComponentMeta<typeof SequenceEditor>;
|
||||
|
||||
|
||||
const Template: ComponentStory<typeof SequenceEditor> = ({node, ...args}) => {
|
||||
const {classes} = usePageTheme({});
|
||||
const [data, setData] = useState(node);
|
||||
return <div className={classes.page}>
|
||||
<SequenceEditor {...args}
|
||||
node={data}
|
||||
onUpdate={data => {
|
||||
setData(data)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
|
||||
export const ServiceAction = Template.bind({})
|
||||
ServiceAction.args = {
|
||||
node: {
|
||||
$smType: 'action',
|
||||
action: 'service',
|
||||
action_data: {
|
||||
service: 'switch.toggle',
|
||||
data: {},
|
||||
target: {},
|
||||
}
|
||||
} as AutomationAction
|
||||
}
|
||||
@@ -1,14 +1,18 @@
|
||||
import { FC } from "react";
|
||||
import { AutomationSequenceNode } from "~/automations/types";
|
||||
import { AutomationAction } from "~/automations/types/actions";
|
||||
|
||||
|
||||
export const SequenceEditor: FC<{
|
||||
node: AutomationSequenceNode
|
||||
}> = ({}) => {
|
||||
node: AutomationAction,
|
||||
onUpdate: (u: AutomationAction) => void,
|
||||
}> = ({
|
||||
node,
|
||||
onUpdate,
|
||||
}) => {
|
||||
|
||||
return <div>
|
||||
<div>
|
||||
<span></span>
|
||||
<span>{node.action}</span>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
Reference in new issue
Block a user