1 // Because sometimes you need to style the cursor's line.
3 // Adds an option 'styleActiveLine' which, when enabled, gives the
4 // active line's wrapping <div> the CSS class "CodeMirror-activeline",
5 // and gives its background <div> the class "CodeMirror-activeline-background".
8 if (typeof exports
== "object" && typeof module
== "object") // CommonJS
9 mod(require("../../lib/codemirror"));
10 else if (typeof define
== "function" && define
.amd
) // AMD
11 define(["../../lib/codemirror"], mod
);
12 else // Plain browser env
14 })(function(CodeMirror
) {
16 var WRAP_CLASS
= "CodeMirror-activeline";
17 var BACK_CLASS
= "CodeMirror-activeline-background";
19 CodeMirror
.defineOption("styleActiveLine", false, function(cm
, val
, old
) {
20 var prev
= old
&& old
!= CodeMirror
.Init
;
22 cm
.state
.activeLines
= [];
23 updateActiveLines(cm
, cm
.listSelections());
24 cm
.on("beforeSelectionChange", selectionChange
);
25 } else if (!val
&& prev
) {
26 cm
.off("beforeSelectionChange", selectionChange
);
28 delete cm
.state
.activeLines
;
32 function clearActiveLines(cm
) {
33 for (var i
= 0; i
< cm
.state
.activeLines
.length
; i
++) {
34 cm
.removeLineClass(cm
.state
.activeLines
[i
], "wrap", WRAP_CLASS
);
35 cm
.removeLineClass(cm
.state
.activeLines
[i
], "background", BACK_CLASS
);
39 function sameArray(a
, b
) {
40 if (a
.length
!= b
.length
) return false;
41 for (var i
= 0; i
< a
.length
; i
++)
42 if (a
[i
] != b
[i
]) return false;
46 function updateActiveLines(cm
, ranges
) {
48 for (var i
= 0; i
< ranges
.length
; i
++) {
49 var line
= cm
.getLineHandleVisualStart(ranges
[i
].head
.line
);
50 if (active
[active
.length
- 1] != line
) active
.push(line
);
52 if (sameArray(cm
.state
.activeLines
, active
)) return;
53 cm
.operation(function() {
55 for (var i
= 0; i
< active
.length
; i
++) {
56 cm
.addLineClass(active
[i
], "wrap", WRAP_CLASS
);
57 cm
.addLineClass(active
[i
], "background", BACK_CLASS
);
59 cm
.state
.activeLines
= active
;
63 function selectionChange(cm
, sel
) {
64 updateActiveLines(cm
, sel
.ranges
);