-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add Cmd+Shift+F as tidy code shortcut and prevent browser default behaviour #1732
Conversation
@catarak what I did was to detect key short cut of Cmd+Shift+F to call |
}); | ||
|
||
this._cm.on('keydown', (_cm, e) => { | ||
// 9 === Tab | ||
if (e.keyCode === 9 && e.shiftKey) { | ||
this.handleKey(this.map, e); |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes!
I have made the changes accordingly, could you please have a look? Thank you! |
@catarak I am sorry but does this mean that this PR should be closed now? Please correct me if I missed something here |
@vulongphan I just added a comment about using |
// 9 === Tab | ||
if (e.keyCode === 9 && e.shiftKey) { | ||
// 70 === f | ||
if ((e.metaKey || e.ctrlKey) && e.shiftKey && e.keyCode === 70) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
Working great for me! Would you mind updating the KeyboardShortcutModal component with the new shortcut? Thank you! |
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 |
I'm seeing the same thing! I think the issue is that you need to update the file Nav.jsx as well. |
…-shortcut Merge upstream/develop
…phan/p5.js-web-editor into update/tidy-code-shortcut Pulled from origin update/tidy-code-shortcut
@catarak yes I really do agree that we have to update Thank you! |
Because you're changing the appearance of |
Fixes #1711
I have verified that this pull request:
npm run lint
)develop
branch. (If I was asked to make more changes, I have made sure to rebase ontodevelop
then too)Fixes #123