-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathPlayground.tsx
More file actions
88 lines (81 loc) · 2.36 KB
/
Copy pathPlayground.tsx
File metadata and controls
88 lines (81 loc) · 2.36 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
import Editor from '@monaco-editor/react';
import { Card } from 'antd';
import { useEffect, useState } from 'react';
import { Infographic } from './Infographic';
import { getStoredValues, setStoredValues } from './utils/storage';
const STORAGE_KEY = 'playground-code';
const DEFAULT_CODE = `infographic list-row-horizontal-icon-arrow
data
title 信息图示例
desc 粘贴或编辑左侧语法,右侧实时预览
lists
- label 项目一
value 12.3
desc 描述文字
icon company-021_v1_lineal
- label 项目二
value 8.6
desc 描述文字
icon antenna-bars-5_v1_lineal
- label 项目三
value 15.1
desc 描述文字
icon achievment-050_v1_lineal
`;
export const Playground = () => {
const [code, setCode] = useState(DEFAULT_CODE);
const [options, setOptions] = useState(DEFAULT_CODE);
// Hydrate from localStorage on mount
useEffect(() => {
const saved = getStoredValues<{ code: string }>(STORAGE_KEY);
if (saved?.code) {
setCode(saved.code);
setOptions(saved.code);
}
}, []);
// Debounce: update preview 500ms after last edit, also persist
useEffect(() => {
const timer = setTimeout(() => {
setOptions(code);
setStoredValues(STORAGE_KEY, { code });
}, 500);
return () => clearTimeout(timer);
}, [code]);
return (
<div
style={{
display: 'flex',
gap: 16,
padding: 16,
flex: 1,
overflow: 'hidden',
}}
>
<div style={{ width: 420, display: 'flex', flexDirection: 'column' }}>
<Card title="语法输入" size="small" style={{ flex: 1 }}>
<div style={{ height: 'calc(100vh - 160px)' }}>
<Editor
height="100%"
defaultLanguage="plaintext"
value={code}
theme="vs-dark"
options={{
minimap: { enabled: false },
fontSize: 13,
lineNumbers: 'on',
scrollBeyondLastLine: false,
wordWrap: 'on',
}}
onChange={(value) => setCode(value || '')}
/>
</div>
</Card>
</div>
<div style={{ flex: 1, overflow: 'auto' }}>
<Card title="预览" size="small" style={{ height: '100%' }}>
<Infographic options={options} />
</Card>
</div>
</div>
);
};