scinote-web/app/assets/javascripts/sitewide/tiny_mce.js.erb

68 lines
2.7 KiB
Plaintext
Raw Normal View History

2017-01-19 05:40:08 +08:00
var TinyMCE = (function() {
'use strict';
function initHighlightjs() {
2017-01-20 00:09:25 +08:00
$('[class*=language]').each(function(i, block) {
hljs.highlightBlock(block);
});
}
function initHighlightjsIframe(iframe) {
$('[class*=language]', iframe).each(function(i, block) {
hljs.highlightBlock(block);
2017-01-19 05:40:08 +08:00
});
}
2017-01-20 00:09:25 +08:00
// returns a public API for TinyMCE editor
2017-01-19 05:40:08 +08:00
return Object.freeze({
init : function() {
if (typeof tinyMCE != 'undefined') {
tinyMCE.init({
selector: "textarea.tinymce",
2017-01-19 06:06:14 +08:00
toolbar: ["undo redo | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link | print preview | forecolor backcolor | codesample"],
plugins: "link,advlist,codesample,autolink,lists,link,charmap,print,preview,hr,anchor,pagebreak,searchreplace,wordcount,visualblocks,visualchars,code,fullscreen,insertdatetime,nonbreaking,save,table,contextmenu,directionality,paste,textcolor,colorpicker,textpattern,imagetools,toc",
2017-01-19 05:40:08 +08:00
codesample_languages: [{"text":"R","value":"r"},{"text":"MATLAB","value":"matlab"},{"text":"Python","value":"python"},{"text":"JSON","value":"javascript"},{"text":"HTML/XML","value":"markup"},{"text":"JavaScript","value":"javascript"},{"text":"CSS","value":"css"},{"text":"PHP","value":"php"},{"text":"Ruby","value":"ruby"},{"text":"Java","value":"java"},{"text":"C","value":"c"},{"text":"C#","value":"csharp"},{"text":"C++","value":"cpp"}],
init_instance_callback: function(editor) {
SmartAnnotation.init($(editor.contentDocument.activeElement));
2017-01-20 00:09:25 +08:00
initHighlightjsIframe($(this.iframeElement).contents());
2017-01-19 05:40:08 +08:00
},
setup: function(editor) {
2017-01-20 00:09:25 +08:00
editor.on('Change', function(e) {
if(e.keyCode == 13 && $(editor.contentDocument.activeElement).atwho('isSelecting')) {
2017-01-19 05:40:08 +08:00
return false;
2017-01-20 00:09:25 +08:00
}
2017-01-19 05:40:08 +08:00
});
2017-01-20 00:09:25 +08:00
editor.on('NodeChange', function(e) {
var node = e.element;
var editor = this;
setTimeout(function() {
if($(node).is('pre') && !editor.isHidden()){
initHighlightjsIframe($(editor.iframeElement).contents());
}
}, 200);
});
},
codesample_content_css: '<%= asset_path('highlightjs-github-theme') %>'
2017-01-19 05:40:08 +08:00
});
}
},
destroyAll: function() {
_.each(tinymce.editors, function(editor) {
editor.destroy();
initHighlightjs();
});
},
refresh: function() {
this.destroyAll();
this.init();
},
2017-01-19 18:37:59 +08:00
getContent: function() {
return tinymce.editors[0].getContent();
},
2017-01-19 05:40:08 +08:00
highlight: initHighlightjs
});
})();