2017-05-09 13:40:33 +08:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-01-17 15:24:13 +08:00
|
|
|
import { Link } from 'bisheng/router';
|
2017-01-22 19:16:54 +08:00
|
|
|
import { Row, Col, Menu, Icon } from 'antd';
|
2017-03-17 18:56:37 +08:00
|
|
|
import classNames from 'classnames';
|
2016-05-27 11:48:08 +08:00
|
|
|
import Article from './Article';
|
|
|
|
import ComponentDoc from './ComponentDoc';
|
|
|
|
import * as utils from '../utils';
|
2016-08-23 21:00:35 +08:00
|
|
|
|
2016-03-03 11:12:46 +08:00
|
|
|
const SubMenu = Menu.SubMenu;
|
2016-02-29 14:08:40 +08:00
|
|
|
|
2016-09-27 10:06:34 +08:00
|
|
|
function getActiveMenuItem(props) {
|
2016-12-09 13:02:16 +08:00
|
|
|
const children = props.params.children;
|
|
|
|
return (children && children.replace('-cn', '')) ||
|
|
|
|
props.location.pathname.replace(/(^\/|-cn$)/g, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getModuleData(props) {
|
|
|
|
const pathname = props.location.pathname;
|
|
|
|
const moduleName = /^\/?components/.test(pathname) ?
|
|
|
|
'components' : pathname.split('/').filter(item => item).slice(0, 2).join('/');
|
|
|
|
const moduleData = moduleName === 'components' || moduleName === 'docs/react' ||
|
|
|
|
moduleName === 'changelog' || moduleName === 'changelog-cn' ?
|
|
|
|
[...props.picked.components, ...props.picked['docs/react'], ...props.picked.changelog] :
|
|
|
|
props.picked[moduleName];
|
|
|
|
const excludedSuffix = utils.isZhCN(props.location.pathname) ? 'en-US.md' : 'zh-CN.md';
|
|
|
|
return moduleData.filter(({ meta }) => !meta.filename.endsWith(excludedSuffix));
|
2016-09-27 10:06:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function fileNameToPath(filename) {
|
|
|
|
const snippets = filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').split('/');
|
|
|
|
return snippets[snippets.length - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
function isNotTopLevel(level) {
|
|
|
|
return level !== 'topLevel';
|
|
|
|
}
|
|
|
|
|
2016-03-02 11:57:37 +08:00
|
|
|
export default class MainContent extends React.Component {
|
2016-05-27 11:48:08 +08:00
|
|
|
static contextTypes = {
|
2016-09-19 11:23:41 +08:00
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-12-09 13:02:16 +08:00
|
|
|
this.state = { openKeys: this.getSideBarOpenKeys(props) || [] };
|
2016-05-27 11:48:08 +08:00
|
|
|
}
|
|
|
|
|
2016-03-07 17:33:38 +08:00
|
|
|
componentDidMount() {
|
2016-07-26 17:40:08 +08:00
|
|
|
this.componentDidUpdate();
|
|
|
|
}
|
|
|
|
|
2016-09-19 11:23:41 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2016-12-09 13:02:16 +08:00
|
|
|
const openKeys = this.getSideBarOpenKeys(nextProps);
|
|
|
|
if (openKeys) {
|
|
|
|
this.setState({ openKeys });
|
2016-09-19 11:23:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 17:40:08 +08:00
|
|
|
componentDidUpdate() {
|
2016-06-15 11:57:26 +08:00
|
|
|
if (!location.hash) {
|
2016-06-24 11:29:15 +08:00
|
|
|
document.body.scrollTop = 0;
|
2016-06-15 11:57:26 +08:00
|
|
|
document.documentElement.scrollTop = 0;
|
2017-08-27 19:03:25 +08:00
|
|
|
return;
|
2016-06-15 11:57:26 +08:00
|
|
|
}
|
2017-08-27 19:03:25 +08:00
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
}
|
|
|
|
this.timer = setTimeout(() => {
|
|
|
|
location.hash = location.hash;
|
|
|
|
}, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
clearTimeout(this.timer);
|
2016-03-07 17:33:38 +08:00
|
|
|
}
|
2016-03-03 11:12:46 +08:00
|
|
|
|
2016-09-19 11:23:41 +08:00
|
|
|
handleMenuOpenChange = (openKeys) => {
|
|
|
|
this.setState({ openKeys });
|
2016-04-19 17:06:15 +08:00
|
|
|
}
|
|
|
|
|
2016-12-09 13:02:16 +08:00
|
|
|
getSideBarOpenKeys(nextProps) {
|
|
|
|
const pathname = nextProps.location.pathname;
|
|
|
|
const prevModule = this.currentModule;
|
|
|
|
this.currentModule = pathname.replace(/^\//).split('/')[1] || 'components';
|
|
|
|
if (this.currentModule === 'react') {
|
|
|
|
this.currentModule = 'components';
|
|
|
|
}
|
|
|
|
const locale = utils.isZhCN(pathname) ? 'zh-CN' : 'en-US';
|
|
|
|
if (prevModule !== this.currentModule) {
|
|
|
|
const moduleData = getModuleData(nextProps);
|
|
|
|
const shouldOpenKeys = Object.keys(utils.getMenuItems(moduleData, locale));
|
|
|
|
return shouldOpenKeys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 17:33:38 +08:00
|
|
|
generateMenuItem(isTop, item) {
|
2016-09-21 11:28:38 +08:00
|
|
|
const locale = this.context.intl.locale;
|
2016-09-27 10:06:34 +08:00
|
|
|
const key = fileNameToPath(item.filename);
|
2016-03-07 17:33:38 +08:00
|
|
|
const text = isTop ?
|
2016-09-21 11:28:38 +08:00
|
|
|
item.title[locale] || item.title : [
|
|
|
|
<span key="english">{item.title}</span>,
|
|
|
|
<span className="chinese" key="chinese">{item.subtitle}</span>,
|
2016-04-22 14:53:33 +08:00
|
|
|
];
|
2016-03-31 15:22:38 +08:00
|
|
|
const disabled = item.disabled;
|
2016-06-03 15:51:44 +08:00
|
|
|
const url = item.filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').toLowerCase();
|
2016-11-15 15:10:01 +08:00
|
|
|
const child = !item.link ? (
|
2016-12-09 13:02:16 +08:00
|
|
|
<Link
|
|
|
|
to={utils.getLocalizedPathname(/^components/.test(url) ? `${url}/` : url, locale === 'zh-CN')}
|
|
|
|
disabled={disabled}
|
|
|
|
>
|
2016-04-29 12:13:27 +08:00
|
|
|
{text}
|
2016-11-15 15:10:01 +08:00
|
|
|
</Link>
|
|
|
|
) : (
|
2017-01-22 19:16:54 +08:00
|
|
|
<a href={item.link} target="_blank" rel="noopener noreferrer" disabled={disabled} className="menu-item-link-outside">
|
|
|
|
{text} <Icon type="export" />
|
2016-11-15 15:10:01 +08:00
|
|
|
</a>
|
|
|
|
);
|
2016-03-03 11:12:46 +08:00
|
|
|
|
|
|
|
return (
|
2016-05-06 11:06:26 +08:00
|
|
|
<Menu.Item key={key.toLowerCase()} disabled={disabled}>
|
2016-04-29 12:13:27 +08:00
|
|
|
{child}
|
2016-03-03 11:12:46 +08:00
|
|
|
</Menu.Item>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-07 11:35:23 +08:00
|
|
|
generateSubMenuItems(obj) {
|
2017-01-20 11:47:08 +08:00
|
|
|
const { themeConfig } = this.props;
|
2016-03-07 17:33:38 +08:00
|
|
|
const topLevel = (obj.topLevel || []).map(this.generateMenuItem.bind(this, true));
|
2016-09-27 10:06:34 +08:00
|
|
|
const itemGroups = Object.keys(obj).filter(isNotTopLevel)
|
2017-01-20 11:47:08 +08:00
|
|
|
.sort((a, b) => themeConfig.typeOrder[a] - themeConfig.typeOrder[b])
|
2017-01-09 11:51:42 +08:00
|
|
|
.map((type) => {
|
2016-06-15 15:55:39 +08:00
|
|
|
const groupItems = obj[type].sort((a, b) => {
|
2016-09-21 11:28:38 +08:00
|
|
|
return a.title.charCodeAt(0) -
|
|
|
|
b.title.charCodeAt(0);
|
2016-06-15 15:55:39 +08:00
|
|
|
}).map(this.generateMenuItem.bind(this, false));
|
|
|
|
return (
|
2017-01-09 11:51:42 +08:00
|
|
|
<Menu.ItemGroup title={type} key={type}>
|
2016-06-15 15:55:39 +08:00
|
|
|
{groupItems}
|
|
|
|
</Menu.ItemGroup>
|
|
|
|
);
|
|
|
|
});
|
2016-03-07 11:35:23 +08:00
|
|
|
return [...topLevel, ...itemGroups];
|
|
|
|
}
|
|
|
|
|
2016-05-27 11:48:08 +08:00
|
|
|
getMenuItems() {
|
2017-01-20 11:47:08 +08:00
|
|
|
const { themeConfig } = this.props;
|
2016-12-09 13:02:16 +08:00
|
|
|
const moduleData = getModuleData(this.props);
|
2016-09-26 11:08:27 +08:00
|
|
|
const menuItems = utils.getMenuItems(
|
|
|
|
moduleData, this.context.intl.locale
|
|
|
|
);
|
2017-03-17 18:56:37 +08:00
|
|
|
const categories = Object.keys(menuItems).filter(isNotTopLevel);
|
2016-03-07 11:35:23 +08:00
|
|
|
const topLevel = this.generateSubMenuItems(menuItems.topLevel);
|
2017-03-17 18:56:37 +08:00
|
|
|
const result = [...topLevel];
|
|
|
|
result.forEach((item, i) => {
|
|
|
|
const insertCategory = categories.filter(
|
2017-04-07 15:14:14 +08:00
|
|
|
cat => (themeConfig.categoryOrder[cat] ? themeConfig.categoryOrder[cat] <= i : i === result.length - 1)
|
2017-03-17 18:56:37 +08:00
|
|
|
)[0];
|
|
|
|
if (insertCategory) {
|
|
|
|
const target = (
|
|
|
|
<SubMenu title={<h4>{insertCategory}</h4>} key={insertCategory}>
|
|
|
|
{this.generateSubMenuItems(menuItems[insertCategory])}
|
2016-06-15 15:55:39 +08:00
|
|
|
</SubMenu>
|
|
|
|
);
|
2017-04-07 15:14:14 +08:00
|
|
|
result.splice(i + 1, 0, target);
|
2017-03-17 18:56:37 +08:00
|
|
|
categories.splice(categories.indexOf(insertCategory), 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
2016-03-01 17:05:24 +08:00
|
|
|
}
|
|
|
|
|
2016-03-07 10:32:14 +08:00
|
|
|
flattenMenu(menu) {
|
|
|
|
if (menu.type === Menu.Item) {
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(menu)) {
|
2016-04-26 14:09:19 +08:00
|
|
|
return menu.reduce((acc, item) => acc.concat(this.flattenMenu(item)), []);
|
2016-03-07 10:32:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.flattenMenu(menu.props.children);
|
|
|
|
}
|
|
|
|
|
|
|
|
getFooterNav(menuItems, activeMenuItem) {
|
|
|
|
const menuItemsList = this.flattenMenu(menuItems);
|
2016-03-30 22:51:30 +08:00
|
|
|
let activeMenuItemIndex = -1;
|
|
|
|
menuItemsList.forEach((menuItem, i) => {
|
|
|
|
if (menuItem.key === activeMenuItem) {
|
|
|
|
activeMenuItemIndex = i;
|
|
|
|
}
|
2016-03-07 10:32:14 +08:00
|
|
|
});
|
|
|
|
const prev = menuItemsList[activeMenuItemIndex - 1];
|
|
|
|
const next = menuItemsList[activeMenuItemIndex + 1];
|
|
|
|
return { prev, next };
|
|
|
|
}
|
|
|
|
|
2016-02-29 14:08:40 +08:00
|
|
|
render() {
|
2016-05-27 11:48:08 +08:00
|
|
|
const props = this.props;
|
2016-09-27 10:06:34 +08:00
|
|
|
const activeMenuItem = getActiveMenuItem(props);
|
2016-03-01 17:05:24 +08:00
|
|
|
const menuItems = this.getMenuItems();
|
2016-03-07 10:32:14 +08:00
|
|
|
const { prev, next } = this.getFooterNav(menuItems, activeMenuItem);
|
2016-08-04 10:52:09 +08:00
|
|
|
const localizedPageData = props.localizedPageData;
|
2017-03-17 18:56:37 +08:00
|
|
|
const mainContainerClass = classNames('main-container', {
|
|
|
|
'main-container-component': !!props.demos,
|
|
|
|
});
|
2016-02-29 14:08:40 +08:00
|
|
|
return (
|
2016-03-07 14:22:30 +08:00
|
|
|
<div className="main-wrapper">
|
|
|
|
<Row>
|
2017-02-25 15:10:23 +08:00
|
|
|
<Col lg={4} md={6} sm={24} xs={24}>
|
2017-02-21 12:07:28 +08:00
|
|
|
<Menu
|
|
|
|
className="aside-container"
|
|
|
|
mode="inline"
|
2016-09-19 11:23:41 +08:00
|
|
|
openKeys={this.state.openKeys}
|
2016-06-06 13:54:10 +08:00
|
|
|
selectedKeys={[activeMenuItem]}
|
2016-09-19 11:23:41 +08:00
|
|
|
onOpenChange={this.handleMenuOpenChange}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-04-29 12:13:27 +08:00
|
|
|
{menuItems}
|
2016-03-07 14:22:30 +08:00
|
|
|
</Menu>
|
|
|
|
</Col>
|
2017-03-17 18:56:37 +08:00
|
|
|
<Col lg={20} md={18} sm={24} xs={24} className={mainContainerClass}>
|
2016-05-27 11:48:08 +08:00
|
|
|
{
|
2016-12-09 13:02:16 +08:00
|
|
|
props.demos ?
|
2016-06-09 15:00:44 +08:00
|
|
|
<ComponentDoc {...props} doc={localizedPageData} demos={props.demos} /> :
|
2016-05-27 11:48:08 +08:00
|
|
|
<Article {...props} content={localizedPageData} />
|
|
|
|
}
|
2016-03-07 14:22:30 +08:00
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
<Row>
|
2016-12-30 13:44:24 +08:00
|
|
|
<Col
|
|
|
|
lg={{ span: 20, offset: 4 }}
|
2016-03-24 17:13:19 +08:00
|
|
|
md={{ span: 18, offset: 6 }}
|
2017-05-15 12:03:02 +08:00
|
|
|
sm={24}
|
|
|
|
xs={24}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-03-07 14:22:30 +08:00
|
|
|
<section className="prev-next-nav">
|
|
|
|
{
|
2016-08-23 21:00:35 +08:00
|
|
|
prev ?
|
2016-03-07 14:22:30 +08:00
|
|
|
React.cloneElement(prev.props.children, { className: 'prev-page' }) :
|
|
|
|
null
|
|
|
|
}
|
|
|
|
{
|
2016-08-23 21:00:35 +08:00
|
|
|
next ?
|
2016-03-07 14:22:30 +08:00
|
|
|
React.cloneElement(next.props.children, { className: 'next-page' }) :
|
|
|
|
null
|
|
|
|
}
|
|
|
|
</section>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
</div>
|
2016-02-29 14:08:40 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|