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