From 6783b8a98926dde8eab5b1a183f0d04cd6638907 Mon Sep 17 00:00:00 2001 From: 69pmb Date: Sat, 20 Jan 2024 17:25:13 +0100 Subject: [PATCH] fix: row long press --- .../list-composition.component.html | 1 + .../list-fichier/list-fichier.component.html | 1 + src/app/list/list.component.ts | 2 + src/app/row-action/row-action.directive.ts | 48 +++++++++++++------ src/app/services/utils.service.ts | 15 +++++- 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/app/list-composition/list-composition.component.html b/src/app/list-composition/list-composition.component.html index 7157065..3f93b65 100644 --- a/src/app/list-composition/list-composition.component.html +++ b/src/app/list-composition/list-composition.component.html @@ -191,6 +191,7 @@ columns: displayedColumnsComposition " [appRowAction]="element" + [isMobile]="!isDesktop" class="example-element-row" [ngClass]="{ deleted: element.deleted === 1, diff --git a/src/app/list-fichier/list-fichier.component.html b/src/app/list-fichier/list-fichier.component.html index 2f377d6..188b3a4 100644 --- a/src/app/list-fichier/list-fichier.component.html +++ b/src/app/list-fichier/list-fichier.component.html @@ -195,6 +195,7 @@ diff --git a/src/app/list/list.component.ts b/src/app/list/list.component.ts index 65c618a..4ea241c 100644 --- a/src/app/list/list.component.ts +++ b/src/app/list/list.component.ts @@ -15,6 +15,7 @@ export abstract class ListDirective implements OnInit { sort?: Sort; compositionColumns!: string[]; displayedColumnsComposition!: string[]; + isDesktop = false; readonly types = [ new Dropdown('Chanson', 'SONG'), @@ -36,6 +37,7 @@ export abstract class ListDirective implements OnInit { ngOnInit(): void { this.page = this.initPagination(); this.utilsService.isDesktop().subscribe(isDesktop => { + this.isDesktop = isDesktop; if (isDesktop) { this.displayedColumnsComposition = [...this.compositionColumns, 'menu']; } else { diff --git a/src/app/row-action/row-action.directive.ts b/src/app/row-action/row-action.directive.ts index 8d7203a..46268d7 100644 --- a/src/app/row-action/row-action.directive.ts +++ b/src/app/row-action/row-action.directive.ts @@ -1,5 +1,5 @@ import {AfterViewInit, Directive, ElementRef, Input} from '@angular/core'; -import {fromEvent, debounceTime, filter, tap, combineLatestWith} from 'rxjs'; +import {fromEvent, filter, merge, map, tap, debounceTime} from 'rxjs'; import {Composition} from '../utils/model'; import {UtilsService} from '@services/utils.service'; @@ -10,24 +10,42 @@ export class RowActionDirective implements AfterViewInit { @Input() appRowAction!: Composition; - constructor(private el: ElementRef, private utilsService: UtilsService) {} + @Input() + isMobile = false; + + private readonly threshold = 1000; + + constructor( + private elementRef: ElementRef, + private utilsService: UtilsService + ) {} ngAfterViewInit(): void { - let out = false; - fromEvent(this.el.nativeElement as HTMLElement, 'mouseout').subscribe( - () => (out = true) + let out = true; + const touchStart$ = fromEvent( + this.elementRef.nativeElement, + 'touchstart' + ).pipe( + tap(() => (out = true)), + debounceTime(this.threshold), + map(() => true) ); - fromEvent(this.el.nativeElement as HTMLElement, 'mouseover') + const touchEnd$ = fromEvent( + this.elementRef.nativeElement, + 'touchend' + ).pipe(map(() => false)); + const touchLeave$ = fromEvent( + this.elementRef.nativeElement, + 'touchleave' + ).pipe(map(() => false)); + + merge(touchStart$, touchLeave$, touchEnd$) .pipe( - tap(() => (out = false)), - debounceTime(1000), - filter(() => !out), - combineLatestWith( - this.utilsService.isDesktop().pipe(filter(isDesktop => !isDesktop)) - ) + tap(v => (out = out && v)), + filter(() => out && this.isMobile) ) - .subscribe(() => - this.utilsService.compositionInClipBoard(this.appRowAction) - ); + .subscribe(() => { + this.utilsService.compositionInClipBoard(this.appRowAction); + }); } } diff --git a/src/app/services/utils.service.ts b/src/app/services/utils.service.ts index 21ab53a..9fe4444 100644 --- a/src/app/services/utils.service.ts +++ b/src/app/services/utils.service.ts @@ -1,4 +1,11 @@ -import {Observable, catchError, of, map} from 'rxjs'; +import { + Observable, + catchError, + of, + map, + distinctUntilChanged, + debounceTime, +} from 'rxjs'; import {Injectable} from '@angular/core'; import { HttpHeaders, @@ -80,7 +87,11 @@ export class UtilsService { isDesktop(): Observable { return this.breakpointObserver .observe([Breakpoints.Medium, Breakpoints.Large, Breakpoints.XLarge]) - .pipe(map(b => b.matches)); + .pipe( + debounceTime(200), + map(b => b.matches), + distinctUntilChanged() + ); } wikisearch(term: string): Observable {