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

106 lines
4.4 KiB
Text
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-04-21 22:09:04 +08:00
toolbar: ["undo redo | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link | forecolor backcolor | customimageuploader | codesample"],
2017-05-03 21:30:08 +08:00
plugins: "customimageuploader,link,advlist,codesample,autolink,lists,charmap,hr,anchor,searchreplace,wordcount,visualblocks,visualchars,insertdatetime,nonbreaking,save,contextmenu,directionality,paste,textcolor,colorpicker,textpattern",
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"}],
removed_menuitems: 'newdocument',
2017-05-03 21:30:08 +08:00
object_resizing: false,
elementpath: false,
2017-05-03 21:30:08 +08:00
forced_root_block: false,
default_link_target: '_blank',
target_list: [
{title: 'New page', value: '_blank'},
{title: 'Same page', value: '_self'}
],
style_formats: [
{title: 'Headers', items: [
{title: 'Header 1', format: 'h1'},
{title: 'Header 2', format: 'h2'},
{title: 'Header 3', format: 'h3'},
{title: 'Header 4', format: 'h4'},
{title: 'Header 5', format: 'h5'},
{title: 'Header 6', format: 'h6'}
]},
{title: 'Inline', items: [
{title: 'Bold', icon: 'bold', format: 'bold'},
{title: 'Italic', icon: 'italic', format: 'italic'},
{title: 'Underline', icon: 'underline', format: 'underline'},
{title: 'Strikethrough', icon: 'strikethrough', format: 'strikethrough'},
{title: 'Superscript', icon: 'superscript', format: 'superscript'},
{title: 'Subscript', icon: 'subscript', format: 'subscript'},
{title: 'Code', icon: 'code', format: 'code'}
]},
{title: 'Blocks', items: [
{title: 'Paragraph', format: 'p'},
{title: 'Blockquote', format: 'blockquote'}
]},
{title: 'Alignment', items: [
{title: 'Left', icon: 'alignleft', format: 'alignleft'},
{title: 'Center', icon: 'aligncenter', format: 'aligncenter'},
{title: 'Right', icon: 'alignright', format: 'alignright'},
{title: 'Justify', icon: 'alignjustify', format: 'alignjustify'}
]}
],
2017-01-19 05:40:08 +08:00
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-26 20:50:32 +08:00
editor.on('keydown', function(e) {
2017-01-20 00:09:25 +08:00
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);
});
},
2017-04-21 22:09:04 +08:00
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
});
})();