Skip to content

Commit

Permalink
feat/full-diagram (#820)
Browse files Browse the repository at this point in the history
* feat: download full node graph

* Create rare-papayas-lay.md

---------

Co-authored-by: David Boyne <[email protected]>
  • Loading branch information
carlosallexandre and boyney123 authored Sep 21, 2024
1 parent f189715 commit c8118b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-papayas-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@eventcatalog/core": patch
---

fix(core): downloading images in visualizer now shows the whole diagram
18 changes: 11 additions & 7 deletions src/components/MDX/NodeGraph/DownloadButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Panel, useReactFlow, getRectOfNodes } from 'reactflow';
import { Panel, useReactFlow, getNodesBounds, getViewportForBounds } from 'reactflow';
import { toPng } from 'html-to-image';
import { DocumentArrowDownIcon } from '@heroicons/react/24/outline';

Expand All @@ -16,7 +16,10 @@ const imageHeight = 768;
function DownloadButton({ filename, addPadding = true }: { filename?: string; addPadding?: boolean }) {
const { getNodes } = useReactFlow();
const onClick = () => {
const nodesBounds = getRectOfNodes(getNodes());
const nodesBounds = getNodesBounds(getNodes());
const width = imageWidth > nodesBounds.width ? imageWidth : nodesBounds.width;
const height = imageHeight > nodesBounds.height ? imageHeight : nodesBounds.height;
const viewport = getViewportForBounds(nodesBounds, width, height, 0.5, 2);

// Hide the button
// @ts-ignore
Expand All @@ -25,13 +28,14 @@ function DownloadButton({ filename, addPadding = true }: { filename?: string; ad
document.querySelector('.react-flow__controls').style.display = 'none';

// @ts-ignore
toPng(document.querySelector('.react-flow'), {
toPng(document.querySelector('.react-flow__viewport'), {
backgroundColor: '#f1f1f1',
width: imageWidth * 1.2,
height: imageHeight,
width,
height,
style: {
width: imageWidth * 1.2,
height: imageHeight,
width,
height,
transform: `translate(${viewport.x}px, ${viewport.y}px) scale(${viewport.zoom})`,
},
}).then((dataUrl: string) => {
downloadImage(dataUrl, filename);
Expand Down

0 comments on commit c8118b7

Please sign in to comment.