123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- import { defineComponent, useAttrs, ref, computed, nextTick, watch, onMounted, openBlock, createElementBlock, normalizeClass, unref, normalizeStyle, mergeProps, createCommentVNode, renderSlot, createElementVNode, toDisplayString, Fragment, createBlock, withCtx } from 'vue';
- import { isClient, useThrottleFn, useEventListener } from '@vueuse/core';
- import '../../../hooks/index.mjs';
- import { ElImageViewer } from '../../image-viewer/index.mjs';
- import '../../../utils/index.mjs';
- import { imageProps, imageEmits } from './image.mjs';
- import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
- import { useLocale } from '../../../hooks/use-locale/index.mjs';
- import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
- import { useAttrs as useAttrs$1 } from '../../../hooks/use-attrs/index.mjs';
- import { isInContainer } from '../../../utils/dom/position.mjs';
- import { isElement } from '../../../utils/types.mjs';
- import { isString } from '@vue/shared';
- import { getScrollContainer } from '../../../utils/dom/scroll.mjs';
- const _hoisted_1 = ["src", "loading"];
- const _hoisted_2 = { key: 0 };
- const __default__ = defineComponent({
- name: "ElImage",
- inheritAttrs: false
- });
- const _sfc_main = /* @__PURE__ */ defineComponent({
- ...__default__,
- props: imageProps,
- emits: imageEmits,
- setup(__props, { emit }) {
- const props = __props;
- let prevOverflow = "";
- const { t } = useLocale();
- const ns = useNamespace("image");
- const rawAttrs = useAttrs();
- const attrs = useAttrs$1();
- const imageSrc = ref();
- const hasLoadError = ref(false);
- const isLoading = ref(true);
- const showViewer = ref(false);
- const container = ref();
- const _scrollContainer = ref();
- const supportLoading = isClient && "loading" in HTMLImageElement.prototype;
- let stopScrollListener;
- let stopWheelListener;
- const containerStyle = computed(() => rawAttrs.style);
- const imageStyle = computed(() => {
- const { fit } = props;
- if (isClient && fit) {
- return { objectFit: fit };
- }
- return {};
- });
- const preview = computed(() => {
- const { previewSrcList } = props;
- return Array.isArray(previewSrcList) && previewSrcList.length > 0;
- });
- const imageIndex = computed(() => {
- const { previewSrcList, initialIndex } = props;
- let previewIndex = initialIndex;
- if (initialIndex > previewSrcList.length - 1) {
- previewIndex = 0;
- }
- return previewIndex;
- });
- const isManual = computed(() => {
- if (props.loading === "eager")
- return false;
- return !supportLoading && props.loading === "lazy" || props.lazy;
- });
- const loadImage = () => {
- if (!isClient)
- return;
- isLoading.value = true;
- hasLoadError.value = false;
- imageSrc.value = props.src;
- };
- function handleLoad(event) {
- isLoading.value = false;
- hasLoadError.value = false;
- emit("load", event);
- }
- function handleError(event) {
- isLoading.value = false;
- hasLoadError.value = true;
- emit("error", event);
- }
- function handleLazyLoad() {
- if (isInContainer(container.value, _scrollContainer.value)) {
- loadImage();
- removeLazyLoadListener();
- }
- }
- const lazyLoadHandler = useThrottleFn(handleLazyLoad, 200);
- async function addLazyLoadListener() {
- var _a;
- if (!isClient)
- return;
- await nextTick();
- const { scrollContainer } = props;
- if (isElement(scrollContainer)) {
- _scrollContainer.value = scrollContainer;
- } else if (isString(scrollContainer) && scrollContainer !== "") {
- _scrollContainer.value = (_a = document.querySelector(scrollContainer)) != null ? _a : void 0;
- } else if (container.value) {
- _scrollContainer.value = getScrollContainer(container.value);
- }
- if (_scrollContainer.value) {
- stopScrollListener = useEventListener(_scrollContainer, "scroll", lazyLoadHandler);
- setTimeout(() => handleLazyLoad(), 100);
- }
- }
- function removeLazyLoadListener() {
- if (!isClient || !_scrollContainer.value || !lazyLoadHandler)
- return;
- stopScrollListener == null ? void 0 : stopScrollListener();
- _scrollContainer.value = void 0;
- }
- function wheelHandler(e) {
- if (!e.ctrlKey)
- return;
- if (e.deltaY < 0) {
- e.preventDefault();
- return false;
- } else if (e.deltaY > 0) {
- e.preventDefault();
- return false;
- }
- }
- function clickHandler() {
- if (!preview.value)
- return;
- stopWheelListener = useEventListener("wheel", wheelHandler, {
- passive: false
- });
- prevOverflow = document.body.style.overflow;
- document.body.style.overflow = "hidden";
- showViewer.value = true;
- emit("show");
- }
- function closeViewer() {
- stopWheelListener == null ? void 0 : stopWheelListener();
- document.body.style.overflow = prevOverflow;
- showViewer.value = false;
- emit("close");
- }
- function switchViewer(val) {
- emit("switch", val);
- }
- watch(() => props.src, () => {
- if (isManual.value) {
- isLoading.value = true;
- hasLoadError.value = false;
- removeLazyLoadListener();
- addLazyLoadListener();
- } else {
- loadImage();
- }
- });
- onMounted(() => {
- if (isManual.value) {
- addLazyLoadListener();
- } else {
- loadImage();
- }
- });
- return (_ctx, _cache) => {
- return openBlock(), createElementBlock("div", {
- ref_key: "container",
- ref: container,
- class: normalizeClass([unref(ns).b(), _ctx.$attrs.class]),
- style: normalizeStyle(unref(containerStyle))
- }, [
- imageSrc.value !== void 0 && !hasLoadError.value ? (openBlock(), createElementBlock("img", mergeProps({ key: 0 }, unref(attrs), {
- src: imageSrc.value,
- loading: _ctx.loading,
- style: unref(imageStyle),
- class: [
- unref(ns).e("inner"),
- unref(preview) && unref(ns).e("preview"),
- isLoading.value && unref(ns).is("loading")
- ],
- onClick: clickHandler,
- onLoad: handleLoad,
- onError: handleError
- }), null, 16, _hoisted_1)) : createCommentVNode("v-if", true),
- isLoading.value || hasLoadError.value ? (openBlock(), createElementBlock("div", {
- key: 1,
- class: normalizeClass(unref(ns).e("wrapper"))
- }, [
- isLoading.value ? renderSlot(_ctx.$slots, "placeholder", { key: 0 }, () => [
- createElementVNode("div", {
- class: normalizeClass(unref(ns).e("placeholder"))
- }, null, 2)
- ]) : hasLoadError.value ? renderSlot(_ctx.$slots, "error", { key: 1 }, () => [
- createElementVNode("div", {
- class: normalizeClass(unref(ns).e("error"))
- }, toDisplayString(unref(t)("el.image.error")), 3)
- ]) : createCommentVNode("v-if", true)
- ], 2)) : createCommentVNode("v-if", true),
- unref(preview) ? (openBlock(), createElementBlock(Fragment, { key: 2 }, [
- showViewer.value ? (openBlock(), createBlock(unref(ElImageViewer), {
- key: 0,
- "z-index": _ctx.zIndex,
- "initial-index": unref(imageIndex),
- infinite: _ctx.infinite,
- "zoom-rate": _ctx.zoomRate,
- "url-list": _ctx.previewSrcList,
- "hide-on-click-modal": _ctx.hideOnClickModal,
- teleported: _ctx.previewTeleported,
- "close-on-press-escape": _ctx.closeOnPressEscape,
- onClose: closeViewer,
- onSwitch: switchViewer
- }, {
- default: withCtx(() => [
- _ctx.$slots.viewer ? (openBlock(), createElementBlock("div", _hoisted_2, [
- renderSlot(_ctx.$slots, "viewer")
- ])) : createCommentVNode("v-if", true)
- ]),
- _: 3
- }, 8, ["z-index", "initial-index", "infinite", "zoom-rate", "url-list", "hide-on-click-modal", "teleported", "close-on-press-escape"])) : createCommentVNode("v-if", true)
- ], 64)) : createCommentVNode("v-if", true)
- ], 6);
- };
- }
- });
- var Image = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/runner/work/element-plus/element-plus/packages/components/image/src/image.vue"]]);
- export { Image as default };
- //# sourceMappingURL=image2.mjs.map
|