ImagePreviewItem.js 8.4 KB

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