|
1 | 1 | import React, { useEffect, useState, useRef } from "react";
|
2 | 2 | import mermaid from "mermaid";
|
3 |
| -import { Info } from "@phosphor-icons/react"; |
| 3 | +import { Download, Info } from "@phosphor-icons/react"; |
| 4 | +import { Button } from "@/components/ui/button"; |
4 | 5 |
|
5 | 6 | interface MermaidProps {
|
6 | 7 | chart: string;
|
@@ -41,8 +42,71 @@ const Mermaid: React.FC<MermaidProps> = ({ chart }) => {
|
41 | 42 | mermaid.contentLoaded();
|
42 | 43 | }, []);
|
43 | 44 |
|
| 45 | + const handleExport = async () => { |
| 46 | + if (!elementRef.current) return; |
| 47 | + |
| 48 | + try { |
| 49 | + // Get SVG element |
| 50 | + const svgElement = elementRef.current.querySelector("svg"); |
| 51 | + if (!svgElement) throw new Error("No SVG found"); |
| 52 | + |
| 53 | + // Get SVG viewBox dimensions |
| 54 | + const viewBox = svgElement.getAttribute("viewBox")?.split(" ").map(Number) || [ |
| 55 | + 0, 0, 0, 0, |
| 56 | + ]; |
| 57 | + const [, , viewBoxWidth, viewBoxHeight] = viewBox; |
| 58 | + |
| 59 | + // Create canvas with viewBox dimensions |
| 60 | + const canvas = document.createElement("canvas"); |
| 61 | + const scale = 2; // For better resolution |
| 62 | + canvas.width = viewBoxWidth * scale; |
| 63 | + canvas.height = viewBoxHeight * scale; |
| 64 | + const ctx = canvas.getContext("2d"); |
| 65 | + if (!ctx) throw new Error("Failed to get canvas context"); |
| 66 | + |
| 67 | + // Convert SVG to data URL |
| 68 | + const svgData = new XMLSerializer().serializeToString(svgElement); |
| 69 | + const svgBlob = new Blob([svgData], { type: "image/svg+xml;charset=utf-8" }); |
| 70 | + const svgUrl = URL.createObjectURL(svgBlob); |
| 71 | + |
| 72 | + // Create and load image |
| 73 | + const img = new Image(); |
| 74 | + img.src = svgUrl; |
| 75 | + |
| 76 | + await new Promise((resolve, reject) => { |
| 77 | + img.onload = () => { |
| 78 | + // Scale context for better resolution |
| 79 | + ctx.scale(scale, scale); |
| 80 | + ctx.drawImage(img, 0, 0, viewBoxWidth, viewBoxHeight); |
| 81 | + |
| 82 | + canvas.toBlob((blob) => { |
| 83 | + if (!blob) { |
| 84 | + reject(new Error("Failed to create blob")); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const url = URL.createObjectURL(blob); |
| 89 | + const a = document.createElement("a"); |
| 90 | + a.href = url; |
| 91 | + a.download = `mermaid-diagram-${Date.now()}.png`; |
| 92 | + a.click(); |
| 93 | + |
| 94 | + // Cleanup |
| 95 | + URL.revokeObjectURL(url); |
| 96 | + URL.revokeObjectURL(svgUrl); |
| 97 | + resolve(true); |
| 98 | + }, "image/png"); |
| 99 | + }; |
| 100 | + |
| 101 | + img.onerror = () => reject(new Error("Failed to load SVG")); |
| 102 | + }); |
| 103 | + } catch (error) { |
| 104 | + console.error("Error exporting diagram:", error); |
| 105 | + setMermaidError("Failed to export diagram"); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
44 | 109 | useEffect(() => {
|
45 |
| - console.log("Rendering mermaid chart:", chart); |
46 | 110 | if (elementRef.current) {
|
47 | 111 | elementRef.current.removeAttribute("data-processed");
|
48 | 112 |
|
@@ -79,6 +143,12 @@ const Mermaid: React.FC<MermaidProps> = ({ chart }) => {
|
79 | 143 | {chart}
|
80 | 144 | </div>
|
81 | 145 | )}
|
| 146 | + {!mermaidError && ( |
| 147 | + <Button onClick={handleExport} variant={"secondary"} className="mt-3"> |
| 148 | + <Download className="w-5 h-5" /> |
| 149 | + Export as PNG |
| 150 | + </Button> |
| 151 | + )} |
82 | 152 | </div>
|
83 | 153 | );
|
84 | 154 | };
|
|
0 commit comments