Skip to content

Commit a2aa12e

Browse files
committed
doc: Add usage document.
1 parent 0f5ba26 commit a2aa12e

File tree

13 files changed

+444
-30
lines changed

13 files changed

+444
-30
lines changed

README.md

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
React Code Preview
22
---
3+
<!--dividing-->
34

45
Code editor for React. Preview Demo: https://uiwjs.github.io/react-code-preview
56

@@ -22,6 +23,7 @@ const code = `import { Button, Divider, Icon } from 'uiw';
2223
ReactDOM.render(
2324
<div>
2425
<Button type="primary">主要按钮</Button>
26+
<Button type="success">成功按钮</Button>
2527
</div>,
2628
_mount_
2729
);
@@ -43,31 +45,34 @@ class Demo extends React.Component {
4345

4446
### Props
4547

46-
```jsx
47-
/**
48-
* To specify a CSS class, use the className attribute.
49-
*/
50-
className?: string;
51-
/**
52-
* `JSX` source code
53-
*/
54-
code?: string;
55-
/**
56-
* Whether to display the code interface.
57-
*/
58-
noCode?: boolean;
59-
/**
60-
* Is the background white or plaid?
61-
*/
62-
bgWhite?: boolean;
63-
/**
64-
* Whether to display the preview interface.
65-
*/
66-
noPreview?: boolean;
67-
/**
68-
* Dependent component
69-
*/
70-
dependencies?: { [key: string]: any };
48+
```ts
49+
export interface ICodePreviewProps {
50+
prefixCls?: string;
51+
/**
52+
* To specify a CSS class, use the className attribute.
53+
*/
54+
className?: string;
55+
/**
56+
* `JSX` source code
57+
*/
58+
code?: string;
59+
/**
60+
* Whether to display the code interface.
61+
*/
62+
noCode?: boolean;
63+
/**
64+
* Is the background white or plaid?
65+
*/
66+
bgWhite?: boolean;
67+
/**
68+
* Whether to display the preview interface.
69+
*/
70+
noPreview?: boolean;
71+
/**
72+
* Dependent component
73+
*/
74+
dependencies?: { [key: string]: any };
75+
}
7176
```
7277

