Skip to content

Commit

Permalink
fix some flakiness in scrolling the cursor into view
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Feb 19, 2011
1 parent 1be1773 commit cac102d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ var CodeMirror = (function() {
'<span>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</span></pre>' +
'<div style="position: relative">' +
'<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div>' +
'<div style="overflow: hidden; position: absolute; width: 0">' +
'<textarea style="position: absolute; width: 10000px;"></textarea></div>' +
'<div style="overflow: hidden; position: absolute; width: 0; left: 0;">' +
'<textarea style="width: 10000px; height: 1px; position: absolute;"></textarea></div>' +
'<div class="CodeMirror-lines"><div style="position: relative">' +
'<pre class="CodeMirror-cursor">&nbsp;</pre>' +
'<div></div></div></div></div></div>';
Expand Down Expand Up @@ -740,21 +740,25 @@ var CodeMirror = (function() {

function scrollCursorIntoView() {
var cursor = localCoords(sel.inverted ? sel.from : sel.to);
cursor.x += paddingLeft(); cursor.y += paddingTop();
scrollIntoView(cursor.x, cursor.y, cursor.x, cursor.yBot);
return scrollIntoView(cursor.x, cursor.y, cursor.x, cursor.yBot);
}
function scrollIntoView(x1, y1, x2, y2) {
var pl = paddingLeft(), pt = paddingTop();
y1 += pt; y2 += pt; x1 += pl; x2 += pl;
var screen = div.clientHeight, screentop = div.scrollTop;
if (y1 < screentop)
div.scrollTop = Math.max(0, y1 - 10);
else if (y2 > screentop + screen)
div.scrollTop = y2 + 10 - screen;

var screenw = div.offsetWidth, screenleft = div.scrollLeft;
var screenw = div.clientWidth, screenleft = div.scrollLeft;
if (x1 < screenleft)
div.scrollLeft = Math.max(0, x1 - 10);
else if (x2 > screenw + screenleft)
else if (x2 > screenw + screenleft) {
div.scrollLeft = x2 + 10 - screenw;
if (x2 > code.clientWidth) return false;
}
return true;
}

function visibleLines() {
Expand Down Expand Up @@ -949,7 +953,7 @@ var CodeMirror = (function() {
function updateCursor() {
var head = sel.inverted ? sel.from : sel.to;
var x = charX(head.line, head.ch) + "px", y = (head.line - showingFrom) * lineHeight() + "px";
inputDiv.style.top = y; inputDiv.style.left = x;
inputDiv.style.top = y;
if (posEq(sel.from, sel.to)) {
cursor.style.top = y; cursor.style.left = x;
cursor.style.display = "";
Expand Down Expand Up @@ -1317,9 +1321,11 @@ var CodeMirror = (function() {
updateInput = null; changes = []; textChanged = false; selectionChanged = false;
}
function endOperation() {
if (selectionChanged) scrollCursorIntoView();
var reScroll = false;
if (selectionChanged) reScroll = !scrollCursorIntoView();
if (changes.length) updateDisplay(changes);
else if (selectionChanged) updateCursor();
if (reScroll) scrollCursorIntoView();
if (selectionChanged) restartBlink();

if (updateInput === true || (updateInput !== false && selectionChanged))
Expand Down

0 comments on commit cac102d

Please sign in to comment.