From 4c9aeb04d46a9d06c29d07de4b20952bb5425ec4 Mon Sep 17 00:00:00 2001 From: Calamari Date: Sun, 2 Feb 2020 13:20:16 +0100 Subject: [PATCH 1/3] Close bullet list on enter of empty bullet line --- js/bootstrap-markdown.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/js/bootstrap-markdown.js b/js/bootstrap-markdown.js index e3f1ca9..5a24d51 100644 --- a/js/bootstrap-markdown.js +++ b/js/bootstrap-markdown.js @@ -880,7 +880,17 @@ var charFollowingLastLineBreak = chars[priorNewlineIndex + 1]; if (charFollowingLastLineBreak === '-') { - this.addBullet(enterIndex); + var line = chars.slice(priorNewlineIndex + 2, enterIndex).join(''); + var allWhitespace = /^[\W]*$/.test(line); + if (allWhitespace) { + // If we hit enter on a empty line, we probably want to close the bullet list + var linesDeleted = line.length + 2; + this.setSelection(priorNewlineIndex, enterIndex); + this.replaceSelection("\n\n"); + this.setSelection(enterIndex + 2 - linesDeleted, enterIndex + 2 - linesDeleted); // Put the cursor into new line + } else { + this.addBullet(enterIndex); + } } else if ($.isNumeric(charFollowingLastLineBreak)) { var numBullet = this.getBulletNumber(priorNewlineIndex + 1); if (numBullet) { From 392cc5dc3e07f216699e243037909c28f9b0bb2f Mon Sep 17 00:00:00 2001 From: Calamari Date: Sun, 2 Feb 2020 13:21:43 +0100 Subject: [PATCH 2/3] If we hit enter on an empty numberd list close it --- js/bootstrap-markdown.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/js/bootstrap-markdown.js b/js/bootstrap-markdown.js index 5a24d51..55a14eb 100644 --- a/js/bootstrap-markdown.js +++ b/js/bootstrap-markdown.js @@ -892,10 +892,20 @@ this.addBullet(enterIndex); } } else if ($.isNumeric(charFollowingLastLineBreak)) { - var numBullet = this.getBulletNumber(priorNewlineIndex + 1); - if (numBullet) { + var numBullet = this.getBulletNumber(priorNewlineIndex + 1); + if (numBullet) { + var line = chars.slice(priorNewlineIndex + 1, enterIndex).join(''); + var allWhitespace = (new RegExp('^' + numBullet + '\\.[\\W]*$')).test(line); + if (allWhitespace) { + // If we hit enter on a empty line, we probably want to close the numbered bullet list + var linesDeleted = line.length; + this.setSelection(priorNewlineIndex, enterIndex); + this.replaceSelection("\n\n"); + this.setSelection(enterIndex + 1 - linesDeleted, enterIndex + 1 - linesDeleted); // Put the cursor into new line + } else { this.addNumberedBullet(enterIndex, numBullet); } + } } break; From 313cc2eb4b13ea7fcbef79ca86b084bf579941e4 Mon Sep 17 00:00:00 2001 From: Calamari Date: Sun, 2 Feb 2020 13:24:35 +0100 Subject: [PATCH 3/3] Fix caret position after auto adding `10.` --- js/bootstrap-markdown.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/bootstrap-markdown.js b/js/bootstrap-markdown.js index 55a14eb..f47a9eb 100644 --- a/js/bootstrap-markdown.js +++ b/js/bootstrap-markdown.js @@ -938,7 +938,7 @@ var numBullet = (num + 1) + '. \n'; this.insertContent(index, numBullet); - var prefixLength = num.toString().length + 2; + var prefixLength = (num + 1).toString().length + 2; this.setSelection(index + prefixLength, index + prefixLength); // Put the cursor after the number }, getBulletNumber: function(startIndex) {