ImagePreviewItem.mjs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { createVNode as _createVNode } from "vue";
  2. import { ref, watch, computed, reactive, defineComponent } from "vue";
  3. import { clamp, numericProp, preventDefault, createNamespace, makeRequiredProp } 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"],
  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 && deltaTime < TAP_TIME) {
  169. if (doubleTapTimer) {
  170. clearTimeout(doubleTapTimer);
  171. doubleTapTimer = null;
  172. toggleScale();
  173. } else {
  174. doubleTapTimer = setTimeout(() => {
  175. emit("close");
  176. doubleTapTimer = null;
  177. }, TAP_TIME);
  178. }
  179. }
  180. };
  181. const onTouchEnd = (event) => {
  182. let stopPropagation = false;
  183. if (state.moving || state.zooming) {
  184. stopPropagation = true;
  185. if (state.moving && startMoveX === state.moveX && startMoveY === state.moveY) {
  186. stopPropagation = false;
  187. }
  188. if (!event.touches.length) {
  189. if (state.zooming) {
  190. state.moveX = clamp(state.moveX, -maxMoveX.value, maxMoveX.value);
  191. state.moveY = clamp(state.moveY, -maxMoveY.value, maxMoveY.value);
  192. state.zooming = false;
  193. }
  194. state.moving = false;
  195. startMoveX = 0;
  196. startMoveY = 0;
  197. startScale = 1;
  198. if (state.scale < 1) {
  199. resetScale();
  200. }
  201. if (state.scale > props.maxZoom) {
  202. state.scale = +props.maxZoom;
  203. }
  204. }
  205. }
  206. preventDefault(event, stopPropagation);
  207. checkTap();
  208. touch.reset();
  209. };
  210. const onLoad = (event) => {
  211. const {
  212. naturalWidth,
  213. naturalHeight
  214. } = event.target;
  215. state.imageRatio = naturalHeight / naturalWidth;
  216. };
  217. watch(() => props.active, resetScale);
  218. watch(() => props.show, (value) => {
  219. if (!value) {
  220. resetScale();
  221. }
  222. });
  223. useEventListener("touchmove", onTouchMove, {
  224. target: computed(() => {
  225. var _a;
  226. return (_a = swipeItem.value) == null ? void 0 : _a.$el;
  227. })
  228. });
  229. return () => {
  230. const imageSlots = {
  231. loading: () => _createVNode(Loading, {
  232. "type": "spinner"
  233. }, null)
  234. };
  235. return _createVNode(SwipeItem, {
  236. "ref": swipeItem,
  237. "class": bem("swipe-item"),
  238. "onTouchstartPassive": onTouchStart,
  239. "onTouchend": onTouchEnd,
  240. "onTouchcancel": onTouchEnd
  241. }, {
  242. default: () => [slots.image ? _createVNode("div", {
  243. "class": bem("image-wrap")
  244. }, [slots.image({
  245. src: props.src
  246. })]) : _createVNode(Image, {
  247. "src": props.src,
  248. "fit": "contain",
  249. "class": bem("image", {
  250. vertical: vertical.value
  251. }),
  252. "style": imageStyle.value,
  253. "onLoad": onLoad
  254. }, imageSlots)]
  255. });
  256. };
  257. }
  258. });
  259. export {
  260. stdin_default as default
  261. };