@@ -2,48 +2,25 @@ import { getTemplate, getTemplates, ThemeConfig } from '@antv/infographic';
22import Editor from '@monaco-editor/react' ;
33import { Button , Card , Checkbox , ColorPicker , Form , Select } from 'antd' ;
44import { useEffect , useMemo , useState } from 'react' ;
5+ import { getDataByTemplate } from '../../shared/get-template-data' ;
56import { Infographic } from './Infographic' ;
6- import {
7- COMPARE_DATA ,
8- HIERARCHY_DATA ,
9- LIST_DATA ,
10- SWOT_DATA ,
11- WORD_CLOUD_DATA ,
12- } from './data' ;
7+ import { DATA_KEYS , DATASET , DEFAULT_DATA_KEY , type DataKey } from './data' ;
138import { getStoredValues , setStoredValues } from './utils/storage' ;
149
1510const templates = getTemplates ( ) ;
1611const STORAGE_KEY = 'preview-form-values' ;
1712
18- const DATA = {
19- list : { label : '列表数据' , value : LIST_DATA } ,
20- hierarchy : { label : '层级数据' , value : HIERARCHY_DATA } ,
21- compare : { label : '对比数据' , value : COMPARE_DATA } ,
22- swot : { label : 'SWOT 数据' , value : SWOT_DATA } ,
23- wordcloud : { label : '词云数据' , value : WORD_CLOUD_DATA } ,
24- } as const ;
25- const TEMPLATE_DATA_MATCHERS : Array < [ string , keyof typeof DATA ] > = [
26- [ 'hierarchy-' , 'hierarchy' ] ,
27- [ 'compare-' , 'compare' ] ,
28- [ 'swot-' , 'swot' ] ,
29- [ 'chart-wordcloud' , 'wordcloud' ] ,
30- ] ;
31- const getDefaultDataString = ( key : keyof typeof DATA ) =>
32- JSON . stringify ( DATA [ key ] . value , null , 2 ) ;
33- const getDataByTemplate = ( nextTemplate : string ) : keyof typeof DATA => {
34- for ( const [ prefix , dataKey ] of TEMPLATE_DATA_MATCHERS ) {
35- if ( nextTemplate . startsWith ( prefix ) ) {
36- return dataKey ;
37- }
38- }
39- return 'list' ;
40- } ;
13+ const getDefaultDataString = ( key : DataKey ) =>
14+ JSON . stringify ( DATASET [ key ] , null , 2 ) ;
15+
16+ const resolvePreviewDataKey = ( data : unknown ) =>
17+ DATA_KEYS . find ( ( key ) => DATASET [ key ] === data ) ?? DEFAULT_DATA_KEY ;
4118
4219export const Preview = ( ) => {
4320 // Get stored values with validation
4421 const storedValues = getStoredValues < {
4522 template : string ;
46- data : keyof typeof DATA ;
23+ data : DataKey ;
4724 theme : 'light' | 'dark' | 'hand-drawn' ;
4825 colorPrimary : string ;
4926 enablePrimary : boolean ;
@@ -57,7 +34,7 @@ export const Preview = () => {
5734 }
5835
5936 // Validate data
60- const dataKeys = Object . keys ( DATA ) as ( keyof typeof DATA ) [ ] ;
37+ const dataKeys = DATA_KEYS ;
6138 if ( stored . data && ! dataKeys . includes ( stored . data ) ) {
6239 fallbacks . data = dataKeys [ 0 ] ;
6340 }
@@ -66,20 +43,20 @@ export const Preview = () => {
6643 } ) ;
6744
6845 const initialTemplate = storedValues ?. template || templates [ 0 ] ;
69- const initialData = storedValues ?. data || 'list' ;
46+ const initialData = storedValues ?. data || DEFAULT_DATA_KEY ;
7047 const initialTheme = storedValues ?. theme || 'light' ;
7148 const initialColorPrimary = storedValues ?. colorPrimary || '#FF356A' ;
7249 const initialEnablePrimary = storedValues ?. enablePrimary ?? true ;
7350 const initialEnablePalette = storedValues ?. enablePalette || false ;
7451
7552 const [ template , setTemplate ] = useState ( initialTemplate ) ;
76- const [ data , setData ] = useState < keyof typeof DATA > ( initialData ) ;
53+ const [ data , setData ] = useState < DataKey > ( initialData ) ;
7754 const [ theme , setTheme ] = useState < string > ( initialTheme ) ;
7855 const [ colorPrimary , setColorPrimary ] = useState ( initialColorPrimary ) ;
7956 const [ enablePrimary , setEnablePrimary ] = useState ( initialEnablePrimary ) ;
8057 const [ enablePalette , setEnablePalette ] = useState ( initialEnablePalette ) ;
8158 const [ customData , setCustomData ] = useState < string > ( ( ) =>
82- JSON . stringify ( DATA [ initialData ] . value , null , 2 ) ,
59+ JSON . stringify ( DATASET [ initialData ] , null , 2 ) ,
8360 ) ;
8461 const [ dataError , setDataError ] = useState < string > ( '' ) ;
8562
@@ -125,10 +102,14 @@ export const Preview = () => {
125102
126103 const applyTemplate = ( nextTemplate : string ) => {
127104 const nextData = getDataByTemplate ( nextTemplate ) ;
105+ const nextSelection = {
106+ key : resolvePreviewDataKey ( nextData ) ,
107+ data : nextData ,
108+ } ;
128109 setTemplate ( nextTemplate ) ;
129- if ( nextData !== data ) {
130- setData ( nextData ) ;
131- setCustomData ( getDefaultDataString ( nextData ) ) ;
110+ if ( nextSelection . key !== data ) {
111+ setData ( nextSelection . key ) ;
112+ setCustomData ( JSON . stringify ( nextSelection . data , null , 2 ) ) ;
132113 setDataError ( '' ) ;
133114 }
134115 } ;
@@ -161,7 +142,7 @@ export const Preview = () => {
161142 return parsed ;
162143 } catch ( error ) {
163144 setDataError ( error instanceof Error ? error . message : 'Invalid JSON' ) ;
164- return DATA [ data ] . value ;
145+ return DATASET [ data ] ;
165146 }
166147 } , [ customData , data ] ) ;
167148
@@ -251,8 +232,8 @@ export const Preview = () => {
251232 < Form . Item label = "数据" >
252233 < Select
253234 value = { data }
254- options = { Object . entries ( DATA ) . map ( ( [ key , { label } ] ) => ( {
255- label,
235+ options = { DATA_KEYS . map ( ( key ) => ( {
236+ label : key ,
256237 value : key ,
257238 } ) ) }
258239 onChange = { ( value ) => {
0 commit comments