continuelist.js 1.7 KB

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