|
0
|
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 var GUTT_CLASS = "CodeMirror-activeline-gutter";
|
|
|
22
|
|
|
23 CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
|
|
|
24 var prev = old && old != CodeMirror.Init;
|
|
|
25 if (val && !prev) {
|
|
|
26 cm.state.activeLines = [];
|
|
|
27 updateActiveLines(cm, cm.listSelections());
|
|
|
28 cm.on("beforeSelectionChange", selectionChange);
|
|
|
29 } else if (!val && prev) {
|
|
|
30 cm.off("beforeSelectionChange", selectionChange);
|
|
|
31 clearActiveLines(cm);
|
|
|
32 delete cm.state.activeLines;
|
|
|
33 }
|
|
|
34 });
|
|
|
35
|
|
|
36 function clearActiveLines(cm) {
|
|
|
37 for (var i = 0; i < cm.state.activeLines.length; i++) {
|
|
|
38 cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS);
|
|
|
39 cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS);
|
|
|
40 cm.removeLineClass(cm.state.activeLines[i], "gutter", GUTT_CLASS);
|
|
|
41 }
|
|
|
42 }
|
|
|
43
|
|
|
44 function sameArray(a, b) {
|
|
|
45 if (a.length != b.length) return false;
|
|
|
46 for (var i = 0; i < a.length; i++)
|
|
|
47 if (a[i] != b[i]) return false;
|
|
|
48 return true;
|
|
|
49 }
|
|
|
50
|
|
|
51 function updateActiveLines(cm, ranges) {
|
|
|
52 var active = [];
|
|
|
53 for (var i = 0; i < ranges.length; i++) {
|
|
|
54 var range = ranges[i];
|
|
|
55 if (!range.empty()) continue;
|
|
|
56 var line = cm.getLineHandleVisualStart(range.head.line);
|
|
|
57 if (active[active.length - 1] != line) active.push(line);
|
|
|
58 }
|
|
|
59 if (sameArray(cm.state.activeLines, active)) return;
|
|
|
60 cm.operation(function() {
|
|
|
61 clearActiveLines(cm);
|
|
|
62 for (var i = 0; i < active.length; i++) {
|
|
|
63 cm.addLineClass(active[i], "wrap", WRAP_CLASS);
|
|
|
64 cm.addLineClass(active[i], "background", BACK_CLASS);
|
|
|
65 cm.addLineClass(active[i], "gutter", GUTT_CLASS);
|
|
|
66 }
|
|
|
67 cm.state.activeLines = active;
|
|
|
68 });
|
|
|
69 }
|
|
|
70
|
|
|
71 function selectionChange(cm, sel) {
|
|
|
72 updateActiveLines(cm, sel.ranges);
|
|
|
73 }
|
|
|
74 });
|