remove and add new nodes

This commit is contained in:
ari committed 2021-11-25 14:54:10 -05:00
1 parent a3774bc6fb
commit f31ec2e60a
6 files changed
+196 -91

No files matched your search

+30 -30
View File
@@ -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 && <>
<circle cx={cx1} cy={cy1} r="5" fill="yellow" />
<line x1={cx1} y1={cy1} x2={x1} y2={y1} stroke="yellow"/>
<line x1={cx1} y1={cy1} x2={x2} y2={y2} stroke="yellow"/>
<circle cx={cx2} cy={cy2} r="5" fill="green"/>
<line x1={cx2} y1={cy2} x2={x1} y2={y1} stroke="green"/>
<line x1={cx1} y1={cy1} x2={x1} y2={y1} stroke="yellow" />
<line x1={cx1} y1={cy1} x2={x2} y2={y2} stroke="yellow" />
<circle cx={cx2} cy={cy2} r="5" fill="green" />
<line x1={cx2} y1={cy2} x2={x1} y2={y1} stroke="green" />
<line x1={cx2} y1={cy2} x2={x2} y2={y2} stroke="green" />
<text x={mx} y={my} stroke="white">{theta}</text>
</>}
<path stroke={color}
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)" : ''
}
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)" : ''
}
/>
</>
}
@@ -107,6 +107,10 @@ Multinode.args = {
action: "choose",
action_data: {
choose: [
{
conditions: [],
sequence: []
},
{
conditions: [
{
@@ -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<JSX.Element, Point | null, never> {
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 <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]]
}
}
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 <DAGEdge
@@ -95,8 +64,64 @@ export function* computeNodesEdges(
direction="1->2"
color={st.edgeColor}
/>
yield <AddButton
key={`add${keyPrefix}.${i}`}
loc={[nextNodeLoc[0], nextNodeLoc[1] - 4.5]}
height={10}
width={10}
onClick={rootUpdater.addNode}
/>
}
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 <DAGEdge
key={`edge${st.keyPrefix}.${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}
/>
if (next.value) {
nextNodeLoc[0] = next.value[0] + st.nodeWidth * st.distanceFactor
lastStartPoint = [Math.max(next.value[0], lastStartPoint[0]), lastStartPoint[1]]
} else {
yield <AddButton
key={`add${st.keyPrefix}.${j}`}
loc={[subStartLoc[0], subStartLoc[1] + st.nodeHeight / 2 - 4.5]}
height={10}
width={10}
onClick={updater.addNode}
/>
}
}
return { lastStartPoint, nextNodeLoc }
}
+10 -7
View File
@@ -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;
}
@@ -0,0 +1,73 @@
import { AutomationSequenceNode } from "types/automations";
export type Updater = ReturnType<typeof makeUpdater>;
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}`)
}
});
+2 -2
View File
@@ -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];
export type ActionType = typeof ActionTypes[number];