From 15a4f65c98a5a1408492339c6eb0dfe17712cce7 Mon Sep 17 00:00:00 2001 From: Jordan Reiter Date: Wed, 22 Mar 2017 15:24:33 -0400 Subject: [PATCH 1/2] Fix content/setContent; add access to editor Currently this.find('.md-editor') will not work since this refers to the textarea which does not contain .md-editor (it comes before). This change makes access to editor more explicit by putting it in the data for this object. It also adds the method 'editor' which provides direct access to the ace editor object. --- src/bootstrap-markdown-editor.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/bootstrap-markdown-editor.js b/src/bootstrap-markdown-editor.js index 16ae146..a32020a 100644 --- a/src/bootstrap-markdown-editor.js +++ b/src/bootstrap-markdown-editor.js @@ -230,6 +230,7 @@ setShortcuts(editor, snippetManager); }); + plugin.data('md-editor', editor); // Image drag and drop and upload events if (defaults.imageUpload) { @@ -370,19 +371,26 @@ return this; }, content: function () { - var editor = ace.edit(this.find('.md-editor')[0]); + var editor = this.data('md-editor'); return editor.getSession().getValue(); }, setContent: function(str) { - var editor = ace.edit(this.find('.md-editor')[0]); + var editor = this.data('md-editor'); editor.setValue(str, 1); - } + }, + editor: function () { + return this.data('md-editor'); + }, }; $.fn.markdownEditor = function (options) { if (methods[options]) { - return methods[options].apply(this, Array.prototype.slice.call(arguments, 1)); + if (options === "init" || this.data('md-editor')) { + return methods[options].apply(this, Array.prototype.slice.call(arguments, 1)); + } else { + $.error('This object has not been initialized as a Markdown Editor.'); + } } else if (typeof options === 'object' || ! options) { return methods.init.apply(this, arguments); From 59e0620cddadb09759d7cb546ea4761ce5147018 Mon Sep 17 00:00:00 2001 From: Jordan Reiter Date: Wed, 22 Mar 2017 15:48:01 -0400 Subject: [PATCH 2/2] Updated minified/distributed js file --- dist/js/bootstrap-markdown-editor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/js/bootstrap-markdown-editor.js b/dist/js/bootstrap-markdown-editor.js index cb6854f..1626166 100644 --- a/dist/js/bootstrap-markdown-editor.js +++ b/dist/js/bootstrap-markdown-editor.js @@ -1,7 +1,7 @@ /*! * Bootstrap Markdown Editor v2.0.2 (https://github.com/inacho/bootstrap-markdown-editor) - * Copyright 2016 Ignacio de Tomás + * Copyright 2017 Ignacio de Tomás * Licensed under MIT (https://github.com/inacho/bootstrap-markdown-editor/blob/master/LICENSE) */ -!function(a){"use strict";function b(b,c,d,e,f){if(c.length){f.show();var g=new FormData,h=0;for(h=0;h1&&(b="\n");for(var c=0;ce&&(c=d-e,b.css("height",c+"px"))}function d(a,b){a.commands.addCommand({name:"bold",bindKey:{win:"Ctrl-B",mac:"Command-B"},exec:function(a){var c=a.session.getTextRange(a.getSelectionRange());""===c?b.insertSnippet(a,"**${1:text}**"):b.insertSnippet(a,"**"+c+"**")},readOnly:!1}),a.commands.addCommand({name:"italic",bindKey:{win:"Ctrl-I",mac:"Command-I"},exec:function(a){var c=a.session.getTextRange(a.getSelectionRange());""===c?b.insertSnippet(a,"*${1:text}*"):b.insertSnippet(a,"*"+c+"*")},readOnly:!1}),a.commands.addCommand({name:"link",bindKey:{win:"Ctrl-K",mac:"Command-K"},exec:function(a){var c=a.session.getTextRange(a.getSelectionRange());""===c?b.insertSnippet(a,"[${1:text}](http://$2)"):b.insertSnippet(a,"["+c+"](http://$1)")},readOnly:!1})}function e(a,b){0===a.getCursorPosition().column?(a.navigateLineStart(),a.insert(b+" ")):(a.navigateLineStart(),a.insert(b+" "),a.navigateLineEnd())}function f(b,c){var d="";return d+='
',d+='
',d+='
',d+='
',d+='',d+='',d+='',d+="
",d+='
',d+='',d+='',d+="
",d+='
',d+='',d+='',d+="
",d+='
',d+='',d+='',c.imageUpload===!0&&(d+='
'),d+="
",c.fullscreen===!0&&(d+='
',d+='",d+="
"),c.preview===!0&&(d+='
',d+='",d+='",d+="
"),d+="
",d+="
",d+='
'+a("
").text(b).html()+"
",d+=''}var g={init:function(g){var h,i=a.extend(!0,{},a.fn.markdownEditor.defaults,g),j=this,k=!1,l=!1;j.addClass("md-textarea-hidden"),h=a("
"),j.after(h),h.addClass("md-container").html(f(j.val(),i)),"function"==typeof a().tooltip&&h.find('[data-mdtooltip="tooltip"]').tooltip({container:"body"});var m=h.find(".md-editor"),n=h.find(".md-preview"),o=h.find(".md-loading");h.css({width:i.width}),m.css({height:i.height,fontSize:i.fontSize}),n.css({height:i.height});var p,q=ace.edit(m[0]);return q.setTheme("ace/theme/"+i.theme),q.getSession().setMode("ace/mode/markdown"),q.getSession().setUseWrapMode(!0),q.getSession().setUseSoftTabs(i.softTabs),q.getSession().on("change",function(){j.val(q.getSession().getValue())}),q.setHighlightActiveLine(!1),q.setShowPrintMargin(!1),q.renderer.setShowGutter(!1),ace.config.loadModule("ace/ext/language_tools",function(){p=ace.require("ace/snippets").snippetManager,d(q,p)}),i.imageUpload&&(h.find(".md-input-upload").on("change",function(){var c=a(this).get(0).files;b(i.uploadPath,c,q,p,o)}),h.on("dragenter",function(a){a.stopPropagation(),a.preventDefault()}),h.on("dragover",function(a){a.stopPropagation(),a.preventDefault()}),h.on("drop",function(a){a.preventDefault();var c=a.originalEvent.dataTransfer.files;b(i.uploadPath,c,q,p,o)})),i.fullscreen===!0&&a(window).resize(function(){l===!0&&c(k===!1?m:n)}),h.find(".md-btn").click(function(){var b=a(this).data("btn"),d=q.session.getTextRange(q.getSelectionRange());"h1"===b?e(q,"#"):"h2"===b?e(q,"##"):"h3"===b?e(q,"###"):"ul"===b?e(q,"*"):"ol"===b?e(q,"1."):"bold"===b?q.execCommand("bold"):"italic"===b?q.execCommand("italic"):"link"===b?q.execCommand("link"):"image"===b?""===d?p.insertSnippet(q,"![${1:text}](http://$2)"):p.insertSnippet(q,"!["+d+"](http://$1)"):"edit"===b?(k=!1,n.hide(),m.show(),h.find(".btn-edit").addClass("active"),h.find(".btn-preview").removeClass("active"),l===!0&&c(m)):"preview"===b?(k=!0,n.html('

