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

Conversation

cedricongjh
Copy link
Contributor

@cedricongjh cedricongjh commented Jul 11, 2022

Summary:

Fixes #823

Changes Made:

  • Add keyboard shortcuts (ctrl+B and ctrl+I) to comment editor
CATcher.3.4.2.-.CATcher-org_undefined.-.Google.Chrome.2022-07-12.03-26-14_Trim.mp4

Proposed Commit Message:

Add keyboard shortcuts to comment editor.

Currently, our comment editor does not support keyboard
shortcuts such as ctrl + b to bold, and ctrl + i to italicize. 

Let's improve the UX of the comment editor by adding this
functionality.

@codecov-commenter
Copy link

codecov-commenter commented Jul 15, 2022

Codecov Report

Base: 62.31% // Head: 62.26% // Decreases project coverage by -0.05% ⚠️

Coverage data is based on head (6d6a53e) compared to base (d85e0c6).
Patch has no changes to coverable lines.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #965      +/-   ##
==========================================
- Coverage   62.31%   62.26%   -0.06%     
==========================================
  Files          86       86              
  Lines        1937     1937              
  Branches      215      215              
==========================================
- Hits         1207     1206       -1     
  Misses        695      695              
- Partials       35       36       +1     
Impacted Files Coverage Δ
src/app/shared/issue-tables/issue-sorter.ts 43.47% <0.00%> (-4.35%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@gycgabriel gycgabriel left a comment

Choose a reason for hiding this comment

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

Good variable names and readability, but doing too much in one method. Would be better to break up methods for readability and maintainability

const highlightedText = currentText.slice(selectionStart, selectionEnd);
const hasInsertedCharBefore = currentText.slice(selectionStart - insertedChar.length, selectionStart) === insertedChar;
const hasInsertedCharAfter = currentText.slice(selectionEnd, selectionEnd + insertedChar.length) === insertedChar;
if (hasInsertedCharBefore && hasInsertedCharAfter) {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a toggling being done here, this can be indicated in the private method name or in a comment. (Toggle bold/italics)

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point @gycgabriel ,to add on I think another suggestion is to extract out this condition into another method/boolean variable e.g. 'alreadyFormatted' (Please go ahead and use another name if you prefer, I can't think of a good one 😅)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestions @gycgabriel and @kkangs0226, i've abstracted this out into a private method

Comment on lines 104 to 112
this.commentField.setValue(
currentText.slice(0, selectionStart) +
SPACE.repeat(spacesRemovedLeft) +
insertedChar +
highlightedTextTrimmed +
insertedChar +
SPACE.repeat(spacesRemovedRight) +
currentText.slice(selectionEnd)
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks readable, I like that variable names make sense

@gycgabriel
Copy link
Contributor

Please also rename your PRs with Caps in front, and ensure that your commit message meets the requirements. (e.g. full stop at end of line)

@@ -69,6 +68,56 @@ export class CommentEditorComponent implements OnInit {
this.commentField.setValidators([Validators.maxLength(this.maxLength)]);
}

onKeyPress(event) {
if (event.ctrlKey) {
Copy link
Contributor

@chunweii chunweii Jul 23, 2022

Choose a reason for hiding this comment

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

Normally, mac users will want to use the command key instead of ctrl. I think we should check for the user's OS and handle the ⌘+b or ⌘+i shortcuts.

I personally don't use a Mac so other developers can comment on this too.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good point, thanks for pointing it out @chunweii . Seems like we can use metaKey to handle the key, you might want to include this in your code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, @chunweii, i've included it in the code, detecting either metaKey or ctrlKey press, as OS detection seems more complicated than I expected. I think it should be ok as metaKey in windows refers to the windows key, and even so, testing on Chrome and Firefox on my windows machine, the browsers did not detect the metaKey press. It seems that its been depreciated on Firefox at least (metaKey is no longer supported on windows):

image
From https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/metaKey

Copy link
Contributor

@kkangs0226 kkangs0226 left a comment

Choose a reason for hiding this comment

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

Thanks for your work, I have left a few comments 😀

@@ -69,6 +68,56 @@ export class CommentEditorComponent implements OnInit {
this.commentField.setValidators([Validators.maxLength(this.maxLength)]);
}

onKeyPress(event) {
if (event.ctrlKey) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good point, thanks for pointing it out @chunweii . Seems like we can use metaKey to handle the key, you might want to include this in your code.

const highlightedText = currentText.slice(selectionStart, selectionEnd);
const hasInsertedCharBefore = currentText.slice(selectionStart - insertedChar.length, selectionStart) === insertedChar;
const hasInsertedCharAfter = currentText.slice(selectionEnd, selectionEnd + insertedChar.length) === insertedChar;
if (hasInsertedCharBefore && hasInsertedCharAfter) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good point @gycgabriel ,to add on I think another suggestion is to extract out this condition into another method/boolean variable e.g. 'alreadyFormatted' (Please go ahead and use another name if you prefer, I can't think of a good one 😅)

return;
}
const currentText = this.commentField.value;
const highlightedText = currentText.slice(selectionStart, selectionEnd);
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like we are not considering trailing/leading spaces here, could we check for spaces here as well for checking whether it is already formatted? e.g. highlighted text' example ' with space before and after will not be considered 'formatted' in this case.

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 good point @kkangs0226, i've added a private method to detect if the trimmed highlighted text is already formatted, and another method to handle that case

@cedricongjh cedricongjh force-pushed the add-keyboard-shortcuts branch 4 times, most recently from 3852580 to 6b41416 Compare July 31, 2022 17:38
@cedricongjh cedricongjh changed the title add keyboard shortcuts Add keyboard shortcuts Jul 31, 2022
@cedricongjh
Copy link
Contributor Author

hi @gycgabriel, @kkangs0226, @chunweii I've refactored this PR into smaller methods, as suggested, do take a look again.
I am thinking of adding unit tests for these methods after this PR is merged, do let me know if its ok, thank you!

Copy link
Contributor

@gycgabriel gycgabriel left a comment

Choose a reason for hiding this comment

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

Methods shortened, and naming const are good making code very readable. Almost LGTM:

Copy link
Contributor

@gycgabriel gycgabriel left a comment

Choose a reason for hiding this comment

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

LGTM

@cedricongjh
Copy link
Contributor Author

hi @chunweii, i've added in the OS detection and tested it on my mac, with the help of these docs:
image
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform

Copy link
Contributor

@kkangs0226 kkangs0226 left a comment

Choose a reason for hiding this comment

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

The issue I have pointed out seems to have been fixed, thanks for your work 👍 after @chunweii approves of the changes, we can proceed to merge.

Copy link
Contributor

@chunweii chunweii left a comment

Choose a reason for hiding this comment

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

Other than the small nitpick, LGTM 👍

There are some minor issues, but I don't think it is a big problem. For example, pressing ctrl+b followed by ctrl+i and ctrl+b will create **_**text**_**. Also, trying to bold or italicize text across multiple lines may not work. But the github comment editor also has the same issues, and it is unlikely to be an inconvenience to students.

@@ -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!

@kkangs0226
Copy link
Contributor

There are some minor issues, but I don't think it is a big problem. For example, pressing ctrl+b followed by ctrl+i and ctrl+b will create **_**text**_**.

Good point raised, this seems like something we can work on. For example, detecting whether there are repeated characters within the highlighted text, rather than just the two ends of the string.

@kkangs0226
Copy link
Contributor

@cedricongjh Do you think you can work on these changes in this PR? If not, we can create another issue for this and go ahead with merging this PR first as it is not critical at the moment.

@cedricongjh
Copy link
Contributor Author

@cedricongjh Do you think you can work on these changes in this PR? If not, we can create another issue for this and go ahead with merging this PR first as it is not critical at the moment.

hi @kkangs0226, I think we should create another issue for this and go ahead with merging this, thanks

@gycgabriel gycgabriel merged commit 5c54fa9 into CATcher-org:master Oct 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to use keyboard shortcuts in editor
6 participants