Skip to content

Instantly share code, notes, and snippets.

@benstr
Last active April 5, 2016 10:56
Show Gist options
  • Save benstr/0605b876f415ab2f2599 to your computer and use it in GitHub Desktop.
Save benstr/0605b876f415ab2f2599 to your computer and use it in GitHub Desktop.
summernote & okgrow:image-upload example
Template.blogList.onRendered( function () {
var self = this;
$('#summernote').summernote({
height: 400,
maxHeight: 800,
minHeight: 250,
onImageUpload: function (files, editor, $editable) {
var file = files[0];
var coll = ImageUpload.getImageCollection('imageUploadCollectionName');
if (file) {
var newFile = new FS.File(file);
newFile.addedBy = Meteor.userId();
newFile.associatedObjectId = '??'; // id of the parent doc, maybe this is a template reactive var or session?
coll.insert(newFile, function (err, fileObj) {
if (err) {
console.log("Error: ", err);
}
// Inserted new doc with ID fileObj._id, and kicked off the data upload using HTTP
if (!self.associatedObjectId) {
// Save the ID of the newly inserted doc in the session so we can use it
// until it's associated.
Session.set("lastImageId-" + self.imageCollection, fileObj._id);
}
// Update summernote
template.autorun(function (c) {
fileObj = coll.findOne(fileObj._id);
var url = fileObj.url();
if (url) {
$("#summernote").summernote("insertImage", fileObj.url(), "Image Title");
c.stop();
}
});
});
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment