Skip to content

Instantly share code, notes, and snippets.

@sazid
Created April 18, 2017 14:21
Show Gist options
  • Save sazid/7dd713128cf7abe97d5b53dd2ec4ea40 to your computer and use it in GitHub Desktop.
Save sazid/7dd713128cf7abe97d5b53dd2ec4ea40 to your computer and use it in GitHub Desktop.
tinymce.init({
selector: 'textarea#doc_editor',
height: 500,
theme: 'modern',
plugins: [
'advlist placeholder autolink lists link image media charmap print preview hr anchor pagebreak',
'searchreplace wordcount visualblocks visualchars code fullscreen',
'insertdatetime media nonbreaking table contextmenu directionality',
'emoticons paste textcolor colorpicker textpattern codesample'
],
browser_spellcheck: true,
toolbar: 'undo redo | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image codesample | forecolor backcolor emoticons | print',
image_caption: true,
// enable title field in the Image dialog
image_title: true,
// enable automatic uploads of images represented by blob or data URIs
// automatic_uploads: true,
// URL of our upload handler (for more details check: https://www.tinymce.com/docs/configure/file-image-upload/#images_upload_url)
// images_upload_url: '/Home/upload_media_from_editor/',
// here we add custom filepicker only to Image dialog
file_picker_types: 'file image media',
// and here's our custom image picker
file_picker_callback: function(cb, value, meta) {
var input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('accept', '*/*');
// Note: In modern browsers input[type="file"] is functional without
// even adding it to the DOM, but that might not be the case in some older
// or quirky browsers like IE, so you might want to add it to the DOM
// just in case, and visually hide it. And do not forget do remove it
// once you do not need it anymore.
input.onchange = function() {
// show the progressbar
$("#progressbar").css("display", "block");
var file = this.files[0];
// // call the callback and populate the Title field with the file name
// cb(url_to_image, { title: file.name });
var data = new FormData();
// name of the File object
data.append("uploaded_file", file);
// console.log(data.get("uploaded_file"));
$.ajax({
url: '/test/url/',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function(data, textStatus, jqXHR) {
if (typeof data.error === 'undefined') {
// Success so call function to process the form
// submitForm(event, data);
var file_url = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port + data["file_url"];
cb(file_url, {
title: file.name
});
} else {
// Handle errors here
console.log('ERRORS: ' + data.error);
}
// hide the progressbar
$("#progressbar").css("display", "none");
},
error: function(jqXHR, textStatus, errorThrown) {
// Handle errors here
console.log('ERRORS: ' + textStatus);
// STOP LOADING SPINNER
// hide the progressbar
$("#progressbar").css("display", "none");
}
});
};
input.click();
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment