UNPKG

tdesign-vue

Version:
242 lines (238 loc) 8.78 kB
/** * tdesign v1.14.1 * (c) 2025 tdesign * @license MIT */ import { h as helper } from '../_chunks/dep-6a4dc7bb.js'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import { defineComponent, toRefs, ref, watch, computed, onMounted, onUnmounted } from '@vue/composition-api'; import { omit, isFunction } from 'lodash-es'; import { ImageIcon, ImageErrorIcon } from 'tdesign-icons-vue'; import observe from '../_common/js/utils/observe.js'; import { useConfig } from '../config-provider/useConfig.js'; import props from './props.js'; import { renderTNodeJSX } from '../utils/render-tnode.js'; import { Space } from '../space/index.js'; import { useImagePreviewUrl } from '../hooks/useImagePreviewUrl.js'; import '../config-provider/context.js'; import '../_common/js/global-config/default-config.js'; import '../_common/js/global-config/locale/zh_CN.js'; import '../_chunks/dep-ba613a02.js'; import '@babel/runtime/helpers/typeof'; import '../_chunks/dep-fdb1b253.js'; import 'dayjs'; import '../_common/js/global-config/t.js'; import '@babel/runtime/helpers/defineProperty'; import '@babel/runtime/helpers/readOnlyError'; import 'vue'; import '../space/space.js'; import '../space/props.js'; import '../hooks/useConfig.js'; import '../_common/js/utils/helper.js'; import '@babel/runtime/helpers/toConsumableArray'; import '@babel/runtime/helpers/objectWithoutProperties'; import '../utils/withInstall.js'; import '../_common/js/upload/utils.js'; import '../_common/js/log/log.js'; var _Image = defineComponent({ name: "TImage", components: { Space: Space }, props: props, setup: function setup(props2, _ref) { var emit = _ref.emit; var onLoad = props2.onLoad, onError = props2.onError; var _toRefs = toRefs(props2), src = _toRefs.src, lazy = _toRefs.lazy, fallback = _toRefs.fallback, overlayTrigger = _toRefs.overlayTrigger; var rest = omit(props2, ["className", "src", "style", "alt", "fit", "position", "shape", "placeholder", "loading", "error", "overlayTrigger", "overlayContent", "lazy", "gallery", "onLoad", "onError"]); var _useConfig = useConfig("image"), classPrefix = _useConfig.classPrefix, globalConfig = _useConfig.globalConfig; var imageRef = ref(null); var imageStrSrc = ref(src.value); watch([src, globalConfig], function (_ref2) { var _ref3 = _slicedToArray(_ref2, 2), src2 = _ref3[0], globalConfig2 = _ref3[1]; var _ref4 = globalConfig2 || {}, replaceImageSrc = _ref4.replaceImageSrc; var tmpUrl = isFunction(replaceImageSrc) ? replaceImageSrc(props2) : src2; if (tmpUrl === imageStrSrc.value && imageStrSrc.value) return; imageStrSrc.value = tmpUrl; }, { immediate: true }); var _useImagePreviewUrl = useImagePreviewUrl(imageStrSrc), previewUrl = _useImagePreviewUrl.previewUrl; watch([previewUrl], function () { hasError.value = false; isLoaded.value = false; }); var shouldLoad = ref(!lazy.value); var handleLoadImage = function handleLoadImage() { shouldLoad.value = true; }; var isLoaded = ref(false); var handleLoad = function handleLoad(e) { isLoaded.value = true; emit("load", { e: e }); onLoad === null || onLoad === void 0 || onLoad({ e: e }); }; var hasError = ref(false); var handleError = function handleError(e) { hasError.value = true; if (fallback.value) { imageStrSrc.value = fallback.value; hasError.value = false; } emit("error", { e: e }); onError === null || onError === void 0 || onError({ e: e }); }; var hasMouseEvent = overlayTrigger.value === "hover"; var shouldShowOverlay = ref(!hasMouseEvent); var handleToggleOverlay = function handleToggleOverlay() { if (hasMouseEvent) { shouldShowOverlay.value = !shouldShowOverlay.value; } }; var imageClasses = computed(function () { return ["".concat(classPrefix.value, "-image"), "".concat(classPrefix.value, "-image--fit-").concat(props2.fit), "".concat(classPrefix.value, "-image--position-").concat(props2.position)]; }); var io = ref(null); onMounted(function () { if (!lazy.value || !imageRef.value) return; io.value = observe(imageRef.value, null, handleLoadImage, 0); }); onUnmounted(function () { imageRef.value && io.value && io.value.unobserve(imageRef.value); }); return { imageRef: imageRef, imageClasses: imageClasses, handleLoadImage: handleLoadImage, classPrefix: classPrefix, globalConfig: globalConfig, hasMouseEvent: hasMouseEvent, handleToggleOverlay: handleToggleOverlay, shouldShowOverlay: shouldShowOverlay, imageStrSrc: imageStrSrc, previewUrl: previewUrl, hasError: hasError, shouldLoad: shouldLoad, handleError: handleError, handleLoad: handleLoad, isLoaded: isLoaded, io: io, rest: rest }; }, methods: { renderPlaceholder: function renderPlaceholder() { var h = this.$createElement; var placeholder = renderTNodeJSX(this, "placeholder"); if (!placeholder) return; return h("div", { "class": "".concat(this.classPrefix, "-image__placeholder") }, [placeholder]); }, renderGalleryShadow: function renderGalleryShadow() { var h = this.$createElement; if (!this.gallery) return null; return h("div", { "class": "".concat(this.classPrefix, "-image__gallery-shadow") }); }, renderOverlay: function renderOverlay() { var h = this.$createElement; var overlay = renderTNodeJSX(this, "overlayContent"); if (!overlay) return null; return h("div", { "class": ["".concat(this.classPrefix, "-image__overlay-content"), !this.shouldShowOverlay && "".concat(this.classPrefix, "-image__overlay-content--hidden")] }, [overlay]); }, renderImageSrcset: function renderImageSrcset() { var h = this.$createElement; return h("picture", [Object.entries(this.srcset).map(function (_ref5) { var _ref6 = _slicedToArray(_ref5, 2), type = _ref6[0], url = _ref6[1]; return h("source", { "attrs": { "type": type, "srcset": url } }); }), this.renderImage()]); }, renderImage: function renderImage() { var h = this.$createElement; var url = typeof this.imageStrSrc === "string" ? this.imageStrSrc : this.previewUrl; return h("img", { "attrs": { "src": url, "alt": this.alt, "referrerpolicy": this.referrerpolicy }, "on": { "error": this.handleError, "load": this.handleLoad }, "class": this.imageClasses }); } }, render: function render() { var h = arguments[0]; return h("div", helper([{ "ref": "imageRef", "class": ["".concat(this.classPrefix, "-image__wrapper"), "".concat(this.classPrefix, "-image__wrapper--shape-").concat(this.shape), this.gallery && "".concat(this.classPrefix, "-image__wrapper--gallery"), this.hasMouseEvent && "".concat(this.classPrefix, "-image__wrapper--need-hover")], "on": { "mouseenter": this.handleToggleOverlay, "mouseleave": this.handleToggleOverlay } }, this.rest, {}, { "on": this.$listeners }]), [this.renderPlaceholder(), this.renderGalleryShadow(), (this.hasError || !this.shouldLoad) && h("div", { "class": "".concat(this.classPrefix, "-image") }), !(this.hasError || !this.shouldLoad) && (this.srcset && Object.keys(this.srcset).length ? this.renderImageSrcset() : this.renderImage()), !(this.hasError || !this.shouldLoad) && !this.isLoaded && h("div", { "class": "".concat(this.classPrefix, "-image__loading") }, [renderTNodeJSX(this, "loading") || h(Space, { "attrs": { "direction": "vertical", "size": 8, "align": "center" } }, [h(ImageIcon, { "attrs": { "size": "24px" } }), typeof this.loading === "string" ? this.loading : this.globalConfig.loadingText])]), this.hasError && h("div", { "class": "".concat(this.classPrefix, "-image__error") }, [renderTNodeJSX(this, "error") || h(Space, { "attrs": { "direction": "vertical", "size": 8, "align": "center" } }, [h(ImageErrorIcon, { "attrs": { "size": "24px" } }), typeof this.error === "string" ? this.error : this.globalConfig.errorText])]), this.renderOverlay()]); } }); export { _Image as default }; //# sourceMappingURL=image.js.map