Skip to content

Commit 5ecd7a2

Browse files
author
Paige Bolduc
committed
Add functionality for continuing lists on 'enter' press
Now, pressing enter after a numbered or bulleted list item will start a new line the begins with another bullet or the next number in the list.
1 parent 97e23df commit 5ecd7a2

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

js/bootstrap-markdown.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,29 @@
856856

857857
case 13: // enter
858858
blocked = false;
859+
var chars = this.getContent().split('');
860+
var enterIndex = this.getSelection().start;
861+
var priorNewlineIndex = -1; // initial line break at before 0 index
862+
863+
// traverse backwards through chars to find last prior line break to check if was num/bullet item
864+
for (var i = enterIndex - 2; i >= 0; i--) {
865+
if (chars[i] === '\n') {
866+
priorNewlineIndex = i;
867+
break;
868+
}
869+
}
870+
871+
var charFollowingLastLineBreak = chars[priorNewlineIndex + 1];
872+
if (charFollowingLastLineBreak === '-') {
873+
this.addBullet(enterIndex);
874+
} else if ($.isNumeric(charFollowingLastLineBreak)) {
875+
var numBullet = this.getBulletNumber(priorNewlineIndex + 1);
876+
if (numBullet) {
877+
this.addNumberedBullet(enterIndex, numBullet);
878+
}
879+
}
859880
break;
881+
860882
case 27: // escape
861883
if (this.$isFullscreen) this.setFullscreen(false);
862884
blocked = false;
@@ -873,6 +895,26 @@
873895

874896
this.$options.onChange(this);
875897
},
898+
insertContent: function(index, content) {
899+
var firstHalf = this.getContent().slice(0, index);
900+
var secondHalf = this.getContent().slice(index + 1);
901+
this.setContent(firstHalf.concat(content).concat(secondHalf));
902+
},
903+
addBullet: function(index) {
904+
this.insertContent(index, '- \n');
905+
this.setSelection(index + 2, index + 2); // Put the cursor after the bullet
906+
},
907+
addNumberedBullet: function(index, num) {
908+
var numBullet = (num + 1) + '. \n';
909+
this.insertContent(index, numBullet);
910+
911+
var prefixLength = num.toString().length + 2;
912+
this.setSelection(index + prefixLength, index + prefixLength); // Put the cursor after the number
913+
},
914+
getBulletNumber: function(startIndex) {
915+
var bulletNum = this.getContent().slice(startIndex).split('.')[0];
916+
return $.isNumeric(bulletNum) ? parseInt(bulletNum) : null;
917+
},
876918
change: function(e) {
877919
this.$options.onChange(this);
878920
return this;

0 commit comments

Comments
 (0)