|
1 |
| -import React from 'react'; |
| 1 | +import React, { useState } from 'react'; |
2 | 2 | import { Node, NodeStatus } from '../../models';
|
3 | 3 | import { Step } from '../../models';
|
4 | 4 | import Mermaid from '../atoms/Mermaid';
|
| 5 | +import { ToggleButton, ToggleButtonGroup } from '@mui/material'; |
| 6 | +import { ZoomIn, ZoomOut, RestartAlt } from '@mui/icons-material'; |
5 | 7 |
|
6 | 8 | type onClickNode = (name: string) => void;
|
7 | 9 | export type FlowchartType = 'TD' | 'LR';
|
@@ -29,6 +31,20 @@ const Graph: React.FC<Props> = ({
|
29 | 31 | showIcons = true,
|
30 | 32 | animate = true,
|
31 | 33 | }) => {
|
| 34 | + const [scale, setScale] = useState(1); |
| 35 | + |
| 36 | + const zoomIn = () => { |
| 37 | + setScale((prevScale) => Math.min(prevScale + 0.1, 2)); |
| 38 | + }; |
| 39 | + |
| 40 | + const zoomOut = () => { |
| 41 | + setScale((prevScale) => Math.max(prevScale - 0.1, 0.5)); |
| 42 | + }; |
| 43 | + |
| 44 | + const resetZoom = () => { |
| 45 | + setScale(1); |
| 46 | + }; |
| 47 | + |
32 | 48 | // Calculate width based on flowchart type and graph breadth
|
33 | 49 | const width = React.useMemo(() => {
|
34 | 50 | if (!steps) return '100%';
|
@@ -59,6 +75,17 @@ const Graph: React.FC<Props> = ({
|
59 | 75 | backgroundSize: '20px 20px',
|
60 | 76 | };
|
61 | 77 |
|
| 78 | + const containerStyle = { |
| 79 | + position: 'relative' as const, |
| 80 | + }; |
| 81 | + |
| 82 | + const toggleButtonStyle = { |
| 83 | + position: 'absolute' as const, |
| 84 | + top: '10px', |
| 85 | + right: '10px', |
| 86 | + zIndex: 1, |
| 87 | + }; |
| 88 | + |
62 | 89 | // Define FontAwesome icons for each status with colors and animations
|
63 | 90 | const statusIcons: { [key: number]: string } = {
|
64 | 91 | [NodeStatus.None]:
|
@@ -171,7 +198,38 @@ const Graph: React.FC<Props> = ({
|
171 | 198 | return dat.join('\n');
|
172 | 199 | }, [steps, onClickNode, flowchart, showIcons]);
|
173 | 200 |
|
174 |
| - return <Mermaid style={mermaidStyle} def={graph} />; |
| 201 | + return ( |
| 202 | + <div style={containerStyle}> |
| 203 | + <ToggleButtonGroup |
| 204 | + size="small" |
| 205 | + sx={{ |
| 206 | + ...toggleButtonStyle, |
| 207 | + backgroundColor: 'white', |
| 208 | + '& .MuiToggleButton-root': { |
| 209 | + border: '1px solid rgba(0, 0, 0, 0.12)', |
| 210 | + borderRadius: '4px !important', |
| 211 | + marginRight: '8px', |
| 212 | + padding: '4px 8px', |
| 213 | + color: 'rgba(0, 0, 0, 0.54)', |
| 214 | + '&:hover': { |
| 215 | + backgroundColor: 'rgba(0, 0, 0, 0.04)', |
| 216 | + }, |
| 217 | + }, |
| 218 | + }} |
| 219 | + > |
| 220 | + <ToggleButton value="zoomin" onClick={zoomIn}> |
| 221 | + <ZoomIn fontSize="small" /> |
| 222 | + </ToggleButton> |
| 223 | + <ToggleButton value="zoomout" onClick={zoomOut}> |
| 224 | + <ZoomOut fontSize="small" /> |
| 225 | + </ToggleButton> |
| 226 | + <ToggleButton value="reset" onClick={resetZoom}> |
| 227 | + <RestartAlt fontSize="small" /> |
| 228 | + </ToggleButton> |
| 229 | + </ToggleButtonGroup> |
| 230 | + <Mermaid style={mermaidStyle} def={graph} scale={scale} /> |
| 231 | + </div> |
| 232 | + ); |
175 | 233 | };
|
176 | 234 |
|
177 | 235 | // Function to calculate the maximum breadth of the graph
|
|
0 commit comments