continuelist.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. var listRE = /^(\s*)([*+-]|(\d+)\.)(\s*)/,
  11. unorderedBullets = "*+-";
  12. CodeMirror.commands.newlineAndIndentContinueMarkdownList = function(cm) {
  13. if (cm.getOption("disableInput")) return CodeMirror.Pass;
  14. var ranges = cm.listSelections(), replacements = [];
  15. for (var i = 0; i < ranges.length; i++) {
  16. var pos = ranges[i].head, match;
  17. var inList = cm.getStateAfter(pos.line).list !== false;
  18. if (!ranges[i].empty() || !inList || !(match = cm.getLine(pos.line).match(listRE))) {
  19. cm.execCommand("newlineAndIndent");
  20. return;
  21. }
  22. var indent = match[1], after = match[4];
  23. var bullet = unorderedBullets.indexOf(match[2]) >= 0
  24. ? match[2]
  25. : (parseInt(match[3], 10) + 1) + ".";
  26. replacements[i] = "\n" + indent + bullet + after;
  27. }
  28. cm.replaceSelections(replacements);
  29. };
  30. });