refactor(internals): make prop optional

This commit is contained in:
moklick
2022-05-26 20:57:12 +02:00
parent ee7084b5dc
commit 2ed3ffe818
8 changed files with 30 additions and 14 deletions
+18 -1
View File
@@ -33,7 +33,18 @@ const UseZoomPanHelperFlow = () => {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes); const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = (params: Connection | Edge) => setEdges((eds) => addEdge(params, eds)); const onConnect = (params: Connection | Edge) => setEdges((eds) => addEdge(params, eds));
const { project, setCenter, zoomIn, zoomOut, fitView, addNodes, setNodes: setNodesHook, addEdges } = useReactFlow(); const {
project,
setCenter,
zoomIn,
zoomOut,
fitView,
addNodes,
setNodes: setNodesHook,
addEdges,
getNodes,
getEdges,
} = useReactFlow();
const onPaneClick = useCallback( const onPaneClick = useCallback(
(evt: MouseEvent) => { (evt: MouseEvent) => {
@@ -72,6 +83,11 @@ const UseZoomPanHelperFlow = () => {
addNodes(newNode); addNodes(newNode);
}, [addNodes]); }, [addNodes]);
const logNodes = useCallback(() => {
console.log('nodes', getNodes());
console.log('edges', getEdges());
}, [getNodes]);
useEffect(() => { useEffect(() => {
addEdges({ id: 'e3-4', source: '3', target: '4' }); addEdges({ id: 'e3-4', source: '3', target: '4' });
}, [addEdges]); }, [addEdges]);
@@ -97,6 +113,7 @@ const UseZoomPanHelperFlow = () => {
<button onClick={() => fitView({ duration: 1200, padding: 0.3 })}>fitView</button> <button onClick={() => fitView({ duration: 1200, padding: 0.3 })}>fitView</button>
<button onClick={onAddNode}>add node</button> <button onClick={onAddNode}>add node</button>
<button onClick={onResetNodes}>reset nodes</button> <button onClick={onResetNodes}>reset nodes</button>
<button onClick={logNodes}>useNodes</button>
</div> </div>
<Background /> <Background />
<MiniMap /> <MiniMap />
+1 -1
View File
@@ -38,7 +38,7 @@ export default ({
const { nodeInternals, transform } = useStore(selector, shallow); const { nodeInternals, transform } = useStore(selector, shallow);
const fromNode = useRef<Node | undefined>(nodeInternals.get(nodeId)); const fromNode = useRef<Node | undefined>(nodeInternals.get(nodeId));
const fromHandleBounds = fromNode.current?.[internalsSymbol].handleBounds; const fromHandleBounds = fromNode.current?.[internalsSymbol]?.handleBounds;
if (!fromNode.current || !isConnectable || !fromHandleBounds?.[connectionHandleType]) { if (!fromNode.current || !isConnectable || !fromHandleBounds?.[connectionHandleType]) {
return null; return null;
+1 -1
View File
@@ -168,7 +168,7 @@ export function isEdgeVisible({
export function getNodeData(nodeInternals: NodeInternals, nodeId: string): [Rect, NodeHandleBounds | null, boolean] { export function getNodeData(nodeInternals: NodeInternals, nodeId: string): [Rect, NodeHandleBounds | null, boolean] {
const node = nodeInternals.get(nodeId); const node = nodeInternals.get(nodeId);
const handleBounds = node?.[internalsSymbol].handleBounds || null; const handleBounds = node?.[internalsSymbol]?.handleBounds || null;
const isInvalid = const isInvalid =
!node || !node ||
+2 -2
View File
@@ -118,8 +118,8 @@ const NodeRenderer = (props: NodeRendererProps) => {
isConnectable={isConnectable} isConnectable={isConnectable}
resizeObserver={resizeObserver} resizeObserver={resizeObserver}
dragHandle={node.dragHandle} dragHandle={node.dragHandle}
zIndex={node[internalsSymbol].z ?? 0} zIndex={node[internalsSymbol]?.z ?? 0}
isParent={!!node[internalsSymbol].isParent} isParent={!!node[internalsSymbol]?.isParent}
noDragClassName={props.noDragClassName} noDragClassName={props.noDragClassName}
noPanClassName={props.noPanClassName} noPanClassName={props.noPanClassName}
/> />
+2 -2
View File
@@ -18,8 +18,8 @@ function groupEdgesByZLevel(edges: Edge[], nodeInternals: NodeInternals, elevate
z = hasZIndex z = hasZIndex
? edge.zIndex! ? edge.zIndex!
: Math.max( : Math.max(
nodeInternals.get(edge.source)?.[internalsSymbol].z || 0, nodeInternals.get(edge.source)?.[internalsSymbol]?.z || 0,
nodeInternals.get(edge.target)?.[internalsSymbol].z || 0 nodeInternals.get(edge.target)?.[internalsSymbol]?.z || 0
); );
} }
-1
View File
@@ -32,7 +32,6 @@ export {
} from './utils/graph'; } from './utils/graph';
export { applyNodeChanges, applyEdgeChanges } from './utils/changes'; export { applyNodeChanges, applyEdgeChanges } from './utils/changes';
export { getMarkerEnd, getCenter as getEdgeCenter } from './components/Edges/utils'; export { getMarkerEnd, getCenter as getEdgeCenter } from './components/Edges/utils';
export { internalsSymbol } from './utils';
export { default as useReactFlow } from './hooks/useReactFlow'; export { default as useReactFlow } from './hooks/useReactFlow';
export { default as useUpdateNodeInternals } from './hooks/useUpdateNodeInternals'; export { default as useUpdateNodeInternals } from './hooks/useUpdateNodeInternals';
+5 -5
View File
@@ -30,7 +30,7 @@ function calculateXYZPosition(
return calculateXYZPosition(parentNode, nodeInternals, parentNodes, { return calculateXYZPosition(parentNode, nodeInternals, parentNodes, {
x: (result.x ?? 0) + (parentNode.position?.x ?? 0), x: (result.x ?? 0) + (parentNode.position?.x ?? 0),
y: (result.y ?? 0) + (parentNode.position?.y ?? 0), y: (result.y ?? 0) + (parentNode.position?.y ?? 0),
z: (parentNode[internalsSymbol].z ?? 0) > (result.z ?? 0) ? parentNode[internalsSymbol].z ?? 0 : result.z ?? 0, z: (parentNode[internalsSymbol]?.z ?? 0) > (result.z ?? 0) ? parentNode[internalsSymbol]?.z ?? 0 : result.z ?? 0,
}); });
} }
@@ -60,7 +60,7 @@ export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals)
Object.defineProperty(internals, internalsSymbol, { Object.defineProperty(internals, internalsSymbol, {
enumerable: false, enumerable: false,
value: { value: {
handleBounds: currInternals?.[internalsSymbol].handleBounds, handleBounds: currInternals?.[internalsSymbol]?.handleBounds,
z, z,
}, },
}); });
@@ -76,7 +76,7 @@ export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals)
if (node.parentNode || parentNodes[node.id]) { if (node.parentNode || parentNodes[node.id]) {
const { x, y, z } = calculateXYZPosition(node, nextNodeInternals, parentNodes, { const { x, y, z } = calculateXYZPosition(node, nextNodeInternals, parentNodes, {
...node.position, ...node.position,
z: node[internalsSymbol].z ?? 0, z: node[internalsSymbol]?.z ?? 0,
}); });
node.positionAbsolute = { node.positionAbsolute = {
@@ -84,10 +84,10 @@ export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals)
y, y,
}; };
node[internalsSymbol].z = z; node[internalsSymbol]!.z = z;
if (parentNodes[node.id]) { if (parentNodes[node.id]) {
node[internalsSymbol].isParent = true; node[internalsSymbol]!.isParent = true;
} }
} }
}); });
+1 -1
View File
@@ -29,7 +29,7 @@ export interface Node<T = any> {
positionAbsolute?: XYPosition; positionAbsolute?: XYPosition;
// only used internally // only used internally
[internalsSymbol]: { [internalsSymbol]?: {
z?: number; z?: number;
handleBounds?: NodeHandleBounds; handleBounds?: NodeHandleBounds;
isParent?: boolean; isParent?: boolean;