@@ -4,6 +4,8 @@ var shortcuts = {
4
4
'Cmd-B' : toggleBold ,
5
5
'Cmd-I' : toggleItalic ,
6
6
'Cmd-K' : drawLink ,
7
+ 'Cmd-H' : toggleHeadingSmaller ,
8
+ 'Cmd-Alt-H' : toggleHeadingBigger ,
7
9
'Cmd-Alt-I' : drawImage ,
8
10
"Cmd-'" : toggleBlockquote ,
9
11
'Cmd-Alt-L' : toggleOrderedList ,
@@ -137,6 +139,21 @@ function toggleBlockquote(editor) {
137
139
_toggleLine ( cm , 'quote' ) ;
138
140
}
139
141
142
+ /**
143
+ * Action for toggling heading size: normal -> h1 -> h2 -> h3 -> h4 -> h5 -> h6 -> normal
144
+ */
145
+ function toggleHeadingSmaller ( editor ) {
146
+ var cm = editor . codemirror ;
147
+ _toggleHeading ( cm , 'smaller' ) ;
148
+ }
149
+
150
+ /**
151
+ * Action for toggling heading size: normal -> h6 -> h5 -> h4 -> h3 -> h2 -> h1 -> normal
152
+ */
153
+ function toggleHeadingBigger ( editor ) {
154
+ var cm = editor . codemirror ;
155
+ _toggleHeading ( cm , 'bigger' ) ;
156
+ }
140
157
141
158
142
159
/**
@@ -269,6 +286,44 @@ function _replaceSelection(cm, active, start, end) {
269
286
}
270
287
271
288
289
+ function _toggleHeading ( cm , direction ) {
290
+ if ( / e d i t o r - p r e v i e w - a c t i v e / . test ( cm . getWrapperElement ( ) . lastChild . className ) )
291
+ return ;
292
+
293
+ var startPoint = cm . getCursor ( 'start' ) ;
294
+ var endPoint = cm . getCursor ( 'end' ) ;
295
+ for ( var i = startPoint . line ; i <= endPoint . line ; i ++ ) {
296
+ ( function ( i ) {
297
+ var text = cm . getLine ( i ) ;
298
+ var currHeadingLevel = text . search ( / [ ^ # ] / ) ;
299
+ if ( currHeadingLevel <= 0 ) {
300
+ if ( direction == 'bigger' ) {
301
+ text = '###### ' + text ;
302
+ } else {
303
+ text = '# ' + text ;
304
+ }
305
+ } else if ( ( currHeadingLevel == 6 && direction == 'smaller' ) || ( currHeadingLevel == 1 && direction == 'bigger' ) ) {
306
+ text = text . substr ( 7 ) ;
307
+ } else {
308
+ if ( direction == 'bigger' ) {
309
+ text = text . substr ( 1 ) ;
310
+ } else {
311
+ text = '#' + text ;
312
+ }
313
+ }
314
+ cm . replaceRange ( text , {
315
+ line : i ,
316
+ ch : 0
317
+ } , {
318
+ line : i ,
319
+ ch : 99999999999999
320
+ } ) ;
321
+ } ) ( i ) ;
322
+ }
323
+ cm . focus ( ) ;
324
+ }
325
+
326
+
272
327
function _toggleLine ( cm , name ) {
273
328
if ( / e d i t o r - p r e v i e w - a c t i v e / . test ( cm . getWrapperElement ( ) . lastChild . className ) )
274
329
return ;
@@ -394,6 +449,11 @@ var toolbar = [{
394
449
action : toggleItalic ,
395
450
className : "fa fa-italic" ,
396
451
title : "Italic (Ctrl+I)" ,
452
+ } , {
453
+ name : "headingSmaller" ,
454
+ action : toggleHeadingSmaller ,
455
+ className : "fa fa-header" ,
456
+ title : "Heading 1-6 (Ctrl+H and Ctrl+Alt+H)" ,
397
457
} ,
398
458
"|" , {
399
459
name : "quote" ,
@@ -736,6 +796,8 @@ SimpleMDE.prototype.value = function(val) {
736
796
SimpleMDE . toggleBold = toggleBold ;
737
797
SimpleMDE . toggleItalic = toggleItalic ;
738
798
SimpleMDE . toggleBlockquote = toggleBlockquote ;
799
+ SimpleMDE . toggleHeadingSmaller = toggleHeadingSmaller ;
800
+ SimpleMDE . toggleHeadingBigger = toggleHeadingBigger ;
739
801
SimpleMDE . toggleCodeBlock = toggleCodeBlock ;
740
802
SimpleMDE . toggleUnorderedList = toggleUnorderedList ;
741
803
SimpleMDE . toggleOrderedList = toggleOrderedList ;
@@ -759,6 +821,12 @@ SimpleMDE.prototype.toggleItalic = function() {
759
821
SimpleMDE . prototype . toggleBlockquote = function ( ) {
760
822
toggleBlockquote ( this ) ;
761
823
} ;
824
+ SimpleMDE . prototype . toggleHeadingSmaller = function ( ) {
825
+ toggleHeadingSmaller ( this ) ;
826
+ } ;
827
+ SimpleMDE . prototype . toggleHeadingBigger = function ( ) {
828
+ toggleHeadingBigger ( this ) ;
829
+ } ;
762
830
SimpleMDE . prototype . toggleCodeBlock = function ( ) {
763
831
toggleCodeBlock ( this ) ;
764
832
} ;
0 commit comments