-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathExample.tsx
177 lines (173 loc) · 4.98 KB
/
Example.tsx
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import React, { Fragment } from 'react';
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
import rehypePrettyCode from 'rehype-pretty-code';
import MDEditor, { MDEditorProps } from '@uiw/react-md-editor';
import styled from 'styled-components';
const Toolbar = styled.div`
padding-top: 10px;
text-align: center;
`;
let count = 1;
const Example = (props = {} as { mdStr: string }) => {
const [state, setVisible] = React.useState<MDEditorProps>({
visibleDragbar: true,
hideToolbar: true,
overflow: true,
highlightEnable: true,
enableScroll: true,
value: props.mdStr || '',
preview: 'live',
toolbarBottom: false,
});
const upPreview = (e: React.ChangeEvent<HTMLInputElement>) => {
setVisible({ ...state, preview: e.target.value as MDEditorProps['preview'] });
};
const updateHandle = (str: string) => {
setVisible({ ...state, value: str });
};
return (
<Fragment>
<MDEditor
autoFocus
value={state.value}
overflow={state.overflow}
previewOptions={{
rehypePlugins: [
[
rehypeSanitize,
{
...defaultSchema,
attributes: {
...defaultSchema.attributes,
span: [
// @ts-ignore
...(defaultSchema.attributes.span || []),
// List of all allowed tokens:
['className'],
],
code: [['className']],
},
},
],
rehypePrettyCode,
],
}}
height={400}
highlightEnable={state.highlightEnable}
hideToolbar={!state.hideToolbar}
enableScroll={state.enableScroll}
toolbarBottom={state.toolbarBottom}
visibleDragbar={state.visibleDragbar}
textareaProps={{
placeholder: 'Please enter Markdown text',
}}
preview={state.preview}
onChange={(newValue = '') => {
setVisible({ ...state, value: newValue });
}}
/>
<Toolbar>
<label>
<input
type="checkbox"
checked={state.visibleDragbar}
onChange={(e) => {
setVisible({ ...state, visibleDragbar: e.target.checked });
}}
/>
{state.visibleDragbar ? 'Show' : 'Hidden'} Drag Bar
</label>
<label>
<input
type="checkbox"
checked={state.highlightEnable}
onChange={(e) => {
setVisible({ ...state, highlightEnable: e.target.checked });
}}
/>
{state.highlightEnable ? 'Enable' : 'Unenable'} highlight
</label>
<label>
<input
type="checkbox"
checked={state.enableScroll}
onChange={(e) => {
setVisible({ ...state, enableScroll: e.target.checked });
}}
/>
{state.enableScroll ? 'Enable' : 'Unenable'} Scroll
</label>
<label>
<input
type="checkbox"
checked={state.hideToolbar}
onChange={(e) => {
setVisible({ ...state, hideToolbar: e.target.checked });
}}
/>
{state.hideToolbar ? 'Show' : 'Hidden'} ToolBar
</label>
<label>
<input
type="checkbox"
checked={!state.toolbarBottom}
onChange={(e) => {
setVisible({ ...state, toolbarBottom: !e.target.checked });
}}
/>
{!state.toolbarBottom ? 'Top' : 'Bottom'} ToolBar
</label>
<label>
<input
type="checkbox"
checked={state.overflow}
onChange={(e) => {
setVisible({ ...state, overflow: e.target.checked });
}}
/>
overflow
</label>
<br />
<label>
<input type="radio" name="preview" value="edit" checked={state.preview === 'edit'} onChange={upPreview} />
Edit
</label>
<label>
<input type="radio" name="preview" value="live" checked={state.preview === 'live'} onChange={upPreview} />
Live Preview
</label>
<label>
<input
type="radio"
name="preview"
value="preview"
checked={state.preview === 'preview'}
onChange={upPreview}
/>
Preview
</label>
<button
type="button"
style={{ marginLeft: 10 }}
onClick={() => {
count += 1;
updateHandle(`## Test ${count}`);
}}
>
Set Value
</button>
<button
type="button"
disabled={!state.value}
style={{ marginLeft: 10 }}
onClick={() => {
updateHandle(undefined as any);
}}
>
Clear
</button>
</Toolbar>
</Fragment>
);
};
export default Example;