2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
2017-07-18 18:04:48 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
|
|
|
import Affix from '../affix';
|
|
|
|
import AnchorLink from './AnchorLink';
|
|
|
|
import getScroll from '../_util/getScroll';
|
2018-05-22 13:01:28 +08:00
|
|
|
import raf from 'raf';
|
2017-07-18 18:04:48 +08:00
|
|
|
|
2018-03-30 15:55:54 +08:00
|
|
|
function getDefaultContainer() {
|
2017-07-24 09:58:56 +08:00
|
|
|
return window;
|
2017-07-18 18:04:48 +08:00
|
|
|
}
|
|
|
|
|
2018-03-30 15:55:54 +08:00
|
|
|
function getOffsetTop(element: HTMLElement, container: AnchorContainer): number {
|
2017-07-18 18:04:48 +08:00
|
|
|
if (!element) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!element.getClientRects().length) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rect = element.getBoundingClientRect();
|
|
|
|
|
|
|
|
if (rect.width || rect.height) {
|
2018-03-30 15:55:54 +08:00
|
|
|
if (container === window) {
|
|
|
|
container = element.ownerDocument.documentElement;
|
|
|
|
return rect.top - container.clientTop;
|
|
|
|
}
|
|
|
|
return rect.top - (container as HTMLElement).getBoundingClientRect().top;
|
2017-07-18 18:04:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rect.top;
|
|
|
|
}
|
|
|
|
|
|
|
|
function easeInOutCubic(t: number, b: number, c: number, d: number) {
|
|
|
|
const cc = c - b;
|
|
|
|
t /= d / 2;
|
|
|
|
if (t < 1) {
|
|
|
|
return cc / 2 * t * t * t + b;
|
|
|
|
}
|
|
|
|
return cc / 2 * ((t -= 2) * t * t + 2) + b;
|
|
|
|
}
|
|
|
|
|
2018-01-03 14:32:32 +08:00
|
|
|
const sharpMatcherRegx = /#([^#]+)$/;
|
2018-03-30 15:55:54 +08:00
|
|
|
function scrollTo(href: string, offsetTop = 0, getContainer: () => AnchorContainer, callback = () => { }) {
|
|
|
|
const container = getContainer();
|
|
|
|
const scrollTop = getScroll(container, true);
|
2018-01-03 14:32:32 +08:00
|
|
|
const sharpLinkMatch = sharpMatcherRegx.exec(href);
|
|
|
|
if (!sharpLinkMatch) { return; }
|
|
|
|
const targetElement = document.getElementById(sharpLinkMatch[1]);
|
2017-07-18 18:04:48 +08:00
|
|
|
if (!targetElement) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-30 15:55:54 +08:00
|
|
|
const eleOffsetTop = getOffsetTop(targetElement, container);
|
2017-07-18 18:04:48 +08:00
|
|
|
const targetScrollTop = scrollTop + eleOffsetTop - offsetTop;
|
|
|
|
const startTime = Date.now();
|
|
|
|
const frameFunc = () => {
|
|
|
|
const timestamp = Date.now();
|
|
|
|
const time = timestamp - startTime;
|
2018-03-30 15:55:54 +08:00
|
|
|
const nextScrollTop = easeInOutCubic(time, scrollTop, targetScrollTop, 450);
|
|
|
|
if (container === window) {
|
|
|
|
window.scrollTo(window.pageXOffset, nextScrollTop);
|
|
|
|
} else {
|
|
|
|
(container as HTMLElement).scrollTop = nextScrollTop;
|
|
|
|
}
|
2017-07-18 18:04:48 +08:00
|
|
|
if (time < 450) {
|
2018-05-22 13:01:28 +08:00
|
|
|
raf(frameFunc);
|
2017-07-18 18:04:48 +08:00
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
2018-05-22 13:01:28 +08:00
|
|
|
raf(frameFunc);
|
2017-07-18 18:04:48 +08:00
|
|
|
history.pushState(null, '', href);
|
|
|
|
}
|
|
|
|
|
|
|
|
type Section = {
|
|
|
|
link: String;
|
|
|
|
top: number;
|
|
|
|
};
|
|
|
|
|
2018-03-30 15:55:54 +08:00
|
|
|
export type AnchorContainer = HTMLElement | Window;
|
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
export interface AnchorProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
offsetTop?: number;
|
|
|
|
bounds?: number;
|
|
|
|
affix?: boolean;
|
|
|
|
showInkInFixed?: boolean;
|
2018-03-30 15:55:54 +08:00
|
|
|
getContainer?: () => AnchorContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AnchorDefaultProps extends AnchorProps {
|
|
|
|
prefixCls: string;
|
|
|
|
affix: boolean;
|
|
|
|
showInkInFixed: boolean;
|
|
|
|
getContainer: () => AnchorContainer;
|
2017-07-18 18:04:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Anchor extends React.Component<AnchorProps, any> {
|
|
|
|
static Link: typeof AnchorLink;
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-anchor',
|
|
|
|
affix: true,
|
|
|
|
showInkInFixed: false,
|
2018-03-30 15:55:54 +08:00
|
|
|
getContainer: getDefaultContainer,
|
2017-07-18 18:04:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static childContextTypes = {
|
|
|
|
antAnchor: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
2017-11-22 11:34:46 +08:00
|
|
|
private inkNode: HTMLSpanElement;
|
2017-07-18 18:04:48 +08:00
|
|
|
|
|
|
|
private links: String[];
|
|
|
|
private scrollEvent: any;
|
|
|
|
private animating: boolean;
|
|
|
|
|
|
|
|
constructor(props: AnchorProps) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
activeLink: null,
|
|
|
|
};
|
|
|
|
this.links = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
getChildContext() {
|
|
|
|
return {
|
|
|
|
antAnchor: {
|
|
|
|
registerLink: (link: String) => {
|
|
|
|
if (!this.links.includes(link)) {
|
|
|
|
this.links.push(link);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unregisterLink: (link: String) => {
|
|
|
|
const index = this.links.indexOf(link);
|
|
|
|
if (index !== -1) {
|
|
|
|
this.links.splice(index, 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
activeLink: this.state.activeLink,
|
|
|
|
scrollTo: this.handleScrollTo,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-03-30 15:55:54 +08:00
|
|
|
const { getContainer } = this.props as AnchorDefaultProps;
|
|
|
|
this.scrollEvent = addEventListener(getContainer(), 'scroll', this.handleScroll);
|
2017-07-18 18:04:48 +08:00
|
|
|
this.handleScroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.scrollEvent) {
|
|
|
|
this.scrollEvent.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
this.updateInk();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleScroll = () => {
|
|
|
|
if (this.animating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { offsetTop, bounds } = this.props;
|
|
|
|
this.setState({
|
|
|
|
activeLink: this.getCurrentAnchor(offsetTop, bounds),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-22 11:34:46 +08:00
|
|
|
handleScrollTo = (link: string) => {
|
2018-03-30 15:55:54 +08:00
|
|
|
const { offsetTop, getContainer } = this.props as AnchorDefaultProps;
|
2017-07-18 18:04:48 +08:00
|
|
|
this.animating = true;
|
|
|
|
this.setState({ activeLink: link });
|
2018-03-30 15:55:54 +08:00
|
|
|
scrollTo(link, offsetTop, getContainer, () => {
|
2017-07-18 18:04:48 +08:00
|
|
|
this.animating = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentAnchor(offsetTop = 0, bounds = 5) {
|
|
|
|
let activeLink = '';
|
|
|
|
if (typeof document === 'undefined') {
|
|
|
|
return activeLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
const linkSections: Array<Section> = [];
|
2018-03-30 15:55:54 +08:00
|
|
|
const { getContainer } = this.props as AnchorDefaultProps;
|
|
|
|
const container = getContainer();
|
2017-07-18 18:04:48 +08:00
|
|
|
this.links.forEach(link => {
|
2018-01-03 14:32:32 +08:00
|
|
|
const sharpLinkMatch = sharpMatcherRegx.exec(link.toString());
|
|
|
|
if (!sharpLinkMatch) { return; }
|
|
|
|
const target = document.getElementById(sharpLinkMatch[1]);
|
2018-03-30 15:55:54 +08:00
|
|
|
if (target) {
|
|
|
|
const top = getOffsetTop(target, container);
|
|
|
|
if (top < offsetTop + bounds) {
|
|
|
|
linkSections.push({
|
|
|
|
link,
|
|
|
|
top,
|
|
|
|
});
|
|
|
|
}
|
2017-07-18 18:04:48 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (linkSections.length) {
|
|
|
|
const maxSection = linkSections.reduce((prev, curr) => curr.top > prev.top ? curr : prev);
|
|
|
|
return maxSection.link;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
updateInk = () => {
|
|
|
|
if (typeof document === 'undefined') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { prefixCls } = this.props;
|
2018-04-11 12:01:28 +08:00
|
|
|
const anchorNode = ReactDOM.findDOMNode(this) as Element;
|
|
|
|
const linkNode = anchorNode.getElementsByClassName(`${prefixCls}-link-title-active`)[0];
|
2017-07-18 18:04:48 +08:00
|
|
|
if (linkNode) {
|
2017-09-08 09:37:20 +08:00
|
|
|
this.inkNode.style.top = `${(linkNode as any).offsetTop + linkNode.clientHeight / 2 - 4.5}px`;
|
2017-07-18 18:04:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 11:34:46 +08:00
|
|
|
saveInkNode = (node: HTMLSpanElement) => {
|
2017-09-08 09:37:20 +08:00
|
|
|
this.inkNode = node;
|
|
|
|
}
|
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
className = '',
|
|
|
|
style,
|
|
|
|
offsetTop,
|
|
|
|
affix,
|
|
|
|
showInkInFixed,
|
|
|
|
children,
|
|
|
|
} = this.props;
|
|
|
|
const { activeLink } = this.state;
|
|
|
|
|
|
|
|
const inkClass = classNames(`${prefixCls}-ink-ball`, {
|
|
|
|
visible: activeLink,
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapperClass = classNames(className, `${prefixCls}-wrapper`);
|
|
|
|
|
|
|
|
const anchorClass = classNames(prefixCls, {
|
|
|
|
'fixed': !affix && !showInkInFixed,
|
|
|
|
});
|
|
|
|
|
2017-12-28 13:13:27 +08:00
|
|
|
const wrapperStyle = {
|
|
|
|
maxHeight: offsetTop ? `calc(100vh - ${offsetTop}px)` : '100vh',
|
|
|
|
...style,
|
|
|
|
};
|
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
const anchorContent = (
|
2017-12-28 13:13:27 +08:00
|
|
|
<div
|
|
|
|
className={wrapperClass}
|
|
|
|
style={wrapperStyle}
|
|
|
|
>
|
2017-07-18 18:04:48 +08:00
|
|
|
<div className={anchorClass}>
|
|
|
|
<div className={`${prefixCls}-ink`} >
|
2017-09-08 09:37:20 +08:00
|
|
|
<span className={inkClass} ref={this.saveInkNode} />
|
2017-07-18 18:04:48 +08:00
|
|
|
</div>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return !affix ? anchorContent : (
|
|
|
|
<Affix offsetTop={offsetTop}>
|
|
|
|
{anchorContent}
|
|
|
|
</Affix>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|