continuelist.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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. var cursorBeforeBullet = /^\s*$/.test(line.slice(0, pos.ch));
  25. if (!ranges[i].empty() || (!inList && !inQuote) || !match || cursorBeforeBullet) {
  26. cm.execCommand("newlineAndIndent");
  27. return;
  28. }
  29. if (emptyListRE.test(line)) {
  30. if (!/>\s*$/.test(line)) cm.replaceRange("", {
  31. line: pos.line, ch: 0
  32. }, {
  33. line: pos.line, ch: pos.ch + 1
  34. });
  35. replacements[i] = "\n";
  36. } else {
  37. var indent = match[1], after = match[5];
  38. var numbered = !(unorderedListRE.test(match[2]) || match[2].indexOf(">") >= 0);
  39. var bullet = numbered ? (parseInt(match[3], 10) + 1) + match[4] : match[2].replace("x", " ");
  40. replacements[i] = "\n" + indent + bullet + after;
  41. if (numbered) incrementRemainingMarkdownListNumbers(cm, pos);
  42. }
  43. }
  44. cm.replaceSelections(replacements);
  45. };
  46. // Auto-updating Markdown list numbers when a new item is added to the
  47. // middle of a list
  48. function incrementRemainingMarkdownListNumbers(cm, pos) {
  49. var startLine = pos.line, lookAhead = 0, skipCount = 0;
  50. var startItem = listRE.exec(cm.getLine(startLine)), startIndent = startItem[1];
  51. do {
  52. lookAhead += 1;
  53. var nextLineNumber = startLine + lookAhead;
  54. var nextLine = cm.getLine(nextLineNumber), nextItem = listRE.exec(nextLine);
  55. if (nextItem) {
  56. var nextIndent = nextItem[1];
  57. var newNumber = (parseInt(startItem[3], 10) + lookAhead - skipCount);
  58. var nextNumber = (parseInt(nextItem[3], 10)), itemNumber = nextNumber;
  59. if (startIndent === nextIndent && !isNaN(nextNumber)) {
  60. if (newNumber === nextNumber) itemNumber = nextNumber + 1;
  61. if (newNumber > nextNumber) itemNumber = newNumber + 1;
  62. cm.replaceRange(
  63. nextLine.replace(listRE, nextIndent + itemNumber + nextItem[4] + nextItem[5]),
  64. {
  65. line: nextLineNumber, ch: 0
  66. }, {
  67. line: nextLineNumber, ch: nextLine.length
  68. });
  69. } else {
  70. if (startIndent.length > nextIndent.length) return;
  71. // This doesn't run if the next line immediatley indents, as it is
  72. // not clear of the users intention (new indented item or same level)
  73. if ((startIndent.length < nextIndent.length) && (lookAhead === 1)) return;
  74. skipCount += 1;
  75. }
  76. }
  77. } while (nextItem);
  78. }
  79. });