|
| 1 | +import { useCallback } from 'react' |
| 2 | +import type { Node, ReactFlowInstance } from 'reactflow' |
| 3 | + |
| 4 | +interface VisibleBounds { |
| 5 | + width: number |
| 6 | + height: number |
| 7 | + offsetLeft: number |
| 8 | + offsetRight: number |
| 9 | + offsetBottom: number |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Gets the visible canvas bounds accounting for sidebar, terminal, and panel overlays. |
| 14 | + * Works correctly regardless of whether the ReactFlow container extends under the sidebar or not. |
| 15 | + */ |
| 16 | +function getVisibleCanvasBounds(): VisibleBounds { |
| 17 | + const style = getComputedStyle(document.documentElement) |
| 18 | + |
| 19 | + const sidebarWidth = Number.parseInt(style.getPropertyValue('--sidebar-width') || '0', 10) |
| 20 | + const terminalHeight = Number.parseInt(style.getPropertyValue('--terminal-height') || '0', 10) |
| 21 | + const panelWidth = Number.parseInt(style.getPropertyValue('--panel-width') || '0', 10) |
| 22 | + |
| 23 | + const flowContainer = document.querySelector('.react-flow') |
| 24 | + if (!flowContainer) { |
| 25 | + return { |
| 26 | + width: window.innerWidth - sidebarWidth - panelWidth, |
| 27 | + height: window.innerHeight - terminalHeight, |
| 28 | + offsetLeft: sidebarWidth, |
| 29 | + offsetRight: panelWidth, |
| 30 | + offsetBottom: terminalHeight, |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + const rect = flowContainer.getBoundingClientRect() |
| 35 | + |
| 36 | + // Calculate actual visible area in screen coordinates |
| 37 | + // This works regardless of whether the container extends under overlays |
| 38 | + const visibleLeft = Math.max(rect.left, sidebarWidth) |
| 39 | + const visibleRight = Math.min(rect.right, window.innerWidth - panelWidth) |
| 40 | + const visibleBottom = Math.min(rect.bottom, window.innerHeight - terminalHeight) |
| 41 | + |
| 42 | + // Calculate visible dimensions and offsets relative to the container |
| 43 | + const visibleWidth = Math.max(0, visibleRight - visibleLeft) |
| 44 | + const visibleHeight = Math.max(0, visibleBottom - rect.top) |
| 45 | + |
| 46 | + return { |
| 47 | + width: visibleWidth, |
| 48 | + height: visibleHeight, |
| 49 | + offsetLeft: visibleLeft - rect.left, |
| 50 | + offsetRight: rect.right - visibleRight, |
| 51 | + offsetBottom: rect.bottom - visibleBottom, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Gets the center of the visible canvas in screen coordinates. |
| 57 | + */ |
| 58 | +function getVisibleCanvasCenter(): { x: number; y: number } { |
| 59 | + const style = getComputedStyle(document.documentElement) |
| 60 | + const sidebarWidth = Number.parseInt(style.getPropertyValue('--sidebar-width') || '0', 10) |
| 61 | + const panelWidth = Number.parseInt(style.getPropertyValue('--panel-width') || '0', 10) |
| 62 | + const terminalHeight = Number.parseInt(style.getPropertyValue('--terminal-height') || '0', 10) |
| 63 | + |
| 64 | + const flowContainer = document.querySelector('.react-flow') |
| 65 | + if (!flowContainer) { |
| 66 | + const visibleWidth = window.innerWidth - sidebarWidth - panelWidth |
| 67 | + const visibleHeight = window.innerHeight - terminalHeight |
| 68 | + return { |
| 69 | + x: sidebarWidth + visibleWidth / 2, |
| 70 | + y: visibleHeight / 2, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + const rect = flowContainer.getBoundingClientRect() |
| 75 | + |
| 76 | + // Calculate actual visible area in screen coordinates |
| 77 | + const visibleLeft = Math.max(rect.left, sidebarWidth) |
| 78 | + const visibleRight = Math.min(rect.right, window.innerWidth - panelWidth) |
| 79 | + const visibleBottom = Math.min(rect.bottom, window.innerHeight - terminalHeight) |
| 80 | + |
| 81 | + return { |
| 82 | + x: (visibleLeft + visibleRight) / 2, |
| 83 | + y: (rect.top + visibleBottom) / 2, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +interface FitViewToBoundsOptions { |
| 88 | + padding?: number |
| 89 | + maxZoom?: number |
| 90 | + minZoom?: number |
| 91 | + duration?: number |
| 92 | + nodes?: Node[] |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Hook providing canvas viewport utilities that account for sidebar, panel, and terminal overlays. |
| 97 | + */ |
| 98 | +export function useCanvasViewport(reactFlowInstance: ReactFlowInstance | null) { |
| 99 | + /** |
| 100 | + * Gets the center of the visible canvas in flow coordinates. |
| 101 | + */ |
| 102 | + const getViewportCenter = useCallback(() => { |
| 103 | + if (!reactFlowInstance) { |
| 104 | + return { x: 0, y: 0 } |
| 105 | + } |
| 106 | + |
| 107 | + const center = getVisibleCanvasCenter() |
| 108 | + return reactFlowInstance.screenToFlowPosition(center) |
| 109 | + }, [reactFlowInstance]) |
| 110 | + |
| 111 | + /** |
| 112 | + * Fits the view to show all nodes within the visible canvas bounds, |
| 113 | + * accounting for sidebar, panel, and terminal overlays. |
| 114 | + * @param padding - Fraction of viewport to leave as margin (0.1 = 10% on each side) |
| 115 | + */ |
| 116 | + const fitViewToBounds = useCallback( |
| 117 | + (options: FitViewToBoundsOptions = {}) => { |
| 118 | + if (!reactFlowInstance) return |
| 119 | + |
| 120 | + const { |
| 121 | + padding = 0.1, |
| 122 | + maxZoom = 1, |
| 123 | + minZoom = 0.1, |
| 124 | + duration = 300, |
| 125 | + nodes: targetNodes, |
| 126 | + } = options |
| 127 | + |
| 128 | + const nodes = targetNodes ?? reactFlowInstance.getNodes() |
| 129 | + if (nodes.length === 0) { |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + const bounds = getVisibleCanvasBounds() |
| 134 | + |
| 135 | + // Calculate node bounds |
| 136 | + let minX = Number.POSITIVE_INFINITY |
| 137 | + let minY = Number.POSITIVE_INFINITY |
| 138 | + let maxX = Number.NEGATIVE_INFINITY |
| 139 | + let maxY = Number.NEGATIVE_INFINITY |
| 140 | + |
| 141 | + nodes.forEach((node) => { |
| 142 | + const nodeWidth = node.width ?? 200 |
| 143 | + const nodeHeight = node.height ?? 100 |
| 144 | + |
| 145 | + minX = Math.min(minX, node.position.x) |
| 146 | + minY = Math.min(minY, node.position.y) |
| 147 | + maxX = Math.max(maxX, node.position.x + nodeWidth) |
| 148 | + maxY = Math.max(maxY, node.position.y + nodeHeight) |
| 149 | + }) |
| 150 | + |
| 151 | + const contentWidth = maxX - minX |
| 152 | + const contentHeight = maxY - minY |
| 153 | + |
| 154 | + // Apply padding as fraction of viewport (matches ReactFlow's fitView behavior) |
| 155 | + const availableWidth = bounds.width * (1 - padding * 2) |
| 156 | + const availableHeight = bounds.height * (1 - padding * 2) |
| 157 | + |
| 158 | + // Calculate zoom to fit content in available area |
| 159 | + const zoomX = availableWidth / contentWidth |
| 160 | + const zoomY = availableHeight / contentHeight |
| 161 | + const zoom = Math.max(minZoom, Math.min(maxZoom, Math.min(zoomX, zoomY))) |
| 162 | + |
| 163 | + // Calculate center of content in flow coordinates |
| 164 | + const contentCenterX = minX + contentWidth / 2 |
| 165 | + const contentCenterY = minY + contentHeight / 2 |
| 166 | + |
| 167 | + // Calculate viewport position to center content in visible area |
| 168 | + // Account for sidebar offset on the left |
| 169 | + const visibleCenterX = bounds.offsetLeft + bounds.width / 2 |
| 170 | + const visibleCenterY = bounds.height / 2 |
| 171 | + |
| 172 | + const x = visibleCenterX - contentCenterX * zoom |
| 173 | + const y = visibleCenterY - contentCenterY * zoom |
| 174 | + |
| 175 | + reactFlowInstance.setViewport({ x, y, zoom }, { duration }) |
| 176 | + }, |
| 177 | + [reactFlowInstance] |
| 178 | + ) |
| 179 | + |
| 180 | + return { |
| 181 | + getViewportCenter, |
| 182 | + fitViewToBounds, |
| 183 | + getVisibleCanvasBounds, |
| 184 | + } |
| 185 | +} |
0 commit comments