From 20fe2cb2894ce8dd204d2fbd3a676e6f5b02f4c5 Mon Sep 17 00:00:00 2001 From: kolirt Date: Tue, 18 Jun 2024 00:59:20 +0300 Subject: [PATCH] add fixTma and unfixTma methods --- lib/helpers/fixTma.ts | 56 +++++++++++++++++++++++++++++++++++++++++++ lib/index.ts | 1 + package.json | 2 +- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 lib/helpers/fixTma.ts diff --git a/lib/helpers/fixTma.ts b/lib/helpers/fixTma.ts new file mode 100644 index 0000000..14fcd2c --- /dev/null +++ b/lib/helpers/fixTma.ts @@ -0,0 +1,56 @@ +const options = { passive: false } +let ts: any +let scrollableEl: HTMLElement | null + +function ignore(e: TouchEvent) { + e.preventDefault() +} + +function onTouchStart(e: TouchEvent) { + ts = e.touches[0].clientY +} + +function onTouchMove(e: TouchEvent) { + if (scrollableEl) { + const scroll = scrollableEl.scrollTop + const te = e.changedTouches[0].clientY + if (scroll <= 0 && ts < te) { + e.preventDefault() + } + } else { + e.preventDefault() + } +} + +function onScroll() { + // @ts-ignore + if (window.scrollY !== window.overflowPadding) { + // @ts-ignore + window.scrollTo(0, window.overflowPadding) + } +} + +export function fixTma(el: HTMLElement | null) { + scrollableEl = el + + // @ts-ignore + window.overflowPadding = 100 + // @ts-ignore + document.body.style.height = window.innerHeight + window.overflowPadding + 'px' + // @ts-ignore + window.scrollTo(0, window.overflowPadding) + + window.addEventListener('scroll', onScroll) + document.documentElement.addEventListener('touchstart', onTouchStart, options) + document.documentElement.addEventListener('touchmove', onTouchMove, options) + document.body.addEventListener('touchcancel', ignore, options) + document.body.addEventListener('touchend', ignore, options) +} + +export function unfixTma() { + window.removeEventListener('scroll', onScroll) + document.documentElement.removeEventListener('touchstart', onTouchStart) + document.documentElement.removeEventListener('touchmove', onTouchMove) + document.body.removeEventListener('touchcancel', ignore) + document.body.removeEventListener('touchend', ignore) +} diff --git a/lib/index.ts b/lib/index.ts index 12b9444..68e9b8a 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,4 +1,5 @@ export * from './actions' +export { fixTma, unfixTma } from './helpers/fixTma' export { createTelegramMiniApp } from './plugin' export { state, updateState } from './state' export { EventType } from './types' diff --git a/package.json b/package.json index ad39392..abb1e98 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kolirt/vue-telegram-mini-app", - "version": "0.0.3", + "version": "0.0.4", "type": "module", "description": "Vue 3 telegram mini app package", "author": "kolirt",