| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 | import { withDirectives as _withDirectives, mergeProps as _mergeProps, resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";import { ref, watch, computed, nextTick, onBeforeUnmount, defineComponent, getCurrentInstance } from "vue";import { isDef, addUnit, inBrowser, truthProp, numericProp, makeStringProp, createNamespace } from "../utils/index.mjs";import { Icon } from "../icon/index.mjs";const [name, bem] = createNamespace("image");const imageProps = {  src: String,  alt: String,  fit: String,  position: String,  round: Boolean,  block: Boolean,  width: numericProp,  height: numericProp,  radius: numericProp,  lazyLoad: Boolean,  iconSize: numericProp,  showError: truthProp,  errorIcon: makeStringProp("photo-fail"),  iconPrefix: String,  showLoading: truthProp,  loadingIcon: makeStringProp("photo")};var stdin_default = defineComponent({  name,  props: imageProps,  emits: ["load", "error"],  setup(props, {    emit,    slots  }) {    const error = ref(false);    const loading = ref(true);    const imageRef = ref();    const {      $Lazyload    } = getCurrentInstance().proxy;    const style = computed(() => {      const style2 = {        width: addUnit(props.width),        height: addUnit(props.height)      };      if (isDef(props.radius)) {        style2.overflow = "hidden";        style2.borderRadius = addUnit(props.radius);      }      return style2;    });    watch(() => props.src, () => {      error.value = false;      loading.value = true;    });    const onLoad = (event) => {      loading.value = false;      emit("load", event);    };    const onError = (event) => {      error.value = true;      loading.value = false;      emit("error", event);    };    const renderIcon = (name2, className, slot) => {      if (slot) {        return slot();      }      return _createVNode(Icon, {        "name": name2,        "size": props.iconSize,        "class": className,        "classPrefix": props.iconPrefix      }, null);    };    const renderPlaceholder = () => {      if (loading.value && props.showLoading) {        return _createVNode("div", {          "class": bem("loading")        }, [renderIcon(props.loadingIcon, bem("loading-icon"), slots.loading)]);      }      if (error.value && props.showError) {        return _createVNode("div", {          "class": bem("error")        }, [renderIcon(props.errorIcon, bem("error-icon"), slots.error)]);      }    };    const renderImage = () => {      if (error.value || !props.src) {        return;      }      const attrs = {        alt: props.alt,        class: bem("img"),        style: {          objectFit: props.fit,          objectPosition: props.position        }      };      if (props.lazyLoad) {        return _withDirectives(_createVNode("img", _mergeProps({          "ref": imageRef        }, attrs), null), [[_resolveDirective("lazy"), props.src]]);      }      return _createVNode("img", _mergeProps({        "src": props.src,        "onLoad": onLoad,        "onError": onError      }, attrs), null);    };    const onLazyLoaded = ({      el    }) => {      const check = () => {        if (el === imageRef.value && loading.value) {          onLoad();        }      };      if (imageRef.value) {        check();      } else {        nextTick(check);      }    };    const onLazyLoadError = ({      el    }) => {      if (el === imageRef.value && !error.value) {        onError();      }    };    if ($Lazyload && inBrowser) {      $Lazyload.$on("loaded", onLazyLoaded);      $Lazyload.$on("error", onLazyLoadError);      onBeforeUnmount(() => {        $Lazyload.$off("loaded", onLazyLoaded);        $Lazyload.$off("error", onLazyLoadError);      });    }    return () => {      var _a;      return _createVNode("div", {        "class": bem({          round: props.round,          block: props.block        }),        "style": style.value      }, [renderImage(), renderPlaceholder(), (_a = slots.default) == null ? void 0 : _a.call(slots)]);    };  }});export {  stdin_default as default};
 |