Skip to content

Add Cmd+Shift+F as tidy code shortcut and prevent browser default behaviour #1732

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

Merged
merged 14 commits into from
Feb 24, 2021

Conversation

vulongphan
Copy link
Contributor

Fixes #1711

I have verified that this pull request:

  • has no linting errors (npm run lint)
  • is from a uniquely-named feature branch and has been rebased on top of the latest develop branch. (If I was asked to make more changes, I have made sure to rebase onto develop then too)
  • is descriptively named and links to an issue number, i.e. Fixes #123

@vulongphan
Copy link
Contributor Author

vulongphan commented Jan 8, 2021

@catarak what I did was to detect key short cut of Cmd+Shift+F to call tidyCode() and to prevent browser default behaviour (Full Screen Mode in Chrome on Mac). Could you please have a look and tell me if I have to change anything?

});

this._cm.on('keydown', (_cm, e) => {
// 9 === Tab
if (e.keyCode === 9 && e.shiftKey) {
this.handleKey(this.map, e);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how using this function this.handleKey is helping in this scenario? Just from reading this it seems like it's adding some intermediate code that is unnecessary, but please explain if I'm wrong!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason why I use this function is because to detect shortcut Cmd+Shift+F the following conditional statement would not work

if (e.keyCode === 91 && e.keyCode === 16 && e.keyCode === 70){ }

I guess the reason is because the browser cannot detect multiple key press. To solve this, I used an object called this.map to store the keydown state of the keyboard. For example, if the shortcut Cmd+Shift+F is pressed, this.map will be

{91: true, 16: true, 70: true}

and when we release the shortcut, this.map will be

{91: false, 16: false, 70: false}

I wonder if there is a better way to get around this.

Copy link
Member

Choose a reason for hiding this comment

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

there is! if you look at the documentation for a KeyboardEvent, you'll see that it contains metaKey, which will be true if Command is pressed, ctrlKey, which will be true if Control is pressed, and shiftKey, which will be true if Shift is pressed.

// 9 === Tab
if (e.keyCode === 9 && e.shiftKey) {
this.handleKey(this.map, e);
// 91 === Cmd
Copy link
Member

Choose a reason for hiding this comment

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

There's a variable that's imported to this file called metaKey which will be set to 'Cmd' if the user is on Mac, and 'Ctrl' if the user is on Windows/Linux. I think you should use this variable rather than just 'Cmd' since 'Cmd' doesn't exist on Windows/Linux!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I will incorporate the use of metaKey in my changes!


// delete CodeMirror.keyMap.sublime['Shift-Cmd-F'];

console.log(CodeMirror.keyMap.sublime);
Copy link
Member

Choose a reason for hiding this comment

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

Could you delete this console.log and the commented out 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.

Yes!

@vulongphan
Copy link
Contributor Author

I have made the changes accordingly, could you please have a look?

Thank you!

@vulongphan
Copy link
Contributor Author

@catarak I am sorry but does this mean that this PR should be closed now? Please correct me if I missed something here

@catarak
Copy link
Member

catarak commented Feb 2, 2021

@vulongphan I just added a comment about using KeyboardEvent.metaKey, KeyboardEvent.shiftKey, and KeyboardEvent.ctrlKey—could you use these rather than handleKey?

@vulongphan
Copy link
Contributor Author

@catarak It was my bad I did not see your comment earlier on. Also please ignore the first two commits (e53f956 and 0be0c85) and check 059b171.
I am really sorry for the inconvenience.

@catarak
Copy link
Member

catarak commented Feb 3, 2021

@catarak It was my bad I did not see your comment earlier on. Also please ignore the first two commits (e53f956 and 0be0c85) and check 059b171.
I am really sorry for the inconvenience.

No need to apologize!!! Contributing is about learning rather than being perfect 😄

// 9 === Tab
if (e.keyCode === 9 && e.shiftKey) {
// 70 === f
if ((e.metaKey || e.ctrlKey) && e.shiftKey && e.keyCode === 70) {
Copy link
Member

Choose a reason for hiding this comment

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

i think it would also make sense to check the value of metaKey, so that Ctrl + Shift + F doesn't work on Mac.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I have added the check accordingly!

@catarak
Copy link
Member

catarak commented Feb 16, 2021

Working great for me! Would you mind updating the KeyboardShortcutModal component with the new shortcut? Thank you!

@vulongphan
Copy link
Contributor Author

Hi @catarak, I have updated the file but for some reason I cannot see the change in my local development. Could you please check for me? Attached is the local development after I updated the file
Screen Shot 2021-02-18 at 5 13 10 PM

@catarak
Copy link
Member

catarak commented Feb 19, 2021

I'm seeing the same thing! I think the issue is that you need to update the file Nav.jsx as well.

@vulongphan
Copy link
Contributor Author

vulongphan commented Feb 20, 2021

@catarak yes I really do agree that we have to update Nav.jsx as well. I have updated the file and now see the changes in my local development.
However, npm run test has failed which does not seem to be caused by the changes I have made. Could you please give me some suggestion?

Thank you!

@catarak
Copy link
Member

catarak commented Feb 24, 2021

Because you're changing the appearance of Nav.jsx, you need to update the snapshot as well: npm run test -- --updateSnapshot. I'm going to go ahead and just do this so I can merge this fix in.

@catarak catarak merged commit 0e917dd into processing:develop Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change keyboard shortcut for tidy code
2 participants