UNPKG

2.74 kBJavaScriptView Raw
1var __defProp = Object.defineProperty;
2var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3var __getOwnPropNames = Object.getOwnPropertyNames;
4var __hasOwnProp = Object.prototype.hasOwnProperty;
5var __export = (target, all) => {
6 for (var name in all)
7 __defProp(target, name, { get: all[name], enumerable: true });
8};
9var __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};
17var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18var stdin_exports = {};
19__export(stdin_exports, {
20 useTouch: () => useTouch
21});
22module.exports = __toCommonJS(stdin_exports);
23var import_vue = require("vue");
24var import_utils = require("../utils");
25function getDirection(x, y) {
26 if (x > y) {
27 return "horizontal";
28 }
29 if (y > x) {
30 return "vertical";
31 }
32 return "";
33}
34function useTouch() {
35 const startX = (0, import_vue.ref)(0);
36 const startY = (0, import_vue.ref)(0);
37 const deltaX = (0, import_vue.ref)(0);
38 const deltaY = (0, import_vue.ref)(0);
39 const offsetX = (0, import_vue.ref)(0);
40 const offsetY = (0, import_vue.ref)(0);
41 const direction = (0, import_vue.ref)("");
42 const isTap = (0, import_vue.ref)(true);
43 const isVertical = () => direction.value === "vertical";
44 const isHorizontal = () => direction.value === "horizontal";
45 const reset = () => {
46 deltaX.value = 0;
47 deltaY.value = 0;
48 offsetX.value = 0;
49 offsetY.value = 0;
50 direction.value = "";
51 isTap.value = true;
52 };
53 const start = (event) => {
54 reset();
55 startX.value = event.touches[0].clientX;
56 startY.value = event.touches[0].clientY;
57 };
58 const move = (event) => {
59 const touch = event.touches[0];
60 deltaX.value = (touch.clientX < 0 ? 0 : touch.clientX) - startX.value;
61 deltaY.value = touch.clientY - startY.value;
62 offsetX.value = Math.abs(deltaX.value);
63 offsetY.value = Math.abs(deltaY.value);
64 const LOCK_DIRECTION_DISTANCE = 10;
65 if (!direction.value || offsetX.value < LOCK_DIRECTION_DISTANCE && offsetY.value < LOCK_DIRECTION_DISTANCE) {
66 direction.value = getDirection(offsetX.value, offsetY.value);
67 }
68 if (isTap.value && (offsetX.value > import_utils.TAP_OFFSET || offsetY.value > import_utils.TAP_OFFSET)) {
69 isTap.value = false;
70 }
71 };
72 return {
73 move,
74 start,
75 reset,
76 startX,
77 startY,
78 deltaX,
79 deltaY,
80 offsetX,
81 offsetY,
82 direction,
83 isVertical,
84 isHorizontal,
85 isTap
86 };
87}