much better placement
This commit is contained in:
1 parent
8e02468eab
commit
aae609c248
6 files changed
+264
-215
No files matched your search
@@ -23,6 +23,7 @@ export interface DAGEdgeElm {
|
||||
p2: Point;
|
||||
direction: EdgeDirection;
|
||||
toChild?: boolean;
|
||||
onAdd?: () => void;
|
||||
}
|
||||
export type DAGElement = DAGEdgeElm | DAGNodeElm;
|
||||
export interface DAGBoardSettings extends DAGBoardElmDims {
|
||||
@@ -69,7 +70,6 @@ export function* mapDataToElements({
|
||||
y * st.nodeHeight * st.distanceFactor + oy,
|
||||
])
|
||||
for (let elm of elements) {
|
||||
console.log({ elm })
|
||||
switch (elm.type) {
|
||||
case 'node':
|
||||
const nodeLoc = mapWithNode(elm.loc);
|
||||
@@ -78,7 +78,7 @@ export function* mapDataToElements({
|
||||
st.addHeight + st.addHeight / 2
|
||||
])
|
||||
yield <DAGNode
|
||||
key={elm.key}
|
||||
key={`(${elm.loc})${elm.key}-*`}
|
||||
height={st.nodeHeight}
|
||||
width={st.nodeWidth}
|
||||
loc={nodeLoc}
|
||||
@@ -88,14 +88,14 @@ export function* mapDataToElements({
|
||||
/>
|
||||
if (elm.onAdd) {
|
||||
yield <DAGEdge
|
||||
key={`${elm.key}-edge`}
|
||||
key={`${elm.key}->+`}
|
||||
p1={offsetPoint(nodeLoc, [st.nodeWidth, st.nodeHeight / 2])}
|
||||
p2={offsetPoint(addLoc, [0, st.addHeight / 2])}
|
||||
direction="1->2"
|
||||
color={st.edgeNextColor}
|
||||
/>
|
||||
yield <AddButton
|
||||
key={`${elm.key}-add`}
|
||||
key={`${elm.key}-+`}
|
||||
loc={addLoc}
|
||||
height={st.addHeight}
|
||||
width={st.addWidth}
|
||||
@@ -104,13 +104,23 @@ export function* mapDataToElements({
|
||||
}
|
||||
break
|
||||
case 'edge':
|
||||
const p2 = mapWithNode(elm.p2, [0, st.nodeHeight / 2]);
|
||||
yield <DAGEdge
|
||||
key={elm.key}
|
||||
key={`${elm.key}->`}
|
||||
p1={mapWithNode(elm.p1, [st.nodeWidth, st.nodeHeight / 2])}
|
||||
p2={mapWithNode(elm.p2, [0, st.nodeHeight / 2])}
|
||||
p2={p2}
|
||||
direction={elm.direction}
|
||||
color={elm.toChild ? st.edgeChildColor : st.edgeNextColor}
|
||||
/>
|
||||
if (elm.onAdd) {
|
||||
yield <AddButton
|
||||
key={`${elm.key}-+`}
|
||||
loc={offsetPoint(p2, [0, -st.addHeight / 2])}
|
||||
height={st.addHeight}
|
||||
width={st.addWidth}
|
||||
onClick={elm.onAdd}
|
||||
/>
|
||||
}
|
||||
break
|
||||
default:
|
||||
break
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import React from "react";
|
||||
import { ComponentMeta, ComponentStory } from '@storybook/react';
|
||||
import { SequenceNodes } from './index';
|
||||
import { SVGBoard } from '../DAGSvgs/Board';
|
||||
import { NODE_HEIGHT, NODE_WIDTH } from '../DAGSvgs/constants';
|
||||
import { NODE_HEIGHT, NODE_WIDTH, DISTANCE_FACTOR, ADD_HEIGHT, ADD_WIDTH } from '../DAGSvgs/constants';
|
||||
import { useState } from 'react';
|
||||
|
||||
|
||||
@@ -11,22 +10,25 @@ export default {
|
||||
component: SequenceNodes,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {
|
||||
nodeHeight: NODE_HEIGHT,
|
||||
nodeWidth: NODE_WIDTH,
|
||||
distanceFactor: 1.5,
|
||||
startPoint: [50, 50]
|
||||
zoomLevel: 0.5,
|
||||
startPoint: [0.5, 0.5],
|
||||
dims: {
|
||||
nodeHeight: NODE_HEIGHT,
|
||||
nodeWidth: NODE_WIDTH,
|
||||
addHeight: ADD_HEIGHT,
|
||||
addWidth: ADD_WIDTH,
|
||||
distanceFactor: DISTANCE_FACTOR,
|
||||
}
|
||||
}
|
||||
} as ComponentMeta<typeof SequenceNodes>
|
||||
|
||||
const Template: ComponentStory<typeof SequenceNodes> = args => {
|
||||
const [state, setState] = useState(args.sequence)
|
||||
return <SVGBoard>
|
||||
<SequenceNodes
|
||||
{...args}
|
||||
sequence={state}
|
||||
onChange={setState}
|
||||
/>
|
||||
</SVGBoard>
|
||||
return <SequenceNodes
|
||||
{...args}
|
||||
sequence={state}
|
||||
onChange={setState}
|
||||
/>
|
||||
}
|
||||
|
||||
export const Simple = Template.bind({})
|
||||
@@ -77,7 +79,6 @@ Simple.args = {
|
||||
export const Multinode = Template.bind({})
|
||||
Multinode.args = {
|
||||
...Multinode.args,
|
||||
startPoint: [0, 0],
|
||||
sequence: [
|
||||
{
|
||||
$smType: "condition",
|
||||
@@ -179,3 +180,67 @@ Multinode.args = {
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export const DeepNested = Template.bind({})
|
||||
DeepNested.args = {
|
||||
...DeepNested.args,
|
||||
sequence: [
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [{
|
||||
"sequence": [{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [],
|
||||
"default": []
|
||||
}
|
||||
}],
|
||||
"conditions": [],
|
||||
}],
|
||||
"default": [
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [
|
||||
{
|
||||
"sequence": [{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [],
|
||||
"default": []
|
||||
}
|
||||
}],
|
||||
"conditions": [],
|
||||
}
|
||||
],
|
||||
"default": [
|
||||
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [],
|
||||
"default": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"$smType": "action",
|
||||
"action": "choose",
|
||||
"action_data": {
|
||||
"choose": [],
|
||||
"default": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,17 +1,31 @@
|
||||
import { DAGBoard } from "components/DAGSvgs/DAGBoard";
|
||||
import { FC } from "react";
|
||||
import { useTheme } from "styles/theme";
|
||||
import { computeNodesEdges } from './location';
|
||||
import { SequenceNodeSettings } from './types';
|
||||
import { SequenceNodeProps } from './types';
|
||||
import Color from 'chroma-js';
|
||||
import { computeNodesEdgesPos } from './positions';
|
||||
|
||||
|
||||
export const SequenceNodes: FC<SequenceNodeSettings> = (props) => {
|
||||
export const SequenceNodes: FC<SequenceNodeProps & { zoomLevel: number }> = (props) => {
|
||||
const theme = useTheme();
|
||||
|
||||
return <>
|
||||
{Array.from(computeNodesEdges({
|
||||
...props,
|
||||
keyPrefix: "",
|
||||
edgeColor: theme.secondaryAccent,
|
||||
}))}
|
||||
</>
|
||||
// return <>
|
||||
// {Array.from(computeNodesEdges({
|
||||
// ...props,
|
||||
// keyPrefix: "",
|
||||
// edgeColor: theme.secondaryAccent,
|
||||
// }))}
|
||||
// </>
|
||||
return <DAGBoard
|
||||
zoomLevel={props.zoomLevel}
|
||||
elements={computeNodesEdgesPos(
|
||||
props.startPoint,
|
||||
props.sequence,
|
||||
props.onChange
|
||||
)}
|
||||
settings={{
|
||||
...props.dims,
|
||||
edgeNextColor: theme.secondaryAccent,
|
||||
edgeChildColor: Color(theme.secondaryAccent).set('rgb.b', 200).hex(),
|
||||
}}
|
||||
/>
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
import { DAGEdge } from "components/DAGSvgs/DAGEdge";
|
||||
import { Point } from "types/graphs";
|
||||
import { DAGNode } from '../DAGSvgs/DAGNode';
|
||||
import { BaseSequenceNodeSettings, SequenceNodeSettings } from './types';
|
||||
import { AddButton } from '../DAGSvgs/AddButton';
|
||||
import { makeUpdater, Updater } from "./updater";
|
||||
import { ChooseAction } from "types/automations/actions";
|
||||
import { AutomationSequenceNode } from '../../types/automations/index';
|
||||
|
||||
|
||||
export function* computeNodesEdges(
|
||||
{
|
||||
sequence,
|
||||
keyPrefix = "",
|
||||
onChange = () => { },
|
||||
...st
|
||||
}: SequenceNodeSettings,
|
||||
): 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] : [
|
||||
lastStartPoint[0] + st.nodeWidth * st.distanceFactor,
|
||||
lastStartPoint[1]
|
||||
];
|
||||
let nextNodeLoc: Point = [nodeLoc[0] + st.distanceFactor * st.nodeWidth, nodeLoc[1] + st.nodeHeight / 2];
|
||||
yield <DAGNode
|
||||
key={`node${keyPrefix}.${i}`}
|
||||
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] + st.nodeWidth, lastNodeLoc[1] + st.nodeHeight / 2],
|
||||
color: st.edgeColor,
|
||||
}}
|
||||
onXClick={() => rootUpdater.removeNode(i)}
|
||||
/>
|
||||
if ((node.$smType === 'action') && (node.action === 'choose')) {
|
||||
const it = (new computeChooseNodesEdges(
|
||||
node,
|
||||
nodeLoc,
|
||||
lastStartPoint,
|
||||
nextNodeLoc,
|
||||
j => rootUpdater.makeChildUpdaterForChooseAction(i, j),
|
||||
{ ...st, keyPrefix: `${keyPrefix}.${i}` }
|
||||
)).iter();
|
||||
let next = it.next();
|
||||
while (!next.done) {
|
||||
yield next.value
|
||||
next = it.next();
|
||||
};
|
||||
nextNodeLoc = next.value.nextNodeLoc
|
||||
lastStartPoint = [
|
||||
next.value.lastStartPoint[0],
|
||||
lastStartPoint[1],
|
||||
]
|
||||
}
|
||||
if (i === sequence.length - 1) {
|
||||
yield <DAGEdge
|
||||
key={`edge${keyPrefix}.${i}`}
|
||||
p1={[nodeLoc[0] + st.nodeWidth, nodeLoc[1] + st.nodeHeight / 2]}
|
||||
p2={nextNodeLoc}
|
||||
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
|
||||
}
|
||||
|
||||
class computeChooseNodesEdges {
|
||||
constructor(
|
||||
private node: ChooseAction,
|
||||
private nodeLoc: Point,
|
||||
private lastStartPoint: Point,
|
||||
private nextNodeLoc: Point,
|
||||
private makeChildUpder: (j: number | null) => Updater,
|
||||
private st: BaseSequenceNodeSettings,
|
||||
) { }
|
||||
|
||||
private *genNodesEdges(
|
||||
seq: AutomationSequenceNode[],
|
||||
subStartLoc: Point,
|
||||
updater: Updater,
|
||||
keyPrefix: string
|
||||
) {
|
||||
const {
|
||||
st, nodeLoc,
|
||||
} = this;
|
||||
let it = computeNodesEdges({
|
||||
...st,
|
||||
onChange: updater.onChange,
|
||||
sequence: seq,
|
||||
startPoint: subStartLoc,
|
||||
keyPrefix: keyPrefix
|
||||
});
|
||||
let next = it.next();
|
||||
while (!next.done) {
|
||||
yield next.value
|
||||
next = it.next()
|
||||
}
|
||||
yield <DAGEdge
|
||||
key={`edge${keyPrefix}`}
|
||||
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) {
|
||||
this.nextNodeLoc[0] = next.value[0] + st.nodeWidth * st.distanceFactor
|
||||
this.lastStartPoint = [Math.max(next.value[0], this.lastStartPoint[0]), Math.max(this.lastStartPoint[1], next.value[1])]
|
||||
} else {
|
||||
yield <AddButton
|
||||
key={`add${keyPrefix}`}
|
||||
loc={[subStartLoc[0], subStartLoc[1] + st.nodeHeight / 2 - 4.5]}
|
||||
height={10}
|
||||
width={10}
|
||||
onClick={updater.addNode}
|
||||
/>
|
||||
}
|
||||
}
|
||||
|
||||
*iter() {
|
||||
const { st, node, makeChildUpder, nodeLoc } = this;
|
||||
for (let j = 0; j < node.action_data.choose.length; j++) {
|
||||
const it = this.genNodesEdges(
|
||||
node.action_data.choose[j].sequence,
|
||||
[
|
||||
nodeLoc[0] + st.nodeWidth * st.distanceFactor,
|
||||
Math.max(nodeLoc[1] + st.nodeHeight * (j + 1), this.lastStartPoint[1])
|
||||
],
|
||||
makeChildUpder(j),
|
||||
`${st.keyPrefix}.${j}`,
|
||||
);
|
||||
let next = it.next();
|
||||
while (!next.done) {
|
||||
yield next.value
|
||||
next = it.next();
|
||||
}
|
||||
}
|
||||
const it = this.genNodesEdges(
|
||||
node.action_data.default,
|
||||
[
|
||||
nodeLoc[0] + st.nodeWidth * st.distanceFactor,
|
||||
nodeLoc[1] + st.nodeHeight * (node.action_data.choose.length + 1)
|
||||
],
|
||||
makeChildUpder(null),
|
||||
`${st.keyPrefix}.default`,
|
||||
);
|
||||
let next = it.next();
|
||||
while (!next.done) {
|
||||
yield next.value
|
||||
next = it.next();
|
||||
}
|
||||
return {
|
||||
lastStartPoint: this.lastStartPoint,
|
||||
nextNodeLoc: this.nextNodeLoc
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import { DAGElement } from "components/DAGSvgs/DAGBoard";
|
||||
import { AutomationSequenceNode } from "types/automations";
|
||||
import { Point } from "types/graphs";
|
||||
import { makeUpdater, Updater } from './updater';
|
||||
|
||||
const offsetPoint = (
|
||||
[x, y]: Point,
|
||||
[ox, oy]: Point = [0, 0]
|
||||
): Point => [x + ox, y + oy];
|
||||
;
|
||||
|
||||
function* inner(
|
||||
nodeLoc: Point,
|
||||
innerStartLoc: Point,
|
||||
updater: Updater,
|
||||
keyPrefix: string,
|
||||
sequence: AutomationSequenceNode[],
|
||||
): Generator<DAGElement> {
|
||||
yield {
|
||||
type: 'edge',
|
||||
p1: nodeLoc,
|
||||
p2: innerStartLoc,
|
||||
direction: '1->2',
|
||||
key: `${keyPrefix}-${nodeLoc}:${innerStartLoc}`,
|
||||
onAdd: sequence.length === 0 ? updater.addNode : undefined,
|
||||
}
|
||||
for (let childNode of computeNodesEdgesPos(
|
||||
innerStartLoc,
|
||||
sequence,
|
||||
updater.onChange,
|
||||
keyPrefix,
|
||||
)) {
|
||||
yield childNode
|
||||
}
|
||||
}
|
||||
|
||||
const makeOffsetMaintainer = (startPoint: Point) => {
|
||||
let offset: Point = [0, 0];
|
||||
let i = 0;
|
||||
return {
|
||||
setI(newI: number) {
|
||||
i = newI;
|
||||
},
|
||||
resetY() {
|
||||
offset[1] = 0;
|
||||
},
|
||||
update(p: Point, innerStartLoc: Point) {
|
||||
console.log('updating', JSON.stringify({i, offset, p, innerStartLoc}))
|
||||
offset[0] = Math.max(offset[0], p[0] - i - 0.5 - startPoint[0])
|
||||
offset[1] = Math.max(offset[1], p[1] - innerStartLoc[1] + 1)
|
||||
console.log('|->', JSON.stringify({offset}))
|
||||
},
|
||||
get x() {
|
||||
return offset[0]
|
||||
},
|
||||
get y() {
|
||||
return offset[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
export function* computeNodesEdgesPos(
|
||||
startPoint: Point,
|
||||
sequence: AutomationSequenceNode[],
|
||||
onChange: (s: AutomationSequenceNode[]) => void,
|
||||
keyPrefix: string = '$',
|
||||
): Generator<DAGElement> {
|
||||
const rootUpdater = makeUpdater(sequence, onChange);
|
||||
const offset = makeOffsetMaintainer(startPoint)
|
||||
|
||||
for (let i = 0; i < sequence.length; i++) {
|
||||
offset.setI(i)
|
||||
const node = sequence[i];
|
||||
const nodeLoc = offsetPoint(startPoint, [i + offset.x, 0]);
|
||||
yield {
|
||||
type: 'node',
|
||||
key: `${keyPrefix}${i}`,
|
||||
loc: nodeLoc,
|
||||
node,
|
||||
onRemove: () => rootUpdater.removeNode(i),
|
||||
onAdd: i === sequence.length - 1 ? rootUpdater.addNode : undefined,
|
||||
}
|
||||
if (i > 0) {
|
||||
yield {
|
||||
type: 'edge',
|
||||
key: `${keyPrefix}(${i-1}->${i})`,
|
||||
p1: offsetPoint(startPoint, [i - 1, 0]),
|
||||
p2: offsetPoint(nodeLoc, [0, 0]),
|
||||
direction: '1->2',
|
||||
}
|
||||
}
|
||||
if (
|
||||
(node.$smType === 'action') && (node.action === 'choose')
|
||||
) {
|
||||
offset.resetY()
|
||||
|
||||
for (let j = 0; j < node.action_data.choose.length; j++) {
|
||||
const innerStartLoc = offsetPoint(nodeLoc, [1, j + 1 + offset.y]);
|
||||
for (let elm of inner(
|
||||
nodeLoc,
|
||||
innerStartLoc ,
|
||||
rootUpdater.makeChildUpdaterForChooseAction(i, j),
|
||||
`${keyPrefix}.${i}.${j}`,
|
||||
node.action_data.choose[j].sequence
|
||||
)) {
|
||||
yield elm
|
||||
if (elm.type === 'node') {
|
||||
offset.update(elm.loc, innerStartLoc)
|
||||
}
|
||||
}
|
||||
}
|
||||
const innerStartLoc = offsetPoint(nodeLoc, [1, node.action_data.choose.length + 1 + offset.y]);
|
||||
for (let elm of inner(
|
||||
nodeLoc,
|
||||
innerStartLoc,
|
||||
rootUpdater.makeChildUpdaterForChooseAction(i, null),
|
||||
`${keyPrefix}${i}-d`,
|
||||
node.action_data.default,
|
||||
)) {
|
||||
yield elm
|
||||
if (elm.type === 'node') {
|
||||
offset.update(elm.loc, innerStartLoc)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,24 @@
|
||||
import { DAGBoardElmDims } from "components/DAGSvgs/DAGBoard";
|
||||
import { AutomationSequenceNode } from "types/automations";
|
||||
import { Point } from "types/graphs";
|
||||
|
||||
export interface SequenceNodeSettings extends BaseSequenceNodeSettings{
|
||||
export interface SequenceNodeProps {
|
||||
startPoint: Point;
|
||||
dims: DAGBoardElmDims;
|
||||
sequence: AutomationSequenceNode[],
|
||||
onChange?: (s: AutomationSequenceNode[]) => void;
|
||||
onChange: (s: AutomationSequenceNode[]) => void;
|
||||
}
|
||||
|
||||
export interface BaseSequenceNodeSettings {
|
||||
startPoint: Point;
|
||||
nodeHeight: number;
|
||||
nodeWidth: number;
|
||||
distanceFactor: number;
|
||||
edgeColor: string;
|
||||
keyPrefix?: string;
|
||||
}
|
||||
// export interface SequenceNodeSettings extends BaseSequenceNodeSettings{
|
||||
// sequence: AutomationSequenceNode[],
|
||||
// onChange?: (s: AutomationSequenceNode[]) => void;
|
||||
// }
|
||||
|
||||
// export interface BaseSequenceNodeSettings {
|
||||
// startPoint: Point;
|
||||
// nodeHeight: number;
|
||||
// nodeWidth: number;
|
||||
// distanceFactor: number;
|
||||
// edgeColor: string;
|
||||
// keyPrefix?: string;
|
||||
// }
|
||||
Reference in new issue
Block a user