Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard shortcuts #965

Merged
merged 11 commits into from
Oct 7, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<mat-label></mat-label>
<textarea
(paste)="onPaste($event)"
(keydown)="onKeyPress($event)"
#commentTextArea
(dragover)="disableCaretMovement($event)"
id="{{ this.id }}"
Expand Down
129 changes: 127 additions & 2 deletions src/app/shared/comment-editor/comment-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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';
const os = require('os');
Copy link
Contributor

@chunweii chunweii Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this line since we are not using node js os module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah my bad, removed!


const DISPLAYABLE_CONTENT = ['gif', 'jpeg', 'jpg', 'png'];
const BYTES_PER_MB = 1024 * 1024;
Expand All @@ -16,6 +17,8 @@ const MAX_UPLOAD_SIZE = (SHOWN_MAX_UPLOAD_SIZE_MB + 1) * BYTES_PER_MB; // 11MB t
const MAX_VIDEO_UPLOAD_SIZE = (SHOWN_MAX_VIDEO_UPLOAD_SIZE_MB + 1) * BYTES_PER_MB; // 6MB to allow 5.x MB
const ISSUE_BODY_SIZE_LIMIT = 40000;

const SPACE = ' ';

@Component({
selector: 'app-comment-editor',
templateUrl: './comment-editor.component.html',
Expand Down Expand Up @@ -47,7 +50,6 @@ export class CommentEditorComponent implements OnInit {
@ViewChild('dropArea', { static: true }) dropArea;
@ViewChild('commentTextArea', { static: true }) commentTextArea;
@ViewChild('markdownArea', { static: false }) markdownArea;

dragActiveCounter = 0;
uploadErrorMessage: string;
maxLength = ISSUE_BODY_SIZE_LIMIT;
Expand All @@ -69,6 +71,23 @@ export class CommentEditorComponent implements OnInit {
this.commentField.setValidators([Validators.maxLength(this.maxLength)]);
}

onKeyPress(event) {
if (this.isControlKeyPressed(event)) {
switch (event.key) {
case 'b':
event.preventDefault();
this.insertOrRemoveCharsFromHighlightedText('**');
break;
case 'i':
event.preventDefault();
this.insertOrRemoveCharsFromHighlightedText('_');
break;
default:
return;
}
}
}

onDragEnter(event) {
event.preventDefault();
event.stopPropagation();
Expand Down Expand Up @@ -233,7 +252,6 @@ export class CommentEditorComponent implements OnInit {
} else {
toInsert = `[Uploading ${filename}...]\n`;
}

const cursorPosition = this.commentTextArea.nativeElement.selectionEnd;
const endOfLineIndex = originalDescription.indexOf('\n', cursorPosition);
const nextCursorPosition = cursorPosition + toInsert.length;
Expand Down Expand Up @@ -280,4 +298,111 @@ export class CommentEditorComponent implements OnInit {
this.dropArea.nativeElement.classList.remove('highlight-drag-box-disabled');
}
}

private isControlKeyPressed(event) {
if (navigator.platform.indexOf('Mac') === 0) {
return event.metaKey;
}
return event.ctrlKey;
}

private insertOrRemoveCharsFromHighlightedText(char) {
const selectionStart = this.commentTextArea.nativeElement.selectionStart;
const selectionEnd = this.commentTextArea.nativeElement.selectionEnd;
const currentText = this.commentField.value;
const highlightedText = currentText.slice(selectionStart, selectionEnd);
const highlightedTextTrimmed = highlightedText.trim();
const spacesRemovedLeft = highlightedText.trimRight().length - highlightedTextTrimmed.length;
const spacesRemovedRight = highlightedText.trimLeft().length - highlightedTextTrimmed.length;

if (this.hasCharsBeforeAndAfterHighlight(selectionStart, selectionEnd, currentText, char)) {
this.removeCharsBeforeAndAfterHighlightedText(selectionStart, selectionEnd, currentText, highlightedText, char);
} else if (this.hasCharsInTrimmedHighlight(highlightedText, char)) {
this.removeCharsFromHighlightedText(
selectionStart,
selectionEnd,
currentText,
highlightedTextTrimmed,
char,
spacesRemovedLeft,
spacesRemovedRight
);
} else {
this.insertCharsToHighlightedText(
selectionStart,
selectionEnd,
currentText,
highlightedTextTrimmed,
char,
spacesRemovedLeft,
spacesRemovedRight
);
}
}

private hasCharsBeforeAndAfterHighlight(selectionStart, selectionEnd, currentText, char) {
const hasInsertedCharBefore = currentText.slice(selectionStart - char.length, selectionStart) === char;
const hasInsertedCharAfter = currentText.slice(selectionEnd, selectionEnd + char.length) === char;
return hasInsertedCharBefore && hasInsertedCharAfter;
}

private hasCharsInTrimmedHighlight(highlightedText, char) {
const highlightedTextTrimmed = highlightedText.trim();
const hasCharAtFront = highlightedTextTrimmed.slice(0, char.length) === char;
const hasCharAtEnd = highlightedTextTrimmed.slice(-char.length) === char;
return hasCharAtFront && hasCharAtEnd;
}

private removeCharsBeforeAndAfterHighlightedText(selectionStart, selectionEnd, currentText, highlightedText, char) {
this.commentField.setValue(
currentText.slice(0, selectionStart - char.length) + highlightedText + currentText.slice(selectionEnd + char.length)
);
this.commentTextArea.nativeElement.setSelectionRange(selectionStart - char.length, selectionEnd - char.length);
}

private removeCharsFromHighlightedText(
selectionStart,
selectionEnd,
currentText,
highlightedTextTrimmed,
char,
spacesRemovedLeft,
spacesRemovedRight
) {
this.commentField.setValue(
currentText.slice(0, selectionStart) +
SPACE.repeat(spacesRemovedLeft) +
highlightedTextTrimmed.slice(char.length, -char.length) +
SPACE.repeat(spacesRemovedRight) +
currentText.slice(selectionEnd)
);
this.commentTextArea.nativeElement.setSelectionRange(
selectionStart + spacesRemovedLeft,
selectionEnd - 2 * char.length - spacesRemovedRight
);
}

private insertCharsToHighlightedText(
selectionStart,
selectionEnd,
currentText,
highlightedTextTrimmed,
char,
spacesRemovedLeft,
spacesRemovedRight
) {
this.commentField.setValue(
currentText.slice(0, selectionStart) +
SPACE.repeat(spacesRemovedLeft) +
char +
highlightedTextTrimmed +
char +
SPACE.repeat(spacesRemovedRight) +
currentText.slice(selectionEnd)
);
this.commentTextArea.nativeElement.setSelectionRange(
selectionStart + char.length + spacesRemovedLeft,
selectionEnd + char.length - spacesRemovedRight
);
}
}