|
1 | | -import { ChangeDetectionStrategy, Component } from '@angular/core'; |
| 1 | +import { ChangeDetectionStrategy, Component, effect, ElementRef, inject, OnDestroy, OnInit, signal, viewChild } from '@angular/core'; |
| 2 | +import { filter, fromEvent, map, scan, takeWhile, finalize, timer } from 'rxjs'; |
| 3 | +import { AsyncPipe } from '@angular/common'; |
| 4 | +import { TextLoaderService } from './text-loader.service'; |
| 5 | +import { discardIrrelevantKeys, extractKey } from './utils'; |
| 6 | +import JSConfetti from 'js-confetti'; |
| 7 | +import { toObservable } from '@angular/core/rxjs-interop'; |
2 | 8 |
|
3 | 9 | @Component({ |
4 | 10 | selector: 'app-main-area', |
5 | | - imports: [], |
| 11 | + imports: [AsyncPipe], |
6 | 12 | templateUrl: './main-area.component.html', |
7 | 13 | styleUrl: './main-area.component.scss', |
8 | 14 | changeDetection: ChangeDetectionStrategy.OnPush, |
9 | 15 | host: { |
10 | | - class: "bg-bg-light col-start-2 col-span-4 row-span-5 rounded-xl p-2" |
| 16 | + class: ` |
| 17 | + overflow-scroll no-scrollbar |
| 18 | + relative bg-light rounded-xl p-2 |
| 19 | + col-start-3 col-span-2 row-span-5 |
| 20 | + border-5 border-double border-secondary |
| 21 | + `, |
| 22 | + "[style.fontFamily]": "'Roboto Mono'", |
11 | 23 | } |
12 | 24 | }) |
13 | | -export class MainAreaComponent { |
| 25 | +export class MainAreaComponent implements OnInit, OnDestroy { |
14 | 26 |
|
| 27 | + hostElement = inject(ElementRef).nativeElement as HTMLElement; |
| 28 | + textareaElement = viewChild<ElementRef<HTMLTextAreaElement>>("textarea"); |
| 29 | + textareaFocus$ = toObservable(this.textareaElement).pipe( // to retrieve lost focus on textarea |
| 30 | + filter(value => value != undefined), |
| 31 | + map(textarea => textarea.nativeElement.focus()), |
| 32 | + ); |
| 33 | + |
| 34 | + // TODO: create confetti service |
| 35 | + confetti = new JSConfetti(); // confetti at the end of typing session |
| 36 | + |
| 37 | + textLoaderService = inject(TextLoaderService); |
| 38 | + loadedText = this.textLoaderService.text; |
| 39 | + characterArray: string[] = []; |
| 40 | + finishedTyping = signal(false); |
| 41 | + |
| 42 | + // TYPING STREAM |
| 43 | + typing$ = fromEvent<KeyboardEvent>(this.hostElement, "keydown").pipe( |
| 44 | + filter(discardIrrelevantKeys), // to filter out "Shift" / "CapsLock" / etc... |
| 45 | + map(extractKey), |
| 46 | + scan((acc, key) => { |
| 47 | + let index = (key === "Backspace") ? --acc[0] : ++acc[0]; |
| 48 | + index = (index < 0) ? -1 : index; // min index: -1 |
| 49 | + return [index, key] as [number, string]; |
| 50 | + }, [-1, ""] as [number, string]), |
| 51 | + takeWhile(([index, _key]) => index < this.characterArray.length - 1), // TODO: you must add extra char to finish the session |
| 52 | + map(([index, char]) => { |
| 53 | + return [index, char, this.characterArray[index] === char] as [number, string, boolean]; |
| 54 | + }), |
| 55 | + scan((error_counter, [_index, key, isCorrectKey]) => { |
| 56 | + if (!isCorrectKey && key !== "Backspace") { |
| 57 | + error_counter.set(key, (error_counter.get(key) ?? 0) + 1) |
| 58 | + }; |
| 59 | + return error_counter; |
| 60 | + }, new Map(this.characterArray.map(char => [char, 0]))), |
| 61 | + finalize(() => { |
| 62 | + timer(200).subscribe(() => { |
| 63 | + this.finishedTyping.set(true); |
| 64 | + this.confetti.addConfetti(); |
| 65 | + }); |
| 66 | + timer(5000).subscribe(() => { |
| 67 | + this.confetti.destroyCanvas(); |
| 68 | + }) |
| 69 | + }), |
| 70 | + ); |
| 71 | + |
| 72 | + constructor() { |
| 73 | + // TODO: computed / linkedSignal instead? |
| 74 | + effect(() => { // new text -> new character array |
| 75 | + let text = this.loadedText() ?? ""; |
| 76 | + this.characterArray = [...text]; |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + ngOnInit() { |
| 81 | + this.hostElement.scrollTo({ top: 0, behavior: "smooth" }); |
| 82 | + this.typing$.subscribe(v => console.log("v: ", v)); |
| 83 | + |
| 84 | + // TODO: autoscrolling |
| 85 | + //interval(50).pipe(delay(2_000)).subscribe(val => this.host.scrollTo({top: val / 5, behavior: "smooth"})) |
| 86 | + } |
| 87 | + |
| 88 | + ngOnDestroy() { |
| 89 | + this.confetti.destroyCanvas(); |
| 90 | + } |
15 | 91 | } |
0 commit comments