This commit is contained in:
ari committed 2021-11-30 16:50:18 -05:00
1 parent f56c8db9a6
commit 60b522922a
5 files changed
+66 -25

No files matched your search

+13 -4
View File
@@ -3,27 +3,36 @@ import { FC, useState } from 'react';
import { Point } from 'types/graphs';
import useWindowSize from "utils/useWindowSize";
import { useBoardStyles } from "./styles";
import { NODE_HEIGHT, NODE_WIDTH } from './constants';
export const SVGBoard: FC<{
graphHeight?: number,
minGraphWidth?: number,
nodeHeight?: number,
nodeWidth?: number,
zoomLevel?: number,
}> = ({
children,
graphHeight = 300,
minGraphWidth = 300,
nodeHeight = NODE_HEIGHT,
nodeWidth = NODE_WIDTH,
zoomLevel = 1,
}) => {
// state
const { ratioWbh } = useWindowSize();
const { classes, theme } = useBoardStyles({});
const [[offX, offY], setOffset] = useState<Point>([0, 0])
// alias
const graphWidth = Math.max(
Math.round(graphHeight * ratioWbh),
minGraphWidth
);
const { classes, theme } = useBoardStyles({
boardHeight: Math.max(nodeHeight, graphHeight) * zoomLevel,
boardWidth: Math.max(5 * nodeWidth, graphWidth) * zoomLevel,
});
// render
return <div className={classes.root} style={{
@@ -32,7 +41,7 @@ export const SVGBoard: FC<{
<div className={classes.dag}>
<svg
className={classes.svg}
viewBox={[offX, offY, graphWidth, graphHeight].join(" ")}
viewBox={[0, 0, graphWidth, graphHeight].join(" ")}
xmlns="http://www.w3.org/2000/svg"
>
<defs>
@@ -1,7 +1,7 @@
import React from "react";
import { ComponentMeta, ComponentStory } from '@storybook/react';
import { DAGBoard, DAGElement, DAGNodeElm } from "./DAGBoard";
import { NODE_HEIGHT, NODE_WIDTH } from './constants';
import { NODE_HEIGHT, NODE_WIDTH, ADD_HEIGHT, ADD_WIDTH, DISTANCE_FACTOR } from './constants';
export default {
@@ -9,12 +9,13 @@ export default {
component: DAGBoard,
parameters: { actions: { argTypesRegex: '^on.*' } },
args: {
zoomLevel: 1,
settings: {
nodeHeight: NODE_HEIGHT,
nodeWidth: NODE_WIDTH,
addHeight: 10,
addWidth: 10,
distanceFactor: 1.5,
addHeight: ADD_HEIGHT,
addWidth: ADD_WIDTH,
distanceFactor: DISTANCE_FACTOR,
edgeChildColor: 'blue',
edgeNextColor: 'white',
},
@@ -38,9 +39,28 @@ const nodeFromPoint = ([x, y, add = true]: any): DAGNodeElm => ({
}
})
export const Basic: ComponentStory<typeof DAGBoard> = args => <DAGBoard {...args} />
Basic.args = {
...Basic.args,
export const Simple: ComponentStory<typeof DAGBoard> = args => <DAGBoard {...args} />
Simple.args = {
...Simple.args,
elements: [
[0, 0, false],
[1, 0, false],
[2, 1, false],
].map<DAGElement>(nodeFromPoint).concat([
{
type: 'edge',
key: 'e1',
p1: [0, 0],
p2: [1, 0],
direction: '1->2'
},
])
}
export const Complex: ComponentStory<typeof DAGBoard> = args => <DAGBoard {...args} />
Complex.args = {
...Complex.args,
elements: [
[0, 0, false],
[1, 0, false],
+15 -9
View File
@@ -25,18 +25,20 @@ export interface DAGEdgeElm {
toChild?: boolean;
}
export type DAGElement = DAGEdgeElm | DAGNodeElm;
export interface DAGBoardSettings {
export interface DAGBoardSettings extends DAGBoardElmDims {
edgeChildColor: string;
edgeNextColor: string;
}
export interface DAGBoardElmDims {
nodeHeight: number;
nodeWidth: number;
addHeight: number;
addWidth: number;
distanceFactor: number;
edgeChildColor: string;
edgeNextColor: string;
}
export interface Props {
elements: DAGElement[];
elements: Generator<DAGElement>;
settings: DAGBoardSettings;
}
@@ -67,6 +69,7 @@ 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);
@@ -117,14 +120,17 @@ export function* mapDataToElements({
}
export const DAGBoard: FC<{
elements: DAGElement[];
settings: DAGBoardSettings;
}> = props => {
export const DAGBoard: FC<Props & { zoomLevel: number }> = 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}>
return <SVGBoard
graphHeight={y1 - y0}
minGraphWidth={x1 - x0}
nodeHeight={props.settings.nodeHeight}
nodeWidth={props.settings.nodeWidth}
zoomLevel={props.zoomLevel}
>
{el}
</SVGBoard>
}
@@ -3,3 +3,6 @@
export const NODE_WIDTH = 100 as const;
export const NODE_HEIGHT = 40 as const;
export const ADD_HEIGHT = 10 as const;
export const ADD_WIDTH = 10 as const;
export const DISTANCE_FACTOR = 1.5 as const;
+8 -5
View File
@@ -4,7 +4,10 @@ import { createAppUseStyles } from "styles/theme";
import { Node } from "types/graphs";
export const useBoardStyles = createAppUseStyles(theme => ({
export const useBoardStyles = createAppUseStyles<{
boardHeight: number,
boardWidth: number,
}>(theme => ({
root: {
height: '99%',
width: '99%',
@@ -21,12 +24,12 @@ export const useBoardStyles = createAppUseStyles(theme => ({
height: '100%',
overflow: 'scroll'
},
svg: {
minHeight: '800px',
minWidth: '800px',
svg: ({boardHeight, boardWidth}) => ({
minHeight: `${boardHeight}px`,
minWidth: `${boardWidth}px`,
padding: 0,
margin: 0,
},
}),
circle: {
fill: Color(theme.secondary).set('rgb.b', 125).hex(),
},