Skip to content

Commit f8dcde9

Browse files
committed
more clever key activity detection
we now use the fact that keypress will be repeatedly fired for held keys do the polling for changes in a more efficient way. this also fixes the issue with enter not repeating on Opera.
1 parent 5b55680 commit f8dcde9

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lib/codemirror.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var CodeMirror = (function() {
6161
// happened during the operation.
6262
var updateInput, changes, textChanged, selectionChanged;
6363
// Current visible range (may be bigger than the view window).
64-
var showingFrom = 0, showingTo = 0, lastHeight = 0;
64+
var showingFrom = 0, showingTo = 0, lastHeight = 0, curKeyId = null;
6565
// editing will hold an object describing the things we put in the
6666
// textarea, to help figure out whether something changed.
6767
// bracketHighlighted is used to remember that a backet has been
@@ -80,7 +80,7 @@ var CodeMirror = (function() {
8080
connect(window, "resize", function() {updateDisplay(true);});
8181
connect(input, "keyup", operation(onKeyUp));
8282
connect(input, "keydown", operation(onKeyDown));
83-
connect(input, "keypress", onKeyPress);
83+
connect(input, "keypress", operation(onKeyPress));
8484
connect(input, "focus", onFocus);
8585
connect(input, "blur", onBlur);
8686

@@ -281,7 +281,7 @@ var CodeMirror = (function() {
281281
if (mod && (code == 36 || code == 35)) {scrollEnd(code == 36); return e.stop();} // ctrl-home/end
282282
if (mod && code == 65) {selectAll(); return e.stop();} // ctrl-a
283283
if (!options.readOnly) {
284-
if (!anyMod && code == 13) {handleEnter(); return e.stop();} // enter
284+
if (!anyMod && code == 13) {return;} // enter
285285
if (!anyMod && code == 9 && handleTab(e.e.shiftKey)) return e.stop(); // tab
286286
if (mod && code == 90) {undo(); return e.stop();} // ctrl-z
287287
if (mod && ((e.e.shiftKey && code == 90) || code == 89)) {redo(); return e.stop();} // ctrl-shift-z, ctrl-y
@@ -293,15 +293,15 @@ var CodeMirror = (function() {
293293
// its start when it is inverted and a movement key is pressed
294294
// (and later restore it again), shouldn't be used for
295295
// non-movement keys.
296-
var id = (mod ? "c" : "") + code;
296+
curKeyId = (mod ? "c" : "") + code;
297297
if (sel.inverted && movementKeys.hasOwnProperty(id)) {
298298
var range = selRange(input);
299299
if (range) {
300300
reducedSelection = {anchor: range.start};
301301
setSelRange(input, range.start, range.start);
302302
}
303303
}
304-
fastPoll(id);
304+
fastPoll(curKeyId);
305305
}
306306
function onKeyUp(e) {
307307
if (reducedSelection) {
@@ -319,7 +319,9 @@ var CodeMirror = (function() {
319319
}
320320
var code = e.e.keyCode;
321321
// Re-stop tab and enter. Necessary on some browsers.
322-
if (code == 13 || (code == 9 && options.tabMode != "default")) e.stop();
322+
if (code == 13) {handleEnter(); e.stop();}
323+
else if (code == 9 && options.tabMode != "default") e.stop();
324+
else fastPoll(curKeyId);
323325
}
324326

325327
function onFocus() {
@@ -467,13 +469,12 @@ var CodeMirror = (function() {
467469
});
468470
}
469471
function fastPoll(keyId) {
470-
var misses = 0;
472+
var missed = false;
471473
function p() {
472474
startOperation();
473475
var changed = readInput();
474476
if (changed == "moved" && keyId) movementKeys[keyId] = true;
475-
if (changed) {poll.set(80, p); misses = 0;}
476-
else if (misses++ < 5) {poll.set(80, p);}
477+
if (!changed && !missed) {missed = true; poll.set(80, p);}
477478
else slowPoll();
478479
endOperation();
479480
}

0 commit comments

Comments
 (0)