You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
9.0 KiB

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'bisheng/router';
import { Row, Col, Menu, Icon, Affix } from 'antd';
import classNames from 'classnames';
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';
const { SubMenu } = Menu;
function getActiveMenuItem(props) {
const { children } = props.params;
6 years ago
return (
(children && children.replace('-cn', '')) || props.location.pathname.replace(/(^\/|-cn$)/g, '')
);
}
function getModuleData(props) {
const { pathname } = props.location;
const moduleName = /^\/?components/.test(pathname)
6 years ago
? '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];
}
6 years ago
const getSideBarOpenKeys = nextProps => {
const { themeConfig } = nextProps;
const { pathname } = nextProps.location;
const locale = utils.isZhCN(pathname) ? 'zh-CN' : 'en-US';
const moduleData = getModuleData(nextProps);
6 years ago
const shouldOpenKeys = utils
.getMenuItems(moduleData, locale, themeConfig.categoryOrder, themeConfig.typeOrder)
.map(m => (m.title && m.title[locale]) || m.title);
return shouldOpenKeys;
};
export default class MainContent extends Component {
static contextTypes = {
intl: PropTypes.object.isRequired,
7 years ago
isMobile: PropTypes.bool.isRequired,
6 years ago
};
state = {
openKeys: undefined,
6 years ago
};
componentDidMount() {
this.componentDidUpdate();
}
static getDerivedStateFromProps(props, state) {
if (!state.openKeys) {
return {
...state,
openKeys: getSideBarOpenKeys(props),
};
}
return null;
}
7 years ago
componentDidUpdate(prevProps) {
const { location } = this.props;
if (!prevProps || prevProps.location.pathname !== location.pathname) {
this.bindScroller();
}
if (!window.location.hash && prevProps && prevProps.location.pathname !== location.pathname) {
document.documentElement.scrollTop = 0;
}
setTimeout(() => {
if (
6 years ago
window.location.hash &&
document.querySelector(decodeURIComponent(window.location.hash)) &&
document.documentElement.scrollTop === 0
) {
document.querySelector(decodeURIComponent(window.location.hash)).scrollIntoView();
}
}, 0);
}
componentWillUnmount() {
this.scroller.disable();
}
getMenuItems(footerNavIcons = {}) {
const { themeConfig } = this.props;
6 years ago
const {
intl: { locale },
} = this.context;
const moduleData = getModuleData(this.props);
const menuItems = utils.getMenuItems(
moduleData,
locale,
themeConfig.categoryOrder,
themeConfig.typeOrder,
);
6 years ago
return menuItems.map(menuItem => {
if (menuItem.children) {
return (
<SubMenu title={<h4>{menuItem.title}</h4>} key={menuItem.title}>
6 years ago
{menuItem.children.map(child => {
if (child.type === 'type') {
return (
<Menu.ItemGroup title={child.title} key={child.title}>
6 years ago
{child.children
.sort((a, b) => a.title.charCodeAt(0) - b.title.charCodeAt(0))
.map(leaf => this.generateMenuItem(false, leaf, footerNavIcons))}
</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 };
}
6 years ago
handleMenuOpenChange = openKeys => {
this.setState({ openKeys });
6 years ago
};
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 }) => {
6 years ago
[].forEach.call(document.querySelectorAll('.toc-affix li a'), node => {
node.className = ''; // eslint-disable-line
});
const currentNode = document.querySelectorAll(`.toc-affix li a[href="#${element.id}"]`)[0];
if (currentNode) {
currentNode.className = 'current';
}
});
}
generateMenuItem(isTop, item, { before = null, after = null }) {
6 years ago
const {
intl: { locale },
} = this.context;
const key = fileNameToPath(item.filename);
if (!item.title) {
return null;
}
7 years ago
const title = item.title[locale] || item.title;
6 years ago
const text = isTop
? title
: [
<span key="english">{title}</span>,
<span className="chinese" key="chinese">
{item.subtitle}
</span>,
];
const { disabled } = item;
9 years ago
const url = item.filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').toLowerCase();
8 years ago
const child = !item.link ? (
<Link
6 years ago
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}
6 years ago
className="menu-item-link-outside"
>
6 years ago
{before}
{text} <Icon type="export" />
{after}
</a>
);
return (
<Menu.Item key={key.toLowerCase()} disabled={disabled}>
{child}
</Menu.Item>
);
}
9 years ago
flattenMenu(menu) {
if (!menu) {
return null;
}
if (menu.type && menu.type.isMenuItem) {
9 years ago
return menu;
}
if (Array.isArray(menu)) {
return menu.reduce((acc, item) => acc.concat(this.flattenMenu(item)), []);
9 years ago
}
return this.flattenMenu((menu.props && menu.props.children) || menu.children);
9 years ago
}
render() {
const { props } = this;
7 years ago
const { isMobile } = this.context;
const { openKeys } = this.state;
const activeMenuItem = getActiveMenuItem(props);
const menuItems = this.getMenuItems();
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);
const { localizedPageData } = props;
const mainContainerClass = classNames('main-container', {
'main-container-component': !!props.demos,
});
const menuChild = (
<Menu
inlineIndent="40"
7 years ago
className="aside-container menu-site"
mode="inline"
openKeys={openKeys}
selectedKeys={[activeMenuItem]}
onOpenChange={this.handleMenuOpenChange}
>
{menuItems}
6 years ago
</Menu>
);
return (
<div className="main-wrapper">
<Row>
7 years ago
{isMobile ? (
<MobileMenu
iconChild={[<Icon type="menu-unfold" />, <Icon type="menu-fold" />]}
7 years ago
key="Mobile-menu"
wrapperClassName="drawer-wrapper"
>
{menuChild}
6 years ago
</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>
6 years ago
</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} />
)}
</section>
<PrevAndNext prev={prev} next={next} />
<Footer />
</Col>
</Row>
</div>
);
}
}