2
0
mirror of https://github.com/Ionaru/easy-markdown-editor synced 2025-08-03 07:12:43 -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,6 +1,5 @@
// 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"));
@ -17,30 +16,33 @@
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(),
replacements = [];
for (var i = 0; i < ranges.length; i++) { for (var i = 0; i < ranges.length; i++) {
var pos = ranges[i].head; var pos = ranges[i].head;
var eolState = cm.getStateAfter(pos.line); var eolState = cm.getStateAfter(pos.line);
var inList = eolState.list !== false; var inList = eolState.list !== false;
var inQuote = eolState.quote !== 0; var inQuote = eolState.quote !== 0;
var line = cm.getLine(pos.line), match = listRE.exec(line); var line = cm.getLine(pos.line),
match = listRE.exec(line);
if (!ranges[i].empty() || (!inList && !inQuote) || !match) { if (!ranges[i].empty() || (!inList && !inQuote) || !match) {
cm.execCommand("newlineAndIndent"); cm.execCommand("newlineAndIndent");
return; return;
} }
if (emptyListRE.test(line)) { if (emptyListRE.test(line)) {
cm.replaceRange("", { cm.replaceRange("", {
line: pos.line, ch: 0 line: pos.line,
ch: 0
}, { }, {
line: pos.line, ch: pos.ch + 1 line: pos.line,
ch: pos.ch + 1
}); });
replacements[i] = "\n"; replacements[i] = "\n";
} else { } else {
var indent = match[1], after = match[5]; var indent = match[1],
var bullet = unorderedListRE.test(match[2]) || match[2].indexOf(">") >= 0 after = match[5];
? match[2] var bullet = unorderedListRE.test(match[2]) || match[2].indexOf(">") >= 0 ? match[2] : (parseInt(match[3], 10) + 1) + match[4];
: (parseInt(match[3], 10) + 1) + match[4];
replacements[i] = "\n" + indent + bullet + after; replacements[i] = "\n" + indent + bullet + after;
} }
@ -48,4 +50,44 @@
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);
}
};
}); });