Skip to content

Commit e378721

Browse files
committed
website: add pkg example.
1 parent 2459f09 commit e378721

File tree

9 files changed

+98
-29
lines changed

9 files changed

+98
-29
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
},
3737
"license": "MIT",
3838
"devDependencies": {
39+
"@types/react": "~18.2.0",
40+
"@types/react-dom": "~18.2.0",
3941
"react": "~18.2.0",
4042
"react-dom": "~18.2.0",
4143
"husky": "~9.1.6",

pkg-example/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Example
22
===
33

4+
Add an example of loading EMS package dependencies
5+
46
```jsx mdx:preview
57
import React from "react";
68
import Example from 'pkg-example';

website/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
"@kkt/less-modules": "~7.5.5",
3232
"@kkt/raw-modules": "~7.5.5",
3333
"@kkt/scope-plugin-options": "~7.5.5",
34-
"@types/react": "^18.2.0",
35-
"@types/react-dom": "^18.2.0",
3634
"kkt": "~7.5.5",
35+
"pkg-example": "2.1.9",
3736
"markdown-react-code-preview-loader": "2.1.9"
3837
},
3938
"eslintConfig": {

website/src/App.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { HashRouter, Routes, Route } from 'react-router-dom';
33
import { Layout } from './Layout';
44
import { HomePage } from './pages/docs';
55
import { ExamplePage } from './pages/example';
6+
import { PkgExamplePage } from './pages/pkg';
67

78
export default function App() {
89
return (
@@ -11,6 +12,7 @@ export default function App() {
1112
<Route element={<Layout />}>
1213
<Route path="/" element={<HomePage />} />
1314
<Route path="/example" element={<ExamplePage />} />
15+
<Route path="/pkg-example" element={<PkgExamplePage />} />
1416
</Route>
1517
</Routes>
1618
</HashRouter>

website/src/Layout.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export function Layout() {
1919
<NavLink to="/example" replace>
2020
{i18next.t('example')}
2121
</NavLink>
22+
<NavLink to="/pkg-example" replace>
23+
{i18next.t('pkg-example')}
24+
</NavLink>
2225
<dark-mode permanent></dark-mode>
2326
<Language />
2427
</nav>

website/src/language/locale/en.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"zh": "chinese",
44
"en": "english",
55
"docuemnt": "Docuemnt",
6-
"example": "Example"
6+
"example": "Example",
7+
"pkg-example": "PKG Example"
78
}

website/src/language/locale/zh.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"zh": "中文",
44
"en": "英文",
55
"docuemnt": "文档",
6-
"example": "案例"
6+
"example": "案例",
7+
"pkg-example": "PKG 示例"
78
}

website/src/pages/example/App-zh.md

+20-25
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,53 @@ import Alert from "@uiw/react-alert";
1616

1717
```jsx mdx:preview
1818
import React from "react";
19+
import { useState } from 'react';
1920
import ReactDOM from "react-dom";
2021
import { Alert, ButtonGroup, Button } from "uiw";
2122

22-
class Demo extends React.Component {
23-
constructor() {
24-
super();
25-
this.state = {
26-
visible1: false,
27-
visible2: false,
28-
};
29-
}
30-
onClick(type) {
31-
this.setState({ [type]: !this.state[type] });
32-
}
33-
onClosed(type) {
34-
this.setState({ [type]: false });
35-
}
36-
render() {
37-
return (
23+
export default function Demo() {
24+
const [visible1, setVisible1] = useState(false);
25+
const [visible2, setVisible2] = useState(false);
26+
return (
3827
<div>
3928
<Alert
40-
isOpen={this.state.visible1}
29+
isOpen={visible1}
4130
confirmText="确定按钮"
42-
onClosed={this.onClosed.bind(this, "visible1")}
31+
onClosed={() => {
32+
setVisible1(false)
33+
}}
4334
content="这个对话框只有两个个按钮,单击“确定按钮”后,此对话框将关闭。用作通知用户重要信息。"
4435
/>
4536
<Alert
46-
isOpen={this.state.visible2}
37+
isOpen={visible2}
4738
confirmText="确定按钮"
4839
cancelText="取消按钮"
4940
type="danger"
5041
onConfirm={() => console.log("您点击了确定按钮!")}
5142
onCancel={() => console.log("您点击了取消按钮!")}
52-
onClosed={this.onClosed.bind(this, "visible2")}
43+
onClosed={() => {
44+
setVisible2(false)
45+
}}
5346
>
5447
这个对话框有两个按钮,单击 “<b>确定按钮</b>” 或 “<b>取消按钮</b>
5548
后,此对话框将关闭,触发 “<b>onConfirm</b>” 或 “<b>onCancel</b>
5649
事件。用作通知用户重要信息。
5750
</Alert>
5851
<ButtonGroup>
59-
<Button onClick={this.onClick.bind(this, "visible1")}>
52+
<Button onClick={() => {
53+
setVisible1(false)
54+
}}>
6055
单个按钮确认对话框
6156
</Button>
62-
<Button onClick={this.onClick.bind(this, "visible2")}>
57+
<Button onClick={() => {
58+
setVisible2(false)
59+
}}>
6360
确认对话框
6461
</Button>
6562
</ButtonGroup>
6663
</div>
67-
);
68-
}
64+
)
6965
}
70-
export default Demo;
7166
```
7267

7368
## 延迟关闭对话框

website/src/pages/pkg/index.tsx

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react';
2+
import { FC, PropsWithChildren, useRef } from 'react';
3+
import MarkdownPreview from '@uiw/react-markdown-preview';
4+
import { getMetaId, getURLParameters, CodeBlockData } from 'markdown-react-code-preview-loader';
5+
6+
import { Root, Element, RootContent } from 'hast';
7+
import MDSource from 'pkg-example/README.md';
8+
import CodeLayout from 'react-code-preview-layout';
9+
10+
const Preview = CodeLayout.Preview;
11+
const Code = CodeLayout.Code;
12+
const Toolbar = CodeLayout.Toolbar;
13+
14+
interface CodePreviewProps {
15+
mdData?: CodeBlockData;
16+
'data-meta'?: string;
17+
}
18+
19+
const CodePreview: FC<PropsWithChildren<CodePreviewProps>> = (props) => {
20+
const $dom = useRef<HTMLDivElement>(null);
21+
// @ts-ignore
22+
const { mdData, node, 'data-meta': meta, ...rest } = props;
23+
// @ts-ignore
24+
const line = node?.position?.start.line;
25+
const metaId = getMetaId(meta) || String(line);
26+
const Child = mdData?.components[`${metaId}`];
27+
if (metaId && typeof Child === 'function') {
28+
const code = mdData?.data[metaId].value || '';
29+
const param = getURLParameters(meta || '');
30+
return (
31+
<CodeLayout ref={$dom}>
32+
<Preview>
33+
<Child />
34+
</Preview>
35+
<Toolbar text={code}>{param.title || 'Example'}</Toolbar>
36+
<Code>
37+
<pre {...rest} />
38+
</Code>
39+
</CodeLayout>
40+
);
41+
}
42+
return <code {...rest} />;
43+
};
44+
45+
export function PkgExamplePage() {
46+
return (
47+
<MarkdownPreview
48+
disableCopy={true}
49+
style={{ background: 'transparent' }}
50+
source={MDSource.source}
51+
rehypeRewrite={(node: Root | RootContent, index: number, parent: Root | Element) => {
52+
if (node.type === 'element' && parent && parent.type === 'root' && /h(1|2|3|4|5|6)/.test(node.tagName)) {
53+
const child = node.children && (node.children[0] as Element);
54+
if (child && child.properties && child.properties.ariaHidden === 'true') {
55+
child.children = [];
56+
}
57+
}
58+
}}
59+
components={{
60+
code: (props) => <CodePreview {...props} mdData={MDSource} />,
61+
}}
62+
/>
63+
);
64+
}

0 commit comments

Comments
 (0)