element-plus
Version:
A Component Library for Vue 3
233 lines (230 loc) • 8.61 kB
JavaScript
import { defineComponent, useAttrs, ref, computed, nextTick, watch, onMounted, openBlock, createElementBlock, normalizeClass, unref, normalizeStyle, renderSlot, createElementVNode, toDisplayString, Fragment, mergeProps, createCommentVNode, 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", "crossorigin"];
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 imageKls = computed(() => [
ns.e("inner"),
preview.value && ns.e("preview"),
isLoading.value && ns.is("loading")
]);
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, true);
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))
}, [
hasLoadError.value ? renderSlot(_ctx.$slots, "error", { key: 0 }, () => [
createElementVNode("div", {
class: normalizeClass(unref(ns).e("error"))
}, toDisplayString(unref(t)("el.image.error")), 3)
]) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
imageSrc.value !== void 0 ? (openBlock(), createElementBlock("img", mergeProps({ key: 0 }, unref(attrs), {
src: imageSrc.value,
loading: _ctx.loading,
style: unref(imageStyle),
class: unref(imageKls),
crossorigin: _ctx.crossorigin,
onClick: clickHandler,
onLoad: handleLoad,
onError: handleError
}), null, 16, _hoisted_1)) : createCommentVNode("v-if", true),
isLoading.value ? (openBlock(), createElementBlock("div", {
key: 1,
class: normalizeClass(unref(ns).e("wrapper"))
}, [
renderSlot(_ctx.$slots, "placeholder", {}, () => [
createElementVNode("div", {
class: normalizeClass(unref(ns).e("placeholder"))
}, null, 2)
])
], 2)) : createCommentVNode("v-if", true)
], 64)),
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,
"min-scale": _ctx.minScale,
"max-scale": _ctx.maxScale,
"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", "min-scale", "max-scale", "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", "image.vue"]]);
export { Image as default };
//# sourceMappingURL=image2.mjs.map