Image.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_vue = require("vue");
  24. var import_vue2 = 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. };
  46. var stdin_default = (0, import_vue2.defineComponent)({
  47. name,
  48. props: imageProps,
  49. emits: ["load", "error"],
  50. setup(props, {
  51. emit,
  52. slots
  53. }) {
  54. const error = (0, import_vue2.ref)(false);
  55. const loading = (0, import_vue2.ref)(true);
  56. const imageRef = (0, import_vue2.ref)();
  57. const {
  58. $Lazyload
  59. } = (0, import_vue2.getCurrentInstance)().proxy;
  60. const style = (0, import_vue2.computed)(() => {
  61. const style2 = {
  62. width: (0, import_utils.addUnit)(props.width),
  63. height: (0, import_utils.addUnit)(props.height)
  64. };
  65. if ((0, import_utils.isDef)(props.radius)) {
  66. style2.overflow = "hidden";
  67. style2.borderRadius = (0, import_utils.addUnit)(props.radius);
  68. }
  69. return style2;
  70. });
  71. (0, import_vue2.watch)(() => props.src, () => {
  72. error.value = false;
  73. loading.value = true;
  74. });
  75. const onLoad = (event) => {
  76. loading.value = false;
  77. emit("load", event);
  78. };
  79. const onError = (event) => {
  80. error.value = true;
  81. loading.value = false;
  82. emit("error", event);
  83. };
  84. const renderIcon = (name2, className, slot) => {
  85. if (slot) {
  86. return slot();
  87. }
  88. return (0, import_vue.createVNode)(import_icon.Icon, {
  89. "name": name2,
  90. "size": props.iconSize,
  91. "class": className,
  92. "classPrefix": props.iconPrefix
  93. }, null);
  94. };
  95. const renderPlaceholder = () => {
  96. if (loading.value && props.showLoading) {
  97. return (0, import_vue.createVNode)("div", {
  98. "class": bem("loading")
  99. }, [renderIcon(props.loadingIcon, bem("loading-icon"), slots.loading)]);
  100. }
  101. if (error.value && props.showError) {
  102. return (0, import_vue.createVNode)("div", {
  103. "class": bem("error")
  104. }, [renderIcon(props.errorIcon, bem("error-icon"), slots.error)]);
  105. }
  106. };
  107. const renderImage = () => {
  108. if (error.value || !props.src) {
  109. return;
  110. }
  111. const attrs = {
  112. alt: props.alt,
  113. class: bem("img"),
  114. style: {
  115. objectFit: props.fit,
  116. objectPosition: props.position
  117. }
  118. };
  119. if (props.lazyLoad) {
  120. return (0, import_vue.withDirectives)((0, import_vue.createVNode)("img", (0, import_vue.mergeProps)({
  121. "ref": imageRef
  122. }, attrs), null), [[(0, import_vue.resolveDirective)("lazy"), props.src]]);
  123. }
  124. return (0, import_vue.createVNode)("img", (0, import_vue.mergeProps)({
  125. "src": props.src,
  126. "onLoad": onLoad,
  127. "onError": onError
  128. }, attrs), null);
  129. };
  130. const onLazyLoaded = ({
  131. el
  132. }) => {
  133. const check = () => {
  134. if (el === imageRef.value && loading.value) {
  135. onLoad();
  136. }
  137. };
  138. if (imageRef.value) {
  139. check();
  140. } else {
  141. (0, import_vue2.nextTick)(check);
  142. }
  143. };
  144. const onLazyLoadError = ({
  145. el
  146. }) => {
  147. if (el === imageRef.value && !error.value) {
  148. onError();
  149. }
  150. };
  151. if ($Lazyload && import_utils.inBrowser) {
  152. $Lazyload.$on("loaded", onLazyLoaded);
  153. $Lazyload.$on("error", onLazyLoadError);
  154. (0, import_vue2.onBeforeUnmount)(() => {
  155. $Lazyload.$off("loaded", onLazyLoaded);
  156. $Lazyload.$off("error", onLazyLoadError);
  157. });
  158. }
  159. return () => {
  160. var _a;
  161. return (0, import_vue.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. });