Image.mjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { withDirectives as _withDirectives, mergeProps as _mergeProps, resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
  2. import { ref, watch, computed, nextTick, onBeforeUnmount, defineComponent, getCurrentInstance } from "vue";
  3. import { isDef, addUnit, inBrowser, truthProp, numericProp, makeStringProp, createNamespace } from "../utils/index.mjs";
  4. import { Icon } from "../icon/index.mjs";
  5. const [name, bem] = createNamespace("image");
  6. const imageProps = {
  7. src: String,
  8. alt: String,
  9. fit: String,
  10. position: String,
  11. round: Boolean,
  12. block: Boolean,
  13. width: numericProp,
  14. height: numericProp,
  15. radius: numericProp,
  16. lazyLoad: Boolean,
  17. iconSize: numericProp,
  18. showError: truthProp,
  19. errorIcon: makeStringProp("photo-fail"),
  20. iconPrefix: String,
  21. showLoading: truthProp,
  22. loadingIcon: makeStringProp("photo")
  23. };
  24. var stdin_default = defineComponent({
  25. name,
  26. props: imageProps,
  27. emits: ["load", "error"],
  28. setup(props, {
  29. emit,
  30. slots
  31. }) {
  32. const error = ref(false);
  33. const loading = ref(true);
  34. const imageRef = ref();
  35. const {
  36. $Lazyload
  37. } = getCurrentInstance().proxy;
  38. const style = computed(() => {
  39. const style2 = {
  40. width: addUnit(props.width),
  41. height: addUnit(props.height)
  42. };
  43. if (isDef(props.radius)) {
  44. style2.overflow = "hidden";
  45. style2.borderRadius = addUnit(props.radius);
  46. }
  47. return style2;
  48. });
  49. watch(() => props.src, () => {
  50. error.value = false;
  51. loading.value = true;
  52. });
  53. const onLoad = (event) => {
  54. loading.value = false;
  55. emit("load", event);
  56. };
  57. const onError = (event) => {
  58. error.value = true;
  59. loading.value = false;
  60. emit("error", event);
  61. };
  62. const renderIcon = (name2, className, slot) => {
  63. if (slot) {
  64. return slot();
  65. }
  66. return _createVNode(Icon, {
  67. "name": name2,
  68. "size": props.iconSize,
  69. "class": className,
  70. "classPrefix": props.iconPrefix
  71. }, null);
  72. };
  73. const renderPlaceholder = () => {
  74. if (loading.value && props.showLoading) {
  75. return _createVNode("div", {
  76. "class": bem("loading")
  77. }, [renderIcon(props.loadingIcon, bem("loading-icon"), slots.loading)]);
  78. }
  79. if (error.value && props.showError) {
  80. return _createVNode("div", {
  81. "class": bem("error")
  82. }, [renderIcon(props.errorIcon, bem("error-icon"), slots.error)]);
  83. }
  84. };
  85. const renderImage = () => {
  86. if (error.value || !props.src) {
  87. return;
  88. }
  89. const attrs = {
  90. alt: props.alt,
  91. class: bem("img"),
  92. style: {
  93. objectFit: props.fit,
  94. objectPosition: props.position
  95. }
  96. };
  97. if (props.lazyLoad) {
  98. return _withDirectives(_createVNode("img", _mergeProps({
  99. "ref": imageRef
  100. }, attrs), null), [[_resolveDirective("lazy"), props.src]]);
  101. }
  102. return _createVNode("img", _mergeProps({
  103. "src": props.src,
  104. "onLoad": onLoad,
  105. "onError": onError
  106. }, attrs), null);
  107. };
  108. const onLazyLoaded = ({
  109. el
  110. }) => {
  111. const check = () => {
  112. if (el === imageRef.value && loading.value) {
  113. onLoad();
  114. }
  115. };
  116. if (imageRef.value) {
  117. check();
  118. } else {
  119. nextTick(check);
  120. }
  121. };
  122. const onLazyLoadError = ({
  123. el
  124. }) => {
  125. if (el === imageRef.value && !error.value) {
  126. onError();
  127. }
  128. };
  129. if ($Lazyload && inBrowser) {
  130. $Lazyload.$on("loaded", onLazyLoaded);
  131. $Lazyload.$on("error", onLazyLoadError);
  132. onBeforeUnmount(() => {
  133. $Lazyload.$off("loaded", onLazyLoaded);
  134. $Lazyload.$off("error", onLazyLoadError);
  135. });
  136. }
  137. return () => {
  138. var _a;
  139. return _createVNode("div", {
  140. "class": bem({
  141. round: props.round,
  142. block: props.block
  143. }),
  144. "style": style.value
  145. }, [renderImage(), renderPlaceholder(), (_a = slots.default) == null ? void 0 : _a.call(slots)]);
  146. };
  147. }
  148. });
  149. export {
  150. stdin_default as default
  151. };