browser/devtools/sourceeditor/codemirror/activeline.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Because sometimes you need to style the cursor's line.
michael@0 2 //
michael@0 3 // Adds an option 'styleActiveLine' which, when enabled, gives the
michael@0 4 // active line's wrapping <div> the CSS class "CodeMirror-activeline",
michael@0 5 // and gives its background <div> the class "CodeMirror-activeline-background".
michael@0 6
michael@0 7 (function(mod) {
michael@0 8 if (typeof exports == "object" && typeof module == "object") // CommonJS
michael@0 9 mod(require("../../lib/codemirror"));
michael@0 10 else if (typeof define == "function" && define.amd) // AMD
michael@0 11 define(["../../lib/codemirror"], mod);
michael@0 12 else // Plain browser env
michael@0 13 mod(CodeMirror);
michael@0 14 })(function(CodeMirror) {
michael@0 15 "use strict";
michael@0 16 var WRAP_CLASS = "CodeMirror-activeline";
michael@0 17 var BACK_CLASS = "CodeMirror-activeline-background";
michael@0 18
michael@0 19 CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
michael@0 20 var prev = old && old != CodeMirror.Init;
michael@0 21 if (val && !prev) {
michael@0 22 cm.state.activeLines = [];
michael@0 23 updateActiveLines(cm, cm.listSelections());
michael@0 24 cm.on("beforeSelectionChange", selectionChange);
michael@0 25 } else if (!val && prev) {
michael@0 26 cm.off("beforeSelectionChange", selectionChange);
michael@0 27 clearActiveLines(cm);
michael@0 28 delete cm.state.activeLines;
michael@0 29 }
michael@0 30 });
michael@0 31
michael@0 32 function clearActiveLines(cm) {
michael@0 33 for (var i = 0; i < cm.state.activeLines.length; i++) {
michael@0 34 cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS);
michael@0 35 cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS);
michael@0 36 }
michael@0 37 }
michael@0 38
michael@0 39 function sameArray(a, b) {
michael@0 40 if (a.length != b.length) return false;
michael@0 41 for (var i = 0; i < a.length; i++)
michael@0 42 if (a[i] != b[i]) return false;
michael@0 43 return true;
michael@0 44 }
michael@0 45
michael@0 46 function updateActiveLines(cm, ranges) {
michael@0 47 var active = [];
michael@0 48 for (var i = 0; i < ranges.length; i++) {
michael@0 49 var line = cm.getLineHandleVisualStart(ranges[i].head.line);
michael@0 50 if (active[active.length - 1] != line) active.push(line);
michael@0 51 }
michael@0 52 if (sameArray(cm.state.activeLines, active)) return;
michael@0 53 cm.operation(function() {
michael@0 54 clearActiveLines(cm);
michael@0 55 for (var i = 0; i < active.length; i++) {
michael@0 56 cm.addLineClass(active[i], "wrap", WRAP_CLASS);
michael@0 57 cm.addLineClass(active[i], "background", BACK_CLASS);
michael@0 58 }
michael@0 59 cm.state.activeLines = active;
michael@0 60 });
michael@0 61 }
michael@0 62
michael@0 63 function selectionChange(cm, sel) {
michael@0 64 updateActiveLines(cm, sel.ranges);
michael@0 65 }
michael@0 66 });

mercurial