helper.js 6.2 KB

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