1
1
import { Component , ElementRef , Input , OnInit , ViewChild } from '@angular/core' ;
2
2
import { FormArray , FormBuilder , FormGroup , Validators } from '@angular/forms' ;
3
- import { Observable } from 'rxjs' ;
4
3
import { tagsValidator } from '../../shared/directive/tags-validation.directive' ;
5
4
import { UserDataStore } from '../../core/user/userdata.store' ;
6
5
import { Logger } from '../../core/logger.service' ;
@@ -110,7 +109,7 @@ export class CreateSnippetFormComponent extends SnippetFormBaseComponent impleme
110
109
111
110
buildInitialForm ( ) : void {
112
111
this . snippetFormGroup = this . formBuilder . group ( {
113
- title : [ this . title ? this . ext === 'vscode' ? this . decodeTextVsCode ( this . title ) : this . title : '' , Validators . required ] ,
112
+ title : [ this . title ? this . ext === 'vscode' ? this . decodeTextVsCode ( this . title ) : this . title : '' , Validators . required ] ,
114
113
tags : this . formBuilder . array ( [ ] , [ tagsValidator , Validators . required ] ) ,
115
114
codeSnippets : new FormArray ( [ this . createInitialCodeSnippet ( ) ] ) ,
116
115
sourceUrl : this . sourceUrl ? this . sourceUrl : '' ,
@@ -120,6 +119,10 @@ export class CreateSnippetFormComponent extends SnippetFormBaseComponent impleme
120
119
}
121
120
122
121
createInitialCodeSnippet ( ) : FormGroup {
122
+ if ( this . code ) {
123
+ this . code = this . removeLeadingWhitespaces ( this . code ) ;
124
+ }
125
+
123
126
return this . formBuilder . group ( {
124
127
code : [ this . code ? this . ext === 'vscode' ? this . decodeTextVsCode ( this . code ) : this . code : '' , textSizeValidator ( 5000 , 500 ) ] ,
125
128
comment : [ this . comment ? this . comment : '' , textSizeValidator ( 1000 , 30 ) ]
@@ -129,10 +132,28 @@ export class CreateSnippetFormComponent extends SnippetFormBaseComponent impleme
129
132
decodeTextVsCode ( text : string ) : string {
130
133
let response = text . replace ( / a m p c ; / gi, '&' ) ;
131
134
response = response . replace ( / q m c ; / gi, '?' ) ;
135
+ response = response . replace ( / h a s h c ; / gi, '#' ) ;
132
136
133
137
return response ;
134
138
}
135
139
140
+ /**
141
+ * Remove leading whitespaces - max number removed is given by the number of white spaces of the first line
142
+ */
143
+ removeLeadingWhitespaces ( code : string ) : string {
144
+ const LINE_EXPRESSION = / \r \n | \n \r | \n | \r / g; // expression symbols order is very important
145
+ const lines = code . split ( LINE_EXPRESSION ) ;
146
+ const firstLineNumberSpaces = lines [ 0 ] . length - lines [ 0 ] . trimLeft ( ) . length ;
147
+ if ( firstLineNumberSpaces > 0 ) {
148
+ let response = '' ;
149
+ for ( let i = 0 ; i < lines . length - 1 ; i ++ ) {
150
+ response += lines [ i ] . substring ( firstLineNumberSpaces , lines [ i ] . length ) . trimRight ( ) + '\r\n' ;
151
+ }
152
+ response += lines [ lines . length - 1 ] . substring ( firstLineNumberSpaces , lines [ lines . length - 1 ] . length ) . trimRight ( ) ;
153
+ return response ;
154
+ } else {
155
+ return code ;
156
+ }
157
+ }
136
158
}
137
159
138
-
0 commit comments