2016-05-27 11:48:08 +08:00
|
|
|
import React from 'react';
|
2019-11-09 17:42:41 +08:00
|
|
|
import { Helmet } from 'react-helmet-async';
|
2019-08-08 12:35:48 +08:00
|
|
|
import { FormattedMessage, injectIntl } from 'react-intl';
|
2016-05-27 11:48:08 +08:00
|
|
|
import classNames from 'classnames';
|
2019-08-13 14:07:17 +08:00
|
|
|
import { Row, Col, Affix, Tooltip } from 'antd';
|
2016-05-27 11:48:08 +08:00
|
|
|
import { getChildren } from 'jsonml.js/lib/utils';
|
2019-12-25 19:09:17 +08:00
|
|
|
import { CodeFilled, CodeOutlined, BugFilled, BugOutlined } from '@ant-design/icons';
|
2016-05-27 11:48:08 +08:00
|
|
|
import Demo from './Demo';
|
2016-08-17 12:06:38 +08:00
|
|
|
import EditButton from './EditButton';
|
2019-09-16 11:23:26 +08:00
|
|
|
import { ping, getMetaDescription } from '../utils';
|
2016-05-27 11:48:08 +08:00
|
|
|
|
2019-08-08 12:35:48 +08:00
|
|
|
class ComponentDoc extends React.Component {
|
2019-01-29 18:14:03 +08:00
|
|
|
state = {
|
|
|
|
expandAll: false,
|
2019-12-30 16:31:30 +08:00
|
|
|
visibleAll: process.env.NODE_ENV !== 'production',
|
2019-03-21 09:48:50 +08:00
|
|
|
showRiddleButton: false,
|
2019-01-29 18:14:03 +08:00
|
|
|
};
|
2016-05-27 11:48:08 +08:00
|
|
|
|
2019-03-21 09:48:50 +08:00
|
|
|
componentDidMount() {
|
2019-12-02 15:21:56 +08:00
|
|
|
const { demos = {}, location = {} } = this.props;
|
|
|
|
if (location.hash) {
|
|
|
|
const demoKey = location.hash.split('-demo-')[1];
|
|
|
|
const demoData = demos[demoKey];
|
|
|
|
if (demoData && demoData.meta && demoData.meta.debug) {
|
|
|
|
this.setState({ visibleAll: true });
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 09:48:50 +08:00
|
|
|
this.pingTimer = ping(status => {
|
|
|
|
if (status !== 'timeout' && status !== 'error') {
|
|
|
|
this.setState({
|
|
|
|
showRiddleButton: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:51:53 +08:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2019-12-18 20:06:36 +08:00
|
|
|
const { location, theme } = this.props;
|
|
|
|
const { location: nextLocation, theme: nextTheme } = nextProps;
|
2019-12-02 15:21:56 +08:00
|
|
|
const { expandAll, visibleAll, showRiddleButton } = this.state;
|
|
|
|
const {
|
|
|
|
expandAll: nextExpandAll,
|
|
|
|
visibleAll: nextVisibleAll,
|
|
|
|
showRiddleButton: nextShowRiddleButton,
|
|
|
|
} = nextState;
|
2019-05-16 13:28:05 +08:00
|
|
|
|
2019-05-21 17:51:53 +08:00
|
|
|
if (
|
|
|
|
nextLocation.pathname === location.pathname &&
|
|
|
|
expandAll === nextExpandAll &&
|
2019-12-18 20:06:36 +08:00
|
|
|
showRiddleButton === nextShowRiddleButton &&
|
2019-12-25 21:13:07 +08:00
|
|
|
theme === nextTheme &&
|
2019-12-02 15:21:56 +08:00
|
|
|
visibleAll === nextVisibleAll &&
|
2019-05-21 17:51:53 +08:00
|
|
|
showRiddleButton === nextShowRiddleButton
|
|
|
|
) {
|
2019-05-16 13:28:05 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-21 09:48:50 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
clearTimeout(this.pingTimer);
|
|
|
|
}
|
|
|
|
|
2016-05-27 11:48:08 +08:00
|
|
|
handleExpandToggle = () => {
|
2018-06-22 21:05:13 +08:00
|
|
|
const { expandAll } = this.state;
|
2016-05-27 11:48:08 +08:00
|
|
|
this.setState({
|
2018-06-22 21:05:13 +08:00
|
|
|
expandAll: !expandAll,
|
2016-05-27 11:48:08 +08:00
|
|
|
});
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2016-05-27 11:48:08 +08:00
|
|
|
|
2019-12-02 15:21:56 +08:00
|
|
|
handleVisibleToggle = () => {
|
|
|
|
const { visibleAll } = this.state;
|
|
|
|
this.setState({
|
|
|
|
visibleAll: !visibleAll,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-05-27 11:48:08 +08:00
|
|
|
render() {
|
2018-12-07 16:17:45 +08:00
|
|
|
const {
|
2019-08-12 13:34:23 +08:00
|
|
|
doc,
|
|
|
|
location,
|
2018-12-07 16:17:45 +08:00
|
|
|
intl: { locale },
|
2019-08-12 13:34:23 +08:00
|
|
|
utils,
|
2019-12-18 20:06:36 +08:00
|
|
|
theme,
|
2020-01-02 17:54:11 +08:00
|
|
|
setIframeTheme,
|
2019-08-12 13:34:23 +08:00
|
|
|
demos,
|
|
|
|
} = this.props;
|
2016-05-27 11:48:08 +08:00
|
|
|
const { content, meta } = doc;
|
2019-08-08 12:35:48 +08:00
|
|
|
const demoValues = Object.keys(demos).map(key => demos[key]);
|
2019-12-02 15:21:56 +08:00
|
|
|
const { expandAll, visibleAll, showRiddleButton } = this.state;
|
2016-05-27 11:48:08 +08:00
|
|
|
const isSingleCol = meta.cols === 1;
|
|
|
|
const leftChildren = [];
|
|
|
|
const rightChildren = [];
|
2019-12-02 15:21:56 +08:00
|
|
|
let showedDemo = demoValues.some(demo => demo.meta.only)
|
2019-08-08 12:35:48 +08:00
|
|
|
? demoValues.filter(demo => demo.meta.only)
|
|
|
|
: demoValues.filter(demo => demo.preview);
|
2019-12-02 15:21:56 +08:00
|
|
|
if (!visibleAll) {
|
|
|
|
showedDemo = showedDemo.filter(item => !item.meta.debug);
|
|
|
|
}
|
2018-12-07 16:17:45 +08:00
|
|
|
showedDemo
|
|
|
|
.sort((a, b) => a.meta.order - b.meta.order)
|
2016-05-27 11:48:08 +08:00
|
|
|
.forEach((demoData, index) => {
|
2017-01-13 14:59:14 +08:00
|
|
|
const demoElem = (
|
|
|
|
<Demo
|
|
|
|
{...demoData}
|
|
|
|
key={demoData.meta.filename}
|
2019-08-08 12:35:48 +08:00
|
|
|
utils={utils}
|
2018-07-01 12:13:46 +08:00
|
|
|
expand={expandAll}
|
2017-01-13 14:59:14 +08:00
|
|
|
location={location}
|
2019-12-18 20:06:36 +08:00
|
|
|
theme={theme}
|
2020-01-02 17:54:11 +08:00
|
|
|
setIframeTheme={setIframeTheme}
|
2017-01-13 14:59:14 +08:00
|
|
|
/>
|
|
|
|
);
|
2016-05-27 11:48:08 +08:00
|
|
|
if (index % 2 === 0 || isSingleCol) {
|
2017-01-13 14:59:14 +08:00
|
|
|
leftChildren.push(demoElem);
|
2016-05-27 11:48:08 +08:00
|
|
|
} else {
|
2017-01-13 14:59:14 +08:00
|
|
|
rightChildren.push(demoElem);
|
2016-05-27 11:48:08 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const expandTriggerClass = classNames({
|
|
|
|
'code-box-expand-trigger': true,
|
2018-07-01 12:13:46 +08:00
|
|
|
'code-box-expand-trigger-active': expandAll,
|
2016-05-27 11:48:08 +08:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
const jumper = showedDemo.map(demo => {
|
2017-10-09 13:23:20 +08:00
|
|
|
const { title } = demo.meta;
|
2016-05-27 11:48:08 +08:00
|
|
|
const localizeTitle = title[locale] || title;
|
|
|
|
return (
|
2016-09-15 01:42:45 +08:00
|
|
|
<li key={demo.meta.id} title={localizeTitle}>
|
2018-12-07 16:17:45 +08:00
|
|
|
<a href={`#${demo.meta.id}`}>{localizeTitle}</a>
|
2016-05-27 11:48:08 +08:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-09-21 11:28:38 +08:00
|
|
|
const { title, subtitle, filename } = meta;
|
2019-03-21 09:48:50 +08:00
|
|
|
const articleClassName = classNames({
|
|
|
|
'show-riddle-button': showRiddleButton,
|
|
|
|
});
|
2019-09-16 11:23:26 +08:00
|
|
|
const helmetTitle = `${subtitle || ''} ${title[locale] || title} - Ant Design`;
|
|
|
|
const contentChild = getMetaDescription(getChildren(content));
|
|
|
|
|
2016-05-27 11:48:08 +08:00
|
|
|
return (
|
2019-09-16 11:23:26 +08:00
|
|
|
<article className={articleClassName}>
|
2019-09-20 17:05:13 +08:00
|
|
|
<Helmet encodeSpecialCharacters={false}>
|
2019-09-16 11:23:26 +08:00
|
|
|
{helmetTitle && <title>{helmetTitle}</title>}
|
|
|
|
{helmetTitle && <meta property="og:title" content={helmetTitle} />}
|
|
|
|
{contentChild && <meta name="description" content={contentChild} />}
|
|
|
|
</Helmet>
|
|
|
|
<Affix className="toc-affix" offsetTop={16}>
|
|
|
|
<ul id="demo-toc" className="toc">
|
|
|
|
{jumper}
|
2020-03-01 20:31:41 +08:00
|
|
|
{doc.api && (
|
|
|
|
<li key="API" title="API">
|
|
|
|
<a href="#API">API</a>
|
|
|
|
</li>
|
|
|
|
)}
|
2019-09-16 11:23:26 +08:00
|
|
|
</ul>
|
|
|
|
</Affix>
|
|
|
|
<section className="markdown">
|
|
|
|
<h1>
|
|
|
|
{title[locale] || title}
|
|
|
|
{!subtitle ? null : <span className="subtitle">{subtitle}</span>}
|
|
|
|
<EditButton
|
|
|
|
title={<FormattedMessage id="app.content.edit-page" />}
|
|
|
|
filename={filename}
|
|
|
|
/>
|
|
|
|
</h1>
|
|
|
|
{utils.toReactComponent(
|
|
|
|
['section', { className: 'markdown' }].concat(getChildren(content)),
|
|
|
|
)}
|
|
|
|
<h2>
|
|
|
|
<FormattedMessage id="app.component.examples" />
|
2020-01-02 19:10:16 +08:00
|
|
|
<span className="all-code-box-controls">
|
2019-12-02 15:21:56 +08:00
|
|
|
<Tooltip
|
|
|
|
title={
|
|
|
|
<FormattedMessage
|
|
|
|
id={`app.component.examples.${expandAll ? 'collapse' : 'expand'}`}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
2019-12-25 19:09:17 +08:00
|
|
|
{expandAll ? (
|
|
|
|
<CodeFilled className={expandTriggerClass} onClick={this.handleExpandToggle} />
|
|
|
|
) : (
|
|
|
|
<CodeOutlined className={expandTriggerClass} onClick={this.handleExpandToggle} />
|
|
|
|
)}
|
2019-12-02 15:21:56 +08:00
|
|
|
</Tooltip>
|
|
|
|
<Tooltip
|
|
|
|
title={
|
|
|
|
<FormattedMessage
|
|
|
|
id={`app.component.examples.${visibleAll ? 'hide' : 'visible'}`}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
2019-12-25 19:09:17 +08:00
|
|
|
{visibleAll ? (
|
2020-01-15 12:13:20 +08:00
|
|
|
<BugFilled className={expandTriggerClass} onClick={this.handleVisibleToggle} />
|
2019-12-25 19:09:17 +08:00
|
|
|
) : (
|
|
|
|
<BugOutlined className={expandTriggerClass} onClick={this.handleVisibleToggle} />
|
|
|
|
)}
|
2019-12-02 15:21:56 +08:00
|
|
|
</Tooltip>
|
|
|
|
</span>
|
2019-09-16 11:23:26 +08:00
|
|
|
</h2>
|
|
|
|
</section>
|
|
|
|
<Row gutter={16}>
|
|
|
|
<Col
|
|
|
|
span={isSingleCol ? 24 : 12}
|
|
|
|
className={isSingleCol ? 'code-boxes-col-1-1' : 'code-boxes-col-2-1'}
|
|
|
|
>
|
|
|
|
{leftChildren}
|
|
|
|
</Col>
|
|
|
|
{isSingleCol ? null : (
|
|
|
|
<Col className="code-boxes-col-2-1" span={12}>
|
|
|
|
{rightChildren}
|
2016-05-27 11:48:08 +08:00
|
|
|
</Col>
|
2018-12-07 16:17:45 +08:00
|
|
|
)}
|
2019-09-16 11:23:26 +08:00
|
|
|
</Row>
|
|
|
|
{utils.toReactComponent(
|
|
|
|
[
|
|
|
|
'section',
|
|
|
|
{
|
|
|
|
className: 'markdown api-container',
|
|
|
|
},
|
|
|
|
].concat(getChildren(doc.api || ['placeholder'])),
|
|
|
|
)}
|
|
|
|
</article>
|
2016-05-27 11:48:08 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 12:35:48 +08:00
|
|
|
|
|
|
|
export default injectIntl(ComponentDoc);
|