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

306 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';
import { Row, Col, Menu, Icon, Affix } from 'antd';
import classNames from 'classnames';
2018-06-06 22:55:29 +08:00
import MobileMenu from 'rc-drawer';
import Article from './Article';
import PrevAndNext from './PrevAndNext';
import Footer from '../Layout/Footer';
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;
2018-12-07 16:17:45 +08:00
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)
2018-12-07 16:17:45 +08:00
? '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));
}
function fileNameToPath(filename) {
const snippets = filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').split('/');
return snippets[snippets.length - 1];
}
2018-12-07 16:17:45 +08:00
const getSideBarOpenKeys = nextProps => {
const { themeConfig } = nextProps;
const { pathname } = nextProps.location;
const locale = utils.isZhCN(pathname) ? 'zh-CN' : 'en-US';
const moduleData = getModuleData(nextProps);
2018-12-07 16:17:45 +08:00
const shouldOpenKeys = utils
.getMenuItems(moduleData, locale, themeConfig.categoryOrder, themeConfig.typeOrder)
2018-12-22 18:13:20 +08:00
.map(m => (m.title && m.title[locale]) || m.title);
return shouldOpenKeys;
};
export default class MainContent extends React.PureComponent {
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,
2018-12-07 16:17:45 +08:00
};
2016-09-19 11:23:41 +08:00
state = {
openKeys: undefined,
2018-12-07 16:17:45 +08:00
};
2016-03-07 17:33:38 +08:00
componentDidMount() {
2016-07-26 17:40:08 +08:00
this.componentDidUpdate();
}
static getDerivedStateFromProps(props, state) {
2018-12-27 09:44:52 +08:00
if (!state.openKeys) {
return {
...state,
openKeys: getSideBarOpenKeys(props),
};
}
return null;
2016-09-19 11:23:41 +08:00
}
2018-01-05 18:37:07 +08:00
componentDidUpdate(prevProps) {
const { location } = this.props;
if (!prevProps || prevProps.location.pathname !== location.pathname) {
this.bindScroller();
}
2018-10-19 11:17:58 +08:00
if (!window.location.hash && prevProps && prevProps.location.pathname !== location.pathname) {
2016-06-15 11:57:26 +08:00
document.documentElement.scrollTop = 0;
}
setTimeout(() => {
if (
2018-12-07 16:17:45 +08:00
window.location.hash &&
document.querySelector(decodeURIComponent(window.location.hash)) &&
document.documentElement.scrollTop === 0
) {
document.querySelector(decodeURIComponent(window.location.hash)).scrollIntoView();
}
}, 0);
2017-08-27 19:03:25 +08:00
}
componentWillUnmount() {
this.scroller.disable();
}
2018-11-28 15:00:03 +08:00
getMenuItems(footerNavIcons = {}) {
const { themeConfig } = this.props;
2018-12-07 16:17:45 +08:00
const {
intl: { locale },
} = this.context;
2018-11-28 15:00:03 +08:00
const moduleData = getModuleData(this.props);
const menuItems = utils.getMenuItems(
moduleData,
locale,
themeConfig.categoryOrder,
themeConfig.typeOrder,
);
2018-12-07 16:17:45 +08:00
return menuItems.map(menuItem => {
2018-11-28 15:00:03 +08:00
if (menuItem.children) {
return (
<SubMenu title={<h4>{menuItem.title}</h4>} key={menuItem.title}>
2018-12-07 16:17:45 +08:00
{menuItem.children.map(child => {
2018-11-28 15:00:03 +08:00
if (child.type === 'type') {
return (
<Menu.ItemGroup title={child.title} key={child.title}>
2018-12-07 16:17:45 +08:00
{child.children
.sort((a, b) => a.title.charCodeAt(0) - b.title.charCodeAt(0))
.map(leaf => this.generateMenuItem(false, leaf, footerNavIcons))}
2018-11-28 15:00:03 +08:00
</Menu.ItemGroup>
);
}
return this.generateMenuItem(false, child, footerNavIcons);
})}
</SubMenu>
);
}
return this.generateMenuItem(true, menuItem, footerNavIcons);
});
}
getFooterNav(menuItems, activeMenuItem) {
const menuItemsList = this.flattenMenu(menuItems);
let activeMenuItemIndex = -1;
menuItemsList.forEach((menuItem, i) => {
if (menuItem && menuItem.key === activeMenuItem) {
activeMenuItemIndex = i;
}
});
const prev = menuItemsList[activeMenuItemIndex - 1];
const next = menuItemsList[activeMenuItemIndex + 1];
return { prev, next };
}
2018-12-07 16:17:45 +08:00
handleMenuOpenChange = openKeys => {
2018-11-28 15:00:03 +08:00
this.setState({ openKeys });
2018-12-07 16:17:45 +08:00
};
2018-11-28 15:00:03 +08:00
bindScroller() {
if (this.scroller) {
this.scroller.disable();
}
require('intersection-observer'); // eslint-disable-line
const scrollama = require('scrollama'); // eslint-disable-line
this.scroller = scrollama();
this.scroller
.setup({
step: '.markdown > h2, .code-box', // required
offset: 0,
})
.onStepEnter(({ element }) => {
2018-12-07 16:17:45 +08:00
[].forEach.call(document.querySelectorAll('.toc-affix li a'), node => {
2018-11-28 15:00:03 +08:00
node.className = ''; // eslint-disable-line
});
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
2018-08-21 17:46:20 +08:00
generateMenuItem(isTop, item, { before = null, after = null }) {
2018-12-07 16:17:45 +08:00
const {
intl: { locale },
} = this.context;
const key = fileNameToPath(item.filename);
2018-12-22 18:13:20 +08:00
if (!item.title) {
return null;
}
2017-12-22 17:49:38 +08:00
const title = item.title[locale] || item.title;
2018-12-07 16:17:45 +08:00
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
2018-12-07 16:17:45 +08:00
to={utils.getLocalizedPathname(
/^components/.test(url) ? `${url}/` : url,
locale === 'zh-CN',
)}
disabled={disabled}
>
{before}
{text}
{after}
</Link>
) : (
<a
href={item.link}
target="_blank"
rel="noopener noreferrer"
disabled={disabled}
2018-12-07 16:17:45 +08:00
className="menu-item-link-outside"
>
2018-12-07 16:17:45 +08:00
{before}
{text} <Icon type="export" />
{after}
</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>
);
}
2016-03-07 10:32:14 +08:00
flattenMenu(menu) {
2018-12-22 18:13:20 +08:00
if (!menu) {
return null;
}
2018-12-22 19:45:24 +08:00
if (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
}
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 { openKeys } = this.state;
const activeMenuItem = getActiveMenuItem(props);
2016-03-01 17:05:24 +08:00
const menuItems = this.getMenuItems();
2018-08-21 17:46:20 +08:00
const menuItemsForFooterNav = this.getMenuItems({
before: <Icon className="footer-nav-icon-before" type="left" />,
after: <Icon className="footer-nav-icon-after" type="right" />,
});
const { prev, next } = this.getFooterNav(menuItemsForFooterNav, 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"
2018-12-27 09:44:52 +08:00
openKeys={openKeys}
selectedKeys={[activeMenuItem]}
onOpenChange={this.handleMenuOpenChange}
>
{menuItems}
2018-12-07 16:17:45 +08:00
</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-12-07 16:17:45 +08:00
</MobileMenu>
) : (
<Col xxl={4} xl={5} lg={6} md={24} sm={24} xs={24} className="main-menu">
<Affix>
<section className="main-menu-inner">{menuChild}</section>
</Affix>
2018-12-07 16:17:45 +08:00
</Col>
)}
<Col xxl={20} xl={19} lg={18} md={24} sm={24} xs={24}>
<section className={mainContainerClass}>
{props.demos ? (
<ComponentDoc {...props} doc={localizedPageData} demos={props.demos} />
) : (
<Article {...props} content={localizedPageData} />
)}
2016-03-07 14:22:30 +08:00
</section>
<PrevAndNext prev={prev} next={next} />
<Footer />
2016-03-07 14:22:30 +08:00
</Col>
</Row>
</div>
2016-02-29 14:08:40 +08:00
);
}
}