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
41 changes: 30 additions & 11 deletions src/hooks/useDiagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,30 +396,49 @@ export function useDiagram(username: string, repo: string) {
const svgElement = document.querySelector(".mermaid svg");
if (!(svgElement instanceof SVGSVGElement)) return;

const svgElementCopy = svgElement.cloneNode(true) as SVGSVGElement; //Copy the SVG to avoid modifying the original

const removeZoomControls = svgElementCopy.querySelector(
"#svg-pan-zoom-controls",
);
removeZoomControls?.remove();

const svgZoomViewportGroup = svgElementCopy.querySelector(
".svg-pan-zoom_viewport",
);

if (svgZoomViewportGroup) {
svgZoomViewportGroup.removeAttribute("transform");
svgZoomViewportGroup.removeAttribute("style");
}

try {
const canvas = document.createElement("canvas");
const scale = 4;

const bbox = svgElement.getBBox();
const transform = svgElement.getScreenCTM();
if (!transform) return;
const rootElement = svgElement.querySelector("g.root");

if (!rootElement) {
console.error("Could not find root element");
return;
}

const originalBbox = (rootElement as SVGGElement).getBBox();
const width = originalBbox.width;
const height = originalBbox.height;

const width = Math.ceil(bbox.width * transform.a);
const height = Math.ceil(bbox.height * transform.d);
canvas.width = width * scale;
canvas.height = height * scale;
canvas.width = width;
canvas.height = height;

const ctx = canvas.getContext("2d");
if (!ctx) return;

const svgData = new XMLSerializer().serializeToString(svgElement);
const svgData = new XMLSerializer().serializeToString(svgElementCopy);
const img = new Image();

img.onload = () => {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);

ctx.scale(scale, scale);
ctx.drawImage(img, 0, 0, width, height);

const a = document.createElement("a");
Expand All @@ -432,7 +451,7 @@ export function useDiagram(username: string, repo: string) {

img.src =
"data:image/svg+xml;base64," +
btoa(unescape(encodeURIComponent(svgData)));
btoa(decodeURIComponent(encodeURIComponent(svgData))); //removed deprecated unescape()
} catch (error) {
console.error("Error generating PNG:", error);
}
Expand Down