From 192c6d4de9796be0146b312a5cceef3cec5df716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steve=20Fr=C3=A9cinaux?= Date: Tue, 28 Oct 2014 13:42:32 +0100 Subject: [PATCH] Make the onPreview() callback asynchronous. This makes it possible to perform asynchronous operations while in the onPreview() callback, for instance using an AJAX request: $("#form_content").markdown({ onPreview: function(editor, callback) { jQuery.ajax({ type: "POST", url: "/render-markdown", data: { content: editor.$textarea.val() }, success: callback }); } }); A default implementation of onPreview() is provided, using an updated parseContent() method which doesn't call the onPreview() callback anymore. This effectively adds a parameter to the onPreview() callback, but backward compatibility is retained by checking the onPreview() callback return value and using it if it exists. --- js/bootstrap-markdown.js | 48 +++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/js/bootstrap-markdown.js b/js/bootstrap-markdown.js index 964b217..aac68c8 100644 --- a/js/bootstrap-markdown.js +++ b/js/bootstrap-markdown.js @@ -427,40 +427,39 @@ } , parseContent: function() { - var content, - callbackContent = this.$options.onPreview(this) // Try to get the content from callback - - if (typeof callbackContent == 'string') { - // Set the content based by callback content - content = callbackContent + // Set the content + var val = this.$textarea.val(); + if (typeof markdown == 'object') { + content = markdown.toHTML(val); + } else if (typeof marked == 'function') { + content = marked(val); } else { - // Set the content - var val = this.$textarea.val(); - if(typeof markdown == 'object') { - content = markdown.toHTML(val); - }else if(typeof marked == 'function') { - content = marked(val); - } else { - content = val; - } + content = val; } return content; } , showPreview: function() { - var options = this.$options, - container = this.$textarea, - afterContainer = container.next(), - replacementContainer = $('
',{'class':'md-preview','data-provider':'markdown-preview'}), - content - // Give flag that tell the editor enter preview mode this.$isPreview = true // Disable all buttons this.disableButtons('all').enableButtons('cmdPreview') - content = this.parseContent() + // Render the content asynchronously + var r = this.$options.onPreview(this, jQuery.proxy(this.showPreviewWithContent, this)) + + // Backward compatibility: if someone has an old callback, it will return + // something and we can just call the callback by ourselves + if (typeof r == 'string') { + this.showPreviewWithContent(r) + } + } + + , showPreviewWithContent: function(content) { + var container = this.$textarea, + afterContainer = container.next(), + replacementContainer = $('
',{'class':'md-preview','data-provider':'markdown-preview'}) // Build preview element replacementContainer.html(content) @@ -1271,7 +1270,10 @@ /* Events hook */ onShow: function (e) {}, - onPreview: function (e) {}, + onPreview: function (e, callback) { + // Default behaviour here is to parse the content and pass it to the callback. + callback(e.parseContent()) + }, onSave: function (e) {}, onBlur: function (e) {}, onFocus: function (e) {},