7378
## Development

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@kkt/plugin-less": "^1.2.0",
4040
"@types/classnames": "^2.2.7",
4141
"@types/codemirror": "0.0.76",
42+
"@types/highlight.js": "^9.12.3",
4243
"@types/react": "^16.8.17",
4344
"@types/react-dom": "^16.8.4",
4445
"@types/react-test-renderer": "^16.8.1",
@@ -47,6 +48,7 @@
4748
"babel-plugin-transform-remove-imports": "^1.0.8",
4849
"babel-plugin-transform-rename-import": "^2.3.0",
4950
"compile-less-cli": "^1.1.5",
51+
"highlight.js": "^9.15.9",
5052
"kkt": "^4.2.0",
5153
"raw-loader": "^3.1.0",
5254
"react": "^16.8.6",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ body, html {
77
.warpper {
88
padding: 20px;
99
height: 300px;
10+
max-width: 995px;
11+
margin: 0 auto;
1012
}
1113

1214
.title {
1315
font-weight: bold;
1416
font-size: 26px;
1517
margin-bottom: 26px;
18+
text-align: center;
1619
}

website/App.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import React from 'react';
2-
import { Switch, Divider, Button } from "uiw";
2+
import { Switch, Divider, Button } from 'uiw';
3+
import Markdown from './Markdown';
4+
import GithubCorner from './GithubCorner';
35
import CodePreview from '../src';
4-
import './App.less';
6+
import DocumentStr from '../README.md'; // @ts-ignore
7+
import styles from './App.module.less';
58

69
const code = `import { Button, Divider, Icon } from 'uiw';
710
811
ReactDOM.render(
912
<div>
1013
<Button type="primary">主要按钮</Button>
14+
<Button type="success">成功按钮</Button>
15+
<Button type="warning">警告按钮</Button>
16+
<Button type="danger">错误按钮</Button>
17+
<Button type="light">亮按钮</Button>
18+
<Button type="dark">暗按钮</Button>
1119
</div>,
1220
_mount_
1321
);
@@ -30,9 +38,12 @@ export default class App extends React.PureComponent<{}, IAppState> {
3038
this.setState({ ...state });
3139
}
3240
render () {
41+
let DocumentStrSource = DocumentStr;
42+
if (DocumentStrSource) DocumentStrSource = DocumentStr.replace(/([\s\S]*)<!--dividing-->/, '');
3343
return (
34-
<div className="warpper">
35-
<h1 className="title">React Code Preview</h1>
44+
<div className={styles.warpper}>
45+
<GithubCorner url="https://uiwjs.github.io/react-code-preview" />
46+
<h1 className={styles.title}>React Code Preview</h1>
3647
<CodePreview
3748
code={code}
3849
dependencies={{ Button }}
@@ -70,6 +81,7 @@ export default class App extends React.PureComponent<{}, IAppState> {
7081
是否显示实例预览 `noPreview`
7182
</Switch>
7283
</div>
84+
<Markdown source={DocumentStrSource} />
7385
</div>
7486
)
7587
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
a.corner {
3+
position: fixed;
4+
top: 0;
5+
right: 0;
6+
svg {
7+
fill:#e1e1e1;
8+
color:#000;
9+
position:absolute;
10+
top:0;
11+
border:0;
12+
right:0;
13+
z-index:99;
14+
width:4.375rem;
15+
height:4.375rem;
16+
}
17+
&:hover :global(.octo-arm) {
18+
animation: octocat-wave 560ms ease-in-out;
19+
}
20+
}
21+
22+
@keyframes octocat-wave {
23+
0%,100% {
24+
transform:rotate(0);
25+
}
26+
20%,60% {
27+
transform:rotate(-25deg);
28+
}
29+
40%,80% {
30+
transform:rotate(10deg);
31+
}
32+
}

website/GithubCorner/index.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import styles from './index.module.less';
3+
4+
export default function GithubCorner({ url }: { url: string }) {
5+
if (!url) {
6+
return null;
7+
}
8+
return (
9+
<a href={url} target="_blank" rel="noopener noreferrer" className={styles.corner}>
10+
<svg viewBox="0 0 250 250">
11+
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z" />
12+
<path fill="currentColor" style={{ transformOrigin: '130px 106px' }} className="octo-arm" d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" />
13+
<path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" className="octo-body" />
14+
</svg>
15+
</a>
16+
);
17+
}

website/Markdown/index.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React, { Component } from 'react';
2+
import ReactMarkdown, { MarkdownAbstractSyntaxTree} from 'react-markdown';
3+
import hljs from 'highlight.js';
4+
import classNames from 'classnames';
5+
import styles from './styles/index.module.less';
6+
7+
hljs.configure({
8+
tabReplace: ' ', // 2 spaces
9+
classPrefix: '', // don't append class prefix
10+
});
11+
12+
export interface IMarkdown {
13+
source?: string;
14+
}
15+
16+
export default class Markdown extends Component<IMarkdown> {
17+
public mdDom = React.createRef<HTMLDivElement>();
18+
componentDidMount() {
19+
if (this.mdDom.current){
20+
const code = this.mdDom.current.getElementsByTagName('code');
21+
for (let i = 0; i < code.length; i += 1) {
22+
if (code[i] && /^language/.test(code[i].className) && code[i].parentNode && (code[i].parentNode as HTMLElement).tagName) {
23+
hljs.highlightBlock(code[i]);
24+
}
25+
}
26+
}
27+
}
28+
render() {
29+
const { source } = this.props;
30+
return (
31+
<div ref={this.mdDom}>
32+
<ReactMarkdown
33+
className={classNames(styles.markdown, 'markdown')}
34+
source={source}
35+
escapeHtml={false}
36+
/>
37+
</div>
38+
);
39+
}
40+
}

0 commit comments

Comments
 (0)