📄 editor.js
字号:
findReplace();
return false;
}
// Ctrl+Z:Undo
if (key == "Z"){
goHistory(-1);
return false;
}
// Ctrl+Y:Redo
if (key == "Y"){
goHistory(1);
return false;
}
}
if(!event.ctrlKey && event.keyCode != 90 && event.keyCode != 89) {
if (event.keyCode == 32 || event.keyCode == 13){
saveHistory()
}
}
return true;
break;
default:
if (event.keyCode==13){
var sel = eWebEditor.document.selection.createRange();
sel.pasteHTML("<BR>");
event.cancelBubble = true;
event.returnValue = false;
sel.select();
sel.moveEnd("character", 1);
sel.moveStart("character", 1);
sel.collapse(false);
return false;
}
// 屏蔽事件
if (event.ctrlKey){
// Ctrl+B,I,U
if ((key == "B")||(key == "I")||(key == "U")){
return false;
}
}
}
}
// 取剪粘板中的HTML格式数据
function GetClipboardHTML() {
var oDiv = document.getElementById("eWebEditor_Temp_HTML")
oDiv.innerHTML = "" ;
var oTextRange = document.body.createTextRange() ;
oTextRange.moveToElementText(oDiv) ;
oTextRange.execCommand("Paste") ;
var sData = oDiv.innerHTML ;
oDiv.innerHTML = "" ;
return sData ;
}
// 清除WORD冗余格式并粘贴
function cleanAndPaste( html ) {
// Remove all SPAN tags
html = html.replace(/<\/?SPAN[^>]*>/gi, "" );
// Remove Class attributes
html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// Remove Style attributes
html = html.replace(/<(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
// Remove Lang attributes
html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// Remove XML elements and declarations
html = html.replace(/<\\?\?xml[^>]*>/gi, "") ;
// Remove Tags with XML namespace declarations: <o:p></o:p>
html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
// Replace the
html = html.replace(/ /, " " );
// Transform <P> to <DIV>
var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ; // Different because of a IE 5.0 error
html = html.replace( re, "<div$2</div>" ) ;
insertHTML( html ) ;
}
// 在当前文档位置插入.
function insertHTML(html) {
if (isModeView()) return false;
if (eWebEditor.document.selection.type.toLowerCase() != "none"){
eWebEditor.document.selection.clear() ;
}
if (sCurrMode!="EDIT"){
html=HTMLEncode(html);
}
eWebEditor.document.selection.createRange().pasteHTML(html) ;
}
// 设置编辑器的内容
function setHTML(html) {
ContentEdit.value = html;
switch (sCurrMode){
case "CODE":
eWebEditor.document.designMode="On";
eWebEditor.document.open();
eWebEditor.document.write(config.StyleEditorHeader);
eWebEditor.document.body.innerText=html;
eWebEditor.document.body.contentEditable="true";
eWebEditor.document.close();
bEditMode=false;
break;
case "EDIT":
eWebEditor.document.designMode="On";
eWebEditor.document.open();
eWebEditor.document.write(config.StyleEditorHeader+html);
eWebEditor.document.body.contentEditable="true";
eWebEditor.document.execCommand("2D-Position",true,true);
eWebEditor.document.execCommand("MultipleSelection", true, true);
eWebEditor.document.execCommand("LiveResize", true, true);
eWebEditor.document.close();
doZoom(nCurrZoomSize);
bEditMode=true;
eWebEditor.document.onselectionchange = function () { doToolbar();}
break;
case "TEXT":
eWebEditor.document.designMode="On";
eWebEditor.document.open();
eWebEditor.document.write(config.StyleEditorHeader);
eWebEditor.document.body.innerText=html;
eWebEditor.document.body.contentEditable="true";
eWebEditor.document.close();
bEditMode=false;
break;
case "VIEW":
eWebEditor.document.designMode="off";
eWebEditor.document.open();
eWebEditor.document.write(config.StyleEditorHeader+html);
eWebEditor.document.body.contentEditable="false";
eWebEditor.document.close();
bEditMode=false;
break;
}
eWebEditor.document.body.onpaste = onPaste ;
eWebEditor.document.body.onhelp = onHelp ;
eWebEditor.document.onkeydown = new Function("return onKeyDown(eWebEditor.event);");
eWebEditor.document.oncontextmenu=new Function("return showContextMenu(eWebEditor.event);");
if ((borderShown != "0")&&bEditMode) {
borderShown = "0";
showBorders();
}
initHistory();
}
// 取编辑器的内容
function getHTML() {
var html;
if((sCurrMode=="EDIT")||(sCurrMode=="VIEW")){
html = eWebEditor.document.body.innerHTML;
}else{
html = eWebEditor.document.body.innerText;
}
if (sCurrMode!="TEXT"){
if ((html.toLowerCase()=="<p> </p>")||(html.toLowerCase()=="<p></p>")){
html = "";
}
}
return html;
}
// 在尾部追加内容
function appendHTML(html) {
if (isModeView()) return false;
if(sCurrMode=="EDIT"){
eWebEditor.document.body.innerHTML += html;
}else{
eWebEditor.document.body.innerText += html;
}
}
// 从Word中粘贴,去除格式
function PasteWord(){
if (!validateMode()) return;
eWebEditor.focus();
if (BrowserInfo.IsIE55OrMore)
cleanAndPaste( GetClipboardHTML() ) ;
else if ( confirm( "此功能要求IE5.5版本以上,你当前的浏览器不支持,是否按常规粘贴进行?" ) )
format("paste") ;
eWebEditor.focus();
}
// 粘贴纯文本
function PasteText(){
if (!validateMode()) return;
eWebEditor.focus();
var sText = HTMLEncode( clipboardData.getData("Text") ) ;
insertHTML(sText);
eWebEditor.focus();
}
// 检测当前是否允许编辑
function validateMode() {
if (sCurrMode=="EDIT") return true;
alert("需转换为编辑状态后才能使用编辑功能!");
eWebEditor.focus();
return false;
}
// 检测当前是否在预览模式
function isModeView(){
if (sCurrMode=="VIEW"){
alert("预览时不允许设置编辑区内容。");
return true;
}
return false;
}
// 格式化编辑器中的内容
function format(what,opt) {
if (!validateMode()) return;
eWebEditor.focus();
if (opt=="RemoveFormat") {
what=opt;
opt=null;
}
if (opt==null) eWebEditor.document.execCommand(what);
else eWebEditor.document.execCommand(what,"",opt);
eWebEditor.focus();
}
// 确保焦点在 eWebEditor 内
function VerifyFocus() {
if ( eWebEditor )
eWebEditor.focus();
}
// 改变模式:代码、编辑、文本、预览
function setMode(NewMode){
if (NewMode!=sCurrMode){
if (!BrowserInfo.IsIE55OrMore){
if ((NewMode=="CODE") || (NewMode=="EDIT") || (NewMode=="VIEW")){
alert("HTML编辑模式需要IE5.5版本以上的支持!");
return false;
}
}
if (NewMode=="TEXT"){
if (sCurrMode==ModeEdit.value){
if (!confirm("警告!切换到纯文本模式会丢失您所有的HTML格式,您确认切换吗?")){
return false;
}
}
}
var sBody = "";
switch(sCurrMode){
case "CODE":
if (NewMode=="TEXT"){
eWebEditor_Temp_HTML.innerHTML = eWebEditor.document.body.innerText;
sBody = eWebEditor_Temp_HTML.innerText;
}else{
sBody = eWebEditor.document.body.innerText;
}
break;
case "TEXT":
sBody = eWebEditor.document.body.innerText;
sBody = HTMLEncode(sBody);
break;
case "EDIT":
case "VIEW":
if (NewMode=="TEXT"){
sBody = eWebEditor.document.body.innerText;
}else{
sBody = eWebEditor.document.body.innerHTML;
}
break;
default:
sBody = ContentEdit.value;
break;
}
// 换图片
try{
document.all["eWebEditor_CODE"].className = "StatusBarBtnOff";
document.all["eWebEditor_EDIT"].className = "StatusBarBtnOff";
document.all["eWebEditor_TEXT"].className = "StatusBarBtnOff";
document.all["eWebEditor_VIEW"].className = "StatusBarBtnOff";
document.all["eWebEditor_"+NewMode].className = "StatusBarBtnOn";
}
catch(e){
}
sCurrMode = NewMode;
ModeEdit.value = NewMode;
setHTML(sBody);
disableChildren(eWebEditor_Toolbar);
}
}
// 使工具栏无效
function disableChildren(obj){
if (obj){
obj.disabled=(!bEditMode);
for (var i=0; i<obj.children.length; i++){
disableChildren(obj.children[i]);
}
}
}
// 显示无模式对话框
function ShowDialog(url, width, height, optValidate) {
if (optValidate) {
if (!validateMode()) return;
}
eWebEditor.focus();
var arr = showModalDialog(url, window, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:no;scroll:no;status:no");
eWebEditor.focus();
}
// 全屏编辑
function Maximize() {
if (!validateMode()) return;
window.open("dialog/fullscreen.htm?style="+config.StyleName, 'FullScreen'+sLinkFieldName, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,fullscreen=yes');
}
// 创建或修改超级链接
function createLink(){
if (!validateMode()) return;
if (eWebEditor.document.selection.type == "Control") {
var oControlRange = eWebEditor.document.selection.createRange();
if (oControlRange(0).tagName.toUpperCase() != "IMG") {
alert("链接只能是图片或文本");
return;
}
}
ShowDialog("dialog/hyperlink.htm", 350, 170, true);
}
// 替换特殊字符
function HTMLEncode(text){
text = text.replace(/&/g, "&") ;
text = text.replace(/"/g, """) ;
text = text.replace(/</g, "<") ;
text = text.replace(/>/g, ">") ;
text = text.replace(/'/g, "’") ;
text = text.replace(/\ /g," ");
text = text.replace(/\n/g,"<br>");
text = text.replace(/\t/g," ");
return text;
}
// 插入特殊对象
function insert(what) {
if (!validateMode()) return;
eWebEditor.focus();
var sel = eWebEditor.document.selection.createRange();
switch(what){
case "excel": // 插入EXCEL表格
insertHTML("<object classid='clsid:0002E510-0000-0000-C000-000000000046' id='Spreadsheet1' codebase='file:\\Bob\software\office2000\msowc.cab' width='100%' height='250'><param name='HTMLURL' value><param name='HTMLData' value='<html xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns="http://www.w3.org/TR/REC-html40"><head><style type="text/css"><!--tr{mso-height-source:auto;}td{black-space:nowrap;}.wc4590F88{black-space:nowrap;font-family:宋体;mso-number-format:General;font-size:auto;font-weight:auto;font-style:auto;text-decoration:auto;mso-background-source:auto;mso-pattern:auto;mso-color-source:auto;text-align:general;vertical-align:bottom;border-top:none;border-left:none;border-right:none;border-bottom:none;mso-protection:locked;}--></style></head><body><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:OWCVersion>9.0.0.2710</x:OWCVersion><x:Label Style='border-top:solid .5pt silver;border-left:solid .5pt silver;border-right:solid .5pt silver;border-bottom:solid .5pt silver'><x:Caption>Microsoft Office Spreadsheet</x:Caption> </x:Label><x:Name>Sheet1</x:Name><x:WorksheetOptions><x:Selected/><x:Height>7620</x:Height><x:Width>15240</x:Width><x:TopRowVisible>0</x:TopRowVisible><x:LeftColumnVisible>0</x:LeftColumnVisible> <x:ProtectContents>False</x:ProtectContents> <x:DefaultRowHeight>210</x:DefaultRowHeight> <x:StandardWidth>2389</x:StandardWidth> </x:WorksheetOptions> </x:ExcelWorksheet></x:ExcelWorksheets> <x:MaxHeight>80%</x:MaxHeight><x:MaxWidth>80%</x:MaxWidth></x:ExcelWorkbook></xml><![endif]--><table class=wc4590F88 x:str><col width="56"><tr height="14"><td></td></tr></table></body></html>'> <param name='DataType' value='HTMLDATA'> <param name='AutoFit' value='0'><param name='DisplayColHeaders' value='-1'><param name='DisplayGridlines' value='-1'><param name='DisplayHorizontalScrollBar' value='-1'><param name='DisplayRowHeaders' value='-1'><param name='DisplayTitleBar' value='-1'><param name='DisplayToolbar' value='-1'><param name='DisplayVerticalScrollBar' value='-1'> <param name='EnableAutoCalculate' value='-1'> <param name='EnableEvents' value='-1'><param name='MoveAfterReturn' value='-1'><param name='MoveAfterReturnDirection' value='0'><param name='RightToLeft' value='0'><param name='ViewableRange' value='1:65536'></object>");
break;
case "nowdate": // 插入当前系统日期
var d = new Date();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -