dynamic svg board

This commit is contained in:
ari committed 2021-11-30 11:55:48 -05:00
1 parent e6ef41623a
commit f56c8db9a6
5 files changed
+260 -25

No files matched your search

+7 -7
View File
@@ -7,10 +7,11 @@ import { useBoardStyles } from "./styles";
export const SVGBoard: FC<{
graphHeight?: number,
children?: JSX.Element;
minGraphWidth?: number,
}> = ({
children,
graphHeight = 300,
minGraphWidth = 300,
}) => {
// state
@@ -19,7 +20,10 @@ export const SVGBoard: FC<{
const [[offX, offY], setOffset] = useState<Point>([0, 0])
// alias
const graphWidth = Math.round(graphHeight * ratioWbh);
const graphWidth = Math.max(
Math.round(graphHeight * ratioWbh),
minGraphWidth
);
// render
return <div className={classes.root} style={{
@@ -28,7 +32,7 @@ export const SVGBoard: FC<{
<div className={classes.dag}>
<svg
className={classes.svg}
viewBox={[offX, offY, graphWidth + offX, graphHeight + offY].join(" ")}
viewBox={[offX, offY, graphWidth, graphHeight].join(" ")}
xmlns="http://www.w3.org/2000/svg"
>
<defs>
@@ -41,9 +45,5 @@ export const SVGBoard: FC<{
{children}
</svg>
</div>
<div className="board-btn top" onClick={() => setOffset([offX, offY - 10])}></div>
<div className="board-btn left" onClick={() => setOffset([offX - 10, offY])}></div>
<div className="board-btn right" onClick={() => setOffset([offX + 10, offY])}></div>
<div className="board-btn bottom" onClick={() => setOffset([offX, offY + 10])}></div>
</div>
}
@@ -0,0 +1,103 @@
import React from "react";
import { ComponentMeta, ComponentStory } from '@storybook/react';
import { DAGBoard, DAGElement, DAGNodeElm } from "./DAGBoard";
import { NODE_HEIGHT, NODE_WIDTH } from './constants';
export default {
title: 'DAGSvgs/DAGBoard',
component: DAGBoard,
parameters: { actions: { argTypesRegex: '^on.*' } },
args: {
settings: {
nodeHeight: NODE_HEIGHT,
nodeWidth: NODE_WIDTH,
addHeight: 10,
addWidth: 10,
distanceFactor: 1.5,
edgeChildColor: 'blue',
edgeNextColor: 'white',
},
}
} as ComponentMeta<typeof DAGBoard>;
const nodeFromPoint = ([x, y, add = true]: any): DAGNodeElm => ({
type: 'node',
key: `${x},${y}`,
loc: [x, y],
onRemove() { },
onAdd: add ? () => { } : undefined,
node: {
$smType: 'action',
alias: `${x},${y}`,
action: 'template',
action_data: {
value_template: ""
}
}
})
export const Basic: ComponentStory<typeof DAGBoard> = args => <DAGBoard {...args} />
Basic.args = {
...Basic.args,
elements: [
[0, 0, false],
[1, 0, false],
[2, 1, false],
[4, 1],
[3, 2],
[2, 3],
[5, 0, false],
[6, 0],
[0, 6],
].map<DAGElement>(nodeFromPoint).concat([
{
type: 'edge',
key: 'e1',
p1: [0, 0],
p2: [1, 0],
direction: '1->2'
},
{
type: 'edge',
key: 'e2',
p1: [2, 1],
p2: [4, 1],
direction: '1->2',
},
{
type: 'edge',
key: 'e3',
p1: [1, 0],
p2: [2, 1],
direction: '1->2',
toChild: true,
},
{
type: 'edge',
key: 'e4',
p1: [2, 1],
p2: [3, 2],
direction: '1->2',
toChild: true,
},
{
type: 'edge',
key: 'e5',
p1: [1, 0],
p2: [2, 3],
direction: '1->2',
toChild: true,
},
{
type: 'edge',
key: 'e6',
p1: [1, 0],
p2: [5, 0],
direction: '1->2'
},
])
}
+130
View File
@@ -0,0 +1,130 @@
import { FC } from "react";
import { SVGBoard } from "./Board";
import { AutomationNode } from '../../types/automations/index';
import { Point, EdgeDirection, BBox } from 'types/graphs';
import { DAGNode } from "./DAGNode";
import { DAGEdge } from "./DAGEdge";
import { AddButton } from "./AddButton";
import { IteratorWrap } from "utils/iter";
export interface DAGNodeElm {
type: 'node';
key: string;
loc: Point;
node: AutomationNode;
onRemove: () => void;
onAdd?: () => void;
}
export interface DAGEdgeElm {
type: 'edge';
key: string;
p1: Point;
p2: Point;
direction: EdgeDirection;
toChild?: boolean;
}
export type DAGElement = DAGEdgeElm | DAGNodeElm;
export interface DAGBoardSettings {
nodeHeight: number;
nodeWidth: number;
addHeight: number;
addWidth: number;
distanceFactor: number;
edgeChildColor: string;
edgeNextColor: string;
}
export interface Props {
elements: DAGElement[];
settings: DAGBoardSettings;
}
export function* mapDataToElements({
elements,
settings: st,
}: Props) {
let bbox: BBox = [[0, 0], [0, 0]];
const updateBBox = (p: Point) => {
bbox = [
[Math.min(bbox[0][0], p[0]), Math.min(bbox[0][1], p[1])],
[Math.max(bbox[1][0], p[0] + st.nodeWidth), Math.max(bbox[1][1], p[1] + st.nodeHeight)],
]
return p;
}
const offsetPoint = (
[x, y]: Point,
[ox, oy]: Point = [0, 0]
): Point => updateBBox([x + ox, y + oy]);
const mapWithNode = (
[x, y]: Point,
[ox, oy]: Point = [0, 0],
): Point => updateBBox([
x * st.nodeWidth * st.distanceFactor + ox,
y * st.nodeHeight * st.distanceFactor + oy,
])
for (let elm of elements) {
switch (elm.type) {
case 'node':
const nodeLoc = mapWithNode(elm.loc);
const addLoc = mapWithNode(elm.loc, [
st.nodeWidth * st.distanceFactor,
st.addHeight + st.addHeight / 2
])
yield <DAGNode
key={elm.key}
height={st.nodeHeight}
width={st.nodeWidth}
loc={nodeLoc}
text={elm.node?.alias ?? elm.node.$smType}
color={elm.node.$smType === 'action' ? 'green' : 'blue'}
onXClick={elm.onRemove}
/>
if (elm.onAdd) {
yield <DAGEdge
key={`${elm.key}-edge`}
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`}
loc={addLoc}
height={st.addHeight}
width={st.addWidth}
onClick={elm.onAdd}
/>
}
break
case 'edge':
yield <DAGEdge
key={elm.key}
p1={mapWithNode(elm.p1, [st.nodeWidth, st.nodeHeight / 2])}
p2={mapWithNode(elm.p2, [0, st.nodeHeight / 2])}
direction={elm.direction}
color={elm.toChild ? st.edgeChildColor : st.edgeNextColor}
/>
break
default:
break
}
}
return bbox;
}
export const DAGBoard: FC<{
elements: DAGElement[];
settings: DAGBoardSettings;
}> = props => {
const it = new IteratorWrap(mapDataToElements(props));
const el = it.toArray();
const [[x0, y0], [x1, y1]] = it.returnValue as BBox;
return <SVGBoard graphHeight={y1 - y0} minGraphWidth={x1 - x0}>
{el}
</SVGBoard>
}
+16 -16
View File
@@ -1,7 +1,7 @@
import Color from "chroma-js";
import { PencilIcon } from "components/Icons"
import { useNodeStyles } from "./styles"
import { Edge, Node, Point } from "types/graphs";
import { EdgeDirection, Node, Point } from "types/graphs";
import { DAGEdge } from "./DAGEdge";
@@ -15,7 +15,7 @@ export interface NodeProp extends Node {
edge?: {
color: string;
loc: Point;
direction: Edge['direction'];
direction: EdgeDirection;
}
}
export const DAGNode = ({
@@ -27,7 +27,7 @@ export const DAGNode = ({
onOpenClick = () => { },
color,
edge,
debug=false,
debug = false,
}: NodeProp) => {
const { classes, theme } = useNodeStyles({
color,
@@ -56,19 +56,19 @@ export const DAGNode = ({
</foreignObject>
{
edge && (edge.direction === '1->2' ?
<DAGEdge
p1={[x + width, y + height/ 2]}
p2={edge.loc}
direction={edge.direction}
color={edge.color}
debug={debug}
/> :
<DAGEdge
p1={[x, y + height/ 2]}
p2={edge.loc}
direction={edge.direction}
color={edge.color}
debug={debug}
<DAGEdge
p1={[x + width, y + height / 2]}
p2={edge.loc}
direction={edge.direction}
color={edge.color}
debug={debug}
/> :
<DAGEdge
p1={[x, y + height / 2]}
p2={edge.loc}
direction={edge.direction}
color={edge.color}
debug={debug}
/>)
}
</>
+4 -2
View File
@@ -18,10 +18,12 @@ export const useBoardStyles = createAppUseStyles(theme => ({
borderStyle: 'solid',
borderColor: theme.secondaryAccent,
width: '100%',
height: '100%',
overflow: 'scroll'
},
svg: {
height: '99%',
width: '99%',
minHeight: '800px',
minWidth: '800px',
padding: 0,
margin: 0,
},