| 776 |
lars |
1 |
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
|
2 |
// Distributed under an MIT license: http://codemirror.net/LICENSE
|
|
|
3 |
|
|
|
4 |
// Because sometimes you need to style the cursor's line.
|
|
|
5 |
//
|
|
|
6 |
// Adds an option 'styleActiveLine' which, when enabled, gives the
|
|
|
7 |
// active line's wrapping <div> the CSS class "CodeMirror-activeline",
|
|
|
8 |
// and gives its background <div> the class "CodeMirror-activeline-background".
|
|
|
9 |
|
|
|
10 |
(function(mod) {
|
|
|
11 |
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
|
12 |
mod(require("../../lib/codemirror"));
|
|
|
13 |
else if (typeof define == "function" && define.amd) // AMD
|
|
|
14 |
define(["../../lib/codemirror"], mod);
|
|
|
15 |
else // Plain browser env
|
|
|
16 |
mod(CodeMirror);
|
|
|
17 |
})(function(CodeMirror) {
|
|
|
18 |
"use strict";
|
|
|
19 |
var WRAP_CLASS = "CodeMirror-activeline";
|
|
|
20 |
var BACK_CLASS = "CodeMirror-activeline-background";
|
|
|
21 |
|
|
|
22 |
CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
|
|
|
23 |
var prev = old && old != CodeMirror.Init;
|
|
|
24 |
if (val && !prev) {
|
|
|
25 |
cm.state.activeLines = [];
|
|
|
26 |
updateActiveLines(cm, cm.listSelections());
|
|
|
27 |
cm.on("beforeSelectionChange", selectionChange);
|
|
|
28 |
} else if (!val && prev) {
|
|
|
29 |
cm.off("beforeSelectionChange", selectionChange);
|
|
|
30 |
clearActiveLines(cm);
|
|
|
31 |
delete cm.state.activeLines;
|
|
|
32 |
}
|
|
|
33 |
});
|
|
|
34 |
|
|
|
35 |
function clearActiveLines(cm) {
|
|
|
36 |
for (var i = 0; i < cm.state.activeLines.length; i++) {
|
|
|
37 |
cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS);
|
|
|
38 |
cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS);
|
|
|
39 |
}
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
function sameArray(a, b) {
|
|
|
43 |
if (a.length != b.length) return false;
|
|
|
44 |
for (var i = 0; i < a.length; i++)
|
|
|
45 |
if (a[i] != b[i]) return false;
|
|
|
46 |
return true;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
function updateActiveLines(cm, ranges) {
|
|
|
50 |
var active = [];
|
|
|
51 |
for (var i = 0; i < ranges.length; i++) {
|
|
|
52 |
var range = ranges[i];
|
|
|
53 |
if (!range.empty()) continue;
|
|
|
54 |
var line = cm.getLineHandleVisualStart(range.head.line);
|
|
|
55 |
if (active[active.length - 1] != line) active.push(line);
|
|
|
56 |
}
|
|
|
57 |
if (sameArray(cm.state.activeLines, active)) return;
|
|
|
58 |
cm.operation(function() {
|
|
|
59 |
clearActiveLines(cm);
|
|
|
60 |
for (var i = 0; i < active.length; i++) {
|
|
|
61 |
cm.addLineClass(active[i], "wrap", WRAP_CLASS);
|
|
|
62 |
cm.addLineClass(active[i], "background", BACK_CLASS);
|
|
|
63 |
}
|
|
|
64 |
cm.state.activeLines = active;
|
|
|
65 |
});
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
function selectionChange(cm, sel) {
|
|
|
69 |
updateActiveLines(cm, sel.ranges);
|
|
|
70 |
}
|
|
|
71 |
});
|