|
167 | 167 | this.$textarea.css('resize', this.$options.resize);
|
168 | 168 | }
|
169 | 169 |
|
| 170 | + // Re-attach markdown data |
| 171 | + this.$textarea.data('markdown', this); |
| 172 | + }, |
| 173 | + __setEventListeners: function() { |
170 | 174 | this.$textarea.on({
|
171 | 175 | 'focus': $.proxy(this.focus, this),
|
172 | 176 | 'keyup': $.proxy(this.keyup, this),
|
|
181 | 185 | if (this.eventSupported('keypress')) {
|
182 | 186 | this.$textarea.on('keypress', $.proxy(this.keypress, this));
|
183 | 187 | }
|
184 |
| - |
185 |
| - // Re-attach markdown data |
186 |
| - this.$textarea.data('markdown', this); |
187 | 188 | },
|
188 | 189 | __handle: function(e) {
|
189 | 190 | var target = $(e.currentTarget),
|
|
418 | 419 | this.$oldContent = this.getContent();
|
419 | 420 |
|
420 | 421 | this.__setListener();
|
| 422 | + this.__setEventListeners(); |
421 | 423 |
|
422 | 424 | // Set editor attributes, data short-hand API and listener
|
423 | 425 | this.$editor.attr('id', (new Date()).getTime());
|
|
856 | 858 |
|
857 | 859 | case 13: // enter
|
858 | 860 | 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 | + } |
859 | 882 | break;
|
| 883 | + |
860 | 884 | case 27: // escape
|
861 | 885 | if (this.$isFullscreen) this.setFullscreen(false);
|
862 | 886 | blocked = false;
|
|
873 | 897 |
|
874 | 898 | this.$options.onChange(this);
|
875 | 899 | },
|
| 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 | + }, |
876 | 920 | change: function(e) {
|
877 | 921 | this.$options.onChange(this);
|
878 | 922 | return this;
|
|
0 commit comments