import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'bisheng/router';
import { Row, Col, Menu, Icon } from 'antd';
import classNames from 'classnames';
import MobileMenu from 'rc-drawer';
import Article from './Article';
import ComponentDoc from './ComponentDoc';
import * as utils from '../utils';
const { SubMenu } = Menu;
function getActiveMenuItem(props) {
const { children } = props.params;
return (children && children.replace('-cn', ''))
|| props.location.pathname.replace(/(^\/|-cn$)/g, '');
}
function getModuleData(props) {
const { pathname } = props.location;
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));
}
function fileNameToPath(filename) {
const snippets = filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').split('/');
return snippets[snippets.length - 1];
}
const getSideBarOpenKeys = (nextProps) => {
const { themeConfig } = nextProps;
const { pathname } = nextProps.location;
const locale = utils.isZhCN(pathname) ? 'zh-CN' : 'en-US';
const moduleData = getModuleData(nextProps);
const shouldOpenKeys = utils.getMenuItems(
moduleData,
locale,
themeConfig.categoryOrder,
themeConfig.typeOrder
).map(m => m.title[locale] || m.title);
return shouldOpenKeys;
};
export default class MainContent extends React.PureComponent {
static contextTypes = {
intl: PropTypes.object.isRequired,
isMobile: PropTypes.bool.isRequired,
}
state = {
openKeys: undefined,
}
componentDidMount() {
this.componentDidUpdate();
}
static getDerivedStateFromProps(props, state) {
return {
...state,
openKeys: getSideBarOpenKeys(props),
};
}
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 (
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();
}
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 }) => {
[].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';
}
});
}
handleMenuOpenChange = (openKeys) => {
this.setState({ openKeys });
}
generateMenuItem(isTop, item, { before = null, after = null }) {
const { intl: { locale } } = this.context;
const key = fileNameToPath(item.filename);
const title = item.title[locale] || item.title;
const text = isTop ? title : [
{title},
{item.subtitle},
];
const { disabled } = item;
const url = item.filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').toLowerCase();
const child = !item.link ? (
{before}{text}{after}
) : (
{before}{text}