Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frontend): Center initial canvas #8644

Merged
merged 7 commits into from
Nov 14, 2024
25 changes: 25 additions & 0 deletions autogpt_platform/frontend/src/components/Flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,31 @@ const FlowEditor: React.FC<{

const { x, y, zoom } = useViewport();

// Set the initial view port to center the canvas.
useEffect(() => {
if (nodes.length <= 0 || x !== 0 || y !== 0) {
return;
}
const topLeft = { x: Infinity, y: Infinity };
const bottomRight = { x: -Infinity, y: -Infinity };
nodes.forEach((node) => {
const { x, y } = node.position;
topLeft.x = Math.min(topLeft.x, x);
topLeft.y = Math.min(topLeft.y, y);
bottomRight.x = Math.max(bottomRight.x, x + 100);
majdyz marked this conversation as resolved.
Show resolved Hide resolved
bottomRight.y = Math.max(bottomRight.y, y + 100);
});

const centerX = (topLeft.x + bottomRight.x) / 2;
const centerY = (topLeft.y + bottomRight.y) / 2;

setViewport({
x: -centerX + window.innerWidth / 2,
y: -centerY + window.innerHeight / 2,
zoom: 0.8,
});
}, [nodes, setViewport, x, y]);

const addNode = useCallback(
(blockId: string, nodeType: string, hardcodedValues: any = {}) => {
const nodeSchema = availableNodes.find((node) => node.id === blockId);
Expand Down
Loading