-
-
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
Changes from 1 commit
ecefaa8
25f3ffa
f801d1f
0ef3880
e53f956
0be0c85
059b171
afec089
7005999
e3ea72a
38db3b0
11f27f9
1cb1aa6
3cef359
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,11 @@ const beautifyHTML = beautifyJS.html; | |
window.JSHINT = JSHINT; | ||
window.CSSLint = CSSLint; | ||
window.HTMLHint = HTMLHint; | ||
delete CodeMirror.keyMap.sublime['Shift-Tab']; | ||
|
||
|
||
// delete CodeMirror.keyMap.sublime['Shift-Cmd-F']; | ||
|
||
console.log(CodeMirror.keyMap.sublime); | ||
|
||
const IS_TAB_INDENT = false; | ||
const INDENTATION_AMOUNT = 2; | ||
|
@@ -88,6 +92,7 @@ class Editor extends React.Component { | |
this.findPrev = this.findPrev.bind(this); | ||
this.showReplace = this.showReplace.bind(this); | ||
this.getContent = this.getContent.bind(this); | ||
this.handleKey = this.handleKey.bind(this); | ||
} | ||
|
||
componentDidMount() { | ||
|
@@ -155,14 +160,21 @@ class Editor extends React.Component { | |
} | ||
}, 1000)); | ||
|
||
this._cm.on('keyup', () => { | ||
this.map = {}; | ||
|
||
this._cm.on('keyup', (_cm, e) => { | ||
const temp = this.props.t('Editor.KeyUpLineNumber', { lineNumber: parseInt((this._cm.getCursor().line) + 1, 10) }); | ||
document.getElementById('current-line').innerHTML = temp; | ||
this.handleKey(this.map, e); | ||
}); | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how using this function There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
I guess the reason is because the browser cannot detect multiple key press. To solve this, I used an object called
and when we release the shortcut,
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 commentThe 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 |
||
// 91 === Cmd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a variable that's imported to this file called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I will incorporate the use of |
||
// 16 === Shift | ||
// 70 === f | ||
if (this.map[91] && this.map[16] && this.map[70]) { | ||
e.preventDefault(); // prevent browser's default behaviour | ||
this.tidyCode(); | ||
} | ||
}); | ||
|
@@ -192,7 +204,7 @@ class Editor extends React.Component { | |
|
||
componentDidUpdate(prevProps) { | ||
if (this.props.file.content !== prevProps.file.content && | ||
this.props.file.content !== this._cm.getValue()) { | ||
this.props.file.content !== this._cm.getValue()) { | ||
const oldDoc = this._cm.swapDoc(this._docs[this.props.file.id]); | ||
this._docs[prevProps.file.id] = oldDoc; | ||
this._cm.focus(); | ||
|
@@ -327,6 +339,10 @@ class Editor extends React.Component { | |
} | ||
} | ||
|
||
handleKey(map, e) { // update the state of each key pressed and released | ||
map[e.keyCode] = e.type === 'keydown'; | ||
} | ||
|
||
render() { | ||
const editorSectionClass = classNames({ | ||
'editor': true, | ||
|
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!