1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: http://codemirror.net/LICENSE
4 // Because sometimes you need to style the cursor's line.
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".
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
17 })(function(CodeMirror
) {
19 var WRAP_CLASS
= "CodeMirror-activeline";
20 var BACK_CLASS
= "CodeMirror-activeline-background";
22 CodeMirror
.defineOption("styleActiveLine", false, function(cm
, val
, old
) {
23 var prev
= old
&& old
!= CodeMirror
.Init
;
25 cm
.state
.activeLines
= [];
26 updateActiveLines(cm
, cm
.listSelections());
27 cm
.on("beforeSelectionChange", selectionChange
);
28 } else if (!val
&& prev
) {
29 cm
.off("beforeSelectionChange", selectionChange
);
31 delete cm
.state
.activeLines
;
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
);
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;
49 function updateActiveLines(cm
, ranges
) {
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
);
57 if (sameArray(cm
.state
.activeLines
, active
)) return;
58 cm
.operation(function() {
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
);
64 cm
.state
.activeLines
= active
;
68 function selectionChange(cm
, sel
) {
69 updateActiveLines(cm
, sel
.ranges
);