LaravelTest
101 строка · 3.9 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14var listRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/,15emptyListRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/,16unorderedListRE = /[*+-]\s/;17
18CodeMirror.commands.newlineAndIndentContinueMarkdownList = function(cm) {19if (cm.getOption("disableInput")) return CodeMirror.Pass;20var ranges = cm.listSelections(), replacements = [];21for (var i = 0; i < ranges.length; i++) {22var pos = ranges[i].head;23
24// If we're not in Markdown mode, fall back to normal newlineAndIndent25var eolState = cm.getStateAfter(pos.line);26var inner = CodeMirror.innerMode(cm.getMode(), eolState);27if (inner.mode.name !== "markdown") {28cm.execCommand("newlineAndIndent");29return;30} else {31eolState = inner.state;32}33
34var inList = eolState.list !== false;35var inQuote = eolState.quote !== 0;36
37var line = cm.getLine(pos.line), match = listRE.exec(line);38var cursorBeforeBullet = /^\s*$/.test(line.slice(0, pos.ch));39if (!ranges[i].empty() || (!inList && !inQuote) || !match || cursorBeforeBullet) {40cm.execCommand("newlineAndIndent");41return;42}43if (emptyListRE.test(line)) {44var endOfQuote = inQuote && />\s*$/.test(line)45var endOfList = !/>\s*$/.test(line)46if (endOfQuote || endOfList) cm.replaceRange("", {47line: pos.line, ch: 048}, {49line: pos.line, ch: pos.ch + 150});51replacements[i] = "\n";52} else {53var indent = match[1], after = match[5];54var numbered = !(unorderedListRE.test(match[2]) || match[2].indexOf(">") >= 0);55var bullet = numbered ? (parseInt(match[3], 10) + 1) + match[4] : match[2].replace("x", " ");56replacements[i] = "\n" + indent + bullet + after;57
58if (numbered) incrementRemainingMarkdownListNumbers(cm, pos);59}60}61
62cm.replaceSelections(replacements);63};64
65// Auto-updating Markdown list numbers when a new item is added to the66// middle of a list67function incrementRemainingMarkdownListNumbers(cm, pos) {68var startLine = pos.line, lookAhead = 0, skipCount = 0;69var startItem = listRE.exec(cm.getLine(startLine)), startIndent = startItem[1];70
71do {72lookAhead += 1;73var nextLineNumber = startLine + lookAhead;74var nextLine = cm.getLine(nextLineNumber), nextItem = listRE.exec(nextLine);75
76if (nextItem) {77var nextIndent = nextItem[1];78var newNumber = (parseInt(startItem[3], 10) + lookAhead - skipCount);79var nextNumber = (parseInt(nextItem[3], 10)), itemNumber = nextNumber;80
81if (startIndent === nextIndent && !isNaN(nextNumber)) {82if (newNumber === nextNumber) itemNumber = nextNumber + 1;83if (newNumber > nextNumber) itemNumber = newNumber + 1;84cm.replaceRange(85nextLine.replace(listRE, nextIndent + itemNumber + nextItem[4] + nextItem[5]),86{87line: nextLineNumber, ch: 088}, {89line: nextLineNumber, ch: nextLine.length90});91} else {92if (startIndent.length > nextIndent.length) return;93// This doesn't run if the next line immediately indents, as it is94// not clear of the users intention (new indented item or same level)95if ((startIndent.length < nextIndent.length) && (lookAhead === 1)) return;96skipCount += 1;97}98}99} while (nextItem);100}101});102