|
6 | 6 |
|
7 | 7 | import { CodeEditor } from '@acrodata/code-editor' |
8 | 8 | import { HttpClient } from '@angular/common/http' |
9 | | -import { Component, inject, input, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core' |
| 9 | +import { Component, inject, input, OnDestroy, OnInit, signal, ViewEncapsulation } from '@angular/core' |
10 | 10 | import { FormsModule } from '@angular/forms' |
11 | 11 | import { LanguageDescription } from '@codemirror/language' |
12 | 12 | import { languages } from '@codemirror/language-data' |
| 13 | +import { openSearchPanel } from '@codemirror/search' |
| 14 | +import { FaIconComponent } from '@fortawesome/angular-fontawesome' |
| 15 | +import { faFloppyDisk, faLock, faLockOpen, faMagnifyingGlass } from '@fortawesome/free-solid-svg-icons' |
| 16 | +import { L10N_LOCALE, L10nLocale, L10nTranslatePipe } from 'angular-l10n' |
| 17 | +import { TooltipModule } from 'ngx-bootstrap/tooltip' |
13 | 18 | import { themeDark } from '../../../../layout/layout.interfaces' |
14 | 19 | import { LayoutService } from '../../../../layout/layout.service' |
15 | 20 | import { FileModel } from '../../models/file.model' |
| 21 | +import { FilesUploadService } from '../../services/files-upload.service' |
16 | 22 |
|
17 | 23 | @Component({ |
18 | 24 | selector: 'app-files-viewer-text', |
19 | 25 | encapsulation: ViewEncapsulation.None, |
20 | | - imports: [CodeEditor, FormsModule], |
| 26 | + imports: [CodeEditor, TooltipModule, FormsModule, FaIconComponent, L10nTranslatePipe], |
21 | 27 | styles: [ |
22 | 28 | ` |
23 | 29 | .code-editor { |
24 | | - height: 100%; |
| 30 | + height: calc(100% - 40px); |
25 | 31 | font-size: 0.75rem; |
26 | 32 | } |
27 | 33 | ` |
28 | 34 | ], |
29 | | - template: ` <div [style.height.px]="currentHeight()"> |
30 | | - <code-editor |
31 | | - [languages]="languages" |
32 | | - [language]="currentLanguage" |
33 | | - [ngModel]="content" |
34 | | - [theme]="currentTheme" |
35 | | - [readonly]="true" |
36 | | - [lineWrapping]="true" |
37 | | - ></code-editor> |
38 | | - </div>` |
| 35 | + templateUrl: 'files-viewer-text.component.html' |
39 | 36 | }) |
40 | 37 | export class FilesViewerTextComponent implements OnInit, OnDestroy { |
41 | 38 | currentHeight = input<number>() |
42 | 39 | file = input<FileModel>() |
| 40 | + isReadonly = signal(true) |
| 41 | + isReadable = signal(false) |
| 42 | + isModified = signal(false) |
| 43 | + protected openSearchPanel = openSearchPanel |
43 | 44 | protected content: string |
44 | 45 | protected currentLanguage = undefined |
45 | 46 | protected readonly languages: LanguageDescription[] = languages |
46 | 47 | protected currentTheme: any = 'light' |
| 48 | + protected readonly icons = { faFloppyDisk, faLock, faLockOpen, faMagnifyingGlass } |
| 49 | + protected readonly locale = inject<L10nLocale>(L10N_LOCALE) |
47 | 50 | private readonly layout = inject(LayoutService) |
48 | 51 | private readonly http = inject(HttpClient) |
| 52 | + private readonly filesUpload = inject(FilesUploadService) |
49 | 53 | private subscription = this.layout.switchTheme.subscribe((layout: string) => (this.currentTheme = layout === themeDark ? 'dark' : 'light')) |
50 | 54 | private readonly maxSize = 5242880 // 5MB |
51 | 55 |
|
| 56 | + toggleReadonly() { |
| 57 | + this.isReadonly.update((value) => !value) |
| 58 | + } |
| 59 | + |
| 60 | + async save() { |
| 61 | + const file = new File([new Blob([this.content])], this.file().name, { type: 'text/plain' }) |
| 62 | + await this.filesUpload.addFiles([file], true) |
| 63 | + this.isModified.set(false) |
| 64 | + } |
| 65 | + |
52 | 66 | ngOnInit() { |
53 | 67 | const language: LanguageDescription = LanguageDescription.matchFilename(languages, this.file().name) |
54 | 68 | if (language?.name || this.file().size <= this.maxSize) { |
55 | 69 | this.currentLanguage = language?.name |
| 70 | + this.isReadable.set(true) |
56 | 71 | this.http.get(this.file().dataUrl, { responseType: 'text' }).subscribe((data: string) => (this.content = data)) |
57 | 72 | } else { |
| 73 | + this.isReadable.set(false) |
58 | 74 | this.content = this.layout.translateString('This file contains binary data that can not be read') |
59 | 75 | } |
60 | 76 | } |
|
0 commit comments