Skip to content

Commit

Permalink
Refactor code for inserting upload text (#982)
Browse files Browse the repository at this point in the history
Currently, the comment-editor contains code that handle
the insertion of upload text into the comment input.

Let's apply separation of concerns and move these functions
to a separate typescript module.
  • Loading branch information
snss231 authored Sep 19, 2022
1 parent 2dab84a commit a2529c2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 64 deletions.
68 changes: 4 additions & 64 deletions src/app/shared/comment-editor/comment-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import * as DOMPurify from 'dompurify';
import { ErrorHandlingService } from '../../core/services/error-handling.service';
import { LoggingService } from '../../core/services/logging.service';
import { FILE_TYPE_SUPPORT_ERROR, getSizeExceedErrorMsg, SUPPORTED_FILE_TYPES, UploadService } from '../../core/services/upload.service';
import { insertUploadingText, insertUploadUrl, insertUploadUrlVideo } from './upload-text-insertor';

const DISPLAYABLE_CONTENT = ['gif', 'jpeg', 'jpg', 'png'];
const BYTES_PER_MB = 1024 * 1024;
const SHOWN_MAX_UPLOAD_SIZE_MB = 10;
const SHOWN_MAX_VIDEO_UPLOAD_SIZE_MB = 5;
Expand Down Expand Up @@ -148,7 +148,7 @@ export class CommentEditorComponent implements OnInit {
this.uploadErrorMessage = null;
const reader = new FileReader();
const filename = file.name;
const insertedText = this.insertUploadingText(filename);
const insertedText = insertUploadingText(filename, this.commentField, this.commentTextArea);

if (file.size >= MAX_UPLOAD_SIZE) {
this.handleUploadError(getSizeExceedErrorMsg('file', SHOWN_MAX_UPLOAD_SIZE_MB), insertedText);
Expand Down Expand Up @@ -176,9 +176,9 @@ export class CommentEditorComponent implements OnInit {
this.uploadService.uploadFile(reader.result, filename).subscribe(
(response) => {
if (this.uploadService.isVideoFile(filename)) {
this.insertUploadUrlVideo(filename, response.data.content.download_url);
insertUploadUrlVideo(filename, response.data.content.download_url, this.commentField, this.commentTextArea);
} else {
this.insertUploadUrl(filename, response.data.content.download_url);
insertUploadUrl(filename, response.data.content.download_url, this.commentField, this.commentTextArea);
}
},
(error) => {
Expand Down Expand Up @@ -227,66 +227,6 @@ export class CommentEditorComponent implements OnInit {
this.commentField.setValue(this.commentField.value.replace(insertedText, ''));
}

private insertUploadingText(filename: string): string {
const originalDescription = this.commentField.value;

const fileType = filename.split('.').pop();
let toInsert: string;
if (DISPLAYABLE_CONTENT.includes(fileType.toLowerCase())) {
toInsert = `![Uploading ${filename}...]\n`;
} else {
toInsert = `[Uploading ${filename}...]\n`;
}

const cursorPosition = this.commentTextArea.nativeElement.selectionEnd;
const endOfLineIndex = originalDescription.indexOf('\n', cursorPosition);
const nextCursorPosition = cursorPosition + toInsert.length;

if (endOfLineIndex === -1) {
if (this.commentField.value === '') {
this.commentField.setValue(toInsert);
} else {
this.commentField.setValue(`${this.commentField.value}\n${toInsert}`);
}
} else {
const startTillNewline = originalDescription.slice(0, endOfLineIndex + 1);
const newlineTillEnd = originalDescription.slice(endOfLineIndex);
this.commentField.setValue(`${startTillNewline + toInsert + newlineTillEnd}`);
}

this.commentTextArea.nativeElement.setSelectionRange(nextCursorPosition, nextCursorPosition);
return toInsert;
}

private replacePlaceholderString(filename: string, insertedString: string) {
const cursorPosition = this.commentTextArea.nativeElement.selectionEnd;
const insertingString = `[Uploading ${filename}...]`;
const startIndexOfString = this.commentField.value.indexOf(insertingString);
const endIndexOfString = startIndexOfString + insertingString.length;
const endOfInsertedString = startIndexOfString + insertedString.length;
const differenceInLength = endOfInsertedString - endIndexOfString;
const newCursorPosition =
cursorPosition > startIndexOfString - 1 && cursorPosition <= endIndexOfString // within the range of uploading text
? endOfInsertedString
: cursorPosition < startIndexOfString // before the uploading text
? cursorPosition
: cursorPosition + differenceInLength; // after the uploading text

this.commentField.setValue(this.commentField.value.replace(insertingString, insertedString));
this.commentTextArea.nativeElement.setSelectionRange(newCursorPosition, newCursorPosition);
}

private insertUploadUrlVideo(filename: string, uploadUrl: string) {
const insertedString = `<i><video controls><source src="${uploadUrl}" type="video/mp4">Your browser does not support the video tag.</video><br>video:${uploadUrl}</i>`;

this.replacePlaceholderString(filename, insertedString);
}

private insertUploadUrl(filename: string, uploadUrl: string) {
const insertedString = `[${filename}](${uploadUrl})`;
this.replacePlaceholderString(filename, insertedString);
}

private removeHighlightBorderStyle() {
this.dragActiveCounter--;
if (this.dragActiveCounter === 0) {
Expand Down
83 changes: 83 additions & 0 deletions src/app/shared/comment-editor/upload-text-insertor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { ElementRef } from '@angular/core';
import { AbstractControl } from '@angular/forms';

const DISPLAYABLE_CONTENT = ['gif', 'jpeg', 'jpg', 'png'];

export function insertUploadingText(
filename: string,
commentField: AbstractControl,
commentTextArea: ElementRef<HTMLTextAreaElement>
): string {
const originalDescription = commentField.value;

const fileType = filename.split('.').pop();
let toInsert: string;
if (DISPLAYABLE_CONTENT.includes(fileType.toLowerCase())) {
toInsert = `![Uploading ${filename}...]\n`;
} else {
toInsert = `[Uploading ${filename}...]\n`;
}

const cursorPosition = commentTextArea.nativeElement.selectionEnd;
const endOfLineIndex = originalDescription.indexOf('\n', cursorPosition);
const nextCursorPosition = cursorPosition + toInsert.length;

if (endOfLineIndex === -1) {
if (commentField.value === '') {
commentField.setValue(toInsert);
} else {
commentField.setValue(`${commentField.value}\n${toInsert}`);
}
} else {
const startTillNewline = originalDescription.slice(0, endOfLineIndex + 1);
const newlineTillEnd = originalDescription.slice(endOfLineIndex);
commentField.setValue(`${startTillNewline + toInsert + newlineTillEnd}`);
}

commentTextArea.nativeElement.setSelectionRange(nextCursorPosition, nextCursorPosition);
return toInsert;
}

export function insertUploadUrlVideo(
filename: string,
uploadUrl: string,
commentField: AbstractControl,
commentTextArea: ElementRef<HTMLTextAreaElement>
) {
const insertedString = `<i><video controls><source src="${uploadUrl}" type="video/mp4">Your browser does not support the video tag.</video><br>video:${uploadUrl}</i>`;

replacePlaceholderString(filename, insertedString, commentField, commentTextArea);
}

export function insertUploadUrl(
filename: string,
uploadUrl: string,
commentField: AbstractControl,
commentTextArea: ElementRef<HTMLTextAreaElement>
) {
const insertedString = `[${filename}](${uploadUrl})`;
replacePlaceholderString(filename, insertedString, commentField, commentTextArea);
}

function replacePlaceholderString(
filename: string,
insertedString: string,
commentField: AbstractControl,
commentTextArea: ElementRef<HTMLTextAreaElement>
) {
const cursorPosition = this.commentTextArea.nativeElement.selectionEnd;
const insertingString = `[Uploading ${filename}...]`;
const startIndexOfString = this.commentField.value.indexOf(insertingString);
const endIndexOfString = startIndexOfString + insertingString.length;
const endOfInsertedString = startIndexOfString + insertedString.length;
const differenceInLength = endOfInsertedString - endIndexOfString;
const newCursorPosition =
cursorPosition > startIndexOfString - 1 && cursorPosition <= endIndexOfString // within the range of uploading text
? endOfInsertedString
: cursorPosition < startIndexOfString // before the uploading text
? cursorPosition
: cursorPosition + differenceInLength; // after the uploading text

commentField.setValue(this.commentField.value.replace(insertingString, insertedString));
commentTextArea.nativeElement.setSelectionRange(newCursorPosition, newCursorPosition);
}

0 comments on commit a2529c2

Please sign in to comment.