2020-06-08 17:42:35 +08:00
|
|
|
import { inject, provide } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import classNames from 'classnames';
|
2020-03-07 19:45:13 +08:00
|
|
|
import addEventListener from '../vc-util/Dom/addEventListener';
|
2019-01-12 11:33:27 +08:00
|
|
|
import Affix from '../affix';
|
2020-03-07 19:45:13 +08:00
|
|
|
import scrollTo from '../_util/scrollTo';
|
2019-01-12 11:33:27 +08:00
|
|
|
import getScroll from '../_util/getScroll';
|
2019-01-23 12:43:15 +08:00
|
|
|
import { initDefaultProps } from '../_util/props-util';
|
2019-01-12 11:33:27 +08:00
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2019-03-11 14:43:23 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-01-12 11:33:27 +08:00
|
|
|
|
|
|
|
function getDefaultContainer() {
|
|
|
|
return window;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getOffsetTop(element, container) {
|
2018-06-02 18:11:50 +08:00
|
|
|
if (!element) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return 0;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!element.getClientRects().length) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return 0;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const rect = element.getBoundingClientRect();
|
2018-06-02 18:11:50 +08:00
|
|
|
|
|
|
|
if (rect.width || rect.height) {
|
|
|
|
if (container === window) {
|
2019-01-12 11:33:27 +08:00
|
|
|
container = element.ownerDocument.documentElement;
|
|
|
|
return rect.top - container.clientTop;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return rect.top - container.getBoundingClientRect().top;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
return rect.top;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
// function easeInOutCubic(t, b, c, d) {
|
|
|
|
// 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-06-02 18:11:50 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const sharpMatcherRegx = /#([^#]+)$/;
|
2020-03-07 19:45:13 +08:00
|
|
|
// function scrollTo(href, offsetTop = 0, getContainer, callback = () => {}) {
|
|
|
|
// const container = getContainer();
|
|
|
|
// const scrollTop = getScroll(container, true);
|
|
|
|
// const sharpLinkMatch = sharpMatcherRegx.exec(href);
|
|
|
|
// if (!sharpLinkMatch) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// const targetElement = document.getElementById(sharpLinkMatch[1]);
|
|
|
|
// if (!targetElement) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// const eleOffsetTop = getOffsetTop(targetElement, container);
|
|
|
|
// const targetScrollTop = scrollTop + eleOffsetTop - offsetTop;
|
|
|
|
// const startTime = Date.now();
|
|
|
|
// const frameFunc = () => {
|
|
|
|
// const timestamp = Date.now();
|
|
|
|
// const time = timestamp - startTime;
|
|
|
|
// const nextScrollTop = easeInOutCubic(time, scrollTop, targetScrollTop, 450);
|
|
|
|
// if (container === window) {
|
|
|
|
// window.scrollTo(window.pageXOffset, nextScrollTop);
|
|
|
|
// } else {
|
|
|
|
// container.scrollTop = nextScrollTop;
|
|
|
|
// }
|
|
|
|
// if (time < 450) {
|
|
|
|
// raf(frameFunc);
|
|
|
|
// } else {
|
|
|
|
// callback();
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// raf(frameFunc);
|
|
|
|
// }
|
2018-06-02 18:11:50 +08:00
|
|
|
|
|
|
|
export const AnchorProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
offsetTop: PropTypes.number,
|
|
|
|
bounds: PropTypes.number,
|
2018-06-04 17:09:01 +08:00
|
|
|
affix: PropTypes.bool,
|
|
|
|
showInkInFixed: PropTypes.bool,
|
2018-06-02 18:11:50 +08:00
|
|
|
getContainer: PropTypes.func,
|
2019-01-23 12:43:15 +08:00
|
|
|
wrapperClass: PropTypes.string,
|
|
|
|
wrapperStyle: PropTypes.object,
|
2020-03-07 19:45:13 +08:00
|
|
|
getCurrentAnchor: PropTypes.func,
|
|
|
|
targetOffset: PropTypes.number,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-06-02 18:11:50 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'AAnchor',
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
inheritAttrs: false,
|
|
|
|
props: initDefaultProps(AnchorProps, {
|
|
|
|
affix: true,
|
|
|
|
showInkInFixed: false,
|
|
|
|
getContainer: getDefaultContainer,
|
|
|
|
}),
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
this.links = [];
|
2019-03-11 14:43:23 +08:00
|
|
|
this._sPrefixCls = '';
|
2018-06-02 18:11:50 +08:00
|
|
|
return {
|
|
|
|
activeLink: null,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
2020-06-08 17:42:35 +08:00
|
|
|
created() {
|
|
|
|
provide('antAnchor', {
|
|
|
|
registerLink: link => {
|
|
|
|
if (!this.links.includes(link)) {
|
|
|
|
this.links.push(link);
|
|
|
|
}
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
2020-06-08 17:42:35 +08:00
|
|
|
unregisterLink: link => {
|
|
|
|
const index = this.links.indexOf(link);
|
|
|
|
if (index !== -1) {
|
|
|
|
this.links.splice(index, 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
$data: this.$data,
|
|
|
|
scrollTo: this.handleScrollTo,
|
|
|
|
});
|
|
|
|
provide('antAnchorContext', this);
|
|
|
|
},
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
mounted() {
|
2018-06-02 18:11:50 +08:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const { getContainer } = this;
|
2020-03-07 19:45:13 +08:00
|
|
|
this.scrollContainer = getContainer();
|
|
|
|
this.scrollEvent = addEventListener(this.scrollContainer, 'scroll', this.handleScroll);
|
2019-01-12 11:33:27 +08:00
|
|
|
this.handleScroll();
|
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
updated() {
|
2018-06-02 18:11:50 +08:00
|
|
|
this.$nextTick(() => {
|
2020-03-07 19:45:13 +08:00
|
|
|
if (this.scrollEvent) {
|
|
|
|
const { getContainer } = this;
|
|
|
|
const currentContainer = getContainer();
|
|
|
|
if (this.scrollContainer !== currentContainer) {
|
|
|
|
this.scrollContainer = currentContainer;
|
|
|
|
this.scrollEvent.remove();
|
|
|
|
this.scrollEvent = addEventListener(this.scrollContainer, 'scroll', this.handleScroll);
|
|
|
|
this.handleScroll();
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.updateInk();
|
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
beforeDestroy() {
|
|
|
|
if (this.scrollEvent) {
|
|
|
|
this.scrollEvent.remove();
|
|
|
|
}
|
|
|
|
},
|
2018-06-02 18:11:50 +08:00
|
|
|
methods: {
|
2020-03-07 19:45:13 +08:00
|
|
|
getCurrentActiveLink(offsetTop = 0, bounds = 5) {
|
|
|
|
const { getCurrentAnchor } = this;
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
if (typeof getCurrentAnchor === 'function') {
|
|
|
|
return getCurrentAnchor();
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const activeLink = '';
|
2018-06-02 18:11:50 +08:00
|
|
|
if (typeof document === 'undefined') {
|
2019-01-12 11:33:27 +08:00
|
|
|
return activeLink;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const linkSections = [];
|
|
|
|
const { getContainer } = this;
|
|
|
|
const container = getContainer();
|
2018-06-02 18:11:50 +08:00
|
|
|
this.links.forEach(link => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const sharpLinkMatch = sharpMatcherRegx.exec(link.toString());
|
|
|
|
if (!sharpLinkMatch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const target = document.getElementById(sharpLinkMatch[1]);
|
2018-06-02 18:11:50 +08:00
|
|
|
if (target) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const top = getOffsetTop(target, container);
|
2018-06-02 18:11:50 +08:00
|
|
|
if (top < offsetTop + bounds) {
|
|
|
|
linkSections.push({
|
|
|
|
link,
|
|
|
|
top,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
|
|
|
|
if (linkSections.length) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const maxSection = linkSections.reduce((prev, curr) => (curr.top > prev.top ? curr : prev));
|
|
|
|
return maxSection.link;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return '';
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
handleScrollTo(link) {
|
|
|
|
const { offsetTop, getContainer, targetOffset } = this;
|
|
|
|
|
|
|
|
this.setCurrentActiveLink(link);
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setCurrentActiveLink(link) {
|
|
|
|
const { activeLink } = this;
|
|
|
|
|
|
|
|
if (activeLink !== link) {
|
|
|
|
this.setState({
|
|
|
|
activeLink: link,
|
|
|
|
});
|
|
|
|
this.$emit('change', link);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleScroll() {
|
|
|
|
if (this.animating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { offsetTop, bounds, targetOffset } = this;
|
|
|
|
const currentActiveLink = this.getCurrentActiveLink(
|
|
|
|
targetOffset !== undefined ? targetOffset : offsetTop || 0,
|
|
|
|
bounds,
|
|
|
|
);
|
|
|
|
this.setCurrentActiveLink(currentActiveLink);
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
updateInk() {
|
2018-06-02 18:11:50 +08:00
|
|
|
if (typeof document === 'undefined') {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
2019-03-11 14:43:23 +08:00
|
|
|
const { _sPrefixCls } = this;
|
|
|
|
const linkNode = this.$el.getElementsByClassName(`${_sPrefixCls}-link-title-active`)[0];
|
2018-06-02 18:11:50 +08:00
|
|
|
if (linkNode) {
|
2020-03-07 19:45:13 +08:00
|
|
|
this.$refs.inkNode.style.top = `${linkNode.offsetTop + linkNode.clientHeight / 2 - 4.5}px`;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2019-05-28 11:37:38 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
offsetTop,
|
|
|
|
affix,
|
|
|
|
showInkInFixed,
|
|
|
|
activeLink,
|
|
|
|
$slots,
|
|
|
|
getContainer,
|
|
|
|
} = this;
|
2019-03-11 14:43:23 +08:00
|
|
|
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-11 14:43:23 +08:00
|
|
|
const prefixCls = getPrefixCls('anchor', customizePrefixCls);
|
|
|
|
this._sPrefixCls = prefixCls;
|
2018-06-02 18:11:50 +08:00
|
|
|
|
|
|
|
const inkClass = classNames(`${prefixCls}-ink-ball`, {
|
|
|
|
visible: activeLink,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2019-01-23 12:43:15 +08:00
|
|
|
const wrapperClass = classNames(this.wrapperClass, `${prefixCls}-wrapper`);
|
2018-06-02 18:11:50 +08:00
|
|
|
|
|
|
|
const anchorClass = classNames(prefixCls, {
|
2019-01-12 11:33:27 +08:00
|
|
|
fixed: !affix && !showInkInFixed,
|
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
|
|
|
|
const wrapperStyle = {
|
|
|
|
maxHeight: offsetTop ? `calc(100vh - ${offsetTop}px)` : '100vh',
|
2019-01-23 12:43:15 +08:00
|
|
|
...this.wrapperStyle,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-06-02 18:11:50 +08:00
|
|
|
|
|
|
|
const anchorContent = (
|
2019-01-12 11:33:27 +08:00
|
|
|
<div class={wrapperClass} style={wrapperStyle}>
|
2018-06-02 18:11:50 +08:00
|
|
|
<div class={anchorClass}>
|
2019-01-12 11:33:27 +08:00
|
|
|
<div class={`${prefixCls}-ink`}>
|
2020-03-07 19:45:13 +08:00
|
|
|
<span class={inkClass} ref="inkNode" />
|
2018-06-02 18:11:50 +08:00
|
|
|
</div>
|
2020-06-08 15:45:14 +08:00
|
|
|
{$slots.default && $slots.default()}
|
2018-06-02 18:11:50 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
return !affix ? (
|
|
|
|
anchorContent
|
|
|
|
) : (
|
2018-09-05 21:28:54 +08:00
|
|
|
<Affix offsetTop={offsetTop} target={getContainer}>
|
2018-06-02 18:11:50 +08:00
|
|
|
{anchorContent}
|
|
|
|
</Affix>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|