anchor2.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var core = require('@vueuse/core');
  5. var anchor = require('./anchor.js');
  6. var constants = require('./constants.js');
  7. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  8. var element = require('../../../utils/dom/element.js');
  9. var throttleByRaf = require('../../../utils/throttleByRaf.js');
  10. var types = require('../../../utils/types.js');
  11. var scroll = require('../../../utils/dom/scroll.js');
  12. var position = require('../../../utils/dom/position.js');
  13. var index = require('../../../hooks/use-namespace/index.js');
  14. var event = require('../../../constants/event.js');
  15. const __default__ = vue.defineComponent({
  16. name: "ElAnchor"
  17. });
  18. const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  19. ...__default__,
  20. props: anchor.anchorProps,
  21. emits: anchor.anchorEmits,
  22. setup(__props, { expose, emit }) {
  23. const props = __props;
  24. const currentAnchor = vue.ref("");
  25. const anchorRef = vue.ref(null);
  26. const markerRef = vue.ref(null);
  27. const containerEl = vue.ref();
  28. const links = {};
  29. let isScrolling = false;
  30. let currentScrollTop = 0;
  31. const ns = index.useNamespace("anchor");
  32. const cls = vue.computed(() => [
  33. ns.b(),
  34. props.type === "underline" ? ns.m("underline") : "",
  35. ns.m(props.direction)
  36. ]);
  37. const addLink = (state) => {
  38. links[state.href] = state.el;
  39. };
  40. const removeLink = (href) => {
  41. delete links[href];
  42. };
  43. const setCurrentAnchor = (href) => {
  44. const activeHref = currentAnchor.value;
  45. if (activeHref !== href) {
  46. currentAnchor.value = href;
  47. emit(event.CHANGE_EVENT, href);
  48. }
  49. };
  50. let clearAnimate = null;
  51. const scrollToAnchor = (href) => {
  52. if (!containerEl.value)
  53. return;
  54. const target = element.getElement(href);
  55. if (!target)
  56. return;
  57. if (clearAnimate)
  58. clearAnimate();
  59. isScrolling = true;
  60. const scrollEle = scroll.getScrollElement(target, containerEl.value);
  61. const distance = position.getOffsetTopDistance(target, scrollEle);
  62. const max = scrollEle.scrollHeight - scrollEle.clientHeight;
  63. const to = Math.min(distance - props.offset, max);
  64. clearAnimate = scroll.animateScrollTo(containerEl.value, currentScrollTop, to, props.duration, () => {
  65. setTimeout(() => {
  66. isScrolling = false;
  67. }, 20);
  68. });
  69. };
  70. const scrollTo = (href) => {
  71. if (href) {
  72. setCurrentAnchor(href);
  73. scrollToAnchor(href);
  74. }
  75. };
  76. const handleClick = (e, href) => {
  77. emit("click", e, href);
  78. scrollTo(href);
  79. };
  80. const handleScroll = throttleByRaf.throttleByRaf(() => {
  81. if (containerEl.value) {
  82. currentScrollTop = scroll.getScrollTop(containerEl.value);
  83. }
  84. const currentHref = getCurrentHref();
  85. if (isScrolling || types.isUndefined(currentHref))
  86. return;
  87. setCurrentAnchor(currentHref);
  88. });
  89. const getCurrentHref = () => {
  90. if (!containerEl.value)
  91. return;
  92. const scrollTop = scroll.getScrollTop(containerEl.value);
  93. const anchorTopList = [];
  94. for (const href of Object.keys(links)) {
  95. const target = element.getElement(href);
  96. if (!target)
  97. continue;
  98. const scrollEle = scroll.getScrollElement(target, containerEl.value);
  99. const distance = position.getOffsetTopDistance(target, scrollEle);
  100. anchorTopList.push({
  101. top: distance - props.offset - props.bound,
  102. href
  103. });
  104. }
  105. anchorTopList.sort((prev, next) => prev.top - next.top);
  106. for (let i = 0; i < anchorTopList.length; i++) {
  107. const item = anchorTopList[i];
  108. const next = anchorTopList[i + 1];
  109. if (i === 0 && scrollTop === 0) {
  110. return props.selectScrollTop ? item.href : "";
  111. }
  112. if (item.top <= scrollTop && (!next || next.top > scrollTop)) {
  113. return item.href;
  114. }
  115. }
  116. };
  117. const getContainer = () => {
  118. const el = element.getElement(props.container);
  119. if (!el || types.isWindow(el)) {
  120. containerEl.value = window;
  121. } else {
  122. containerEl.value = el;
  123. }
  124. };
  125. core.useEventListener(containerEl, "scroll", handleScroll);
  126. const markerStyle = vue.computed(() => {
  127. if (!anchorRef.value || !markerRef.value || !currentAnchor.value)
  128. return {};
  129. const currentLinkEl = links[currentAnchor.value];
  130. if (!currentLinkEl)
  131. return {};
  132. const anchorRect = anchorRef.value.getBoundingClientRect();
  133. const markerRect = markerRef.value.getBoundingClientRect();
  134. const linkRect = currentLinkEl.getBoundingClientRect();
  135. if (props.direction === "horizontal") {
  136. const left = linkRect.left - anchorRect.left;
  137. return {
  138. left: `${left}px`,
  139. width: `${linkRect.width}px`,
  140. opacity: 1
  141. };
  142. } else {
  143. const top = linkRect.top - anchorRect.top + (linkRect.height - markerRect.height) / 2;
  144. return {
  145. top: `${top}px`,
  146. opacity: 1
  147. };
  148. }
  149. });
  150. vue.onMounted(() => {
  151. getContainer();
  152. const hash = decodeURIComponent(window.location.hash);
  153. const target = element.getElement(hash);
  154. if (target) {
  155. scrollTo(hash);
  156. } else {
  157. handleScroll();
  158. }
  159. });
  160. vue.watch(() => props.container, () => {
  161. getContainer();
  162. });
  163. vue.provide(constants.anchorKey, {
  164. ns,
  165. direction: props.direction,
  166. currentAnchor,
  167. addLink,
  168. removeLink,
  169. handleClick
  170. });
  171. expose({
  172. scrollTo
  173. });
  174. return (_ctx, _cache) => {
  175. return vue.openBlock(), vue.createElementBlock("div", {
  176. ref_key: "anchorRef",
  177. ref: anchorRef,
  178. class: vue.normalizeClass(vue.unref(cls))
  179. }, [
  180. _ctx.marker ? (vue.openBlock(), vue.createElementBlock("div", {
  181. key: 0,
  182. ref_key: "markerRef",
  183. ref: markerRef,
  184. class: vue.normalizeClass(vue.unref(ns).e("marker")),
  185. style: vue.normalizeStyle(vue.unref(markerStyle))
  186. }, null, 6)) : vue.createCommentVNode("v-if", true),
  187. vue.createElementVNode("div", {
  188. class: vue.normalizeClass(vue.unref(ns).e("list"))
  189. }, [
  190. vue.renderSlot(_ctx.$slots, "default")
  191. ], 2)
  192. ], 2);
  193. };
  194. }
  195. });
  196. var Anchor = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "anchor.vue"]]);
  197. exports["default"] = Anchor;
  198. //# sourceMappingURL=anchor2.js.map