michael@0: // Because sometimes you need to style the cursor's line. michael@0: // michael@0: // Adds an option 'styleActiveLine' which, when enabled, gives the michael@0: // active line's wrapping
the CSS class "CodeMirror-activeline", michael@0: // and gives its background
the class "CodeMirror-activeline-background". michael@0: michael@0: (function(mod) { michael@0: if (typeof exports == "object" && typeof module == "object") // CommonJS michael@0: mod(require("../../lib/codemirror")); michael@0: else if (typeof define == "function" && define.amd) // AMD michael@0: define(["../../lib/codemirror"], mod); michael@0: else // Plain browser env michael@0: mod(CodeMirror); michael@0: })(function(CodeMirror) { michael@0: "use strict"; michael@0: var WRAP_CLASS = "CodeMirror-activeline"; michael@0: var BACK_CLASS = "CodeMirror-activeline-background"; michael@0: michael@0: CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) { michael@0: var prev = old && old != CodeMirror.Init; michael@0: if (val && !prev) { michael@0: cm.state.activeLines = []; michael@0: updateActiveLines(cm, cm.listSelections()); michael@0: cm.on("beforeSelectionChange", selectionChange); michael@0: } else if (!val && prev) { michael@0: cm.off("beforeSelectionChange", selectionChange); michael@0: clearActiveLines(cm); michael@0: delete cm.state.activeLines; michael@0: } michael@0: }); michael@0: michael@0: function clearActiveLines(cm) { michael@0: for (var i = 0; i < cm.state.activeLines.length; i++) { michael@0: cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS); michael@0: cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS); michael@0: } michael@0: } michael@0: michael@0: function sameArray(a, b) { michael@0: if (a.length != b.length) return false; michael@0: for (var i = 0; i < a.length; i++) michael@0: if (a[i] != b[i]) return false; michael@0: return true; michael@0: } michael@0: michael@0: function updateActiveLines(cm, ranges) { michael@0: var active = []; michael@0: for (var i = 0; i < ranges.length; i++) { michael@0: var line = cm.getLineHandleVisualStart(ranges[i].head.line); michael@0: if (active[active.length - 1] != line) active.push(line); michael@0: } michael@0: if (sameArray(cm.state.activeLines, active)) return; michael@0: cm.operation(function() { michael@0: clearActiveLines(cm); michael@0: for (var i = 0; i < active.length; i++) { michael@0: cm.addLineClass(active[i], "wrap", WRAP_CLASS); michael@0: cm.addLineClass(active[i], "background", BACK_CLASS); michael@0: } michael@0: cm.state.activeLines = active; michael@0: }); michael@0: } michael@0: michael@0: function selectionChange(cm, sel) { michael@0: updateActiveLines(cm, sel.ranges); michael@0: } michael@0: });