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