Skip to content

Commit

Permalink
More selection drawing hackery
Browse files Browse the repository at this point in the history
  • Loading branch information
twiss committed Jul 12, 2013
1 parent a63205b commit 8c9d277
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lib/codemirror.css
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,10 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}
}

.CodeMirror-selected { background: #d9d9d9; border-width: 0; border-style: solid; border-color: #aaa; }
.CodeMirror-selected-top { border-top-left-radius: 3px; border-top-right-radius: 3px; border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; }
.CodeMirror-selected-bottom { border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-left-width: 1px; border-bottom-width: 1px; border-right-width: 1px; }
.CodeMirror-selected-bottom { border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-left-width: 1px; border-bottom-width: 1px; border-right-width: 1px; margin-top: 1px; }
.CodeMirror-selected-top { border-top-left-radius: 3px; border-top-right-radius: 3px; border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; margin-top: 0; }
.CodeMirror-selected-middle { border-bottom-right-radius: 3px; border-width: 1px; }
.CodeMirror-selected-middle-continued { border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; }
.CodeMirror-selected-top-left { border-top-left-radius: 3px; }
.CodeMirror-focused .CodeMirror-selected { background: #d7d4f0; }

Expand Down
27 changes: 18 additions & 9 deletions lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ window.CodeMirror = (function() {
"px; height: " + (bottom - top) + "px"));
}
function addAll(className) {
for (var i = 0; i < this.added.length; i++) add(this.added[i][0], this.added[i][1], this.added[i][2], this.added[i][3], className);
for (var i = 0; i < this.added.length; i++) add(this.added[i][0], this.added[i][1], this.added[i][2], this.added[i][3], className || this.added[i][4]);
}

function drawForLine(line, fromArg, toArg) {
Expand All @@ -829,9 +829,9 @@ window.CodeMirror = (function() {
}
if (fromArg == null && from == 0) left = pl;
if (rightPos.top - leftPos.top > 3) { // Different lines, draw top part
added.push([left, leftPos.top, null, leftPos.bottom]);
added.push([left, leftPos.top - 2, null, leftPos.bottom - 2, "CodeMirror-selected-top"]);
left = pl;
if (leftPos.bottom < rightPos.top) added.push([left, leftPos.bottom, null, rightPos.top]);
if (leftPos.bottom < rightPos.top) added.push([left, leftPos.bottom, null, rightPos.top, "CodeMirror-selected-top"]);
}
if (toArg == null && to == lineLen) right = clientWidth;
if (!start || leftPos.top < start.top || leftPos.top == start.top && leftPos.left < start.left)
Expand All @@ -840,7 +840,7 @@ window.CodeMirror = (function() {
end = rightPos;
if (left < pl + 1) left = pl;
if (right - left)
added.push([left, rightPos.top, right - left, rightPos.bottom - 1]);
added.push([left, rightPos.top, right - left, rightPos.bottom, added.length ? "" : "CodeMirror-selected-top"]);
});
return {start: start, end: end, added: added, add: addAll};
}
Expand All @@ -850,17 +850,21 @@ window.CodeMirror = (function() {
if (posEq(sel.from, sel.to)) continue;
foundSel = true;
if (sel.from.line == sel.to.line) {
drawForLine(sel.from.line, sel.from.ch, sel.to.ch).add("CodeMirror-selected-top CodeMirror-selected-bottom");
var left = drawForLine(sel.from.line, sel.from.ch, sel.to.ch);
if (left.start.top < left.end.top)
add(pl, left.start.bottom - 2, null, left.end.top - 2, "CodeMirror-selected-middle");
if (left.added[1]) left.added[1][4] += " CodeMirror-selected-top-left";
lst(left.added)[4] += " CodeMirror-selected-bottom";
left.add();
} else {
var fromLine = getLine(doc, sel.from.line), toLine = getLine(doc, sel.to.line);
var singleVLine = visualLine(doc, fromLine) == visualLine(doc, toLine);
var right = drawForLine(sel.to.line, singleVLine ? 0 : null, sel.to.ch);
var rightStart = right.start;
var left = drawForLine(sel.from.line, sel.from.ch, singleVLine ? fromLine.text.length : null);
var leftEnd = left.end;
var addMiddle = leftEnd.bottom < rightStart.top || right.added.length;
if (addMiddle)
add(pl + (leftEnd.bottom < rightStart.top ? 0 : 1), leftEnd.bottom - 1, null, rightStart.top - 1, "CodeMirror-selected-middle" + (sel.from.ch ? " CodeMirror-selected-top-left" : "") + (right.added.length ? "" : " CodeMirror-selected-bottom"));
if (leftEnd.bottom < rightStart.top || right.added.length)
add(pl, leftEnd.bottom, null, rightStart.top, "CodeMirror-selected-middle" + (sel.from.ch && left.added.length < 2 ? " CodeMirror-selected-top-left" : "") + (right.added.length ? "" : " CodeMirror-selected-bottom"));
if (singleVLine) {
if (leftEnd.top < rightStart.top - 2) {
add(leftEnd.right, leftEnd.top, null, leftEnd.bottom, "");
Expand All @@ -869,7 +873,12 @@ window.CodeMirror = (function() {
add(leftEnd.right, leftEnd.top, rightStart.left - leftEnd.right, leftEnd.bottom, "");
}
}
left.add("CodeMirror-selected-top" + (right.added.length || leftEnd.bottom < rightStart.top ? "" : " CodeMirror-selected-bottom"));
left.added[0][4] = "CodeMirror-selected-top";
if (left.added[1]) left.added[1][4] += " CodeMirror-selected-top-left";
if (!right.added.length && leftEnd.bottom >= rightStart.top) lst(left.added)[4] += " CodeMirror-selected-bottom";
if (right.added.length) for (var j = 1; j < left.added.length; j++) left.added[1][4] += " CodeMirror-selected-middle-continued";
left.added.reverse();
left.add();
right.add("CodeMirror-selected-bottom" + (!sel.from.ch || leftEnd.bottom < rightStart.top ? "" : " CodeMirror-selected-top-left"));
}
}
Expand Down

0 comments on commit 8c9d277

Please sign in to comment.