Skip to content

Commit

Permalink
Switch to a dummy cursor to force doc width in non-wrapping mode
Browse files Browse the repository at this point in the history
This prevents situations where the cursor will cause a scrollbar, but the
rest of the code doesn't realize this. Also removes the lineSpace.style.width
setting code, which appears to have been broken in several different ways.

Closes #550
  • Loading branch information
marijnh committed Jun 18, 2012
1 parent 8de67d2 commit beadea5
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ var CodeMirror = (function() {
'<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div>' +
// Provides positioning relative to (visible) text origin
'<div class="CodeMirror-lines"><div style="position: relative; z-index: 0">' +
// Used to measure text size
'<div style="position: absolute; width: 100%; height: 0; overflow: hidden; visibility: hidden;"></div>' +
'<pre class="CodeMirror-cursor">&#160;</pre>' + // Absolutely positioned blinky cursor
'<pre class="CodeMirror-cursor" style="visibility: hidden">&#160;</pre>' + // Used to force a width
'<div style="position: relative; z-index: -1"></div><div></div>' + // DIVs containing the selection and the actual code
'</div></div></div></div></div>';
if (place.appendChild) place.appendChild(wrapper); else place(wrapper);
Expand All @@ -40,10 +42,9 @@ var CodeMirror = (function() {
scroller = wrapper.lastChild, code = scroller.firstChild,
mover = code.firstChild, gutter = mover.firstChild, gutterText = gutter.firstChild,
lineSpace = gutter.nextSibling.firstChild, measure = lineSpace.firstChild,
cursor = measure.nextSibling, selectionDiv = cursor.nextSibling,
lineDiv = selectionDiv.nextSibling,
scrollbar = inputDiv.nextSibling,
scrollbarInner = scrollbar.firstChild;
cursor = measure.nextSibling, widthForcer = cursor.nextSibling,
selectionDiv = widthForcer.nextSibling, lineDiv = selectionDiv.nextSibling,
scrollbar = inputDiv.nextSibling, scrollbarInner = scrollbar.firstChild;
themeChanged(); keyMapChanged();
// Needed to hide big blue blinking cursor on Mobile Safari
if (ios) input.style.width = "0px";
Expand Down Expand Up @@ -94,15 +95,15 @@ var CodeMirror = (function() {
// Variables used by startOperation/endOperation to track what
// happened during the operation.
var updateInput, userSelChange, changes, textChanged, selectionChanged, leaveInputAlone,
gutterDirty, callbacks, maxLengthChanged;
gutterDirty, callbacks;
// Current visible range (may be bigger than the view window).
var displayOffset = 0, showingFrom = 0, showingTo = 0, lastSizeC = 0;
// bracketHighlighted is used to remember that a bracket has been
// marked.
var bracketHighlighted;
// Tracks the maximum line length so that the horizontal scrollbar
// can be kept static when scrolling.
var maxLine = "", maxWidth;
var maxLine = "", updateMaxLine = false, maxLineChanged = true;
var tabCache = {};

// Initialize the content.
Expand Down Expand Up @@ -789,11 +790,11 @@ var CodeMirror = (function() {
doc.iter(from.line, from.line + newText.length, function(line) {
var l = line.text;
if (!line.hidden && l.length > maxLineLength) {
maxLine = l; maxLineLength = l.length; maxWidth = null;
maxLine = l; maxLineLength = l.length; maxLineChanged = true;
recomputeMaxLength = false;
}
});
if (recomputeMaxLength) maxLengthChanged = true;
if (recomputeMaxLength) updateMaxLine = true;
}

// Add these lines to the work array, so that they will be
Expand Down Expand Up @@ -853,14 +854,14 @@ var CodeMirror = (function() {

function computeMaxLength() {
var maxLineLength = 0;
maxLine = ""; maxWidth = null;
maxLine = ""; maxLineChanged = true;
doc.iter(0, doc.size, function(line) {
var l = line.text;
if (!line.hidden && l.length > maxLineLength) {
maxLineLength = l.length; maxLine = l;
}
});
maxLengthChanged = false;
updateMaxLine = false;
}

function replaceRange(code, from, to) {
Expand Down Expand Up @@ -1073,7 +1074,6 @@ var CodeMirror = (function() {
" nodes=" + lineDiv.childNodes.length);

function checkHeights() {
maxWidth = scroller.clientWidth;
var curNode = lineDiv.firstChild, heightChanged = false;
doc.iter(showingFrom, showingTo, function(line) {
if (!line.hidden) {
Expand All @@ -1096,25 +1096,15 @@ var CodeMirror = (function() {
var virtualHeight = Math.floor(doc.height * th + 2 * paddingTop()), scrollbarHeight = scroller.clientHeight;
if (virtualHeight > scrollbarHeight) scrollbar.style.display = "block";
checkHeights();
} else {
if (maxWidth == null) maxWidth = stringWidth(maxLine);
if (maxWidth > scroller.clientWidth) {
lineSpace.style.width = maxWidth + "px";
// Needed to prevent odd wrapping/hiding of widgets placed in here.
code.style.width = "";
code.style.width = scroller.scrollWidth + "px";
} else {
lineSpace.style.width = code.style.width = "";
}
}

gutter.style.display = gutterDisplay;
if (different || gutterDirty) {
// If the gutter grew in size, re-check heights. If those changed, re-draw gutter.
updateGutter() && options.lineWrapping && checkHeights() && updateGutter();
}
updateVerticalScroll(scrollTop);
updateSelection();
updateVerticalScroll(scrollTop);
if (!suppressCallback && options.onUpdate) options.onUpdate(instance);
return true;
}
Expand Down Expand Up @@ -1467,9 +1457,10 @@ var CodeMirror = (function() {
if (guess != 1) updateLineHeight(line, guess);
});
lineSpace.style.width = code.style.width = "";
widthForcer.style.left = "";
} else {
wrapper.className = wrapper.className.replace(" CodeMirror-wrap", "");
maxWidth = null; maxLine = "";
maxLine = ""; maxLineChanged = true;
doc.iter(0, doc.size, function(line) {
if (line.height != 1 && !line.hidden) updateLineHeight(line, 1);
if (line.text.length > maxLine.length) maxLine = line.text;
Expand Down Expand Up @@ -1602,10 +1593,9 @@ var CodeMirror = (function() {
if (!options.lineWrapping) {
var l = line.text;
if (hidden && l.length == maxLine.length) {
maxLengthChanged = true;
updateMaxLine = true;
} else if (!hidden && l.length > maxLine.length) {
maxLine = l; maxWidth = null;
maxLengthChanged = false;
maxLine = l; maxWidth = null; updateMaxLine = false;
}
}
updateLineHeight(line, hidden ? 0 : 1);
Expand Down Expand Up @@ -1958,8 +1948,12 @@ var CodeMirror = (function() {
changes = []; selectionChanged = false; callbacks = [];
}
function endOperation() {
if (updateMaxLine) computeMaxLength();
if (maxLineChanged && !options.lineWrapping) {
widthForcer.style.left = stringWidth(maxLine) + "px";
maxLineChanged = false;
}
var newScrollPos, updated;
if (maxLengthChanged) computeMaxLength();
if (selectionChanged) {
var coords = calculateCursorCoords();
newScrollPos = calculateScrollPos(coords.x, coords.y, coords.x, coords.yBot);
Expand Down

0 comments on commit beadea5

Please sign in to comment.