diff --git a/webapp/src/components/DAGSvgs/Board.tsx b/webapp/src/components/DAGSvgs/Board.tsx index 45ed5d0..710d8fc 100644 --- a/webapp/src/components/DAGSvgs/Board.tsx +++ b/webapp/src/components/DAGSvgs/Board.tsx @@ -7,10 +7,11 @@ import { useBoardStyles } from "./styles"; export const SVGBoard: FC<{ graphHeight?: number, - children?: JSX.Element; + minGraphWidth?: number, }> = ({ children, graphHeight = 300, + minGraphWidth = 300, }) => { // state @@ -19,7 +20,10 @@ export const SVGBoard: FC<{ const [[offX, offY], setOffset] = useState([0, 0]) // alias - const graphWidth = Math.round(graphHeight * ratioWbh); + const graphWidth = Math.max( + Math.round(graphHeight * ratioWbh), + minGraphWidth + ); // render return
@@ -41,9 +45,5 @@ export const SVGBoard: FC<{ {children}
-
setOffset([offX, offY - 10])}>
-
setOffset([offX - 10, offY])}>
-
setOffset([offX + 10, offY])}>
-
setOffset([offX, offY + 10])}>
} diff --git a/webapp/src/components/DAGSvgs/DAGBoard.stories.tsx b/webapp/src/components/DAGSvgs/DAGBoard.stories.tsx new file mode 100644 index 0000000..58bfbf0 --- /dev/null +++ b/webapp/src/components/DAGSvgs/DAGBoard.stories.tsx @@ -0,0 +1,103 @@ +import React from "react"; +import { ComponentMeta, ComponentStory } from '@storybook/react'; +import { DAGBoard, DAGElement, DAGNodeElm } from "./DAGBoard"; +import { NODE_HEIGHT, NODE_WIDTH } from './constants'; + + +export default { + title: 'DAGSvgs/DAGBoard', + component: DAGBoard, + parameters: { actions: { argTypesRegex: '^on.*' } }, + args: { + settings: { + nodeHeight: NODE_HEIGHT, + nodeWidth: NODE_WIDTH, + addHeight: 10, + addWidth: 10, + distanceFactor: 1.5, + edgeChildColor: 'blue', + edgeNextColor: 'white', + }, + } +} as ComponentMeta; + + +const nodeFromPoint = ([x, y, add = true]: any): DAGNodeElm => ({ + type: 'node', + key: `${x},${y}`, + loc: [x, y], + onRemove() { }, + onAdd: add ? () => { } : undefined, + node: { + $smType: 'action', + alias: `${x},${y}`, + action: 'template', + action_data: { + value_template: "" + } + } +}) + +export const Basic: ComponentStory = args => +Basic.args = { + ...Basic.args, + elements: [ + [0, 0, false], + [1, 0, false], + [2, 1, false], + [4, 1], + [3, 2], + [2, 3], + [5, 0, false], + [6, 0], + [0, 6], + ].map(nodeFromPoint).concat([ + { + type: 'edge', + key: 'e1', + p1: [0, 0], + p2: [1, 0], + direction: '1->2' + }, + { + type: 'edge', + key: 'e2', + p1: [2, 1], + p2: [4, 1], + direction: '1->2', + }, + { + type: 'edge', + key: 'e3', + p1: [1, 0], + p2: [2, 1], + direction: '1->2', + toChild: true, + }, + { + type: 'edge', + key: 'e4', + p1: [2, 1], + p2: [3, 2], + direction: '1->2', + toChild: true, + }, + { + type: 'edge', + key: 'e5', + p1: [1, 0], + p2: [2, 3], + direction: '1->2', + toChild: true, + }, + { + type: 'edge', + key: 'e6', + p1: [1, 0], + p2: [5, 0], + direction: '1->2' + }, + ]) +} + + diff --git a/webapp/src/components/DAGSvgs/DAGBoard.tsx b/webapp/src/components/DAGSvgs/DAGBoard.tsx new file mode 100644 index 0000000..afab49a --- /dev/null +++ b/webapp/src/components/DAGSvgs/DAGBoard.tsx @@ -0,0 +1,130 @@ +import { FC } from "react"; +import { SVGBoard } from "./Board"; +import { AutomationNode } from '../../types/automations/index'; +import { Point, EdgeDirection, BBox } from 'types/graphs'; +import { DAGNode } from "./DAGNode"; +import { DAGEdge } from "./DAGEdge"; +import { AddButton } from "./AddButton"; +import { IteratorWrap } from "utils/iter"; + + +export interface DAGNodeElm { + type: 'node'; + key: string; + loc: Point; + node: AutomationNode; + onRemove: () => void; + onAdd?: () => void; +} +export interface DAGEdgeElm { + type: 'edge'; + key: string; + p1: Point; + p2: Point; + direction: EdgeDirection; + toChild?: boolean; +} +export type DAGElement = DAGEdgeElm | DAGNodeElm; +export interface DAGBoardSettings { + nodeHeight: number; + nodeWidth: number; + addHeight: number; + addWidth: number; + distanceFactor: number; + edgeChildColor: string; + edgeNextColor: string; +} + +export interface Props { + elements: DAGElement[]; + settings: DAGBoardSettings; +} + + + + +export function* mapDataToElements({ + elements, + settings: st, +}: Props) { + let bbox: BBox = [[0, 0], [0, 0]]; + const updateBBox = (p: Point) => { + bbox = [ + [Math.min(bbox[0][0], p[0]), Math.min(bbox[0][1], p[1])], + [Math.max(bbox[1][0], p[0] + st.nodeWidth), Math.max(bbox[1][1], p[1] + st.nodeHeight)], + ] + return p; + } + const offsetPoint = ( + [x, y]: Point, + [ox, oy]: Point = [0, 0] + ): Point => updateBBox([x + ox, y + oy]); + const mapWithNode = ( + [x, y]: Point, + [ox, oy]: Point = [0, 0], + ): Point => updateBBox([ + x * st.nodeWidth * st.distanceFactor + ox, + y * st.nodeHeight * st.distanceFactor + oy, + ]) + for (let elm of elements) { + switch (elm.type) { + case 'node': + const nodeLoc = mapWithNode(elm.loc); + const addLoc = mapWithNode(elm.loc, [ + st.nodeWidth * st.distanceFactor, + st.addHeight + st.addHeight / 2 + ]) + yield + if (elm.onAdd) { + yield + yield + } + break + case 'edge': + yield + break + default: + break + } + } + return bbox; +} + + +export const DAGBoard: FC<{ + elements: DAGElement[]; + settings: DAGBoardSettings; +}> = props => { + const it = new IteratorWrap(mapDataToElements(props)); + const el = it.toArray(); + const [[x0, y0], [x1, y1]] = it.returnValue as BBox; + return + {el} + +} diff --git a/webapp/src/components/DAGSvgs/DAGNode.tsx b/webapp/src/components/DAGSvgs/DAGNode.tsx index 51ce14e..9cd704c 100644 --- a/webapp/src/components/DAGSvgs/DAGNode.tsx +++ b/webapp/src/components/DAGSvgs/DAGNode.tsx @@ -1,7 +1,7 @@ import Color from "chroma-js"; import { PencilIcon } from "components/Icons" import { useNodeStyles } from "./styles" -import { Edge, Node, Point } from "types/graphs"; +import { EdgeDirection, Node, Point } from "types/graphs"; import { DAGEdge } from "./DAGEdge"; @@ -15,7 +15,7 @@ export interface NodeProp extends Node { edge?: { color: string; loc: Point; - direction: Edge['direction']; + direction: EdgeDirection; } } export const DAGNode = ({ @@ -27,7 +27,7 @@ export const DAGNode = ({ onOpenClick = () => { }, color, edge, - debug=false, + debug = false, }: NodeProp) => { const { classes, theme } = useNodeStyles({ color, @@ -56,19 +56,19 @@ export const DAGNode = ({ { edge && (edge.direction === '1->2' ? - : - : + ) } diff --git a/webapp/src/components/DAGSvgs/styles.ts b/webapp/src/components/DAGSvgs/styles.ts index 979120d..18a4b14 100644 --- a/webapp/src/components/DAGSvgs/styles.ts +++ b/webapp/src/components/DAGSvgs/styles.ts @@ -18,10 +18,12 @@ export const useBoardStyles = createAppUseStyles(theme => ({ borderStyle: 'solid', borderColor: theme.secondaryAccent, width: '100%', + height: '100%', + overflow: 'scroll' }, svg: { - height: '99%', - width: '99%', + minHeight: '800px', + minWidth: '800px', padding: 0, margin: 0, },