2021-06-26 09:35:40 +08:00
|
|
|
import type { Ref, InjectionKey } from 'vue';
|
|
|
|
import { computed, inject, provide } from 'vue';
|
2021-06-07 17:35:03 +08:00
|
|
|
|
|
|
|
export interface AnchorContext {
|
|
|
|
registerLink: (link: string) => void;
|
|
|
|
unregisterLink: (link: string) => void;
|
|
|
|
activeLink: Ref<string>;
|
|
|
|
scrollTo: (link: string) => void;
|
|
|
|
handleClick: (e: Event, info: { title: any; href: string }) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
function noop(..._any: any[]): any {}
|
|
|
|
|
|
|
|
export const AnchorContextKey: InjectionKey<AnchorContext> = Symbol('anchorContextKey');
|
|
|
|
|
|
|
|
const useProvideAnchor = (state: AnchorContext) => {
|
|
|
|
provide(AnchorContextKey, state);
|
|
|
|
};
|
|
|
|
|
|
|
|
const useInjectAnchor = () => {
|
|
|
|
return inject(AnchorContextKey, {
|
|
|
|
registerLink: noop,
|
|
|
|
unregisterLink: noop,
|
|
|
|
scrollTo: noop,
|
|
|
|
activeLink: computed(() => ''),
|
|
|
|
handleClick: noop,
|
|
|
|
} as AnchorContext);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { useInjectAnchor, useProvideAnchor };
|
|
|
|
export default useProvideAnchor;
|