|
| 1 | +import Editor from '@monaco-editor/react'; |
| 2 | +import { Card } from 'antd'; |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import { Infographic } from './Infographic'; |
| 5 | +import { getStoredValues, setStoredValues } from './utils/storage'; |
| 6 | + |
| 7 | +const STORAGE_KEY = 'playground-code'; |
| 8 | + |
| 9 | +const DEFAULT_CODE = `infographic list-row-horizontal-icon-arrow |
| 10 | +data |
| 11 | + title 信息图示例 |
| 12 | + desc 粘贴或编辑左侧语法,右侧实时预览 |
| 13 | + lists |
| 14 | + - label 项目一 |
| 15 | + value 12.3 |
| 16 | + desc 描述文字 |
| 17 | + icon company-021_v1_lineal |
| 18 | + - label 项目二 |
| 19 | + value 8.6 |
| 20 | + desc 描述文字 |
| 21 | + icon antenna-bars-5_v1_lineal |
| 22 | + - label 项目三 |
| 23 | + value 15.1 |
| 24 | + desc 描述文字 |
| 25 | + icon achievment-050_v1_lineal |
| 26 | +`; |
| 27 | + |
| 28 | +export const Playground = () => { |
| 29 | + const [code, setCode] = useState(() => { |
| 30 | + const saved = getStoredValues<{ code: string }>(STORAGE_KEY); |
| 31 | + return saved?.code || DEFAULT_CODE; |
| 32 | + }); |
| 33 | + const [options, setOptions] = useState(code); |
| 34 | + |
| 35 | + // Debounce: update preview 500ms after last edit, also persist |
| 36 | + useEffect(() => { |
| 37 | + const timer = setTimeout(() => { |
| 38 | + setOptions(code); |
| 39 | + setStoredValues(STORAGE_KEY, { code }); |
| 40 | + }, 500); |
| 41 | + return () => clearTimeout(timer); |
| 42 | + }, [code]); |
| 43 | + |
| 44 | + return ( |
| 45 | + <div |
| 46 | + style={{ |
| 47 | + display: 'flex', |
| 48 | + gap: 16, |
| 49 | + padding: 16, |
| 50 | + flex: 1, |
| 51 | + overflow: 'hidden', |
| 52 | + }} |
| 53 | + > |
| 54 | + <div style={{ width: 420, display: 'flex', flexDirection: 'column' }}> |
| 55 | + <Card |
| 56 | + title="语法输入" |
| 57 | + size="small" |
| 58 | + style={{ flex: 1, display: 'flex', flexDirection: 'column' }} |
| 59 | + styles={{ |
| 60 | + body: { |
| 61 | + flex: 1, |
| 62 | + display: 'flex', |
| 63 | + flexDirection: 'column', |
| 64 | + overflow: 'hidden', |
| 65 | + }, |
| 66 | + }} |
| 67 | + > |
| 68 | + <div style={{ flex: 1 }}> |
| 69 | + <Editor |
| 70 | + height="100%" |
| 71 | + defaultLanguage="plaintext" |
| 72 | + value={code} |
| 73 | + theme="vs-dark" |
| 74 | + options={{ |
| 75 | + minimap: { enabled: false }, |
| 76 | + fontSize: 13, |
| 77 | + lineNumbers: 'on', |
| 78 | + scrollBeyondLastLine: false, |
| 79 | + wordWrap: 'on', |
| 80 | + }} |
| 81 | + onChange={(value) => setCode(value || '')} |
| 82 | + /> |
| 83 | + </div> |
| 84 | + </Card> |
| 85 | + </div> |
| 86 | + |
| 87 | + <div style={{ flex: 1, overflow: 'auto' }}> |
| 88 | + <Card title="预览" size="small" style={{ height: '100%' }}> |
| 89 | + <Infographic options={options} /> |
| 90 | + </Card> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}; |
0 commit comments