Blame view

build/lib/codemirror/addon/edit/continuelist.js 1.71 KB
5a739853   patrick.he   commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  // CodeMirror, copyright (c) by Marijn Haverbeke and others
  // Distributed under an MIT license: http://codemirror.net/LICENSE
  
  (function(mod) {
    if (typeof exports == "object" && typeof module == "object") // CommonJS
      mod(require("../../lib/codemirror"));
    else if (typeof define == "function" && define.amd) // AMD
      define(["../../lib/codemirror"], mod);
    else // Plain browser env
      mod(CodeMirror);
  })(function(CodeMirror) {
    "use strict";
  
    var listRE = /^(\s*)([> ]+|[*+-]|(\d+)\.)(\s+)/,
        emptyListRE = /^(\s*)([> ]+|[*+-]|(\d+)\.)(\s*)$/,
        unorderedBullets = "*+-";
  
    CodeMirror.commands.newlineAndIndentContinueMarkdownList = function(cm) {
      if (cm.getOption("disableInput")) return CodeMirror.Pass;
      var ranges = cm.listSelections(), replacements = [];
      for (var i = 0; i < ranges.length; i++) {
        var pos = ranges[i].head, match;
        var eolState = cm.getStateAfter(pos.line);
        var inList = eolState.list !== false;
        var inQuote = eolState.quote !== false;
  
        if (!ranges[i].empty() || (!inList && !inQuote) || !(match = cm.getLine(pos.line).match(listRE))) {
          cm.execCommand("newlineAndIndent");
          return;
        }
        if (cm.getLine(pos.line).match(emptyListRE)) {
          cm.replaceRange("", {
            line: pos.line, ch: 0
          }, {
            line: pos.line, ch: pos.ch + 1
          });
          replacements[i] = "\n";
  
        } else {
          var indent = match[1], after = match[4];
          var bullet = unorderedBullets.indexOf(match[2]) >= 0 || match[2].indexOf(">") >= 0
            ? match[2]
            : (parseInt(match[3], 10) + 1) + ".";
  
          replacements[i] = "\n" + indent + bullet + after;
        }
      }
  
      cm.replaceSelections(replacements);
    };
  });