📄 editor.js
字号:
/******************************************************************************
Crossday Discuz! Board - Editor Modules for Discuz!
Copyright 2001-2006 Comsenz Inc. (http://www.comsenz.com)
*******************************************************************************/
var PHP = new phpEmulator();
function wysiwygEditor(editorid, mode, parsesmilies, initialtext) {
var sizeoptions = new Array(1, 2, 3, 4, 5, 6, 7);
var istyles = new Array();
istyles = {"pi_button_down" : [ "#98B5E2", "#000000", "0px", "1px solid #316AC5" ], "pi_button_hover" : [ "#C1D2EE", "#000000", "0px", "1px solid #316AC5" ], "pi_button_normal" : [ "#EEEEEE", "#000000", "1px", "none" ], "pi_button_selected" : [ "#F1F6F8", "#000000", "0px", "1px solid #316AC5" ], "pi_menu_down" : [ "#98B5E2", "#316AC5", "0px", "1px solid #316AC5" ], "pi_menu_hover" : [ "#C1D2EE", "#316AC5", "0px", "1px solid #316AC5" ], "pi_menu_normal" : [ "#FFFFFF", "#000000", "0px", "1px solid #FFFFFF" ], "pi_popup_down" : [ "#98B5E2", "#000000", "0px", "1px solid #316AC5" ]};
var contextcontrols = new Array('bold', 'italic', 'underline', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist');
var coloroptions = new Array();
coloroptions = {'#000000' : 'Black', '#a0522d' : 'Sienna', '#556b2f' : 'DarkOliveGreen', '#006400' : 'DarkGreen', '#483d8b' : 'DarkSlateBlue', '#000080' : 'Navy', '#4b0082' : 'Indigo', '#2f4f4f' : 'DarkSlateGray', '#8b0000' : 'DarkRed', '#ff8c00' : 'DarkOrange', '#808000' : 'Olive', '#008000' : 'Green', '#008080' : 'Teal', '#0000ff' : 'Blue', '#708090' : 'SlateGray', '#696969' : 'DimGray', '#ff0000' : 'Red', '#f4a460' : 'SandyBrown', '#9acd32' : 'YellowGreen', '#2e8b57' : 'SeaGreen', '#48d1cc' : 'MediumTurquoise', '#4169e1' : 'RoyalBlue', '#800080' : 'Purple', '#808080' : 'Gray', '#ff00ff' : 'Magenta', '#ffa500' : 'Orange', '#ffff00' : 'Yellow', '#00ff00' : 'Lime', '#00ffff' : 'Cyan', '#00bfff' : 'DeepSkyBlue', '#9932cc' : 'DarkOrchid', '#c0c0c0' : 'Silver', '#ffc0cb' : 'Pink', '#f5deb3' : 'Wheat', '#fffacd' : 'LemonChiffon', '#98fb98' : 'PaleGreen', '#afeeee' : 'PaleTurquoise', '#add8e6' : 'LightBlue', '#dda0dd' : 'Plum', '#ffffff' : 'White'};
var EVENT = new wysiwygEditorEvents();
var history = new History();
var textobj = $(editorid + '_textarea');
var controlbar = $(editorid + '_controls');
var fontstate = null;
var sizestate = null;
var eButtons = new Array();
var ePopups = new Array();
var wysiwyg = parseInt(mode) ? 1 : 0;
bbcodemode = $('bbcodemode');
wysiwygmode = $('wysiwygmode');
bbcodemode.disabled = wysiwyg ? false : true;
wysiwygmode.disabled = wysiwyg ? true : false;
if(!allowswitcheditor && wysiwyg) {
bbcodemode.disabled = true;
} else if((!allowswitcheditor && !wysiwyg) || (is_opera && opera.version() < 9)) {
wysiwyg = 0;
wysiwygmode.disabled = true;
}
$(editorid + '_cmd_table').style.display = wysiwyg ? '' : 'none';
this.textobj = textobj;
this.popups = ePopups;
this.wysiwyg = wysiwyg;
this.initialized = false;
this.parsesmilies = undefined(parsesmilies) || parseInt(parsesmilies) ? 1 : 0;
this.init = function() {
if(this.initialized) {
return;
}
textobj.disabled = false;
this.setEditorContents(initialtext);
this.setEditorEvents();
this.initControls();
this.initSmilies($(editorid + '_smiliebox'));
this.initialized = true;
};
this.checkFocus = function() {
if(!this.editwin.hasfocus) {
this.editwin.focus();
}
}
this.initControls = function() {
if(controlbar == null) {
return;
}
var controls = new Array();
var buttons = findtags(controlbar, 'div');
buttonslength = buttons.length;
for(var i = 0; i < buttonslength; i++) {
if(buttons[i].className == 'editor_imagebutton' && buttons[i].id) {
controls[controls.length] = buttons[i].id;
}
}
controlslength = controls.length;
for(var i = 0; i < controlslength; i++) {
var control = $(controls[i]);
if(control.id.indexOf(editorid + '_cmd_') != -1) {
this.initCommandButton(control);
} else if(control.id.indexOf(editorid + '_popup_') != -1) {
this.initPopupButton(control);
}
}
setUnselectable(controlbar);
};
this.initSmilies = function(smilie_container) {
if(smilie_container != null) {
var smilies = findtags(smilie_container, 'img');
smilieslength = smilies.length;
for(var i = 0; i < smilieslength; i++) {
if(smilies[i].id && smilies[i].id.indexOf('_smilie_') != false) {
smilies[i].style.cursor = is_ie ? 'hand' : 'pointer';
smilies[i].editorid = editorid;
smilies[i].onclick = EVENT.smilie_onclick;
smilies[i].unselectable = 'on';
}
}
}
}
this.initCommandButton = function(obj) {
obj.cmd = obj.id.substr(obj.id.indexOf('_cmd_') + 5);
obj.editorid = editorid;
eButtons[obj.cmd] = obj;
obj.state = false;
obj.mode = 'normal';
obj.onclick = obj.onmousedown = obj.onmouseover = obj.onmouseout = EVENT.command_button_onmouseevent;
}
this.initPopupButton = function(obj) {
obj.cmd = obj.id.substr(obj.id.indexOf('_popup_') + 7);
popupmenu.register(true, obj.id);
popupmenu.menus[obj.id].open_steps = 5;
obj.editorid = editorid;
obj.state = false;
eButtons[obj.cmd] = obj;
if(obj.cmd == 'fontname') {
this.fontout = $(editorid + '_font_out');
this.fontout.innerHTML = obj.title;
this.fontoptions = {'' : this.fontout};
for(var option in fontoptions) {
var div = document.createElement('div');
div.style.width = this.fontout.style.width;
div.style.display = 'none';
div.innerHTML = fontoptions[option];
this.fontoptions[fontoptions[option]] = this.fontout.parentNode.appendChild(div);
}
} else if(obj.cmd == 'fontsize') {
this.sizeout = $(editorid + '_size_out');
this.sizeout.innerHTML = obj.title;
this.sizeoptions = {'' : this.sizeout};
for(var option in sizeoptions) {
var div = document.createElement('div');
div.style.width = this.sizeout.style.width;
div.style.display = 'none';
div.innerHTML = sizeoptions[option];
this.sizeoptions[sizeoptions[option]] = this.sizeout.parentNode.appendChild(div);
}
}
obj._onmouseover = obj.onmouseover;
obj._onclick = obj.onclick;
obj.onmouseover = obj.onmouseout = obj.onclick = EVENT.popup_button_onmouseevent;
popupmenu.menus[obj.id]._show = popupmenu.menus[obj.id].show;
popupmenu.menus[obj.id].show = EVENT.popup_button_show;
}
this.initPopupMenu = function(obj) {
switch(obj.cmd) {
case 'fontname':
var menu = this.initMenuContainer('fontname', '200px', fontoptions.length > 10 ? '220px' : 'auto', 'auto');
this.buildFontnamePopup(obj, menu);
break;
case 'fontsize':
var menu = this.initMenuContainer('fontsize', 'auto', 'auto', 'visible');
this.buildFontsizePopup(obj, menu);
break;
case 'forecolor':
var menu = this.initMenuContainer('forecolor', 'auto', 'auto', 'visible');
this.buildForecolorPopup(obj, menu);
break;
}
ePopups[obj.cmd] = controlbar.appendChild(menu);
setUnselectable(menu);
};
this.initMenuContainer = function(cmd, width, height, overflow) {
var menu = document.createElement('div');
menu.id = editorid + '_popup_' + cmd + '_menu';
menu.className = 'popupmenu_popup';
menu.style.display = 'none';
menu.style.cursor = 'default';
menu.style.padding = '3px';
menu.style.width = width;
menu.style.height = height;
menu.style.overflow = overflow;
return menu;
}
this.buildFontnamePopup = function(obj, menu) {
fol = fontoptions.length;
for(var n = 0; n < fol; n++) {
var option = document.createElement('div');
option.innerHTML = '<font face="' + fontoptions[n] + '">' + fontoptions[n] + '</font>';
option.className = 'editor_ofont';
option.style.textAlign = 'left';
option.cmd = obj.cmd;
option.editorid = editorid;
option.onmouseover = option.onmouseout = option.onmouseup = option.onmousedown = EVENT.menuoption_onmouseevent;
option.onclick = EVENT.formatting_option_onclick;
menu.appendChild(option);
}
}
this.buildFontsizePopup = function(obj, menu) {
sol = sizeoptions.length;
for(var n = 0; n < sol; n++) {
var option = document.createElement('div');
option.innerHTML = '<font size="' + sizeoptions[n] + '">' + sizeoptions[n] + '</font>';
option.className = 'editor_osize';
option.style.textAlign = 'center';
option.cmd = obj.cmd;
option.editorid = editorid;
option.onmouseover = option.onmouseout = option.onmouseup = option.onmousedown = EVENT.menuoption_onmouseevent;
option.onclick = EVENT.formatting_option_onclick;
menu.appendChild(option);
}
}
this.buildForecolorPopup = function(obj, menu) {
var colorout = $(editorid + '_color_out');
colorout.editorid = editorid;
colorout.onclick = EVENT.colorout_onclick;
var table = document.createElement('table');
table.cellPadding = 0;
table.cellSpacing = 0;
table.border = 0;
var i = 0;
for(var hex in coloroptions) {
if(i % 8 == 0) {
var tr = table.insertRow(-1);
}
i++;
var div = document.createElement('div');
div.style.backgroundColor = coloroptions[hex];
var option = tr.insertCell(-1);
option.style.textAlign = 'center';
option.className = 'editor_ocolor';
option.appendChild(div);
option.cmd = obj.cmd;
option.editorid = editorid;
option.colorname = coloroptions[hex];
option.onmouseover = option.onmouseout = option.onmouseup = option.onmousedown = EVENT.menuoption_onmouseevent;
option.onclick = EVENT.coloroption_onclick;
}
menu.appendChild(table);
}
this.menu_context = function(obj, state) {
if(obj.state) {
this.setControlStyle(obj, 'button', 'down');
} else if(state=='mouseout') {
this.setControlStyle(obj, 'button', 'normal');
} else if(state=='mousedown') {
this.setControlStyle(obj, 'popup', 'down');
} else if(state=='mouseup' || state=='mouseover') {
this.setControlStyle(obj, 'button', 'hover');
}
};
this.button_context = function(obj, state, controltype) {
if(undefined(controltype)) {
controltype = 'button';
}
if(obj.state) {
if(state=='mouseover' || state=='mousedown' || state=='mouseup') {
this.setControlStyle(obj, controltype, 'down');
} else if(state=='mouseout') {
this.setControlStyle(obj, 'button', 'selected');
}
}else{
if(state=='mouseout') {
this.setControlStyle(obj, controltype, 'normal');
} else if(state=='mousedown') {
this.setControlStyle(obj, controltype, 'down');
} else if(state=='mouseup' || state=='mouseover') {
this.setControlStyle(obj, controltype, 'hover');
}
}
};
this.setControlStyle = function(obj, controltype, mode) {
if(obj.mode != mode) {
obj.mode = mode;
istyle = 'pi_' + controltype + '_' + obj.mode;
if(typeof istyles != 'undefined' && typeof istyles[istyle] != 'undefined') {
obj.style.background = istyles[istyle][0];
obj.style.color = istyles[istyle][1];
if(controltype != 'menu') {
obj.style.padding = istyles[istyle][2];
}
obj.style.border = istyles[istyle][3];
var tds = findtags(obj, 'td');
for(var i = 0; i < tds.length; i++) {
switch(tds[i].className) {
case 'editor_ofeedback':
tds[i].style.borderRight = (mode == 'normal' ? istyles['pi_menu_normal'][3] : istyles[istyle][3]);
break;
case 'editor_opickbutton':
tds[i].style.borderColor = (mode == 'normal' ? istyles['pi_menu_normal'][0] : istyles[istyle][0]);
break;
case 'editor_apickbutton':
if(obj.state) {
tds[i].style.paddingLeft = istyles['pi_button_normal'][2];
tds[i].style.borderLeft = istyles['pi_button_normal'][3];
} else {
tds[i].style.paddingLeft = istyles[istyle][2];
tds[i].style.borderLeft = istyles[istyle][3];
}
}
}
}
}
};
this.format = function(e, cmd, arg) {
e = doane(e);
if(cmd != 'redo') {
history.add_snapshot(this.getEditorContents());
}
if(cmd.substr(0, 6) == 'resize') {
this.resizeEditor(parseInt(cmd.substr(9), 10) * (parseInt(cmd.substr(7, 1), 10) == '1' ? 1 : -1));
return;
}
this.checkFocus();
if(cmd.substr(0, 4) == 'wrap') {
var ret = this.wrapTags(cmd.substr(6), (cmd.substr(4, 1) == '1' ? true : false));
} else if(cmd.substr(0, 6) == 'custom') {
var ret = this.customTags(cmd.substr(8), cmd.substr(6, 1));
} else if(this[cmd]) {
var ret = this[cmd](e);
} else {
try {
var ret = this.applyFormat(cmd, false, (undefined(arg) ? true : arg));
} catch(e) {
var ret = false;
}
}
if(cmd != 'undo') {
history.add_snapshot(this.getEditorContents());
}
this.setContext(cmd);
this.checkFocus();
return ret;
};
this.insertimage = function(e, img) {
if(undefined(img)) {
img = this.showPrompt(lang['enter_image_url'], 'http://');
}
if(img = this.verifyPrompt(img)) {
return this.applyFormat('insertimage', false, img);
} else {
return false;
}
};
this.wrapTags = function(tagname, useoption, selection) {
if(tagname=='CODE' || tagname=='HTML' || tagname=='PHP') {
this.applyFormat('removeformat');
}
if(undefined(selection)) {
selection = this.getSel();
if(selection === false) {
selection = '';
} else {
selection = new String(selection);
}
}
if(useoption === true) {
var option = this.showPrompt(construct_phrase(lang['enter_tag_option'], ('[' + tagname + ']')), '');
if(option = this.verifyPrompt(option)) {
var opentag = '[' + tagname + '=' + option + ']';
} else {
return false;
}
} else if(useoption !== false) {
var opentag = '[' + tagname + '=' + useoption + ']';
} else {
var opentag = '[' + tagname + ']';
}
var closetag = '[/' + tagname + ']';
var text = opentag + selection + closetag;
this.insertText(text, mb_strlen(opentag), mb_strlen(closetag));
return false;
};
this.customTags = function(tagname, params, selection) {
this.applyFormat('removeformat');
if(custombbcodes[tagname].indexOf(']') == -1) {
custombbcodes[tagname] = '[' + tagname + '][/' + tagname + ']';
}
if(params == 1) {
if(undefined(selection)) {
selection = this.getSel();
if(selection === false) {
selection = '';
} else {
selection = new String(selection);
}
}
var opentag = '[' + tagname + ']';
var closetag = '[/' + tagname + ']';
var text = opentag + selection + closetag;
selection == '' ? this.insertText(custombbcodes[tagname], mb_strlen('[' + tagname + ']'), mb_strlen('[/' + tagname + ']')) : this.insertText(text, mb_strlen(opentag), mb_strlen(closetag));
} else {
this.insertText(custombbcodes[tagname], custombbcodes[tagname].indexOf(']') + 1, mb_strlen('[/' + tagname + ']'));
}
return false;
};
this.showPrompt = function(dialogtxt, defaultval) {
return PHP.trim(new String(prompt(dialogtxt, defaultval)));
};
this.verifyPrompt = function(str) {
if(str=='http://' || str=='null' || str=='undefined' || str=='false' || str=='' || str==null || str==false) {
return false;
} else {
return str;
}
};
this.resizeEditor = function(change) {
var newheight = parseInt(this.editbox.style.height, 10) + change;
if(newheight >= 100) {
this.editbox.style.height = newheight + 'px';
if(change % 99 != 0) {
document.cookie = 'editor_height=' + escape(newheight) + '; path=/';
}
}
};
this.destroyPopup = function(popupname) {
ePopups[popupname].parentNode.removeChild(ePopups[popupname]);
}
this.destroy = function() {
for(var i in eButtons) {
this.setControlStyle(eButtons[i], 'button', 'normal');
}
for(var menu in ePopups) {
this.destroyPopup(menu);
}
if(this.fontoptions) {
for(var i in this.fontoptions) {
if(i != '') {
this.fontoptions[i].parentNode.removeChild(this.fontoptions[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -