Merge pull request #9 from wbkd/feat/gridrenderer

feat: GridRenderer
This commit is contained in:
Moritz
2019-10-07 21:41:58 +02:00
committed by GitHub
2 changed files with 56 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ import { useStoreState, useStoreActions } from 'easy-peasy';
import NodeRenderer from '../NodeRenderer';
import EdgeRenderer from '../EdgeRenderer';
import GridRenderer from '../GridRenderer';
import UserSelection from '../../components/UserSelection';
import NodesSelection from '../../components/NodesSelection';
import useKeyPress from '../../hooks/useKeyPress';
@@ -67,6 +68,7 @@ const GraphView = memo(({
return (
<div className="react-flow__renderer" ref={rendererNode}>
<GridRenderer />
<NodeRenderer
nodeTypes={nodeTypes}
onElementClick={onElementClick}

View File

@@ -0,0 +1,54 @@
import React, { memo } from 'react';
import { useStoreState } from 'easy-peasy';
import classnames from 'classnames';
const baseStyles = {
position: 'absolute',
top: 0,
left: 0
};
const GridRenderer = memo(({
gap = 24, strokeColor = '#999', strokeWidth = 0.1, styles,
className
}) => {
const {
width,
height,
transform: [x, y, scale],
} = useStoreState(s => s);
const gridClasses = classnames('react-flow__grid', className);
const scaledGap = gap * scale;
const xStart = x % scaledGap;
const yStart = y % scaledGap;
const lineCountX = Math.ceil(width / scaledGap) + 1;
const lineCountY = Math.ceil(height / scaledGap) + 1;
const xValues = Array.from({length: lineCountX}, (_, index) => `M${index * scaledGap + xStart} 0 V${height}`);
const yValues = Array.from({length: lineCountY}, (_, index) => `M0 ${index * scaledGap + yStart} H${width}`);
const path = [...xValues, ...yValues].join(' ');
return (
<svg
width={width}
height={height}
style={{ ...baseStyles, ...styles }}
className={gridClasses}
>
<path
fill="none"
stroke={strokeColor}
strokeWidth={strokeWidth}
d={path}
/>
</svg>
);
});
GridRenderer.displayName = 'GridRenderer';
export default GridRenderer;