Image.mjs 4.7 KB

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