Image.js 5.7 KB

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