javaeye3/public/javascripts/editor/editor.js

276 lines
10 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
http://jira.csdn.net/browse/BBS-231
为下文缺少bbcode.js而出错的代码使用try之后将异常信息作为debug信息输出
*/
if(typeof window.console === 'undefined'){
window.console = {debug: function(){}};
}
/*
http://jira.csdn.net/browse/BBS-231
查看HTML源码时由于显示源码的页面和编辑器不在相同域所以需要设置相同的顶级域避免因为不安全的跨域访问而导致无法显示HTML源码。
*/
document.domain = /[^\.]+\.[^\.]+$/.exec(location.host)[0];
if(typeof(tinyMCE) != 'undefined') {
tinyMCE.init({
plugins : "javaeye,media,table,emotions,contextmenu,fullscreen,inlinepopups",
mode : "none",
language : "zh-cn",
theme : "advanced",
theme_advanced_buttons1 : "formatselect,fontselect,fontsizeselect,removeformat,separator,forecolor,backcolor,separator,bold,italic,underline,strikethrough,separator,bullist,numlist",
theme_advanced_buttons2 : "undo,redo,cut,copy,paste,separator,justifyleft,justifycenter,justifyright,separator,outdent,indent,separator,link,unlink,image,media,emotions,table,separator,quote,insertcode,separator,code,fullscreen",
theme_advanced_buttons3 : "",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
/*
http://jira.csdn.net/browse/BBS-231
完善字体。隶书、微软雅黑需要客户端安装有这个字体才可以正常显示。
字体的值必须是英文名称,否则会在后端处理时会被过滤。
*/
theme_advanced_fonts :
"宋体=SimSun,\\5B8B\\4F53,tahoma,arial,helvetica,sans-serif;" +
"黑体=SimHei,\\9ED1\\4F53,tahoma,arial,helvetica,sans-serif;" +
"微软雅黑=\"Microsoft YaHei\",\\5FAE\\8F6F\\96C5\\9ED1,SimHei,tahoma,arial,helvetica,sans-serif;" +
"隶书=LiSu,\\96B6\\4E66,tahoma,arial,helvetica,sans-serif;" +
"仿宋=FangSong_GB2312,\\4EFF\\5B8B_GB2312,tahoma,arial,helvetica,sans-serif;" +
"楷体=KaiTi_GB2312,\\6977\\4F53_GB2312,tahoma,arial,helvetica,sans-serif;" +
"幼圆=YouYuan,\\5E7C\\5706,tahoma,arial,helvetica,sans-serif;" +
"Arial=arial,helvetica,sans-serif;" +
"Comic Sans MS=comic sans ms,sans-serif;" +
"Courier New=courier new,courier;" +
"Tahoma=tahoma,arial,helvetica,sans-serif;" +
"Times New Roman=times new roman,times;" +
"Verdana=verdana,geneva;" +
"Webdings=webdings;" +
"Wingdings=wingdings,zapf dingbats;",
theme_advanced_font_sizes :
"10px,11px,12px,14px,16px,18px,20px,24px,36px",
convert_fonts_to_spans : true,
remove_trailing_nbsp : true,
remove_linebreaks : false,
width : "100%",
extended_valid_elements : "pre[name|class],object[classid|codebase|width|height|align],param[name|value],embed[quality|type|pluginspage|width|height|src|align|wmode]",
relative_urls: false,
content_css: "/javascripts/tinymce/plugins/javaeye/css/content.css",
doctype: '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
/*
http://jira.csdn.net/browse/BBS-231
清除格式后,默认字体大小,通过在打开和保存时添加额外的样式代码实现。编辑和发布界面有效,预览界面无效。
*/
save_callback: "mceSaveCallback",
setupcontent_callback : "mceSetupContentCallback"
});
}
function mceSaveCallback(element_id, html, body){
html = html.replace(/<pre([^>]*)>((?:.|\n)*?)<\/pre>/gi,function(a,b,c){
c=c.replace(/<br\s*\/?>\n*/gi,'\n');
return'<pre'+b+'>'+c+'</pre>';
});
var m = /^\s*<div class="iteye-blog-content-contain"[^>]*>/i.exec(html);
if (!m) {
html = '<div class="iteye-blog-content-contain" style="font-size: 14px">' + html + '</div>';
}
return html;
}
function mceSetupContentCallback(element_id, body, doc) {
var v = '14px';
var pvcss = $('editor_preview').style;
pvcss.fontSize = v;
pvcss.paddingLeft = pvcss.paddingRight = '8px';
pvcss.paddingTop = '62px'
pvcss.width = '';
// if (typeof body.style.cssText !== 'undefined') {
// body.style.cssText = 'font-size: ' + v;
// } else {
body.style.fontSize = v;
body.style.lineHeight = '1.5';
body.style.fontFamily = 'Helvetica, Tahoma, Arial, sans-serif';
// }
}
Control.TextArea.Editor = Class.create();
Object.extend(Control.TextArea.Editor.prototype,{
bbcode_editor: false,
rich_editor: false,
mode: false,
in_preview: false,
initialize: function(textarea, mode, autosave){
this.editor_bbcode_flag = $("editor_bbcode_flag");
this.textarea = textarea;
this.switchMode(mode);
if(autosave) this._initAutosave();
},
switchMode: function(mode, convert){
if(this.in_preview && this.mode == mode) {
$("editor_tab_bbcode").removeClassName("activetab");
$("editor_tab_rich").removeClassName("activetab");
$("editor_tab_preview").removeClassName("activetab");
$("editor_tab_" + mode).addClassName("activetab");
$("editor_preview").hide();
$("editor_main").show();
this.in_preview = false;
return;
}
if(this.mode == mode) return;
if(convert) {
var old_text = this.getValue();
if(old_text != ""){
if(!confirm("切换编辑器模式可能导致格式和内容丢失,你确定吗?")) return;
$('editor_switch_spinner').show();
}
}
this.mode = mode;
if($("editor_switch")) {
$("editor_tab_bbcode").removeClassName("activetab");
$("editor_tab_rich").removeClassName("activetab");
$("editor_tab_preview").removeClassName("activetab");
$("editor_tab_" + mode).addClassName("activetab");
$("editor_preview").hide();
$("editor_main").show();
this.in_preview = false;
}
if(this.mode == "rich") {
this.editor_bbcode_flag.value = "false";
if(this.bbcode_editor) this.bbcode_editor.hide();
this.rich_editor = true;
tinyMCE.execCommand('mceAddControl', false, this.textarea);
}else{
this.editor_bbcode_flag.value = "true";
if(this.rich_editor) tinyMCE.execCommand('mceRemoveControl', false, this.textarea);
if (this.bbcode_editor) {
this.bbcode_editor.show();
} else {
/*
http://jira.csdn.net/browse/BBS-231
为缺少bbcode.js而出错的代码使用try
*/
try {
this.bbcode_editor = new Control.TextArea.BBCode(this.textarea);
} catch(e) {
console.debug(e + '');
}
}
}
if(convert && old_text != "") {
new Ajax.Request(this.mode == "rich" ? '/editor/bbcode2html' : '/editor/html2bbcode', {
method: 'post',
parameters: {text: old_text},
asynchronous: true,
onSuccess: function(transport) {this.setValue(transport.responseText);$('editor_switch_spinner').hide();}.bind(this)
});
}
},
getValue: function() {
if (this.mode === "bbcode") {
/*
http://jira.csdn.net/browse/BBS-231
为缺少bbcode.js而出错的代码使用try
*/
try {
return this.bbcode_editor.textarea.element.value;
} catch(e) {
console.debug(e + '');
}
} else {
return tinyMCE.activeEditor.getContent();
}
},
setValue: function(value) {
if(this.mode == "bbcode") {
this.bbcode_editor.textarea.element.value = value;
}else{
tinyMCE.get(this.textarea).setContent(value);
}
},
preview: function() {
this.in_preview = true;
$('editor_switch_spinner').show();
$("editor_preview").show();
$("editor_main").hide();
$("editor_tab_bbcode").removeClassName("activetab");
$("editor_tab_rich").removeClassName("activetab");
$("editor_tab_preview").addClassName("activetab");
new Ajax.Updater("editor_preview", "/editor/preview", {
parameters: {text: this.getValue(), mode: this.mode},
evalScripts: true,
onSuccess: function() {$('editor_switch_spinner').hide();}
});
},
insertImage: function(url) {
if(this.mode == "bbcode") {
this.bbcode_editor.textarea.insertAfterSelection("\n[img]"+url+"[/img]\n");
}else{
tinyMCE.execCommand("mceInsertContent", false, "<br/><img src='"+url+"'/><br/>&nbsp;");
}
},
_initAutosave: function() {
this.autosave_url = window.location.href;
new Ajax.Request('/editor/check_autosave', {
method: 'post',
parameters: {url: this.autosave_url},
asynchronous: true,
onSuccess: this._loadAutosave.bind(this)
});
setInterval(this._autosave.bind(this), 90 * 1000);
},
_loadAutosave: function(transport) {
var text = transport.responseText;
if(text != "nil") {
eval("this.auto_save = " + text);
$('editor_auto_save_update').update('<span style="color:red">您有一份自动保存于'+this.auto_save.updated_at+'的草稿,<a href="#" onclick=\'editor._setAutosave();return false;\'>恢复</a>还是<a href="#" onclick=\'editor._discardAutosave();return false;\'>丢弃</a>呢?</span>');
}
},
_setAutosave: function() {
$("editor_auto_save_id").value = this.auto_save.id;
$('editor_auto_save_update').update("");
this.auto_save.bbcode ? this.switchMode("bbcode") : this.switchMode("rich");
this.setValue(this.auto_save.body);
},
_discardAutosave: function() {
$("editor_auto_save_id").value = this.auto_save.id;
$('editor_auto_save_update').update("");
},
_autosave: function() {
var body = this.getValue();
if(body.length < 100) return;
new Ajax.Request('/editor/autosave', {
method: 'post',
parameters: {
url: this.autosave_url,
body: body,
bbcode: this.mode == "bbcode"
},
asynchronous: true,
onSuccess: function(transport){
var now = new Date();
$('editor_auto_save_id').value = transport.responseText;
/*
http://jira.csdn.net/
修改自动保存时间只显示时分秒.
*/
$('editor_auto_save_update').update('<span style="color:red">ITeye编辑器帮您自动保存草稿于' +
now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + '</span>');
}
});
}
});