55 */
66
77import { CodeEditor } from '@acrodata/code-editor'
8- import { HttpClient } from '@angular/common/http'
8+ import { HttpClient , HttpErrorResponse } from '@angular/common/http'
99import { Component , inject , input , linkedSignal , OnDestroy , OnInit , signal , ViewEncapsulation } from '@angular/core'
1010import { FormsModule } from '@angular/forms'
1111import { LanguageDescription } from '@codemirror/language'
@@ -41,29 +41,20 @@ export class FilesViewerTextComponent implements OnInit, OnDestroy {
4141 isReadonly = linkedSignal ( ( ) => this . mode ( ) === 'view' )
4242 isReadable = signal ( false )
4343 isModified = signal ( false )
44- protected openSearchPanel = openSearchPanel
44+ protected readonly openSearchPanel = openSearchPanel
4545 protected content : string
4646 protected currentLanguage = undefined
4747 protected readonly languages : LanguageDescription [ ] = languages
4848 protected currentTheme : any = 'light'
4949 protected readonly icons = { faFloppyDisk, faLock, faLockOpen, faMagnifyingGlass }
5050 protected readonly locale = inject < L10nLocale > ( L10N_LOCALE )
51+ private isContentReady = false
5152 private readonly layout = inject ( LayoutService )
5253 private readonly http = inject ( HttpClient )
5354 private readonly filesUpload = inject ( FilesUploadService )
5455 private subscription = this . layout . switchTheme . subscribe ( ( layout : string ) => ( this . currentTheme = layout === themeDark ? 'dark' : 'light' ) )
5556 private readonly maxSize = 5242880 // 5MB
5657
57- toggleReadonly ( ) {
58- this . isReadonly . update ( ( value ) => ! value )
59- }
60-
61- async save ( ) {
62- const file = new File ( [ new Blob ( [ this . content ] ) ] , this . file ( ) . name , { type : 'text/plain' } )
63- await this . filesUpload . addFiles ( [ file ] , true )
64- this . isModified . set ( false )
65- }
66-
6758 ngOnInit ( ) {
6859 const language : LanguageDescription = LanguageDescription . matchFilename ( languages , this . file ( ) . name )
6960 if ( language ?. name || this . file ( ) . size <= this . maxSize ) {
@@ -76,6 +67,26 @@ export class FilesViewerTextComponent implements OnInit, OnDestroy {
7667 }
7768 }
7869
70+ toggleReadonly ( ) {
71+ this . isReadonly . update ( ( value ) => ! value )
72+ }
73+
74+ save ( ) {
75+ this . filesUpload . uploadOneFile ( this . file ( ) , this . content , true ) . subscribe ( {
76+ next : ( ) => this . isModified . set ( false ) ,
77+ error : ( e : HttpErrorResponse ) => this . layout . sendNotification ( 'error' , 'Unable to save document' , e . error . message )
78+ } )
79+ }
80+
81+ contentChange ( ) {
82+ // Ignore first call
83+ if ( this . isContentReady ) {
84+ this . isModified . set ( true )
85+ } else {
86+ this . isContentReady = true
87+ }
88+ }
89+
7990 ngOnDestroy ( ) {
8091 if ( this . subscription ) {
8192 this . subscription . unsubscribe ( )
0 commit comments