ImagePreviewItem.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 name in all)
  7. __defProp(target, name, { get: all[name], 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_use_touch = require("../composables/use-touch");
  27. var import_use = require("@vant/use");
  28. var import_image = require("../image");
  29. var import_loading = require("../loading");
  30. var import_swipe_item = require("../swipe-item");
  31. const getDistance = (touches) => Math.sqrt((touches[0].clientX - touches[1].clientX) ** 2 + (touches[0].clientY - touches[1].clientY) ** 2);
  32. const bem = (0, import_utils.createNamespace)("image-preview")[1];
  33. var stdin_default = (0, import_vue2.defineComponent)({
  34. props: {
  35. src: String,
  36. show: Boolean,
  37. active: Number,
  38. minZoom: (0, import_utils.makeRequiredProp)(import_utils.numericProp),
  39. maxZoom: (0, import_utils.makeRequiredProp)(import_utils.numericProp),
  40. rootWidth: (0, import_utils.makeRequiredProp)(Number),
  41. rootHeight: (0, import_utils.makeRequiredProp)(Number)
  42. },
  43. emits: ["scale", "close"],
  44. setup(props, {
  45. emit,
  46. slots
  47. }) {
  48. const state = (0, import_vue2.reactive)({
  49. scale: 1,
  50. moveX: 0,
  51. moveY: 0,
  52. moving: false,
  53. zooming: false,
  54. imageRatio: 0,
  55. displayWidth: 0,
  56. displayHeight: 0
  57. });
  58. const touch = (0, import_use_touch.useTouch)();
  59. const swipeItem = (0, import_vue2.ref)();
  60. const vertical = (0, import_vue2.computed)(() => {
  61. const {
  62. rootWidth,
  63. rootHeight
  64. } = props;
  65. const rootRatio = rootHeight / rootWidth;
  66. return state.imageRatio > rootRatio;
  67. });
  68. const imageStyle = (0, import_vue2.computed)(() => {
  69. const {
  70. scale,
  71. moveX,
  72. moveY,
  73. moving,
  74. zooming
  75. } = state;
  76. const style = {
  77. transitionDuration: zooming || moving ? "0s" : ".3s"
  78. };
  79. if (scale !== 1) {
  80. const offsetX = moveX / scale;
  81. const offsetY = moveY / scale;
  82. style.transform = `scale(${scale}, ${scale}) translate(${offsetX}px, ${offsetY}px)`;
  83. }
  84. return style;
  85. });
  86. const maxMoveX = (0, import_vue2.computed)(() => {
  87. if (state.imageRatio) {
  88. const {
  89. rootWidth,
  90. rootHeight
  91. } = props;
  92. const displayWidth = vertical.value ? rootHeight / state.imageRatio : rootWidth;
  93. return Math.max(0, (state.scale * displayWidth - rootWidth) / 2);
  94. }
  95. return 0;
  96. });
  97. const maxMoveY = (0, import_vue2.computed)(() => {
  98. if (state.imageRatio) {
  99. const {
  100. rootWidth,
  101. rootHeight
  102. } = props;
  103. const displayHeight = vertical.value ? rootHeight : rootWidth * state.imageRatio;
  104. return Math.max(0, (state.scale * displayHeight - rootHeight) / 2);
  105. }
  106. return 0;
  107. });
  108. const setScale = (scale) => {
  109. scale = (0, import_utils.clamp)(scale, +props.minZoom, +props.maxZoom + 1);
  110. if (scale !== state.scale) {
  111. state.scale = scale;
  112. emit("scale", {
  113. scale,
  114. index: props.active
  115. });
  116. }
  117. };
  118. const resetScale = () => {
  119. setScale(1);
  120. state.moveX = 0;
  121. state.moveY = 0;
  122. };
  123. const toggleScale = () => {
  124. const scale = state.scale > 1 ? 1 : 2;
  125. setScale(scale);
  126. state.moveX = 0;
  127. state.moveY = 0;
  128. };
  129. let fingerNum;
  130. let startMoveX;
  131. let startMoveY;
  132. let startScale;
  133. let startDistance;
  134. let doubleTapTimer;
  135. let touchStartTime;
  136. const onTouchStart = (event) => {
  137. const {
  138. touches
  139. } = event;
  140. const {
  141. offsetX
  142. } = touch;
  143. touch.start(event);
  144. fingerNum = touches.length;
  145. startMoveX = state.moveX;
  146. startMoveY = state.moveY;
  147. touchStartTime = Date.now();
  148. state.moving = fingerNum === 1 && state.scale !== 1;
  149. state.zooming = fingerNum === 2 && !offsetX.value;
  150. if (state.zooming) {
  151. startScale = state.scale;
  152. startDistance = getDistance(event.touches);
  153. }
  154. };
  155. const onTouchMove = (event) => {
  156. const {
  157. touches
  158. } = event;
  159. touch.move(event);
  160. if (state.moving || state.zooming) {
  161. (0, import_utils.preventDefault)(event, true);
  162. }
  163. if (state.moving) {
  164. const {
  165. deltaX,
  166. deltaY
  167. } = touch;
  168. const moveX = deltaX.value + startMoveX;
  169. const moveY = deltaY.value + startMoveY;
  170. state.moveX = (0, import_utils.clamp)(moveX, -maxMoveX.value, maxMoveX.value);
  171. state.moveY = (0, import_utils.clamp)(moveY, -maxMoveY.value, maxMoveY.value);
  172. }
  173. if (state.zooming && touches.length === 2) {
  174. const distance = getDistance(touches);
  175. const scale = startScale * distance / startDistance;
  176. setScale(scale);
  177. }
  178. };
  179. const checkTap = () => {
  180. if (fingerNum > 1) {
  181. return;
  182. }
  183. const {
  184. offsetX,
  185. offsetY
  186. } = touch;
  187. const deltaTime = Date.now() - touchStartTime;
  188. const TAP_TIME = 250;
  189. const TAP_OFFSET = 5;
  190. if (offsetX.value < TAP_OFFSET && offsetY.value < TAP_OFFSET && deltaTime < TAP_TIME) {
  191. if (doubleTapTimer) {
  192. clearTimeout(doubleTapTimer);
  193. doubleTapTimer = null;
  194. toggleScale();
  195. } else {
  196. doubleTapTimer = setTimeout(() => {
  197. emit("close");
  198. doubleTapTimer = null;
  199. }, TAP_TIME);
  200. }
  201. }
  202. };
  203. const onTouchEnd = (event) => {
  204. let stopPropagation = false;
  205. if (state.moving || state.zooming) {
  206. stopPropagation = true;
  207. if (state.moving && startMoveX === state.moveX && startMoveY === state.moveY) {
  208. stopPropagation = false;
  209. }
  210. if (!event.touches.length) {
  211. if (state.zooming) {
  212. state.moveX = (0, import_utils.clamp)(state.moveX, -maxMoveX.value, maxMoveX.value);
  213. state.moveY = (0, import_utils.clamp)(state.moveY, -maxMoveY.value, maxMoveY.value);
  214. state.zooming = false;
  215. }
  216. state.moving = false;
  217. startMoveX = 0;
  218. startMoveY = 0;
  219. startScale = 1;
  220. if (state.scale < 1) {
  221. resetScale();
  222. }
  223. if (state.scale > props.maxZoom) {
  224. state.scale = +props.maxZoom;
  225. }
  226. }
  227. }
  228. (0, import_utils.preventDefault)(event, stopPropagation);
  229. checkTap();
  230. touch.reset();
  231. };
  232. const onLoad = (event) => {
  233. const {
  234. naturalWidth,
  235. naturalHeight
  236. } = event.target;
  237. state.imageRatio = naturalHeight / naturalWidth;
  238. };
  239. (0, import_vue2.watch)(() => props.active, resetScale);
  240. (0, import_vue2.watch)(() => props.show, (value) => {
  241. if (!value) {
  242. resetScale();
  243. }
  244. });
  245. (0, import_use.useEventListener)("touchmove", onTouchMove, {
  246. target: (0, import_vue2.computed)(() => {
  247. var _a;
  248. return (_a = swipeItem.value) == null ? void 0 : _a.$el;
  249. })
  250. });
  251. return () => {
  252. const imageSlots = {
  253. loading: () => (0, import_vue.createVNode)(import_loading.Loading, {
  254. "type": "spinner"
  255. }, null)
  256. };
  257. return (0, import_vue.createVNode)(import_swipe_item.SwipeItem, {
  258. "ref": swipeItem,
  259. "class": bem("swipe-item"),
  260. "onTouchstartPassive": onTouchStart,
  261. "onTouchend": onTouchEnd,
  262. "onTouchcancel": onTouchEnd
  263. }, {
  264. default: () => [slots.image ? (0, import_vue.createVNode)("div", {
  265. "class": bem("image-wrap")
  266. }, [slots.image({
  267. src: props.src
  268. })]) : (0, import_vue.createVNode)(import_image.Image, {
  269. "src": props.src,
  270. "fit": "contain",
  271. "class": bem("image", {
  272. vertical: vertical.value
  273. }),
  274. "style": imageStyle.value,
  275. "onLoad": onLoad
  276. }, imageSlots)]
  277. });
  278. };
  279. }
  280. });