tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

foldgutter.js (4825B)


      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("resource://devtools/client/shared/sourceeditor/codemirror/lib/codemirror.js"), require("resource://devtools/client/shared/sourceeditor/codemirror/addon/fold/foldcode.js"));
      7  else if (typeof define == "function" && define.amd) // AMD
      8    define(["../../lib/codemirror", "./foldcode"], mod);
      9  else // Plain browser env
     10    mod(CodeMirror);
     11 })(function(CodeMirror) {
     12  "use strict";
     13 
     14  CodeMirror.defineOption("foldGutter", false, function(cm, val, old) {
     15    if (old && old != CodeMirror.Init) {
     16      cm.clearGutter(cm.state.foldGutter.options.gutter);
     17      cm.state.foldGutter = null;
     18      cm.off("gutterClick", onGutterClick);
     19      cm.off("changes", onChange);
     20      cm.off("viewportChange", onViewportChange);
     21      cm.off("fold", onFold);
     22      cm.off("unfold", onFold);
     23      cm.off("swapDoc", onChange);
     24    }
     25    if (val) {
     26      cm.state.foldGutter = new State(parseOptions(val));
     27      updateInViewport(cm);
     28      cm.on("gutterClick", onGutterClick);
     29      cm.on("changes", onChange);
     30      cm.on("viewportChange", onViewportChange);
     31      cm.on("fold", onFold);
     32      cm.on("unfold", onFold);
     33      cm.on("swapDoc", onChange);
     34    }
     35  });
     36 
     37  var Pos = CodeMirror.Pos;
     38 
     39  function State(options) {
     40    this.options = options;
     41    this.from = this.to = 0;
     42  }
     43 
     44  function parseOptions(opts) {
     45    if (opts === true) opts = {};
     46    if (opts.gutter == null) opts.gutter = "CodeMirror-foldgutter";
     47    if (opts.indicatorOpen == null) opts.indicatorOpen = "CodeMirror-foldgutter-open";
     48    if (opts.indicatorFolded == null) opts.indicatorFolded = "CodeMirror-foldgutter-folded";
     49    return opts;
     50  }
     51 
     52  function isFolded(cm, line) {
     53    var marks = cm.findMarks(Pos(line, 0), Pos(line + 1, 0));
     54    for (var i = 0; i < marks.length; ++i) {
     55      if (marks[i].__isFold) {
     56        var fromPos = marks[i].find(-1);
     57        if (fromPos && fromPos.line === line)
     58          return marks[i];
     59      }
     60    }
     61  }
     62 
     63  function marker(spec) {
     64    if (typeof spec == "string") {
     65      var elt = document.createElement("div");
     66      elt.className = spec + " CodeMirror-guttermarker-subtle";
     67      return elt;
     68    } else {
     69      return spec.cloneNode(true);
     70    }
     71  }
     72 
     73  function updateFoldInfo(cm, from, to) {
     74    var opts = cm.state.foldGutter.options, cur = from;
     75    var minSize = cm.foldOption(opts, "minFoldSize");
     76    var func = cm.foldOption(opts, "rangeFinder");
     77    cm.eachLine(from, to, function(line) {
     78      var mark = null;
     79      if (isFolded(cm, cur)) {
     80        mark = marker(opts.indicatorFolded);
     81      } else {
     82        var pos = Pos(cur, 0);
     83        var range = func && func(cm, pos);
     84        if (range && range.to.line - range.from.line >= minSize)
     85          mark = marker(opts.indicatorOpen);
     86      }
     87      cm.setGutterMarker(line, opts.gutter, mark);
     88      ++cur;
     89    });
     90  }
     91 
     92  function updateInViewport(cm) {
     93    var vp = cm.getViewport(), state = cm.state.foldGutter;
     94    if (!state) return;
     95    cm.operation(function() {
     96      updateFoldInfo(cm, vp.from, vp.to);
     97    });
     98    state.from = vp.from; state.to = vp.to;
     99  }
    100 
    101  function onGutterClick(cm, line, gutter) {
    102    var state = cm.state.foldGutter;
    103    if (!state) return;
    104    var opts = state.options;
    105    if (gutter != opts.gutter) return;
    106    var folded = isFolded(cm, line);
    107    if (folded) folded.clear();
    108    else cm.foldCode(Pos(line, 0), opts);
    109  }
    110 
    111  function onChange(cm) {
    112    var state = cm.state.foldGutter;
    113    if (!state) return;
    114    var opts = state.options;
    115    state.from = state.to = 0;
    116    clearTimeout(state.changeUpdate);
    117    state.changeUpdate = setTimeout(function() { updateInViewport(cm); }, opts.foldOnChangeTimeSpan || 600);
    118  }
    119 
    120  function onViewportChange(cm) {
    121    var state = cm.state.foldGutter;
    122    if (!state) return;
    123    var opts = state.options;
    124    clearTimeout(state.changeUpdate);
    125    state.changeUpdate = setTimeout(function() {
    126      var vp = cm.getViewport();
    127      if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
    128        updateInViewport(cm);
    129      } else {
    130        cm.operation(function() {
    131          if (vp.from < state.from) {
    132            updateFoldInfo(cm, vp.from, state.from);
    133            state.from = vp.from;
    134          }
    135          if (vp.to > state.to) {
    136            updateFoldInfo(cm, state.to, vp.to);
    137            state.to = vp.to;
    138          }
    139        });
    140      }
    141    }, opts.updateViewportTimeSpan || 400);
    142  }
    143 
    144  function onFold(cm, from) {
    145    var state = cm.state.foldGutter;
    146    if (!state) return;
    147    var line = from.line;
    148    if (line >= state.from && line < state.to)
    149      updateFoldInfo(cm, line, line + 1);
    150  }
    151 });