working trigger editor
This commit is contained in:
1 parent
3cfb8151eb
commit
24f7c3c3af
14 files changed
+250
-66
No files matched your search
@@ -48,7 +48,16 @@ Simple.args = {
|
||||
mode: 'single',
|
||||
},
|
||||
trigger: [
|
||||
|
||||
{
|
||||
"$smType": undefined,
|
||||
"platform": "numeric_state",
|
||||
"entity_id": "test",
|
||||
},
|
||||
{
|
||||
"$smType": undefined,
|
||||
"platform": "homeassistant",
|
||||
"event": "start",
|
||||
}
|
||||
],
|
||||
sequence: [
|
||||
{
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
import { DAGBoardElmDims } from "components/DAGSvgs/DAGBoard";
|
||||
import { InfoIcon } from "components/Icons";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
import { SequenceNodes } from "components/SequenceNodes";
|
||||
import { FC, useState } from "react";
|
||||
import { AutomationData, AutomationSequenceNode } from "types/automations";
|
||||
import useWindowSize from "utils/useWindowSize";
|
||||
import { AutoInfoBox } from "./AutoInfoBox";
|
||||
import { useEditorStyles } from "./styles";
|
||||
import { computeTriggerPos, computeExtraField } from './positions';
|
||||
import { chain } from "utils/iter";
|
||||
import { AutomationTrigger } from 'types/automations/triggers';
|
||||
import { Modal } from "components/Modal";
|
||||
import { NodeEditor } from "components/NodeEditor";
|
||||
|
||||
|
||||
interface TriggerEditorModalState {
|
||||
trigger: AutomationTrigger;
|
||||
onSave: (node: AutomationTrigger) => void;
|
||||
}
|
||||
interface Props {
|
||||
automation: AutomationData;
|
||||
dims: DAGBoardElmDims;
|
||||
@@ -22,6 +29,7 @@ export const AutomationEditor: FC<Props> = ({
|
||||
// state
|
||||
const { ratioWbh } = useWindowSize();
|
||||
const [closeInfo, setCloseInfo] = useState(false);
|
||||
const [modalState, setModalState] = useState<null | TriggerEditorModalState>(null);
|
||||
const { classes } = useEditorStyles({
|
||||
closeInfo,
|
||||
horizontalMode: ratioWbh < 0.75
|
||||
@@ -32,9 +40,21 @@ export const AutomationEditor: FC<Props> = ({
|
||||
...automation,
|
||||
sequence,
|
||||
});
|
||||
const updateTriggers = (trigger: AutomationTrigger[]) => onUpdate({
|
||||
...automation,
|
||||
trigger,
|
||||
});
|
||||
|
||||
// render
|
||||
return <div className={classes.root}>
|
||||
<Modal open={!!modalState}>
|
||||
{!!modalState && <NodeEditor
|
||||
node={modalState.trigger}
|
||||
onClose={() => setModalState(null)}
|
||||
onSave={modalState.onSave as any}
|
||||
allowedTypes={['trigger']}
|
||||
/>}
|
||||
</Modal>
|
||||
<AutoInfoBox
|
||||
className={classes.infoBox}
|
||||
metadata={automation.metadata}
|
||||
@@ -48,10 +68,23 @@ export const AutomationEditor: FC<Props> = ({
|
||||
|
||||
<SequenceNodes
|
||||
zoomLevel={1.5}
|
||||
startPoint={[0.5, 0.5]}
|
||||
startPoint={[2, 0.5]}
|
||||
dims={dims}
|
||||
sequence={automation.sequence}
|
||||
onChange={updateSequence}
|
||||
additionalElements={chain([
|
||||
computeExtraField([1, 0.5], [2, 0.5]),
|
||||
computeTriggerPos(
|
||||
[0.5, 0.5],
|
||||
automation.trigger,
|
||||
[1.5, 0.5],
|
||||
updateTriggers,
|
||||
(trigger, onSave) => setModalState({
|
||||
trigger,
|
||||
onSave,
|
||||
})
|
||||
),
|
||||
])}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { DAGElement } from "components/DAGSvgs/DAGBoard";
|
||||
import { Point } from "types/graphs";
|
||||
import { AutomationTrigger } from 'types/automations/triggers';
|
||||
import { avgPoints } from "utils/graph";
|
||||
|
||||
|
||||
export function* computeTriggerPos(
|
||||
startPoint: Point,
|
||||
triggers: AutomationTrigger[],
|
||||
toPath: Point,
|
||||
onUpdate: (ts: AutomationTrigger[]) => void,
|
||||
onEdit: (nt: AutomationTrigger, onSave: (ts: AutomationTrigger) => void) => void,
|
||||
): Generator<DAGElement> {
|
||||
// functions
|
||||
const makeRemoveNode = (i: number) => () => onUpdate([
|
||||
...triggers.slice(0, i),
|
||||
...triggers.slice(i + 1),
|
||||
]);
|
||||
const updateNode = (n: AutomationTrigger, i: number) => onUpdate([
|
||||
...triggers.slice(0, i),
|
||||
n,
|
||||
...triggers.slice(i + 1),
|
||||
])
|
||||
|
||||
// main loop
|
||||
for (let i = 0; i < triggers.length; i++) {
|
||||
const node = triggers[i];
|
||||
const nodeLoc: Point = [startPoint[0], startPoint[1] + i];
|
||||
yield {
|
||||
type: "node",
|
||||
node,
|
||||
loc: nodeLoc,
|
||||
onRemove: makeRemoveNode(i),
|
||||
onOpen: () => onEdit(node, up => updateNode(up, i))
|
||||
}
|
||||
yield {
|
||||
type: "edge",
|
||||
p1: nodeLoc,
|
||||
p2: toPath,
|
||||
direction: "1->2",
|
||||
}
|
||||
}
|
||||
yield {
|
||||
type: 'circle',
|
||||
icon: 'add',
|
||||
loc: [startPoint[0] + 0.25, startPoint[1] + triggers.length],
|
||||
onAdd: () => onUpdate(triggers.concat({
|
||||
$smType: 'trigger',
|
||||
platform: 'state',
|
||||
entity_id: "",
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
export function* computeExtraField(
|
||||
triggerEndPos: Point,
|
||||
sequenceStartPos: Point,
|
||||
): Generator<DAGElement> {
|
||||
yield {
|
||||
type: 'circle',
|
||||
loc: avgPoints(triggerEndPos, sequenceStartPos),
|
||||
icon: 'color',
|
||||
color: 'blue',
|
||||
}
|
||||
yield {
|
||||
type: 'edge',
|
||||
p1: triggerEndPos,
|
||||
p2: sequenceStartPos,
|
||||
direction: '1->2',
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user