continuelist.js 1.8 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*)(>[> ]*|- \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/,
  13. emptyListRE = /^(\s*)(>[> ]*|- \[[x ]\]|[*+-]|(\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;
  20. var eolState = cm.getStateAfter(pos.line);
  21. var inList = eolState.list !== false;
  22. var inQuote = eolState.quote !== 0;
  23. var line = cm.getLine(pos.line), match = listRE.exec(line);
  24. if (!ranges[i].empty() || (!inList && !inQuote) || !match) {
  25. cm.execCommand("newlineAndIndent");
  26. return;
  27. }
  28. if (emptyListRE.test(line)) {
  29. cm.replaceRange("", {
  30. line: pos.line, ch: 0
  31. }, {
  32. line: pos.line, ch: pos.ch + 1
  33. });
  34. replacements[i] = "\n";
  35. } else {
  36. var indent = match[1], after = match[5];
  37. var bullet = unorderedListRE.test(match[2]) || match[2].indexOf(">") >= 0
  38. ? match[2].replace("x", " ")
  39. : (parseInt(match[3], 10) + 1) + match[4];
  40. replacements[i] = "\n" + indent + bullet + after;
  41. }
  42. }
  43. cm.replaceSelections(replacements);
  44. };
  45. });