image2.mjs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { defineComponent, useAttrs, ref, computed, nextTick, watch, onMounted, openBlock, createElementBlock, normalizeClass, unref, normalizeStyle, mergeProps, createCommentVNode, renderSlot, createElementVNode, toDisplayString, Fragment, createBlock, withCtx } from 'vue';
  2. import { isClient, useThrottleFn, useEventListener } from '@vueuse/core';
  3. import '../../../hooks/index.mjs';
  4. import { ElImageViewer } from '../../image-viewer/index.mjs';
  5. import '../../../utils/index.mjs';
  6. import { imageProps, imageEmits } from './image.mjs';
  7. import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
  8. import { useLocale } from '../../../hooks/use-locale/index.mjs';
  9. import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
  10. import { useAttrs as useAttrs$1 } from '../../../hooks/use-attrs/index.mjs';
  11. import { isInContainer } from '../../../utils/dom/position.mjs';
  12. import { isElement } from '../../../utils/types.mjs';
  13. import { isString } from '@vue/shared';
  14. import { getScrollContainer } from '../../../utils/dom/scroll.mjs';
  15. const _hoisted_1 = ["src", "loading"];
  16. const _hoisted_2 = { key: 0 };
  17. const __default__ = defineComponent({
  18. name: "ElImage",
  19. inheritAttrs: false
  20. });
  21. const _sfc_main = /* @__PURE__ */ defineComponent({
  22. ...__default__,
  23. props: imageProps,
  24. emits: imageEmits,
  25. setup(__props, { emit }) {
  26. const props = __props;
  27. let prevOverflow = "";
  28. const { t } = useLocale();
  29. const ns = useNamespace("image");
  30. const rawAttrs = useAttrs();
  31. const attrs = useAttrs$1();
  32. const imageSrc = ref();
  33. const hasLoadError = ref(false);
  34. const isLoading = ref(true);
  35. const showViewer = ref(false);
  36. const container = ref();
  37. const _scrollContainer = ref();
  38. const supportLoading = isClient && "loading" in HTMLImageElement.prototype;
  39. let stopScrollListener;
  40. let stopWheelListener;
  41. const containerStyle = computed(() => rawAttrs.style);
  42. const imageStyle = computed(() => {
  43. const { fit } = props;
  44. if (isClient && fit) {
  45. return { objectFit: fit };
  46. }
  47. return {};
  48. });
  49. const preview = computed(() => {
  50. const { previewSrcList } = props;
  51. return Array.isArray(previewSrcList) && previewSrcList.length > 0;
  52. });
  53. const imageIndex = computed(() => {
  54. const { previewSrcList, initialIndex } = props;
  55. let previewIndex = initialIndex;
  56. if (initialIndex > previewSrcList.length - 1) {
  57. previewIndex = 0;
  58. }
  59. return previewIndex;
  60. });
  61. const isManual = computed(() => {
  62. if (props.loading === "eager")
  63. return false;
  64. return !supportLoading && props.loading === "lazy" || props.lazy;
  65. });
  66. const loadImage = () => {
  67. if (!isClient)
  68. return;
  69. isLoading.value = true;
  70. hasLoadError.value = false;
  71. imageSrc.value = props.src;
  72. };
  73. function handleLoad(event) {
  74. isLoading.value = false;
  75. hasLoadError.value = false;
  76. emit("load", event);
  77. }
  78. function handleError(event) {
  79. isLoading.value = false;
  80. hasLoadError.value = true;
  81. emit("error", event);
  82. }
  83. function handleLazyLoad() {
  84. if (isInContainer(container.value, _scrollContainer.value)) {
  85. loadImage();
  86. removeLazyLoadListener();
  87. }
  88. }
  89. const lazyLoadHandler = useThrottleFn(handleLazyLoad, 200);
  90. async function addLazyLoadListener() {
  91. var _a;
  92. if (!isClient)
  93. return;
  94. await nextTick();
  95. const { scrollContainer } = props;
  96. if (isElement(scrollContainer)) {
  97. _scrollContainer.value = scrollContainer;
  98. } else if (isString(scrollContainer) && scrollContainer !== "") {
  99. _scrollContainer.value = (_a = document.querySelector(scrollContainer)) != null ? _a : void 0;
  100. } else if (container.value) {
  101. _scrollContainer.value = getScrollContainer(container.value);
  102. }
  103. if (_scrollContainer.value) {
  104. stopScrollListener = useEventListener(_scrollContainer, "scroll", lazyLoadHandler);
  105. setTimeout(() => handleLazyLoad(), 100);
  106. }
  107. }
  108. function removeLazyLoadListener() {
  109. if (!isClient || !_scrollContainer.value || !lazyLoadHandler)
  110. return;
  111. stopScrollListener == null ? void 0 : stopScrollListener();
  112. _scrollContainer.value = void 0;
  113. }
  114. function wheelHandler(e) {
  115. if (!e.ctrlKey)
  116. return;
  117. if (e.deltaY < 0) {
  118. e.preventDefault();
  119. return false;
  120. } else if (e.deltaY > 0) {
  121. e.preventDefault();
  122. return false;
  123. }
  124. }
  125. function clickHandler() {
  126. if (!preview.value)
  127. return;
  128. stopWheelListener = useEventListener("wheel", wheelHandler, {
  129. passive: false
  130. });
  131. prevOverflow = document.body.style.overflow;
  132. document.body.style.overflow = "hidden";
  133. showViewer.value = true;
  134. emit("show");
  135. }
  136. function closeViewer() {
  137. stopWheelListener == null ? void 0 : stopWheelListener();
  138. document.body.style.overflow = prevOverflow;
  139. showViewer.value = false;
  140. emit("close");
  141. }
  142. function switchViewer(val) {
  143. emit("switch", val);
  144. }
  145. watch(() => props.src, () => {
  146. if (isManual.value) {
  147. isLoading.value = true;
  148. hasLoadError.value = false;
  149. removeLazyLoadListener();
  150. addLazyLoadListener();
  151. } else {
  152. loadImage();
  153. }
  154. });
  155. onMounted(() => {
  156. if (isManual.value) {
  157. addLazyLoadListener();
  158. } else {
  159. loadImage();
  160. }
  161. });
  162. return (_ctx, _cache) => {
  163. return openBlock(), createElementBlock("div", {
  164. ref_key: "container",
  165. ref: container,
  166. class: normalizeClass([unref(ns).b(), _ctx.$attrs.class]),
  167. style: normalizeStyle(unref(containerStyle))
  168. }, [
  169. imageSrc.value !== void 0 && !hasLoadError.value ? (openBlock(), createElementBlock("img", mergeProps({ key: 0 }, unref(attrs), {
  170. src: imageSrc.value,
  171. loading: _ctx.loading,
  172. style: unref(imageStyle),
  173. class: [
  174. unref(ns).e("inner"),
  175. unref(preview) && unref(ns).e("preview"),
  176. isLoading.value && unref(ns).is("loading")
  177. ],
  178. onClick: clickHandler,
  179. onLoad: handleLoad,
  180. onError: handleError
  181. }), null, 16, _hoisted_1)) : createCommentVNode("v-if", true),
  182. isLoading.value || hasLoadError.value ? (openBlock(), createElementBlock("div", {
  183. key: 1,
  184. class: normalizeClass(unref(ns).e("wrapper"))
  185. }, [
  186. isLoading.value ? renderSlot(_ctx.$slots, "placeholder", { key: 0 }, () => [
  187. createElementVNode("div", {
  188. class: normalizeClass(unref(ns).e("placeholder"))
  189. }, null, 2)
  190. ]) : hasLoadError.value ? renderSlot(_ctx.$slots, "error", { key: 1 }, () => [
  191. createElementVNode("div", {
  192. class: normalizeClass(unref(ns).e("error"))
  193. }, toDisplayString(unref(t)("el.image.error")), 3)
  194. ]) : createCommentVNode("v-if", true)
  195. ], 2)) : createCommentVNode("v-if", true),
  196. unref(preview) ? (openBlock(), createElementBlock(Fragment, { key: 2 }, [
  197. showViewer.value ? (openBlock(), createBlock(unref(ElImageViewer), {
  198. key: 0,
  199. "z-index": _ctx.zIndex,
  200. "initial-index": unref(imageIndex),
  201. infinite: _ctx.infinite,
  202. "zoom-rate": _ctx.zoomRate,
  203. "url-list": _ctx.previewSrcList,
  204. "hide-on-click-modal": _ctx.hideOnClickModal,
  205. teleported: _ctx.previewTeleported,
  206. "close-on-press-escape": _ctx.closeOnPressEscape,
  207. onClose: closeViewer,
  208. onSwitch: switchViewer
  209. }, {
  210. default: withCtx(() => [
  211. _ctx.$slots.viewer ? (openBlock(), createElementBlock("div", _hoisted_2, [
  212. renderSlot(_ctx.$slots, "viewer")
  213. ])) : createCommentVNode("v-if", true)
  214. ]),
  215. _: 3
  216. }, 8, ["z-index", "initial-index", "infinite", "zoom-rate", "url-list", "hide-on-click-modal", "teleported", "close-on-press-escape"])) : createCommentVNode("v-if", true)
  217. ], 64)) : createCommentVNode("v-if", true)
  218. ], 6);
  219. };
  220. }
  221. });
  222. var Image = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/runner/work/element-plus/element-plus/packages/components/image/src/image.vue"]]);
  223. export { Image as default };
  224. //# sourceMappingURL=image2.mjs.map