repeat node initial support
This commit is contained in:
1 parent
f392cf17fd
commit
de2307901b
13 files changed
+301
-77
No files matched your search
@@ -2,7 +2,6 @@ import { DAGDims } from "./types";
|
||||
|
||||
export const NODE_WIDTH = 200 as const;
|
||||
export const NODE_HEIGHT = 80 as const;
|
||||
export const CIRCLE_SIZE = 40 as const;
|
||||
|
||||
export const DEFAULT_DIMS: DAGDims = {
|
||||
trigger: {
|
||||
@@ -17,10 +16,6 @@ export const DEFAULT_DIMS: DAGDims = {
|
||||
height: NODE_HEIGHT * 3,
|
||||
width: NODE_WIDTH * 1.5,
|
||||
},
|
||||
circle: {
|
||||
height: CIRCLE_SIZE,
|
||||
width: CIRCLE_SIZE,
|
||||
},
|
||||
distanceFactor: {
|
||||
node: 1.5,
|
||||
collection: 1,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { DAGDims, DAGEntities, Size } from "./types";
|
||||
import { DAGDims, DAGEntities } from "./types";
|
||||
import { XYPosition } from "react-flow-renderer";
|
||||
|
||||
export const moveAlong = (
|
||||
@@ -22,26 +22,6 @@ export const moveAlong = (
|
||||
return !dims.flipped ? moveOnX : moveOnY;
|
||||
};
|
||||
|
||||
export const moveAlongOnce = (
|
||||
by: keyof DAGEntities,
|
||||
startPoint: XYPosition,
|
||||
dims: DAGDims,
|
||||
reversed: boolean = false
|
||||
): XYPosition => {
|
||||
const moveOnY = {
|
||||
y: startPoint.y + dims[by].height * (dims.distanceFactor.node - 1),
|
||||
x: startPoint.x,
|
||||
};
|
||||
const moveOnX = {
|
||||
x: startPoint.x + dims[by].width * (dims.distanceFactor.node - 1),
|
||||
y: startPoint.y,
|
||||
};
|
||||
if (reversed) {
|
||||
return dims.flipped ? moveOnX : moveOnY;
|
||||
}
|
||||
return !dims.flipped ? moveOnX : moveOnY;
|
||||
};
|
||||
|
||||
export const moveAlongRelativeTo = (
|
||||
offsetPos: XYPosition,
|
||||
relatedPos: XYPosition,
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
} from "types/automations";
|
||||
import { AutomationCondition } from "types/automations/conditions";
|
||||
import { CollectionNodeMaker } from "../nodes/CollectionNode";
|
||||
import { ChooseAction } from "types/automations/actions";
|
||||
import { ChooseAction, RepeatAction } from "types/automations/actions";
|
||||
import { XYPosition } from "react-flow-renderer";
|
||||
import { DAGElementsOutputState } from "./outputState";
|
||||
import { SequenceNodeMaker } from "../nodes/SequenceNode";
|
||||
@@ -31,7 +31,7 @@ export const useAutomationNodes = (
|
||||
...args.dims,
|
||||
};
|
||||
const state = useDAGElementsState();
|
||||
const outputState = new DAGElementsOutputState();
|
||||
const outputState = new DAGElementsOutputState(dims.position, dims);
|
||||
outputState.extend(
|
||||
makeTriggerNodes(automation.trigger, {
|
||||
...args,
|
||||
@@ -51,7 +51,7 @@ export const useAutomationNodes = (
|
||||
position: distance.moveFromTo(
|
||||
"trigger",
|
||||
"condition",
|
||||
outputState.lastNodePos,
|
||||
outputState.getLastNodePos(),
|
||||
dims
|
||||
),
|
||||
nodeId: `${dims.flipped}-condition`,
|
||||
@@ -68,7 +68,7 @@ export const useAutomationNodes = (
|
||||
position: distance.moveFromTo(
|
||||
"condition",
|
||||
"node",
|
||||
outputState.lastNodePos,
|
||||
outputState.getLastNodePos(),
|
||||
dims
|
||||
),
|
||||
nodeId: `${dims.flipped}-sequence`,
|
||||
@@ -84,7 +84,7 @@ export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
|
||||
nodes,
|
||||
{ dims, namer, stateUpdater, nodeId, position }
|
||||
) => {
|
||||
const outputState = new DAGElementsOutputState();
|
||||
const outputState = new DAGElementsOutputState(position, dims);
|
||||
outputState.addNode(
|
||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||
...dims.trigger,
|
||||
@@ -104,7 +104,7 @@ export const makeConditionNodes: ElementMaker<AutomationCondition> = (
|
||||
nodes,
|
||||
{ nodeId, stateUpdater, position, dims, namer, lastNodeId }
|
||||
) => {
|
||||
const outputState = new DAGElementsOutputState();
|
||||
const outputState = new DAGElementsOutputState(position, dims);
|
||||
outputState.addNode(
|
||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||
...dims.condition,
|
||||
@@ -119,7 +119,7 @@ export const makeConditionNodes: ElementMaker<AutomationCondition> = (
|
||||
})
|
||||
);
|
||||
if (lastNodeId && outputState.lastNodeId) {
|
||||
outputState.addEdge(lastNodeId, outputState.lastNodeId, true);
|
||||
outputState.addEdge(lastNodeId, outputState.lastNodeId, { animated: true });
|
||||
}
|
||||
return outputState;
|
||||
};
|
||||
@@ -128,18 +128,19 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
nodes,
|
||||
args
|
||||
) => {
|
||||
const outputState = new DAGElementsOutputState();
|
||||
const { lastNodeId, position, dims, namer, stateUpdater, state } = args;
|
||||
const outputState = new DAGElementsOutputState(position, dims);
|
||||
nodes.forEach((node, nodeIndex) => {
|
||||
// the JSON.stringify is hack to make the edges render nicely
|
||||
const nodeId = `${args.nodeId}-${
|
||||
args.nodeIndex
|
||||
}-${nodeIndex}-${JSON.stringify(node)}`;
|
||||
outputState.incNextPos("node");
|
||||
const element = outputState.addNode(
|
||||
SequenceNodeMaker.makeElement(
|
||||
{
|
||||
id: nodeId,
|
||||
position: distance.moveAlong("node", position, nodeIndex, dims),
|
||||
position: outputState.nextPos,
|
||||
},
|
||||
dims,
|
||||
{
|
||||
@@ -163,8 +164,20 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
outputState.extend(
|
||||
makeChooseeNodes([node], {
|
||||
...args,
|
||||
nodeId,
|
||||
lastNodeId: element.id,
|
||||
position: outputState.lastNodePos,
|
||||
position: outputState.getLastNodePos(),
|
||||
nodeIndex,
|
||||
}),
|
||||
true
|
||||
);
|
||||
} else if ("repeat" in node) {
|
||||
outputState.extend(
|
||||
makeRepeatNodes([node], {
|
||||
...args,
|
||||
nodeId,
|
||||
lastNodeId: element.id,
|
||||
position: outputState.getLastNodePos(),
|
||||
nodeIndex,
|
||||
}),
|
||||
true
|
||||
@@ -176,7 +189,12 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
ButtonNodeMaker.makeElement(
|
||||
{
|
||||
id: `${args.nodeId}-add-new`,
|
||||
position: distance.moveAlong("node", position, nodes.length, dims),
|
||||
position: distance.moveAlong(
|
||||
"node",
|
||||
outputState.getLastNodePos(position),
|
||||
1,
|
||||
dims
|
||||
),
|
||||
},
|
||||
dims,
|
||||
{
|
||||
@@ -195,7 +213,7 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
};
|
||||
|
||||
export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
const outputState = new DAGElementsOutputState();
|
||||
const outputState = new DAGElementsOutputState(args.position, args.dims);
|
||||
if (nodes.length !== 1) {
|
||||
throw new Error("makeChooseNodes only access a list of 1");
|
||||
}
|
||||
@@ -227,7 +245,7 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
nodeIndex: j,
|
||||
nodeId: `${args.nodeId}-choose-${j}-condition`,
|
||||
});
|
||||
conditionState.data.edges[0].label = `Condition #${j}`;
|
||||
conditionState.data.edges[0].label = `Condition #${j + 1}`;
|
||||
outputState.extend(conditionState);
|
||||
pos = distance.moveFromTo("condition", "node", pos, args.dims);
|
||||
lastNodeId = conditionState.lastNodeId;
|
||||
@@ -243,12 +261,10 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
nodeId: `${args.nodeId}-choose-${j}-sequence`,
|
||||
});
|
||||
if (lastNodeId && output.data.nodes.length > 0) {
|
||||
outputState.addEdge(
|
||||
lastNodeId,
|
||||
output.data.nodes[0].id,
|
||||
true,
|
||||
j === "else" ? "else" : undefined
|
||||
);
|
||||
outputState.addEdge(lastNodeId, output.data.nodes[0].id, {
|
||||
animated: true,
|
||||
label: j === "else" ? "else" : undefined,
|
||||
});
|
||||
}
|
||||
offsetPos = distance.moveAlongRelativeTo(
|
||||
offsetPos,
|
||||
@@ -269,3 +285,69 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
|
||||
return outputState;
|
||||
};
|
||||
|
||||
export const makeRepeatNodes: ElementMaker<RepeatAction> = (nodes, args) => {
|
||||
const outputState = new DAGElementsOutputState(args.position, args.dims);
|
||||
if (nodes.length !== 1) {
|
||||
throw new Error("makeChooseNodes only access a list of 1");
|
||||
}
|
||||
const node = nodes[0];
|
||||
if (
|
||||
(node.enabled ?? true) &&
|
||||
!args.state.get(args.lastNodeId ?? "").isClosed
|
||||
) {
|
||||
// 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,
|
||||
};
|
||||
const stateUpdater = args.stateUpdater.createRepeatNodeUpdater(
|
||||
args.nodeIndex
|
||||
);
|
||||
let pos = offsetPos;
|
||||
let lastNodeId = args.lastNodeId;
|
||||
const conditionState = makeConditionNodes(node.repeat.while, {
|
||||
...args,
|
||||
stateUpdater,
|
||||
position: pos,
|
||||
lastNodeId,
|
||||
nodeIndex: 0,
|
||||
nodeId: `${args.nodeId}-repeat-while`,
|
||||
});
|
||||
conditionState.data.edges[0].label = `While`;
|
||||
outputState.extend(conditionState);
|
||||
pos = distance.moveFromTo("condition", "node", pos, args.dims);
|
||||
lastNodeId = conditionState.lastNodeId;
|
||||
const sequenceState = makeSequenceNodes(node.repeat.sequence, {
|
||||
...args,
|
||||
stateUpdater,
|
||||
position: pos,
|
||||
lastNodeId: undefined,
|
||||
nodeIndex: 0,
|
||||
nodeId: `${args.nodeId}-repeat-sequence`,
|
||||
});
|
||||
if (lastNodeId && sequenceState.data.nodes.length > 0) {
|
||||
outputState.addEdge(lastNodeId, sequenceState.data.nodes[0].id, {
|
||||
animated: true,
|
||||
label: "do",
|
||||
});
|
||||
}
|
||||
offsetPos = distance.moveAlongRelativeTo(
|
||||
offsetPos,
|
||||
sequenceState.bbox[1],
|
||||
args.dims,
|
||||
"condition"
|
||||
);
|
||||
if (sequenceState.lastNodeId && conditionState.lastNodeId) {
|
||||
outputState.addEdge(sequenceState.lastNodeId, conditionState.lastNodeId, {
|
||||
animated: true,
|
||||
label: "repeat",
|
||||
targetHandle: "return",
|
||||
});
|
||||
}
|
||||
outputState.extend(sequenceState);
|
||||
}
|
||||
|
||||
return outputState;
|
||||
};
|
||||
@@ -1,6 +1,8 @@
|
||||
import { MarkerType, Edge, XYPosition } from "react-flow-renderer";
|
||||
import { DAGAutomationFlowNode } from "../nodes";
|
||||
import { addSize, pointApply } from "./math";
|
||||
import { Bbox, ElementData, LastNode } from "./types";
|
||||
import { Bbox, DAGDims, DAGEntities, ElementData, LastNode } from "./types";
|
||||
import { moveAlong } from "./distance";
|
||||
|
||||
export class DAGElementsOutputState {
|
||||
public data: ElementData = {
|
||||
@@ -13,6 +15,8 @@ export class DAGElementsOutputState {
|
||||
{ x: 0, y: 0 },
|
||||
];
|
||||
|
||||
constructor(public nextPos: XYPosition, public dims: DAGDims) {}
|
||||
|
||||
public extend(
|
||||
other: DAGElementsOutputState,
|
||||
ignoreLastNode: boolean = false
|
||||
@@ -23,6 +27,18 @@ export class DAGElementsOutputState {
|
||||
this.bbox[1] = pointApply(this.bbox[1], other.bbox[1], Math.max);
|
||||
if (!ignoreLastNode) {
|
||||
this.lastNode = other.lastNode;
|
||||
this.nextPos = (this.lastNode as any).pos;
|
||||
}
|
||||
if (this.dims.flipped) {
|
||||
this.nextPos = {
|
||||
y: Math.max(this.nextPos.y, other.bbox[1].y),
|
||||
x: this.nextPos.x,
|
||||
};
|
||||
} else {
|
||||
this.nextPos = {
|
||||
x: Math.max(this.nextPos.x, other.bbox[1].x),
|
||||
y: this.nextPos.y,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +48,9 @@ export class DAGElementsOutputState {
|
||||
animatedEdge: boolean = false
|
||||
) {
|
||||
if (addEdge && this.lastNodeId) {
|
||||
this.addEdge(this.lastNodeId, node.id, animatedEdge);
|
||||
this.addEdge(this.lastNodeId, node.id, {
|
||||
animated: animatedEdge,
|
||||
});
|
||||
}
|
||||
this.data.nodes.push(node);
|
||||
this.bbox[0] = pointApply(this.bbox[0], node.position, Math.min);
|
||||
@@ -52,18 +70,17 @@ export class DAGElementsOutputState {
|
||||
return node;
|
||||
}
|
||||
|
||||
public addEdge(
|
||||
from: string,
|
||||
to: string,
|
||||
animated: boolean = false,
|
||||
label: string | undefined = undefined
|
||||
) {
|
||||
public addEdge(from: string, to: string, edgeProps: Partial<Edge> = {}) {
|
||||
this.data.edges.push({
|
||||
markerEnd: {
|
||||
type: MarkerType.ArrowClosed,
|
||||
width: 25,
|
||||
height: 25,
|
||||
},
|
||||
...edgeProps,
|
||||
id: `${from}->${to}`,
|
||||
source: from,
|
||||
target: to,
|
||||
label,
|
||||
animated,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -74,10 +91,20 @@ export class DAGElementsOutputState {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public get lastNodePos() {
|
||||
public getLastNodePos(orThis: XYPosition | undefined = undefined) {
|
||||
if (this.lastNode) {
|
||||
return this.lastNode.pos;
|
||||
}
|
||||
if (orThis) {
|
||||
return orThis;
|
||||
}
|
||||
throw Error("missing lastnode");
|
||||
}
|
||||
|
||||
public incNextPos(by: keyof DAGEntities) {
|
||||
if (!this.lastNode) {
|
||||
return;
|
||||
}
|
||||
this.nextPos = moveAlong(by, this.nextPos, 1, this.dims);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ export type DAGNodeState = Partial<{
|
||||
export type DAGElementsState = ReturnType<typeof useDAGElementsState>;
|
||||
export const useDAGElementsState = () => {
|
||||
const [data, setData] = useState<Record<string, DAGNodeState>>({});
|
||||
console.log(data);
|
||||
return {
|
||||
get(nodeId: string): DAGNodeState {
|
||||
return data[nodeId] ?? {};
|
||||
@@ -28,7 +27,7 @@ export const useDAGElementsState = () => {
|
||||
},
|
||||
|
||||
getIsClosedActions(nodeId: string, node: AutomationNode) {
|
||||
if ("choose" in node) {
|
||||
if ("choose" in node || "repeat" in node) {
|
||||
return {
|
||||
isClosed: this.get(nodeId).isClosed ?? false,
|
||||
setIsClosed: (v: boolean) => this.set(nodeId, "isClosed", v),
|
||||
|
||||
@@ -16,7 +16,6 @@ export type ElementData = {
|
||||
export type DAGEntityDim = Size;
|
||||
export type DAGEntities = {
|
||||
node: DAGEntityDim;
|
||||
circle: DAGEntityDim;
|
||||
condition: DAGEntityDim;
|
||||
trigger: DAGEntityDim;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user