state updater for choosenodes

This commit is contained in:
ari committed 2022-07-12 20:25:20 -04:00
1 parent 0c5ef76f4b
commit 1661df888a
2 files changed
+135 -67

No files matched your search

@@ -19,7 +19,6 @@ import { XYPosition } from "react-flow-renderer";
import { DAGElementsState } from "./state";
import { SequenceNodeMaker } from "../nodes/SequenceNode";
import { getNodeType } from "utils/automations";
import { pointApply } from "./math";
import { ButtonNodeMaker } from "../nodes/ButtonNode";
import { AddIcon } from "components/Icons";
@@ -204,76 +203,73 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
const state = new DAGElementsState();
nodes.forEach((node, nodeIndex) => {
if (node.enabled ?? true) {
// offset position
let offsetPos: XYPosition = {
x:
args.position.x +
args.dims.distanceFactor.node *
args.dims.node.width *
(nodeIndex + 1),
y:
args.position.y +
args.dims.distanceFactor.node *
args.dims.node.height *
(nodeIndex + 1),
};
// functions
const processSequence = (
sequence: AutomationSequenceNode[],
index: number,
condition: AutomationCondition[] | null = null
) => {
let pos = offsetPos;
let lastNodeId = args.lastNodeId;
if (condition !== null) {
const conditionState = makeConditionNodes(condition, {
...args,
position: pos,
lastNodeId,
nodeIndex: index,
nodeId: `${args.nodeId}-choose-${nodeIndex}-${index}-condition`,
});
conditionState.data.edges[0].label = `Condition #${index}`;
state.extend(conditionState);
pos = distance.moveFromTo("condition", "node", pos, args.dims);
lastNodeId = conditionState.lastNodeId;
}
const output = makeSequenceNodes(sequence, {
if (nodes.length !== 1) {
throw new Error("makeChooseNodes only access a list of 1");
}
const node = nodes[0];
if (node.enabled ?? true) {
// offset position
let offsetPos: XYPosition = {
x: args.position.x + args.dims.distanceFactor.node * args.dims.node.width,
y:
args.position.y + args.dims.distanceFactor.node * args.dims.node.height,
};
// functions
const processSequence = (j: number | "else") => {
const stateUpdater = args.stateUpdater.createChoosNodeUpdater(
args.nodeIndex,
j
);
let pos = offsetPos;
let lastNodeId = args.lastNodeId;
if (j !== "else") {
const conditionState = makeConditionNodes(node.choose[j].conditions, {
...args,
stateUpdater,
position: pos,
lastNodeId: undefined,
nodeIndex: index,
nodeId: `${args.nodeId}-choose-${index}-sequence`,
lastNodeId,
nodeIndex: j,
nodeId: `${args.nodeId}-choose-${j}-condition`,
});
if (lastNodeId && output.data.nodes.length > 0) {
state.addEdge(
lastNodeId,
output.data.nodes[0].id,
true,
condition === null ? "else" : undefined
);
}
offsetPos = distance.moveAlongRelativeTo(
offsetPos,
output.bbox[1],
args.dims,
condition === null ? "node" : "condition"
);
state.extend(output);
};
// process options
for (let sIndex = 0; sIndex < node.choose.length; sIndex++) {
const subSequence = node.choose[sIndex];
processSequence(subSequence.sequence, sIndex, subSequence.conditions);
conditionState.data.edges[0].label = `Condition #${j}`;
state.extend(conditionState);
pos = distance.moveFromTo("condition", "node", pos, args.dims);
lastNodeId = conditionState.lastNodeId;
}
// else
processSequence(node.default ?? [], node.choose.length);
const sequence =
j === "else" ? node.default ?? [] : node.choose[j].sequence;
const output = makeSequenceNodes(sequence, {
...args,
stateUpdater,
position: pos,
lastNodeId: undefined,
nodeIndex: j === "else" ? node.choose.length : j,
nodeId: `${args.nodeId}-choose-${j}-sequence`,
});
if (lastNodeId && output.data.nodes.length > 0) {
state.addEdge(
lastNodeId,
output.data.nodes[0].id,
true,
j === "else" ? "else" : undefined
);
}
offsetPos = distance.moveAlongRelativeTo(
offsetPos,
output.bbox[1],
args.dims,
j === "else" ? "node" : "condition"
);
state.extend(output);
};
// process options
for (let sIndex = 0; sIndex < node.choose.length; sIndex++) {
processSequence(sIndex);
}
});
// else
processSequence("else");
}
return state;
};
@@ -1,12 +1,13 @@
import {
AutomationActionData,
AutomationNodeTypes,
AutomationSequenceNode,
} from "types/automations";
import { ModalState } from "../board/types";
import { SequenceNodeActions } from "../nodes/SequenceNode/types";
import { DAGUpdaterArgs } from "./types";
import { mapAutoActionKeyToNodeType } from "./util";
import { ChooseAction } from "types/automations/actions";
import { AutomationCondition } from "types/automations/conditions";
export const createBasicUpdater =
<K extends keyof AutomationActionData, N extends AutomationActionData[K][0]>(
@@ -156,6 +157,77 @@ export const createUpdater = (
onMove,
};
},
createChoosNodeUpdater(i: number, j: number | "else") {
const node = args.sequence.data[i] as ChooseAction;
if (j === "else") {
return createUpdater({
openModal: args.openModal,
condition: {
data: [],
onUpdate: console.warn,
},
sequence: {
data: node.default ?? [],
onUpdate: (upd) =>
basic.sequence.updateNode(
{
...node,
default: upd,
},
i
),
},
trigger: {
data: [],
onUpdate: console.warn,
},
});
} else {
return createUpdater({
openModal: args.openModal,
condition: {
data: node.choose[j].conditions,
onUpdate: (upd: any) =>
basic.sequence.updateNode(
{
...node,
choose: [
...node.choose.slice(0, j),
{
...node.choose[j],
conditions: upd,
},
...node.choose.slice(j + 1),
],
},
i
),
},
sequence: {
data: node.choose[j].sequence,
onUpdate: (upd) =>
basic.sequence.updateNode(
{
...node,
choose: [
...node.choose.slice(0, j),
{
...node.choose[j],
sequence: upd,
},
...node.choose.slice(j + 1),
],
},
i
),
},
trigger: {
data: [],
onUpdate: console.warn,
},
});
}
},
};
};