Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 21bea9d

Browse files
committed
refactor into separate events
1 parent 92735bf commit 21bea9d

3 files changed

Lines changed: 53 additions & 44 deletions

File tree

src/editor/CodeHintManager.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ define(function (require, exports, module) {
517517
}
518518

519519
/**
520-
* Handles keys related to displaying, searching, and navigating the hint list.
520+
* Handle keys related to displaying, searching, and navigating the hint list.
521521
* This gets called before handleChange.
522522
*
523523
* TODO: Ideally, we'd get a more semantic event from the editor that told us
@@ -529,24 +529,29 @@ define(function (require, exports, module) {
529529
* @param {Editor} editor
530530
* @param {KeyboardEvent} event
531531
*/
532-
function _handleKeyEvent(jqEvent, editor, event) {
532+
function _handleKeydownEvent(jqEvent, editor, event) {
533533
keyDownEditor = editor;
534-
if (event.type === "keydown") {
535-
if (!(event.ctrlKey || event.altKey || event.metaKey) &&
536-
(event.keyCode === KeyEvent.DOM_VK_ENTER ||
537-
event.keyCode === KeyEvent.DOM_VK_RETURN ||
538-
event.keyCode === KeyEvent.DOM_VK_TAB)) {
539-
lastChar = String.fromCharCode(event.keyCode);
540-
}
541-
} else if (event.type === "keypress") {
542-
// Last inserted character, used later by handleChange
543-
lastChar = String.fromCharCode(event.charCode);
544-
545-
// Pending Text is used in hintList._keydownHook()
546-
if (hintList) {
547-
hintList.addPendingText(lastChar);
548-
}
549-
} else if (event.type === "keyup" && _inSession(editor)) {
534+
if (!(event.ctrlKey || event.altKey || event.metaKey) &&
535+
(event.keyCode === KeyEvent.DOM_VK_ENTER ||
536+
event.keyCode === KeyEvent.DOM_VK_RETURN ||
537+
event.keyCode === KeyEvent.DOM_VK_TAB)) {
538+
lastChar = String.fromCharCode(event.keyCode);
539+
}
540+
}
541+
function _handleKeypressEvent(jqEvent, editor, event) {
542+
keyDownEditor = editor;
543+
544+
// Last inserted character, used later by handleChange
545+
lastChar = String.fromCharCode(event.charCode);
546+
547+
// Pending Text is used in hintList._keydownHook()
548+
if (hintList) {
549+
hintList.addPendingText(lastChar);
550+
}
551+
}
552+
function _handleKeyupEvent(jqEvent, editor, event) {
553+
keyDownEditor = editor;
554+
if (_inSession(editor)) {
550555
if (event.keyCode === KeyEvent.DOM_VK_HOME || event.keyCode === KeyEvent.DOM_VK_END) {
551556
_endSession();
552557
} else if (event.keyCode === KeyEvent.DOM_VK_LEFT ||
@@ -626,13 +631,17 @@ define(function (require, exports, module) {
626631
function activeEditorChangeHandler(event, current, previous) {
627632
if (current) {
628633
$(current).on("editorChange", _handleChange);
629-
$(current).on("keyEvent", _handleKeyEvent);
634+
$(current).on("keydown", _handleKeydownEvent);
635+
$(current).on("keypress", _handleKeypressEvent);
636+
$(current).on("keyup", _handleKeyupEvent);
630637
}
631638

632639
if (previous) {
633640
//Removing all old Handlers
634641
$(previous).off("editorChange", _handleChange);
635-
$(previous).off("keyEvent", _handleKeyEvent);
642+
$(previous).off("keydown", _handleKeydownEvent);
643+
$(previous).off("keypress", _handleKeypressEvent);
644+
$(previous).off("keyup", _handleKeyupEvent);
636645
}
637646
}
638647

src/editor/Editor.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ define(function (require, exports, module) {
257257
$(this).on("cursorActivity", function (jqEvent, editor) {
258258
self._handleCursorActivity(jqEvent);
259259
});
260-
$(this).on("keyEvent", function (jqEvent, editor, cmEvent) {
261-
self._handleKeyEvents(cmEvent);
260+
$(this).on("keypress", function (jqEvent, editor, cmEvent) {
261+
self._handleKeypressEvents(cmEvent);
262262
});
263263
$(this).on("change", function (jqEvent, editor, changeList) {
264264
self._handleEditorChange(changeList);
@@ -332,24 +332,23 @@ define(function (require, exports, module) {
332332
* back-indenting it if so.
333333
*/
334334
Editor.prototype._checkElectricChars = function (event) {
335-
var instance = this._codeMirror;
336-
if (event.type === "keypress") {
337-
var keyStr = String.fromCharCode(event.which || event.keyCode);
338-
if (/[\]\{\}\)]/.test(keyStr)) {
339-
// If all text before the cursor is whitespace, auto-indent it
340-
var cursor = this.getCursorPos();
341-
var lineStr = instance.getLine(cursor.line);
342-
var nonWS = lineStr.search(/\S/);
343-
344-
if (nonWS === -1 || nonWS >= cursor.ch) {
345-
// Need to do the auto-indent on a timeout to ensure
346-
// the keypress is handled before auto-indenting.
347-
// This is the same timeout value used by the
348-
// electricChars feature in CodeMirror.
349-
window.setTimeout(function () {
350-
instance.indentLine(cursor.line);
351-
}, 75);
352-
}
335+
var instance = this._codeMirror,
336+
keyStr = String.fromCharCode(event.which || event.keyCode);
337+
338+
if (/[\]\{\}\)]/.test(keyStr)) {
339+
// If all text before the cursor is whitespace, auto-indent it
340+
var cursor = this.getCursorPos();
341+
var lineStr = instance.getLine(cursor.line);
342+
var nonWS = lineStr.search(/\S/);
343+
344+
if (nonWS === -1 || nonWS >= cursor.ch) {
345+
// Need to do the auto-indent on a timeout to ensure
346+
// the keypress is handled before auto-indenting.
347+
// This is the same timeout value used by the
348+
// electricChars feature in CodeMirror.
349+
window.setTimeout(function () {
350+
instance.indentLine(cursor.line);
351+
}, 75);
353352
}
354353
}
355354
};
@@ -379,7 +378,7 @@ define(function (require, exports, module) {
379378
* Handle CodeMirror key events.
380379
* @param {!Event} event
381380
*/
382-
Editor.prototype._handleKeyEvents = function (event) {
381+
Editor.prototype._handleKeypressEvents = function (event) {
383382
this._checkElectricChars(event);
384383
};
385384

@@ -693,7 +692,8 @@ define(function (require, exports, module) {
693692

694693
// Redispatch these CodeMirror key events as jQuery events
695694
function _onKeyEvent(instance, event) {
696-
$(self).triggerHandler("keyEvent", [self, event]);
695+
$(self).triggerHandler("keyEvent", [self, event]); // deprecated
696+
$(self).triggerHandler(event.type, [self, event]);
697697
return event.defaultPrevented; // false tells CodeMirror we didn't eat the event
698698
}
699699
this._codeMirror.on("keydown", _onKeyEvent);

src/extensions/default/JavaScriptCodeHints/ParameterHintManager.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ define(function (require, exports, module) {
393393
*/
394394
function installListeners(editor) {
395395

396-
$(editor).on("keyEvent", function (jqEvent, editor, event) {
397-
if (event.type === "keydown" && event.keyCode === KeyEvent.DOM_VK_ESCAPE) {
396+
$(editor).on("keydown", function (jqEvent, editor, event) {
397+
if (event.keyCode === KeyEvent.DOM_VK_ESCAPE) {
398398
dismissHint();
399399
}
400400
}).on("scroll", function () {

0 commit comments

Comments
 (0)