Skip to content

Commit

Permalink
fix: row long press
Browse files Browse the repository at this point in the history
  • Loading branch information
69pmb committed Jan 20, 2024
1 parent 4d44d85 commit 6783b8a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/app/list-composition/list-composition.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
columns: displayedColumnsComposition
"
[appRowAction]="element"
[isMobile]="!isDesktop"
class="example-element-row"
[ngClass]="{
deleted: element.deleted === 1,
Expand Down
1 change: 1 addition & 0 deletions src/app/list-fichier/list-fichier.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
<mat-row
*matRowDef="let compoRow; columns: displayedColumnsComposition"
[appRowAction]="compoRow"
[isMobile]="!isDesktop"
[ngClass]="{deleted: compoRow.deleted === 1}"
>
</mat-row>
Expand Down
2 changes: 2 additions & 0 deletions src/app/list/list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export abstract class ListDirective<T> implements OnInit {
sort?: Sort;
compositionColumns!: string[];
displayedColumnsComposition!: string[];
isDesktop = false;

readonly types = [
new Dropdown('Chanson', 'SONG'),
Expand All @@ -36,6 +37,7 @@ export abstract class ListDirective<T> implements OnInit {
ngOnInit(): void {
this.page = this.initPagination();
this.utilsService.isDesktop().subscribe(isDesktop => {
this.isDesktop = isDesktop;
if (isDesktop) {
this.displayedColumnsComposition = [...this.compositionColumns, 'menu'];
} else {
Expand Down
48 changes: 33 additions & 15 deletions src/app/row-action/row-action.directive.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<TouchEvent>(
this.elementRef.nativeElement,
'touchstart'
).pipe(
tap(() => (out = true)),
debounceTime(this.threshold),
map(() => true)
);
fromEvent(this.el.nativeElement as HTMLElement, 'mouseover')
const touchEnd$ = fromEvent<TouchEvent>(
this.elementRef.nativeElement,
'touchend'
).pipe(map(() => false));
const touchLeave$ = fromEvent<TouchEvent>(
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);
});
}
}
15 changes: 13 additions & 2 deletions src/app/services/utils.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -80,7 +87,11 @@ export class UtilsService {
isDesktop(): Observable<boolean> {
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<string> {
Expand Down

0 comments on commit 6783b8a

Please sign in to comment.