fixed up styling for choose

This commit is contained in:
ari committed 2022-07-12 18:47:18 -04:00
1 parent 084a3585d0
commit 0c5ef76f4b
11 files changed
+283 -61

No files matched your search

@@ -11,4 +11,15 @@
position: absolute;
left: 50%;
top: 50%;
}
.dag-graphboard rect.react-flow__edge-textbg {
fill: var(--mui-background-paper);
stroke: var(--mui-text-primary);
stroke-width: 0.25px;
}
.dag-graphboard text.react-flow__edge-text {
fill: var(--mui-text-primary);
}
@@ -58,7 +58,7 @@ export const DAGGraphBoardInner: FC<DAGGraphBoardProps> = ({
);
}
// this hack avoids breakage on when zooming
const prefix = Math.floor((((zoom - 0.5) / (2 - 0.5)) * 10) / 3);
const prefix = Math.floor(zoom / (2.5 / 3));
const elements = {
nodes: state.data.elements.nodes.map((n) => ({
...n,
@@ -7,7 +7,7 @@ export const CIRCLE_SIZE = 40 as const;
export const DEFAULT_DIMS: DAGDims = {
trigger: {
height: NODE_HEIGHT * 3,
width: NODE_WIDTH * 4.5,
width: NODE_WIDTH * 1.5,
},
node: {
height: NODE_HEIGHT,
@@ -15,7 +15,7 @@ export const DEFAULT_DIMS: DAGDims = {
},
condition: {
height: NODE_HEIGHT * 3,
width: NODE_WIDTH * 4.5,
width: NODE_WIDTH * 1.5,
},
circle: {
height: CIRCLE_SIZE,
@@ -22,6 +22,46 @@ 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,
dims: DAGDims,
incrementBy: keyof DAGEntities = "condition"
) =>
dims.flipped
? {
x:
relatedPos.x +
(dims.distanceFactor.node - 1) * dims[incrementBy].width,
y: offsetPos.y,
}
: {
y:
relatedPos.y +
(dims.distanceFactor.node - 1) * dims[incrementBy].height,
x: offsetPos.x,
};
export const moveFromTo = (
from: keyof DAGEntities,
to: keyof DAGEntities,
@@ -97,7 +97,7 @@ export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
state.addNode(
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
...dims.trigger,
color: "red",
collectionType: "trigger",
onAddNode: () => stateUpdater.basic.trigger.addNode(null),
nodes: nodes.map((node, index) => ({
enabled: node.enabled ?? true,
@@ -117,7 +117,7 @@ export const makeConditionNodes: ElementMaker<AutomationCondition> = (
state.addNode(
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
...dims.condition,
color: "blue",
collectionType: "condition",
hasInput: !!lastNodeId,
onAddNode: () => stateUpdater.basic.condition.addNode(null),
nodes: nodes.map((node, index) => ({
@@ -127,6 +127,7 @@ export const makeConditionNodes: ElementMaker<AutomationCondition> = (
})),
})
);
console.log("pre", { lastNodeId, stateLast: state.lastNodeId });
if (lastNodeId && state.lastNodeId) {
state.addEdge(lastNodeId, state.lastNodeId, true);
}
@@ -227,25 +228,19 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
) => {
let pos = offsetPos;
let lastNodeId = args.lastNodeId;
// if (condition !== null) {
// const conditionState = makeConditionNodes(condition, {
// ...args,
// position: pos,
// lastNodeId,
// nodeIndex: index,
// nodeId: `${args.nodeId}-choose-${index}-condition`,
// });
// // pos.y = conditionState.bbox[1].y;
// state.extend(conditionState);
// if (lastNodeId && conditionState.data.nodes.length > 0) {
// state.addEdge(lastNodeId, conditionState.data.nodes[0].id, true);
// }
// lastNodeId = conditionState.lastNodeId ?? lastNodeId;
// pos = {
// y: conditionState.bbox[1].y,
// x: pos.x,
// };
// }
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, {
...args,
position: pos,
@@ -254,21 +249,19 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
nodeId: `${args.nodeId}-choose-${index}-sequence`,
});
if (lastNodeId && output.data.nodes.length > 0) {
state.addEdge(lastNodeId, output.data.nodes[0].id, true);
state.addEdge(
lastNodeId,
output.data.nodes[0].id,
true,
condition === null ? "else" : undefined
);
}
offsetPos = args.dims.flipped
? {
x:
output.bbox[1].x +
(args.dims.distanceFactor.node - 1) * args.dims.node.width,
y: offsetPos.y,
}
: {
y:
output.bbox[1].y +
(args.dims.distanceFactor.node - 1) * args.dims.node.height,
x: offsetPos.x,
};
offsetPos = distance.moveAlongRelativeTo(
offsetPos,
output.bbox[1],
args.dims,
condition === null ? "node" : "condition"
);
state.extend(output);
};
@@ -49,11 +49,17 @@ export class DAGElementsState {
return node;
}
public addEdge(from: string, to: string, animated: boolean = false) {
public addEdge(
from: string,
to: string,
animated: boolean = false,
label: string | undefined = undefined
) {
this.data.edges.push({
id: `${from}->${to}`,
source: from,
target: to,
label,
animated,
});
}
@@ -1,7 +1,10 @@
.collection-nodes--wrap {
--padding: 1.5em;
--child-height: calc(var(--node-height) - 2*var(--padding));
--child-width: calc(var(--node-width) - 2*var(--padding));
--padding-left: 1.5em;
--padding-right: 3em;
--padding-top: 2.25em;
--padding-bottom: 0.25em;
--child-height: calc(var(--node-height) - var(--padding-top) - var(--padding-bottom));
--child-width: calc(var(--node-width) - var(--padding-left) - var(--padding-right));
min-width: var(--node-width);
min-height: var(--node-height);
@@ -18,20 +21,34 @@
.collection-nodes {
position: relative;
display: flex;
flex-wrap: wrap;
flex-direction: row;
align-items: center;
gap: 0em 1em;
max-width: var(--child-width);
max-height: var(--child-height);
border-color: var(--node-color);
border-style: solid;
border-width: 1px;
border-radius: 10px;
max-width: max-content;
max-height: max-content;
padding-right: var(--padding-right);
}
.collection-nodes--inner {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
justify-content: space-around;
gap: 0 1em;
max-width: var(--child-width);
max-height: var(--child-height);
min-height: calc(var(--child-height) / 4);
min-width: var(--child-width);
padding-left: var(--padding-left);
padding-top: var(--padding-top);
padding-bottom: var(--padding-bottom);
overflow: auto;
padding: var(--padding);
}
.collection-nodes .react-flow__handle {
@@ -43,4 +60,16 @@
bottom: 0.25em;
right: 0.25em;
font-size: 0.5em;
}
.collection-nodes .add {
position: absolute;
bottom: calc(50% - var(--padding-bottom));
right: 0.25em;
}
.collection-nodes--title {
position: absolute;
top: 0.5em;
left: calc(var(--child-width)/2);
}
@@ -7,6 +7,7 @@ import { SequenceNodeElement } from "../SequenceNode/SequenceNodeElement";
import { CollectionNodeProps } from "./types";
import { Handle, Position } from "react-flow-renderer";
import { useSequenceNodeColor } from "../SequenceNode/util";
import { prettyName } from "utils/formatting";
export const CollectionNode: FC<CollectionNodeProps> = ({
onAddNode,
@@ -17,6 +18,7 @@ export const CollectionNode: FC<CollectionNodeProps> = ({
flipped,
color,
hasInput = false,
collectionType,
}) => {
const nodeColor = useSequenceNodeColor(color);
@@ -32,22 +34,29 @@ export const CollectionNode: FC<CollectionNodeProps> = ({
}
>
<div className="collection-nodes">
{nodes.map((n, i) => (
<SequenceNodeElement
{...n}
{...sequenceNode}
key={i}
color={color}
flipped={flipped}
/>
))}
<ButtonIcon icon={<Add />} onClick={onAddNode} />
<span className="collection-nodes--title">
{prettyName(collectionType)}s
</span>
<div className="collection-nodes--inner">
{nodes.map((n, i) => (
<SequenceNodeElement
{...n}
{...sequenceNode}
key={i}
color={color}
flipped={flipped}
/>
))}
</div>
<ButtonIcon className="add" icon={<Add />} onClick={onAddNode} />
<Handle
key="source"
type="source"
position={flipped ? Position.Bottom : Position.Right}
/>
{hasInput && (
<Handle
key="target"
type="target"
position={flipped ? Position.Top : Position.Left}
/>
@@ -0,0 +1,126 @@
import React, { FC } from "react";
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { CollectionNode } from "./CollectionNode";
import { DAGGraphBoard } from "components/DAGGraph/board";
import { CollectionNodeMaker } from "./index";
import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants";
const Demo: FC<{
height: number;
width: number;
totalChildNodes: number;
onAddNode: () => void;
enabledChild: boolean;
}> = (args) => {
return (
<Page>
<DAGGraphBoard
closeModal={() => {}}
state={{
ready: true,
data: {
elements: {
edges: [],
nodes: ["trogger", "sequence", "condition"].map(
(collectionType: any, i: number) =>
CollectionNodeMaker.makeElement(
{
id: collectionType,
position: {
x: 50 + args.width * (i % 3 === 0 ? 1 : 0),
y: 50 + args.height * ((i + 1) % 2 === 0 ? 1 : 0),
},
},
DEFAULT_DIMS,
{
hasInput: false,
onAddNode: args.onAddNode,
width: args.width,
height: args.height,
nodes: [
{
enabled: args.enabledChild,
label: collectionType,
},
...Array.from(Array(args.totalChildNodes)).map(
(_, i) => ({
enabled: true,
label: `Dummy Node #${i + 1}`,
})
),
],
collectionType,
}
)
),
},
},
}}
/>
</Page>
);
};
export default {
title: "DAGGraph/Nodes/Collection",
component: Demo,
parameters: { actions: { argTypesRegex: "^on.*" } },
args: {
...DEFAULT_DIMS.condition,
totalChildNodes: 1,
enabledChild: true,
},
} as ComponentMeta<typeof Demo>;
export const Colors: ComponentStory<typeof Demo> = (args) => <Demo {...args} />;
export const Empty: ComponentStory<typeof Demo> = (args) => {
return (
<Page>
<DAGGraphBoard
closeModal={() => {}}
state={{
ready: true,
data: {
elements: {
edges: [],
nodes: [
CollectionNodeMaker.makeElement(
{
id: "only-one",
position: {
x: 50,
y: 50,
},
},
DEFAULT_DIMS,
{
hasInput: false,
onAddNode: args.onAddNode,
width: args.width,
height: args.height,
nodes: [
...Array.from(Array(args.totalChildNodes)).map(
(_, i) => ({
enabled: true,
label: `Dummy Node #${i + 1}`,
})
),
],
collectionType: "condition",
}
),
],
},
},
}}
/>
</Page>
);
};
Empty.args = {
...Empty.args,
totalChildNodes: 0,
};
@@ -1,12 +1,18 @@
import { NodeManager } from "components/DAGGraph/nodes/NodeManager";
import { CollectionNode } from "./CollectionNode";
import { CollectionNodeDataProps } from "./types";
import { SequenceNodeColor } from "../SequenceNode/types";
export const CollectionNodeMaker = NodeManager.register(
"CollectionNode",
CollectionNode,
(pre: CollectionNodeDataProps, { node, flipped }) => ({
...pre,
color: (pre.collectionType === "condition"
? "blue"
: pre.collectionType === "sequence"
? "green"
: "red") as SequenceNodeColor,
sequenceNode: node,
flipped,
})
@@ -1,6 +1,7 @@
import { Size } from "components/DAGGraph/elements/types";
import { ReactNode } from "react";
import { SequenceNodeActions, SequenceNodeColor } from "../SequenceNode/types";
import { AutomationActionData } from "types/automations";
export type CollectionNodeChildProps = Partial<SequenceNodeActions> & {
enabled: boolean;
@@ -9,11 +10,12 @@ export type CollectionNodeChildProps = Partial<SequenceNodeActions> & {
export type CollectionNodeDataProps = Size & {
nodes: CollectionNodeChildProps[];
onAddNode: () => void;
color: SequenceNodeColor;
hasInput?: boolean;
collectionType: keyof AutomationActionData;
};
export type CollectionNodeProps = CollectionNodeDataProps & {
sequenceNode: Size;
flipped: boolean;
color: SequenceNodeColor;
};