📄 editor.js
字号:
/*
* Samuels [Xoops Project]
* Based on Justin Koivisto [W.A. Fisher Interactive] Koivi editor
*
*
// $Id: editor.js,v 1.1.2.1 2005/06/04 02:11:53 phppp Exp $
*
*/
var editors = new Array();//ACTIVE EDITORS
var isie;
var url;
//init wysiwyg editor
function XK_init(id, isiexplore, editorurl,textdirection)
{
isie=isiexplore;
url=editorurl;
var doc=document.getElementById("iframe"+id).contentWindow.document;
if(isie)
{
if (document.readyState != 'complete')
{
setTimeout(function(){ XK_init(id, isiexplore, editorurl,textdirection);},100);
return;
}
doc.designMode="On";
}
else
{
//try if mozilla is ready to go
try {setTimeout(function(){doc.designMode="On";doc.execCommand("usecss",false,true);},1000);}
//I don't know how to do if it fails
catch (e) {alert("ERROR: can't load the editor, please refresh the page");}
}
if (!XK_registered(id)){editors[editors.length] = id;}
//update hidden text fields on submit
XK_onSubmitHandler(id);
//update iframes with textfields values
XK_insertText(id);
//add hide floating menus event handlers;
XK_attachEvent(document,"mousedown",XK_hideMenus);
XK_attachEvent(doc,"mousedown",XK_hideMenus);
//contextmenu:
contextmenu =new XK_contextMenu(id);
//set text direction
if(textdirection=="rtl")XK_textDirection(textdirection,id);
//destroy <p></p> on enter
if(isie)XK_destroyPTag(id);
};
function XK_appendXoopsCss(id,themeCss)
{
if(themeCss)
{
if(document.getElementById("cssEnabled"+id).checked)XK_AppendCss(id,themeCss,"xoopsTheme");
else XK_DeleteCss(id,"xoopsTheme");
}
}
function XK_destroyPTag(id)
{
//disable manual undo "ctrl+z" because sometimes iexplore goes crazy.NOT NEEDED BY NOW
//if (doc.event.ctrlKey && doc.event.keyCode == 90)return false;
var doc = document.getElementById("iframe"+id).contentWindow;
try{var enabled = document.getElementById("ptagenabled"+id).checked;}catch(e){var enabled=false;};
if (!enabled)
{
doc.document.onkeydown = function ()
{
if ( (doc.event.keyCode == 13) )
{
if ( !(XK_excludedTags(id)) && !(doc.document.queryCommandState( "insertunorderedlist" )) && !(doc.document.queryCommandState( "insertorderedlist" )))
{
XK_cancelBubble(doc.event);
XK_insertHTML("<br>",id);
return false;
}else return;
}
else return;
};
}
else
{
doc.document.onkeydown = function (){};
}
doc.focus();
};
function XK_excludedTags(id)
{
var exludedTags=["H1","H2","H3","H4","H5","H6","PRE","ADDRESS","BLOCKQUOTE"];
for(i=0;i< exludedTags.length;i++)
{
if(XK_isInsideThisTag(id,exludedTags[i])) return true;
}
return false;
}
//update iframe on init
function XK_insertText(id)
{
//replace <strong>,<me> tags by <b><li> and others used by mozilla
var my_content=XK_toWYSIWYG(document.getElementById(id).value);
var doc=document.getElementById("iframe"+id).contentWindow;
doc.document.open();
doc.document.write(my_content);
doc.document.close();
};
function XK_registered(id)
{
var found = false;
for(i=0;i<editors.length;i++)
{
if ((editors[i]).toUpperCase() == id.toUpperCase())
{
found = true;
break;
}
}
return(found);
};
//update textfields on submit
function XK_onSubmitHandler(id)
{
var sTemp = "";
oForm = document.getElementById(id).form;
if(oForm.onsubmit != null) {
sTemp = oForm.onsubmit.toString();
iStart = sTemp.indexOf("{") + 2;
sTemp = sTemp.substr(iStart,sTemp.length-iStart-2);
}
if (sTemp.indexOf("XK_updateFields();") == -1)
{
oForm.onsubmit = new Function("XK_updateFields();" + sTemp);
}
};
function XK_updateFields()
{
var text;
//var html_source;
for (i=0; i<editors.length; i++)
{
//if html mode toggle to wysiwyg
if (document.getElementById("iframe"+editors[i]).style.display=="none")XK_doToggleView(editors[i]);
var doc=document.getElementById("iframe"+editors[i]).contentWindow.document;
//I need to sanitize the HTML, so it's a must to take the DOM tree from the iframe
var text=(document.getElementById(editors[i]).value.indexOf("<body")!=-1)?XK_getXHTML(doc):XK_getXHTML(doc.body);
text=XK_removeLineBreaks(text);
//now I'm sure the textfield is updated with the cleaned HTML
document.getElementById(editors[i]).value=text;
}
};
function XK_maximizeEditor(id)
{
var doc=document.getElementById("iframe"+id).contentWindow.document;
if(document.getElementById("alleditor"+id).style.position!="absolute")
{
document.getElementById("floatButton"+id).style.display="none";
document.getElementById("alleditor"+id).style.position="absolute";
document.getElementById("alleditor"+id).style.width=document.body.clientWidth-4+"px";
//push editor to top
document.getElementById("alleditor"+id).style.left="0px";
document.getElementById("alleditor"+id).style.top="0px";
}
else
{
document.getElementById("alleditor"+id).style.position="";
document.getElementById("alleditor"+id).style.width="100%";
document.getElementById("floatButton"+id).style.display="";
}
if(!isie){doc.designMode="On";doc.execCommand("usecss",false,true);}
//go to top of the page
document.documentElement.scrollTop=0;
document.getElementById("iframe"+id).contentWindow.focus();
}
function XK_doTextFormat(command, option,id,value)
{
var doc= document.getElementById("iframe"+id).contentWindow;
switch(command)
{
//deprecated function, I'll make an advanced url dialog some day...
case "createlink":
if(XK_getSelectedText(id)==''){alert('Select content first');break;}
if(!isie)
{
var iurl=prompt("Enter a URL:", "");
if(document.getElementById("iframe"+id).contentWindow.document.queryCommandEnabled(command))
{
try{doc.document.execCommand("createlink",false,iurl);}catch(e){};
return true;
}
else return false;
}
else try{doc.document.execCommand("createlink",false);}catch(e){};
break;
case "Quote":
case "Code":
XK_addCodes(command,id);
break;
case "insertimage":
XK_InsertImage(id);
break;
case "fontsize":
case "fontname":
case "formatblock":
XK_fontFormat(command,option,id);
break;
default:
if(doc.document.queryCommandEnabled(command))
{
try{doc.document.execCommand(command,false,value);}catch(e){};
doc.focus();
return true;
}
else
return false;
break;
}
};
function XK_fontFormat(command,option,id)
{
var doc= document.getElementById("iframe"+id).contentWindow;
if(doc.document.queryCommandEnabled(command))
{
try{doc.document.execCommand(command,false,option);}catch(e){};
document.getElementById(command+id).value="";
doc.focus();
}
};
//called from colorpalette
function XK_applyColor(id,color)
{
var option=document.getElementById('coloroption'+id).value;
switch(option)
{
case "forecolor":
XK_foreColor(id,color);
break;
case "hilitecolor":
XK_hiliteColor(id,color);
break;
case "cellcolor":
XK_CellColor(id,color);
break;
}
return;
};
function XK_countCharacters(id)
{
var text=document.getElementById("iframe"+id).contentWindow.document.body.innerHTML;
alert(text.length);
}
function XK_color(id,buttonid,option)
{
document.getElementById('coloroption'+id).value=option;
if (option!='cellcolor')
XK_showHideDiv(id, buttonid, 'colorPalette');
else if(XK_isInsideCell(id))
XK_showHideDiv(id, buttonid, 'colorPalette');
};
function XK_foreColor(id, color)
{
try{document.getElementById("iframe"+id).contentWindow.document.execCommand('forecolor',false, color);}catch(e){};
return;
};
function XK_hiliteColor(id, color)
{
var doc= document.getElementById("iframe"+id).contentWindow;
if (!isie)
{
try
{
doc.document.execCommand('usecss',false,false);
doc.document.execCommand('hilitecolor', false, color);
doc.document.execCommand("usecss",false,true);
}
catch(e){};
}
else try{doc.document.execCommand('backcolor', false, color);}catch(e){};
return;
};
function XK_insertDate(id)
{
var date= new Date();
document.getElementById("iframe"+id).contentWindow.focus();
XK_insertHTML(date.toLocaleString(),id);
};
//makes a XoopsCode div or XoopsQuote div and puts inside it the selected text
function XK_addCodes(type,id)
{
document.getElementById("iframe"+id).contentWindow.focus();
var text = XK_getSelectedText(id);
if (text=="")text=" ";
var text="<div style=\"border:1px solid\" class=\"xoops"+type+"\" >"+text+"</div>";
XK_insertHTML(text,id);
};
function XK_textDirection(direction,id)
{
//simplest version
document.getElementById("iframe"+id).contentWindow.document.dir=direction;
document.getElementById(id).dir=direction;
};
function XK_InsertImage(id,src,alt)
{
document.getElementById("iframe"+id).contentWindow.focus();
if (src==null)
{
var image = prompt("Image source.",'http://');
alt = " ";
}
else var image = src;
if (image!='http://' && image!=null)
XK_insertHTML("<img src="+image+" alt=\""+alt+" \">",id);
return;
};
function XK_insertAnchor(id)
{
var name = prompt("Anchor.",'id');
document.getElementById("iframe"+id).contentWindow.focus();
XK_insertHTML("<img alt=\x22anchor\x22 id=\""+name+"\" title=\""+name+"\" src=\""+url+"/skins/common/anchor.gif\" />",id);
return;
};
function XK_print(id)
{
var doc= document.getElementById("iframe"+id).contentWindow;
if (isie){try{doc.document.execCommand('Print');}catch(e){};}
else doc.print();
};
function XK_insertSymbol(symbol,id)
{
document.getElementById('insertsymbol'+id).value="";
document.getElementById("iframe"+id).contentWindow.focus();
XK_insertHTML(symbol,id);
};
function XK_checkspell()
{
if(isie)
{
try
{
var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
tmpis.CheckAllLinkedDocuments(document);
}
catch(exception)
{
if(exception.number==-2146827859)
{
if(confirm("ieSpell not detected. Click Ok to go to download page."))
window.open("http://www.iespell.com/download.php","Download");
}
else alert("Error Loading ieSpell: Exception " + exception.number);
}
}
else window.open("http://spellbound.sourceforge.net./install.html#header","SpellBound");
};
//change between code and wysiwyg modes
function XK_doToggleView(id)
{
var doc= document.getElementById("iframe"+id).contentWindow.document;
if(document.getElementById("iframe"+id).style.display!="none")
{
//hide editor
if (!isie)doc.designMode="Off";
document.getElementById("iframe"+id).style.display="none";
document.getElementById("toolbar"+id).style.display="none";
//show textarea with code
document.getElementById(id).style.display="block";
//get xhtml
var text=(document.getElementById(id).value.indexOf("<body")!=-1)?XK_getXHTML(doc):XK_getXHTML(doc.body);
//add linebreaks to tags for better reading
document.getElementById(id).value=XK_addLineBreaks(text);
document.getElementById(id).focus();
}
else
{
var text=XK_toWYSIWYG(document.getElementById(id).value);
//if it's a complete html page
if(text.indexOf("<body")!=-1)
{
if(isie)
{
doc.open();
doc.write(text);
doc.close();
}
else
{
//stupid gecko "uncaught exception" if I try to open the doc to write
//more gecko workarounds, I'm saving the head tag
var headCode=text.split(/<\/head>/gi);
if(headCode[0]!='')
{
headCode=headCode[0].split(/<head>/gi);
headCode=(headCode[1])?headCode[1]:'';
text=text.replace(headCode,'');
}
//here it comes the body tag attributes, gecko lose them
var bodyCode=text.split(/<body/gi);
bodyCode=bodyCode[1].split('>');
bodyCode=(bodyCode[0])?bodyCode[0]:'';
bodyCode=bodyCode.replace(/^\s*(.*)/, "$1");
bodyCode=bodyCode.replace(/(.*?)\s*$/, "$1");
bodyCode=bodyCode.replace(/\s/g,"=");
bodyAttrs=bodyCode.split('=');
//insert the html
doc.body.innerHTML = text;
//update head saved before
doc.getElementsByTagName('head')[0].innerHTML=headCode;
//if there are some body attribute, just update it one by one
if(bodyAttrs.length >1)
{
bodyHTML=doc.getElementsByTagName('body')[0];
for(i=0;i<bodyAttrs.length;i++)
{
attribute=bodyAttrs[i];
i++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -