|
856 | 856 |
|
857 | 857 | case 13: // enter
|
858 | 858 | 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 | + } |
859 | 880 | break;
|
| 881 | + |
860 | 882 | case 27: // escape
|
861 | 883 | if (this.$isFullscreen) this.setFullscreen(false);
|
862 | 884 | blocked = false;
|
|
873 | 895 |
|
874 | 896 | this.$options.onChange(this);
|
875 | 897 | },
|
| 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 | + }, |
876 | 918 | change: function(e) {
|
877 | 919 | this.$options.onChange(this);
|
878 | 920 | return this;
|
|
0 commit comments