ant-design/site/theme/template/Content/MainContent.jsx

294 lines
9.0 KiB
React
Raw Normal View History

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';
import classNames from 'classnames';
2018-01-05 15:00:30 +08:00
import MobileMenu from 'rc-drawer-menu';
import Article from './Article';
import ComponentDoc from './ComponentDoc';
import * as utils from '../utils';
2016-08-23 21:00:35 +08:00
2017-10-09 13:23:20 +08:00
const { SubMenu } = Menu;
2016-02-29 14:08:40 +08:00
function getActiveMenuItem(props) {
2017-10-09 13:23:20 +08:00
const { children } = props.params;
return (children && children.replace('-cn', '')) ||
props.location.pathname.replace(/(^\/|-cn$)/g, '');
}
function getModuleData(props) {
2017-10-09 13:23:20 +08:00
const { pathname } = props.location;
const moduleName = /^\/?components/.test(pathname) ?
2017-10-09 13:23:20 +08:00
'components' : pathname.split('/').filter(item => item).slice(0, 2).join('/');
const moduleData = moduleName === 'components' || moduleName === 'docs/react' ||
moduleName === 'changelog' || moduleName === 'changelog-cn' ?
2017-10-09 13:23:20 +08:00
[...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));
}
function fileNameToPath(filename) {
const snippets = filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').split('/');
return snippets[snippets.length - 1];
}
2016-03-02 11:57:37 +08:00
export default class MainContent extends React.Component {
static contextTypes = {
2016-09-19 11:23:41 +08:00
intl: PropTypes.object.isRequired,
2018-01-05 15:00:30 +08:00
isMobile: PropTypes.bool.isRequired,
2016-09-19 11:23:41 +08:00
}
constructor(props) {
super(props);
this.state = {
openKeys: this.getSideBarOpenKeys(props) || [],
};
}
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) {
const openKeys = this.getSideBarOpenKeys(nextProps);
if (openKeys) {
this.setState({ openKeys });
2016-09-19 11:23:41 +08:00
}
}
2018-01-05 18:37:07 +08:00
componentDidUpdate(prevProps) {
if (!prevProps || prevProps.location.pathname !== this.props.location.pathname) {
this.bindScroller();
}
2018-01-05 18:37:07 +08:00
if (!prevProps || (!window.location.hash && prevProps && prevProps.location.pathname !== this.props.location.pathname)) {
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(() => {
2017-12-16 17:35:28 +08:00
if (window.location.hash) {
document.querySelector(window.location.hash).scrollIntoView();
2017-12-16 17:35:28 +08:00
}
2017-08-27 19:03:25 +08:00
}, 10);
}
componentWillUnmount() {
clearTimeout(this.timer);
this.scroller.disable();
}
bindScroller() {
if (this.scroller) {
this.scroller.disable();
}
// eslint-disable-next-line
const scrollama = require('scrollama');
this.scroller = scrollama();
this.scroller
.setup({
step: '.markdown > h2, .code-box', // required
offset: 0,
})
.onStepEnter(({ element }) => {
2018-01-22 12:11:28 +08:00
[].forEach.call(document.querySelectorAll('.toc-affix li a'), (node) => {
node.className = '';
});
const currentNode = document.querySelectorAll(`.toc-affix li a[href="#${element.id}"]`)[0];
if (currentNode) {
currentNode.className = 'current';
}
});
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
}
getSideBarOpenKeys(nextProps) {
2017-12-22 17:49:38 +08:00
const { themeConfig } = nextProps;
2017-10-09 13:23:20 +08:00
const { pathname } = nextProps.location;
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);
2017-12-22 17:49:38 +08:00
const shouldOpenKeys = utils.getMenuItems(
moduleData,
locale,
themeConfig.categoryOrder,
themeConfig.typeOrder
).map(m => m.title[locale] || m.title);
return shouldOpenKeys;
}
}
2016-03-07 17:33:38 +08:00
generateMenuItem(isTop, item) {
2017-10-09 13:23:20 +08:00
const { locale } = this.context.intl;
const key = fileNameToPath(item.filename);
2017-12-22 17:49:38 +08:00
const title = item.title[locale] || item.title;
const text = isTop ? title : [
<span key="english">{title}</span>,
<span className="chinese" key="chinese">{item.subtitle}</span>,
];
2017-10-09 13:23:20 +08:00
const { disabled } = item;
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 ? (
<Link
to={utils.getLocalizedPathname(/^components/.test(url) ? `${url}/` : url, locale === 'zh-CN')}
disabled={disabled}
>
{text}
</Link>) : (
<a
href={item.link}
target="_blank"
rel="noopener noreferrer"
disabled={disabled}
className="menu-item-link-outside"
>
{text} <Icon type="export" />
</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}>
{child}
2016-03-03 11:12:46 +08:00
</Menu.Item>
);
}
getMenuItems() {
2017-01-20 11:47:08 +08:00
const { themeConfig } = this.props;
const moduleData = getModuleData(this.props);
const menuItems = utils.getMenuItems(
2017-12-22 17:49:38 +08:00
moduleData,
this.context.intl.locale,
themeConfig.categoryOrder,
themeConfig.typeOrder,
);
2017-12-22 17:49:38 +08:00
return menuItems.map((menuItem) => {
if (menuItem.children) {
return (
<SubMenu title={<h4>{menuItem.title}</h4>} key={menuItem.title}>
{menuItem.children.map((child) => {
if (child.type === 'type') {
return (
<Menu.ItemGroup title={child.title} key={child.title}>
2018-01-05 23:41:56 +08:00
{child.children.sort((a, b) => {
return a.title.charCodeAt(0) - b.title.charCodeAt(0);
}).map(leaf => this.generateMenuItem(false, leaf))}
2017-12-22 17:49:38 +08:00
</Menu.ItemGroup>
);
}
return this.generateMenuItem(false, child);
})}
2016-06-15 15:55:39 +08:00
</SubMenu>
);
}
2017-12-22 17:49:38 +08:00
return this.generateMenuItem(true, menuItem);
});
2016-03-01 17:05:24 +08:00
}
2016-03-07 10:32:14 +08:00
flattenMenu(menu) {
2017-11-17 18:30:17 +08:00
if (menu && menu.type && menu.type.isMenuItem) {
2016-03-07 10:32:14 +08:00
return menu;
}
if (Array.isArray(menu)) {
return menu.reduce((acc, item) => acc.concat(this.flattenMenu(item)), []);
2016-03-07 10:32:14 +08:00
}
return this.flattenMenu((menu.props && menu.props.children) || menu.children);
2016-03-07 10:32:14 +08:00
}
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 && menuItem.key === activeMenuItem) {
2016-03-30 22:51:30 +08:00
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() {
2017-10-09 13:23:20 +08:00
const { props } = this;
2018-01-05 15:00:30 +08:00
const { isMobile } = this.context;
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);
2017-10-09 13:23:20 +08:00
const { localizedPageData } = props;
const mainContainerClass = classNames('main-container', {
'main-container-component': !!props.demos,
});
const menuChild = (
<Menu
inlineIndent="40"
2017-11-29 11:22:15 +08:00
className="aside-container menu-site"
mode="inline"
openKeys={this.state.openKeys}
selectedKeys={[activeMenuItem]}
onOpenChange={this.handleMenuOpenChange}
>
{menuItems}
</Menu>);
2016-02-29 14:08:40 +08:00
return (
2016-03-07 14:22:30 +08:00
<div className="main-wrapper">
<Row>
2018-01-05 15:00:30 +08:00
{isMobile ? (
<MobileMenu
iconChild={[<Icon type="menu-unfold" />, <Icon type="menu-fold" />]}
2018-01-05 15:00:30 +08:00
key="Mobile-menu"
wrapperClassName="drawer-wrapper"
>
{menuChild}
2018-01-05 15:00:30 +08:00
</MobileMenu>) : (
<Col xxl={4} xl={5} lg={6} md={24} sm={24} xs={24} className="main-menu">
{menuChild}
</Col>
)
}
<Col xxl={20} xl={19} lg={18} md={24} sm={24} xs={24} className={mainContainerClass}>
{
props.demos ?
2016-06-09 15:00:44 +08:00
<ComponentDoc {...props} doc={localizedPageData} demos={props.demos} /> :
<Article {...props} content={localizedPageData} />
}
2016-03-07 14:22:30 +08:00
</Col>
</Row>
<Row>
2016-12-30 13:44:24 +08:00
<Col
2017-11-29 15:29:35 +08:00
xxl={{ span: 20, offset: 4 }}
xl={{ span: 19, offset: 5 }}
lg={{ span: 18, offset: 6 }}
md={24}
2017-05-15 12:03:02 +08:00
sm={24}
xs={24}
>
2016-03-07 14:22:30 +08:00
<section className="prev-next-nav">
{
2016-08-23 21:00:35 +08:00
prev ?
React.cloneElement(prev.props.children || prev.children[0], { className: 'prev-page' }) :
2016-03-07 14:22:30 +08:00
null
}
{
2016-08-23 21:00:35 +08:00
next ?
React.cloneElement(next.props.children || next.children[0], { className: 'next-page' }) :
2016-03-07 14:22:30 +08:00
null
}
</section>
</Col>
</Row>
</div>
2016-02-29 14:08:40 +08:00
);
}
}