deleting sequence nodes

This commit is contained in:
ari committed 2021-11-25 12:52:11 -05:00
1 parent e83e074fe8
commit 13c9ede31c
4 files changed
+93 -68

No files matched your search

@@ -3,6 +3,7 @@ import { ComponentMeta, ComponentStory } from '@storybook/react';
import { SequenceNodes } from './index';
import { SVGBoard } from '../DAGSvgs/Board';
import { NODE_HEIGHT, NODE_WIDTH } from '../DAGSvgs/constants';
import { useState } from 'react';
export default {
@@ -13,15 +14,20 @@ export default {
nodeHeight: NODE_HEIGHT,
nodeWidth: NODE_WIDTH,
distanceFactor: 1.5,
startLoc: [50,50]
startPoint: [50, 50]
}
} as ComponentMeta<typeof SequenceNodes>
const Template: ComponentStory<typeof SequenceNodes> = args => <SVGBoard>
<SequenceNodes
{...args}
/>
</SVGBoard>
const Template: ComponentStory<typeof SequenceNodes> = args => {
const [state, setState] = useState(args.sequence)
return <SVGBoard>
<SequenceNodes
{...args}
sequence={state}
onChange={setState}
/>
</SVGBoard>
}
export const Simple = Template.bind({})
Simple.args = {
@@ -71,7 +77,7 @@ Simple.args = {
export const Multinode = Template.bind({})
Multinode.args = {
...Multinode.args,
startLoc: [0,0],
startPoint: [0, 0],
sequence: [
{
$smType: "condition",
+7 -23
View File
@@ -1,33 +1,17 @@
import { FC } from "react";
import { useTheme } from "styles/theme";
import { Point } from "types/graphs";
import { AutomationSequenceNode } from '../../types/automations/index';
import { computeNodesEdges } from './location';
import { SequenceNodeSettings } from './types';
export const SequenceNodes: FC<{
startLoc: Point,
sequence: AutomationSequenceNode[],
nodeHeight: number,
nodeWidth: number,
distanceFactor: number,
}> = ({
startLoc,
sequence,
nodeHeight,
nodeWidth,
distanceFactor,
}) => {
export const SequenceNodes: FC<SequenceNodeSettings> = (props) => {
const theme = useTheme();
return <>
{Array.from(computeNodesEdges(
startLoc,
nodeWidth,
nodeHeight,
distanceFactor,
sequence,
theme.secondaryAccent,
))}
{Array.from(computeNodesEdges({
...props,
keyPrefix: "",
edgeColor: theme.secondaryAccent,
}))}
</>
}
@@ -2,79 +2,101 @@ 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';
export function* computeNodesEdges(
startPoint: Point,
nodeWidth: number,
nodeHeight: number,
distanceFactor: number,
sequence: AutomationSequenceNode[],
edgeColor: string,
keyPrefix: string = "",
): Generator<JSX.Element, [Point, Point | null], never> {
{
sequence,
keyPrefix = "",
onChange = () => { },
...st
}: SequenceNodeSettings,
): Generator<JSX.Element, Point | null, never> {
let lastStartPoint: Point | null = null;
let lastNodeLoc: Point | null = null;
for (let i = 0; i < sequence.length; i++) {
const node = sequence[i];
const nodeLoc: Point = lastStartPoint = lastStartPoint === null ? [...startPoint] : [
lastStartPoint[0] + nodeWidth * distanceFactor,
const nodeLoc: Point = lastStartPoint = lastStartPoint === null ? [...st.startPoint] : [
lastStartPoint[0] + st.nodeWidth * st.distanceFactor,
lastStartPoint[1]
];
let nextNodeLoc: Point = [nodeLoc[0] + distanceFactor * nodeWidth, nodeLoc[1] + nodeHeight / 2];
let nextNodeLoc: Point = [nodeLoc[0] + st.distanceFactor * st.nodeWidth, nodeLoc[1] + st.nodeHeight / 2];
yield <DAGNode
key={`node${keyPrefix}.${i}`}
height={nodeHeight}
width={nodeWidth}
height={st.nodeHeight}
width={st.nodeWidth}
loc={nodeLoc}
text={node.$smType}
color={node.$smType === 'action' ? 'green' : 'blue'}
edge={lastNodeLoc === null ? undefined : {
direction: '1<-2',
loc: [lastNodeLoc[0] + nodeWidth, lastNodeLoc[1] + nodeHeight / 2],
color: edgeColor,
loc: [lastNodeLoc[0] + st.nodeWidth, lastNodeLoc[1] + st.nodeHeight / 2],
color: st.edgeColor,
}}
onXClick={() => onChange([
...sequence.slice(0, i),
...sequence.slice(i + 1),
])}
/>
if ((node.$smType === 'action') && (node.action === 'choose')) {
for (let j = 0; j < node.action_data.choose.length; j++) {
const subSeq = node.action_data.choose[j].sequence;
const subStartLoc: Point = [
nodeLoc[0] + nodeWidth * distanceFactor,
nodeLoc[1] + nodeHeight * (j + 1)
nodeLoc[0] + st.nodeWidth * st.distanceFactor,
nodeLoc[1] + st.nodeHeight * (j + 1)
];
const it = computeNodesEdges(
subStartLoc,
nodeWidth,
nodeHeight,
distanceFactor,
subSeq,
edgeColor,
keyPrefix=`.${i}.${j}[${nodeLoc}]`
);
yield <DAGEdge
p1={[nodeLoc[0] + nodeWidth, nodeLoc[1] + nodeHeight / 2]}
p2={[subStartLoc[0], subStartLoc[1] + nodeHeight / 2]}
direction="1->2"
color={edgeColor}
/>
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()
}
nextNodeLoc[0] = (next.value as any)[1][0] + nodeWidth * distanceFactor
lastStartPoint = [(next.value as any)[1][0], lastStartPoint[1]]
if (next.value) {
yield <DAGEdge
key={`edge${keyPrefix}.${i}.${j}`}
p1={[nodeLoc[0] + st.nodeWidth, nodeLoc[1] + st.nodeHeight / 2]}
p2={[subStartLoc[0], subStartLoc[1] + st.nodeHeight / 2]}
direction="1->2"
color={st.edgeColor}
/>
nextNodeLoc[0] = next.value[0] + st.nodeWidth * st.distanceFactor
lastStartPoint = [next.value[0], lastStartPoint[1]]
}
}
}
if (i === sequence.length - 1) {
yield <DAGEdge
p1={[nodeLoc[0] + nodeWidth, nodeLoc[1] + nodeHeight / 2]}
key={`edge${keyPrefix}.${i}`}
p1={[nodeLoc[0] + st.nodeWidth, nodeLoc[1] + st.nodeHeight / 2]}
p2={nextNodeLoc}
direction="1->2"
color={edgeColor}
color={st.edgeColor}
/>
}
lastNodeLoc = [...nodeLoc]
}
return [startPoint, lastStartPoint]
return lastStartPoint
}
@@ -0,0 +1,13 @@
import { AutomationSequenceNode } from "types/automations";
import { Point } from "types/graphs";
export type SequenceNodeSettings = {
startPoint: Point,
sequence: AutomationSequenceNode[],
nodeHeight: number,
nodeWidth: number,
distanceFactor: number,
edgeColor: string,
keyPrefix?: string,
onChange?: (s: AutomationSequenceNode[]) => void;
}