You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
298 lines
7.8 KiB
298 lines
7.8 KiB
4 years ago
|
import { defineComponent, inject, provide } from 'vue';
|
||
6 years ago
|
import PropTypes from '../_util/vue-types';
|
||
4 years ago
|
import classNames from '../_util/classNames';
|
||
5 years ago
|
import addEventListener from '../vc-util/Dom/addEventListener';
|
||
6 years ago
|
import Affix from '../affix';
|
||
5 years ago
|
import scrollTo from '../_util/scrollTo';
|
||
6 years ago
|
import getScroll from '../_util/getScroll';
|
||
4 years ago
|
import { findDOMNode } from '../_util/props-util';
|
||
6 years ago
|
import BaseMixin from '../_util/BaseMixin';
|
||
4 years ago
|
import { defaultConfigProvider } from '../config-provider';
|
||
6 years ago
|
|
||
|
function getDefaultContainer() {
|
||
|
return window;
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
function getOffsetTop(element: HTMLElement, container: AnchorContainer): number {
|
||
7 years ago
|
if (!element) {
|
||
6 years ago
|
return 0;
|
||
7 years ago
|
}
|
||
|
|
||
|
if (!element.getClientRects().length) {
|
||
6 years ago
|
return 0;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
const rect = element.getBoundingClientRect();
|
||
7 years ago
|
|
||
|
if (rect.width || rect.height) {
|
||
|
if (container === window) {
|
||
6 years ago
|
container = element.ownerDocument.documentElement;
|
||
|
return rect.top - container.clientTop;
|
||
7 years ago
|
}
|
||
4 years ago
|
return rect.top - (container as HTMLElement).getBoundingClientRect().top;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
return rect.top;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
const sharpMatcherRegx = /#([^#]+)$/;
|
||
7 years ago
|
|
||
4 years ago
|
type Section = {
|
||
|
link: string;
|
||
|
top: number;
|
||
|
};
|
||
|
|
||
|
export type AnchorContainer = HTMLElement | Window;
|
||
|
|
||
|
const AnchorProps = {
|
||
7 years ago
|
prefixCls: PropTypes.string,
|
||
|
offsetTop: PropTypes.number,
|
||
|
bounds: PropTypes.number,
|
||
4 years ago
|
affix: PropTypes.looseBool.def(true),
|
||
|
showInkInFixed: PropTypes.looseBool.def(false),
|
||
|
getContainer: PropTypes.func.def(getDefaultContainer),
|
||
6 years ago
|
wrapperClass: PropTypes.string,
|
||
|
wrapperStyle: PropTypes.object,
|
||
5 years ago
|
getCurrentAnchor: PropTypes.func,
|
||
|
targetOffset: PropTypes.number,
|
||
4 years ago
|
onChange: PropTypes.func,
|
||
|
onClick: PropTypes.func,
|
||
6 years ago
|
};
|
||
7 years ago
|
|
||
4 years ago
|
export interface AntAnchor {
|
||
|
registerLink: (link: string) => void;
|
||
|
unregisterLink: (link: string) => void;
|
||
|
$data: AnchorState;
|
||
|
scrollTo: (link: string) => void;
|
||
|
$emit?: Function
|
||
|
}
|
||
|
interface AnchorState {
|
||
|
activeLink: null | string;
|
||
|
scrollContainer: HTMLElement | Window;
|
||
|
links: string[];
|
||
|
scrollEvent: any;
|
||
|
animating: boolean;
|
||
|
sPrefixCls?: string;
|
||
|
}
|
||
|
|
||
|
export default defineComponent({
|
||
7 years ago
|
name: 'AAnchor',
|
||
|
mixins: [BaseMixin],
|
||
|
inheritAttrs: false,
|
||
4 years ago
|
props: AnchorProps,
|
||
|
emits: ['change', 'click'],
|
||
6 years ago
|
data() {
|
||
4 years ago
|
// this.links = [];
|
||
|
// this.sPrefixCls = '';
|
||
7 years ago
|
return {
|
||
|
activeLink: null,
|
||
4 years ago
|
links: [],
|
||
|
sPrefixCls: '',
|
||
|
scrollContainer: null,
|
||
|
scrollEvent: null,
|
||
|
animating: false,
|
||
|
} as AnchorState;
|
||
7 years ago
|
},
|
||
5 years ago
|
created() {
|
||
|
provide('antAnchor', {
|
||
4 years ago
|
registerLink: (link: string) => {
|
||
5 years ago
|
if (!this.links.includes(link)) {
|
||
|
this.links.push(link);
|
||
|
}
|
||
7 years ago
|
},
|
||
4 years ago
|
unregisterLink: (link: string) => {
|
||
5 years ago
|
const index = this.links.indexOf(link);
|
||
|
if (index !== -1) {
|
||
|
this.links.splice(index, 1);
|
||
|
}
|
||
|
},
|
||
|
$data: this.$data,
|
||
|
scrollTo: this.handleScrollTo,
|
||
4 years ago
|
} as AntAnchor);
|
||
5 years ago
|
provide('antAnchorContext', this);
|
||
|
},
|
||
|
setup() {
|
||
|
return {
|
||
4 years ago
|
configProvider: inject('configProvider', defaultConfigProvider),
|
||
6 years ago
|
};
|
||
7 years ago
|
},
|
||
6 years ago
|
mounted() {
|
||
7 years ago
|
this.$nextTick(() => {
|
||
6 years ago
|
const { getContainer } = this;
|
||
5 years ago
|
this.scrollContainer = getContainer();
|
||
|
this.scrollEvent = addEventListener(this.scrollContainer, 'scroll', this.handleScroll);
|
||
6 years ago
|
this.handleScroll();
|
||
|
});
|
||
7 years ago
|
},
|
||
6 years ago
|
updated() {
|
||
7 years ago
|
this.$nextTick(() => {
|
||
5 years ago
|
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();
|
||
|
}
|
||
|
}
|
||
6 years ago
|
this.updateInk();
|
||
|
});
|
||
7 years ago
|
},
|
||
5 years ago
|
beforeUnmount() {
|
||
5 years ago
|
if (this.scrollEvent) {
|
||
|
this.scrollEvent.remove();
|
||
|
}
|
||
|
},
|
||
7 years ago
|
methods: {
|
||
5 years ago
|
getCurrentActiveLink(offsetTop = 0, bounds = 5) {
|
||
|
const { getCurrentAnchor } = this;
|
||
7 years ago
|
|
||
5 years ago
|
if (typeof getCurrentAnchor === 'function') {
|
||
|
return getCurrentAnchor();
|
||
|
}
|
||
6 years ago
|
const activeLink = '';
|
||
7 years ago
|
if (typeof document === 'undefined') {
|
||
6 years ago
|
return activeLink;
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
const linkSections: Array<Section> = [];
|
||
6 years ago
|
const { getContainer } = this;
|
||
|
const container = getContainer();
|
||
7 years ago
|
this.links.forEach(link => {
|
||
6 years ago
|
const sharpLinkMatch = sharpMatcherRegx.exec(link.toString());
|
||
|
if (!sharpLinkMatch) {
|
||
|
return;
|
||
|
}
|
||
|
const target = document.getElementById(sharpLinkMatch[1]);
|
||
7 years ago
|
if (target) {
|
||
6 years ago
|
const top = getOffsetTop(target, container);
|
||
7 years ago
|
if (top < offsetTop + bounds) {
|
||
|
linkSections.push({
|
||
|
link,
|
||
|
top,
|
||
6 years ago
|
});
|
||
7 years ago
|
}
|
||
|
}
|
||
6 years ago
|
});
|
||
7 years ago
|
|
||
|
if (linkSections.length) {
|
||
6 years ago
|
const maxSection = linkSections.reduce((prev, curr) => (curr.top > prev.top ? curr : prev));
|
||
|
return maxSection.link;
|
||
7 years ago
|
}
|
||
6 years ago
|
return '';
|
||
7 years ago
|
},
|
||
|
|
||
5 years ago
|
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,
|
||
|
});
|
||
|
},
|
||
4 years ago
|
setCurrentActiveLink(link: string) {
|
||
5 years ago
|
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);
|
||
|
},
|
||
|
|
||
6 years ago
|
updateInk() {
|
||
7 years ago
|
if (typeof document === 'undefined') {
|
||
6 years ago
|
return;
|
||
7 years ago
|
}
|
||
4 years ago
|
const { sPrefixCls } = this;
|
||
4 years ago
|
const linkNode = findDOMNode(this).getElementsByClassName(
|
||
4 years ago
|
`${sPrefixCls}-link-title-active`,
|
||
4 years ago
|
)[0];
|
||
7 years ago
|
if (linkNode) {
|
||
4 years ago
|
(this.$refs.inkNode as HTMLElement).style.top = `${linkNode.offsetTop + linkNode.clientHeight / 2 - 4.5}px`;
|
||
7 years ago
|
}
|
||
|
},
|
||
|
},
|
||
|
|
||
6 years ago
|
render() {
|
||
6 years ago
|
const {
|
||
|
prefixCls: customizePrefixCls,
|
||
|
offsetTop,
|
||
|
affix,
|
||
|
showInkInFixed,
|
||
|
activeLink,
|
||
|
$slots,
|
||
|
getContainer,
|
||
|
} = this;
|
||
5 years ago
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
||
6 years ago
|
const prefixCls = getPrefixCls('anchor', customizePrefixCls);
|
||
4 years ago
|
this.sPrefixCls = prefixCls;
|
||
7 years ago
|
|
||
|
const inkClass = classNames(`${prefixCls}-ink-ball`, {
|
||
|
visible: activeLink,
|
||
6 years ago
|
});
|
||
7 years ago
|
|
||
4 years ago
|
const wrapperClass = classNames(this.wrapperClass, `${prefixCls}-wrapper`);
|
||
7 years ago
|
|
||
|
const anchorClass = classNames(prefixCls, {
|
||
6 years ago
|
fixed: !affix && !showInkInFixed,
|
||
|
});
|
||
7 years ago
|
|
||
|
const wrapperStyle = {
|
||
|
maxHeight: offsetTop ? `calc(100vh - ${offsetTop}px)` : '100vh',
|
||
6 years ago
|
...this.wrapperStyle,
|
||
6 years ago
|
};
|
||
7 years ago
|
const anchorContent = (
|
||
6 years ago
|
<div class={wrapperClass} style={wrapperStyle}>
|
||
7 years ago
|
<div class={anchorClass}>
|
||
6 years ago
|
<div class={`${prefixCls}-ink`}>
|
||
5 years ago
|
<span class={inkClass} ref="inkNode" />
|
||
7 years ago
|
</div>
|
||
5 years ago
|
{$slots.default && $slots.default()}
|
||
7 years ago
|
</div>
|
||
|
</div>
|
||
6 years ago
|
);
|
||
7 years ago
|
|
||
6 years ago
|
return !affix ? (
|
||
|
anchorContent
|
||
|
) : (
|
||
4 years ago
|
<Affix {...this.$attrs} offsetTop={offsetTop} target={getContainer}>
|
||
7 years ago
|
{anchorContent}
|
||
|
</Affix>
|
||
6 years ago
|
);
|
||
7 years ago
|
},
|
||
4 years ago
|
});
|