ability to add new branches
This commit is contained in:
1 parent
55239e6517
commit
24afb602cb
4 files changed
+68
-14
No files matched your search
@@ -12,7 +12,7 @@ import { DAGCircle } from "./DAGCircle";
|
||||
export interface DAGCircleElm {
|
||||
type: 'circle';
|
||||
loc: Point;
|
||||
onClick?: () => void;
|
||||
onClick?: ['add', () => void] | ['edit', () => void];
|
||||
}
|
||||
export interface DAGNodeElm {
|
||||
type: 'node';
|
||||
@@ -135,11 +135,16 @@ export function* mapDataToElements({
|
||||
break
|
||||
case 'circle':
|
||||
const loc = mapWithNode(elm.loc, [0, st.nodeHeight / 2 - st.circleSize / 2]);
|
||||
const clickProps = elm.onClick ? (
|
||||
elm.onClick[0] === 'add' ?
|
||||
{ onAdd: elm.onClick[1] } :
|
||||
{ onEdit: elm.onClick[1] }
|
||||
) : {}
|
||||
yield <DAGCircle
|
||||
key={`(${elm.loc})-o`}
|
||||
loc={loc}
|
||||
size={st.circleSize}
|
||||
onClick={elm.onClick}
|
||||
{...clickProps}
|
||||
/>
|
||||
break
|
||||
default:
|
||||
|
||||
@@ -17,11 +17,18 @@ export default {
|
||||
export const Basic: ComponentStory<typeof DAGCircle> = args => {
|
||||
return <SVGBoard>
|
||||
<DAGCircle
|
||||
{...args}
|
||||
size={args.size}
|
||||
loc={args.loc}
|
||||
/>
|
||||
<DAGCircle
|
||||
size={args.size}
|
||||
loc={[args.loc[0] + args.size, args.loc[1]]}
|
||||
onAdd={args.onAdd}
|
||||
/>
|
||||
<DAGCircle
|
||||
size={args.size}
|
||||
loc={[args.loc[0] + 2 * args.size, args.loc[1]]}
|
||||
onEdit={args.onEdit}
|
||||
/>
|
||||
</SVGBoard>
|
||||
}
|
||||
|
||||
@@ -1,26 +1,39 @@
|
||||
// import "./DAGCircle.css";
|
||||
import { Point } from "types/graphs";
|
||||
import { PencilIcon } from '../Icons/index';
|
||||
import { AddIcon, PencilIcon } from '../Icons/index';
|
||||
import { useCircleStyles } from './styles';
|
||||
|
||||
|
||||
|
||||
export interface AddProps {
|
||||
onClick?: () => void;
|
||||
onEdit?: () => void;
|
||||
onAdd?: () => void;
|
||||
loc: Point,
|
||||
size: number,
|
||||
}
|
||||
export const DAGCircle = ({
|
||||
loc: [x, y],
|
||||
size,
|
||||
onClick,
|
||||
onEdit,
|
||||
onAdd,
|
||||
}: AddProps) => {
|
||||
const { classes } = useCircleStyles({ size: size * 0.9 });
|
||||
console.log({ onClick })
|
||||
console.log({ onEdit })
|
||||
return <>
|
||||
<foreignObject x={x} y={y} width={size} height={size}>
|
||||
<div className={classes.root}>
|
||||
{onClick ? <PencilIcon size={70} units="%" className={classes.icon} onClick={onClick} /> : "else"}
|
||||
{onEdit ?
|
||||
<PencilIcon
|
||||
size={70} units="%"
|
||||
className={classes.icon}
|
||||
onClick={onEdit}
|
||||
/> : onAdd ?
|
||||
<AddIcon
|
||||
size={70} units="%"
|
||||
className={classes.icon}
|
||||
onClick={onAdd}
|
||||
/>
|
||||
: "else"}
|
||||
</div>
|
||||
</foreignObject>
|
||||
</>
|
||||
|
||||
@@ -32,7 +32,7 @@ const makeOffsetMaintainer = (startPoint: Point) => {
|
||||
}
|
||||
}
|
||||
|
||||
function* inner(
|
||||
function* dealWithNested(
|
||||
nodeLoc: Point,
|
||||
innerStartLoc: Point,
|
||||
updater: Updater,
|
||||
@@ -49,7 +49,7 @@ function* inner(
|
||||
yield {
|
||||
type: 'circle',
|
||||
loc: innerStartLoc,
|
||||
onClick: onEdit
|
||||
onClick: ['edit', onEdit],
|
||||
}
|
||||
yield {
|
||||
type: 'edge',
|
||||
@@ -72,12 +72,14 @@ function* dealWithChoose(
|
||||
node: ChooseAction,
|
||||
nodeLoc: Point,
|
||||
makeUpdater: (j: number | null) => Updater,
|
||||
onAdd: () => void,
|
||||
offset: ReturnType<typeof makeOffsetMaintainer>,
|
||||
) {
|
||||
): Generator<DAGElement> {
|
||||
offset.resetY()
|
||||
// conditions
|
||||
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(
|
||||
for (let elm of dealWithNested(
|
||||
nodeLoc,
|
||||
innerStartLoc ,
|
||||
makeUpdater(j),
|
||||
@@ -88,13 +90,17 @@ function* dealWithChoose(
|
||||
if (elm.type === 'node') {
|
||||
offset.update(elm.loc, innerStartLoc)
|
||||
}
|
||||
if (elm.type === 'circle') {
|
||||
offset.update(offsetPoint(elm.loc, [0, -1]), innerStartLoc)
|
||||
}
|
||||
}
|
||||
}
|
||||
// default
|
||||
const innerStartLoc = offsetPoint(
|
||||
nodeLoc,
|
||||
[1, node.action_data.choose.length + 1 + offset.y],
|
||||
[1, node.action_data.choose.length + 2 + offset.y],
|
||||
);
|
||||
for (let elm of inner(
|
||||
for (let elm of dealWithNested(
|
||||
nodeLoc,
|
||||
innerStartLoc,
|
||||
makeUpdater(null),
|
||||
@@ -105,6 +111,16 @@ function* dealWithChoose(
|
||||
offset.update(elm.loc, innerStartLoc)
|
||||
}
|
||||
}
|
||||
// add new conditions
|
||||
const newCondLoc = offsetPoint(
|
||||
nodeLoc,
|
||||
[1, node.action_data.choose.length + 1 + offset.y],
|
||||
);
|
||||
yield {
|
||||
type: 'circle',
|
||||
loc: newCondLoc,
|
||||
onClick: ['add', onAdd],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -143,6 +159,19 @@ export function* computeNodesEdgesPos(
|
||||
node,
|
||||
nodeLoc,
|
||||
j => rootUpdater.makeChildUpdaterForChooseAction(i, j),
|
||||
() => rootUpdater.updateNode(i, {
|
||||
...node,
|
||||
action_data: {
|
||||
...node.action_data,
|
||||
choose: [
|
||||
...node.action_data.choose,
|
||||
{
|
||||
conditions: [],
|
||||
sequence: [],
|
||||
}
|
||||
]
|
||||
}
|
||||
}),
|
||||
offset,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user