continuelist.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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. // If we're not in Markdown mode, fall back to normal newlineAndIndent
  21. var eolState = cm.getStateAfter(pos.line);
  22. var inner = CodeMirror.innerMode(cm.getMode(), eolState);
  23. if (inner.mode.name !== "markdown") {
  24. cm.execCommand("newlineAndIndent");
  25. return;
  26. } else {
  27. eolState = inner.state;
  28. }
  29. var inList = eolState.list !== false;
  30. var inQuote = eolState.quote !== 0;
  31. var line = cm.getLine(pos.line), match = listRE.exec(line);
  32. var cursorBeforeBullet = /^\s*$/.test(line.slice(0, pos.ch));
  33. if (!ranges[i].empty() || (!inList && !inQuote) || !match || cursorBeforeBullet) {
  34. cm.execCommand("newlineAndIndent");
  35. return;
  36. }
  37. if (emptyListRE.test(line)) {
  38. var endOfQuote = inQuote && />\s*$/.test(line)
  39. var endOfList = !/>\s*$/.test(line)
  40. if (endOfQuote || endOfList) cm.replaceRange("", {
  41. line: pos.line, ch: 0
  42. }, {
  43. line: pos.line, ch: pos.ch + 1
  44. });
  45. replacements[i] = "\n";
  46. } else {
  47. var indent = match[1], after = match[5];
  48. var numbered = !(unorderedListRE.test(match[2]) || match[2].indexOf(">") >= 0);
  49. var bullet = numbered ? (parseInt(match[3], 10) + 1) + match[4] : match[2].replace("x", " ");
  50. replacements[i] = "\n" + indent + bullet + after;
  51. if (numbered) incrementRemainingMarkdownListNumbers(cm, pos);
  52. }
  53. }
  54. cm.replaceSelections(replacements);
  55. };
  56. // Auto-updating Markdown list numbers when a new item is added to the
  57. // middle of a list
  58. function incrementRemainingMarkdownListNumbers(cm, pos) {
  59. var startLine = pos.line, lookAhead = 0, skipCount = 0;
  60. var startItem = listRE.exec(cm.getLine(startLine)), startIndent = startItem[1];
  61. do {
  62. lookAhead += 1;
  63. var nextLineNumber = startLine + lookAhead;
  64. var nextLine = cm.getLine(nextLineNumber), nextItem = listRE.exec(nextLine);
  65. if (nextItem) {
  66. var nextIndent = nextItem[1];
  67. var newNumber = (parseInt(startItem[3], 10) + lookAhead - skipCount);
  68. var nextNumber = (parseInt(nextItem[3], 10)), itemNumber = nextNumber;
  69. if (startIndent === nextIndent && !isNaN(nextNumber)) {
  70. if (newNumber === nextNumber) itemNumber = nextNumber + 1;
  71. if (newNumber > nextNumber) itemNumber = newNumber + 1;
  72. cm.replaceRange(
  73. nextLine.replace(listRE, nextIndent + itemNumber + nextItem[4] + nextItem[5]),
  74. {
  75. line: nextLineNumber, ch: 0
  76. }, {
  77. line: nextLineNumber, ch: nextLine.length
  78. });
  79. } else {
  80. if (startIndent.length > nextIndent.length) return;
  81. // This doesn't run if the next line immediately indents, as it is
  82. // not clear of the users intention (new indented item or same level)
  83. if ((startIndent.length < nextIndent.length) && (lookAhead === 1)) return;
  84. skipCount += 1;
  85. }
  86. }
  87. } while (nextItem);
  88. }
  89. });