2022-03-12 09:56:32 +08:00
|
|
|
import type { CSSProperties, ExtractPropTypes, PropType } from 'vue';
|
2021-06-07 17:35:03 +08:00
|
|
|
import {
|
|
|
|
defineComponent,
|
|
|
|
nextTick,
|
|
|
|
onBeforeUnmount,
|
|
|
|
onMounted,
|
|
|
|
onUpdated,
|
|
|
|
reactive,
|
|
|
|
ref,
|
|
|
|
computed,
|
|
|
|
} from 'vue';
|
2023-04-21 17:33:38 +08:00
|
|
|
import scrollIntoView from 'scroll-into-view-if-needed';
|
2020-08-31 16:53:19 +08:00
|
|
|
import classNames from '../_util/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';
|
2023-01-27 16:00:17 +08:00
|
|
|
import useConfigInject from '../config-provider/hooks/useConfigInject';
|
2021-06-07 17:35:03 +08:00
|
|
|
import useProvideAnchor from './context';
|
2023-01-25 17:23:03 +08:00
|
|
|
import useStyle from './style';
|
2023-04-21 21:29:20 +08:00
|
|
|
import type { AnchorLinkItemProps } from './AnchorLink';
|
2023-01-25 17:23:03 +08:00
|
|
|
import AnchorLink from './AnchorLink';
|
2023-04-21 17:33:38 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import devWarning from '../vc-util/devWarning';
|
2023-04-21 21:29:20 +08:00
|
|
|
import { arrayType } from '../_util/type';
|
2023-04-21 17:33:38 +08:00
|
|
|
|
|
|
|
export type AnchorDirection = 'vertical' | 'horizontal';
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getDefaultContainer() {
|
|
|
|
return window;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:54:43 +08:00
|
|
|
function getOffsetTop(element: HTMLElement, container: AnchorContainer): number {
|
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) {
|
2021-06-07 17:35:03 +08:00
|
|
|
container = element.ownerDocument!.documentElement!;
|
2019-01-12 11:33:27 +08:00
|
|
|
return rect.top - container.clientTop;
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
2020-10-12 17:54:43 +08:00
|
|
|
return rect.top - (container as HTMLElement).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
|
|
|
}
|
|
|
|
|
2022-03-12 09:56:32 +08:00
|
|
|
const sharpMatcherRegx = /#([\S ]+)$/;
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2023-01-25 17:23:03 +08:00
|
|
|
interface Section {
|
2020-10-12 17:54:43 +08:00
|
|
|
link: string;
|
|
|
|
top: number;
|
2023-01-25 17:23:03 +08:00
|
|
|
}
|
2020-10-12 17:54:43 +08:00
|
|
|
|
|
|
|
export type AnchorContainer = HTMLElement | Window;
|
|
|
|
|
2022-03-12 09:56:32 +08:00
|
|
|
export const anchorProps = () => ({
|
|
|
|
prefixCls: String,
|
|
|
|
offsetTop: Number,
|
|
|
|
bounds: Number,
|
|
|
|
affix: { type: Boolean, default: true },
|
|
|
|
showInkInFixed: { type: Boolean, default: false },
|
|
|
|
getContainer: Function as PropType<() => AnchorContainer>,
|
|
|
|
wrapperClass: String,
|
|
|
|
wrapperStyle: { type: Object as PropType<CSSProperties>, default: undefined as CSSProperties },
|
2022-05-10 15:35:44 +08:00
|
|
|
getCurrentAnchor: Function as PropType<(activeLink: string) => string>,
|
2022-03-12 09:56:32 +08:00
|
|
|
targetOffset: Number,
|
2023-04-21 21:29:20 +08:00
|
|
|
items: arrayType<AnchorLinkItemProps[]>(),
|
2023-04-21 17:33:38 +08:00
|
|
|
direction: PropTypes.oneOf(['vertical', 'horizontal'] as AnchorDirection[]).def('vertical'),
|
2022-03-12 09:56:32 +08:00
|
|
|
onChange: Function as PropType<(currentActiveLink: string) => void>,
|
|
|
|
onClick: Function as PropType<(e: MouseEvent, link: { title: any; href: string }) => void>,
|
|
|
|
});
|
|
|
|
|
|
|
|
export type AnchorProps = Partial<ExtractPropTypes<ReturnType<typeof anchorProps>>>;
|
2021-06-07 17:35:03 +08:00
|
|
|
|
2020-10-19 16:43:10 +08:00
|
|
|
export interface AnchorState {
|
2020-10-13 17:46:52 +08:00
|
|
|
scrollContainer: HTMLElement | Window;
|
|
|
|
links: string[];
|
|
|
|
scrollEvent: any;
|
|
|
|
animating: boolean;
|
2020-10-12 17:54:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defineComponent({
|
2022-09-26 21:33:41 +08:00
|
|
|
compatConfig: { MODE: 3 },
|
2018-06-02 18:11:50 +08:00
|
|
|
name: 'AAnchor',
|
|
|
|
inheritAttrs: false,
|
2022-03-12 09:56:32 +08:00
|
|
|
props: anchorProps(),
|
2021-06-07 17:35:03 +08:00
|
|
|
setup(props, { emit, attrs, slots, expose }) {
|
|
|
|
const { prefixCls, getTargetContainer, direction } = useConfigInject('anchor', props);
|
2023-04-21 17:33:38 +08:00
|
|
|
const anchorDirection = computed(() => props.direction ?? 'vertical');
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
devWarning(
|
2023-04-21 21:29:20 +08:00
|
|
|
props.items && typeof slots.default !== 'function',
|
2023-04-21 17:33:38 +08:00
|
|
|
'Anchor',
|
|
|
|
'`Anchor children` is deprecated. Please use `items` instead.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
devWarning(
|
|
|
|
!(anchorDirection.value === 'horizontal' && props.items?.some(n => 'children' in n)),
|
|
|
|
'Anchor',
|
|
|
|
'`Anchor items#children` is not supported when `Anchor` direction is horizontal.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-25 17:23:03 +08:00
|
|
|
const spanLinkNode = ref<HTMLSpanElement>(null);
|
2021-06-07 17:35:03 +08:00
|
|
|
const anchorRef = ref();
|
|
|
|
const state = reactive<AnchorState>({
|
2020-10-12 17:54:43 +08:00
|
|
|
links: [],
|
|
|
|
scrollContainer: null,
|
|
|
|
scrollEvent: null,
|
|
|
|
animating: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2021-06-20 22:53:42 +08:00
|
|
|
const activeLink = ref(null);
|
2021-06-07 17:35:03 +08:00
|
|
|
const getContainer = computed(() => {
|
|
|
|
const { getContainer } = props;
|
2023-02-02 18:05:58 +08:00
|
|
|
return getContainer || getTargetContainer?.value || getDefaultContainer;
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2021-06-07 17:35:03 +08:00
|
|
|
// func...
|
|
|
|
const getCurrentAnchor = (offsetTop = 0, bounds = 5) => {
|
2020-10-12 17:54:43 +08:00
|
|
|
const linkSections: Array<Section> = [];
|
2021-06-07 17:35:03 +08:00
|
|
|
const container = getContainer.value();
|
|
|
|
state.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 '';
|
2021-06-07 17:35:03 +08:00
|
|
|
};
|
|
|
|
const setCurrentActiveLink = (link: string) => {
|
|
|
|
const { getCurrentAnchor } = props;
|
2021-06-20 22:53:42 +08:00
|
|
|
if (activeLink.value === link) {
|
2021-06-07 17:35:03 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-05-10 15:35:44 +08:00
|
|
|
activeLink.value = typeof getCurrentAnchor === 'function' ? getCurrentAnchor(link) : link;
|
2021-06-07 17:35:03 +08:00
|
|
|
emit('change', link);
|
|
|
|
};
|
|
|
|
const handleScrollTo = (link: string) => {
|
2022-03-12 09:56:32 +08:00
|
|
|
const { offsetTop, targetOffset } = props;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
setCurrentActiveLink(link);
|
2020-03-07 19:45:13 +08:00
|
|
|
const sharpLinkMatch = sharpMatcherRegx.exec(link);
|
|
|
|
if (!sharpLinkMatch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const targetElement = document.getElementById(sharpLinkMatch[1]);
|
|
|
|
if (!targetElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-03 13:14:59 +08:00
|
|
|
const container = getContainer.value();
|
|
|
|
const scrollTop = getScroll(container, true);
|
2020-03-07 19:45:13 +08:00
|
|
|
const eleOffsetTop = getOffsetTop(targetElement, container);
|
|
|
|
let y = scrollTop + eleOffsetTop;
|
|
|
|
y -= targetOffset !== undefined ? targetOffset : offsetTop || 0;
|
2021-06-07 17:35:03 +08:00
|
|
|
state.animating = true;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
|
|
|
scrollTo(y, {
|
|
|
|
callback: () => {
|
2021-06-07 17:35:03 +08:00
|
|
|
state.animating = false;
|
2020-03-07 19:45:13 +08:00
|
|
|
},
|
2022-03-12 09:56:32 +08:00
|
|
|
getContainer: getContainer.value,
|
2020-03-07 19:45:13 +08:00
|
|
|
});
|
2021-06-07 17:35:03 +08:00
|
|
|
};
|
|
|
|
expose({
|
|
|
|
scrollTo: handleScrollTo,
|
|
|
|
});
|
|
|
|
const handleScroll = () => {
|
|
|
|
if (state.animating) {
|
2020-03-07 19:45:13 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
const { offsetTop, bounds, targetOffset } = props;
|
|
|
|
const currentActiveLink = getCurrentAnchor(
|
2020-03-07 19:45:13 +08:00
|
|
|
targetOffset !== undefined ? targetOffset : offsetTop || 0,
|
|
|
|
bounds,
|
|
|
|
);
|
2021-06-07 17:35:03 +08:00
|
|
|
setCurrentActiveLink(currentActiveLink);
|
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
const updateInk = () => {
|
2023-04-21 17:33:38 +08:00
|
|
|
const linkNode = anchorRef.value.querySelector(`.${prefixCls.value}-link-title-active`);
|
2023-01-25 17:23:03 +08:00
|
|
|
if (linkNode && spanLinkNode.value) {
|
2023-04-21 17:33:38 +08:00
|
|
|
const horizontalAnchor = anchorDirection.value === 'horizontal';
|
|
|
|
spanLinkNode.value.style.top = horizontalAnchor
|
|
|
|
? ''
|
|
|
|
: `${linkNode.offsetTop + linkNode.clientHeight / 2}px`;
|
|
|
|
spanLinkNode.value.style.height = horizontalAnchor ? '' : `${linkNode.clientHeight}px`;
|
|
|
|
spanLinkNode.value.style.left = horizontalAnchor ? `${linkNode.offsetLeft}px` : '';
|
|
|
|
spanLinkNode.value.style.width = horizontalAnchor ? `${linkNode.clientWidth}px` : '';
|
|
|
|
if (horizontalAnchor) {
|
|
|
|
scrollIntoView(linkNode, {
|
|
|
|
scrollMode: 'if-needed',
|
|
|
|
block: 'nearest',
|
|
|
|
});
|
|
|
|
}
|
2018-06-02 18:11:50 +08:00
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
};
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
useProvideAnchor({
|
|
|
|
registerLink: (link: string) => {
|
|
|
|
if (!state.links.includes(link)) {
|
|
|
|
state.links.push(link);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unregisterLink: (link: string) => {
|
|
|
|
const index = state.links.indexOf(link);
|
|
|
|
if (index !== -1) {
|
|
|
|
state.links.splice(index, 1);
|
|
|
|
}
|
|
|
|
},
|
2019-05-28 11:37:38 +08:00
|
|
|
activeLink,
|
2021-06-07 17:35:03 +08:00
|
|
|
scrollTo: handleScrollTo,
|
|
|
|
handleClick: (e, info) => {
|
|
|
|
emit('click', e, info);
|
|
|
|
},
|
2023-04-21 17:33:38 +08:00
|
|
|
direction: anchorDirection,
|
2021-06-07 17:35:03 +08:00
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
const container = getContainer.value();
|
|
|
|
state.scrollContainer = container;
|
|
|
|
state.scrollEvent = addEventListener(state.scrollContainer, 'scroll', handleScroll);
|
|
|
|
handleScroll();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
if (state.scrollEvent) {
|
|
|
|
state.scrollEvent.remove();
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2021-06-07 17:35:03 +08:00
|
|
|
onUpdated(() => {
|
|
|
|
if (state.scrollEvent) {
|
|
|
|
const currentContainer = getContainer.value();
|
|
|
|
if (state.scrollContainer !== currentContainer) {
|
|
|
|
state.scrollContainer = currentContainer;
|
|
|
|
state.scrollEvent.remove();
|
|
|
|
state.scrollEvent = addEventListener(state.scrollContainer, 'scroll', handleScroll);
|
|
|
|
handleScroll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateInk();
|
|
|
|
});
|
2023-04-21 17:33:38 +08:00
|
|
|
|
2023-01-25 17:23:03 +08:00
|
|
|
const createNestedLink = (options?: AnchorLinkItemProps[]) =>
|
|
|
|
Array.isArray(options)
|
2023-04-21 21:29:20 +08:00
|
|
|
? options.map(option => {
|
|
|
|
const { children, key, href, target, class: cls, style, title } = option;
|
|
|
|
return (
|
|
|
|
<AnchorLink
|
|
|
|
key={key}
|
|
|
|
href={href}
|
|
|
|
target={target}
|
|
|
|
class={cls}
|
|
|
|
style={style}
|
|
|
|
title={title}
|
|
|
|
customTitleProps={option}
|
|
|
|
v-slots={{ customTitle: slots.customTitle }}
|
|
|
|
>
|
|
|
|
{anchorDirection.value === 'vertical' ? createNestedLink(children) : null}
|
|
|
|
</AnchorLink>
|
|
|
|
);
|
|
|
|
})
|
2023-01-25 17:23:03 +08:00
|
|
|
: null;
|
2023-04-21 17:33:38 +08:00
|
|
|
|
2023-01-25 17:23:03 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2023-04-21 17:33:38 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
return () => {
|
|
|
|
const { offsetTop, affix, showInkInFixed } = props;
|
|
|
|
const pre = prefixCls.value;
|
2023-04-21 17:33:38 +08:00
|
|
|
const inkClass = classNames(`${pre}-ink`, {
|
|
|
|
[`${pre}-ink-visible`]: activeLink.value,
|
2021-06-07 17:35:03 +08:00
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2023-01-25 17:23:03 +08:00
|
|
|
const wrapperClass = classNames(hashId.value, props.wrapperClass, `${pre}-wrapper`, {
|
2023-04-21 17:33:38 +08:00
|
|
|
[`${pre}-wrapper-horizontal`]: anchorDirection.value === 'horizontal',
|
2021-06-07 17:35:03 +08:00
|
|
|
[`${pre}-rtl`]: direction.value === 'rtl',
|
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
const anchorClass = classNames(pre, {
|
2021-11-28 10:14:12 +08:00
|
|
|
[`${pre}-fixed`]: !affix && !showInkInFixed,
|
2021-06-07 17:35:03 +08:00
|
|
|
});
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
const wrapperStyle = {
|
|
|
|
maxHeight: offsetTop ? `calc(100vh - ${offsetTop}px)` : '100vh',
|
|
|
|
...props.wrapperStyle,
|
|
|
|
};
|
|
|
|
const anchorContent = (
|
|
|
|
<div class={wrapperClass} style={wrapperStyle} ref={anchorRef}>
|
|
|
|
<div class={anchorClass}>
|
2023-04-21 17:33:38 +08:00
|
|
|
<span class={inkClass} ref={spanLinkNode} />
|
2023-01-25 17:23:03 +08:00
|
|
|
{Array.isArray(props.items) ? createNestedLink(props.items) : slots.default?.()}
|
2018-06-02 18:11:50 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-06-07 17:35:03 +08:00
|
|
|
);
|
2018-06-02 18:11:50 +08:00
|
|
|
|
2023-01-25 17:23:03 +08:00
|
|
|
return wrapSSR(
|
|
|
|
!affix ? (
|
|
|
|
anchorContent
|
|
|
|
) : (
|
|
|
|
<Affix {...attrs} offsetTop={offsetTop} target={getContainer.value}>
|
|
|
|
{anchorContent}
|
|
|
|
</Affix>
|
|
|
|
),
|
2021-06-07 17:35:03 +08:00
|
|
|
);
|
|
|
|
};
|
2018-06-02 18:11:50 +08:00
|
|
|
},
|
2020-10-12 17:54:43 +08:00
|
|
|
});
|