refactor(nodelookup): use as normale store item

This commit is contained in:
moklick
2021-11-04 14:03:22 +01:00
parent 1b390e103d
commit 5547b0d6cc
5 changed files with 26 additions and 28 deletions
+6 -2
View File
@@ -1,5 +1,6 @@
import React, { useEffect, useState, CSSProperties } from 'react'; import React, { useEffect, useState, CSSProperties } from 'react';
import { useStore } from '../../store';
import { getBezierPath } from '../Edges/BezierEdge'; import { getBezierPath } from '../Edges/BezierEdge';
import { getSmoothStepPath } from '../Edges/SmoothStepEdge'; import { getSmoothStepPath } from '../Edges/SmoothStepEdge';
import { import {
@@ -12,8 +13,9 @@ import {
ConnectionLineComponent, ConnectionLineComponent,
HandleType, HandleType,
Node, Node,
ReactFlowState,
} from '../../types'; } from '../../types';
import useNodeLookup from '../../hooks/useNodeLookup';
interface ConnectionLineProps { interface ConnectionLineProps {
connectionNodeId: ElementId; connectionNodeId: ElementId;
connectionHandleId: ElementId | null; connectionHandleId: ElementId | null;
@@ -27,6 +29,8 @@ interface ConnectionLineProps {
CustomConnectionLineComponent?: ConnectionLineComponent; CustomConnectionLineComponent?: ConnectionLineComponent;
} }
const selector = (s: ReactFlowState) => s.nodeLookup;
export default ({ export default ({
connectionNodeId, connectionNodeId,
connectionHandleId, connectionHandleId,
@@ -39,7 +43,7 @@ export default ({
isConnectable, isConnectable,
CustomConnectionLineComponent, CustomConnectionLineComponent,
}: ConnectionLineProps) => { }: ConnectionLineProps) => {
const nodeLookup = useNodeLookup(); const nodeLookup = useStore(selector);
const [sourceNode, setSourceNode] = useState<NodeLookupItem | null>(null); const [sourceNode, setSourceNode] = useState<NodeLookupItem | null>(null);
const nodeId = connectionNodeId; const nodeId = connectionNodeId;
const handleId = connectionHandleId; const handleId = connectionHandleId;
+2 -3
View File
@@ -17,7 +17,6 @@ import {
NodeHandleBounds, NodeHandleBounds,
} from '../../types'; } from '../../types';
import useVisibleEdges from '../../hooks/useVisibleEdges'; import useVisibleEdges from '../../hooks/useVisibleEdges';
import useNodeLookup from '../../hooks/useNodeLookup';
interface EdgeRendererProps { interface EdgeRendererProps {
edgeTypes: any; edgeTypes: any;
@@ -218,7 +217,7 @@ const selector = (s: ReactFlowState) => ({
width: s.width, width: s.width,
height: s.height, height: s.height,
connectionMode: s.connectionMode, connectionMode: s.connectionMode,
nodes: s.nodes, nodeLookup: s.nodeLookup,
}); });
const EdgeRenderer = (props: EdgeRendererProps) => { const EdgeRenderer = (props: EdgeRendererProps) => {
@@ -233,8 +232,8 @@ const EdgeRenderer = (props: EdgeRendererProps) => {
width, width,
height, height,
connectionMode, connectionMode,
nodeLookup,
} = useStore(selector, shallow); } = useStore(selector, shallow);
const nodeLookup = useNodeLookup();
const edgeTree = useVisibleEdges(props.onlyRenderVisibleElements, nodeLookup); const edgeTree = useVisibleEdges(props.onlyRenderVisibleElements, nodeLookup);
if (!width) { if (!width) {
+2 -2
View File
@@ -4,7 +4,7 @@ import shallow from 'zustand/shallow';
import { useStore } from '../../store'; import { useStore } from '../../store';
import { Node, NodeTypesType, ReactFlowState, WrapNodeProps, SnapGrid, NodeRendererNode } from '../../types'; import { Node, NodeTypesType, ReactFlowState, WrapNodeProps, SnapGrid, NodeRendererNode } from '../../types';
import useVisibleNodes from '../../hooks/useVisibleNodes'; import useVisibleNodes from '../../hooks/useVisibleNodes';
import useNodeLookup from '../../hooks/useNodeLookup';
interface NodeRendererProps { interface NodeRendererProps {
nodeTypes: NodeTypesType; nodeTypes: NodeTypesType;
selectNodesOnDrag: boolean; selectNodesOnDrag: boolean;
@@ -131,9 +131,9 @@ const NodeRenderer = (props: NodeRendererProps) => {
updateNodeDimensions, updateNodeDimensions,
snapGrid, snapGrid,
snapToGrid, snapToGrid,
nodeLookup,
} = useStore(selector, shallow); } = useStore(selector, shallow);
const nodeLookup = useNodeLookup();
const nodes = useVisibleNodes(props.onlyRenderVisibleElements); const nodes = useVisibleNodes(props.onlyRenderVisibleElements);
const resizeObserver = useMemo(() => { const resizeObserver = useMemo(() => {
-12
View File
@@ -1,12 +0,0 @@
import { useRef } from 'react';
import { useStoreApi } from '../store';
function useNodeLookup() {
const store = useStoreApi();
const nodeLookup = useRef(store.getState().nodeLookup);
return nodeLookup.current;
}
export default useNodeLookup;
+16 -9
View File
@@ -28,6 +28,7 @@ import {
NodeDimensionChange, NodeDimensionChange,
NodeLookup, NodeLookup,
NodeLookupItem, NodeLookupItem,
ElementId,
} from '../types'; } from '../types';
import { isNode, isEdge, getRectOfNodes, getNodesInside, getConnectedEdges } from '../utils/graph'; import { isNode, isEdge, getRectOfNodes, getNodesInside, getConnectedEdges } from '../utils/graph';
import { getHandleBounds } from '../components/Nodes/utils'; import { getHandleBounds } from '../components/Nodes/utils';
@@ -128,9 +129,10 @@ const createStore = () =>
setNodes: (nodes: Node[]) => { setNodes: (nodes: Node[]) => {
const { nodeLookup } = get(); const { nodeLookup } = get();
const nextNodeLookup = new Map<ElementId, NodeLookupItem>();
nodes.forEach((node) => { nodes.forEach((node) => {
const lookupNode = { const lookupNode: NodeLookupItem = {
...nodeLookup.get(node.id), ...nodeLookup.get(node.id),
width: node.width || null, width: node.width || null,
height: node.height || null, height: node.height || null,
@@ -141,24 +143,24 @@ const createStore = () =>
if (node.parentNode) { if (node.parentNode) {
lookupNode.parentNode = node.parentNode; lookupNode.parentNode = node.parentNode;
} }
nodeLookup.set(node.id, lookupNode); nextNodeLookup.set(node.id, lookupNode);
}); });
nodes nodes
.filter((node) => node.parentNode) .filter((node) => node.parentNode)
.forEach((node) => { .forEach((node) => {
const positionAbsoluteAndTreeLevel = getAbsolutePositionAndTreeLevel(node, nodeLookup, { const positionAbsoluteAndTreeLevel = getAbsolutePositionAndTreeLevel(node, nextNodeLookup, {
...node.position, ...node.position,
treeLevel: node.zIndex || 0, treeLevel: node.zIndex || 0,
}); });
nodeLookup.set(node.parentNode!, { ...nodeLookup.get(node.parentNode!), isParentNode: true }); nextNodeLookup.set(node.parentNode!, { ...nextNodeLookup.get(node.parentNode!), isParentNode: true });
if (positionAbsoluteAndTreeLevel) { if (positionAbsoluteAndTreeLevel) {
const { treeLevel, x, y } = positionAbsoluteAndTreeLevel; const { treeLevel, x, y } = positionAbsoluteAndTreeLevel;
nodeLookup.set(node.id, { nextNodeLookup.set(node.id, {
...nodeLookup.get(node.id), ...nextNodeLookup.get(node.id),
positionAbsolute: { positionAbsolute: {
x, x,
y, y,
@@ -168,7 +170,7 @@ const createStore = () =>
} }
}); });
set({ nodes }); set({ nodes, nodeLookup: nextNodeLookup });
}, },
setEdges: (edges: Edge[]) => { setEdges: (edges: Edge[]) => {
set({ edges }); set({ edges });
@@ -188,13 +190,16 @@ const createStore = () =>
if (doUpdate) { if (doUpdate) {
const handleBounds = getHandleBounds(update.nodeElement, transform[2]); const handleBounds = getHandleBounds(update.nodeElement, transform[2]);
nodeLookup.set(node.id, { ...nodeLookup.get(node.id), handleBounds }); nodeLookup.set(node.id, {
...nodeLookup.get(node.id),
handleBounds,
...dimensions,
});
const change = { const change = {
id: node.id, id: node.id,
type: 'dimensions', type: 'dimensions',
dimensions, dimensions,
handleBounds,
} as NodeChange; } as NodeChange;
res.push(change); res.push(change);
} }
@@ -203,6 +208,8 @@ const createStore = () =>
return res; return res;
}, []); }, []);
set({ nodeLookup: new Map(nodeLookup) });
onNodesChange?.(nodesToChange); onNodesChange?.(nodesToChange);
}, },
updateNodePosition: ({ id, diff, isDragging }: NodeDiffUpdate) => { updateNodePosition: ({ id, diff, isDragging }: NodeDiffUpdate) => {