From e65ca6c36d83cdb56027f13a40c2bdb581f45725 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Mon, 21 Jun 2021 12:28:31 +0200 Subject: [PATCH] MOBILE-3320 UX: Trigger long press on down event --- src/core/directives/long-press.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/core/directives/long-press.ts b/src/core/directives/long-press.ts index a92f19c47..4d096eea9 100644 --- a/src/core/directives/long-press.ts +++ b/src/core/directives/long-press.ts @@ -25,9 +25,11 @@ import { GestureController } from '@singletons'; }) export class CoreLongPressDirective implements OnInit, OnDestroy { + readonly HOLD_DURATION = 500; + element: HTMLElement; pressGesture?: Gesture; - protected moved = false; + timeout?: NodeJS.Timeout; @Output() longPress = new EventEmitter(); @@ -45,9 +47,15 @@ export class CoreLongPressDirective implements OnInit, OnDestroy { threshold: 0, disableScroll: true, gestureName: 'longpress', - onStart: () => this.moved = false, - onMove: () => this.moved = true, - onEnd: ev => !this.moved && this.longPress.emit(ev.event), + onStart: (event) => { + this.timeout = setTimeout(() => { + this.longPress.emit(event); + + delete this.timeout; + }, this.HOLD_DURATION); + }, + onMove: () => this.clearTimeout(), + onEnd: () => this.clearTimeout(), }, true); this.pressGesture.enable(); @@ -58,6 +66,16 @@ export class CoreLongPressDirective implements OnInit, OnDestroy { */ ngOnDestroy(): void { this.pressGesture?.destroy(); + this.clearTimeout(); + } + + protected clearTimeout(): void { + if (!this.timeout) { + return; + } + + clearTimeout(this.timeout); + delete this.timeout; } }