-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
133 lines (116 loc) · 4.42 KB
/
Copy pathApp.tsx
File metadata and controls
133 lines (116 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import React, { useState, useRef, useCallback } from 'react';
import { ReactZoomPanPinchContentRef } from 'react-zoom-pan-pinch';
import html2canvas from 'html2canvas';
import Toolbar from './components/Toolbar';
import EditorPanel from './components/EditorPanel';
import DiagramCanvas from './components/DiagramCanvas';
import { INITIAL_CODE } from './constants';
import { ViewMode } from './types';
import { fixMermaidCode } from './services/geminiService';
const App: React.FC = () => {
const [code, setCode] = useState<string>(INITIAL_CODE);
const [error, setError] = useState<string | null>(null);
const [viewMode, setViewMode] = useState<ViewMode>(ViewMode.Split);
const [isFixing, setIsFixing] = useState<boolean>(false);
const [zoomControls, setZoomControls] = useState<ReactZoomPanPinchContentRef | null>(null);
const [zoomScale, setZoomScale] = useState<number>(1);
const handleError = useCallback((msg: string) => {
setError(msg);
}, []);
const handleSuccess = useCallback(() => {
setError(null);
}, []);
const handleZoomChange = useCallback((scale: number) => {
setZoomScale(scale);
}, []);
const handleFix = async () => {
if (!code || !error) return;
setIsFixing(true);
try {
const fixedCode = await fixMermaidCode(code, error);
setCode(fixedCode);
// Wait a moment for render, then clear error if successful
setTimeout(() => {
if (!document.querySelector('.error-banner')) {
setError(null);
}
}, 500);
} catch (err) {
alert("Failed to auto-fix code. Please check your API key or internet connection.");
} finally {
setIsFixing(false);
}
};
const handleDownload = async () => {
const svgElement = document.querySelector('.mermaid svg') as SVGSVGElement;
if (!svgElement) return;
// Method 1: Get bounding box for better cropping
const bbox = svgElement.getBBox();
const width = bbox.width + 40; // padding
const height = bbox.height + 40;
// Create a canvas via html2canvas (captures styles better than raw SVG conversion sometimes)
// Or simpler: Convert SVG string to Blob
const svgData = new XMLSerializer().serializeToString(svgElement);
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const img = new Image();
const svgBlob = new Blob([svgData], { type: "image/svg+xml;charset=utf-8" });
const url = URL.createObjectURL(svgBlob);
img.onload = () => {
canvas.width = width * 2; // High res
canvas.height = height * 2;
if(ctx) {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, width * 2, height * 2);
const pngUrl = canvas.toDataURL("image/png");
const downloadLink = document.createElement("a");
downloadLink.href = pngUrl;
downloadLink.download = `diagram-${Date.now()}.png`;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
URL.revokeObjectURL(url);
};
img.src = url;
};
return (
<div className="flex flex-col h-screen w-screen bg-white text-gray-900 overflow-hidden font-sans">
<Toolbar
viewMode={viewMode}
setViewMode={setViewMode}
onDownload={handleDownload}
zoomControls={zoomControls}
scale={zoomScale}
/>
<div className="flex-1 flex overflow-hidden">
{/* Editor Pane */}
{(viewMode === ViewMode.Split || viewMode === ViewMode.EditorOnly) && (
<div className={`${viewMode === ViewMode.Split ? 'w-1/3 min-w-[350px]' : 'w-full'} h-full transition-all duration-300 ease-in-out z-10 shadow-xl`}>
<EditorPanel
code={code}
setCode={setCode}
error={error}
isFixing={isFixing}
onFix={handleFix}
/>
</div>
)}
{/* Preview Pane */}
{(viewMode === ViewMode.Split || viewMode === ViewMode.ViewOnly) && (
<div className="flex-1 h-full relative z-0">
<DiagramCanvas
code={code}
onError={handleError}
onSuccess={handleSuccess}
setZoomControls={setZoomControls}
onZoomChange={handleZoomChange}
/>
</div>
)}
</div>
</div>
);
};
export default App;