Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the onPreview() callback asynchronous. #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions js/bootstrap-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $('<div/>',{'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 = $('<div/>',{'class':'md-preview','data-provider':'markdown-preview'})

// Build preview element
replacementContainer.html(content)
Expand Down Expand Up @@ -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) {},
Expand Down