1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/sourceeditor/codemirror/activeline.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,66 @@ 1.4 +// Because sometimes you need to style the cursor's line. 1.5 +// 1.6 +// Adds an option 'styleActiveLine' which, when enabled, gives the 1.7 +// active line's wrapping <div> the CSS class "CodeMirror-activeline", 1.8 +// and gives its background <div> the class "CodeMirror-activeline-background". 1.9 + 1.10 +(function(mod) { 1.11 + if (typeof exports == "object" && typeof module == "object") // CommonJS 1.12 + mod(require("../../lib/codemirror")); 1.13 + else if (typeof define == "function" && define.amd) // AMD 1.14 + define(["../../lib/codemirror"], mod); 1.15 + else // Plain browser env 1.16 + mod(CodeMirror); 1.17 +})(function(CodeMirror) { 1.18 + "use strict"; 1.19 + var WRAP_CLASS = "CodeMirror-activeline"; 1.20 + var BACK_CLASS = "CodeMirror-activeline-background"; 1.21 + 1.22 + CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) { 1.23 + var prev = old && old != CodeMirror.Init; 1.24 + if (val && !prev) { 1.25 + cm.state.activeLines = []; 1.26 + updateActiveLines(cm, cm.listSelections()); 1.27 + cm.on("beforeSelectionChange", selectionChange); 1.28 + } else if (!val && prev) { 1.29 + cm.off("beforeSelectionChange", selectionChange); 1.30 + clearActiveLines(cm); 1.31 + delete cm.state.activeLines; 1.32 + } 1.33 + }); 1.34 + 1.35 + function clearActiveLines(cm) { 1.36 + for (var i = 0; i < cm.state.activeLines.length; i++) { 1.37 + cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS); 1.38 + cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS); 1.39 + } 1.40 + } 1.41 + 1.42 + function sameArray(a, b) { 1.43 + if (a.length != b.length) return false; 1.44 + for (var i = 0; i < a.length; i++) 1.45 + if (a[i] != b[i]) return false; 1.46 + return true; 1.47 + } 1.48 + 1.49 + function updateActiveLines(cm, ranges) { 1.50 + var active = []; 1.51 + for (var i = 0; i < ranges.length; i++) { 1.52 + var line = cm.getLineHandleVisualStart(ranges[i].head.line); 1.53 + if (active[active.length - 1] != line) active.push(line); 1.54 + } 1.55 + if (sameArray(cm.state.activeLines, active)) return; 1.56 + cm.operation(function() { 1.57 + clearActiveLines(cm); 1.58 + for (var i = 0; i < active.length; i++) { 1.59 + cm.addLineClass(active[i], "wrap", WRAP_CLASS); 1.60 + cm.addLineClass(active[i], "background", BACK_CLASS); 1.61 + } 1.62 + cm.state.activeLines = active; 1.63 + }); 1.64 + } 1.65 + 1.66 + function selectionChange(cm, sel) { 1.67 + updateActiveLines(cm, sel.ranges); 1.68 + } 1.69 +});