Image.js 5.8 KB

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