Skip to content

Commit

Permalink
fix empty line wrongly being inserted in doCode in certain cases; imp…
Browse files Browse the repository at this point in the history
…rove Shift-Enter behavior; improve doCode behavior when working on a single line
  • Loading branch information
balpha committed Mar 2, 2012
1 parent 6d4e5c7 commit eb24a60
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Markdown.Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1733,11 +1733,24 @@
// at the current indent level.
commandProto.doAutoindent = function (chunk, postProcessing) {

var commandMgr = this;
var commandMgr = this,
fakeSelection = false;

chunk.before = chunk.before.replace(/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]*\n$/, "\n\n");
chunk.before = chunk.before.replace(/(\n|^)[ ]{0,3}>[ \t]*\n$/, "\n\n");
chunk.before = chunk.before.replace(/(\n|^)[ \t]+\n$/, "\n\n");

// There's no selection, end the cursor wasn't at the end of the line:
// The user wants to split the current list item / code line / blockquote line
// (for the latter it doesn't really matter) in two. Temporarily select the
// (rest of the) line to achieve this.
if (!chunk.selection && !/^[ \t]*(?:\n|$)/.test(chunk.after)) {
chunk.after = chunk.after.replace(/^[^\n]*/, function (wholeMatch) {
chunk.selection = wholeMatch;
return "";
});
fakeSelection = true;
}

if (/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]+.*\n$/.test(chunk.before)) {
if (commandMgr.doList) {
Expand All @@ -1754,6 +1767,11 @@
commandMgr.doCode(chunk);
}
}

if (fakeSelection) {
chunk.after = chunk.selection + chunk.after;
chunk.selection = "";
}
};

commandProto.doBlockquote = function (chunk, postProcessing) {
Expand Down Expand Up @@ -1918,7 +1936,7 @@
var nLinesBack = 1;
var nLinesForward = 1;

if (/\n(\t|[ ]{4,}).*\n$/.test(chunk.before)) {
if (/(\n|^)(\t|[ ]{4,}).*\n$/.test(chunk.before)) {
nLinesBack = 0;
}
if (/^\n(\t|[ ]{4,})/.test(chunk.after)) {
Expand All @@ -1933,7 +1951,10 @@
}
else {
if (/^[ ]{0,3}\S/m.test(chunk.selection)) {
chunk.selection = chunk.selection.replace(/^/gm, " ");
if (/\n/.test(chunk.selection))
chunk.selection = chunk.selection.replace(/^/gm, " ");
else // if it's not multiline, do not select the four added spaces; this is more consistent with the doList behavior
chunk.before += " ";
}
else {
chunk.selection = chunk.selection.replace(/^[ ]{4}/gm, "");
Expand Down

0 comments on commit eb24a60

Please sign in to comment.