index-f1ec0231.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { c as createNamespace, d as addUnit, k as isDef, N as inBrowser, n as numericProp, t as truthProp, z as makeStringProp, I as Icon, w as withInstall } from "./index-487cde8c.js";
  2. import { d as defineComponent, r as ref, g as getCurrentInstance, a as computed, w as watch, M as onBeforeUnmount, o as onMounted, z as nextTick, c as createVNode, h as withDirectives, N as resolveDirective, m as mergeProps } from "./index-5e4623ce.js";
  3. const [name, bem] = createNamespace("image");
  4. const imageProps = {
  5. src: String,
  6. alt: String,
  7. fit: String,
  8. position: String,
  9. round: Boolean,
  10. block: Boolean,
  11. width: numericProp,
  12. height: numericProp,
  13. radius: numericProp,
  14. lazyLoad: Boolean,
  15. iconSize: numericProp,
  16. showError: truthProp,
  17. errorIcon: makeStringProp("photo-fail"),
  18. iconPrefix: String,
  19. showLoading: truthProp,
  20. loadingIcon: makeStringProp("photo"),
  21. crossorigin: String,
  22. referrerpolicy: String
  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. if (loading.value) {
  55. loading.value = false;
  56. emit("load", event);
  57. }
  58. };
  59. const triggerLoad = () => {
  60. const loadEvent = new Event("load");
  61. Object.defineProperty(loadEvent, "target", {
  62. value: imageRef.value,
  63. enumerable: true
  64. });
  65. onLoad(loadEvent);
  66. };
  67. const onError = (event) => {
  68. error.value = true;
  69. loading.value = false;
  70. emit("error", event);
  71. };
  72. const renderIcon = (name2, className, slot) => {
  73. if (slot) {
  74. return slot();
  75. }
  76. return createVNode(Icon, {
  77. "name": name2,
  78. "size": props.iconSize,
  79. "class": className,
  80. "classPrefix": props.iconPrefix
  81. }, null);
  82. };
  83. const renderPlaceholder = () => {
  84. if (loading.value && props.showLoading) {
  85. return createVNode("div", {
  86. "class": bem("loading")
  87. }, [renderIcon(props.loadingIcon, bem("loading-icon"), slots.loading)]);
  88. }
  89. if (error.value && props.showError) {
  90. return createVNode("div", {
  91. "class": bem("error")
  92. }, [renderIcon(props.errorIcon, bem("error-icon"), slots.error)]);
  93. }
  94. };
  95. const renderImage = () => {
  96. if (error.value || !props.src) {
  97. return;
  98. }
  99. const attrs = {
  100. alt: props.alt,
  101. class: bem("img"),
  102. style: {
  103. objectFit: props.fit,
  104. objectPosition: props.position
  105. },
  106. crossorigin: props.crossorigin,
  107. referrerpolicy: props.referrerpolicy
  108. };
  109. if (props.lazyLoad) {
  110. return withDirectives(createVNode("img", mergeProps({
  111. "ref": imageRef
  112. }, attrs), null), [[resolveDirective("lazy"), props.src]]);
  113. }
  114. return createVNode("img", mergeProps({
  115. "ref": imageRef,
  116. "src": props.src,
  117. "onLoad": onLoad,
  118. "onError": onError
  119. }, attrs), null);
  120. };
  121. const onLazyLoaded = ({
  122. el
  123. }) => {
  124. const check = () => {
  125. if (el === imageRef.value && loading.value) {
  126. triggerLoad();
  127. }
  128. };
  129. if (imageRef.value) {
  130. check();
  131. } else {
  132. nextTick(check);
  133. }
  134. };
  135. const onLazyLoadError = ({
  136. el
  137. }) => {
  138. if (el === imageRef.value && !error.value) {
  139. onError();
  140. }
  141. };
  142. if ($Lazyload && inBrowser) {
  143. $Lazyload.$on("loaded", onLazyLoaded);
  144. $Lazyload.$on("error", onLazyLoadError);
  145. onBeforeUnmount(() => {
  146. $Lazyload.$off("loaded", onLazyLoaded);
  147. $Lazyload.$off("error", onLazyLoadError);
  148. });
  149. }
  150. onMounted(() => {
  151. nextTick(() => {
  152. var _a;
  153. if (((_a = imageRef.value) == null ? void 0 : _a.complete) && !props.lazyLoad) {
  154. triggerLoad();
  155. }
  156. });
  157. });
  158. return () => {
  159. var _a;
  160. return createVNode("div", {
  161. "class": bem({
  162. round: props.round,
  163. block: props.block
  164. }),
  165. "style": style.value
  166. }, [renderImage(), renderPlaceholder(), (_a = slots.default) == null ? void 0 : _a.call(slots)]);
  167. };
  168. }
  169. });
  170. const Image = withInstall(stdin_default);
  171. const index = "";
  172. export {
  173. Image as I
  174. };