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';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2019-07-24 22:56:20 +08:00
|
|
|
import scrollTo from '../_util/scrollTo';
|
2017-07-18 18:04:48 +08:00
|
|
|
import getScroll from '../_util/getScroll';
|
|
|
|
|
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) {
|
2018-09-30 16:07:01 +08:00
|
|
|
container = element.ownerDocument!.documentElement!;
|
2018-03-30 15:55:54 +08:00
|
|
|
return rect.top - container.clientTop;
|
|
|
|
}
|
|
|
|
return rect.top - (container as HTMLElement).getBoundingClientRect().top;
|
2017-07-18 18:04:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rect.top;
|
|
|
|
}
|
|
|
|
|
2018-01-03 14:32:32 +08:00
|
|
|
const sharpMatcherRegx = /#([^#]+)$/;
|
2017-07-18 18:04:48 +08:00
|
|
|
|
|
|
|
type Section = {
|
2018-08-28 20:20:46 +08:00
|
|
|
link: string;
|
2017-07-18 18:04:48 +08:00
|
|
|
top: number;
|
|
|
|
};
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
export type AnchorContainer = HTMLElement | Window;
|
2018-03-30 15:55:54 +08:00
|
|
|
|
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;
|
2019-07-23 19:37:32 +08:00
|
|
|
/** Return customize highlight anchor */
|
2019-07-23 14:59:28 +08:00
|
|
|
getCurrentAnchor?: () => string;
|
2018-12-07 20:02:01 +08:00
|
|
|
onClick?: (
|
|
|
|
e: React.MouseEvent<HTMLElement>,
|
|
|
|
link: { title: React.ReactNode; href: string },
|
|
|
|
) => void;
|
2019-07-23 20:02:40 +08:00
|
|
|
/** Scroll to target offset value, if none, it's offsetTop prop value or 0. */
|
|
|
|
targetOffset?: number;
|
2019-09-06 19:54:13 +08:00
|
|
|
/** Listening event when scrolling change active link */
|
|
|
|
onChange?: (currentActiveLink: string) => void;
|
2018-08-28 20:20:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface AnchorState {
|
|
|
|
activeLink: null | string;
|
2018-03-30 15:55:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface AnchorDefaultProps extends AnchorProps {
|
|
|
|
prefixCls: string;
|
|
|
|
affix: boolean;
|
|
|
|
showInkInFixed: boolean;
|
|
|
|
getContainer: () => AnchorContainer;
|
2017-07-18 18:04:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-28 20:20:46 +08:00
|
|
|
export interface AntAnchor {
|
|
|
|
registerLink: (link: string) => void;
|
|
|
|
unregisterLink: (link: string) => void;
|
|
|
|
activeLink: string | null;
|
|
|
|
scrollTo: (link: string) => void;
|
2018-12-07 20:02:01 +08:00
|
|
|
onClick?: (
|
|
|
|
e: React.MouseEvent<HTMLElement>,
|
|
|
|
link: { title: React.ReactNode; href: string },
|
|
|
|
) => void;
|
2018-08-28 20:20:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Anchor extends React.Component<AnchorProps, AnchorState> {
|
2017-07-18 18:04:48 +08:00
|
|
|
static Link: typeof AnchorLink;
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2018-08-28 20:20:46 +08:00
|
|
|
state = {
|
|
|
|
activeLink: null,
|
|
|
|
};
|
|
|
|
|
2017-11-22 11:34:46 +08:00
|
|
|
private inkNode: HTMLSpanElement;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-07-16 19:19:11 +08:00
|
|
|
// scroll scope's container
|
|
|
|
private scrollContainer: HTMLElement | Window;
|
2017-07-18 18:04:48 +08:00
|
|
|
|
2018-08-28 20:20:46 +08:00
|
|
|
private links: string[] = [];
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
private scrollEvent: any;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
private animating: boolean;
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
private prefixCls?: string;
|
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
getChildContext() {
|
2018-08-28 20:20:46 +08:00
|
|
|
const antAnchor: 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);
|
|
|
|
}
|
2017-07-18 18:04:48 +08:00
|
|
|
},
|
2018-08-28 20:20:46 +08:00
|
|
|
activeLink: this.state.activeLink,
|
|
|
|
scrollTo: this.handleScrollTo,
|
|
|
|
onClick: this.props.onClick,
|
2017-07-18 18:04:48 +08:00
|
|
|
};
|
2018-08-28 20:20:46 +08:00
|
|
|
return { antAnchor };
|
2017-07-18 18:04:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-03-30 15:55:54 +08:00
|
|
|
const { getContainer } = this.props as AnchorDefaultProps;
|
2019-07-16 19:19:11 +08:00
|
|
|
this.scrollContainer = getContainer();
|
|
|
|
this.scrollEvent = addEventListener(this.scrollContainer, 'scroll', this.handleScroll);
|
2017-07-18 18:04:48 +08:00
|
|
|
this.handleScroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2019-07-16 19:19:11 +08:00
|
|
|
if (this.scrollEvent) {
|
|
|
|
const { getContainer } = this.props as AnchorDefaultProps;
|
|
|
|
const currentContainer = getContainer();
|
|
|
|
if (this.scrollContainer !== currentContainer) {
|
|
|
|
this.scrollContainer = currentContainer;
|
|
|
|
this.scrollEvent.remove();
|
|
|
|
this.scrollEvent = addEventListener(this.scrollContainer, 'scroll', this.handleScroll);
|
|
|
|
this.handleScroll();
|
|
|
|
}
|
|
|
|
}
|
2017-07-18 18:04:48 +08:00
|
|
|
this.updateInk();
|
|
|
|
}
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.scrollEvent) {
|
|
|
|
this.scrollEvent.remove();
|
2019-07-13 10:43:35 +08:00
|
|
|
}
|
2019-08-14 16:32:54 +08:00
|
|
|
}
|
2019-07-24 22:56:20 +08:00
|
|
|
|
2018-08-28 20:20:46 +08:00
|
|
|
getCurrentAnchor(offsetTop = 0, bounds = 5): string {
|
2019-07-23 14:59:28 +08:00
|
|
|
const { getCurrentAnchor } = this.props;
|
|
|
|
|
|
|
|
if (typeof getCurrentAnchor === 'function') {
|
|
|
|
return getCurrentAnchor();
|
|
|
|
}
|
|
|
|
|
2018-11-10 00:39:58 +08:00
|
|
|
const activeLink = '';
|
2017-07-18 18:04:48 +08:00
|
|
|
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());
|
2018-12-07 20:02:01 +08:00
|
|
|
if (!sharpLinkMatch) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-03 14:32:32 +08:00
|
|
|
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) {
|
2018-12-07 20:02:01 +08:00
|
|
|
const maxSection = linkSections.reduce((prev, curr) => (curr.top > prev.top ? curr : prev));
|
2017-07-18 18:04:48 +08:00
|
|
|
return maxSection.link;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:07:58 +08:00
|
|
|
handleScrollTo = (link: string) => {
|
|
|
|
const { offsetTop, getContainer, targetOffset } = this.props as AnchorDefaultProps;
|
|
|
|
|
2019-09-06 19:54:13 +08:00
|
|
|
this.setCurrentActiveLink(link);
|
2019-08-14 17:07:58 +08:00
|
|
|
const container = getContainer();
|
|
|
|
const scrollTop = getScroll(container, true);
|
|
|
|
const sharpLinkMatch = sharpMatcherRegx.exec(link);
|
|
|
|
if (!sharpLinkMatch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const targetElement = document.getElementById(sharpLinkMatch[1]);
|
|
|
|
if (!targetElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const eleOffsetTop = getOffsetTop(targetElement, container);
|
|
|
|
let y = scrollTop + eleOffsetTop;
|
|
|
|
y -= targetOffset !== undefined ? targetOffset : offsetTop || 0;
|
|
|
|
this.animating = true;
|
|
|
|
|
|
|
|
scrollTo(y, {
|
|
|
|
callback: () => {
|
|
|
|
this.animating = false;
|
|
|
|
},
|
|
|
|
getContainer,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
saveInkNode = (node: HTMLSpanElement) => {
|
|
|
|
this.inkNode = node;
|
|
|
|
};
|
|
|
|
|
2019-09-06 19:54:13 +08:00
|
|
|
setCurrentActiveLink = (link: string) => {
|
|
|
|
const { activeLink } = this.state;
|
|
|
|
const { onChange } = this.props;
|
|
|
|
|
|
|
|
if (activeLink !== link) {
|
|
|
|
this.setState({
|
|
|
|
activeLink: link,
|
|
|
|
});
|
|
|
|
if (onChange) {
|
|
|
|
onChange(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
handleScroll = () => {
|
|
|
|
if (this.animating) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-04 14:32:31 +08:00
|
|
|
const { offsetTop, bounds, targetOffset } = this.props;
|
|
|
|
const currentActiveLink = this.getCurrentAnchor(
|
|
|
|
targetOffset !== undefined ? targetOffset : offsetTop || 0,
|
|
|
|
bounds,
|
|
|
|
);
|
2019-09-06 19:54:13 +08:00
|
|
|
this.setCurrentActiveLink(currentActiveLink);
|
2019-08-05 18:38:10 +08:00
|
|
|
};
|
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
updateInk = () => {
|
|
|
|
if (typeof document === 'undefined') {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
const { prefixCls } = this;
|
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
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-07-18 18:04:48 +08:00
|
|
|
|
2020-04-07 10:55:36 +08:00
|
|
|
renderAnchor = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2017-07-18 18:04:48 +08:00
|
|
|
const {
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2017-07-18 18:04:48 +08:00
|
|
|
className = '',
|
|
|
|
style,
|
|
|
|
offsetTop,
|
|
|
|
affix,
|
|
|
|
showInkInFixed,
|
|
|
|
children,
|
2018-08-10 13:37:17 +08:00
|
|
|
getContainer,
|
2017-07-18 18:04:48 +08:00
|
|
|
} = this.props;
|
|
|
|
const { activeLink } = this.state;
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('anchor', customizePrefixCls);
|
|
|
|
|
|
|
|
// To support old version react.
|
|
|
|
// Have to add prefixCls on the instance.
|
|
|
|
// https://github.com/facebook/react/issues/12397
|
|
|
|
this.prefixCls = prefixCls;
|
|
|
|
|
2017-07-18 18:04:48 +08:00
|
|
|
const inkClass = classNames(`${prefixCls}-ink-ball`, {
|
|
|
|
visible: activeLink,
|
|
|
|
});
|
|
|
|
|
2020-04-07 10:55:36 +08:00
|
|
|
const wrapperClass = classNames(className, `${prefixCls}-wrapper`, {
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
2017-07-18 18:04:48 +08:00
|
|
|
|
|
|
|
const anchorClass = classNames(prefixCls, {
|
2018-12-07 20:02:01 +08:00
|
|
|
fixed: !affix && !showInkInFixed,
|
2017-07-18 18:04:48 +08:00
|
|
|
});
|
|
|
|
|
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 = (
|
2018-12-07 20:02:01 +08:00
|
|
|
<div className={wrapperClass} style={wrapperStyle}>
|
2017-07-18 18:04:48 +08:00
|
|
|
<div className={anchorClass}>
|
2018-12-07 20:02:01 +08:00
|
|
|
<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>
|
|
|
|
);
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
return !affix ? (
|
|
|
|
anchorContent
|
|
|
|
) : (
|
2018-08-10 13:37:17 +08:00
|
|
|
<Affix offsetTop={offsetTop} target={getContainer}>
|
2017-07-18 18:04:48 +08:00
|
|
|
{anchorContent}
|
|
|
|
</Affix>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderAnchor}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2017-07-18 18:04:48 +08:00
|
|
|
}
|