{"size": 3945, "ext": "js", "max_stars_repo_path": "editor/textarea/colorful.js", "max_stars_repo_name": "HerbertHe/jaxedit", "max_stars_repo_stars_event_min_datetime": "2015-01-01T05:50:20.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-16T13:01:27.000Z", "max_issues_repo_path": "editor/textarea/colorful.js", "max_issues_repo_name": "HerbertHe/jaxedit", "max_issues_repo_issues_event_min_datetime": "2015-05-22T05:31:43.000Z", "max_issues_repo_issues_event_max_datetime": "2019-03-27T04:42:34.000Z", "max_forks_repo_path": "editor/textarea/colorful.js", "max_forks_repo_name": "HerbertHe/jaxedit", "max_forks_repo_forks_event_min_datetime": "2015-02-03T15:11:31.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-10T11:19:38.000Z"}
{"max_stars_count": 137, "max_issues_count": 7, "max_forks_count": 48, "avg_line_length": 33.1512605042, "max_line_length": 131, "alphanum_fraction": 0.6248415716}

/* JaxEdit: online LaTeX editor with live preview
 * Copyright (c) 2011-2017 JaxEdit project
 * License: GNU Lesser General Public License, Version 3
 * Source:  https://github.com/zohooo/jaxedit
 */

jaxedit.doChange = function(editor, change) {
  console.log(change.from, change.to, change.text, change.next);
  var data = this.textdata;
  data.oldtextvalue = data.newtextvalue;
  data.oldtextsize  = data.newtextsize;
  var textvalue = editor.getValue(),
      textsize = textvalue.length;
  var delstart, delend, deltext, instext, start;
  delstart = editor.indexFromPos(change.from);
  if (change.next) {
    do {
      change = change.next;
      start = editor.indexFromPos(change.from);
      if (start < delstart) delstart = start;
    } while (change.next);
    delend = data.oldtextsize;
    while (textvalue.charAt(delend-1) == data.oldtextvalue.charAt(delend-1) && delstart < delend){
      delend -= 1;
    }
    instext = textvalue.substring(delstart, delend + textsize - data.oldtextsize);
  } else {
    instext = change.text.join('\n');
    delend = delstart + data.oldtextsize + instext.length - textsize;
  }
  deltext = data.oldtextvalue.substring(delstart, delend);
  console.log(delstart, delend, deltext, instext, textsize);
  this.childs.lbot.innerHTML = "size: " + textsize + "; oldsize: " + data.oldtextsize + "; change: " + delstart + " to " + delend;
  data.newtextvalue = textvalue;
  data.newtextsize = textsize;

  if (window.localStorage) {
    //IE8 sometimes crashes when writing empty value to a localStorage item
    if (textvalue != "") {
      localStorage.setItem("texcode", textvalue);
    } else {
      localStorage.removeItem("texcode");
    }
  }

  jaxedit.setScrollers(textsize, delstart, editor.getScrollInfo().top);
  if (change.origin !== "setValue") {
    typejax.updater.inQueue(delstart, delend, deltext, instext, textsize, jaxedit.childs.showarea);
  }
};

jaxedit.addEditor = function() {
  this.addOption();
  this.editor = CodeMirror.fromTextArea(this.childs.codearea, {
    lineNumbers: true,
    lineWrapping: true,
    autoCloseEnvs: true,
    matchBrackets: true
  });

  this.editor.setReadOnly = function(bool) {
    jaxedit.editor.readOnly = bool;
  };

  this.doResize();
};

jaxedit.addHandler = function() {
  var codearea = this.childs.codearea,
      showarea = this.childs.showarea;

  this.editor.on('change', function(editor, change) {jaxedit.doChange(editor, change);});

  this.editor.on('scroll', function(editor) {
    if (window.localStorage) {
      localStorage.setItem("scroll", editor.getScrollInfo().top);
    }
    jaxedit.doScroll(true);
  });

  showarea.onscroll = function() {
    jaxedit.doScroll(false);
  };
};

jaxedit.addOption = function() {
  CodeMirror.defineOption('autoCloseEnvs', false, function(cm, val) {
    if (val) {
      var map = {name: 'autoCloseEnvs'};
      map["'}'"] = function(cm) { autoCloseEnv(cm, '}'); };
      cm.addKeyMap(map);
    } else {
      cm.removeKeyMap('autoCloseEnvs');
    }
  });

  function autoCloseEnv(cm, key) {
    var pos = cm.getCursor(true), tok = cm.getTokenAt(pos);
    var inner = CodeMirror.innerMode(cm.getMode(), tok.state);
    if (inner.mode.name != 'stex') return CodeMirror.Pass;

    if (key == "}" /*&& tok.type == 'atom'*/) {
      var env = tok.string, line = pos.line, ch = pos.ch;
      if (tok.end > ch) env = env.slice(0, env.length - tok.end + ch);
      var cmd = cm.getRange({line: line, ch: ch - env.length - 7}, {line: line, ch: ch - env.length});
      var curPos = CodeMirror.Pos(line, ch + 1);
      if (cmd == '\\begin{') {
        cm.replaceSelection('}\n' + '\\end{' + env + '}', {head: curPos, anchor: curPos});
      } else {
        cm.replaceSelection('}', {head: curPos, anchor: curPos});
      }
      return;
    }

    return CodeMirror.Pass;
  }
};
