ImagePreviewItem.mjs 7.5 KB

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