continuelist.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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*)([*+-]|(\d+)\.)(\s+)/,
  13. unorderedBullets = "*+-";
  14. CodeMirror.commands.newlineAndIndentContinueMarkdownList = function(cm) {
  15. if (cm.getOption("disableInput")) return CodeMirror.Pass;
  16. var ranges = cm.listSelections(), replacements = [];
  17. for (var i = 0; i < ranges.length; i++) {
  18. var pos = ranges[i].head, match;
  19. var inList = cm.getStateAfter(pos.line).list !== false;
  20. if (!ranges[i].empty() || !inList || !(match = cm.getLine(pos.line).match(listRE))) {
  21. cm.execCommand("newlineAndIndent");
  22. return;
  23. }
  24. var indent = match[1], after = match[4];
  25. var bullet = unorderedBullets.indexOf(match[2]) >= 0
  26. ? match[2]
  27. : (parseInt(match[3], 10) + 1) + ".";
  28. replacements[i] = "\n" + indent + bullet + after;
  29. }
  30. cm.replaceSelections(replacements);
  31. };
  32. });