From de2307901b633493a0320256ee8a2a9a3ef3cdbc Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 13 Jul 2022 10:52:56 -0400 Subject: [PATCH] repeat node initial support --- .../src/components/DAGGraph/board/index.css | 4 + .../components/DAGGraph/elements/constants.ts | 5 - .../components/DAGGraph/elements/distance.ts | 22 +--- .../components/DAGGraph/elements/index.tsx | 120 +++++++++++++++--- .../DAGGraph/elements/outputState.ts | 49 +++++-- .../src/components/DAGGraph/elements/state.ts | 3 +- .../src/components/DAGGraph/elements/types.ts | 1 - .../DAGGraph/nodes/ButtonNode/ButtonNode.tsx | 4 + .../nodes/CollectionNode/CollectionNode.css | 3 - .../nodes/CollectionNode/CollectionNode.tsx | 12 +- .../CollectionNodes.stories.tsx | 100 ++++++++++++++- .../SequenceNode/SequenceNode.stories.tsx | 16 +-- .../src/components/DAGGraph/updater/index.ts | 39 +++++- 13 files changed, 301 insertions(+), 77 deletions(-) diff --git a/webapp/src/components/DAGGraph/board/index.css b/webapp/src/components/DAGGraph/board/index.css index f94866a..922387a 100644 --- a/webapp/src/components/DAGGraph/board/index.css +++ b/webapp/src/components/DAGGraph/board/index.css @@ -14,6 +14,10 @@ } +.dag-graphboard .react-flow__handle { + opacity: 0; +} + .dag-graphboard rect.react-flow__edge-textbg { fill: var(--mui-background-paper); stroke: var(--mui-text-primary); diff --git a/webapp/src/components/DAGGraph/elements/constants.ts b/webapp/src/components/DAGGraph/elements/constants.ts index 1e48448..0b3d32d 100644 --- a/webapp/src/components/DAGGraph/elements/constants.ts +++ b/webapp/src/components/DAGGraph/elements/constants.ts @@ -2,7 +2,6 @@ import { DAGDims } from "./types"; export const NODE_WIDTH = 200 as const; export const NODE_HEIGHT = 80 as const; -export const CIRCLE_SIZE = 40 as const; export const DEFAULT_DIMS: DAGDims = { trigger: { @@ -17,10 +16,6 @@ export const DEFAULT_DIMS: DAGDims = { height: NODE_HEIGHT * 3, width: NODE_WIDTH * 1.5, }, - circle: { - height: CIRCLE_SIZE, - width: CIRCLE_SIZE, - }, distanceFactor: { node: 1.5, collection: 1, diff --git a/webapp/src/components/DAGGraph/elements/distance.ts b/webapp/src/components/DAGGraph/elements/distance.ts index d28e703..7bda023 100644 --- a/webapp/src/components/DAGGraph/elements/distance.ts +++ b/webapp/src/components/DAGGraph/elements/distance.ts @@ -1,4 +1,4 @@ -import { DAGDims, DAGEntities, Size } from "./types"; +import { DAGDims, DAGEntities } from "./types"; import { XYPosition } from "react-flow-renderer"; export const moveAlong = ( @@ -22,26 +22,6 @@ export const moveAlong = ( return !dims.flipped ? moveOnX : moveOnY; }; -export const moveAlongOnce = ( - by: keyof DAGEntities, - startPoint: XYPosition, - dims: DAGDims, - reversed: boolean = false -): XYPosition => { - const moveOnY = { - y: startPoint.y + dims[by].height * (dims.distanceFactor.node - 1), - x: startPoint.x, - }; - const moveOnX = { - x: startPoint.x + dims[by].width * (dims.distanceFactor.node - 1), - y: startPoint.y, - }; - if (reversed) { - return dims.flipped ? moveOnX : moveOnY; - } - return !dims.flipped ? moveOnX : moveOnY; -}; - export const moveAlongRelativeTo = ( offsetPos: XYPosition, relatedPos: XYPosition, diff --git a/webapp/src/components/DAGGraph/elements/index.tsx b/webapp/src/components/DAGGraph/elements/index.tsx index 632ae9a..42c7253 100644 --- a/webapp/src/components/DAGGraph/elements/index.tsx +++ b/webapp/src/components/DAGGraph/elements/index.tsx @@ -14,7 +14,7 @@ import { } from "types/automations"; import { AutomationCondition } from "types/automations/conditions"; import { CollectionNodeMaker } from "../nodes/CollectionNode"; -import { ChooseAction } from "types/automations/actions"; +import { ChooseAction, RepeatAction } from "types/automations/actions"; import { XYPosition } from "react-flow-renderer"; import { DAGElementsOutputState } from "./outputState"; import { SequenceNodeMaker } from "../nodes/SequenceNode"; @@ -31,7 +31,7 @@ export const useAutomationNodes = ( ...args.dims, }; const state = useDAGElementsState(); - const outputState = new DAGElementsOutputState(); + const outputState = new DAGElementsOutputState(dims.position, dims); outputState.extend( makeTriggerNodes(automation.trigger, { ...args, @@ -51,7 +51,7 @@ export const useAutomationNodes = ( position: distance.moveFromTo( "trigger", "condition", - outputState.lastNodePos, + outputState.getLastNodePos(), dims ), nodeId: `${dims.flipped}-condition`, @@ -68,7 +68,7 @@ export const useAutomationNodes = ( position: distance.moveFromTo( "condition", "node", - outputState.lastNodePos, + outputState.getLastNodePos(), dims ), nodeId: `${dims.flipped}-sequence`, @@ -84,7 +84,7 @@ export const makeTriggerNodes: ElementMaker = ( nodes, { dims, namer, stateUpdater, nodeId, position } ) => { - const outputState = new DAGElementsOutputState(); + const outputState = new DAGElementsOutputState(position, dims); outputState.addNode( CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, { ...dims.trigger, @@ -104,7 +104,7 @@ export const makeConditionNodes: ElementMaker = ( nodes, { nodeId, stateUpdater, position, dims, namer, lastNodeId } ) => { - const outputState = new DAGElementsOutputState(); + const outputState = new DAGElementsOutputState(position, dims); outputState.addNode( CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, { ...dims.condition, @@ -119,7 +119,7 @@ export const makeConditionNodes: ElementMaker = ( }) ); if (lastNodeId && outputState.lastNodeId) { - outputState.addEdge(lastNodeId, outputState.lastNodeId, true); + outputState.addEdge(lastNodeId, outputState.lastNodeId, { animated: true }); } return outputState; }; @@ -128,18 +128,19 @@ export const makeSequenceNodes: ElementMaker = ( nodes, args ) => { - const outputState = new DAGElementsOutputState(); const { lastNodeId, position, dims, namer, stateUpdater, state } = args; + const outputState = new DAGElementsOutputState(position, dims); nodes.forEach((node, nodeIndex) => { // the JSON.stringify is hack to make the edges render nicely const nodeId = `${args.nodeId}-${ args.nodeIndex }-${nodeIndex}-${JSON.stringify(node)}`; + outputState.incNextPos("node"); const element = outputState.addNode( SequenceNodeMaker.makeElement( { id: nodeId, - position: distance.moveAlong("node", position, nodeIndex, dims), + position: outputState.nextPos, }, dims, { @@ -163,8 +164,20 @@ export const makeSequenceNodes: ElementMaker = ( outputState.extend( makeChooseeNodes([node], { ...args, + nodeId, lastNodeId: element.id, - position: outputState.lastNodePos, + position: outputState.getLastNodePos(), + nodeIndex, + }), + true + ); + } else if ("repeat" in node) { + outputState.extend( + makeRepeatNodes([node], { + ...args, + nodeId, + lastNodeId: element.id, + position: outputState.getLastNodePos(), nodeIndex, }), true @@ -176,7 +189,12 @@ export const makeSequenceNodes: ElementMaker = ( ButtonNodeMaker.makeElement( { id: `${args.nodeId}-add-new`, - position: distance.moveAlong("node", position, nodes.length, dims), + position: distance.moveAlong( + "node", + outputState.getLastNodePos(position), + 1, + dims + ), }, dims, { @@ -195,7 +213,7 @@ export const makeSequenceNodes: ElementMaker = ( }; export const makeChooseeNodes: ElementMaker = (nodes, args) => { - const outputState = new DAGElementsOutputState(); + const outputState = new DAGElementsOutputState(args.position, args.dims); if (nodes.length !== 1) { throw new Error("makeChooseNodes only access a list of 1"); } @@ -227,7 +245,7 @@ export const makeChooseeNodes: ElementMaker = (nodes, args) => { nodeIndex: j, nodeId: `${args.nodeId}-choose-${j}-condition`, }); - conditionState.data.edges[0].label = `Condition #${j}`; + conditionState.data.edges[0].label = `Condition #${j + 1}`; outputState.extend(conditionState); pos = distance.moveFromTo("condition", "node", pos, args.dims); lastNodeId = conditionState.lastNodeId; @@ -243,12 +261,10 @@ export const makeChooseeNodes: ElementMaker = (nodes, args) => { nodeId: `${args.nodeId}-choose-${j}-sequence`, }); if (lastNodeId && output.data.nodes.length > 0) { - outputState.addEdge( - lastNodeId, - output.data.nodes[0].id, - true, - j === "else" ? "else" : undefined - ); + outputState.addEdge(lastNodeId, output.data.nodes[0].id, { + animated: true, + label: j === "else" ? "else" : undefined, + }); } offsetPos = distance.moveAlongRelativeTo( offsetPos, @@ -269,3 +285,69 @@ export const makeChooseeNodes: ElementMaker = (nodes, args) => { return outputState; }; + +export const makeRepeatNodes: ElementMaker = (nodes, args) => { + const outputState = new DAGElementsOutputState(args.position, args.dims); + if (nodes.length !== 1) { + throw new Error("makeChooseNodes only access a list of 1"); + } + const node = nodes[0]; + if ( + (node.enabled ?? true) && + !args.state.get(args.lastNodeId ?? "").isClosed + ) { + // offset position + let offsetPos: XYPosition = { + x: args.position.x + args.dims.distanceFactor.node * args.dims.node.width, + y: + args.position.y + args.dims.distanceFactor.node * args.dims.node.height, + }; + const stateUpdater = args.stateUpdater.createRepeatNodeUpdater( + args.nodeIndex + ); + let pos = offsetPos; + let lastNodeId = args.lastNodeId; + const conditionState = makeConditionNodes(node.repeat.while, { + ...args, + stateUpdater, + position: pos, + lastNodeId, + nodeIndex: 0, + nodeId: `${args.nodeId}-repeat-while`, + }); + conditionState.data.edges[0].label = `While`; + outputState.extend(conditionState); + pos = distance.moveFromTo("condition", "node", pos, args.dims); + lastNodeId = conditionState.lastNodeId; + const sequenceState = makeSequenceNodes(node.repeat.sequence, { + ...args, + stateUpdater, + position: pos, + lastNodeId: undefined, + nodeIndex: 0, + nodeId: `${args.nodeId}-repeat-sequence`, + }); + if (lastNodeId && sequenceState.data.nodes.length > 0) { + outputState.addEdge(lastNodeId, sequenceState.data.nodes[0].id, { + animated: true, + label: "do", + }); + } + offsetPos = distance.moveAlongRelativeTo( + offsetPos, + sequenceState.bbox[1], + args.dims, + "condition" + ); + if (sequenceState.lastNodeId && conditionState.lastNodeId) { + outputState.addEdge(sequenceState.lastNodeId, conditionState.lastNodeId, { + animated: true, + label: "repeat", + targetHandle: "return", + }); + } + outputState.extend(sequenceState); + } + + return outputState; +}; diff --git a/webapp/src/components/DAGGraph/elements/outputState.ts b/webapp/src/components/DAGGraph/elements/outputState.ts index dfe47ba..e087375 100644 --- a/webapp/src/components/DAGGraph/elements/outputState.ts +++ b/webapp/src/components/DAGGraph/elements/outputState.ts @@ -1,6 +1,8 @@ +import { MarkerType, Edge, XYPosition } from "react-flow-renderer"; import { DAGAutomationFlowNode } from "../nodes"; import { addSize, pointApply } from "./math"; -import { Bbox, ElementData, LastNode } from "./types"; +import { Bbox, DAGDims, DAGEntities, ElementData, LastNode } from "./types"; +import { moveAlong } from "./distance"; export class DAGElementsOutputState { public data: ElementData = { @@ -13,6 +15,8 @@ export class DAGElementsOutputState { { x: 0, y: 0 }, ]; + constructor(public nextPos: XYPosition, public dims: DAGDims) {} + public extend( other: DAGElementsOutputState, ignoreLastNode: boolean = false @@ -23,6 +27,18 @@ export class DAGElementsOutputState { this.bbox[1] = pointApply(this.bbox[1], other.bbox[1], Math.max); if (!ignoreLastNode) { this.lastNode = other.lastNode; + this.nextPos = (this.lastNode as any).pos; + } + if (this.dims.flipped) { + this.nextPos = { + y: Math.max(this.nextPos.y, other.bbox[1].y), + x: this.nextPos.x, + }; + } else { + this.nextPos = { + x: Math.max(this.nextPos.x, other.bbox[1].x), + y: this.nextPos.y, + }; } } @@ -32,7 +48,9 @@ export class DAGElementsOutputState { animatedEdge: boolean = false ) { if (addEdge && this.lastNodeId) { - this.addEdge(this.lastNodeId, node.id, animatedEdge); + this.addEdge(this.lastNodeId, node.id, { + animated: animatedEdge, + }); } this.data.nodes.push(node); this.bbox[0] = pointApply(this.bbox[0], node.position, Math.min); @@ -52,18 +70,17 @@ export class DAGElementsOutputState { return node; } - public addEdge( - from: string, - to: string, - animated: boolean = false, - label: string | undefined = undefined - ) { + public addEdge(from: string, to: string, edgeProps: Partial = {}) { this.data.edges.push({ + markerEnd: { + type: MarkerType.ArrowClosed, + width: 25, + height: 25, + }, + ...edgeProps, id: `${from}->${to}`, source: from, target: to, - label, - animated, }); } @@ -74,10 +91,20 @@ export class DAGElementsOutputState { return undefined; } - public get lastNodePos() { + public getLastNodePos(orThis: XYPosition | undefined = undefined) { if (this.lastNode) { return this.lastNode.pos; } + if (orThis) { + return orThis; + } throw Error("missing lastnode"); } + + public incNextPos(by: keyof DAGEntities) { + if (!this.lastNode) { + return; + } + this.nextPos = moveAlong(by, this.nextPos, 1, this.dims); + } } diff --git a/webapp/src/components/DAGGraph/elements/state.ts b/webapp/src/components/DAGGraph/elements/state.ts index a6b30a6..b69b300 100644 --- a/webapp/src/components/DAGGraph/elements/state.ts +++ b/webapp/src/components/DAGGraph/elements/state.ts @@ -7,7 +7,6 @@ export type DAGNodeState = Partial<{ export type DAGElementsState = ReturnType; export const useDAGElementsState = () => { const [data, setData] = useState>({}); - console.log(data); return { get(nodeId: string): DAGNodeState { return data[nodeId] ?? {}; @@ -28,7 +27,7 @@ export const useDAGElementsState = () => { }, getIsClosedActions(nodeId: string, node: AutomationNode) { - if ("choose" in node) { + if ("choose" in node || "repeat" in node) { return { isClosed: this.get(nodeId).isClosed ?? false, setIsClosed: (v: boolean) => this.set(nodeId, "isClosed", v), diff --git a/webapp/src/components/DAGGraph/elements/types.ts b/webapp/src/components/DAGGraph/elements/types.ts index 2d51855..bec919b 100644 --- a/webapp/src/components/DAGGraph/elements/types.ts +++ b/webapp/src/components/DAGGraph/elements/types.ts @@ -16,7 +16,6 @@ export type ElementData = { export type DAGEntityDim = Size; export type DAGEntities = { node: DAGEntityDim; - circle: DAGEntityDim; condition: DAGEntityDim; trigger: DAGEntityDim; }; diff --git a/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx b/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx index ac7f681..1a893ac 100644 --- a/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx +++ b/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx @@ -30,6 +30,10 @@ export const ButtonNode: FC = ({ position={flipped ? Position.Top : Position.Left} /> {icon} + } onClick={onClick} diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css index 8f2644f..395c514 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css @@ -51,9 +51,6 @@ overflow: auto; } -.collection-nodes .react-flow__handle { - opacity: 0; -} .collection-nodes--total { position: absolute; diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx index fc247d1..95319cd 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx @@ -17,7 +17,6 @@ export const CollectionNode: FC = ({ width, flipped, color, - hasInput = false, collectionType, }) => { const nodeColor = useSequenceNodeColor(color); @@ -54,13 +53,20 @@ export const CollectionNode: FC = ({ type="source" position={flipped ? Position.Bottom : Position.Right} /> - {hasInput && ( + <> - )} + + {nodes.length} node{nodes.length !== 1 ? "s" : ""} diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodes.stories.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodes.stories.tsx index 233771d..0b6c9c0 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodes.stories.tsx +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodes.stories.tsx @@ -5,14 +5,18 @@ import { CollectionNode } from "./CollectionNode"; import { DAGGraphBoard } from "components/DAGGraph/board"; import { CollectionNodeMaker } from "./index"; import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants"; +import { ButtonNodeMaker } from "../ButtonNode"; +import { AddCircle } from "@mui/icons-material"; +import { MarkerType } from "react-flow-renderer"; -const Demo: FC<{ +type DemoProps = { height: number; width: number; totalChildNodes: number; onAddNode: () => void; enabledChild: boolean; -}> = (args) => { +}; +const Demo: FC = (args) => { return ( = (args) => { + return ( + + {}} + state={{ + ready: true, + data: { + elements: { + edges: [ + { + id: "1", + source: "root", + target: "btn", + label: "1", + }, + { + id: "2", + target: "root", + source: "btn", + animated: true, + targetHandle: "return", + label: "2", + markerEnd: { + type: MarkerType.ArrowClosed, + width: 25, + height: 25, + }, + }, + { + id: "3", + target: "root", + source: "btn", + label: "3", + }, + ], + nodes: [ + ButtonNodeMaker.makeElement( + { + id: "btn", + position: { + x: 50 + (args.width - DEFAULT_DIMS.node.width) / 2, + y: + 50 + + args.height * + 2 * + DEFAULT_DIMS.distanceFactor.collection, + }, + }, + DEFAULT_DIMS, + { + icon: , + onClick: () => {}, + } + ), + CollectionNodeMaker.makeElement( + { + id: "root", + position: { + x: 50, + y: 50, + }, + }, + DEFAULT_DIMS, + { + onAddNode: args.onAddNode, + width: args.width, + height: args.height, + nodes: [ + { + enabled: args.enabledChild, + label: "condition", + }, + ...Array.from(Array(args.totalChildNodes)).map( + (_, i) => ({ + enabled: true, + label: `Dummy Node #${i + 1}`, + }) + ), + ], + collectionType: "condition", + } + ), + ], + }, + }, + }} + /> + + ); +}; diff --git a/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNode.stories.tsx b/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNode.stories.tsx index 372566f..4c176a8 100644 --- a/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNode.stories.tsx +++ b/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNode.stories.tsx @@ -93,15 +93,15 @@ export const OnMoveAllDirections = Template.bind({}); OnMoveAllDirections.args = { ...OnMoveAllDirections.args, onMove: { - up: console.log, - down: console.log, - left: console.log, - right: console.log, + up: console.info, + down: console.info, + left: console.info, + right: console.info, }, onAdd: { - up: console.log, - down: console.log, - left: console.log, - right: console.log, + up: console.info, + down: console.info, + left: console.info, + right: console.info, }, }; diff --git a/webapp/src/components/DAGGraph/updater/index.ts b/webapp/src/components/DAGGraph/updater/index.ts index 9c0c895..da71d88 100644 --- a/webapp/src/components/DAGGraph/updater/index.ts +++ b/webapp/src/components/DAGGraph/updater/index.ts @@ -6,8 +6,7 @@ import { ModalState } from "../board/types"; import { SequenceNodeActions } from "../nodes/SequenceNode/types"; import { DAGUpdaterArgs } from "./types"; import { mapAutoActionKeyToNodeType } from "./util"; -import { ChooseAction } from "types/automations/actions"; -import { AutomationCondition } from "types/automations/conditions"; +import { ChooseAction, RepeatAction } from "types/automations/actions"; export const createBasicUpdater = ( @@ -228,6 +227,42 @@ export const createUpdater = ( }); } }, + createRepeatNodeUpdater(i: number) { + const { repeat } = args.sequence.data[i] as RepeatAction; + return createUpdater({ + openModal: args.openModal, + condition: { + data: repeat.while ?? [], + onUpdate: (upd: any) => + basic.sequence.updateNode( + { + repeat: { + ...repeat, + while: upd, + }, + }, + i + ), + }, + sequence: { + data: repeat.sequence ?? [], + onUpdate: (upd) => + basic.sequence.updateNode( + { + repeat: { + ...repeat, + sequence: upd, + }, + }, + i + ), + }, + trigger: { + data: [], + onUpdate: console.warn, + }, + }); + }, }; };