/
App.tsx
261 lines (254 loc) · 9.44 KB
/
App.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import React, { useEffect, useState } from 'react';
import GitHubCorners from '@uiw/react-github-corners';
import Github from '@uiw/react-shields/esm/github';
import Npm from '@uiw/react-shields/esm/npm';
import MarkdownPreview from '@uiw/react-markdown-preview';
import rehypeAttrs from 'rehype-attr';
import { javascript } from '@codemirror/lang-javascript';
import { html } from '@codemirror/lang-html';
import { css } from '@codemirror/lang-css';
import { json } from '@codemirror/lang-json';
import { python } from '@codemirror/lang-python';
import { markdown } from '@codemirror/lang-markdown';
import { xml } from '@codemirror/lang-xml';
import { sql, MySQL, PostgreSQL } from '@codemirror/lang-sql';
import { java } from '@codemirror/lang-java';
import { rust } from '@codemirror/lang-rust';
import { cpp } from '@codemirror/lang-cpp';
import { lezer } from '@codemirror/lang-lezer';
import { php } from '@codemirror/lang-php';
import { StreamLanguage } from '@codemirror/stream-parser';
import { go } from '@codemirror/legacy-modes/mode/go';
import { ruby } from '@codemirror/legacy-modes/mode/ruby';
import { shell } from '@codemirror/legacy-modes/mode/shell';
import { lua } from '@codemirror/legacy-modes/mode/lua';
import { swift } from '@codemirror/legacy-modes/mode/swift';
import { tcl } from '@codemirror/legacy-modes/mode/tcl';
import { yaml } from '@codemirror/legacy-modes/mode/yaml';
import { vb } from '@codemirror/legacy-modes/mode/vb';
import { powerShell } from '@codemirror/legacy-modes/mode/powershell';
import { brainfuck } from '@codemirror/legacy-modes/mode/brainfuck';
// import { clike } from '@codemirror/legacy-modes/mode/clike';
import { stylus } from '@codemirror/legacy-modes/mode/stylus';
import { erlang } from '@codemirror/legacy-modes/mode/erlang';
import { nginx } from '@codemirror/legacy-modes/mode/nginx';
import { perl } from '@codemirror/legacy-modes/mode/perl';
import { pascal } from '@codemirror/legacy-modes/mode/pascal';
import { liveScript } from '@codemirror/legacy-modes/mode/livescript';
import { scheme } from '@codemirror/legacy-modes/mode/scheme';
import { toml } from '@codemirror/legacy-modes/mode/toml';
import { vbScript } from '@codemirror/legacy-modes/mode/vbscript';
import { clojure } from '@codemirror/legacy-modes/mode/clojure';
import { coffeeScript } from '@codemirror/legacy-modes/mode/coffeescript';
import { dockerFile } from '@codemirror/legacy-modes/mode/dockerfile';
import { julia } from '@codemirror/legacy-modes/mode/julia';
import { r } from '@codemirror/legacy-modes/mode/r';
import logo from './logo.png';
import styles from './App.module.less';
import DocumentStr from '../README.md';
import CodeMirror, { ReactCodeMirrorProps, Extension } from '..';
import { Select } from './Select';
const langs: Record<string, any> = {
javascript,
jsx: () => javascript({ jsx: true }),
typescript: () => javascript({ typescript: true }),
tsx: () => javascript({ jsx: true, typescript: true }),
json,
html,
css,
python,
markdown,
xml,
sql,
mysql: () => sql({ dialect: MySQL }),
pgsql: () => sql({ dialect: PostgreSQL }),
java,
rust,
cpp,
lezer,
php,
go: () => StreamLanguage.define(go),
shell: () => StreamLanguage.define(shell),
lua: () => StreamLanguage.define(lua),
swift: () => StreamLanguage.define(swift),
tcl: () => StreamLanguage.define(tcl),
yaml: () => StreamLanguage.define(yaml),
vb: () => StreamLanguage.define(vb),
powershell: () => StreamLanguage.define(powerShell),
brainfuck: () => StreamLanguage.define(brainfuck),
stylus: () => StreamLanguage.define(stylus),
erlang: () => StreamLanguage.define(erlang),
nginx: () => StreamLanguage.define(nginx),
perl: () => StreamLanguage.define(perl),
ruby: () => StreamLanguage.define(ruby),
pascal: () => StreamLanguage.define(pascal),
livescript: () => StreamLanguage.define(liveScript),
scheme: () => StreamLanguage.define(scheme),
toml: () => StreamLanguage.define(toml),
vbscript: () => StreamLanguage.define(vbScript),
clojure: () => StreamLanguage.define(clojure),
coffeescript: () => StreamLanguage.define(coffeeScript),
julia: () => StreamLanguage.define(julia),
dockerfile: () => StreamLanguage.define(dockerFile),
r: () => StreamLanguage.define(r),
// clike: () => StreamLanguage.define(clike),
// clike: () => clike({ }),
};
const hyperlink: {
href: string;
label: string;
style?: React.CSSProperties;
}[] = [
{
href: 'https://www.npmjs.com/package/@uiw/react-codemirror',
label: 'View on NPM',
},
{
href: 'https://codemirror.net/6/docs/',
label: 'CM Documentation',
},
{
href: 'https://github.com/codemirror/codemirror.next/',
label: 'CodeMirror GitHub',
},
{
href: 'https://raw.githack.com/uiwjs/react-codemirror/doc3/index.html',
label: 'V3 Doc',
style: { color: 'red' },
},
];
const themeOptions = ['dark', 'light'];
const heightOptions = ['auto', '200px', '300px', '500px'];
let count = 0;
export default function App() {
const [mode, setMode] = useState('javascript');
const [placeholder, setPlaceholder] = useState('Please enter the code.');
const [autofocus, setAutofocus] = useState(false);
const [editable, setEditable] = useState(true);
const [theme, setTheme] = useState<ReactCodeMirrorProps['theme']>('light');
const [code, setCode] = useState('');
const [extensions, setExtensions] = useState<Extension[]>();
const [height, setHeight] = useState('500px');
function handleLangChange(lang: string) {
try {
import(`code-example/txt/sample.${lang.toLocaleLowerCase()}.txt`)
.then((data) => {
setCode(data.default);
if (langs[lang]) {
setExtensions([langs[lang]()]);
}
setMode(lang);
})
.catch((err) => {
if (langs[lang]) {
setExtensions([langs[lang]()]);
} else {
setExtensions([]);
}
setMode(lang);
setCode('');
});
if (lang === 'html') {
}
} catch (error) {}
}
useEffect(() => {
handleLangChange('javascript');
}, []);
// @ts-ignore
const version = VERSION;
return (
<div className={styles.App}>
<GitHubCorners fixed target="__blank" zIndex={10} href="https://github.com/uiwjs/react-codemirror" />
<header className={styles.AppHeader}>
<img src={logo} className={styles.AppLogo} alt="logo" />
<h1 className={styles.AppTitle}>
React-CodeMirror <sup>v{version}</sup>
</h1>
<p className={styles.content}>CodeMirror component for React. </p>
<div className={styles.button}>
{hyperlink.map(({ href, label, style }, idx) => {
return (
<a key={idx} target="_blank" rel="noopener noreferrer" href={href} style={style}>
{label}
</a>
);
})}
</div>
<div className={styles.select}></div>
</header>
<CodeMirror
value={code}
height={height}
theme={theme}
editable={editable}
extensions={extensions}
autoFocus={autofocus}
className={styles.codemirror}
placeholder={placeholder}
onChange={(value) => setCode(value)}
style={{
maxWidth: '995px',
margin: '-18px auto 0 auto',
position: 'relative',
zIndex: 999,
}}
/>
<div className={styles.select}>
<Select
label="Lang"
options={Object.keys(langs)}
value={mode}
onChange={(evn) => handleLangChange(evn.target.value)}
/>
<Select
label="Theme"
options={themeOptions}
value={theme}
onChange={(evn) => setTheme(evn.target.value as ReactCodeMirrorProps['theme'])}
/>
<Select label="Height" options={heightOptions} value={height} onChange={(evn) => setHeight(evn.target.value)} />
<button
onClick={() => {
count++;
setCode(`console.log("Hello World! ${count}")`);
}}
>
change code
</button>
<label>
<input type="checkbox" checked={autofocus} onChange={(evn) => setAutofocus(evn.target.checked)} />
autoFocus
</label>
<label>
<input type="checkbox" checked={editable} onChange={(evn) => setEditable(evn.target.checked)} />
editable
</label>
<label>
placeholder:
<input type="text" value={placeholder} onChange={(evn) => setPlaceholder(evn.target.value)} />
</label>
</div>
<MarkdownPreview
className={styles.markdown}
rehypePlugins={[[rehypeAttrs, { properties: 'attr' }]]}
source={DocumentStr.replace(/([\s\S]*)<!--dividing-->/, '')}
/>
<div className={styles.footer}>
<Github user="uiwjs" repo="react-codemirror">
<Github.Social type="forks" href="https://github.com/uiwjs/react-codemirror" />
<Github.Social type="stars" href="https://github.com/uiwjs/react-codemirror/stargazers" />
<Github.Social type="watchers" href="https://github.com/uiwjs/react-codemirror/watchers" />
<a href="https://www.npmjs.com/package/@uiw/react-codemirror" target="__blank">
<img src="https://img.shields.io/npm/dm/@uiw/react-codemirror.svg?style=flat" alt="NPM Downloads" />
</a>
</Github>
<Npm.Version
scope="@uiw"
packageName="react-shields"
href="https://www.npmjs.com/package/@uiw/react-codemirror"
/>
</div>
</div>
);
}