From 6722854700b10ac06947ef1210ee2640194b9974 Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 22 Jun 2022 21:20:28 -0400 Subject: [PATCH] complex movements --- webapp/src/components/DAGFlow/DAGNode.css | 32 ++++ .../components/DAGFlow/DAGNode.stories.tsx | 111 ++++++++----- webapp/src/components/DAGFlow/DAGNode.tsx | 140 ++++++++++++---- webapp/src/components/DAGFlow/flowDataMods.ts | 65 ++------ webapp/src/components/DAGFlow/helpers.tsx | 30 +++- webapp/src/components/DAGFlow/index.tsx | 15 +- .../components/DAGFlow/sequenceConvertor.ts | 48 +++--- webapp/src/components/DAGFlow/styles.ts | 150 ++++++++++-------- webapp/src/components/DAGFlow/updater.ts | 35 ++++ 9 files changed, 412 insertions(+), 214 deletions(-) create mode 100644 webapp/src/components/DAGFlow/DAGNode.css diff --git a/webapp/src/components/DAGFlow/DAGNode.css b/webapp/src/components/DAGFlow/DAGNode.css new file mode 100644 index 0000000..fb409e8 --- /dev/null +++ b/webapp/src/components/DAGFlow/DAGNode.css @@ -0,0 +1,32 @@ +.dagnode--actions { + display: flex; + flex-direction: column; + height: 100%; + min-height: 100%; + justify-content: center; + align-items: center; + position: relative; +} + +.dagnode--actions--list.close { + display: none; +} + +.dagnode--actions--list { + position: absolute; + bottom: -3em; + left: 0em; + background: var(--secondary); +} + +.dagnode--actions--move.right { + transform: rotate(180deg); +} + +.dagnode--actions--move.up { + transform: rotate(90deg); +} + +.dagnode--actions--move.down { + transform: rotate(270deg); +} \ No newline at end of file diff --git a/webapp/src/components/DAGFlow/DAGNode.stories.tsx b/webapp/src/components/DAGFlow/DAGNode.stories.tsx index aaed2e5..1a76e4d 100644 --- a/webapp/src/components/DAGFlow/DAGNode.stories.tsx +++ b/webapp/src/components/DAGFlow/DAGNode.stories.tsx @@ -1,48 +1,87 @@ - - import React from "react"; -import { ComponentMeta, ComponentStory } from '@storybook/react'; -import { DAGAutomationFlow } from '.'; -import { useState } from 'react'; +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { DAGAutomationFlow } from "."; +import { useState } from "react"; import * as dgconst from "components/DAGFlow/constants"; -import ReactFlow, { Controls } from 'react-flow-renderer'; +import ReactFlow, { Controls } from "react-flow-renderer"; import { DAGNode } from "./DAGNode"; import { convertToFlowNode } from "./helpers"; import { Page } from "components/Page"; - export default { - title: 'DAGAutomationFlow/Viewer/DAGNode', + title: "DAGAutomationFlow/Viewer/DAGNode", component: DAGAutomationFlow, - parameters: { actions: { argTypesRegex: '^on.*' } }, + parameters: { actions: { argTypesRegex: "^on.*" } }, args: { - "color": "lblue", - "height": dgconst.NODE_HEIGHT, - "width": dgconst.NODE_WIDTH, - "label": "test", - "hasInput": true, - } -} as ComponentMeta + color: "lblue", + height: dgconst.NODE_HEIGHT, + width: dgconst.NODE_WIDTH, + label: "test", + hasInput: true, + }, +} as ComponentMeta; -export const Colors: ComponentStory = args => { +const Template: ComponentStory = (args) => { + return ( + + + + + + ); +}; + +export const Colors: ComponentStory = (args) => { const nodes = ["blue", "red", "green", "lblue"].map((color, i) => ({ - "data": { ...args, color }, - "id": color, - "type": "mynode", - "position": { "x": 50 + dgconst.NODE_HEIGHT * dgconst.DISTANCE_FACTOR * i, "y": 50 + dgconst.NODE_HEIGHT * dgconst.DISTANCE_FACTOR * i } + data: { ...args, color }, + id: color, + type: "mynode", + position: { + x: 50 + dgconst.NODE_HEIGHT * dgconst.DISTANCE_FACTOR * i, + y: 50 + dgconst.NODE_HEIGHT * dgconst.DISTANCE_FACTOR * i, + }, })); - return - - - - -} + return ( + + + + + + ); +}; + +export const OnMoveAllDirections = Template.bind({}); +OnMoveAllDirections.args = { + ...OnMoveAllDirections.args, + onMove: { + up: console.log, + down: console.log, + left: console.log, + right: console.log, + }, +}; diff --git a/webapp/src/components/DAGFlow/DAGNode.tsx b/webapp/src/components/DAGFlow/DAGNode.tsx index 4c6172d..c6f0121 100644 --- a/webapp/src/components/DAGFlow/DAGNode.tsx +++ b/webapp/src/components/DAGFlow/DAGNode.tsx @@ -1,27 +1,74 @@ -import { PencilIcon } from "components/Icons" +import "./DAGNode.css"; +import { PencilIcon } from "components/Icons"; import { NodeColor } from "types/graphs"; import { useNodeStyles } from "./styles"; -import { Handle, Position } from 'react-flow-renderer'; -import { FC } from "react"; +import { Handle, Position } from "react-flow-renderer"; +import { FC, ReactNode, useState } from "react"; +import ClickAwayListener from "@mui/material/ClickAwayListener"; +import IconButton from "@mui/material/IconButton"; +import DeleteForeverIcon from "@mui/icons-material/DeleteForever"; +import SettingsOutlinedIcon from "@mui/icons-material/SettingsOutlined"; +import ArrowBackIcon from "@mui/icons-material/ArrowBack"; +import { createToNodeMakerFunction } from "./helpers"; - -export interface DAGNodeProps { - height: number; - width: number; +export type DAGNodeOnMove = Record< + Direction, + () => void +>; +export type DAGNodeOnMoveEvents = Partial< + DAGNodeOnMove<"up" | "down" | "left" | "right"> +>; +export interface DAGNodeDataProps { onXClick?: () => void; onEditClick?: () => void; + onMove?: DAGNodeOnMoveEvents; color: NodeColor; - label: string; + label: ReactNode; hasInput?: boolean; accentBackground?: boolean; +} +export interface DAGNodeProps extends DAGNodeDataProps { + height: number; + width: number; flipped: boolean; } + +export const makeConditionPoint = createToNodeMakerFunction< + { + totalConditions: number; + onEditClick: () => void; + }, + DAGNodeProps +>((pre, { conditionWidth, conditionHeight, flipped }) => ({ + color: "lblue", + accentBackground: true, + label: `${pre.totalConditions} Condition${ + pre.totalConditions !== 1 ? "s" : "" + }`, + height: conditionHeight, + width: conditionWidth, + hasInput: true, + onEditClick: pre.onEditClick, + flipped, +})); + +export const makeActionPoint = createToNodeMakerFunction< + DAGNodeDataProps, + DAGNodeProps +>((pre, { nodeHeight, nodeWidth, flipped }) => ({ + ...pre, + height: nodeHeight, + width: nodeWidth, + flipped, +})); + export const DAGNode: FC = ({ label, height, width, onXClick, - onEditClick = () => { }, + onEditClick = () => {}, + onMove = {}, color, hasInput = false, accentBackground = false, @@ -33,25 +80,60 @@ export const DAGNode: FC = ({ nodeWidth: width, accentBackground, }); - return <> - {hasInput && } - -
-
-
- {!!onXClick && } -
-
- - {label} - -
-
- + const [open, setOpen] = useState(false); + return ( + <> + {hasInput && ( + + )} + +
+
+ setOpen(false)}> +
setOpen(!open)} + onMouseEnter={() => setOpen(true)} + > + +
+ + + + {Object.entries(onMove).map(([key, action]) => ( + + + + ))} +
+
+
+
+ {label} +
+
+ +
-
- -} + + ); +}; diff --git a/webapp/src/components/DAGFlow/flowDataMods.ts b/webapp/src/components/DAGFlow/flowDataMods.ts index 162002d..19da0c5 100644 --- a/webapp/src/components/DAGFlow/flowDataMods.ts +++ b/webapp/src/components/DAGFlow/flowDataMods.ts @@ -1,51 +1,18 @@ -import { DAGCircleProps, makeFlowCircle } from "./DAGCircle" -import { DAGAutomationFlowDims } from "./types" -import { Node, XYPosition } from 'react-flow-renderer'; -import { DAGNodeProps } from "./DAGNode"; +import { DAGCircleProps, makeFlowCircle } from "./DAGCircle"; +import { DAGAutomationFlowDims } from "./types"; +import { Node, XYPosition } from "react-flow-renderer"; export const makeAddButton = ( - id: string, - position: XYPosition, - { - circleSize, - flipped, - }: DAGAutomationFlowDims, - onAdd: () => void, - disableTarget: boolean = true, -): Node => makeFlowCircle( - id, - position, - { - flipped, - size: circleSize, - onAdd, - disableSource: true, - disableTarget, - } -) - -export const makeConditionPoint = ( - id: string, - position: XYPosition, - { - conditionWidth, - conditionHeight, - flipped, - }: DAGAutomationFlowDims, - totalConditions: number, - onEditClick: () => void, -): Node => ({ - id, - type: 'dagnode', - position, - data: { - color: 'lblue', - accentBackground: true, - label: `${totalConditions} Condition${totalConditions !== 1 ? 's' : ''}`, - height: conditionHeight, - width: conditionWidth, - hasInput: true, - onEditClick, - flipped, - } -}) \ No newline at end of file + id: string, + position: XYPosition, + { circleSize, flipped }: DAGAutomationFlowDims, + onAdd: () => void, + disableTarget: boolean = true +): Node => + makeFlowCircle(id, position, { + flipped, + size: circleSize, + onAdd, + disableSource: true, + disableTarget, + }); diff --git a/webapp/src/components/DAGFlow/helpers.tsx b/webapp/src/components/DAGFlow/helpers.tsx index f655ead..4589ad3 100644 --- a/webapp/src/components/DAGFlow/helpers.tsx +++ b/webapp/src/components/DAGFlow/helpers.tsx @@ -1,9 +1,25 @@ -import { FC } from 'react'; +import { FC } from "react"; +import { DAGAutomationFlowDims } from "components/DAGFlow/types"; +import { Node, XYPosition } from "react-flow-renderer"; +export const convertToFlowNode = + (Component: FC): FC<{ data: D }> => + ({ data }) => { + return ; + }; -export const convertToFlowNode = ( - Component: FC -): FC<{ data: D }> => - ({ data }) => { - return - } +export const createToNodeMakerFunction = + ( + convert: (pre: NodePreData, dims: DAGAutomationFlowDims) => NodeData + ) => + ( + id: string, + position: XYPosition, + data: NodePreData, + dims: DAGAutomationFlowDims + ): Node => ({ + id, + type: "dagnode", + position, + data: convert(data, dims), + }); diff --git a/webapp/src/components/DAGFlow/index.tsx b/webapp/src/components/DAGFlow/index.tsx index 0836987..957afcb 100644 --- a/webapp/src/components/DAGFlow/index.tsx +++ b/webapp/src/components/DAGFlow/index.tsx @@ -4,7 +4,7 @@ import ReactFlow, { Controls } from "react-flow-renderer"; import { AutomationSequenceNode } from "types/automations"; import { AutomationTrigger } from "types/automations/triggers"; import { triggerToFlow } from "./triggerConvertor"; -import { DAGNode } from "./DAGNode"; +import { DAGNode, makeConditionPoint } from "./DAGNode"; import { DAGAutomationFlowDims, FlowData, @@ -18,7 +18,6 @@ import { NodeEditor } from "components/NodeEditor"; import { MultiNodeEditor } from "components/MultiNodeEditors"; import { Modal } from "components/Modal"; import { AutomationCondition } from "types/automations/conditions"; -import { makeConditionPoint } from "./flowDataMods"; import { convertToFlowNode } from "./helpers"; import { DAGErrorNode } from "./DAGErrorNode"; import { useHA } from "haService"; @@ -108,9 +107,15 @@ export const DAGAutomationFlow: FC = ({ x: dims.padding.x + dims.nodeWidth * dims.distanceFactor, y: dims.padding.y + dims.nodeHeight * 0.25, }, - dims, - condition.length, - makeOnEditAutomationConditions(condition, onConditionUpdate, setModalState) + { + totalConditions: condition.length, + onEditClick: makeOnEditAutomationConditions( + condition, + onConditionUpdate, + setModalState + ), + }, + dims ); const flowData: FlowData = { nodes: [condPoint], diff --git a/webapp/src/components/DAGFlow/sequenceConvertor.ts b/webapp/src/components/DAGFlow/sequenceConvertor.ts index 1c17db0..f152c32 100644 --- a/webapp/src/components/DAGFlow/sequenceConvertor.ts +++ b/webapp/src/components/DAGFlow/sequenceConvertor.ts @@ -6,7 +6,12 @@ import { ChooseAction } from "types/automations/actions"; import { makeFlowCircle } from "./DAGCircle"; import { createOnRemoveForChooseNode, SequenceUpdater } from "./updater"; import { getNodeType } from "utils/automations"; -import { makeAddButton, makeConditionPoint } from "./flowDataMods"; +import { makeAddButton } from "./flowDataMods"; +import { + makeActionPoint, + makeConditionPoint, + DAGNodeOnMoveEvents, +} from "./DAGNode"; export const sequenceToFlow = ( flowData: FlowData, @@ -75,6 +80,7 @@ export const sequenceToFlow = ( addSingleNode(flowData, node, position, nodeId, dims, { onEditClick: updater.makeOnModalOpenForNode(i, node), onXClick: () => updater.removeNode(i), + onMove: updater.makeOnMoveEvents(i, dims.flipped), namer, }); } @@ -82,6 +88,7 @@ export const sequenceToFlow = ( addSingleNode(flowData, node, position, nodeId, dims, { onEditClick: updater.makeOnModalOpenForNode(i, node), onXClick: () => updater.removeNode(i), + onMove: updater.makeOnMoveEvents(i, dims.flipped), namer, }); } @@ -151,27 +158,27 @@ const addSingleNode = ( node: AutomationSequenceNode, position: XYPosition, nodeId: string, - { flipped, nodeHeight, nodeWidth }: DAGAutomationFlowDims, + dims: DAGAutomationFlowDims, opts: { onEditClick: () => void; onXClick: () => void; namer: Namer; + onMove: DAGNodeOnMoveEvents; } ) => - flowData.nodes.push({ - id: nodeId, - type: "dagnode", - data: { - label: getDescriptionFromAutomationNode(node, opts.namer, true), - height: nodeHeight, - width: nodeWidth, - color: getNodeType(node) === "action" ? "green" : "blue", - hasInput: true, - flipped, - ...opts, - }, - position, - }); + flowData.nodes.push( + makeActionPoint( + nodeId, + position, + { + label: getDescriptionFromAutomationNode(node, opts.namer, true), + color: getNodeType(node) === "action" ? "green" : "blue", + hasInput: true, + ...opts, + }, + dims + ) + ); const addErrorNode = ( flowData: FlowData, @@ -212,6 +219,7 @@ const addChooseNode = ( onEditClick: updater.makeOnModalOpenForNode(i, node), onXClick: () => updater.removeNode(i), namer, + onMove: updater.makeOnMoveEvents(i, dims.flipped), }); let lastPos: XYPosition = position; // conditions @@ -252,9 +260,11 @@ const addChooseNode = ( x: circle.position.x + dims.circleSize * dims.distanceFactor, y: circle.position.y + dims.circleSize / 8, }, - dims, - conditions.length, - updater.makeOnEditConditionsForChooseNode(i, j) + { + totalConditions: conditions.length, + onEditClick: updater.makeOnEditConditionsForChooseNode(i, j), + }, + dims ); flowData.nodes.push(condNode); addEdge(flowData, circle.id, condNode.id, true); diff --git a/webapp/src/components/DAGFlow/styles.ts b/webapp/src/components/DAGFlow/styles.ts index 4b4c1b4..19dd01a 100644 --- a/webapp/src/components/DAGFlow/styles.ts +++ b/webapp/src/components/DAGFlow/styles.ts @@ -3,38 +3,42 @@ import { createAppUseStyles } from "styles/theme"; import { NodeColor } from "types/graphs"; export const useNodeStyles = createAppUseStyles<{ - color: NodeColor, - nodeHeight: number, - nodeWidth: number, - accentBackground: boolean, -}>(theme => { + color: NodeColor; + nodeHeight: number; + nodeWidth: number; + accentBackground: boolean; +}>((theme) => { const edgeStyle: Styles = { - flex: '1', - display: 'flex', - flexDirection: 'column', - flexWrap: 'nowrap', - borderRadius: '1em', - justifyContent: 'space-around', - alignItems: 'center', - '&:hover': { + flex: "1", + display: "flex", + flexDirection: "column", + flexWrap: "nowrap", + borderRadius: "1em", + justifyContent: "space-around", + alignItems: "center", + "&:hover": { backgroundColor: theme.primaryAccent, - cursor: 'pointer', - } - } + cursor: "pointer", + }, + }; const buttonStyle: Styles = { - fontSize: '8px', + fontSize: "8px", backgroundColor: "rgba(0,0,0,0)", color: theme.primaryAccent, - borderStyle: 'none', - height: '15px', - width: '15px', - cursor: 'pointer', - } + borderStyle: "none", + height: "15px", + width: "15px", + cursor: "pointer", + }; return { + speeddial: { + height: "2em", + width: "2em", + }, root: ({ nodeHeight, nodeWidth }) => ({ - display: 'flex', - flexDirection: 'column', - justifyContent: 'flex-end', + display: "flex", + flexDirection: "column", + justifyContent: "flex-end", height: nodeHeight, maxHeight: nodeHeight, maxWidth: nodeWidth, @@ -42,56 +46,64 @@ export const useNodeStyles = createAppUseStyles<{ fontSize: 10, }), inner: ({ color, accentBackground }) => ({ - position: 'relative', - display: 'flex', - flexDirection: 'row', - backgroundColor: !accentBackground ? theme.secondary : - color === 'lblue' ? theme.condition.primaryColorOpaque : 'none', - borderRadius: '1em', + position: "relative", + display: "flex", + flexDirection: "row", + backgroundColor: !accentBackground + ? theme.secondary + : color === "lblue" + ? theme.condition.primaryColorOpaque + : "none", + borderRadius: "1em", flex: 1, height: `100%`, maxHeight: `100%`, - maxWidth: '100%', - marginTop: '2.5px', - marginBottom: '2.5px', - borderTopWidth: '1px', - borderTopStyle: 'solid', - borderTopLeftRadius: '10px', - borderTopRightRadius: '10px', - borderTopColor: color === 'red' ? theme.redAccent : - color === 'blue' ? theme.condition.primaryColor : - color === 'green' ? theme.green : - color === 'lblue' ? theme.condition.primaryAccent : - 'none' + maxWidth: "100%", + marginTop: "2.5px", + marginBottom: "2.5px", + borderTopWidth: "1px", + borderTopStyle: "solid", + borderTopLeftRadius: "10px", + borderTopRightRadius: "10px", + borderTopColor: + color === "red" + ? theme.redAccent + : color === "blue" + ? theme.condition.primaryColor + : color === "green" + ? theme.green + : color === "lblue" + ? theme.condition.primaryAccent + : "none", }), textWrap: { - display: 'flex', + display: "flex", flex: 4, - textAlign: 'center', - justifyContent: 'center', - alignItems: 'center', - height: '100%', - maxHeight: '100%', + textAlign: "center", + justifyContent: "center", + alignItems: "center", + height: "100%", + maxHeight: "100%", maxWidth: "50%", }, text: { - textOverflow: 'ellipsis', + textOverflow: "ellipsis", overflow: "hidden", whiteSpace: "break-spaces", lineBreak: "anywhere", - cursor: 'default', + cursor: "default", }, buttonDelete: { ...buttonStyle, "&:hover": { color: theme.primary, - } + }, }, buttonEdit: { ...buttonStyle, "&:hover": { - transform: "rotate(45deg)" - } + transform: "rotate(45deg)", + }, }, leftEdge: { ...edgeStyle, @@ -105,35 +117,35 @@ export const useNodeStyles = createAppUseStyles<{ borderTopLeftRadius: 0, borderBottomLeftRadius: 0, }, - } -}) + }; +}); export const useCircleStyles = createAppUseStyles<{ - size: number, - hasOnFunction: boolean, - hasRemoveFunction: boolean, - backgroundColor?: string, -}>(theme => ({ + size: number; + hasOnFunction: boolean; + hasRemoveFunction: boolean; + backgroundColor?: string; +}>((theme) => ({ root: ({ size, hasOnFunction, hasRemoveFunction, backgroundColor }) => { const baseColor = backgroundColor ? backgroundColor : theme.green; - const hoverColor = hasOnFunction ? theme.greenOpaque : baseColor + const hoverColor = hasOnFunction ? theme.greenOpaque : baseColor; return { backgroundColor: baseColor, borderRadius: hasRemoveFunction ? 10 : 500, height: size, width: size, color: theme.primary, - cursor: hasOnFunction ? "pointer" : 'default', + cursor: hasOnFunction ? "pointer" : "default", "&:hover": { backgroundColor: hoverColor, }, fontSize: `${size * 0.4}px`, - display: 'flex', - justifyContent: 'center', - alignItems: 'center', - } + display: "flex", + justifyContent: "center", + alignItems: "center", + }; }, trashIcon: { padding: 1, - } -})) + }, +})); diff --git a/webapp/src/components/DAGFlow/updater.ts b/webapp/src/components/DAGFlow/updater.ts index fca341e..30a7e5b 100644 --- a/webapp/src/components/DAGFlow/updater.ts +++ b/webapp/src/components/DAGFlow/updater.ts @@ -3,6 +3,8 @@ import { ChooseAction } from "types/automations/actions"; import { AutomationCondition } from "types/automations/conditions"; import { UpdateModalState } from "./types"; import { getNodeType } from "../../utils/automations"; +import { DAGNodeOnMoveEvents } from "./DAGNode"; +import { cleanUpUndefined } from "components/NodeEditor/OptionManager/OptionManager"; export type SequenceUpdater = ReturnType; @@ -121,6 +123,39 @@ export const makeSequenceUpdater = ( update: (n) => this.updateNode(i, n), }); }, + makeOnMoveEvents(i: number, flipped: boolean): DAGNodeOnMoveEvents { + const moveBack = + i > 0 + ? () => + onChange([ + ...sequence.slice(0, i - 1), + sequence[i], + sequence[i - 1], + ...sequence.slice(i + 1), + ]) + : undefined; + const moveForward = + i < sequence.length - 1 + ? () => + onChange([ + ...sequence.slice(0, i), + sequence[i + 1], + sequence[i], + ...sequence.slice(i + 2), + ]) + : undefined; + if (flipped) { + return cleanUpUndefined({ + up: moveBack, + down: moveForward, + }); + } else { + return cleanUpUndefined({ + right: moveForward, + left: moveBack, + }); + } + }, }); export const createOnRemoveForChooseNode =