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