'+i.label.loading+"...

"),i.onPreview(q.getSession().getValue(),function(a){n.html(a)}),m.hide(),n.show(),h.find(".btn-preview").addClass("active"),h.find(".btn-edit").removeClass("active"),l===!0&&c(n)):"fullscreen"===b&&(l===!0?(l=!1,a("body, html").removeClass("md-body-fullscreen"),h.removeClass("md-fullscreen"),m.css("height",i.height),n.css("height",i.height)):(l=!0,a("body, html").addClass("md-body-fullscreen"),h.addClass("md-fullscreen"),c(k===!1?m:n)),q.resize()),q.focus()}),this},content:function(){var a=ace.edit(this.find(".md-editor")[0]);return a.getSession().getValue()},setContent:function(a){var b=ace.edit(this.find(".md-editor")[0]);b.setValue(a,1)}};a.fn.markdownEditor=function(b){return g[b]?g[b].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof b&&b?void a.error("Method "+b+" does not exist on jQuery.markdownEditor"):g.init.apply(this,arguments)},a.fn.markdownEditor.defaults={width:"100%",height:"400px",fontSize:"14px",theme:"tomorrow",softTabs:!0,fullscreen:!0,imageUpload:!1,uploadPath:"",preview:!1,onPreview:function(a,b){b(a)},label:{btnHeader1:"Header 1",btnHeader2:"Header 2",btnHeader3:"Header 3",btnBold:"Bold",btnItalic:"Italic",btnList:"Unordered list",btnOrderedList:"Ordered list",btnLink:"Link",btnImage:"Insert image",btnUpload:"Upload image",btnEdit:"Edit",btnPreview:"Preview",btnFullscreen:"Fullscreen",loading:"Loading"}}}(jQuery); \ No newline at end of file +!function(a){"use strict";function b(b,c,d,e,f){if(c.length){f.show();var g=new FormData,h=0;for(h=0;h1&&(b="\n");for(var c=0;ce&&(c=d-e,b.css("height",c+"px"))}function d(a,b){a.commands.addCommand({name:"bold",bindKey:{win:"Ctrl-B",mac:"Command-B"},exec:function(a){var c=a.session.getTextRange(a.getSelectionRange());""===c?b.insertSnippet(a,"**${1:text}**"):b.insertSnippet(a,"**"+c+"**")},readOnly:!1}),a.commands.addCommand({name:"italic",bindKey:{win:"Ctrl-I",mac:"Command-I"},exec:function(a){var c=a.session.getTextRange(a.getSelectionRange());""===c?b.insertSnippet(a,"*${1:text}*"):b.insertSnippet(a,"*"+c+"*")},readOnly:!1}),a.commands.addCommand({name:"link",bindKey:{win:"Ctrl-K",mac:"Command-K"},exec:function(a){var c=a.session.getTextRange(a.getSelectionRange());""===c?b.insertSnippet(a,"[${1:text}](http://$2)"):b.insertSnippet(a,"["+c+"](http://$1)")},readOnly:!1})}function e(a,b){0===a.getCursorPosition().column?(a.navigateLineStart(),a.insert(b+" ")):(a.navigateLineStart(),a.insert(b+" "),a.navigateLineEnd())}function f(b,c){var d="";return d+='
',d+='
',d+='
',d+='
',d+='',d+='',d+='',d+="
",d+='
',d+='',d+='',d+="
",d+='
',d+='',d+='',d+="
",d+='
',d+='',d+='',c.imageUpload===!0&&(d+='
'),d+="
",c.fullscreen===!0&&(d+='
',d+='",d+="
"),c.preview===!0&&(d+='
',d+='",d+='",d+="
"),d+="
",d+="
",d+='
'+a("
").text(b).html()+"
",d+=''}var g={init:function(g){var h,i=a.extend(!0,{},a.fn.markdownEditor.defaults,g),j=this,k=!1,l=!1;j.addClass("md-textarea-hidden"),h=a("
"),j.after(h),h.addClass("md-container").html(f(j.val(),i)),"function"==typeof a().tooltip&&h.find('[data-mdtooltip="tooltip"]').tooltip({container:"body"});var m=h.find(".md-editor"),n=h.find(".md-preview"),o=h.find(".md-loading");h.css({width:i.width}),m.css({height:i.height,fontSize:i.fontSize}),n.css({height:i.height});var p,q=ace.edit(m[0]);return q.setTheme("ace/theme/"+i.theme),q.getSession().setMode("ace/mode/markdown"),q.getSession().setUseWrapMode(!0),q.getSession().setUseSoftTabs(i.softTabs),q.getSession().on("change",function(){j.val(q.getSession().getValue())}),q.setHighlightActiveLine(!1),q.setShowPrintMargin(!1),q.renderer.setShowGutter(!1),ace.config.loadModule("ace/ext/language_tools",function(){p=ace.require("ace/snippets").snippetManager,d(q,p)}),j.data("md-editor",q),i.imageUpload&&(h.find(".md-input-upload").on("change",function(){var c=a(this).get(0).files;b(i.uploadPath,c,q,p,o)}),h.on("dragenter",function(a){a.stopPropagation(),a.preventDefault()}),h.on("dragover",function(a){a.stopPropagation(),a.preventDefault()}),h.on("drop",function(a){a.preventDefault();var c=a.originalEvent.dataTransfer.files;b(i.uploadPath,c,q,p,o)})),i.fullscreen===!0&&a(window).resize(function(){l===!0&&c(k===!1?m:n)}),h.find(".md-btn").click(function(){var b=a(this).data("btn"),d=q.session.getTextRange(q.getSelectionRange());"h1"===b?e(q,"#"):"h2"===b?e(q,"##"):"h3"===b?e(q,"###"):"ul"===b?e(q,"*"):"ol"===b?e(q,"1."):"bold"===b?q.execCommand("bold"):"italic"===b?q.execCommand("italic"):"link"===b?q.execCommand("link"):"image"===b?""===d?p.insertSnippet(q,"![${1:text}](http://$2)"):p.insertSnippet(q,"!["+d+"](http://$1)"):"edit"===b?(k=!1,n.hide(),m.show(),h.find(".btn-edit").addClass("active"),h.find(".btn-preview").removeClass("active"),l===!0&&c(m)):"preview"===b?(k=!0,n.html('

'+i.label.loading+"...

"),i.onPreview(q.getSession().getValue(),function(a){n.html(a)}),m.hide(),n.show(),h.find(".btn-preview").addClass("active"),h.find(".btn-edit").removeClass("active"),l===!0&&c(n)):"fullscreen"===b&&(l===!0?(l=!1,a("body, html").removeClass("md-body-fullscreen"),h.removeClass("md-fullscreen"),m.css("height",i.height),n.css("height",i.height)):(l=!0,a("body, html").addClass("md-body-fullscreen"),h.addClass("md-fullscreen"),c(k===!1?m:n)),q.resize()),q.focus()}),this},content:function(){return this.data("md-editor").getSession().getValue()},setContent:function(a){this.data("md-editor").setValue(a,1)},editor:function(){return this.data("md-editor")}};a.fn.markdownEditor=function(b){if(g[b]){if("init"===b||this.data("md-editor"))return g[b].apply(this,Array.prototype.slice.call(arguments,1));a.error("This object has not been initialized as a Markdown Editor.")}else{if("object"==typeof b||!b)return g.init.apply(this,arguments);a.error("Method "+b+" does not exist on jQuery.markdownEditor")}},a.fn.markdownEditor.defaults={width:"100%",height:"400px",fontSize:"14px",theme:"tomorrow",softTabs:!0,fullscreen:!0,imageUpload:!1,uploadPath:"",preview:!1,onPreview:function(a,b){b(a)},label:{btnHeader1:"Header 1",btnHeader2:"Header 2",btnHeader3:"Header 3",btnBold:"Bold",btnItalic:"Italic",btnList:"Unordered list",btnOrderedList:"Ordered list",btnLink:"Link",btnImage:"Insert image",btnUpload:"Upload image",btnEdit:"Edit",btnPreview:"Preview",btnFullscreen:"Fullscreen",loading:"Loading"}}}(jQuery); \ No newline at end of file