latest work
This commit is contained in:
1 parent
39757d0045
commit
de49905e62
30 files changed
+735
-617
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import { FC } from "react";
|
||||
import useWindowSize from "utils/useWindowSize";
|
||||
import { useBoardStyles } from "./styles";
|
||||
|
||||
export const SVGBoard: FC<{
|
||||
graphHeight?: number,
|
||||
}> =({
|
||||
children,
|
||||
graphHeight = 300,
|
||||
}) => {
|
||||
|
||||
// state
|
||||
const { ratioWbh } = useWindowSize();
|
||||
const { classes, theme } = useBoardStyles({});
|
||||
|
||||
// alias
|
||||
const graphWidth = Math.round(graphHeight * ratioWbh);
|
||||
|
||||
// render
|
||||
return <div className={classes.root} style={{
|
||||
flexDirection: ratioWbh >= 0.75 ? 'row' : 'column'
|
||||
}}>
|
||||
<div className={classes.dag}>
|
||||
<svg
|
||||
className={classes.svg}
|
||||
viewBox={[0, 0, graphWidth, graphHeight].join(" ")}
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<defs>
|
||||
<marker id="arrow" viewBox="0 0 10 10" refX="10" refY="5"
|
||||
markerWidth="6" markerHeight="6"
|
||||
orient="auto-start-reverse">
|
||||
<path d="M 0 0 L 10 5 L 0 10 z" fill={theme.secondaryAccent} />
|
||||
</marker>
|
||||
</defs>
|
||||
{children}
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { EdgeDirection, Point } from "types/graphs"
|
||||
|
||||
|
||||
|
||||
export interface EdgeProp {
|
||||
p1: Point,
|
||||
p2: Point,
|
||||
direction: EdgeDirection,
|
||||
color: string,
|
||||
debug?: boolean,
|
||||
}
|
||||
export const DAGEdge = ({
|
||||
p1: [x1,y1],
|
||||
p2: [x2,y2],
|
||||
direction,
|
||||
color,
|
||||
debug=false,
|
||||
}: EdgeProp) => {
|
||||
const [mx,my] = [(x1+x2)/2, (y1+y2)/2]
|
||||
let [cx1,cy1] = [(x1+mx)/2, (y1+my)/2]
|
||||
let [cx2, cy2] = [(x2 + mx) / 2, (y2 + my) / 2]
|
||||
const l1 = Math.sqrt((cy1 - y1) ** 2 + (cx1 - x1) ** 2)
|
||||
const l2 = Math.sqrt((cy2 - y2) ** 2 + (cx2 - x2) ** 2)
|
||||
const theta = Math.atan2(y2 -y1, x2-x1)
|
||||
if ((theta < Math.PI / 2) && (theta > -Math.PI / 2)) {
|
||||
cy1 = y1 - 0.5 * l1 * Math.sin(theta)
|
||||
cx1 = x1 + 2 * l1 * Math.cos(theta)
|
||||
cy2 = y2 + 0.5 * l2 * Math.sin(theta)
|
||||
cx2 = x2 - 2 * l2 * Math.cos(theta)
|
||||
} else if (theta === Math.PI) {
|
||||
cy1 *= 1.5
|
||||
cx1 = x1 - 2 * l1 * Math.cos(theta)
|
||||
cy2 *= 0.9
|
||||
} else if ((theta > Math.PI / 2) && (theta < Math.PI)) {
|
||||
cy2 += l2 * Math.sin(theta)
|
||||
cx2 += 10 * l2 * Math.cos(theta)
|
||||
cx1 -= 10 * l1 * Math.cos(theta)
|
||||
}
|
||||
|
||||
return <>
|
||||
{debug && <>
|
||||
<circle cx={cx1} cy={cy1} r="5" fill="yellow" />
|
||||
<line x1={cx1} y1={cy1} x2={x1} y2={y1} stroke="yellow"/>
|
||||
<line x1={cx1} y1={cy1} x2={x2} y2={y2} stroke="yellow"/>
|
||||
<circle cx={cx2} cy={cy2} r="5" fill="green"/>
|
||||
<line x1={cx2} y1={cy2} x2={x1} y2={y1} stroke="green"/>
|
||||
<line x1={cx2} y1={cy2} x2={x2} y2={y2} stroke="green" />
|
||||
<text x={mx} y={my} stroke="white">{theta}</text>
|
||||
</>}
|
||||
<path stroke={color}
|
||||
d={ `M${x1} ${y1} C${cx1} ${cy1}, ${cx2} ${cy2}, ${x2} ${y2}`}
|
||||
fill="none"
|
||||
markerStart={
|
||||
(direction === '1<-2' || direction === '1<->2') ?
|
||||
"url(#arrow)" : ''
|
||||
}
|
||||
markerEnd={
|
||||
(direction === '1->2' || direction === '1<->2') ?
|
||||
"url(#arrow)" : ''
|
||||
}
|
||||
/>
|
||||
</>
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import React from "react";
|
||||
import { ComponentMeta, ComponentStory } from '@storybook/react';
|
||||
import { DAGNode } from "./DAGNode";
|
||||
import { SVGBoard } from './Board';
|
||||
import { NODE_HEIGHT, NODE_WIDTH } from './constants';
|
||||
import { DAGEdge } from "./DAGEdge";
|
||||
|
||||
const GRAPH_HEIGHT = 300;
|
||||
|
||||
export default {
|
||||
title: 'DAGSvgs/DAGNode',
|
||||
component: DAGNode,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
args: {
|
||||
height: NODE_HEIGHT,
|
||||
width: NODE_WIDTH,
|
||||
text: "Test",
|
||||
loc: [NODE_WIDTH * 2,GRAPH_HEIGHT / 2]
|
||||
}
|
||||
} as ComponentMeta<typeof DAGNode>;
|
||||
|
||||
export const Basic: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
graphHeight={GRAPH_HEIGHT}
|
||||
>
|
||||
<DAGNode {...args}/>
|
||||
</SVGBoard>
|
||||
|
||||
export const WithEdge: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
graphHeight={GRAPH_HEIGHT}
|
||||
>
|
||||
<DAGNode {...args} />
|
||||
</SVGBoard>
|
||||
WithEdge.args = {
|
||||
...(WithEdge.args ?? {}),
|
||||
edge: {
|
||||
color: 'white',
|
||||
loc: [ NODE_WIDTH * 4, NODE_HEIGHT * 2 ]
|
||||
}
|
||||
}
|
||||
|
||||
export const WithManyEdge: ComponentStory<typeof DAGNode> = args => <SVGBoard
|
||||
graphHeight={GRAPH_HEIGHT}
|
||||
>
|
||||
<DAGNode {...args} />
|
||||
{[
|
||||
// TODO: make it nicer to view when from behind
|
||||
// [0, 0],
|
||||
// [args.loc[0] - args.width, args.loc[1] + args.height / 2], // behind
|
||||
// [args.loc[0] - 0.5*args.width, args.loc[1] + args.height ], // behind
|
||||
// [args.loc[0], args.loc[1] + args.height ], // behind
|
||||
// [args.loc[0], args.loc[1] + 2 * args.height ], // behind
|
||||
[args.loc[0] + args.width / 2, args.loc[1] + 2 * args.height ], // below
|
||||
[args.loc[0] + args.width, args.loc[1] + args.height * 2], // down
|
||||
[args.loc[0] + args.width, args.loc[1] - args.height * 2], // up
|
||||
[args.loc[0] + 2*args.width, args.loc[1] + args.height * 2], // down right
|
||||
[args.loc[0] + 2*args.width, args.loc[1] + args.height / 2], // forward
|
||||
[args.loc[0] + 2*args.width, args.loc[1] - args.height * 2], // up right
|
||||
].map((p: any) => <DAGEdge
|
||||
p1={[args.loc[0] + args.width, args.loc[1] + args.height / 2]}
|
||||
p2={p}
|
||||
direction="1->2"
|
||||
color="white"
|
||||
// debug
|
||||
/>)}
|
||||
</SVGBoard>
|
||||
@@ -0,0 +1,61 @@
|
||||
import Color from "chroma-js";
|
||||
import { PencilIcon } from "components/Icons"
|
||||
import { useNodeStyles } from "./styles"
|
||||
import { Node, Point } from "types/graphs";
|
||||
import { DAGEdge } from "./DAGEdge";
|
||||
|
||||
|
||||
|
||||
export interface NodeProp extends Node {
|
||||
height: number;
|
||||
width: number;
|
||||
onXClick?: () => void;
|
||||
onOpenClick?: () => void;
|
||||
edge?: {
|
||||
color: string;
|
||||
loc: Point;
|
||||
}
|
||||
}
|
||||
export const DAGNode = ({
|
||||
loc: [x, y],
|
||||
text,
|
||||
height,
|
||||
width,
|
||||
onXClick = () => { },
|
||||
onOpenClick = () => { },
|
||||
color,
|
||||
edge,
|
||||
}: NodeProp) => {
|
||||
const { classes, theme } = useNodeStyles({
|
||||
color,
|
||||
nodeHeight: height,
|
||||
nodeWidth: width,
|
||||
});
|
||||
return <>
|
||||
<foreignObject x={x} y={y} width={width} height={height}>
|
||||
<div className={classes.root}>
|
||||
<div className={classes.inner}>
|
||||
<div className={classes.leftEdge} onClick={onXClick}>
|
||||
<button className={classes.buttonDelete}>X</button>
|
||||
</div>
|
||||
<div className={classes.textWrap}>
|
||||
<span className={classes.text}>
|
||||
{text}
|
||||
</span>
|
||||
</div>
|
||||
<div className={classes.rightEdge} onClick={onOpenClick}>
|
||||
<button className={classes.buttonEdit}>
|
||||
<PencilIcon size={1.1} color={Color(theme.primary).set('rgb.g', 200).hex()} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</foreignObject>
|
||||
{edge && <DAGEdge
|
||||
p1={[x + width, y + height/ 2]}
|
||||
p2={edge.loc}
|
||||
direction="1->2"
|
||||
color={edge.color}
|
||||
/>}
|
||||
</>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
|
||||
|
||||
export const NODE_WIDTH = 100 as const;
|
||||
export const NODE_HEIGHT = 40 as const;
|
||||
@@ -0,0 +1,144 @@
|
||||
import { Styles } from "jss";
|
||||
import Color from 'chroma-js';
|
||||
import { createAppUseStyles } from "styles/theme";
|
||||
import { Node } from "types/graphs";
|
||||
|
||||
|
||||
export const useBoardStyles = createAppUseStyles(theme => ({
|
||||
root: {
|
||||
height: '99%',
|
||||
width: '99%',
|
||||
color: theme.primaryAccent,
|
||||
background: theme.primary,
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
},
|
||||
dag: {
|
||||
borderWidth: 1,
|
||||
borderStyle: 'solid',
|
||||
borderColor: theme.secondaryAccent,
|
||||
width: '100%',
|
||||
},
|
||||
svg: {
|
||||
height: '99%',
|
||||
width: '99%',
|
||||
padding: 0,
|
||||
margin: 0,
|
||||
},
|
||||
circle: {
|
||||
fill: Color(theme.secondary).set('rgb.b', 125).hex(),
|
||||
},
|
||||
infoBox: {
|
||||
paddingLeft: '10px',
|
||||
paddingRight: '10px',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'flex-start',
|
||||
overflow: 'auto',
|
||||
},
|
||||
dagEdge: {
|
||||
stroke: theme.secondaryAccent,
|
||||
},
|
||||
}))
|
||||
|
||||
export const useNodeStyles = createAppUseStyles<{
|
||||
color: Node['color'],
|
||||
nodeHeight: number,
|
||||
nodeWidth: number,
|
||||
}>(theme => {
|
||||
const edgeStyle: Styles = {
|
||||
flex: '1',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
flexWrap: 'nowrap',
|
||||
borderRadius: '1em',
|
||||
justifyContent: 'space-around',
|
||||
alignItems: 'center',
|
||||
'&:hover': {
|
||||
backgroundColor: theme.primaryAccent,
|
||||
cursor: 'pointer',
|
||||
}
|
||||
}
|
||||
const buttonStyle: Styles = {
|
||||
fontSize: '8px',
|
||||
backgroundColor: "rgba(0,0,0,0)",
|
||||
color: theme.primaryAccent,
|
||||
borderStyle: 'none',
|
||||
height: '15px',
|
||||
width: '15px',
|
||||
cursor: 'pointer',
|
||||
transition: "500ms east-out",
|
||||
}
|
||||
return {
|
||||
root: {
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
justifyContent: 'flex-end',
|
||||
height: `100%`,
|
||||
maxHeight: `100%`,
|
||||
maxWidth: '100%',
|
||||
width: '100%',
|
||||
fontSize: 10,
|
||||
},
|
||||
inner: ({ color }) => ({
|
||||
position: 'relative',
|
||||
display: 'flex',
|
||||
flexDirection: 'row',
|
||||
backgroundColor: theme.secondary,
|
||||
borderRadius: '1em',
|
||||
flex: 1,
|
||||
height: `100%`,
|
||||
maxHeight: `100%`,
|
||||
maxWidth: '100%',
|
||||
marginTop: '2.5px',
|
||||
marginBottom: '2.5px',
|
||||
borderTopWidth: '1px',
|
||||
borderTopStyle: 'solid',
|
||||
borderTopLeftRadius: '10px',
|
||||
borderTopRightRadius: '10px',
|
||||
borderTopColor: color === 'red' ? Color(theme.secondary).set('rgb.r', 125).hex() :
|
||||
color === 'blue' ? Color(theme.secondary).set('rgb.b', 125).hex() :
|
||||
color === 'green' ? Color(theme.secondary).set('rgb.g', 125).hex() :
|
||||
'none'
|
||||
}),
|
||||
textWrap: {
|
||||
display: 'flex',
|
||||
flex: 4,
|
||||
textAlign: 'center',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
height: '100%',
|
||||
maxHeight: '100%',
|
||||
maxWidth: "50%",
|
||||
},
|
||||
text: {
|
||||
textOverflow: 'ellipsis',
|
||||
overflow: "hidden",
|
||||
whiteSpace: "nowrap",
|
||||
},
|
||||
buttonDelete: {
|
||||
...buttonStyle,
|
||||
"&:hover": {
|
||||
color: theme.primary,
|
||||
}
|
||||
},
|
||||
buttonEdit: {
|
||||
...buttonStyle,
|
||||
"&:hover": {
|
||||
transform: "rotate(45deg)"
|
||||
}
|
||||
},
|
||||
leftEdge: {
|
||||
...edgeStyle,
|
||||
borderLef: `1px ${theme.secondaryAccent} solid`,
|
||||
borderTopRightRadius: 0,
|
||||
borderBottomRightRadius: 0,
|
||||
},
|
||||
rightEdge: {
|
||||
...edgeStyle,
|
||||
borderRight: `1px ${theme.secondaryAccent} solid`,
|
||||
borderTopLeftRadius: 0,
|
||||
borderBottomLeftRadius: 0,
|
||||
},
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user