refactor(hooks): add useVisibleNodes and useVisibleEdges

This commit is contained in:
moklick
2021-10-21 09:05:56 +02:00
parent 450cb90e67
commit 8f23321bd0
4 changed files with 73 additions and 46 deletions
+44
View File
@@ -0,0 +1,44 @@
import { useCallback } from 'react';
import { useStore } from '../store';
import { isEdgeVisible, getSourceTargetNodes } from '../container/EdgeRenderer/utils';
import { ReactFlowState } from '../types';
function useVisibleEdges(onlyRenderVisible: boolean) {
const edges = useStore(
useCallback(
(s: ReactFlowState) => {
if (!onlyRenderVisible) {
return s.edges;
}
return s.edges.filter((e) => {
const { sourceNode, targetNode } = getSourceTargetNodes(e, s.nodes);
return (
sourceNode?.width &&
sourceNode?.height &&
targetNode?.width &&
targetNode?.height &&
isEdgeVisible({
sourcePos: sourceNode.position,
targetPos: targetNode.position,
sourceWidth: sourceNode.width,
sourceHeight: sourceNode.height,
targetWidth: targetNode.width,
targetHeight: targetNode.height,
width: s.width,
height: s.height,
transform: s.transform,
})
);
});
},
[onlyRenderVisible]
)
);
return edges;
}
export default useVisibleEdges;
+22
View File
@@ -0,0 +1,22 @@
import { useCallback } from 'react';
import { useStore } from '../store';
import { getNodesInside } from '../utils/graph';
import { ReactFlowState } from '../types';
function useVisibleNodes(onlyRenderVisible: boolean) {
const nodes = useStore(
useCallback(
(s: ReactFlowState) => {
return onlyRenderVisible
? getNodesInside(s.nodes, { x: 0, y: 0, width: s.width, height: s.height }, s.transform, true)
: s.nodes;
},
[onlyRenderVisible]
)
);
return nodes;
}
export default useVisibleNodes;