47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { Directive, EventEmitter, HostListener, Input, Output } from '@angular/core';
|
|
|
|
@Directive({
|
|
selector: '[iqserHiddenAction]',
|
|
standalone: true,
|
|
})
|
|
export class HiddenActionDirective {
|
|
@Input() requiredClicks = 4;
|
|
@Output() readonly iqserHiddenAction = new EventEmitter();
|
|
|
|
#dPressed = false;
|
|
#clickCount = 0;
|
|
#clickCountTimeout?: ReturnType<typeof setTimeout>;
|
|
|
|
@HostListener('window:keydown.D')
|
|
onKeydownD() {
|
|
this.#dPressed = true;
|
|
}
|
|
|
|
@HostListener('window:keyup.D')
|
|
onKeyupD() {
|
|
this.#dPressed = false;
|
|
}
|
|
|
|
@HostListener('click', ['$event'])
|
|
countActions($event: MouseEvent): void {
|
|
if (!this.#dPressed) {
|
|
this.#clickCount = 0;
|
|
clearTimeout(this.#clickCountTimeout);
|
|
return;
|
|
}
|
|
|
|
$event.stopPropagation();
|
|
$event.preventDefault();
|
|
|
|
this.#clickCount += 1;
|
|
|
|
if (this.#clickCount === this.requiredClicks) {
|
|
this.#clickCount = 0;
|
|
this.iqserHiddenAction.emit();
|
|
}
|
|
|
|
clearTimeout(this.#clickCountTimeout);
|
|
this.#clickCountTimeout = setTimeout(() => (this.#clickCount = 0), 1000);
|
|
}
|
|
}
|