An example of an Angular directive that also fires the mouse event
import {Directive, ElementRef, Output} from "@angular/core";
import {fromEvent, merge, timer} from "rxjs";
import {filter, skip, switchMap} from "rxjs/operators";
@Directive({
selector: '[appLongClick]'
})
export class LongClickDirective {
/**
* Minimum time between mouse button down to mouse button up
*/
private readonly DUE_TIME = 500;
/**
* Mouse down event (only left button)
*/
private mousedown = fromEvent(this.el.nativeElement, 'mousedown').pipe(
filter((ev: MouseEvent) => ev.button === 0)
);
/**
* Click event (mouse left button up)
*/
private click = fromEvent(this.el.nativeElement, 'click');
/**
* After a mouse button down, take the click only if it comes after the due time
*/
@Output('appLongClick') longClick = this.mousedown.pipe(
switchMap(() =>
merge(this.click, timer(this.DUE_TIME)).pipe(
skip(1),
filter(this.isPointerEvent),
),
),
);
constructor(private el: ElementRef) {}
private isPointerEvent(v: unknown): v is PointerEvent {
return v instanceof PointerEvent;
}
}
Example of use:
<div (appLongClick)="doSomething($event)"></div>