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