ImagePreview.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __export = (target, all) => {
  8. for (var name2 in all)
  9. __defProp(target, name2, { get: all[name2], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  20. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  21. mod
  22. ));
  23. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  24. var stdin_exports = {};
  25. __export(stdin_exports, {
  26. default: () => stdin_default,
  27. imagePreviewProps: () => imagePreviewProps
  28. });
  29. module.exports = __toCommonJS(stdin_exports);
  30. var import_vue = require("vue");
  31. var import_vue2 = require("vue");
  32. var import_utils = require("../utils");
  33. var import_use = require("@vant/use");
  34. var import_use_expose = require("../composables/use-expose");
  35. var import_icon = require("../icon");
  36. var import_swipe = require("../swipe");
  37. var import_popup = require("../popup");
  38. var import_ImagePreviewItem = __toESM(require("./ImagePreviewItem"));
  39. const [name, bem] = (0, import_utils.createNamespace)("image-preview");
  40. const popupProps = ["show", "transition", "overlayStyle", "closeOnPopstate"];
  41. const imagePreviewProps = {
  42. show: Boolean,
  43. loop: import_utils.truthProp,
  44. images: (0, import_utils.makeArrayProp)(),
  45. minZoom: (0, import_utils.makeNumericProp)(1 / 3),
  46. maxZoom: (0, import_utils.makeNumericProp)(3),
  47. overlay: import_utils.truthProp,
  48. closeable: Boolean,
  49. showIndex: import_utils.truthProp,
  50. className: import_utils.unknownProp,
  51. closeIcon: (0, import_utils.makeStringProp)("clear"),
  52. transition: String,
  53. beforeClose: Function,
  54. overlayClass: import_utils.unknownProp,
  55. overlayStyle: Object,
  56. swipeDuration: (0, import_utils.makeNumericProp)(300),
  57. startPosition: (0, import_utils.makeNumericProp)(0),
  58. showIndicators: Boolean,
  59. closeOnPopstate: import_utils.truthProp,
  60. closeIconPosition: (0, import_utils.makeStringProp)("top-right")
  61. };
  62. var stdin_default = (0, import_vue2.defineComponent)({
  63. name,
  64. props: imagePreviewProps,
  65. emits: ["scale", "close", "closed", "change", "longPress", "update:show"],
  66. setup(props, {
  67. emit,
  68. slots
  69. }) {
  70. const swipeRef = (0, import_vue2.ref)();
  71. const state = (0, import_vue2.reactive)({
  72. active: 0,
  73. rootWidth: 0,
  74. rootHeight: 0
  75. });
  76. const resize = () => {
  77. if (swipeRef.value) {
  78. const rect = (0, import_use.useRect)(swipeRef.value.$el);
  79. state.rootWidth = rect.width;
  80. state.rootHeight = rect.height;
  81. swipeRef.value.resize();
  82. }
  83. };
  84. const emitScale = (args) => emit("scale", args);
  85. const updateShow = (show) => emit("update:show", show);
  86. const emitClose = () => {
  87. (0, import_utils.callInterceptor)(props.beforeClose, {
  88. args: [state.active],
  89. done: () => updateShow(false)
  90. });
  91. };
  92. const setActive = (active) => {
  93. if (active !== state.active) {
  94. state.active = active;
  95. emit("change", active);
  96. }
  97. };
  98. const renderIndex = () => {
  99. if (props.showIndex) {
  100. return (0, import_vue.createVNode)("div", {
  101. "class": bem("index")
  102. }, [slots.index ? slots.index({
  103. index: state.active
  104. }) : `${state.active + 1} / ${props.images.length}`]);
  105. }
  106. };
  107. const renderCover = () => {
  108. if (slots.cover) {
  109. return (0, import_vue.createVNode)("div", {
  110. "class": bem("cover")
  111. }, [slots.cover()]);
  112. }
  113. };
  114. const renderImages = () => (0, import_vue.createVNode)(import_swipe.Swipe, {
  115. "ref": swipeRef,
  116. "lazyRender": true,
  117. "loop": props.loop,
  118. "class": bem("swipe"),
  119. "duration": props.swipeDuration,
  120. "initialSwipe": props.startPosition,
  121. "showIndicators": props.showIndicators,
  122. "indicatorColor": "white",
  123. "onChange": setActive
  124. }, {
  125. default: () => [props.images.map((image, index) => (0, import_vue.createVNode)(import_ImagePreviewItem.default, {
  126. "src": image,
  127. "show": props.show,
  128. "active": state.active,
  129. "maxZoom": props.maxZoom,
  130. "minZoom": props.minZoom,
  131. "rootWidth": state.rootWidth,
  132. "rootHeight": state.rootHeight,
  133. "onScale": emitScale,
  134. "onClose": emitClose,
  135. "onLongPress": () => emit("longPress", {
  136. index
  137. })
  138. }, {
  139. image: slots.image
  140. }))]
  141. });
  142. const renderClose = () => {
  143. if (props.closeable) {
  144. return (0, import_vue.createVNode)(import_icon.Icon, {
  145. "role": "button",
  146. "name": props.closeIcon,
  147. "class": [bem("close-icon", props.closeIconPosition), import_utils.HAPTICS_FEEDBACK],
  148. "onClick": emitClose
  149. }, null);
  150. }
  151. };
  152. const onClosed = () => emit("closed");
  153. const swipeTo = (index, options) => {
  154. var _a;
  155. return (_a = swipeRef.value) == null ? void 0 : _a.swipeTo(index, options);
  156. };
  157. (0, import_use_expose.useExpose)({
  158. swipeTo
  159. });
  160. (0, import_vue2.onMounted)(resize);
  161. (0, import_vue2.watch)([import_utils.windowWidth, import_utils.windowHeight], resize);
  162. (0, import_vue2.watch)(() => props.startPosition, (value) => setActive(+value));
  163. (0, import_vue2.watch)(() => props.show, (value) => {
  164. const {
  165. images,
  166. startPosition
  167. } = props;
  168. if (value) {
  169. setActive(+startPosition);
  170. (0, import_vue2.nextTick)(() => {
  171. resize();
  172. swipeTo(+startPosition, {
  173. immediate: true
  174. });
  175. });
  176. } else {
  177. emit("close", {
  178. index: state.active,
  179. url: images[state.active]
  180. });
  181. }
  182. });
  183. return () => (0, import_vue.createVNode)(import_popup.Popup, (0, import_vue.mergeProps)({
  184. "class": [bem(), props.className],
  185. "overlayClass": [bem("overlay"), props.overlayClass],
  186. "onClosed": onClosed,
  187. "onUpdate:show": updateShow
  188. }, (0, import_utils.pick)(props, popupProps)), {
  189. default: () => [renderClose(), renderImages(), renderIndex(), renderCover()]
  190. });
  191. }
  192. });