Skip to content

Commit

Permalink
website: Update example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 7, 2021
1 parent 7405871 commit 7eb7b70
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"@types/testing-library__jest-dom": "5.14.1",
"@uiw/react-github-corners": "1.5.3",
"@uiw/react-markdown-preview": "3.3.5",
"@uiw/react-loader": "4.9.7",
"code-example": "3.3.1",
"compile-less-cli": "1.8.9",
"husky": "7.0.2",
Expand Down
71 changes: 44 additions & 27 deletions website/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,54 @@ import React, { useEffect, useState } from 'react';
import MarkdownPreview from '@uiw/react-markdown-preview';
import GitHubCorners from '@uiw/react-github-corners';
import rehypeAttr from 'rehype-attr';
// @ts-ignore
import Loader from '@uiw/react-loader';
import exts from 'code-example/ext.json';
import TextareaCodeEditor from '../';
import MDStr from '../README.md';
import './App.css';

const App: React.FC = () => {
const [value, setValue] = useState('');
const [language, setLanguage] = useState('jsx');
const [lang, setLang] = useState('jsx');
const useFetch = (language: string) => {
const [code, setCode] = useState('');
const [loading, setLoading] = useState(false);
const [lang, setLang] = useState(language);
const [error, setError] = useState<any>(null);

useEffect(() => {
if (language) {
import(`code-example/txt/sample.${language}.txt`)
.then((code) => {
setValue(code.default || '');
let str = language;
if (/^(mysql|pgsql)$/.test(language)) {
str = 'sql';
}
if (/^(objective-c)$/.test(language)) {
str = 'objc';
}
if (/^(vue)$/.test(language)) {
str = 'html';
}
setLang(str);
})
.catch((err) => {
setValue('');
});
}
setLoading(true);
const fetchData = async () => {
try {
const codeStr = await import(`code-example/txt/sample.${language}.txt`);
setCode(codeStr.default);

let str = language;
if (/^(mysql|pgsql)$/.test(language)) {
str = 'sql';
}
if (/^(objective-c)$/.test(language)) {
str = 'objc';
}
if (/^(vue)$/.test(language)) {
str = 'html';
}
setLang(str);
setLoading(false);
} catch (error) {
console.log(error);
setLoading(false);
setError(error);
setCode('');
}
};
fetchData();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [language]);

return { lang, loading, code, setCode, error };
};

const App: React.FC = () => {
const [language, setLanguage] = useState('jsx');
const { lang, loading, code, setCode } = useFetch(language);
// @ts-ignore
const version = VERSION;
return (
Expand All @@ -46,7 +62,7 @@ const App: React.FC = () => {
<div className="App-editor">
<TextareaCodeEditor
autoFocus
value={value}
value={code}
language={lang}
minHeight={80}
placeholder={`Please enter ${(language || '').toLocaleUpperCase()} code.`}
Expand All @@ -55,7 +71,7 @@ const App: React.FC = () => {
fontSize: 14,
fontFamily: 'ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace',
}}
onChange={(evn) => setValue(evn.target.value)}
onChange={(evn) => setCode(evn.target.value)}
/>
</div>
<div className="App-tools" style={{ marginTop: 5 }}>
Expand All @@ -69,6 +85,7 @@ const App: React.FC = () => {
);
})}
</select>
<Loader loading={loading} />
</div>
<MarkdownPreview source={MDStr} className="info" rehypePlugins={[[rehypeAttr, { properties: 'attr' }]]} />
</div>
Expand Down

0 comments on commit 7eb7b70

Please sign in to comment.