| 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. | |
| 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) { | 4 (function(mod) { |
| 11 if (typeof exports == "object" && typeof module == "object") // CommonJS | 5 if (typeof exports == "object" && typeof module == "object") // CommonJS |
| 12 mod(require("../../lib/codemirror")); | 6 mod(require("../../lib/codemirror")); |
| 13 else if (typeof define == "function" && define.amd) // AMD | 7 else if (typeof define == "function" && define.amd) // AMD |
| 14 define(["../../lib/codemirror"], mod); | 8 define(["../../lib/codemirror"], mod); |
| 15 else // Plain browser env | 9 else // Plain browser env |
| 16 mod(CodeMirror); | 10 mod(CodeMirror); |
| 17 })(function(CodeMirror) { | 11 })(function(CodeMirror) { |
| 18 "use strict"; | 12 "use strict"; |
| 19 var WRAP_CLASS = "CodeMirror-activeline"; | 13 var WRAP_CLASS = "CodeMirror-activeline"; |
| 20 var BACK_CLASS = "CodeMirror-activeline-background"; | 14 var BACK_CLASS = "CodeMirror-activeline-background"; |
| 21 var GUTT_CLASS = "CodeMirror-activeline-gutter"; | 15 var GUTT_CLASS = "CodeMirror-activeline-gutter"; |
| 22 | 16 |
| 23 CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) { | 17 CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) { |
| 24 var prev = old && old != CodeMirror.Init; | 18 var prev = old == CodeMirror.Init ? false : old; |
| 25 if (val && !prev) { | 19 if (val == prev) return |
| 26 cm.state.activeLines = []; | 20 if (prev) { |
| 27 updateActiveLines(cm, cm.listSelections()); | |
| 28 cm.on("beforeSelectionChange", selectionChange); | |
| 29 } else if (!val && prev) { | |
| 30 cm.off("beforeSelectionChange", selectionChange); | 21 cm.off("beforeSelectionChange", selectionChange); |
| 31 clearActiveLines(cm); | 22 clearActiveLines(cm); |
| 32 delete cm.state.activeLines; | 23 delete cm.state.activeLines; |
| 33 } | 24 } |
| 25 if (val) { |
| 26 cm.state.activeLines = []; |
| 27 updateActiveLines(cm, cm.listSelections()); |
| 28 cm.on("beforeSelectionChange", selectionChange); |
| 29 } |
| 34 }); | 30 }); |
| 35 | 31 |
| 36 function clearActiveLines(cm) { | 32 function clearActiveLines(cm) { |
| 37 for (var i = 0; i < cm.state.activeLines.length; i++) { | 33 for (var i = 0; i < cm.state.activeLines.length; i++) { |
| 38 cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS); | 34 cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS); |
| 39 cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS); | 35 cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS); |
| 40 cm.removeLineClass(cm.state.activeLines[i], "gutter", GUTT_CLASS); | 36 cm.removeLineClass(cm.state.activeLines[i], "gutter", GUTT_CLASS); |
| 41 } | 37 } |
| 42 } | 38 } |
| 43 | 39 |
| 44 function sameArray(a, b) { | 40 function sameArray(a, b) { |
| 45 if (a.length != b.length) return false; | 41 if (a.length != b.length) return false; |
| 46 for (var i = 0; i < a.length; i++) | 42 for (var i = 0; i < a.length; i++) |
| 47 if (a[i] != b[i]) return false; | 43 if (a[i] != b[i]) return false; |
| 48 return true; | 44 return true; |
| 49 } | 45 } |
| 50 | 46 |
| 51 function updateActiveLines(cm, ranges) { | 47 function updateActiveLines(cm, ranges) { |
| 52 var active = []; | 48 var active = []; |
| 53 for (var i = 0; i < ranges.length; i++) { | 49 for (var i = 0; i < ranges.length; i++) { |
| 54 var range = ranges[i]; | 50 var range = ranges[i]; |
| 55 if (!range.empty()) continue; | 51 var option = cm.getOption("styleActiveLine"); |
| 52 if (typeof option == "object" && option.nonEmpty ? range.anchor.line != ra
nge.head.line : !range.empty()) |
| 53 continue |
| 56 var line = cm.getLineHandleVisualStart(range.head.line); | 54 var line = cm.getLineHandleVisualStart(range.head.line); |
| 57 if (active[active.length - 1] != line) active.push(line); | 55 if (active[active.length - 1] != line) active.push(line); |
| 58 } | 56 } |
| 59 if (sameArray(cm.state.activeLines, active)) return; | 57 if (sameArray(cm.state.activeLines, active)) return; |
| 60 cm.operation(function() { | 58 cm.operation(function() { |
| 61 clearActiveLines(cm); | 59 clearActiveLines(cm); |
| 62 for (var i = 0; i < active.length; i++) { | 60 for (var i = 0; i < active.length; i++) { |
| 63 cm.addLineClass(active[i], "wrap", WRAP_CLASS); | 61 cm.addLineClass(active[i], "wrap", WRAP_CLASS); |
| 64 cm.addLineClass(active[i], "background", BACK_CLASS); | 62 cm.addLineClass(active[i], "background", BACK_CLASS); |
| 65 cm.addLineClass(active[i], "gutter", GUTT_CLASS); | 63 cm.addLineClass(active[i], "gutter", GUTT_CLASS); |
| 66 } | 64 } |
| 67 cm.state.activeLines = active; | 65 cm.state.activeLines = active; |
| 68 }); | 66 }); |
| 69 } | 67 } |
| 70 | 68 |
| 71 function selectionChange(cm, sel) { | 69 function selectionChange(cm, sel) { |
| 72 updateActiveLines(cm, sel.ranges); | 70 updateActiveLines(cm, sel.ranges); |
| 73 } | 71 } |
| 74 }); | 72 }); |
| OLD | NEW |