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"; import { DAGCircle } from "./DAGCircle"; export interface DAGCircleElm { type: 'circle'; loc: Point; onClick?: () => void; } export interface DAGNodeElm { type: 'node'; loc: Point; node: AutomationNode; onRemove: () => void; onAdd?: () => void; } export interface DAGEdgeElm { type: 'edge'; p1: Point; p2: Point; direction: EdgeDirection; fromCircle?: boolean; onAdd?: () => void; } export type DAGElement = DAGEdgeElm | DAGNodeElm | DAGCircleElm; export interface DAGBoardSettings extends DAGBoardElmDims { edgeChildColor: string; edgeNextColor: string; } export interface DAGBoardElmDims { nodeHeight: number; nodeWidth: number; addHeight: number; addWidth: number; circleSize: number; distanceFactor: number; } export interface Props { elements: Generator; 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 +`} p1={offsetPoint(nodeLoc, [st.nodeWidth, st.nodeHeight / 2])} p2={offsetPoint(addLoc, [0, st.addHeight / 2])} direction="1->2" color={st.edgeNextColor} /> yield } break case 'edge': const p2 = mapWithNode(elm.p2, [0, st.nodeHeight / 2]); let p1: Point; if (elm.fromCircle) { p1 = mapWithNode(elm.p1, [st.circleSize * 0.95, 1.3 * st.circleSize / 2]) } else { p1 = mapWithNode(elm.p1, [st.nodeWidth, st.nodeHeight / 2]) } yield (${elm.p2})`} p1={p1} p2={p2} direction={elm.direction} color={st.edgeNextColor} /> if (elm.onAdd) { yield } break case 'circle': const loc = mapWithNode(elm.loc, [0, st.nodeHeight / 2 - st.circleSize / 2]); yield break default: break } } return bbox; } export const DAGBoard: FC = props => { const it = new IteratorWrap(mapDataToElements(props)); const el = it.toArray(); const [[x0, y0], [x1, y1]] = it.returnValue as BBox; return {el} }