UNPKG

tdesign-mobile-vue

Version:
159 lines (151 loc) 5.91 kB
/** * tdesign v1.9.3 * (c) 2025 TDesign Group * @license MIT */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _slicedToArray = require('@babel/runtime/helpers/slicedToArray'); var vue = require('vue'); var core = require('@vueuse/core'); var shared_dom = require('../shared/dom.js'); var isObject = require('../_chunks/dep-ef223206.js'); var shared_util = require('../shared/util.js'); require('../_chunks/dep-88fe047a.js'); require('../_chunks/dep-2f809ed9.js'); require('@babel/runtime/helpers/typeof'); require('../_chunks/dep-c3cb976c.js'); require('../_chunks/dep-757b152c.js'); require('../_chunks/dep-5be9198d.js'); require('../hooks/useClass.js'); require('../_chunks/dep-21f18d3b.js'); require('@babel/runtime/helpers/defineProperty'); require('../_chunks/dep-b9642a56.js'); require('../_common/js/global-config/mobile/default-config.js'); require('../_common/js/global-config/mobile/locale/zh_CN.js'); require('../_chunks/dep-28b1e09d.js'); require('../_chunks/dep-57aa1aa0.js'); require('dayjs'); require('../_chunks/dep-85204fa0.js'); require('../_chunks/dep-f6b14f80.js'); require('../_chunks/dep-a8d60643.js'); require('../_chunks/dep-7c911ba3.js'); require('../_chunks/dep-49f0a63e.js'); require('../_chunks/dep-d950aa21.js'); require('../_chunks/dep-a697b1b9.js'); require('../_chunks/dep-4dfb9b9c.js'); require('../_chunks/dep-c65deed7.js'); require('../_chunks/dep-94eeec5a.js'); require('../_chunks/dep-060bf1cf.js'); require('../_chunks/dep-0e05e959.js'); require('../_chunks/dep-324da301.js'); require('../_chunks/dep-da6dc2cf.js'); require('../_chunks/dep-ccc9ad3d.js'); require('../_chunks/dep-ff4786c0.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray); var noop = function noop() {}; function useSwipe(target) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var _options$threshold = options.threshold, threshold = _options$threshold === void 0 ? 50 : _options$threshold, onSwipe = options.onSwipe, onSwipeEnd = options.onSwipeEnd, onSwipeStart = options.onSwipeStart, _options$listenerOpti = options.listenerOptions, listenerOptions = _options$listenerOpti === void 0 ? false : _options$listenerOpti; var coordsStart = vue.reactive({ x: 0, y: 0 }); var coordsEnd = vue.reactive({ x: 0, y: 0 }); var diffX = vue.computed(function () { return coordsStart.x - coordsEnd.x; }); var diffY = vue.computed(function () { return coordsStart.y - coordsEnd.y; }); var max = Math.max, abs = Math.abs; var isThresholdExceeded = vue.computed(function () { return max(abs(diffX.value), abs(diffY.value)) >= threshold; }); var isSwiping = vue.ref(false); var direction = vue.computed(function () { if (!isThresholdExceeded.value) return "none"; if (abs(diffX.value) > abs(diffY.value)) { return diffX.value > 0 ? "left" : "right"; } return diffY.value > 0 ? "up" : "down"; }); var getTouchEventCoords = function getTouchEventCoords(e) { return [e.touches[0].clientX, e.touches[0].clientY]; }; var updateCoordsStart = function updateCoordsStart(x, y) { coordsStart.x = x; coordsStart.y = y; }; var updateCoordsEnd = function updateCoordsEnd(x, y) { coordsEnd.x = x; coordsEnd.y = y; }; var isPassiveEventSupported = checkPassiveEventSupport(); var onTouchEnd = function onTouchEnd(e) { if (isSwiping.value) onSwipeEnd === null || onSwipeEnd === void 0 || onSwipeEnd(e, direction.value); isSwiping.value = false; }; var stops = [core.useEventListener(target, "touchstart", function (e) { if (e.touches.length !== 1) return; if (listenerOptions === true || isObject.isObject(listenerOptions) && listenerOptions.capture && !listenerOptions.passive) shared_dom.preventDefault(e, false); var _getTouchEventCoords = getTouchEventCoords(e), _getTouchEventCoords2 = _slicedToArray__default["default"](_getTouchEventCoords, 2), x = _getTouchEventCoords2[0], y = _getTouchEventCoords2[1]; updateCoordsStart(x, y); updateCoordsEnd(x, y); onSwipeStart === null || onSwipeStart === void 0 || onSwipeStart(e); }, listenerOptions), core.useEventListener(target, "touchmove", function (e) { if (e.touches.length !== 1) return; var _getTouchEventCoords3 = getTouchEventCoords(e), _getTouchEventCoords4 = _slicedToArray__default["default"](_getTouchEventCoords3, 2), x = _getTouchEventCoords4[0], y = _getTouchEventCoords4[1]; updateCoordsEnd(x, y); if (!isSwiping.value && isThresholdExceeded.value) isSwiping.value = true; if (isSwiping.value) onSwipe === null || onSwipe === void 0 || onSwipe(e); }, listenerOptions), core.useEventListener(target, "touchend", onTouchEnd, listenerOptions), core.useEventListener(target, "touchcancel", onTouchEnd, listenerOptions)]; var stop = function stop() { return stops.forEach(function (s) { return s(); }); }; return { isPassiveEventSupported: isPassiveEventSupported, isSwiping: isSwiping, direction: direction, coordsStart: coordsStart, coordsEnd: coordsEnd, lengthX: diffX, lengthY: diffY, stop: stop }; } function checkPassiveEventSupport() { var document = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : shared_util.isBrowser ? window.document : void 0; if (!document) return false; var supportsPassive = false; var optionsBlock = { get passive() { supportsPassive = true; return false; } }; document.addEventListener("x", noop, optionsBlock); document.removeEventListener("x", noop); return supportsPassive; } exports.useSwipe = useSwipe; //# sourceMappingURL=useSwipe.js.map