1
- import { CheckOutlined , CopyOutlined } from '@ant-design/icons' ;
2
1
import React , { memo , useRef , useState } from 'react' ;
3
2
import { ErrorBoundary } from 'react-error-boundary' ;
4
3
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter' ;
5
4
import json from 'react-syntax-highlighter/dist/esm/languages/hljs/json' ;
6
5
import { magula } from 'react-syntax-highlighter/dist/esm/styles/hljs' ;
6
+ import { Check , Copy , ZoomIn , ZoomOut } from './icon' ;
7
7
import Loading from './Loading' ;
8
8
import {
9
9
ChartWrapper ,
10
- CopyButton ,
11
10
ErrorMessage ,
12
11
GlobalStyles ,
13
12
StyledGPTVis ,
@@ -17,13 +16,23 @@ import {
17
16
TabHeader ,
18
17
TabLeftGroup ,
19
18
TabRightGroup ,
19
+ TextButton ,
20
20
} from './styles' ;
21
21
import type { ChartComponents , ChartJson , ComponentErrorRender , ErrorRender } from './type' ;
22
22
import { handleCopyCode } from './utils' ;
23
23
24
24
// 注册 JSON 语言支持
25
25
SyntaxHighlighter . registerLanguage ( 'json' , json ) ;
26
26
27
+ const G6List = [
28
+ 'mind-map' ,
29
+ 'fishbone-diagram' ,
30
+ 'flow-diagram' ,
31
+ 'organization-chart' ,
32
+ 'network-graph' ,
33
+ 'indented-tree' ,
34
+ ] ;
35
+
27
36
type RenderVisChartProps = {
28
37
content : string ;
29
38
components : ChartComponents ;
@@ -42,6 +51,7 @@ export const RenderVisChart: React.FC<RenderVisChartProps> = memo(
42
51
const [ activeTab , setActiveTab ] = useState < 'chart' | 'code' > ( 'chart' ) ;
43
52
const [ hasRenderError , setHasRenderError ] = useState ( false ) ;
44
53
const [ copied , setCopied ] = useState ( false ) ;
54
+ const chartRef = useRef < any > ( null ) ;
45
55
let chartJson : ChartJson ;
46
56
47
57
try {
@@ -141,6 +151,25 @@ export const RenderVisChart: React.FC<RenderVisChartProps> = memo(
141
151
}
142
152
} ;
143
153
154
+ const isG6 = G6List . includes ( type ) ;
155
+
156
+ // 缩放功能函数
157
+ const handleZoomOut = ( ) => {
158
+ if ( chartRef . current && typeof chartRef . current . zoomTo === 'function' ) {
159
+ const currentZoom = chartRef . current . getZoom ( ) || 1 ;
160
+ const newZoom = Math . min ( currentZoom * 1.15 , 1.5 ) ;
161
+ chartRef . current . zoomTo ( newZoom ) ;
162
+ }
163
+ } ;
164
+
165
+ const handleZoomIn = ( ) => {
166
+ if ( chartRef . current && typeof chartRef . current . zoomTo === 'function' ) {
167
+ const currentZoom = chartRef . current . getZoom ( ) || 1 ;
168
+ const newZoom = Math . max ( currentZoom / 1.15 , 0.1 ) ;
169
+ chartRef . current . zoomTo ( newZoom ) ;
170
+ }
171
+ } ;
172
+
144
173
// Render the supported chart component with data
145
174
return (
146
175
< TabContainer style = { style } >
@@ -155,13 +184,32 @@ export const RenderVisChart: React.FC<RenderVisChartProps> = memo(
155
184
</ TabLeftGroup >
156
185
157
186
< TabRightGroup >
158
- { activeTab === 'code' && (
187
+ { activeTab === 'chart' ? (
188
+ < >
189
+ { isG6 && (
190
+ < >
191
+ < TextButton
192
+ onClick = { handleZoomIn }
193
+ style = { { width : '24px' , height : '24px' , padding : 0 } }
194
+ >
195
+ < ZoomIn size = { 18 } />
196
+ </ TextButton >
197
+ < TextButton
198
+ onClick = { handleZoomOut }
199
+ style = { { width : '24px' , height : '24px' , padding : 0 } }
200
+ >
201
+ < ZoomOut size = { 18 } />
202
+ </ TextButton >
203
+ </ >
204
+ ) }
205
+ </ >
206
+ ) : (
159
207
< >
160
208
{ /* 复制代码 */ }
161
- < CopyButton onClick = { handleCopy } >
162
- { copied ? < CheckOutlined /> : < CopyOutlined /> }
209
+ < TextButton onClick = { handleCopy } >
210
+ { copied ? < Check /> : < Copy /> }
163
211
{ copied ? '完成' : '复制' }
164
- </ CopyButton >
212
+ </ TextButton >
165
213
</ >
166
214
) }
167
215
</ TabRightGroup >
@@ -185,7 +233,12 @@ export const RenderVisChart: React.FC<RenderVisChartProps> = memo(
185
233
< StyledGPTVis className = "gpt-vis" >
186
234
< GlobalStyles />
187
235
< ChartWrapper >
188
- < ChartComponent { ...chartProps } />
236
+ < ChartComponent
237
+ { ...chartProps }
238
+ onReady = { ( chart : any ) => {
239
+ chartRef . current = chart ;
240
+ } }
241
+ />
189
242
</ ChartWrapper >
190
243
</ StyledGPTVis >
191
244
</ ErrorBoundary >
0 commit comments