From b695aaece043088cd2f88b9e8fcd6a2b60b4a132 Mon Sep 17 00:00:00 2001 From: Benjy Cui Date: Mon, 7 Mar 2016 17:33:38 +0800 Subject: [PATCH] chore: auto scroll to target --- package.json | 1 + site/component/Article/index.jsx | 15 +++++++++--- site/component/ComponentDoc/index.jsx | 21 +++++++++++----- site/component/Demo/index.jsx | 8 +++--- site/component/IconSet/index.jsx | 4 +-- site/component/MainContent/index.jsx | 35 +++++++++++++++++++++------ site/component/utils.js | 8 +++--- site/entry/utils.js | 4 +-- 8 files changed, 68 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 35b5c3fa8e..0c5f345f1d 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "chalk": "^1.1.0", "css-loader": "^0.23.0", "cz-conventional-changelog": "^1.1.5", + "dom-scroll-into-view": "^1.1.0", "dora": "^0.2.3", "dora-plugin-atool-build": "^0.4.1", "es3ify-loader": "^0.1.0", diff --git a/site/component/Article/index.jsx b/site/component/Article/index.jsx index b99445cbb7..4417bf6a39 100644 --- a/site/component/Article/index.jsx +++ b/site/component/Article/index.jsx @@ -1,4 +1,5 @@ import React from 'react'; +import { Link } from 'react-router'; import classNames from 'classnames'; import ImagePreview from './ImagePreview'; import * as utils from '../utils'; @@ -9,7 +10,7 @@ export default class Article extends React.Component { this.imgToPreview = this.imgToPreview.bind(this); } - + isPreviewImg(string) { return /^ { return node.type === 'h2'; }).map((node) => { - return
  • { node.children }
  • ; + return ( +
  • + + { node.children } + +
  • + ); }); content.description = content.description.map(this.imgToPreview); @@ -47,7 +54,7 @@ export default class Article extends React.Component {
      { jumper }
    : null } - { content.description.map(utils.objectToComponent) } + { content.description.map(utils.objectToComponent.bind(null, location.pathname)) } ); } diff --git a/site/component/ComponentDoc/index.jsx b/site/component/ComponentDoc/index.jsx index 39c8c2fef8..1355b73533 100644 --- a/site/component/ComponentDoc/index.jsx +++ b/site/component/ComponentDoc/index.jsx @@ -1,4 +1,5 @@ import React from 'react'; +import { Link } from 'react-router'; import classNames from 'classnames'; import { Row, Col, Icon } from '../../../'; import Demo from '../Demo'; @@ -21,7 +22,7 @@ export default class ComponentDoc extends React.Component { } render() { - const { doc } = this.props; + const { doc, location } = this.props; const { description, meta } = doc; const demos = demosList[meta.fileName] || []; const expand = this.state.expandAll; @@ -34,9 +35,15 @@ export default class ComponentDoc extends React.Component { return a.order - b.order; }).forEach((demoData, index) => { if (index % 2 === 0 || isSingleCol) { - leftChildren.push(); + leftChildren.push( + + ); } else { - rightChildren.push(); + rightChildren.push( + + ); } }); const expandTriggerClass = classNames({ @@ -47,7 +54,9 @@ export default class ComponentDoc extends React.Component { const jumper = demos.map((demo) => { return (
  • - { demo.title } + + { demo.title } +
  • ); }); @@ -59,7 +68,7 @@ export default class ComponentDoc extends React.Component {

    {meta.chinese || meta.english}

    - { description.map(utils.objectToComponent) } + { description.map(utils.objectToComponent.bind(null, location.pathname)) }

    代码演示 { rightChildren } }
    - { (doc.api || []).map(utils.objectToComponent) } + { (doc.api || []).map(utils.objectToComponent.bind(null, location.pathname)) }
    ); diff --git a/site/component/Demo/index.jsx b/site/component/Demo/index.jsx index c5e2cb34b0..a4c5e1d132 100644 --- a/site/component/Demo/index.jsx +++ b/site/component/Demo/index.jsx @@ -1,5 +1,6 @@ import React from 'react'; import ReactDOM from 'react-dom'; +import { Link } from 'react-router'; import antd, { Collapse } from '../../../'; import BrowserDemo from '../BrowserDemo'; import * as utils from '../utils'; @@ -22,9 +23,10 @@ export default class Demo extends React.Component { } render() { - const { id, parentId, preview, title, intro, code, style, expand } = this.props; + const { id, parentId, preview, title, + intro, code, style, expand, pathname } = this.props; const demoId = `${parentId}-${id}`; - const introChildren = intro.map(utils.objectToComponent); + const introChildren = intro.map(utils.objectToComponent.bind(null, pathname)); const highlightedCode = hljs.highlight('javascript', code).value; return ( @@ -39,7 +41,7 @@ export default class Demo extends React.Component {

    - { title } + { title }
    diff --git a/site/component/IconSet/index.jsx b/site/component/IconSet/index.jsx index 837cc87687..a70f11bc69 100644 --- a/site/component/IconSet/index.jsx +++ b/site/component/IconSet/index.jsx @@ -9,7 +9,7 @@ const CopyableIcon = React.createClass({ justCopied: false }; }, - onCopied(e) { + onCopied() { this.setState({ justCopied: true }, () => { setTimeout(() => { this.setState({ justCopied: false }); @@ -17,7 +17,7 @@ const CopyableIcon = React.createClass({ }); }, render() { - const text = ''; + const text = ``; return (
  • diff --git a/site/component/MainContent/index.jsx b/site/component/MainContent/index.jsx index f82c81cfa3..9ff14c82e5 100644 --- a/site/component/MainContent/index.jsx +++ b/site/component/MainContent/index.jsx @@ -1,5 +1,6 @@ import React from 'react'; import { Link } from 'react-router'; +import scrollIntoView from 'dom-scroll-into-view'; import { Row, Col, Menu } from '../../../'; import config from '../../website.config'; const SubMenu = Menu.SubMenu; @@ -9,10 +10,23 @@ function dashed(name) { } export default class MainContent extends React.Component { - constructor(props) { - super(props); + componentDidMount() { + this.componentDidUpdate(); + } - this.generateMenuItem = this.generateMenuItem.bind(this); + componentDidUpdate() { + const scrollTo = this.props.location.query.scrollTo; + if (scrollTo !== undefined) { + const target = document.getElementById(scrollTo); + + if (target !== null) { + scrollIntoView( + target, + document, + { alignWithTop: true, onlyScrollIfNeeded: false } + ); + } + } } getActiveMenuItem(props, index) { @@ -20,9 +34,14 @@ export default class MainContent extends React.Component { return routes[routes.length - 1].path || index; } - generateMenuItem(item) { + generateMenuItem(isTop, item) { const key = dashed(item.english); - const text = item.chinese || item.english; + const text = isTop ? + item.chinese || item.english : + [ + { item.english }, + { item.chinese } + ]; const disabled = item.disabled === 'true'; const child = !item.link ? @@ -45,13 +64,13 @@ export default class MainContent extends React.Component { } generateSubMenuItems(obj) { - const topLevel = (obj.topLevel || []).map(this.generateMenuItem); + const topLevel = (obj.topLevel || []).map(this.generateMenuItem.bind(this, true)); const itemGroups = Object.keys(obj).filter(this.isNotTopLevel) .sort((a, b) => { return config.typeOrder[a] - config.typeOrder[b]; }) .map((type, index) => { - const groupItems = obj[type].map(this.generateMenuItem); + const groupItems = obj[type].map(this.generateMenuItem.bind(this, false)); return ( @@ -117,7 +136,7 @@ export default class MainContent extends React.Component { { menuItems } diff --git a/site/component/utils.js b/site/component/utils.js index accab7e6f2..b13e9ccad0 100644 --- a/site/component/utils.js +++ b/site/component/utils.js @@ -1,5 +1,6 @@ import React from 'react'; import ReactDOM from 'react-dom'; +import { Link } from 'react-router'; import hljs from 'highlight.js'; import antd from '../../'; @@ -13,7 +14,7 @@ function mdLangToHljsLang(lang) { lang; } -export function objectToComponent(object, index) { +export function objectToComponent(pathname, object, index) { if (object === null) return; if (React.isValidElement(object)) { @@ -41,9 +42,10 @@ export function objectToComponent(object, index) { if (isHeading(object.type)) { return React.createElement(object.type, { key: index, + id: children, }, [ object.children, - #, + #, ]); } @@ -71,6 +73,6 @@ export function objectToComponent(object, index) { return React.createElement( object.type, { key: index }, - children && children.map(objectToComponent) // `hr` has no children + children && children.map(objectToComponent.bind(null, pathname)) // `hr` has no children ); } diff --git a/site/entry/utils.js b/site/entry/utils.js index de4b9159d4..2871bc6a41 100644 --- a/site/entry/utils.js +++ b/site/entry/utils.js @@ -55,8 +55,8 @@ export function generateChildren(data) { const children = pagesData.map((pageData, index) => { const hasDemos = demosList[pageData.meta.fileName]; const Wrapper = !hasDemos ? - () =>
    : - () => ; + (props) =>
    : + (props) => ; return (