From 2374bfe0d37dfbcdc5c5a0c5b82256f3e930e9e0 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Sun, 9 Jun 2024 00:10:47 +0300 Subject: [PATCH] feat: textarea/div component in nwui --- .gitignore | 2 + .../components/button/button.component.scss | 2 +- .../lib/components/button/button.component.ts | 4 +- nwaifu-ui/src/lib/components/index.ts | 1 + .../src/lib/components/textarea/index.ts | 1 + .../textarea/textarea.component.html | 5 ++ .../textarea/textarea.component.scss | 20 +++++++ .../components/textarea/textarea.component.ts | 36 +++++++++++++ src/app/app.component.html | 1 - src/app/app.component.scss | 9 ++++ src/app/app.component.ts | 7 ++- src/app/app.config.ts | 4 +- .../translate_block.component.html | 16 ++++-- .../translate_block.component.scss | 54 ++----------------- .../translate_block.component.ts | 30 +++++------ src/app/pipes/translate.pipe.ts | 10 ---- 16 files changed, 108 insertions(+), 94 deletions(-) create mode 100644 nwaifu-ui/src/lib/components/textarea/index.ts create mode 100644 nwaifu-ui/src/lib/components/textarea/textarea.component.html create mode 100644 nwaifu-ui/src/lib/components/textarea/textarea.component.scss create mode 100644 nwaifu-ui/src/lib/components/textarea/textarea.component.ts delete mode 100644 src/app/pipes/translate.pipe.ts diff --git a/.gitignore b/.gitignore index f9418f1..c5b7758 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,5 @@ Thumbs.db .nx/cache .angular + +*.nps \ No newline at end of file diff --git a/nwaifu-ui/src/lib/components/button/button.component.scss b/nwaifu-ui/src/lib/components/button/button.component.scss index 30dcfc8..76dc2f7 100644 --- a/nwaifu-ui/src/lib/components/button/button.component.scss +++ b/nwaifu-ui/src/lib/components/button/button.component.scss @@ -7,7 +7,7 @@ button { color: #f5f6fa; border-radius: 15px; transition: ease-in-out 0.2s; - margin: 2rem; + width: 100%; &:hover, &:active { transform: scale(1.2); diff --git a/nwaifu-ui/src/lib/components/button/button.component.ts b/nwaifu-ui/src/lib/components/button/button.component.ts index fc06d8d..1ecf567 100644 --- a/nwaifu-ui/src/lib/components/button/button.component.ts +++ b/nwaifu-ui/src/lib/components/button/button.component.ts @@ -9,7 +9,7 @@ import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; imports: [CommonModule], changeDetection: ChangeDetectionStrategy.OnPush, }) -export class ButtonComponent { +export class NWUIButtonComponent { @Input() disabled = false; - @Input() type: string | undefined; + @Input() type = 'button'; } diff --git a/nwaifu-ui/src/lib/components/index.ts b/nwaifu-ui/src/lib/components/index.ts index eaf5eea..770f736 100644 --- a/nwaifu-ui/src/lib/components/index.ts +++ b/nwaifu-ui/src/lib/components/index.ts @@ -1 +1,2 @@ export * from './button'; +export * from './textarea'; diff --git a/nwaifu-ui/src/lib/components/textarea/index.ts b/nwaifu-ui/src/lib/components/textarea/index.ts new file mode 100644 index 0000000..8457e35 --- /dev/null +++ b/nwaifu-ui/src/lib/components/textarea/index.ts @@ -0,0 +1 @@ +export * from './textarea.component'; diff --git a/nwaifu-ui/src/lib/components/textarea/textarea.component.html b/nwaifu-ui/src/lib/components/textarea/textarea.component.html new file mode 100644 index 0000000..611e770 --- /dev/null +++ b/nwaifu-ui/src/lib/components/textarea/textarea.component.html @@ -0,0 +1,5 @@ +
+ @if(!value) { + + } +
diff --git a/nwaifu-ui/src/lib/components/textarea/textarea.component.scss b/nwaifu-ui/src/lib/components/textarea/textarea.component.scss new file mode 100644 index 0000000..3eeba0f --- /dev/null +++ b/nwaifu-ui/src/lib/components/textarea/textarea.component.scss @@ -0,0 +1,20 @@ +.nwui-textarea { + border-radius: 8px; + height: auto; + color: #efdee0; + outline: none; + padding-inline: 2rem 1rem; + background-color: transparent; + border: 2px solid #e4bdc3; + font-size: 1rem; + font-weight: inherit; + min-height: 3rem; + line-height: 3rem; + &:focus { + border: 2px solid #efdee0; + } + &.disabled { + opacity: 0.5; + } + word-wrap: break-word; +} diff --git a/nwaifu-ui/src/lib/components/textarea/textarea.component.ts b/nwaifu-ui/src/lib/components/textarea/textarea.component.ts new file mode 100644 index 0000000..34d3c12 --- /dev/null +++ b/nwaifu-ui/src/lib/components/textarea/textarea.component.ts @@ -0,0 +1,36 @@ +import { CommonModule } from '@angular/common'; +import { AfterViewInit, Component, ElementRef, EventEmitter, Input, Output, ViewChild } from '@angular/core'; + +@Component({ + selector: 'nwui-textarea', + imports: [CommonModule], + standalone: true, + styleUrls: ['./textarea.component.scss'], + templateUrl: './textarea.component.html', + // changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class NWUITextAreaComponent implements AfterViewInit { + @Input() disabled = false; + @Input() value = ''; + @Input() contenteditable = true; + // eslint-disable-next-line @typescript-eslint/no-empty-function + @Output() leave = new EventEmitter(); + @ViewChild('ref') ref: ElementRef | null = null; + + get className(): string { + return `nwui-textarea ${this.disabled && !this.contenteditable ? 'disabled' : ''}`; + } + + leaveFn() { + if (this.ref) { + const target = this.ref.nativeElement; + const text = target.textContent || ''; + if (this.leave) this.leave.emit(text); + target.textContent = text; + } + } + + ngAfterViewInit(): void { + if (this.ref && this.value) this.ref.nativeElement.textContent = this.value; + } +} diff --git a/src/app/app.component.html b/src/app/app.component.html index 46eaa7b..1b012e9 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -8,4 +8,3 @@ - diff --git a/src/app/app.component.scss b/src/app/app.component.scss index e69de29..4971974 100644 --- a/src/app/app.component.scss +++ b/src/app/app.component.scss @@ -0,0 +1,9 @@ +.btns { + display: flex; + flex-direction: row; + justify-content: flex-start; + align-items: center; + nwui-button { + margin: 2rem; + } +} diff --git a/src/app/app.component.ts b/src/app/app.component.ts index bec11d2..a2cd933 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,12 +1,11 @@ import { Component, OnInit, ViewChild } from '@angular/core'; -import { RouterModule } from '@angular/router'; -import { ButtonComponent } from '@nwaifu-ui'; +import { NWUIButtonComponent } from '@nwaifu-ui'; import { TextListComponent } from './components/text_list/text_list.component'; import { TranslateData } from './dto/translate_data.dto'; import { parse } from './lib/parser'; @Component({ standalone: true, - imports: [RouterModule, TextListComponent, ButtonComponent], + imports: [TextListComponent, NWUIButtonComponent], selector: 'app-root', templateUrl: './app.component.html', styleUrl: './app.component.scss', @@ -33,7 +32,7 @@ export class AppComponent implements OnInit { submitFile($event: Event) { const target = $event.target as HTMLInputElement; if (target.files) { - const file = target.files?.[0]; + const file = target.files[0]; if (file) { const reader = new FileReader(); reader.onload = () => { diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 93940bf..1c0c942 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,8 +1,6 @@ import { provideHttpClient } from '@angular/common/http'; import { ApplicationConfig } from '@angular/core'; -import { provideRouter } from '@angular/router'; -import { appRoutes } from './app.routes'; export const appConfig: ApplicationConfig = { - providers: [provideHttpClient(), provideRouter(appRoutes)], + providers: [provideHttpClient()], }; diff --git a/src/app/components/translate_block/translate_block.component.html b/src/app/components/translate_block/translate_block.component.html index c008e3d..7c60d57 100644 --- a/src/app/components/translate_block/translate_block.component.html +++ b/src/app/components/translate_block/translate_block.component.html @@ -1,12 +1,18 @@

{{ index + 1 }}

-
{{ item.english_text }}
-
+ {{ item.english_text }} + {{ item.translated_text }}
- Translate - - + Translate + Edit + Clear
diff --git a/src/app/components/translate_block/translate_block.component.scss b/src/app/components/translate_block/translate_block.component.scss index 4203fee..6c2a4ac 100644 --- a/src/app/components/translate_block/translate_block.component.scss +++ b/src/app/components/translate_block/translate_block.component.scss @@ -1,34 +1,3 @@ -.translated-text { - border-radius: 8px; - height: auto; - color: #efdee0; - outline: none; - padding-inline: 2rem 1rem; - background-color: transparent; - border: 2px solid #e4bdc3; - font-size: 1rem; - font-weight: inherit; - min-height: 3rem; - line-height: 3rem; - &:focus { - border: 2px solid #efdee0; - } - &:read-only { - opacity: 0.5; - } - word-wrap: break-word; -} - -.english-text { - color: #efdee0; - border: 2px solid #e4bdc3; - word-wrap: break-word; - border-radius: 8px; - padding-inline: 2rem 1rem; - line-height: 3rem; - background-color: transparent; -} - .element { display: flex; background-color: #413738; @@ -62,24 +31,9 @@ display: flex; flex-direction: column; gap: 1rem; - .btn { - outline: none; - border: none; - cursor: pointer; - background-color: #5b3f45; - padding: 1em 1.5em; - color: #f5f6fa; - border-radius: 15px; - transition: ease-in-out 0.2s; - &:hover { - transform: scale(1.2); - } - &:disabled { - opacity: 0.5; - &:hover { - cursor: not-allowed; - transform: scale(1); - } - } + align-items: center; + padding-inline: 1rem; + nwui-button { + width: 100%; } } diff --git a/src/app/components/translate_block/translate_block.component.ts b/src/app/components/translate_block/translate_block.component.ts index ee51def..f322cfe 100644 --- a/src/app/components/translate_block/translate_block.component.ts +++ b/src/app/components/translate_block/translate_block.component.ts @@ -1,6 +1,6 @@ import { CommonModule } from '@angular/common'; -import { AfterViewInit, Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core'; -import { ButtonComponent } from '@nwaifu-ui'; +import { Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core'; +import { NWUIButtonComponent, NWUITextAreaComponent } from '@nwaifu-ui'; import { TranslateData } from '../../dto/translate_data.dto'; import { ETranslateService } from '../../services/translate.enums'; import { TranslateService } from '../../services/translate.service'; @@ -10,13 +10,14 @@ import { TranslateService } from '../../services/translate.service'; templateUrl: './translate_block.component.html', styleUrls: ['./translate_block.component.scss'], standalone: true, - imports: [CommonModule, ButtonComponent], + imports: [CommonModule, NWUIButtonComponent, NWUITextAreaComponent], providers: [TranslateService], }) -export class TranslateBlockComponent implements OnInit, AfterViewInit { +export class TranslateBlockComponent implements OnInit { @ViewChild('translatedText') translatedText: ElementRef | null = null; - @Input() item: TranslateData = { english_text: '', translated_text: '' }; - @Input() index = 0; + @Input({ required: true }) item: TranslateData = { english_text: '', translated_text: '' }; + @Input({ required: true }) index = 0; + translateLoading = false; isEditing = false; saveChanges() { @@ -34,11 +35,13 @@ export class TranslateBlockComponent implements OnInit, AfterViewInit { } constructor(private translateService: TranslateService) {} + private sendToTranslate(service: ETranslateService = ETranslateService.GOOGLE) { + this.translateLoading = true; this.translateService.translate(this.item.english_text, service).subscribe((text) => { this.item.translated_text = text; - if (this.translatedText) this.translatedText.nativeElement.textContent = text; this.isEditing = false; + this.translateLoading = false; this.saveChanges(); }); } @@ -55,23 +58,14 @@ export class TranslateBlockComponent implements OnInit, AfterViewInit { this.sendToTranslate(ETranslateService.PROMPT); } - saveTranslate() { + saveTranslate(text: string) { this.isEditing = false; - if (this.translatedText) { - this.item.translated_text = this.translatedText.nativeElement.textContent || ''; - this.translatedText.nativeElement.textContent = ''; - this.translatedText.nativeElement.textContent = this.item.translated_text; - } + this.item.translated_text = text; this.saveChanges(); } clear() { this.item.translated_text = ''; - if (this.translatedText) this.translatedText.nativeElement.textContent = ''; this.isEditing = true; this.saveChanges(); } - - ngAfterViewInit(): void { - if (this.translatedText) this.translatedText.nativeElement.textContent = this.item.translated_text; - } } diff --git a/src/app/pipes/translate.pipe.ts b/src/app/pipes/translate.pipe.ts deleted file mode 100644 index e4400b3..0000000 --- a/src/app/pipes/translate.pipe.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Pipe, PipeTransform } from '@angular/core'; -import { TranslateService } from '../services/translate.service'; - -@Pipe({ name: 'translate', standalone: true, pure: false }) -export class TranslatePipe implements PipeTransform { - constructor(private translateService: TranslateService) {} - transform(text: string) { - return this.translateService.translate(text); - } -}