Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/components/CenterCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,45 @@ export default function CenterCanvas({
setIsPanning(false);
};

const handleFitToScreen = () => {
if (nodes.length === 0 || !containerRef.current) return;

let minX = Infinity;
let maxX = -Infinity;
let minY = Infinity;
let maxY = -Infinity;

nodes.forEach((node) => {
minX = Math.min(minX, node.x);
maxX = Math.max(maxX, node.x + NODE_WIDTH);
minY = Math.min(minY, node.y);
maxY = Math.max(maxY, node.y + NODE_HEIGHT);
});

const contentWidth = maxX - minX;
const contentHeight = maxY - minY;

const containerWidth = containerRef.current.clientWidth;
const containerHeight = containerRef.current.clientHeight;

const padding = 80;
const scaleX = (containerWidth - padding) / contentWidth;
const scaleY = (containerHeight - padding) / contentHeight;
let newScale = Math.min(scaleX, scaleY);
newScale = Math.min(Math.max(0.4, newScale), 2);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Fit-to-screen uses inconsistent zoom clamping range [0.4, 2] instead of the canvas-wide [0.25, 3], which can prevent large/small charts from actually fitting to the viewport.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/components/CenterCanvas.tsx, line 127:

<comment>Fit-to-screen uses inconsistent zoom clamping range [0.4, 2] instead of the canvas-wide [0.25, 3], which can prevent large/small charts from actually fitting to the viewport.</comment>

<file context>
@@ -99,6 +99,45 @@ export default function CenterCanvas({
+    const scaleX = (containerWidth - padding) / contentWidth;
+    const scaleY = (containerHeight - padding) / contentHeight;
+    let newScale = Math.min(scaleX, scaleY);
+    newScale = Math.min(Math.max(0.4, newScale), 2);
+    newScale = parseFloat(newScale.toFixed(2));
+
</file context>
Suggested change
newScale = Math.min(Math.max(0.4, newScale), 2);
newScale = Math.min(Math.max(0.25, newScale), 3);

newScale = parseFloat(newScale.toFixed(2));

const contentCx = minX + contentWidth / 2;
const contentCy = minY + contentHeight / 2;

const newPanX = containerWidth / 2 - contentCx * newScale;
const newPanY = containerHeight / 2 - contentCy * newScale;

setScale(newScale);
setPan({ x: Math.round(newPanX), y: Math.round(newPanY) });
showToast?.('Fit flowchart to screen', 'info');
};

// Wheel zoom listener with passive ref to prevent page scroll
React.useEffect(() => {
const container = containerRef.current;
Expand Down Expand Up @@ -748,6 +787,13 @@ export default function CenterCanvas({
>
Reset
</button>
<button
onClick={handleFitToScreen}
title="Fit all blocks to viewport"
className="px-2.5 py-1.5 text-[11px] font-bold text-indigo-600 hover:text-indigo-700 hover:bg-indigo-50 border border-indigo-100 hover:border-indigo-200 rounded-lg transition-colors cursor-pointer"
>
Fit
</button>
</div>
</>
)}
Expand Down
Loading