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.

0 lines
17 KiB

1 month ago
  1. {"ast":null,"code":"import { defineComponent, ref, computed, onMounted, watch, provide, openBlock, createElementBlock, normalizeClass, unref, normalizeStyle, createCommentVNode, createElementVNode, renderSlot } from 'vue';\nimport { useEventListener } from '@vueuse/core';\nimport { anchorProps, anchorEmits } from './anchor.mjs';\nimport { anchorKey } from './constants.mjs';\nimport _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';\nimport { getElement } from '../../../utils/dom/element.mjs';\nimport { throttleByRaf } from '../../../utils/throttleByRaf.mjs';\nimport { isWindow, isUndefined } from '../../../utils/types.mjs';\nimport { getScrollElement, animateScrollTo, getScrollTop } from '../../../utils/dom/scroll.mjs';\nimport { getOffsetTopDistance } from '../../../utils/dom/position.mjs';\nimport { useNamespace } from '../../../hooks/use-namespace/index.mjs';\nconst __default__ = defineComponent({\n name: \"ElAnchor\"\n});\nconst _sfc_main = /* @__PURE__ */defineComponent({\n ...__default__,\n props: anchorProps,\n emits: anchorEmits,\n setup(__props, {\n expose,\n emit\n }) {\n const props = __props;\n const currentAnchor = ref(\"\");\n const anchorRef = ref(null);\n const markerRef = ref(null);\n const containerEl = ref();\n const links = {};\n let isScrolling = false;\n let currentScrollTop = 0;\n const ns = useNamespace(\"anchor\");\n const cls = computed(() => [ns.b(), props.type === \"underline\" ? ns.m(\"underline\") : \"\", ns.m(props.direction)]);\n const addLink = state => {\n links[state.href] = state.el;\n };\n const removeLink = href => {\n delete links[href];\n };\n const setCurrentAnchor = href => {\n const activeHref = currentAnchor.value;\n if (activeHref !== href) {\n currentAnchor.value = href;\n emit(\"change\", href);\n }\n };\n let clearAnimate = null;\n const scrollToAnchor = href => {\n if (!containerEl.value) return;\n const target = getElement(href);\n if (!target) return;\n if (clearAnimate) clearAnimate();\n isScrolling = true;\n const scrollEle = getScrollElement(target, containerEl.value);\n const distance = getOffsetTopDistance(target, scrollEle);\n const max = scrollEle.scrollHeight - scrollEle.clientHeight;\n const to = Math.min(distance - props.offset, max);\n clearAnimate = animateScrollTo(containerEl.value, currentScrollTop, to, props.duration, () => {\n setTimeout(() => {\n isScrolling = false;\n }, 20);\n });\n };\n const scrollTo = href => {\n if (href) {\n setCurrentAnchor(href);\n scrollToAnchor(href);\n }\n };\n const handleClick = (e, href) => {\n emit(\"click\", e, href);\n scrollTo(href);\n };\n const handleScroll = throttleByRaf(() => {\n if (containerEl.value) {\n currentScrollTop = getScrollTop(containerEl.value);\n }\n const currentHref = getCurrentHref();\n if (isScrolling || isUndefined(currentHref)) return;\n setCurrentAnchor(currentHref);\n });\n const getCurrentHref = () => {\n if (!containerEl.value) return;\n const scrollTop = getScrollTop(containerEl.value);\n const anchorTopList = [];\n for (const href of Object.keys(links)) {\n const target = getElement(href);\n if (!target) continue;\n const scrollEle = getScrollElement(target, containerEl.value);\n const distance = getOffsetTopDistance(target, scrollEle);\n anchorTopList.push({\n top: distance - props.offset - props.bound,\n href\n });\n }\n anchorTopList.sort((prev, next) => prev.top - next.top);\n for (let i = 0; i < anchorTopList.length; i++) {\n const item = anchorTopList[i];\n const next = anchorTopList[i + 1];\n if (i === 0 && scrollTop === 0) {\n return props.selectScrollTop ? item.href : \"\";\n }\n if (item.top <= scrollTop && (!next || next.top > scrollTop)) {\n return