2
0
mirror of https://github.com/Ionaru/easy-markdown-editor synced 2025-07-31 13:54:28 -06:00

Huge improvement to tabbing

Now respects tab size, Now respects indent with tab, Now allows tabbing
lists, Now allows shift-tabbing lists
This commit is contained in:
Wes Cossick 2015-06-26 14:44:39 -05:00
parent 6c91d90763
commit 21f3424822

View File

@ -1,51 +1,93 @@
// CodeMirror, copyright (c) by Marijn Haverbeke and others // CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE // Distributed under an MIT license: http://codemirror.net/LICENSE
(function(mod) { (function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror")); mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod); define(["../../lib/codemirror"], mod);
else // Plain browser env else // Plain browser env
mod(CodeMirror); mod(CodeMirror);
})(function(CodeMirror) { })(function(CodeMirror) {
"use strict"; "use strict";
var listRE = /^(\s*)(>[> ]*|[*+-]\s|(\d+)([.)]))(\s*)/, var listRE = /^(\s*)(>[> ]*|[*+-]\s|(\d+)([.)]))(\s*)/,
emptyListRE = /^(\s*)(>[> ]*|[*+-]|(\d+)[.)])(\s*)$/, emptyListRE = /^(\s*)(>[> ]*|[*+-]|(\d+)[.)])(\s*)$/,
unorderedListRE = /[*+-]\s/; unorderedListRE = /[*+-]\s/;
CodeMirror.commands.newlineAndIndentContinueMarkdownList = function(cm) { CodeMirror.commands.newlineAndIndentContinueMarkdownList = function(cm) {
if (cm.getOption("disableInput")) return CodeMirror.Pass; if (cm.getOption("disableInput")) return CodeMirror.Pass;
var ranges = cm.listSelections(), replacements = []; var ranges = cm.listSelections(),
for (var i = 0; i < ranges.length; i++) { replacements = [];
var pos = ranges[i].head; for (var i = 0; i < ranges.length; i++) {
var eolState = cm.getStateAfter(pos.line); var pos = ranges[i].head;
var inList = eolState.list !== false; var eolState = cm.getStateAfter(pos.line);
var inQuote = eolState.quote !== 0; var inList = eolState.list !== false;
var inQuote = eolState.quote !== 0;
var line = cm.getLine(pos.line), match = listRE.exec(line); var line = cm.getLine(pos.line),
if (!ranges[i].empty() || (!inList && !inQuote) || !match) { match = listRE.exec(line);
cm.execCommand("newlineAndIndent"); if (!ranges[i].empty() || (!inList && !inQuote) || !match) {
return; cm.execCommand("newlineAndIndent");
} return;
if (emptyListRE.test(line)) { }
cm.replaceRange("", { if (emptyListRE.test(line)) {
line: pos.line, ch: 0 cm.replaceRange("", {
}, { line: pos.line,
line: pos.line, ch: pos.ch + 1 ch: 0
}); }, {
replacements[i] = "\n"; line: pos.line,
} else { ch: pos.ch + 1
var indent = match[1], after = match[5]; });
var bullet = unorderedListRE.test(match[2]) || match[2].indexOf(">") >= 0 replacements[i] = "\n";
? match[2] } else {
: (parseInt(match[3], 10) + 1) + match[4]; var indent = match[1],
after = match[5];
var bullet = unorderedListRE.test(match[2]) || match[2].indexOf(">") >= 0 ? match[2] : (parseInt(match[3], 10) + 1) + match[4];
replacements[i] = "\n" + indent + bullet + after; replacements[i] = "\n" + indent + bullet + after;
} }
} }
cm.replaceSelections(replacements); cm.replaceSelections(replacements);
}; };
CodeMirror.commands.shiftTabAndIndentContinueMarkdownList = function(cm) {
var ranges = cm.listSelections();
var pos = ranges[0].head;
var eolState = cm.getStateAfter(pos.line);
var inList = eolState.list !== false;
if (inList) {
cm.execCommand('indentLess');
return;
}
if(cm.options.indentWithTabs){
cm.execCommand('insertTab');
}
else{
var spaces = Array(cm.options.tabSize + 1).join(" ");
cm.replaceSelection(spaces);
}
};
CodeMirror.commands.tabAndIndentContinueMarkdownList = function(cm) {
var ranges = cm.listSelections();
var pos = ranges[0].head;
var eolState = cm.getStateAfter(pos.line);
var inList = eolState.list !== false;
if (inList) {
cm.execCommand('indentMore');
return;
}
if(cm.options.indentWithTabs){
cm.execCommand('insertTab');
}
else{
var spaces = Array(cm.options.tabSize + 1).join(" ");
cm.replaceSelection(spaces);
}
};
}); });