Skip to content

Commit ae5d620

Browse files
authored
Merge pull request refactory-id#290 from bolducp/master
Add list continuation on 'Enter' press functionality & fix extra event listener bug
2 parents 97e23df + c3907a9 commit ae5d620

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

js/bootstrap-markdown.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@
167167
this.$textarea.css('resize', this.$options.resize);
168168
}
169169

170+
// Re-attach markdown data
171+
this.$textarea.data('markdown', this);
172+
},
173+
__setEventListeners: function() {
170174
this.$textarea.on({
171175
'focus': $.proxy(this.focus, this),
172176
'keyup': $.proxy(this.keyup, this),
@@ -181,9 +185,6 @@
181185
if (this.eventSupported('keypress')) {
182186
this.$textarea.on('keypress', $.proxy(this.keypress, this));
183187
}
184-
185-
// Re-attach markdown data
186-
this.$textarea.data('markdown', this);
187188
},
188189
__handle: function(e) {
189190
var target = $(e.currentTarget),
@@ -418,6 +419,7 @@
418419
this.$oldContent = this.getContent();
419420

420421
this.__setListener();
422+
this.__setEventListeners();
421423

422424
// Set editor attributes, data short-hand API and listener
423425
this.$editor.attr('id', (new Date()).getTime());
@@ -856,7 +858,29 @@
856858

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

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

0 commit comments

Comments
 (0)