refactor(minimap): cleanup

This commit is contained in:
moklick
2022-05-18 00:02:20 +02:00
parent 7d13461126
commit 71f670eeb1
3 changed files with 23 additions and 35 deletions
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "react-flow-renderer", "name": "react-flow-renderer",
"version": "10.2.3-next.2", "version": "10.2.3-next.3",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "react-flow-renderer", "name": "react-flow-renderer",
"version": "10.2.3-next.2", "version": "10.2.3-next.3",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@babel/runtime": "^7.17.9", "@babel/runtime": "^7.17.9",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "react-flow-renderer", "name": "react-flow-renderer",
"version": "10.2.3-next.2", "version": "10.2.3-next.3",
"engines": { "engines": {
"node": ">=14" "node": ">=14"
}, },
+20 -32
View File
@@ -3,7 +3,6 @@ import cc from 'classcat';
import shallow from 'zustand/shallow'; import shallow from 'zustand/shallow';
import MiniMapNode from './MiniMapNode'; import MiniMapNode from './MiniMapNode';
import { useStore } from '../../store'; import { useStore } from '../../store';
import { getRectOfNodes } from '../../utils/graph'; import { getRectOfNodes } from '../../utils/graph';
import { getBoundsofRects } from '../../utils'; import { getBoundsofRects } from '../../utils';
@@ -19,9 +18,11 @@ const selector = (s: ReactFlowState) => ({
width: s.width, width: s.width,
height: s.height, height: s.height,
transform: s.transform, transform: s.transform,
nodeInternals: s.nodeInternals, nodes: Array.from(s.nodeInternals.values()),
}); });
const getAttrFunction = (func: any): GetMiniMapNodeAttribute => (func instanceof Function ? func : () => func);
const MiniMap = ({ const MiniMap = ({
style, style,
className, className,
@@ -32,30 +33,19 @@ const MiniMap = ({
nodeStrokeWidth = 2, nodeStrokeWidth = 2,
maskColor = 'rgb(240, 242, 243, 0.7)', maskColor = 'rgb(240, 242, 243, 0.7)',
}: MiniMapProps) => { }: MiniMapProps) => {
const { width: containerWidth, height: containerHeight, transform, nodeInternals } = useStore(selector, shallow); const { width: containerWidth, height: containerHeight, transform, nodes } = useStore(selector, shallow);
const [tX, tY, tScale] = transform; const elementWidth = (style?.width as number) ?? defaultWidth;
const elementHeight = (style?.height as number) ?? defaultHeight;
const mapClasses = cc(['react-flow__minimap', className]); const nodeColorFunc = getAttrFunction(nodeColor);
const elementWidth = (style?.width || defaultWidth)! as number; const nodeStrokeColorFunc = getAttrFunction(nodeStrokeColor);
const elementHeight = (style?.height || defaultHeight)! as number; const nodeClassNameFunc = getAttrFunction(nodeClassName);
const nodeColorFunc = (nodeColor instanceof Function ? nodeColor : () => nodeColor) as GetMiniMapNodeAttribute;
const nodeStrokeColorFunc = (
nodeStrokeColor instanceof Function ? nodeStrokeColor : () => nodeStrokeColor
) as GetMiniMapNodeAttribute;
const nodeClassNameFunc = (
nodeClassName instanceof Function ? nodeClassName : () => nodeClassName
) as GetMiniMapNodeAttribute;
const hasNodes = nodeInternals && nodeInternals.size > 0;
// @TODO: work with nodeInternals instead of converting it to an array
const nodes = Array.from(nodeInternals).map(([_, node]) => node);
const bb = getRectOfNodes(nodes);
const viewBB: Rect = { const viewBB: Rect = {
x: -tX / tScale, x: -transform[0] / transform[2],
y: -tY / tScale, y: -transform[1] / transform[2],
width: containerWidth / tScale, width: containerWidth / transform[2],
height: containerHeight / tScale, height: containerHeight / transform[2],
}; };
const boundingRect = hasNodes ? getBoundsofRects(bb, viewBB) : viewBB; const boundingRect = nodes.length > 0 ? getBoundsofRects(getRectOfNodes(nodes), viewBB) : viewBB;
const scaledWidth = boundingRect.width / elementWidth; const scaledWidth = boundingRect.width / elementWidth;
const scaledHeight = boundingRect.height / elementHeight; const scaledHeight = boundingRect.height / elementHeight;
const viewScale = Math.max(scaledWidth, scaledHeight); const viewScale = Math.max(scaledWidth, scaledHeight);
@@ -74,18 +64,16 @@ const MiniMap = ({
height={elementHeight} height={elementHeight}
viewBox={`${x} ${y} ${width} ${height}`} viewBox={`${x} ${y} ${width} ${height}`}
style={style} style={style}
className={mapClasses} className={cc(['react-flow__minimap', className])}
> >
{Array.from(nodeInternals) {nodes
.filter(([_, node]) => !node.hidden && node.width && node.height) .filter((node) => !node.hidden && node.width && node.height)
.map(([_, node]) => { .map((node) => {
const positionAbsolute = nodeInternals.get(node.id)?.positionAbsolute;
return ( return (
<MiniMapNode <MiniMapNode
key={node.id} key={node.id}
x={positionAbsolute?.x || 0} x={node.positionAbsolute?.x ?? 0}
y={positionAbsolute?.y || 0} y={node.positionAbsolute?.y ?? 0}
width={node.width!} width={node.width!}
height={node.height!} height={node.height!}
style={node.style} style={node.style}