feat: Implement debounced hover handling in WikiBrowser for improved node interaction
- Introduced a shared timer for debouncing mouseleave to mouseenter transitions, preventing flickering when quickly moving between adjacent nodes. - Updated event listeners to clear and set the hover timer, ensuring smoother highlight transitions without abrupt visual changes. - Enhanced the rendering logic to maintain consistent highlighting behavior based on node selection state. These changes enhance the user experience by providing a more fluid interaction with the graph nodes in the WikiBrowser.
This commit is contained in:
@@ -1024,6 +1024,9 @@ interface GNode {
|
|||||||
let graphNodes: GNode[] = []
|
let graphNodes: GNode[] = []
|
||||||
let graphSvg: SVGSVGElement | null = null
|
let graphSvg: SVGSVGElement | null = null
|
||||||
let graphAnimFrame = 0
|
let graphAnimFrame = 0
|
||||||
|
// Shared timer for debouncing node mouseleave -> mouseenter transitions.
|
||||||
|
// Prevents flickering when the pointer quickly moves between adjacent nodes.
|
||||||
|
let graphHoverLeaveTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
|
|
||||||
// Used for graph search centering interaction
|
// Used for graph search centering interaction
|
||||||
let graphPanZoomRef: {
|
let graphPanZoomRef: {
|
||||||
@@ -1054,6 +1057,7 @@ function renderGraph() {
|
|||||||
|
|
||||||
// Stop any previous animation
|
// Stop any previous animation
|
||||||
if (graphAnimFrame) { cancelAnimationFrame(graphAnimFrame); graphAnimFrame = 0 }
|
if (graphAnimFrame) { cancelAnimationFrame(graphAnimFrame); graphAnimFrame = 0 }
|
||||||
|
if (graphHoverLeaveTimer) { clearTimeout(graphHoverLeaveTimer); graphHoverLeaveTimer = null }
|
||||||
|
|
||||||
const width = container.clientWidth || 800
|
const width = container.clientWidth || 800
|
||||||
const height = container.clientHeight || 600
|
const height = container.clientHeight || 600
|
||||||
@@ -1247,26 +1251,36 @@ function renderGraph() {
|
|||||||
g.appendChild(text)
|
g.appendChild(text)
|
||||||
|
|
||||||
// Hover highlight
|
// Hover highlight
|
||||||
|
// We debounce the "leave" side so that quickly sliding the pointer from
|
||||||
|
// one node to the next doesn't flash through the fully-unhighlighted state
|
||||||
|
// (which is what caused the whole-graph flickering).
|
||||||
g.addEventListener('mouseenter', () => {
|
g.addEventListener('mouseenter', () => {
|
||||||
// Don't change highlight on hover if we have a selected node
|
if (graphHoverLeaveTimer) {
|
||||||
|
clearTimeout(graphHoverLeaveTimer)
|
||||||
|
graphHoverLeaveTimer = null
|
||||||
|
}
|
||||||
if (!graphSelectedSlug.value) {
|
if (!graphSelectedSlug.value) {
|
||||||
|
if (graphHighlightSlug.value === n.slug) return
|
||||||
graphHighlightSlug.value = n.slug
|
graphHighlightSlug.value = n.slug
|
||||||
applyHighlight(n.slug, adjacency, nodeEls, edgeEls)
|
applyHighlight(n.slug, adjacency, nodeEls, edgeEls)
|
||||||
} else if (graphSelectedSlug.value !== n.slug) {
|
} else if (graphSelectedSlug.value !== n.slug) {
|
||||||
// If a node is selected, hover over others should just add to highlight
|
if (graphHighlightSlug.value === n.slug) return
|
||||||
// but we'll keep the selected one as the main focus
|
graphHighlightSlug.value = n.slug
|
||||||
applyHighlight(graphSelectedSlug.value, adjacency, nodeEls, edgeEls, n.slug)
|
applyHighlight(graphSelectedSlug.value, adjacency, nodeEls, edgeEls, n.slug)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
g.addEventListener('mouseleave', () => {
|
g.addEventListener('mouseleave', () => {
|
||||||
// Only clear if we were just highlighting on hover, not selected
|
if (graphHoverLeaveTimer) clearTimeout(graphHoverLeaveTimer)
|
||||||
if (!graphSelectedSlug.value) {
|
graphHoverLeaveTimer = setTimeout(() => {
|
||||||
graphHighlightSlug.value = null
|
graphHoverLeaveTimer = null
|
||||||
clearHighlight(nodeEls, edgeEls)
|
if (!graphSelectedSlug.value) {
|
||||||
} else {
|
graphHighlightSlug.value = null
|
||||||
// Restore pure selected state
|
clearHighlight(nodeEls, edgeEls)
|
||||||
applyHighlight(graphSelectedSlug.value, adjacency, nodeEls, edgeEls)
|
} else {
|
||||||
}
|
graphHighlightSlug.value = null
|
||||||
|
applyHighlight(graphSelectedSlug.value, adjacency, nodeEls, edgeEls)
|
||||||
|
}
|
||||||
|
}, 60)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Click to select & open drawer directly
|
// Click to select & open drawer directly
|
||||||
@@ -1833,6 +1847,14 @@ onUnmounted(() => {
|
|||||||
if (statsTimer) {
|
if (statsTimer) {
|
||||||
clearInterval(statsTimer)
|
clearInterval(statsTimer)
|
||||||
}
|
}
|
||||||
|
if (graphHoverLeaveTimer) {
|
||||||
|
clearTimeout(graphHoverLeaveTimer)
|
||||||
|
graphHoverLeaveTimer = null
|
||||||
|
}
|
||||||
|
if (graphAnimFrame) {
|
||||||
|
cancelAnimationFrame(graphAnimFrame)
|
||||||
|
graphAnimFrame = 0
|
||||||
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user