diff --git a/webapp/src/components/DAGSvgs/DAGBoard.tsx b/webapp/src/components/DAGSvgs/DAGBoard.tsx index 6ac1f0c..8659f13 100644 --- a/webapp/src/components/DAGSvgs/DAGBoard.tsx +++ b/webapp/src/components/DAGSvgs/DAGBoard.tsx @@ -23,6 +23,7 @@ export interface DAGEdgeElm { p2: Point; direction: EdgeDirection; toChild?: boolean; + onAdd?: () => void; } export type DAGElement = DAGEdgeElm | DAGNodeElm; export interface DAGBoardSettings extends DAGBoardElmDims { @@ -69,7 +70,6 @@ export function* mapDataToElements({ y * st.nodeHeight * st.distanceFactor + oy, ]) for (let elm of elements) { - console.log({ elm }) switch (elm.type) { case 'node': const nodeLoc = mapWithNode(elm.loc); @@ -78,7 +78,7 @@ export function* mapDataToElements({ 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 `} p1={mapWithNode(elm.p1, [st.nodeWidth, st.nodeHeight / 2])} - p2={mapWithNode(elm.p2, [0, st.nodeHeight / 2])} + p2={p2} direction={elm.direction} color={elm.toChild ? st.edgeChildColor : st.edgeNextColor} /> + if (elm.onAdd) { + yield + } break default: break diff --git a/webapp/src/components/SequenceNodes/index.stories.tsx b/webapp/src/components/SequenceNodes/index.stories.tsx index 9891faa..8412bf8 100644 --- a/webapp/src/components/SequenceNodes/index.stories.tsx +++ b/webapp/src/components/SequenceNodes/index.stories.tsx @@ -1,8 +1,7 @@ import React from "react"; import { ComponentMeta, ComponentStory } from '@storybook/react'; import { SequenceNodes } from './index'; -import { SVGBoard } from '../DAGSvgs/Board'; -import { NODE_HEIGHT, NODE_WIDTH } from '../DAGSvgs/constants'; +import { NODE_HEIGHT, NODE_WIDTH, DISTANCE_FACTOR, ADD_HEIGHT, ADD_WIDTH } from '../DAGSvgs/constants'; import { useState } from 'react'; @@ -11,22 +10,25 @@ export default { component: SequenceNodes, parameters: { actions: { argTypesRegex: '^on.*' } }, args: { - nodeHeight: NODE_HEIGHT, - nodeWidth: NODE_WIDTH, - distanceFactor: 1.5, - startPoint: [50, 50] + zoomLevel: 0.5, + startPoint: [0.5, 0.5], + dims: { + nodeHeight: NODE_HEIGHT, + nodeWidth: NODE_WIDTH, + addHeight: ADD_HEIGHT, + addWidth: ADD_WIDTH, + distanceFactor: DISTANCE_FACTOR, + } } } as ComponentMeta const Template: ComponentStory = args => { const [state, setState] = useState(args.sequence) - return - - + return } export const Simple = Template.bind({}) @@ -77,7 +79,6 @@ Simple.args = { export const Multinode = Template.bind({}) Multinode.args = { ...Multinode.args, - startPoint: [0, 0], sequence: [ { $smType: "condition", @@ -179,3 +180,67 @@ Multinode.args = { } ] } + +export const DeepNested = Template.bind({}) +DeepNested.args = { + ...DeepNested.args, + sequence: [ + { + "$smType": "action", + "action": "choose", + "action_data": { + "choose": [{ + "sequence": [{ + "$smType": "action", + "action": "choose", + "action_data": { + "choose": [], + "default": [] + } + }], + "conditions": [], + }], + "default": [ + { + "$smType": "action", + "action": "choose", + "action_data": { + "choose": [ + { + "sequence": [{ + "$smType": "action", + "action": "choose", + "action_data": { + "choose": [], + "default": [] + } + }], + "conditions": [], + } + ], + "default": [ + + ] + } + }, + { + "$smType": "action", + "action": "choose", + "action_data": { + "choose": [], + "default": [] + } + } + ] + } + }, + { + "$smType": "action", + "action": "choose", + "action_data": { + "choose": [], + "default": [] + } + } + ] +} diff --git a/webapp/src/components/SequenceNodes/index.tsx b/webapp/src/components/SequenceNodes/index.tsx index 9b885c5..3328968 100644 --- a/webapp/src/components/SequenceNodes/index.tsx +++ b/webapp/src/components/SequenceNodes/index.tsx @@ -1,17 +1,31 @@ +import { DAGBoard } from "components/DAGSvgs/DAGBoard"; import { FC } from "react"; import { useTheme } from "styles/theme"; -import { computeNodesEdges } from './location'; -import { SequenceNodeSettings } from './types'; +import { SequenceNodeProps } from './types'; +import Color from 'chroma-js'; +import { computeNodesEdgesPos } from './positions'; - -export const SequenceNodes: FC = (props) => { +export const SequenceNodes: FC = (props) => { const theme = useTheme(); - return <> - {Array.from(computeNodesEdges({ - ...props, - keyPrefix: "", - edgeColor: theme.secondaryAccent, - }))} - + // return <> + // {Array.from(computeNodesEdges({ + // ...props, + // keyPrefix: "", + // edgeColor: theme.secondaryAccent, + // }))} + // + return } diff --git a/webapp/src/components/SequenceNodes/location.tsx b/webapp/src/components/SequenceNodes/location.tsx deleted file mode 100644 index 5728503..0000000 --- a/webapp/src/components/SequenceNodes/location.tsx +++ /dev/null @@ -1,174 +0,0 @@ -import { DAGEdge } from "components/DAGSvgs/DAGEdge"; -import { Point } from "types/graphs"; -import { DAGNode } from '../DAGSvgs/DAGNode'; -import { BaseSequenceNodeSettings, SequenceNodeSettings } from './types'; -import { AddButton } from '../DAGSvgs/AddButton'; -import { makeUpdater, Updater } from "./updater"; -import { ChooseAction } from "types/automations/actions"; -import { AutomationSequenceNode } from '../../types/automations/index'; - - -export function* computeNodesEdges( - { - sequence, - keyPrefix = "", - onChange = () => { }, - ...st - }: SequenceNodeSettings, -): Generator { - let lastStartPoint: Point | null = null; - let lastNodeLoc: Point | null = null; - const rootUpdater = makeUpdater(sequence, onChange); - for (let i = 0; i < sequence.length; i++) { - const node = sequence[i]; - const nodeLoc: Point = lastStartPoint = lastStartPoint === null ? [...st.startPoint] : [ - lastStartPoint[0] + st.nodeWidth * st.distanceFactor, - lastStartPoint[1] - ]; - let nextNodeLoc: Point = [nodeLoc[0] + st.distanceFactor * st.nodeWidth, nodeLoc[1] + st.nodeHeight / 2]; - yield rootUpdater.removeNode(i)} - /> - if ((node.$smType === 'action') && (node.action === 'choose')) { - const it = (new computeChooseNodesEdges( - node, - nodeLoc, - lastStartPoint, - nextNodeLoc, - j => rootUpdater.makeChildUpdaterForChooseAction(i, j), - { ...st, keyPrefix: `${keyPrefix}.${i}` } - )).iter(); - let next = it.next(); - while (!next.done) { - yield next.value - next = it.next(); - }; - nextNodeLoc = next.value.nextNodeLoc - lastStartPoint = [ - next.value.lastStartPoint[0], - lastStartPoint[1], - ] - } - if (i === sequence.length - 1) { - yield - yield - } - lastNodeLoc = [...nodeLoc] - } - return lastStartPoint -} - -class computeChooseNodesEdges { - constructor( - private node: ChooseAction, - private nodeLoc: Point, - private lastStartPoint: Point, - private nextNodeLoc: Point, - private makeChildUpder: (j: number | null) => Updater, - private st: BaseSequenceNodeSettings, - ) { } - - private *genNodesEdges( - seq: AutomationSequenceNode[], - subStartLoc: Point, - updater: Updater, - keyPrefix: string - ) { - const { - st, nodeLoc, - } = this; - let it = computeNodesEdges({ - ...st, - onChange: updater.onChange, - sequence: seq, - startPoint: subStartLoc, - keyPrefix: keyPrefix - }); - let next = it.next(); - while (!next.done) { - yield next.value - next = it.next() - } - yield - if (next.value) { - this.nextNodeLoc[0] = next.value[0] + st.nodeWidth * st.distanceFactor - this.lastStartPoint = [Math.max(next.value[0], this.lastStartPoint[0]), Math.max(this.lastStartPoint[1], next.value[1])] - } else { - yield - } - } - - *iter() { - const { st, node, makeChildUpder, nodeLoc } = this; - for (let j = 0; j < node.action_data.choose.length; j++) { - const it = this.genNodesEdges( - node.action_data.choose[j].sequence, - [ - nodeLoc[0] + st.nodeWidth * st.distanceFactor, - Math.max(nodeLoc[1] + st.nodeHeight * (j + 1), this.lastStartPoint[1]) - ], - makeChildUpder(j), - `${st.keyPrefix}.${j}`, - ); - let next = it.next(); - while (!next.done) { - yield next.value - next = it.next(); - } - } - const it = this.genNodesEdges( - node.action_data.default, - [ - nodeLoc[0] + st.nodeWidth * st.distanceFactor, - nodeLoc[1] + st.nodeHeight * (node.action_data.choose.length + 1) - ], - makeChildUpder(null), - `${st.keyPrefix}.default`, - ); - let next = it.next(); - while (!next.done) { - yield next.value - next = it.next(); - } - return { - lastStartPoint: this.lastStartPoint, - nextNodeLoc: this.nextNodeLoc - } - } -} - diff --git a/webapp/src/components/SequenceNodes/positions.ts b/webapp/src/components/SequenceNodes/positions.ts new file mode 100644 index 0000000..9132be4 --- /dev/null +++ b/webapp/src/components/SequenceNodes/positions.ts @@ -0,0 +1,126 @@ +import { DAGElement } from "components/DAGSvgs/DAGBoard"; +import { AutomationSequenceNode } from "types/automations"; +import { Point } from "types/graphs"; +import { makeUpdater, Updater } from './updater'; + +const offsetPoint = ( + [x, y]: Point, + [ox, oy]: Point = [0, 0] +): Point => [x + ox, y + oy]; +; + +function* inner( + nodeLoc: Point, + innerStartLoc: Point, + updater: Updater, + keyPrefix: string, + sequence: AutomationSequenceNode[], +): Generator { + yield { + type: 'edge', + p1: nodeLoc, + p2: innerStartLoc, + direction: '1->2', + key: `${keyPrefix}-${nodeLoc}:${innerStartLoc}`, + onAdd: sequence.length === 0 ? updater.addNode : undefined, + } + for (let childNode of computeNodesEdgesPos( + innerStartLoc, + sequence, + updater.onChange, + keyPrefix, + )) { + yield childNode + } +} + +const makeOffsetMaintainer = (startPoint: Point) => { + let offset: Point = [0, 0]; + let i = 0; + return { + setI(newI: number) { + i = newI; + }, + resetY() { + offset[1] = 0; + }, + update(p: Point, innerStartLoc: Point) { + console.log('updating', JSON.stringify({i, offset, p, innerStartLoc})) + offset[0] = Math.max(offset[0], p[0] - i - 0.5 - startPoint[0]) + offset[1] = Math.max(offset[1], p[1] - innerStartLoc[1] + 1) + console.log('|->', JSON.stringify({offset})) + }, + get x() { + return offset[0] + }, + get y() { + return offset[1] + } + } +} +export function* computeNodesEdgesPos( + startPoint: Point, + sequence: AutomationSequenceNode[], + onChange: (s: AutomationSequenceNode[]) => void, + keyPrefix: string = '$', +): Generator { + const rootUpdater = makeUpdater(sequence, onChange); + const offset = makeOffsetMaintainer(startPoint) + + for (let i = 0; i < sequence.length; i++) { + offset.setI(i) + const node = sequence[i]; + const nodeLoc = offsetPoint(startPoint, [i + offset.x, 0]); + yield { + type: 'node', + key: `${keyPrefix}${i}`, + loc: nodeLoc, + node, + onRemove: () => rootUpdater.removeNode(i), + onAdd: i === sequence.length - 1 ? rootUpdater.addNode : undefined, + } + if (i > 0) { + yield { + type: 'edge', + key: `${keyPrefix}(${i-1}->${i})`, + p1: offsetPoint(startPoint, [i - 1, 0]), + p2: offsetPoint(nodeLoc, [0, 0]), + direction: '1->2', + } + } + if ( + (node.$smType === 'action') && (node.action === 'choose') + ) { + offset.resetY() + + for (let j = 0; j < node.action_data.choose.length; j++) { + const innerStartLoc = offsetPoint(nodeLoc, [1, j + 1 + offset.y]); + for (let elm of inner( + nodeLoc, + innerStartLoc , + rootUpdater.makeChildUpdaterForChooseAction(i, j), + `${keyPrefix}.${i}.${j}`, + node.action_data.choose[j].sequence + )) { + yield elm + if (elm.type === 'node') { + offset.update(elm.loc, innerStartLoc) + } + } + } + const innerStartLoc = offsetPoint(nodeLoc, [1, node.action_data.choose.length + 1 + offset.y]); + for (let elm of inner( + nodeLoc, + innerStartLoc, + rootUpdater.makeChildUpdaterForChooseAction(i, null), + `${keyPrefix}${i}-d`, + node.action_data.default, + )) { + yield elm + if (elm.type === 'node') { + offset.update(elm.loc, innerStartLoc) + } + } + } + } +} diff --git a/webapp/src/components/SequenceNodes/types.ts b/webapp/src/components/SequenceNodes/types.ts index 0c615d2..252014d 100644 --- a/webapp/src/components/SequenceNodes/types.ts +++ b/webapp/src/components/SequenceNodes/types.ts @@ -1,16 +1,24 @@ +import { DAGBoardElmDims } from "components/DAGSvgs/DAGBoard"; import { AutomationSequenceNode } from "types/automations"; import { Point } from "types/graphs"; -export interface SequenceNodeSettings extends BaseSequenceNodeSettings{ +export interface SequenceNodeProps { + startPoint: Point; + dims: DAGBoardElmDims; sequence: AutomationSequenceNode[], - onChange?: (s: AutomationSequenceNode[]) => void; + onChange: (s: AutomationSequenceNode[]) => void; } -export interface BaseSequenceNodeSettings { - startPoint: Point; - nodeHeight: number; - nodeWidth: number; - distanceFactor: number; - edgeColor: string; - keyPrefix?: string; -} +// export interface SequenceNodeSettings extends BaseSequenceNodeSettings{ +// sequence: AutomationSequenceNode[], +// onChange?: (s: AutomationSequenceNode[]) => void; +// } + +// export interface BaseSequenceNodeSettings { +// startPoint: Point; +// nodeHeight: number; +// nodeWidth: number; +// distanceFactor: number; +// edgeColor: string; +// keyPrefix?: string; +// }