From f31ec2e60a26e2a3f36543e7438d03d8589ad30e Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Thu, 25 Nov 2021 14:54:10 -0500 Subject: [PATCH] remove and add new nodes --- webapp/src/components/DAGSvgs/DAGEdge.tsx | 60 ++++---- .../SequenceNodes/index.stories.tsx | 4 + .../src/components/SequenceNodes/location.tsx | 129 +++++++++++------- webapp/src/components/SequenceNodes/types.ts | 17 ++- .../src/components/SequenceNodes/updater.ts | 73 ++++++++++ webapp/src/types/automations/actions.ts | 4 +- 6 files changed, 196 insertions(+), 91 deletions(-) create mode 100644 webapp/src/components/SequenceNodes/updater.ts diff --git a/webapp/src/components/DAGSvgs/DAGEdge.tsx b/webapp/src/components/DAGSvgs/DAGEdge.tsx index eec0d81..0571a37 100644 --- a/webapp/src/components/DAGSvgs/DAGEdge.tsx +++ b/webapp/src/components/DAGSvgs/DAGEdge.tsx @@ -3,57 +3,57 @@ import { EdgeDirection, Point } from "types/graphs" export interface EdgeProp { - p1: Point, - p2: Point, - direction: EdgeDirection, + p1: Point, + p2: Point, + direction: EdgeDirection, color: string, debug?: boolean, } export const DAGEdge = ({ - p1: [x1,y1], - p2: [x2,y2], - direction, - color, - debug=false, + p1: [x1, y1], + p2: [x2, y2], + direction, + color, + debug = false, }: EdgeProp) => { - const [mx,my] = [(x1+x2)/2, (y1+y2)/2] - let [cx1,cy1] = [(x1+mx)/2, (y1+my)/2] + const [mx, my] = [(x1 + x2) / 2, (y1 + y2) / 2] + let [cx1, cy1] = [(x1 + mx) / 2, (y1 + my) / 2] let [cx2, cy2] = [(x2 + mx) / 2, (y2 + my) / 2] const l1 = Math.sqrt((cy1 - y1) ** 2 + (cx1 - x1) ** 2) const l2 = Math.sqrt((cy2 - y2) ** 2 + (cx2 - x2) ** 2) - const theta = Math.atan2(y2 -y1, x2-x1) + const theta = Math.atan2(y2 - y1, x2 - x1) if ((theta < Math.PI / 2) && (theta > -Math.PI / 2)) { cy1 = y1 - 0.5 * l1 * Math.sin(theta) - cx1 = x1 + 2 * l1 * Math.cos(theta) - cy2 = y2 + 0.5 * l2 * Math.sin(theta) - cx2 = x2 - 2 * l2 * Math.cos(theta) + cx1 = x1 + 2 * l1 * Math.cos(theta) + cy2 = y2 + cx2 = x2 - 3 * l2 * Math.cos(theta) } else if ((theta > Math.PI / 2) && (theta < Math.PI)) { cy2 += l2 * Math.sin(theta) - cx2 += 10 * l2 * Math.cos(theta) - cx1 -= 10 * l1 * Math.cos(theta) + cx2 += 10 * l2 * Math.cos(theta) + cx1 -= 10 * l1 * Math.cos(theta) } return <> {debug && <> - - - - + + + + {theta} } 2') ? - "url(#arrow)" : '' - } - markerEnd={ - (direction === '1->2' || direction === '1<->2') ? - "url(#arrow)" : '' - } + d={`M${x1} ${y1} C${cx1} ${cy1}, ${cx2} ${cy2}, ${x2} ${y2}`} + fill="none" + markerStart={ + (direction === '1<-2' || direction === '1<->2') ? + "url(#arrow)" : '' + } + markerEnd={ + (direction === '1->2' || direction === '1<->2') ? + "url(#arrow)" : '' + } /> } diff --git a/webapp/src/components/SequenceNodes/index.stories.tsx b/webapp/src/components/SequenceNodes/index.stories.tsx index f5bf1ee..ece1b2a 100644 --- a/webapp/src/components/SequenceNodes/index.stories.tsx +++ b/webapp/src/components/SequenceNodes/index.stories.tsx @@ -107,6 +107,10 @@ Multinode.args = { action: "choose", action_data: { choose: [ + { + conditions: [], + sequence: [] + }, { conditions: [ { diff --git a/webapp/src/components/SequenceNodes/location.tsx b/webapp/src/components/SequenceNodes/location.tsx index 0bae367..a71450a 100644 --- a/webapp/src/components/SequenceNodes/location.tsx +++ b/webapp/src/components/SequenceNodes/location.tsx @@ -1,8 +1,10 @@ import { DAGEdge } from "components/DAGSvgs/DAGEdge"; -import { AutomationSequenceNode } from "types/automations"; import { Point } from "types/graphs"; import { DAGNode } from '../DAGSvgs/DAGNode'; -import { SequenceNodeSettings } from './types'; +import { BaseSequenceNodeSettings, SequenceNodeSettings } from './types'; +import { AddButton } from '../DAGSvgs/AddButton'; +import { makeUpdater, Updater } from "./updater"; +import { ChooseAction } from "types/automations/actions"; export function* computeNodesEdges( @@ -15,6 +17,7 @@ export function* computeNodesEdges( ): 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] : [ @@ -34,58 +37,24 @@ export function* computeNodesEdges( loc: [lastNodeLoc[0] + st.nodeWidth, lastNodeLoc[1] + st.nodeHeight / 2], color: st.edgeColor, }} - onXClick={() => onChange([ - ...sequence.slice(0, i), - ...sequence.slice(i + 1), - ])} + onXClick={() => rootUpdater.removeNode(i)} /> if ((node.$smType === 'action') && (node.action === 'choose')) { - for (let j = 0; j < node.action_data.choose.length; j++) { - const subStartLoc: Point = [ - nodeLoc[0] + st.nodeWidth * st.distanceFactor, - nodeLoc[1] + st.nodeHeight * (j + 1) - ]; - const it = computeNodesEdges({ - ...st, - onChange: seq => onChange([ - ...sequence.slice(0, i), - { - ...node, - action_data: { - ...node.action_data, - choose: [ - ...node.action_data.choose.slice(0, j), - { - ...node.action_data.choose[j], - sequence: seq, - }, - ...node.action_data.choose.slice(j + 1), - ] - } - }, - ...sequence.slice(i + 1), - ]), - sequence: node.action_data.choose[j].sequence, - startPoint: subStartLoc, - keyPrefix: `.${i}.${j}` - }); - let next = it.next(); - while (!next.done) { - yield next.value - next = it.next() - } - if (next.value) { - yield - nextNodeLoc[0] = next.value[0] + st.nodeWidth * st.distanceFactor - lastStartPoint = [next.value[0], lastStartPoint[1]] - } - } + const it = computeChooseNodesEdges( + node, + nodeLoc, + lastStartPoint, + nextNodeLoc, + j => rootUpdater.makeChildUpdaterForChooseAction(i, j), + { ...st, keyPrefix: `${keyPrefix}.${i}` } + ); + let next = it.next(); + while (!next.done) { + yield next.value + next = it.next(); + }; + nextNodeLoc = next.value.nextNodeLoc + lastStartPoint = next.value.lastStartPoint } if (i === sequence.length - 1) { yield + yield } lastNodeLoc = [...nodeLoc] } return lastStartPoint } + +export function* computeChooseNodesEdges( + node: ChooseAction, + nodeLoc: Point, + lastStartPoint: Point, + nextNodeLoc: Point, + makeChildUpder: (j: number) => Updater, + st: BaseSequenceNodeSettings, +) { + for (let j = 0; j < node.action_data.choose.length; j++) { + const subStartLoc: Point = [ + nodeLoc[0] + st.nodeWidth * st.distanceFactor, + nodeLoc[1] + st.nodeHeight * (j + 1) + ]; + const updater = makeChildUpder(j); + const it = computeNodesEdges({ + ...st, + onChange: updater.onChange, + sequence: node.action_data.choose[j].sequence, + startPoint: subStartLoc, + keyPrefix: `.${j}` + }); + let next = it.next(); + while (!next.done) { + yield next.value + next = it.next() + } + yield + if (next.value) { + nextNodeLoc[0] = next.value[0] + st.nodeWidth * st.distanceFactor + lastStartPoint = [Math.max(next.value[0], lastStartPoint[0]), lastStartPoint[1]] + } else { + yield + } + } + return { lastStartPoint, nextNodeLoc } +} diff --git a/webapp/src/components/SequenceNodes/types.ts b/webapp/src/components/SequenceNodes/types.ts index 3bfd6b5..0c615d2 100644 --- a/webapp/src/components/SequenceNodes/types.ts +++ b/webapp/src/components/SequenceNodes/types.ts @@ -1,13 +1,16 @@ import { AutomationSequenceNode } from "types/automations"; import { Point } from "types/graphs"; -export type SequenceNodeSettings = { - startPoint: Point, +export interface SequenceNodeSettings extends BaseSequenceNodeSettings{ sequence: AutomationSequenceNode[], - nodeHeight: number, - nodeWidth: number, - distanceFactor: number, - edgeColor: string, - keyPrefix?: string, onChange?: (s: AutomationSequenceNode[]) => void; } + +export interface BaseSequenceNodeSettings { + startPoint: Point; + nodeHeight: number; + nodeWidth: number; + distanceFactor: number; + edgeColor: string; + keyPrefix?: string; +} diff --git a/webapp/src/components/SequenceNodes/updater.ts b/webapp/src/components/SequenceNodes/updater.ts new file mode 100644 index 0000000..35b88da --- /dev/null +++ b/webapp/src/components/SequenceNodes/updater.ts @@ -0,0 +1,73 @@ +import { AutomationSequenceNode } from "types/automations"; + +export type Updater = ReturnType; + +export const makeUpdater = ( + sequence: AutomationSequenceNode[], + onChange: (s: AutomationSequenceNode[]) => void +) => ({ + sequence, + onChange, + removeNode(i: number) { + return onChange([ + ...sequence.slice(0, i), + ...sequence.slice(i+1), + ]) + }, + addNode() { + return onChange([ + ...sequence, + { + $smType: 'action', + action: 'choose', + action_data: { + choose: [], + default: [], + } + } + ]) + }, + updateNode(i: number, node: AutomationSequenceNode) { + return onChange([ + ...sequence.slice(0, i), + node, + ...sequence.slice(i+1), + ]) + }, + makeChildUpdaterForChooseAction(i: number, j:number | null) { + const node = sequence[i]; + if ((node.$smType === 'action') && (node.action === 'choose')) { + if (j !== null) { + return makeUpdater( + node.action_data.choose[j].sequence, + seq => this.updateNode(i, { + ...node, + action_data: { + ...node.action_data, + choose: [ + ...node.action_data.choose.slice(0, j), + { + ...node.action_data.choose[j], + sequence: seq, + }, + ...node.action_data.choose.slice(j+1), + ] + } + }) + ) + } else { + return makeUpdater( + node.action_data.default, + seq => this.updateNode(i, { + ...node, + action_data: { + ...node.action_data, + default: seq + } + }) + ) + } + } + throw new Error(`Cannot create sub-updater for ${node}`) + } +}); diff --git a/webapp/src/types/automations/actions.ts b/webapp/src/types/automations/actions.ts index ba1180e..c9f5e3d 100644 --- a/webapp/src/types/automations/actions.ts +++ b/webapp/src/types/automations/actions.ts @@ -42,7 +42,7 @@ export interface ChooseAction extends AutomationActionNodeBase<"choose", { conditions: AutomationCondition[]; sequence: AutomationSequenceNode[]; }>; - default: AutomationAction[]; + default: AutomationSequenceNode[]; }>{} export type AutomationAction = | ServiceAction @@ -60,4 +60,4 @@ export const ActionTypes = [ 'Device', 'Choose' ] as const; -export type ActionType = typeof ActionTypes[number]; \ No newline at end of file +export type ActionType = typeof ActionTypes[number];