Skip to content

Commit 36f9760

Browse files
author
Wes Cossick
committed
Refactor default toolbar organization; Allow showIcons setting (sparksuite#170)
1 parent 4dbe95f commit 36f9760

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

src/js/simplemde.js

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -666,19 +666,20 @@ function wordCount(data) {
666666
return count;
667667
}
668668

669-
670669
var toolbarBuiltInButtons = {
671670
"bold": {
672671
name: "bold",
673672
action: toggleBold,
674673
className: "fa fa-bold",
675-
title: "Bold (Ctrl+B)"
674+
title: "Bold (Ctrl+B)",
675+
default: true
676676
},
677677
"italic": {
678678
name: "italic",
679679
action: toggleItalic,
680680
className: "fa fa-italic",
681-
title: "Italic (Ctrl+I)"
681+
title: "Italic (Ctrl+I)",
682+
default: true
682683
},
683684
"strikethrough": {
684685
name: "strikethrough",
@@ -690,7 +691,8 @@ var toolbarBuiltInButtons = {
690691
name: "heading",
691692
action: toggleHeadingSmaller,
692693
className: "fa fa-header",
693-
title: "Heading (Ctrl+H)"
694+
title: "Heading (Ctrl+H)",
695+
default: true
694696
},
695697
"heading-smaller": {
696698
name: "heading-smaller",
@@ -722,6 +724,9 @@ var toolbarBuiltInButtons = {
722724
className: "fa fa-header fa-header-x fa-header-3",
723725
title: "Small Heading"
724726
},
727+
"separator-1": {
728+
name: "separator-1"
729+
},
725730
"code": {
726731
name: "code",
727732
action: toggleCodeBlock,
@@ -732,31 +737,39 @@ var toolbarBuiltInButtons = {
732737
name: "quote",
733738
action: toggleBlockquote,
734739
className: "fa fa-quote-left",
735-
title: "Quote (Ctrl+')"
740+
title: "Quote (Ctrl+')",
741+
default: true
736742
},
737743
"unordered-list": {
738744
name: "unordered-list",
739745
action: toggleUnorderedList,
740746
className: "fa fa-list-ul",
741-
title: "Generic List (Ctrl+L)"
747+
title: "Generic List (Ctrl+L)",
748+
default: true
742749
},
743750
"ordered-list": {
744751
name: "ordered-list",
745752
action: toggleOrderedList,
746753
className: "fa fa-list-ol",
747-
title: "Numbered List (Ctrl+Alt+L)"
754+
title: "Numbered List (Ctrl+Alt+L)",
755+
default: true
756+
},
757+
"separator-2": {
758+
name: "separator-2"
748759
},
749760
"link": {
750761
name: "link",
751762
action: drawLink,
752763
className: "fa fa-link",
753-
title: "Create Link (Ctrl+K)"
764+
title: "Create Link (Ctrl+K)",
765+
default: true
754766
},
755767
"image": {
756768
name: "image",
757769
action: drawImage,
758770
className: "fa fa-picture-o",
759-
title: "Insert Image (Ctrl+Alt+I)"
771+
title: "Insert Image (Ctrl+Alt+I)",
772+
default: true
760773
},
761774
"table": {
762775
name: "table",
@@ -770,29 +783,36 @@ var toolbarBuiltInButtons = {
770783
className: "fa fa-minus",
771784
title: "Insert Horizontal Line"
772785
},
786+
"separator-3": {
787+
name: "separator-3"
788+
},
773789
"preview": {
774790
name: "preview",
775791
action: togglePreview,
776792
className: "fa fa-eye no-disable",
777-
title: "Toggle Preview (Ctrl+P)"
793+
title: "Toggle Preview (Ctrl+P)",
794+
default: true
778795
},
779796
"side-by-side": {
780797
name: "side-by-side",
781798
action: toggleSideBySide,
782799
className: "fa fa-columns no-disable no-mobile",
783-
title: "Toggle Side by Side (F9)"
800+
title: "Toggle Side by Side (F9)",
801+
default: true
784802
},
785803
"fullscreen": {
786804
name: "fullscreen",
787805
action: toggleFullScreen,
788806
className: "fa fa-arrows-alt no-disable no-mobile",
789-
title: "Toggle Fullscreen (F11)"
807+
title: "Toggle Fullscreen (F11)",
808+
default: true
790809
},
791810
"guide": {
792811
name: "guide",
793812
action: "http://nextstepwebs.github.io/simplemde-markdown-editor/markdown-guide",
794813
className: "fa fa-question-circle",
795-
title: "Markdown Guide"
814+
title: "Markdown Guide",
815+
default: true
796816
}
797817
};
798818

@@ -857,10 +877,28 @@ function SimpleMDE(options) {
857877
}
858878

859879

860-
// Handle toolbar and status bar
861-
if(options.toolbar !== false)
862-
options.toolbar = options.toolbar || SimpleMDE.toolbar;
880+
// Handle toolbar
881+
if(options.toolbar === undefined) {
882+
// Initialize
883+
options.toolbar = [];
863884

885+
886+
// Loop over the built in buttons, to get the preferred order
887+
for(var key in toolbarBuiltInButtons) {
888+
if(toolbarBuiltInButtons.hasOwnProperty(key)) {
889+
if(key.indexOf("separator-") != -1) {
890+
options.toolbar.push("|");
891+
}
892+
893+
if(toolbarBuiltInButtons[key].default === true || (options.showIcons && options.showIcons.constructor === Array && options.showIcons.indexOf(key) != -1)) {
894+
options.toolbar.push(key);
895+
}
896+
}
897+
}
898+
}
899+
900+
901+
// Handle status bar
864902
if(!options.hasOwnProperty("status")) {
865903
options.status = ["autosave", "lines", "words", "cursor"];
866904
}
@@ -908,11 +946,6 @@ function SimpleMDE(options) {
908946
}
909947
}
910948

911-
/**
912-
* Default toolbar elements.
913-
*/
914-
SimpleMDE.toolbar = ["bold", "italic", "heading", "|", "quote", "unordered-list", "ordered-list", "|", "link", "image", "|", "preview", "side-by-side", "fullscreen", "guide"];
915-
916949
/**
917950
* Default markdown render.
918951
*/

0 commit comments

Comments
 (0)