Fix auto fit disabled afther zoom in or out manually

- Enhanced FocusOnNode component logic
- Added reset to default view when no node
- Updated mouse event handling for custom BBox
- Added sigmaRef for future use
- Triggered camera reset after graph updates
This commit is contained in:
yangdx
2025-03-13 00:34:20 +08:00
parent 727b137506
commit d06d3a20b1
3 changed files with 28 additions and 8 deletions

View File

@@ -13,16 +13,25 @@ const FocusOnNode = ({ node, move }: { node: string | null; move?: boolean }) =>
* When the selected item changes, highlighted the node and center the camera on it. * When the selected item changes, highlighted the node and center the camera on it.
*/ */
useEffect(() => { useEffect(() => {
if (!node) return
sigma.getGraph().setNodeAttribute(node, 'highlighted', true)
if (move) { if (move) {
if (node) {
sigma.getGraph().setNodeAttribute(node, 'highlighted', true)
gotoNode(node) gotoNode(node)
} else {
// If no node is selected but move is true, reset to default view
sigma.setCustomBBox(null)
sigma.getCamera().animate({ x: 0.5, y: 0.5, ratio: 1 }, { duration: 0 })
}
useGraphStore.getState().setMoveToSelectedNode(false) useGraphStore.getState().setMoveToSelectedNode(false)
} else if (node) {
sigma.getGraph().setNodeAttribute(node, 'highlighted', true)
} }
return () => { return () => {
if (node) {
sigma.getGraph().setNodeAttribute(node, 'highlighted', false) sigma.getGraph().setNodeAttribute(node, 'highlighted', false)
} }
}
}, [node, move, sigma, gotoNode]) }, [node, move, sigma, gotoNode])
return null return null

View File

@@ -1,4 +1,4 @@
import { useEffect, useState, useCallback, useMemo } from 'react' import { useEffect, useState, useCallback, useMemo, useRef } from 'react'
// import { MiniMap } from '@react-sigma/minimap' // import { MiniMap } from '@react-sigma/minimap'
import { SigmaContainer, useRegisterEvents, useSigma } from '@react-sigma/core' import { SigmaContainer, useRegisterEvents, useSigma } from '@react-sigma/core'
import { Settings as SigmaSettings } from 'sigma/settings' import { Settings as SigmaSettings } from 'sigma/settings'
@@ -91,8 +91,12 @@ const GraphEvents = () => {
} }
}, },
// Disable the autoscale at the first down interaction // Disable the autoscale at the first down interaction
mousedown: () => { mousedown: (e) => {
if (!sigma.getCustomBBox()) sigma.setCustomBBox(sigma.getBBox()) // Only set custom BBox if it's a drag operation (mouse button is pressed)
const mouseEvent = e.original as MouseEvent;
if (mouseEvent.buttons !== 0 && !sigma.getCustomBBox()) {
sigma.setCustomBBox(sigma.getBBox())
}
} }
}) })
}, [registerEvents, sigma, draggedNode]) }, [registerEvents, sigma, draggedNode])
@@ -102,6 +106,7 @@ const GraphEvents = () => {
const GraphViewer = () => { const GraphViewer = () => {
const [sigmaSettings, setSigmaSettings] = useState(defaultSigmaSettings) const [sigmaSettings, setSigmaSettings] = useState(defaultSigmaSettings)
const sigmaRef = useRef<any>(null)
const selectedNode = useGraphStore.use.selectedNode() const selectedNode = useGraphStore.use.selectedNode()
const focusedNode = useGraphStore.use.focusedNode() const focusedNode = useGraphStore.use.focusedNode()
@@ -144,7 +149,11 @@ const GraphViewer = () => {
) )
return ( return (
<SigmaContainer settings={sigmaSettings} className="!bg-background !size-full overflow-hidden"> <SigmaContainer
settings={sigmaSettings}
className="!bg-background !size-full overflow-hidden"
ref={sigmaRef}
>
<GraphControl /> <GraphControl />
{enableNodeDrag && <GraphEvents />} {enableNodeDrag && <GraphEvents />}

View File

@@ -254,6 +254,8 @@ const useLightrangeGraph = () => {
delete fetchStatusRef.current[fetchKey]; delete fetchStatusRef.current[fetchKey];
} }
// Reset fetching state after all updates are complete // Reset fetching state after all updates are complete
// Reset camera view by triggering FocusOnNode component
state.setMoveToSelectedNode(true);
state.setIsFetching(false); state.setIsFetching(false);
}).catch(() => { }).catch(() => {
// Reset fetching state and remove flag in case of error // Reset fetching state and remove flag in case of error