Hidden action: press D key

This commit is contained in:
Adina Țeudan 2022-05-24 16:29:02 +03:00
parent 55fb40dd67
commit 41af1ba194
2 changed files with 32 additions and 11 deletions

View File

@ -1,3 +1,3 @@
<div (click)="countActions()">
<div (click)="countActions($event)">
<ng-content></ng-content>
</div>

View File

@ -1,4 +1,5 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { ChangeDetectionStrategy, Component, EventEmitter, HostListener, Input, Output } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Component({
selector: 'iqser-hidden-action',
@ -9,18 +10,38 @@ import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from
export class HiddenActionComponent {
@Input() requiredClicks = 4;
@Output() readonly action = new EventEmitter();
private _clickCount = 0;
private _clickCountTimeout?: ReturnType<typeof setTimeout>;
#dPressed = new BehaviorSubject(false);
#clickCount = 0;
#clickCountTimeout?: ReturnType<typeof setTimeout>;
countActions(): void {
this._clickCount += 1;
if (this._clickCount === this.requiredClicks) {
this._clickCount = 0;
@HostListener('window:keydown.D', ['$event'])
onKeydownD() {
this.#dPressed.next(true);
}
@HostListener('window:keyup.D', ['$event'])
onKeyupD() {
this.#dPressed.next(false);
}
countActions($event: MouseEvent): void {
if (!this.#dPressed.value) {
this.#clickCount = 0;
clearTimeout(this.#clickCountTimeout);
return;
}
$event.stopPropagation();
$event.preventDefault();
this.#clickCount += 1;
if (this.#clickCount === this.requiredClicks) {
this.#clickCount = 0;
this.action.emit();
}
clearTimeout(this._clickCountTimeout);
this._clickCountTimeout = setTimeout(() => {
this._clickCount = 0;
clearTimeout(this.#clickCountTimeout);
this.#clickCountTimeout = setTimeout(() => {
this.#clickCount = 0;
}, 1000);
}
}