Skip to content

Commit ee471ab

Browse files
committed
Merge pull request sparksuite#53 from fraserredmond/master
Heading 1-6
2 parents 02ef20b + 0f9dd5e commit ee471ab

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/js/simplemde.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var shortcuts = {
44
'Cmd-B': toggleBold,
55
'Cmd-I': toggleItalic,
66
'Cmd-K': drawLink,
7+
'Cmd-H': toggleHeadingSmaller,
8+
'Cmd-Alt-H': toggleHeadingBigger,
79
'Cmd-Alt-I': drawImage,
810
"Cmd-'": toggleBlockquote,
911
'Cmd-Alt-L': toggleOrderedList,
@@ -137,6 +139,21 @@ function toggleBlockquote(editor) {
137139
_toggleLine(cm, 'quote');
138140
}
139141

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+
}
140157

141158

142159
/**
@@ -269,6 +286,44 @@ function _replaceSelection(cm, active, start, end) {
269286
}
270287

271288

289+
function _toggleHeading(cm, direction) {
290+
if(/editor-preview-active/.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+
272327
function _toggleLine(cm, name) {
273328
if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
274329
return;
@@ -394,6 +449,11 @@ var toolbar = [{
394449
action: toggleItalic,
395450
className: "fa fa-italic",
396451
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)",
397457
},
398458
"|", {
399459
name: "quote",
@@ -736,6 +796,8 @@ SimpleMDE.prototype.value = function(val) {
736796
SimpleMDE.toggleBold = toggleBold;
737797
SimpleMDE.toggleItalic = toggleItalic;
738798
SimpleMDE.toggleBlockquote = toggleBlockquote;
799+
SimpleMDE.toggleHeadingSmaller = toggleHeadingSmaller;
800+
SimpleMDE.toggleHeadingBigger = toggleHeadingBigger;
739801
SimpleMDE.toggleCodeBlock = toggleCodeBlock;
740802
SimpleMDE.toggleUnorderedList = toggleUnorderedList;
741803
SimpleMDE.toggleOrderedList = toggleOrderedList;
@@ -759,6 +821,12 @@ SimpleMDE.prototype.toggleItalic = function() {
759821
SimpleMDE.prototype.toggleBlockquote = function() {
760822
toggleBlockquote(this);
761823
};
824+
SimpleMDE.prototype.toggleHeadingSmaller = function() {
825+
toggleHeadingSmaller(this);
826+
};
827+
SimpleMDE.prototype.toggleHeadingBigger = function() {
828+
toggleHeadingBigger(this);
829+
};
762830
SimpleMDE.prototype.toggleCodeBlock = function() {
763831
toggleCodeBlock(this);
764832
};

0 commit comments

Comments
 (0)