helper.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var dom = require('@floating-ui/dom');
  5. var shared = require('@vue/shared');
  6. var core = require('@vueuse/core');
  7. var objects = require('../../../utils/objects.js');
  8. const useTarget = (target, open, gap, mergedMask, scrollIntoViewOptions) => {
  9. const posInfo = vue.ref(null);
  10. const getTargetEl = () => {
  11. let targetEl;
  12. if (shared.isString(target.value)) {
  13. targetEl = document.querySelector(target.value);
  14. } else if (shared.isFunction(target.value)) {
  15. targetEl = target.value();
  16. } else {
  17. targetEl = target.value;
  18. }
  19. return targetEl;
  20. };
  21. const updatePosInfo = () => {
  22. const targetEl = getTargetEl();
  23. if (!targetEl || !open.value) {
  24. posInfo.value = null;
  25. return;
  26. }
  27. if (!isInViewPort(targetEl)) {
  28. targetEl.scrollIntoView(scrollIntoViewOptions.value);
  29. }
  30. const { left, top, width, height } = targetEl.getBoundingClientRect();
  31. posInfo.value = {
  32. left,
  33. top,
  34. width,
  35. height,
  36. radius: 0
  37. };
  38. };
  39. vue.onMounted(() => {
  40. vue.watch(
  41. [open, target],
  42. () => {
  43. updatePosInfo();
  44. },
  45. {
  46. immediate: true
  47. }
  48. );
  49. window.addEventListener("resize", updatePosInfo);
  50. });
  51. vue.onBeforeUnmount(() => {
  52. window.removeEventListener("resize", updatePosInfo);
  53. });
  54. const getGapOffset = (index) => {
  55. var _a;
  56. return (_a = shared.isArray(gap.value.offset) ? gap.value.offset[index] : gap.value.offset) != null ? _a : 6;
  57. };
  58. const mergedPosInfo = vue.computed(() => {
  59. var _a;
  60. if (!posInfo.value)
  61. return posInfo.value;
  62. const gapOffsetX = getGapOffset(0);
  63. const gapOffsetY = getGapOffset(1);
  64. const gapRadius = ((_a = gap.value) == null ? void 0 : _a.radius) || 2;
  65. return {
  66. left: posInfo.value.left - gapOffsetX,
  67. top: posInfo.value.top - gapOffsetY,
  68. width: posInfo.value.width + gapOffsetX * 2,
  69. height: posInfo.value.height + gapOffsetY * 2,
  70. radius: gapRadius
  71. };
  72. });
  73. const triggerTarget = vue.computed(() => {
  74. const targetEl = getTargetEl();
  75. if (!mergedMask.value || !targetEl || !window.DOMRect) {
  76. return targetEl || void 0;
  77. }
  78. return {
  79. getBoundingClientRect() {
  80. var _a, _b, _c, _d;
  81. return window.DOMRect.fromRect({
  82. width: ((_a = mergedPosInfo.value) == null ? void 0 : _a.width) || 0,
  83. height: ((_b = mergedPosInfo.value) == null ? void 0 : _b.height) || 0,
  84. x: ((_c = mergedPosInfo.value) == null ? void 0 : _c.left) || 0,
  85. y: ((_d = mergedPosInfo.value) == null ? void 0 : _d.top) || 0
  86. });
  87. }
  88. };
  89. });
  90. return {
  91. mergedPosInfo,
  92. triggerTarget
  93. };
  94. };
  95. const tourKey = Symbol("ElTour");
  96. function isInViewPort(element) {
  97. const viewWidth = window.innerWidth || document.documentElement.clientWidth;
  98. const viewHeight = window.innerHeight || document.documentElement.clientHeight;
  99. const { top, right, bottom, left } = element.getBoundingClientRect();
  100. return top >= 0 && left >= 0 && right <= viewWidth && bottom <= viewHeight;
  101. }
  102. const useFloating = (referenceRef, contentRef, arrowRef, placement, strategy, offset, zIndex, showArrow) => {
  103. const x = vue.ref();
  104. const y = vue.ref();
  105. const middlewareData = vue.ref({});
  106. const states = {
  107. x,
  108. y,
  109. placement,
  110. strategy,
  111. middlewareData
  112. };
  113. const middleware = vue.computed(() => {
  114. const _middleware = [
  115. dom.offset(vue.unref(offset)),
  116. dom.flip(),
  117. dom.shift(),
  118. overflowMiddleware()
  119. ];
  120. if (vue.unref(showArrow) && vue.unref(arrowRef)) {
  121. _middleware.push(
  122. dom.arrow({
  123. element: vue.unref(arrowRef)
  124. })
  125. );
  126. }
  127. return _middleware;
  128. });
  129. const update = async () => {
  130. if (!core.isClient)
  131. return;
  132. const referenceEl = vue.unref(referenceRef);
  133. const contentEl = vue.unref(contentRef);
  134. if (!referenceEl || !contentEl)
  135. return;
  136. const data = await dom.computePosition(referenceEl, contentEl, {
  137. placement: vue.unref(placement),
  138. strategy: vue.unref(strategy),
  139. middleware: vue.unref(middleware)
  140. });
  141. objects.keysOf(states).forEach((key) => {
  142. states[key].value = data[key];
  143. });
  144. };
  145. const contentStyle = vue.computed(() => {
  146. if (!vue.unref(referenceRef)) {
  147. return {
  148. position: "fixed",
  149. top: "50%",
  150. left: "50%",
  151. transform: "translate3d(-50%, -50%, 0)",
  152. maxWidth: "100vw",
  153. zIndex: vue.unref(zIndex)
  154. };
  155. }
  156. const { overflow } = vue.unref(middlewareData);
  157. return {
  158. position: vue.unref(strategy),
  159. zIndex: vue.unref(zIndex),
  160. top: vue.unref(y) != null ? `${vue.unref(y)}px` : "",
  161. left: vue.unref(x) != null ? `${vue.unref(x)}px` : "",
  162. maxWidth: (overflow == null ? void 0 : overflow.maxWidth) ? `${overflow == null ? void 0 : overflow.maxWidth}px` : ""
  163. };
  164. });
  165. const arrowStyle = vue.computed(() => {
  166. if (!vue.unref(showArrow))
  167. return {};
  168. const { arrow: arrow2 } = vue.unref(middlewareData);
  169. return {
  170. left: (arrow2 == null ? void 0 : arrow2.x) != null ? `${arrow2 == null ? void 0 : arrow2.x}px` : "",
  171. top: (arrow2 == null ? void 0 : arrow2.y) != null ? `${arrow2 == null ? void 0 : arrow2.y}px` : ""
  172. };
  173. });
  174. let cleanup;
  175. vue.onMounted(() => {
  176. const referenceEl = vue.unref(referenceRef);
  177. const contentEl = vue.unref(contentRef);
  178. if (referenceEl && contentEl) {
  179. cleanup = dom.autoUpdate(referenceEl, contentEl, update);
  180. }
  181. vue.watchEffect(() => {
  182. update();
  183. });
  184. });
  185. vue.onBeforeUnmount(() => {
  186. cleanup && cleanup();
  187. });
  188. return {
  189. update,
  190. contentStyle,
  191. arrowStyle
  192. };
  193. };
  194. const overflowMiddleware = () => {
  195. return {
  196. name: "overflow",
  197. async fn(state) {
  198. const overflow = await dom.detectOverflow(state);
  199. let overWidth = 0;
  200. if (overflow.left > 0)
  201. overWidth = overflow.left;
  202. if (overflow.right > 0)
  203. overWidth = overflow.right;
  204. const floatingWidth = state.rects.floating.width;
  205. return {
  206. data: {
  207. maxWidth: floatingWidth - overWidth
  208. }
  209. };
  210. }
  211. };
  212. };
  213. exports.tourKey = tourKey;
  214. exports.useFloating = useFloating;
  215. exports.useTarget = useTarget;
  216. //# sourceMappingURL=helper.js.map