Skip to content

Commit a366d56

Browse files
committed
feat(snippets): remove leading whitespaces when code comes via url (bookmarklet or extensions)
1 parent 37a3db9 commit a366d56

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

frontend/src/app/my-snippets/create-snippet-form/create-snippet-form.component.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core';
22
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
3-
import { Observable } from 'rxjs';
43
import { tagsValidator } from '../../shared/directive/tags-validation.directive';
54
import { UserDataStore } from '../../core/user/userdata.store';
65
import { Logger } from '../../core/logger.service';
@@ -110,7 +109,7 @@ export class CreateSnippetFormComponent extends SnippetFormBaseComponent impleme
110109

111110
buildInitialForm(): void {
112111
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],
114113
tags: this.formBuilder.array([], [tagsValidator, Validators.required]),
115114
codeSnippets: new FormArray([this.createInitialCodeSnippet()]),
116115
sourceUrl: this.sourceUrl ? this.sourceUrl : '',
@@ -120,6 +119,10 @@ export class CreateSnippetFormComponent extends SnippetFormBaseComponent impleme
120119
}
121120

122121
createInitialCodeSnippet(): FormGroup {
122+
if (this.code) {
123+
this.code = this.removeLeadingWhitespaces(this.code);
124+
}
125+
123126
return this.formBuilder.group({
124127
code: [this.code ? this.ext === 'vscode' ? this.decodeTextVsCode(this.code) : this.code : '', textSizeValidator(5000, 500)],
125128
comment: [this.comment ? this.comment : '', textSizeValidator(1000, 30)]
@@ -129,10 +132,28 @@ export class CreateSnippetFormComponent extends SnippetFormBaseComponent impleme
129132
decodeTextVsCode(text: string): string {
130133
let response = text.replace(/ampc;/gi, '&');
131134
response = response.replace(/qmc;/gi, '?');
135+
response = response.replace(/hashc;/gi, '#');
132136

133137
return response;
134138
}
135139

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+
}
136158
}
137159

138-

0 commit comments

Comments
 (0)