import React, { PropTypes } from 'react'; import { Link } from 'bisheng/router'; import { Row, Col, Menu } from 'antd'; import Article from './Article'; import ComponentDoc from './ComponentDoc'; import * as utils from '../utils'; const SubMenu = Menu.SubMenu; function getActiveMenuItem(props) { 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)); } 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'; } export default class MainContent extends React.Component { static contextTypes = { intl: PropTypes.object.isRequired, } constructor(props) { super(props); this.state = { openKeys: this.getSideBarOpenKeys(props) || [] }; } componentDidMount() { this.componentDidUpdate(); } componentWillReceiveProps(nextProps) { const openKeys = this.getSideBarOpenKeys(nextProps); if (openKeys) { this.setState({ openKeys }); } } componentDidUpdate() { if (!location.hash) { document.body.scrollTop = 0; document.documentElement.scrollTop = 0; } else { if (this.timer) { clearTimeout(this.timer); } this.timer = setTimeout(() => { document.getElementById(decodeURI(location.hash.replace('#', ''))).scrollIntoView(); }, 10); } } componentWillUnmount() { clearTimeout(this.timer); } handleMenuOpenChange = (openKeys) => { this.setState({ openKeys }); } 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; } } generateMenuItem(isTop, item) { const locale = this.context.intl.locale; const key = fileNameToPath(item.filename); const text = isTop ? item.title[locale] || item.title : [ {item.title}, {item.subtitle}, ]; const disabled = item.disabled; const url = item.filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').toLowerCase(); const child = !item.link ? ( {text} ) : ( {text} ); return (