refactor of graph pt2
This commit is contained in:
1 parent
730c804c22
commit
2cb2e779d7
20 files changed
+714
-246
No files matched your search
@@ -73,7 +73,7 @@ Basic.args = {
|
||||
data: {
|
||||
label: "hi",
|
||||
},
|
||||
},
|
||||
} as any,
|
||||
],
|
||||
edges: [
|
||||
{ id: "1->2", source: "1", target: "2", label: "wow" },
|
||||
|
||||
@@ -2,7 +2,11 @@ import "./index.css";
|
||||
|
||||
import { FC } from "react";
|
||||
import CircularProgress from "@mui/material/CircularProgress";
|
||||
import ReactFlow, { Controls } from "react-flow-renderer";
|
||||
import ReactFlow, {
|
||||
Controls,
|
||||
ReactFlowProvider,
|
||||
useViewport,
|
||||
} from "react-flow-renderer";
|
||||
|
||||
import { NodeEditor } from "components/NodeEditor";
|
||||
import { Modal } from "components/Modal";
|
||||
@@ -10,11 +14,20 @@ import { Modal } from "components/Modal";
|
||||
import { nodeTypes } from "../nodes";
|
||||
import { DAGGraphBoardProps } from "./types";
|
||||
|
||||
export const DAGGraphBoard: FC<DAGGraphBoardProps> = ({
|
||||
export const DAGGraphBoard: FC<DAGGraphBoardProps> = (props) => {
|
||||
return (
|
||||
<ReactFlowProvider>
|
||||
<DAGGraphBoardInner {...props} />
|
||||
</ReactFlowProvider>
|
||||
);
|
||||
};
|
||||
|
||||
export const DAGGraphBoardInner: FC<DAGGraphBoardProps> = ({
|
||||
modalState,
|
||||
state,
|
||||
closeModal,
|
||||
}) => {
|
||||
const { zoom } = useViewport();
|
||||
// render unready or errored states
|
||||
if (!state.ready) {
|
||||
return (
|
||||
@@ -44,7 +57,20 @@ export const DAGGraphBoard: FC<DAGGraphBoardProps> = ({
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// this hack avoids breakage on when zooming
|
||||
const elements = {
|
||||
nodes: state.data.elements.nodes.map((n) => ({
|
||||
...n,
|
||||
id: `${zoom}-${n.id}`,
|
||||
})),
|
||||
edges: state.data.elements.edges.map((n) => ({
|
||||
...n,
|
||||
id: `${zoom}-${n.id}`,
|
||||
source: `${zoom}-${n.source}`,
|
||||
target: `${zoom}-${n.target}`,
|
||||
})),
|
||||
};
|
||||
console.log(elements);
|
||||
// main render
|
||||
return (
|
||||
<>
|
||||
@@ -56,7 +82,7 @@ export const DAGGraphBoard: FC<DAGGraphBoardProps> = ({
|
||||
onlyRenderVisibleElements
|
||||
nodesConnectable={false}
|
||||
nodesDraggable={false}
|
||||
{...state.data.elements}
|
||||
{...elements}
|
||||
>
|
||||
<Controls />
|
||||
</ReactFlow>
|
||||
|
||||
@@ -23,7 +23,7 @@ export const DEFAULT_DIMS: DAGDims = {
|
||||
},
|
||||
distanceFactor: {
|
||||
node: 1.5,
|
||||
collection: 1.25,
|
||||
collection: 1,
|
||||
},
|
||||
position: {
|
||||
x: 50,
|
||||
|
||||
@@ -5,7 +5,8 @@ export const moveAlong = (
|
||||
by: keyof DAGEntities,
|
||||
startPoint: XYPosition,
|
||||
increment: number,
|
||||
dims: DAGDims
|
||||
dims: DAGDims,
|
||||
reversed: boolean = false
|
||||
): XYPosition => {
|
||||
const moveOnY = {
|
||||
y: startPoint.y + dims[by].height * dims.distanceFactor.node * increment,
|
||||
@@ -15,6 +16,9 @@ export const moveAlong = (
|
||||
x: startPoint.x + dims[by].width * dims.distanceFactor.node * increment,
|
||||
y: startPoint.y,
|
||||
};
|
||||
if (reversed) {
|
||||
return dims.flipped ? moveOnX : moveOnY;
|
||||
}
|
||||
return !dims.flipped ? moveOnX : moveOnY;
|
||||
};
|
||||
|
||||
|
||||
@@ -2,8 +2,7 @@ import React, { FC, useState } from "react";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { DAGGraphBoard } from "components/DAGGraph/board";
|
||||
import { AutomationTrigger } from "types/automations/triggers";
|
||||
import { makeTriggerNodes, makeAutomationNodes } from ".";
|
||||
import { makeAutomationNodes } from "./index";
|
||||
import { useHA } from "haService";
|
||||
import { AutomationActionData } from "types/automations";
|
||||
import { createUpdaterFromAutomationData } from "../updater";
|
||||
@@ -274,3 +273,97 @@ FullAuto.args = {
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
export const ChooseAuto = Template.bind({});
|
||||
ChooseAuto.args = {
|
||||
...ChooseAuto.args,
|
||||
automation: {
|
||||
condition: [],
|
||||
trigger: [],
|
||||
sequence: [
|
||||
{
|
||||
choose: [
|
||||
{
|
||||
conditions: [
|
||||
{
|
||||
condition: "state",
|
||||
entity_id: "lights.bathroom",
|
||||
state: "off",
|
||||
},
|
||||
],
|
||||
sequence: [
|
||||
{
|
||||
service: "light.turn_on",
|
||||
target: {
|
||||
entity_id: "lights.bathroom",
|
||||
},
|
||||
data: {},
|
||||
},
|
||||
{
|
||||
alias: "Turn off bedroom",
|
||||
service: "light.turn_off",
|
||||
target: {
|
||||
entity_id: "lights.bedroom",
|
||||
},
|
||||
data: {},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
conditions: [
|
||||
{
|
||||
condition: "state",
|
||||
entity_id: "lights.bedroom",
|
||||
state: "off",
|
||||
},
|
||||
],
|
||||
sequence: [
|
||||
{
|
||||
alias: "Turn on bedroom",
|
||||
service: "light.turn_on",
|
||||
target: {
|
||||
entity_id: "lights.bedroom",
|
||||
},
|
||||
data: {},
|
||||
},
|
||||
{
|
||||
choose: [
|
||||
{
|
||||
conditions: [],
|
||||
sequence: [
|
||||
{
|
||||
choose: [],
|
||||
default: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
default: [
|
||||
{
|
||||
choose: [],
|
||||
default: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
default: [
|
||||
{
|
||||
service: "light.turn_off",
|
||||
target: {
|
||||
entity_id: "lights.bathroom",
|
||||
},
|
||||
data: {},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
service: "switch.turn_on",
|
||||
target: {
|
||||
entity_id: "switch.light_balcony",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
@@ -1,194 +0,0 @@
|
||||
import {
|
||||
Bbox,
|
||||
DAGDims,
|
||||
ElementMaker,
|
||||
ElementMakerBaseProps,
|
||||
ElementMakerOutput,
|
||||
LastNode,
|
||||
} from "./types";
|
||||
import { AutomationTrigger } from "types/automations/triggers";
|
||||
import * as distance from "./distance";
|
||||
import { getDescriptionFromAutomationNode } from "utils/formatting";
|
||||
import {
|
||||
AutomationActionData,
|
||||
AutomationSequenceNode,
|
||||
} from "types/automations";
|
||||
import { AutomationCondition } from "types/automations/conditions";
|
||||
import { CollectionNodeMaker } from "../nodes/CollectionNode";
|
||||
import { SequenceNodeMaker } from "../nodes/SequenceNode";
|
||||
import { getNodeType } from "utils/automations";
|
||||
|
||||
export const makeAutomationNodes = (
|
||||
automation: AutomationActionData,
|
||||
args: ElementMakerBaseProps
|
||||
) => {
|
||||
const dims: DAGDims = {
|
||||
...args.dims,
|
||||
// think of a better to do this
|
||||
// condition: args.dims.flipped
|
||||
// ? args.dims.condition
|
||||
// : {
|
||||
// height: args.dims.condition.width,
|
||||
// width: args.dims.condition.height,
|
||||
// },
|
||||
// trigger: args.dims.flipped
|
||||
// ? args.dims.trigger
|
||||
// : {
|
||||
// height: args.dims.trigger.width,
|
||||
// width: args.dims.trigger.height,
|
||||
// },
|
||||
};
|
||||
let state: ElementMakerOutput = makeTriggerNodes(automation.trigger, {
|
||||
...args,
|
||||
dims,
|
||||
elementData: {
|
||||
nodes: [],
|
||||
edges: [],
|
||||
},
|
||||
position: dims.position,
|
||||
nodeId: `${dims.flipped}-trigger`,
|
||||
nodeIndex: 0,
|
||||
});
|
||||
|
||||
state = makeConditionNodes(automation.condition, {
|
||||
...args,
|
||||
dims,
|
||||
elementData: state.elementData,
|
||||
position: distance.moveFromTo(
|
||||
"trigger",
|
||||
"condition",
|
||||
state.lastNode.pos,
|
||||
dims
|
||||
),
|
||||
nodeId: `${dims.flipped}-condition`,
|
||||
nodeIndex: 1,
|
||||
lastNodeId: state.lastNode.nodeId,
|
||||
});
|
||||
|
||||
state = makeSequenceNodes(automation.sequence, {
|
||||
...args,
|
||||
dims,
|
||||
elementData: state.elementData,
|
||||
position: distance.moveFromTo(
|
||||
"condition",
|
||||
"node",
|
||||
state.lastNode.pos,
|
||||
dims
|
||||
),
|
||||
nodeId: `${dims.flipped}-sequence`,
|
||||
nodeIndex: 2,
|
||||
lastNodeId: state.lastNode.nodeId,
|
||||
});
|
||||
|
||||
return state.elementData;
|
||||
};
|
||||
|
||||
export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
|
||||
nodes,
|
||||
{ elementData, dims, namer, openModal, stateUpdater, nodeId, position }
|
||||
) => {
|
||||
elementData.nodes.push(
|
||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||
...dims.trigger,
|
||||
color: "red",
|
||||
onAddNode: () => stateUpdater.basic.trigger.addNode(null),
|
||||
nodes: nodes.map((node, index) => ({
|
||||
enabled: node.enabled ?? true,
|
||||
label: getDescriptionFromAutomationNode(node, namer, true),
|
||||
...stateUpdater.createNodeActions("trigger", index, {}),
|
||||
})),
|
||||
})
|
||||
);
|
||||
return {
|
||||
elementData,
|
||||
lastNode: {
|
||||
nodeId,
|
||||
pos: elementData.nodes[elementData.nodes.length - 1].position,
|
||||
size: dims.trigger,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const makeConditionNodes: ElementMaker<AutomationCondition> = (
|
||||
nodes,
|
||||
{ elementData, nodeId, stateUpdater, position, dims, namer, lastNodeId }
|
||||
) => {
|
||||
elementData.nodes.push(
|
||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||
...dims.condition,
|
||||
color: "blue",
|
||||
hasInput: !!lastNodeId,
|
||||
onAddNode: () => stateUpdater.basic.condition.addNode(null),
|
||||
nodes: nodes.map((node, index) => ({
|
||||
enabled: node.enabled ?? true,
|
||||
label: getDescriptionFromAutomationNode(node, namer, true),
|
||||
...stateUpdater.createNodeActions("condition", index, {}),
|
||||
})),
|
||||
})
|
||||
);
|
||||
if (lastNodeId) {
|
||||
elementData.edges.push({
|
||||
source: lastNodeId,
|
||||
target: elementData.nodes[elementData.nodes.length - 1].id,
|
||||
animated: true,
|
||||
id: `${lastNodeId}->${
|
||||
elementData.nodes[elementData.nodes.length - 1].id
|
||||
}`,
|
||||
});
|
||||
}
|
||||
return {
|
||||
elementData,
|
||||
lastNode: {
|
||||
nodeId,
|
||||
pos: elementData.nodes[elementData.nodes.length - 1].position,
|
||||
size: dims.condition,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
nodes,
|
||||
{ nodeId, lastNodeId, elementData, dims, position, namer, stateUpdater }
|
||||
) => {
|
||||
let lastNode: LastNode = {
|
||||
nodeId: lastNodeId ?? nodeId,
|
||||
pos: elementData.nodes[elementData.nodes.length - 1].position,
|
||||
size: dims.node,
|
||||
};
|
||||
nodes.forEach((node, nodeIndex) => {
|
||||
const element = SequenceNodeMaker.makeElement(
|
||||
{
|
||||
// the JSON.stringify is hack to make the edges render nicely
|
||||
id: `${nodeId}-${nodeIndex}-${JSON.stringify(node)}`,
|
||||
position: distance.moveAlong("node", position, nodeIndex, dims),
|
||||
},
|
||||
dims,
|
||||
{
|
||||
color: getNodeType(node) === "action" ? "green" : "blue",
|
||||
enabled: node.enabled ?? true,
|
||||
hasInput: true,
|
||||
label: getDescriptionFromAutomationNode(node, namer, true),
|
||||
...stateUpdater.createNodeActions("sequence", nodeIndex, {
|
||||
includeAdd: true,
|
||||
flipped: dims.flipped,
|
||||
}),
|
||||
}
|
||||
);
|
||||
elementData.nodes.push(element);
|
||||
elementData.edges.push({
|
||||
id: `${lastNode.nodeId}->${element.id}`,
|
||||
target: element.id,
|
||||
source: lastNode.nodeId,
|
||||
});
|
||||
lastNode = {
|
||||
nodeId: element.id,
|
||||
pos: element.position,
|
||||
size: dims.node,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
elementData,
|
||||
lastNode,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,286 @@
|
||||
import {
|
||||
DAGDims,
|
||||
ElementData,
|
||||
ElementMaker,
|
||||
ElementMakerBaseProps,
|
||||
LastNode,
|
||||
} from "./types";
|
||||
import { AutomationTrigger } from "types/automations/triggers";
|
||||
import * as distance from "./distance";
|
||||
import { getDescriptionFromAutomationNode } from "utils/formatting";
|
||||
import {
|
||||
AutomationActionData,
|
||||
AutomationSequenceNode,
|
||||
} from "types/automations";
|
||||
import { AutomationCondition } from "types/automations/conditions";
|
||||
import { CollectionNodeMaker } from "../nodes/CollectionNode";
|
||||
import { ChooseAction } from "types/automations/actions";
|
||||
import { XYPosition } from "react-flow-renderer";
|
||||
import { DAGElementsState } from "./state";
|
||||
import { SequenceNodeMaker } from "../nodes/SequenceNode";
|
||||
import { getNodeType } from "utils/automations";
|
||||
import { pointApply } from "./math";
|
||||
import { ButtonNodeMaker } from "../nodes/ButtonNode";
|
||||
import { AddIcon } from "components/Icons";
|
||||
|
||||
export const makeAutomationNodes = (
|
||||
automation: AutomationActionData,
|
||||
args: ElementMakerBaseProps
|
||||
) => {
|
||||
const dims: DAGDims = {
|
||||
...args.dims,
|
||||
// think of a better to do this
|
||||
// condition: args.dims.flipped
|
||||
// ? args.dims.condition
|
||||
// : {
|
||||
// height: args.dims.condition.width,
|
||||
// width: args.dims.condition.height,
|
||||
// },
|
||||
// trigger: args.dims.flipped
|
||||
// ? args.dims.trigger
|
||||
// : {
|
||||
// height: args.dims.trigger.width,
|
||||
// width: args.dims.trigger.height,
|
||||
// },
|
||||
};
|
||||
const state = new DAGElementsState();
|
||||
state.extend(
|
||||
makeTriggerNodes(automation.trigger, {
|
||||
...args,
|
||||
dims,
|
||||
position: dims.position,
|
||||
nodeId: `${dims.flipped}-trigger`,
|
||||
nodeIndex: 0,
|
||||
})
|
||||
);
|
||||
|
||||
state.extend(
|
||||
makeConditionNodes(automation.condition, {
|
||||
...args,
|
||||
dims,
|
||||
position: distance.moveFromTo(
|
||||
"trigger",
|
||||
"condition",
|
||||
state.lastNodePos,
|
||||
dims
|
||||
),
|
||||
nodeId: `${dims.flipped}-condition`,
|
||||
nodeIndex: 1,
|
||||
lastNodeId: state.lastNodeId,
|
||||
})
|
||||
);
|
||||
|
||||
state.extend(
|
||||
makeSequenceNodes(automation.sequence, {
|
||||
...args,
|
||||
dims,
|
||||
position: distance.moveFromTo(
|
||||
"condition",
|
||||
"node",
|
||||
state.lastNodePos,
|
||||
dims
|
||||
),
|
||||
nodeId: `${dims.flipped}-sequence`,
|
||||
nodeIndex: 2,
|
||||
lastNodeId: state.lastNodeId,
|
||||
})
|
||||
);
|
||||
|
||||
return state.data;
|
||||
};
|
||||
|
||||
export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
|
||||
nodes,
|
||||
{ dims, namer, stateUpdater, nodeId, position }
|
||||
) => {
|
||||
const state = new DAGElementsState();
|
||||
state.addNode(
|
||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||
...dims.trigger,
|
||||
color: "red",
|
||||
onAddNode: () => stateUpdater.basic.trigger.addNode(null),
|
||||
nodes: nodes.map((node, index) => ({
|
||||
enabled: node.enabled ?? true,
|
||||
label: getDescriptionFromAutomationNode(node, namer, true),
|
||||
...stateUpdater.createNodeActions("trigger", index, {}),
|
||||
})),
|
||||
})
|
||||
);
|
||||
return state;
|
||||
};
|
||||
|
||||
export const makeConditionNodes: ElementMaker<AutomationCondition> = (
|
||||
nodes,
|
||||
{ nodeId, stateUpdater, position, dims, namer, lastNodeId }
|
||||
) => {
|
||||
const state = new DAGElementsState();
|
||||
state.addNode(
|
||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||
...dims.condition,
|
||||
color: "blue",
|
||||
hasInput: !!lastNodeId,
|
||||
onAddNode: () => stateUpdater.basic.condition.addNode(null),
|
||||
nodes: nodes.map((node, index) => ({
|
||||
enabled: node.enabled ?? true,
|
||||
label: getDescriptionFromAutomationNode(node, namer, true),
|
||||
...stateUpdater.createNodeActions("condition", index, {}),
|
||||
})),
|
||||
})
|
||||
);
|
||||
if (lastNodeId && state.lastNodeId) {
|
||||
state.addEdge(lastNodeId, state.lastNodeId, true);
|
||||
}
|
||||
return state;
|
||||
};
|
||||
|
||||
export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
nodes,
|
||||
args
|
||||
) => {
|
||||
const state = new DAGElementsState();
|
||||
const { lastNodeId, nodeId, position, dims, namer, stateUpdater } = args;
|
||||
nodes.forEach((node, nodeIndex) => {
|
||||
const element = state.addNode(
|
||||
SequenceNodeMaker.makeElement(
|
||||
{
|
||||
// the JSON.stringify is hack to make the edges render nicely
|
||||
id: `${nodeId}-${args.nodeIndex}-${nodeIndex}-${JSON.stringify(
|
||||
node
|
||||
)}`,
|
||||
position: distance.moveAlong("node", position, nodeIndex, dims),
|
||||
},
|
||||
dims,
|
||||
{
|
||||
color: getNodeType(node) === "action" ? "green" : "blue",
|
||||
enabled: node.enabled ?? true,
|
||||
hasInput: true,
|
||||
label: getDescriptionFromAutomationNode(node, namer, true),
|
||||
...stateUpdater.createNodeActions("sequence", nodeIndex, {
|
||||
includeAdd: true,
|
||||
flipped: dims.flipped,
|
||||
}),
|
||||
}
|
||||
),
|
||||
true
|
||||
);
|
||||
if (nodeIndex === 0 && lastNodeId) {
|
||||
state.addEdge(lastNodeId, element.id);
|
||||
}
|
||||
if ("choose" in node) {
|
||||
state.extend(
|
||||
makeChooseeNodes([node], {
|
||||
...args,
|
||||
lastNodeId: element.id,
|
||||
position: state.lastNodePos,
|
||||
nodeIndex,
|
||||
}),
|
||||
true
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
const addElement = state.addNode(
|
||||
ButtonNodeMaker.makeElement(
|
||||
{
|
||||
id: `${nodeId}-add-new`,
|
||||
position: distance.moveAlong("node", position, nodes.length, dims),
|
||||
},
|
||||
dims,
|
||||
{
|
||||
icon: <AddIcon />,
|
||||
onClick: () => stateUpdater.basic.sequence.addNode(null),
|
||||
}
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
if (nodes.length === 0 && lastNodeId) {
|
||||
state.addEdge(lastNodeId, addElement.id);
|
||||
}
|
||||
|
||||
return state;
|
||||
};
|
||||
|
||||
export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
const state = new DAGElementsState();
|
||||
|
||||
nodes.forEach((node, nodeIndex) => {
|
||||
if (node.enabled ?? true) {
|
||||
// offset position
|
||||
let offsetPos: XYPosition = {
|
||||
x:
|
||||
args.position.x +
|
||||
args.dims.distanceFactor.node *
|
||||
args.dims.node.width *
|
||||
(nodeIndex + 1),
|
||||
y:
|
||||
args.position.y +
|
||||
args.dims.distanceFactor.node *
|
||||
args.dims.node.height *
|
||||
(nodeIndex + 1),
|
||||
};
|
||||
// functions
|
||||
const processSequence = (
|
||||
sequence: AutomationSequenceNode[],
|
||||
index: number,
|
||||
condition: AutomationCondition[] | null = null
|
||||
) => {
|
||||
let pos = offsetPos;
|
||||
let lastNodeId = args.lastNodeId;
|
||||
// if (condition !== null) {
|
||||
// const conditionState = makeConditionNodes(condition, {
|
||||
// ...args,
|
||||
// position: pos,
|
||||
// lastNodeId,
|
||||
// nodeIndex: index,
|
||||
// nodeId: `${args.nodeId}-choose-${index}-condition`,
|
||||
// });
|
||||
// // pos.y = conditionState.bbox[1].y;
|
||||
// state.extend(conditionState);
|
||||
// if (lastNodeId && conditionState.data.nodes.length > 0) {
|
||||
// state.addEdge(lastNodeId, conditionState.data.nodes[0].id, true);
|
||||
// }
|
||||
// lastNodeId = conditionState.lastNodeId ?? lastNodeId;
|
||||
// pos = {
|
||||
// y: conditionState.bbox[1].y,
|
||||
// x: pos.x,
|
||||
// };
|
||||
// }
|
||||
const output = makeSequenceNodes(sequence, {
|
||||
...args,
|
||||
position: pos,
|
||||
lastNodeId: undefined,
|
||||
nodeIndex: index,
|
||||
nodeId: `${args.nodeId}-choose-${index}-sequence`,
|
||||
});
|
||||
if (lastNodeId && output.data.nodes.length > 0) {
|
||||
state.addEdge(lastNodeId, output.data.nodes[0].id, true);
|
||||
}
|
||||
offsetPos = args.dims.flipped
|
||||
? {
|
||||
x:
|
||||
output.bbox[1].x +
|
||||
(args.dims.distanceFactor.node - 1) * args.dims.node.width,
|
||||
y: offsetPos.y,
|
||||
}
|
||||
: {
|
||||
y:
|
||||
output.bbox[1].y +
|
||||
(args.dims.distanceFactor.node - 1) * args.dims.node.height,
|
||||
x: offsetPos.x,
|
||||
};
|
||||
state.extend(output);
|
||||
};
|
||||
|
||||
// process options
|
||||
for (let sIndex = 0; sIndex < node.choose.length; sIndex++) {
|
||||
const subSequence = node.choose[sIndex];
|
||||
processSequence(subSequence.sequence, sIndex, subSequence.conditions);
|
||||
}
|
||||
// else
|
||||
processSequence(node.default ?? [], node.choose.length);
|
||||
}
|
||||
});
|
||||
|
||||
return state;
|
||||
};
|
||||
@@ -0,0 +1,74 @@
|
||||
import { DAGAutomationFlowNode } from "../nodes";
|
||||
import { addSize, pointApply } from "./math";
|
||||
import { Bbox, ElementData, LastNode } from "./types";
|
||||
|
||||
export class DAGElementsState {
|
||||
public data: ElementData = {
|
||||
nodes: [],
|
||||
edges: [],
|
||||
};
|
||||
public lastNode?: LastNode;
|
||||
public bbox: Bbox = [
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 0, y: 0 },
|
||||
];
|
||||
|
||||
public extend(other: DAGElementsState, ignoreLastNode: boolean = false) {
|
||||
this.data.nodes = this.data.nodes.concat(other.data.nodes);
|
||||
this.data.edges = this.data.edges.concat(other.data.edges);
|
||||
this.bbox[0] = pointApply(this.bbox[0], other.bbox[0], Math.min);
|
||||
this.bbox[1] = pointApply(this.bbox[1], other.bbox[1], Math.max);
|
||||
if (!ignoreLastNode) {
|
||||
this.lastNode = other.lastNode;
|
||||
}
|
||||
}
|
||||
|
||||
public addNode(
|
||||
node: DAGAutomationFlowNode,
|
||||
addEdge: boolean = false,
|
||||
animatedEdge: boolean = false
|
||||
) {
|
||||
if (addEdge && this.lastNodeId) {
|
||||
this.addEdge(this.lastNodeId, node.id, animatedEdge);
|
||||
}
|
||||
this.data.nodes.push(node);
|
||||
this.bbox[0] = pointApply(this.bbox[0], node.position, Math.min);
|
||||
this.bbox[1] = pointApply(
|
||||
this.bbox[1],
|
||||
addSize(node.position, node.data),
|
||||
Math.max
|
||||
);
|
||||
this.lastNode = {
|
||||
nodeId: node.id,
|
||||
pos: node.position,
|
||||
size: {
|
||||
height: node.data.height,
|
||||
width: node.data.width,
|
||||
},
|
||||
};
|
||||
return node;
|
||||
}
|
||||
|
||||
public addEdge(from: string, to: string, animated: boolean = false) {
|
||||
this.data.edges.push({
|
||||
id: `${from}->${to}`,
|
||||
source: from,
|
||||
target: to,
|
||||
animated,
|
||||
});
|
||||
}
|
||||
|
||||
public get lastNodeId() {
|
||||
if (this.lastNode) {
|
||||
return this.lastNode.nodeId;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public get lastNodePos() {
|
||||
if (this.lastNode) {
|
||||
return this.lastNode.pos;
|
||||
}
|
||||
throw Error("missing lastnode");
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { Namer } from "utils/formatting";
|
||||
import { ModalState } from "../board/types";
|
||||
import { DAGAutomationFlowNode } from "../nodes";
|
||||
import { DAGGraphUpdater } from "../updater";
|
||||
import { DAGElementsState } from "./state";
|
||||
|
||||
export type Bbox = [XYPosition, XYPosition];
|
||||
export type Size = { height: number; width: number };
|
||||
@@ -33,7 +34,6 @@ export type ElementMakerBaseProps = {
|
||||
openModal: (m: ModalState) => void;
|
||||
};
|
||||
export type ElementMakerProps = ElementMakerBaseProps & {
|
||||
elementData: ElementData;
|
||||
nodeId: string;
|
||||
nodeIndex: number;
|
||||
position: XYPosition;
|
||||
@@ -44,12 +44,7 @@ export type LastNode = {
|
||||
pos: XYPosition;
|
||||
size: Size;
|
||||
};
|
||||
export type ElementMakerOutput = {
|
||||
elementData: ElementData;
|
||||
lastNode: LastNode;
|
||||
};
|
||||
|
||||
export type ElementMaker<A extends AutomationNode = AutomationNode> = (
|
||||
haNodes: A[],
|
||||
args: ElementMakerProps
|
||||
) => ElementMakerOutput;
|
||||
) => DAGElementsState;
|
||||
@@ -0,0 +1,87 @@
|
||||
import React from "react";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { ButtonNode } from "./ButtonNode";
|
||||
import { DAGGraphBoard } from "components/DAGGraph/board";
|
||||
import { ButtonNodeMaker } from ".";
|
||||
import { DEFAULT_DIMS } from "../../elements/constants";
|
||||
import { AddIcon } from "components/Icons";
|
||||
import { SequenceNodeMaker } from "../SequenceNode";
|
||||
|
||||
export default {
|
||||
title: "DAGGraph/Nodes/Button",
|
||||
component: ButtonNode,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
icon: <AddIcon />,
|
||||
flipped: true,
|
||||
...DEFAULT_DIMS.node,
|
||||
},
|
||||
} as ComponentMeta<typeof ButtonNode>;
|
||||
|
||||
const Template: ComponentStory<typeof ButtonNode> = (args) => {
|
||||
const dims = {
|
||||
...DEFAULT_DIMS,
|
||||
...args,
|
||||
};
|
||||
return (
|
||||
<Page>
|
||||
<DAGGraphBoard
|
||||
closeModal={() => {}}
|
||||
state={{
|
||||
ready: true,
|
||||
data: {
|
||||
elements: {
|
||||
edges: [
|
||||
{
|
||||
id: `${dims.flipped}-1`,
|
||||
source: `${dims.flipped}-node`,
|
||||
target: `${dims.flipped}-button`,
|
||||
},
|
||||
],
|
||||
nodes: [
|
||||
SequenceNodeMaker.makeElement(
|
||||
{
|
||||
id: `${dims.flipped}-node`,
|
||||
position: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
},
|
||||
},
|
||||
dims,
|
||||
{
|
||||
label: "test",
|
||||
color: "lblue",
|
||||
enabled: true,
|
||||
}
|
||||
),
|
||||
ButtonNodeMaker.makeElement(
|
||||
{
|
||||
id: `${dims.flipped}-button`,
|
||||
position: !dims.flipped
|
||||
? {
|
||||
x:
|
||||
DEFAULT_DIMS.node.width *
|
||||
DEFAULT_DIMS.distanceFactor.node,
|
||||
y: 0,
|
||||
}
|
||||
: {
|
||||
x: 0,
|
||||
y:
|
||||
DEFAULT_DIMS.node.height *
|
||||
DEFAULT_DIMS.distanceFactor.node,
|
||||
},
|
||||
},
|
||||
dims,
|
||||
args
|
||||
),
|
||||
],
|
||||
},
|
||||
},
|
||||
}}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
export const Basic = Template.bind({});
|
||||
@@ -0,0 +1,14 @@
|
||||
.button-node {
|
||||
max-width: var(--button-width);
|
||||
max-height: var(--button-height);
|
||||
min-width: var(--button-width);
|
||||
min-height: var(--button-height);
|
||||
width: var(--button-width);
|
||||
height: var(--button-height);
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
|
||||
/* outline: 1px dashed white; */
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import "./ButtonNode.css";
|
||||
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { FC } from "react";
|
||||
import { ButtonNodeProps } from "./types";
|
||||
import { Handle, Position } from "react-flow-renderer";
|
||||
|
||||
export const ButtonNode: FC<ButtonNodeProps> = ({
|
||||
height,
|
||||
width,
|
||||
icon,
|
||||
flipped,
|
||||
onClick,
|
||||
}) => {
|
||||
return (
|
||||
<div
|
||||
className="button-node"
|
||||
style={
|
||||
{
|
||||
"--button-height": `${height}px`,
|
||||
"--button-width": `${width}px`,
|
||||
} as any
|
||||
}
|
||||
>
|
||||
<ButtonIcon
|
||||
icon={
|
||||
<>
|
||||
<Handle
|
||||
type="target"
|
||||
position={flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
{icon}
|
||||
</>
|
||||
}
|
||||
onClick={onClick}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
import { NodeManager } from "components/DAGGraph/nodes/NodeManager";
|
||||
import { ButtonNode } from "./ButtonNode";
|
||||
import { ButtonNodeDataProps } from "./types";
|
||||
|
||||
export const ButtonNodeMaker = NodeManager.register(
|
||||
"ButtonNode",
|
||||
ButtonNode,
|
||||
(pre: ButtonNodeDataProps, { node, flipped }) => ({
|
||||
...node,
|
||||
...pre,
|
||||
flipped,
|
||||
})
|
||||
);
|
||||
@@ -0,0 +1,12 @@
|
||||
import { ReactNode } from "react";
|
||||
|
||||
export type ButtonNodeDataProps = {
|
||||
icon: ReactNode;
|
||||
onClick: () => void;
|
||||
};
|
||||
|
||||
export type ButtonNodeProps = ButtonNodeDataProps & {
|
||||
width: number;
|
||||
height: number;
|
||||
flipped: boolean;
|
||||
};
|
||||
@@ -26,7 +26,7 @@
|
||||
max-width: var(--child-width);
|
||||
max-height: var(--child-height);
|
||||
|
||||
border-color: var(--child-color);
|
||||
border-color: var(--node-color);
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
border-radius: 10px;
|
||||
@@ -36,4 +36,11 @@
|
||||
|
||||
.collection-nodes .react-flow__handle {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.collection-nodes--total {
|
||||
position: absolute;
|
||||
bottom: 0.25em;
|
||||
right: 0.25em;
|
||||
font-size: 0.5em;
|
||||
}
|
||||
@@ -52,6 +52,9 @@ export const CollectionNode: FC<CollectionNodeProps> = ({
|
||||
position={flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
)}
|
||||
<span className="collection-nodes--total">
|
||||
{nodes.length} node{nodes.length !== 1 ? "s" : ""}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -5,9 +5,24 @@
|
||||
align-items: center;
|
||||
cursor: default;
|
||||
|
||||
max-height: var(--sequence-node-height);
|
||||
max-width: var(--sequence-node-width);
|
||||
height: var(--sequence-node-height);
|
||||
width: var(--sequence-node-width);
|
||||
min-height: var(--sequence-node-height);
|
||||
min-width: var(--sequence-node-width);
|
||||
|
||||
/* outline: 1px dotted white; */
|
||||
}
|
||||
|
||||
.sequence-node.disabled .sequence-node--inner {
|
||||
background-color: var(--mui-grey-500);
|
||||
}
|
||||
|
||||
.sequence-node.enabled .sequence-node--inner {
|
||||
background-color: var(--mui-background-default);
|
||||
}
|
||||
|
||||
.sequence-node--inner {
|
||||
position: relative;
|
||||
display: flex;
|
||||
@@ -20,6 +35,15 @@
|
||||
border-left-width: 1px;
|
||||
border-right-width: 1px;
|
||||
border-radius: 10px;
|
||||
|
||||
|
||||
border-bottom-color: var(--mui-grey-500);
|
||||
border-left-color: var(--mui-grey-700);
|
||||
border-right-color: var(--mui-grey-700);
|
||||
border-top-color: var(--sequence-node-color);
|
||||
max-height: calc(var(--sequence-node-height) - 1em);
|
||||
max-width: calc(var(--sequence-node-width) - 1em);
|
||||
min-width: calc(max(var(--sequence-node-width) / 2, var(--sequence-node-height) / 2));
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +53,10 @@
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
overflow: hidden;
|
||||
|
||||
max-height: calc(var(--sequence-node-height) - 1em);
|
||||
max-width: calc(var(--sequence-node-width) - 1em);
|
||||
min-height: 1em;
|
||||
}
|
||||
|
||||
.sequence-node--label span {
|
||||
|
||||
@@ -17,6 +17,7 @@ export default {
|
||||
width: dgconst.NODE_WIDTH,
|
||||
label: "test",
|
||||
hasInput: true,
|
||||
enabled: true,
|
||||
},
|
||||
} as ComponentMeta<typeof SequenceNode>;
|
||||
|
||||
|
||||
@@ -35,11 +35,9 @@ export const SequenceNodeElement: FC<
|
||||
onSetEnabled,
|
||||
children,
|
||||
}) => {
|
||||
const theme = useTheme();
|
||||
const confirm = useConfirm();
|
||||
const snackbr = useSnackbar();
|
||||
const nodeColor = useSequenceNodeColor(color);
|
||||
console.log({ onAdd });
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
@@ -48,26 +46,15 @@ export const SequenceNodeElement: FC<
|
||||
flipped ? "flipped" : "",
|
||||
enabled ? "enabled" : "disabled",
|
||||
].join(" ")}
|
||||
style={{
|
||||
height,
|
||||
width,
|
||||
}}
|
||||
style={
|
||||
{
|
||||
"--sequence-node-height": height + "px",
|
||||
"--sequence-node-width": width + "px",
|
||||
"--sequence-node-color": nodeColor,
|
||||
} as any
|
||||
}
|
||||
>
|
||||
<div
|
||||
className="sequence-node--inner"
|
||||
style={{
|
||||
maxHeight: `calc(${height}px - 1em)`,
|
||||
maxWidth: `calc(${width}px - 1em)`,
|
||||
minWidth: Math.max(width / 2, height / 2),
|
||||
borderBottomColor: theme.palette.grey[500],
|
||||
borderLeftColor: theme.palette.grey[700],
|
||||
borderRightColor: theme.palette.grey[700],
|
||||
borderTopColor: nodeColor,
|
||||
backgroundColor: enabled
|
||||
? theme.palette.background.default
|
||||
: theme.palette.grey[600],
|
||||
}}
|
||||
>
|
||||
<div className="sequence-node--inner">
|
||||
<SpeedDial
|
||||
icon={
|
||||
<SettingsOutlinedIcon className="sequence-node--actions-btn" />
|
||||
@@ -131,22 +118,10 @@ export const SequenceNodeElement: FC<
|
||||
/>
|
||||
)}
|
||||
{children}
|
||||
<div
|
||||
className="sequence-node--label"
|
||||
style={{
|
||||
maxHeight: `calc(${height}px - 1em)`,
|
||||
maxWidth: `calc(${width}px - 1em)`,
|
||||
minHeight: `1em`,
|
||||
}}
|
||||
>
|
||||
<div className="sequence-node--label">
|
||||
{typeof label === "string" ? <span>{label}</span> : label}
|
||||
</div>
|
||||
</div>
|
||||
{/* {onAddNode && (
|
||||
<div className="sequence-node--midway-options">
|
||||
<ButtonIcon icon={<Add />} onClick={onAddNode} />
|
||||
</div>
|
||||
)} */}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { Node, NodeTypes } from "react-flow-renderer";
|
||||
import { NodeManager } from "./NodeManager";
|
||||
import { CollectionNodeProps } from "./CollectionNode/types";
|
||||
import { SequenceNodeProps } from "./SequenceNode/types";
|
||||
import { ButtonNodeProps } from "./ButtonNode/types";
|
||||
|
||||
export const nodeTypes: NodeTypes = NodeManager.nodeTypes;
|
||||
export type DAGAutomationFlowNode = Node<any>;
|
||||
export type DAGAutomationFlowNode = Node<
|
||||
CollectionNodeProps | SequenceNodeProps | ButtonNodeProps
|
||||
>;
|
||||
Reference in new issue
Block a user