index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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("codemirror/lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["codemirror/lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var Pos = CodeMirror.Pos;
  13. var listTokenRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
  14. function matchListToken(pos, cm) {
  15. /* Get some info about the current state */
  16. var eolState = cm.getStateAfter(pos.line);
  17. var inList = eolState.list !== false;
  18. var inQuote = eolState.quote !== 0;
  19. /* Get the line from the start to where the cursor currently is */
  20. var lineStart = cm.getRange(Pos(pos.line, 0), pos);
  21. /* Matches the beginning of the list line with the list token RE */
  22. var match = listTokenRE.exec(lineStart);
  23. /* Not being in a list, or being in a list but not right after the list
  24. * token, are both not considered a match */
  25. if ((!inList && !inQuote) || !match)
  26. return false
  27. else
  28. return true
  29. }
  30. CodeMirror.commands.autoIndentMarkdownList = function(cm) {
  31. if (cm.getOption("disableInput")) return CodeMirror.Pass;
  32. var ranges = cm.listSelections();
  33. for (var i = 0; i < ranges.length; i++) {
  34. var pos = ranges[i].head;
  35. if (!ranges[i].empty() || !matchListToken(pos, cm)) {
  36. /* If no match, call regular Tab handler */
  37. cm.execCommand("defaultTab");
  38. return;
  39. }
  40. /* Select the whole list line and indent it by one unit */
  41. cm.indentLine(pos.line, "add");
  42. }
  43. };
  44. CodeMirror.commands.autoUnindentMarkdownList = function(cm) {
  45. if (cm.getOption("disableInput")) return CodeMirror.Pass;
  46. var ranges = cm.listSelections();
  47. for (var i = 0; i < ranges.length; i++) {
  48. var pos = ranges[i].head;
  49. if (!ranges[i].empty() || !matchListToken(pos, cm)) {
  50. /* If no match, call regular Shift-Tab handler */
  51. cm.execCommand("indentAuto");
  52. return;
  53. }
  54. /* Select the whole list line and unindent it by one unit */
  55. cm.indentLine(pos.line, "subtract");
  56. }
  57. };
  58. });