From 0c5ef76f4bf111ed6ebfa9d6351952231deff0bf Mon Sep 17 00:00:00 2001 From: Ari Date: Tue, 12 Jul 2022 18:47:18 -0400 Subject: [PATCH] fixed up styling for choose --- .../src/components/DAGGraph/board/index.css | 11 ++ .../src/components/DAGGraph/board/index.tsx | 2 +- .../components/DAGGraph/elements/constants.ts | 4 +- .../components/DAGGraph/elements/distance.ts | 40 ++++++ .../components/DAGGraph/elements/index.tsx | 63 ++++----- .../src/components/DAGGraph/elements/state.ts | 8 +- .../nodes/CollectionNode/CollectionNode.css | 51 +++++-- .../nodes/CollectionNode/CollectionNode.tsx | 29 ++-- .../CollectionNodes.stories.tsx | 126 ++++++++++++++++++ .../DAGGraph/nodes/CollectionNode/index.tsx | 6 + .../DAGGraph/nodes/CollectionNode/types.ts | 4 +- 11 files changed, 283 insertions(+), 61 deletions(-) create mode 100644 webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodes.stories.tsx diff --git a/webapp/src/components/DAGGraph/board/index.css b/webapp/src/components/DAGGraph/board/index.css index 9c8499c..f94866a 100644 --- a/webapp/src/components/DAGGraph/board/index.css +++ b/webapp/src/components/DAGGraph/board/index.css @@ -11,4 +11,15 @@ position: absolute; left: 50%; top: 50%; +} + + +.dag-graphboard rect.react-flow__edge-textbg { + fill: var(--mui-background-paper); + stroke: var(--mui-text-primary); + stroke-width: 0.25px; +} + +.dag-graphboard text.react-flow__edge-text { + fill: var(--mui-text-primary); } \ No newline at end of file diff --git a/webapp/src/components/DAGGraph/board/index.tsx b/webapp/src/components/DAGGraph/board/index.tsx index 9a46e77..80db584 100644 --- a/webapp/src/components/DAGGraph/board/index.tsx +++ b/webapp/src/components/DAGGraph/board/index.tsx @@ -58,7 +58,7 @@ export const DAGGraphBoardInner: FC = ({ ); } // this hack avoids breakage on when zooming - const prefix = Math.floor((((zoom - 0.5) / (2 - 0.5)) * 10) / 3); + const prefix = Math.floor(zoom / (2.5 / 3)); const elements = { nodes: state.data.elements.nodes.map((n) => ({ ...n, diff --git a/webapp/src/components/DAGGraph/elements/constants.ts b/webapp/src/components/DAGGraph/elements/constants.ts index 43ff72b..1e48448 100644 --- a/webapp/src/components/DAGGraph/elements/constants.ts +++ b/webapp/src/components/DAGGraph/elements/constants.ts @@ -7,7 +7,7 @@ export const CIRCLE_SIZE = 40 as const; export const DEFAULT_DIMS: DAGDims = { trigger: { height: NODE_HEIGHT * 3, - width: NODE_WIDTH * 4.5, + width: NODE_WIDTH * 1.5, }, node: { height: NODE_HEIGHT, @@ -15,7 +15,7 @@ export const DEFAULT_DIMS: DAGDims = { }, condition: { height: NODE_HEIGHT * 3, - width: NODE_WIDTH * 4.5, + width: NODE_WIDTH * 1.5, }, circle: { height: CIRCLE_SIZE, diff --git a/webapp/src/components/DAGGraph/elements/distance.ts b/webapp/src/components/DAGGraph/elements/distance.ts index b3f96a7..d28e703 100644 --- a/webapp/src/components/DAGGraph/elements/distance.ts +++ b/webapp/src/components/DAGGraph/elements/distance.ts @@ -22,6 +22,46 @@ 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, + dims: DAGDims, + incrementBy: keyof DAGEntities = "condition" +) => + dims.flipped + ? { + x: + relatedPos.x + + (dims.distanceFactor.node - 1) * dims[incrementBy].width, + y: offsetPos.y, + } + : { + y: + relatedPos.y + + (dims.distanceFactor.node - 1) * dims[incrementBy].height, + x: offsetPos.x, + }; + export const moveFromTo = ( from: keyof DAGEntities, to: keyof DAGEntities, diff --git a/webapp/src/components/DAGGraph/elements/index.tsx b/webapp/src/components/DAGGraph/elements/index.tsx index d1b4817..2754936 100644 --- a/webapp/src/components/DAGGraph/elements/index.tsx +++ b/webapp/src/components/DAGGraph/elements/index.tsx @@ -97,7 +97,7 @@ export const makeTriggerNodes: ElementMaker = ( state.addNode( CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, { ...dims.trigger, - color: "red", + collectionType: "trigger", onAddNode: () => stateUpdater.basic.trigger.addNode(null), nodes: nodes.map((node, index) => ({ enabled: node.enabled ?? true, @@ -117,7 +117,7 @@ export const makeConditionNodes: ElementMaker = ( state.addNode( CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, { ...dims.condition, - color: "blue", + collectionType: "condition", hasInput: !!lastNodeId, onAddNode: () => stateUpdater.basic.condition.addNode(null), nodes: nodes.map((node, index) => ({ @@ -127,6 +127,7 @@ export const makeConditionNodes: ElementMaker = ( })), }) ); + console.log("pre", { lastNodeId, stateLast: state.lastNodeId }); if (lastNodeId && state.lastNodeId) { state.addEdge(lastNodeId, state.lastNodeId, true); } @@ -227,25 +228,19 @@ export const makeChooseeNodes: ElementMaker = (nodes, args) => { ) => { 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, - // }; - // } + if (condition !== null) { + const conditionState = makeConditionNodes(condition, { + ...args, + position: pos, + lastNodeId, + nodeIndex: index, + nodeId: `${args.nodeId}-choose-${nodeIndex}-${index}-condition`, + }); + conditionState.data.edges[0].label = `Condition #${index}`; + state.extend(conditionState); + pos = distance.moveFromTo("condition", "node", pos, args.dims); + lastNodeId = conditionState.lastNodeId; + } const output = makeSequenceNodes(sequence, { ...args, position: pos, @@ -254,21 +249,19 @@ export const makeChooseeNodes: ElementMaker = (nodes, args) => { nodeId: `${args.nodeId}-choose-${index}-sequence`, }); if (lastNodeId && output.data.nodes.length > 0) { - state.addEdge(lastNodeId, output.data.nodes[0].id, true); + state.addEdge( + lastNodeId, + output.data.nodes[0].id, + true, + condition === null ? "else" : undefined + ); } - 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, - }; + offsetPos = distance.moveAlongRelativeTo( + offsetPos, + output.bbox[1], + args.dims, + condition === null ? "node" : "condition" + ); state.extend(output); }; diff --git a/webapp/src/components/DAGGraph/elements/state.ts b/webapp/src/components/DAGGraph/elements/state.ts index 5f4ca20..1677edc 100644 --- a/webapp/src/components/DAGGraph/elements/state.ts +++ b/webapp/src/components/DAGGraph/elements/state.ts @@ -49,11 +49,17 @@ export class DAGElementsState { return node; } - public addEdge(from: string, to: string, animated: boolean = false) { + public addEdge( + from: string, + to: string, + animated: boolean = false, + label: string | undefined = undefined + ) { this.data.edges.push({ id: `${from}->${to}`, source: from, target: to, + label, animated, }); } diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css index 82c5d06..8f2644f 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css @@ -1,7 +1,10 @@ .collection-nodes--wrap { - --padding: 1.5em; - --child-height: calc(var(--node-height) - 2*var(--padding)); - --child-width: calc(var(--node-width) - 2*var(--padding)); + --padding-left: 1.5em; + --padding-right: 3em; + --padding-top: 2.25em; + --padding-bottom: 0.25em; + --child-height: calc(var(--node-height) - var(--padding-top) - var(--padding-bottom)); + --child-width: calc(var(--node-width) - var(--padding-left) - var(--padding-right)); min-width: var(--node-width); min-height: var(--node-height); @@ -18,20 +21,34 @@ .collection-nodes { position: relative; - display: flex; - flex-wrap: wrap; - flex-direction: row; - align-items: center; - gap: 0em 1em; - max-width: var(--child-width); - max-height: var(--child-height); border-color: var(--node-color); border-style: solid; border-width: 1px; border-radius: 10px; + + max-width: max-content; + max-height: max-content; + padding-right: var(--padding-right); +} + +.collection-nodes--inner { + display: flex; + flex-wrap: wrap; + flex-direction: column; + align-items: center; + justify-content: space-around; + gap: 0 1em; + max-width: var(--child-width); + max-height: var(--child-height); + min-height: calc(var(--child-height) / 4); + min-width: var(--child-width); + + padding-left: var(--padding-left); + padding-top: var(--padding-top); + padding-bottom: var(--padding-bottom); + overflow: auto; - padding: var(--padding); } .collection-nodes .react-flow__handle { @@ -43,4 +60,16 @@ bottom: 0.25em; right: 0.25em; font-size: 0.5em; +} + +.collection-nodes .add { + position: absolute; + bottom: calc(50% - var(--padding-bottom)); + right: 0.25em; +} + +.collection-nodes--title { + position: absolute; + top: 0.5em; + left: calc(var(--child-width)/2); } \ No newline at end of file diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx index bfcd437..fc247d1 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx @@ -7,6 +7,7 @@ import { SequenceNodeElement } from "../SequenceNode/SequenceNodeElement"; import { CollectionNodeProps } from "./types"; import { Handle, Position } from "react-flow-renderer"; import { useSequenceNodeColor } from "../SequenceNode/util"; +import { prettyName } from "utils/formatting"; export const CollectionNode: FC = ({ onAddNode, @@ -17,6 +18,7 @@ export const CollectionNode: FC = ({ flipped, color, hasInput = false, + collectionType, }) => { const nodeColor = useSequenceNodeColor(color); @@ -32,22 +34,29 @@ export const CollectionNode: FC = ({ } >
- {nodes.map((n, i) => ( - - ))} - } onClick={onAddNode} /> + + {prettyName(collectionType)}s + +
+ {nodes.map((n, i) => ( + + ))} +
+ } onClick={onAddNode} /> {hasInput && ( diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodes.stories.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodes.stories.tsx new file mode 100644 index 0000000..233771d --- /dev/null +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNodes.stories.tsx @@ -0,0 +1,126 @@ +import React, { FC } from "react"; +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { Page } from "components/Page"; +import { CollectionNode } from "./CollectionNode"; +import { DAGGraphBoard } from "components/DAGGraph/board"; +import { CollectionNodeMaker } from "./index"; +import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants"; + +const Demo: FC<{ + height: number; + width: number; + totalChildNodes: number; + onAddNode: () => void; + enabledChild: boolean; +}> = (args) => { + return ( + + {}} + state={{ + ready: true, + data: { + elements: { + edges: [], + nodes: ["trogger", "sequence", "condition"].map( + (collectionType: any, i: number) => + CollectionNodeMaker.makeElement( + { + id: collectionType, + position: { + x: 50 + args.width * (i % 3 === 0 ? 1 : 0), + y: 50 + args.height * ((i + 1) % 2 === 0 ? 1 : 0), + }, + }, + DEFAULT_DIMS, + { + hasInput: false, + onAddNode: args.onAddNode, + width: args.width, + height: args.height, + nodes: [ + { + enabled: args.enabledChild, + label: collectionType, + }, + ...Array.from(Array(args.totalChildNodes)).map( + (_, i) => ({ + enabled: true, + label: `Dummy Node #${i + 1}`, + }) + ), + ], + collectionType, + } + ) + ), + }, + }, + }} + /> + + ); +}; + +export default { + title: "DAGGraph/Nodes/Collection", + component: Demo, + parameters: { actions: { argTypesRegex: "^on.*" } }, + args: { + ...DEFAULT_DIMS.condition, + totalChildNodes: 1, + enabledChild: true, + }, +} as ComponentMeta; + +export const Colors: ComponentStory = (args) => ; + +export const Empty: ComponentStory = (args) => { + return ( + + {}} + state={{ + ready: true, + data: { + elements: { + edges: [], + nodes: [ + CollectionNodeMaker.makeElement( + { + id: "only-one", + position: { + x: 50, + y: 50, + }, + }, + DEFAULT_DIMS, + { + hasInput: false, + onAddNode: args.onAddNode, + width: args.width, + height: args.height, + nodes: [ + ...Array.from(Array(args.totalChildNodes)).map( + (_, i) => ({ + enabled: true, + label: `Dummy Node #${i + 1}`, + }) + ), + ], + collectionType: "condition", + } + ), + ], + }, + }, + }} + /> + + ); +}; + +Empty.args = { + ...Empty.args, + totalChildNodes: 0, +}; diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx index cf8fd38..4e169dc 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx @@ -1,12 +1,18 @@ import { NodeManager } from "components/DAGGraph/nodes/NodeManager"; import { CollectionNode } from "./CollectionNode"; import { CollectionNodeDataProps } from "./types"; +import { SequenceNodeColor } from "../SequenceNode/types"; export const CollectionNodeMaker = NodeManager.register( "CollectionNode", CollectionNode, (pre: CollectionNodeDataProps, { node, flipped }) => ({ ...pre, + color: (pre.collectionType === "condition" + ? "blue" + : pre.collectionType === "sequence" + ? "green" + : "red") as SequenceNodeColor, sequenceNode: node, flipped, }) diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts b/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts index 6184cf2..47cf2b7 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts @@ -1,6 +1,7 @@ import { Size } from "components/DAGGraph/elements/types"; import { ReactNode } from "react"; import { SequenceNodeActions, SequenceNodeColor } from "../SequenceNode/types"; +import { AutomationActionData } from "types/automations"; export type CollectionNodeChildProps = Partial & { enabled: boolean; @@ -9,11 +10,12 @@ export type CollectionNodeChildProps = Partial & { export type CollectionNodeDataProps = Size & { nodes: CollectionNodeChildProps[]; onAddNode: () => void; - color: SequenceNodeColor; hasInput?: boolean; + collectionType: keyof AutomationActionData; }; export type CollectionNodeProps = CollectionNodeDataProps & { sequenceNode: Size; flipped: boolean; + color: SequenceNodeColor; };