📄 weiseditor.js
字号:
////////////////////WeisEditor2.0////////////Class///////////////////
//Power By Gloot CopyRight @2008
//Edit Section for weiseditor.com
//Blog http://blog.sina.com.cn/tecz
//QQ 345268267
///////////////////////////////////////////////////////////
var WeisEditor = function(){};
var $ = function(el){return document.getElementById(el);};
window.isIE = (navigator.appName.toLowerCase() == "microsoft internet explorer");
function hashtable(key, value){this.key = key;this.value = value;};
WeisEditor.Array = new Array();
String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g,'');};
function loadJS(F){document.write('<script type=\'text/javascript\' src=\''+F+'\'></scr'+'ipt>');};
window.PWindow = (function(){var win = null;win = window;while(win!=win.parent){if (!win.parent){return win;}if (win.parent.document.getElementsByTagName('frameset').length>0) break;win = win.parent;}return win;})();
window.PDocument = window.PWindow.document;
var $P = function(el){return window.PDocument.getElementById(el);};
var isComplete = function(){var rev=null;for(var i=0;i<arguments.length;i++){var args=arguments[i];try{rev=(args.readyState==4);}catch(e){rev=(args.readyState=='complete');} } return rev;};
var GetParaStr = function(strname){var hrefstr,pos,parastr,para,tempstr;hrefstr = window.location.href;pos = hrefstr.indexOf("?");parastr = hrefstr.substring(pos+1);para = parastr.split("&");tempstr="";for(i=0;i<para.length;i++) {tempstr = para[i];pos = tempstr.indexOf("=");if(tempstr.substring(0,pos) == strname){tempstr = tempstr.substring(pos+1);pos = tempstr.indexOf("#");if (pos > 0)return tempstr.substring(0,pos);return tempstr;} }return '';};
WeisEditor.prototype.Config = {
HtmlEdit:null,
lang:"en",
EditorID:"HtmlEdit",
HidSaveID:"hidAreaBox",
Hasfocus:false,
BackColor:"",
BackImage:"",
WEditorMode:false,
EditMode:"EDIT",
CopyPanel:"",
HideScrollHeight:0,
CodeHTML:"",
SelectedPoint:null,
CodeType:{Text:"",Value:""},
Document:null,
WeisEditorContent:function()
{
var hidObj = parent.document.getElementById(Editor.Config.HidSaveID);
if (!hidObj)
return;
if (Editor.Config.WEditorMode)
{
hidObj.value = Editor.replaceSPCTags(true);
}
else
{
hidObj.value = Editor.replaceSPCTags(false);
}
},
Selection:function()
{
if (document.selection)
{
return Editor.Config.Document.selection;
}
else if(window.getSelection)
{
return Editor.Config.HtmlEdit.getSelection();
}
else if(document.getSelection)
{
return Editor.Config.Document.getSelection();
}
else
{
return null;
}
},
GetRange:function()
{
if (!Editor.Config.Selection()) return;
var Range = null;
if (!window.isIE)
Range = Editor.Config.SelectedPoint != null ? Editor.Config.SelectedPoint : Editor.Config.Selection().getRangeAt(0);
else Range = Editor.Config.SelectedPoint != null ? Editor.Config.SelectedPoint : Editor.Config.Selection().createRange();
return Range;
},
Range:{
startContainer:null,
startOffset:0,
endContainer:null,
endOffset:0,
collapsed:false,
firstNode:null,
lastNode:null,
Rinit:function()
{
this.startContainer=null,
this.startOffset=0,
this.endContainer=null,
this.endOffset=0,
this.collapsed=false,
this.firstNode = null,
this.lastNode = null
},
setStart:function(N,S,r)
{
this.startContainer = N;
this.startOffset = S;
if (!this.endContainer)
{
this.endContainer = N;
this.endOffset = S;
}
this.isCollapsed();
},
setEnd:function(N,E,r)
{
this.endContainer = N;
this.endOffset = E;
if (!this.startContainer)
{
this.startContainer = N;
this.startOffset = E;
}
this.isCollapsed();
},
setEndAfter:function(N,r)
{
if(window.isIE)this.setStart(N.parentNode,Editor.Config.GetIndexNode(N)+1);else r.setEndAfter(N);
},
setStartAfter:function(N,r)
{
if(window.isIE)this.setEnd(N.parentNode,Editor.Config.GetIndexNode(N)+1);else r.setStartAfter(N);
},
isCollapsed:function()
{
this.collapsed = (this.startContainer==this.endContainer&&this.startOffset==this.endOffset);
},
getNodeAndOffsetWhenFocus:function(nodeID)
{
if (nodeID.previousSibling&&nodeID.previousSibling.nodeType==1&&nodeID.nextSibling&&nodeID.nextSibling.nodeType==1)
{
this.endContainer = this.startContainer = nodeID.parentNode;
this.endOffset = this.startOffset = Editor.Config.GetIndexNode(nodeID)-1+1; //2008-1-6 Change +1
}
else if (nodeID.previousSibling&&nodeID.previousSibling.nodeType==1&&nodeID.nextSibling&&nodeID.nextSibling.nodeType==3)
{
this.endContainer = this.startContainer = nodeID.nextSibling;
this.endOffset = this.startOffset = 0;
}
else if (nodeID.previousSibling&&nodeID.previousSibling.nodeType==3&&nodeID.nextSibling&&nodeID.nextSibling.nodeType==1)
{
this.endContainer = this.startContainer = nodeID.previousSibling;
this.endOffset = this.startOffset = nodeID.previousSibling.nodeValue.length;
}
else if (nodeID.previousSibling&&nodeID.previousSibling.nodeType==3&&nodeID.nextSibling&&nodeID.nextSibling.nodeType==3)
{
for (var p=nodeID.previousSibling;p!=null;p=p.previousSibling)
{
if(p.nodeType==1)
break;
else if (p.nodeType==3)
this.endOffset += p.nodeValue.length;
}
this.startOffset = this.endOffset;
nodeID.previousSibling.data = nodeID.previousSibling.data + nodeID.nextSibling.data;
this.endContainer = this.startContainer = nodeID.previousSibling;
nodeID.parentNode.removeChild(nodeID.nextSibling);
}
else if (!nodeID.previousSibling)
{
if(nodeID.nextSibling&&nodeID.nextSibling.nodeType==3)
{
this.endContainer = this.startContainer = nodeID.nextSibling;
this.endOffset = this.startOffset = 0;
}
else if(nodeID.nextSibling&&nodeID.nextSibling.nodeType==1)
{
this.endContainer = this.startContainer = nodeID.parentNode;
this.endOffset = this.startOffset = Editor.Config.GetIndexNode(nodeID);
}
else if (!nodeID.nextSibling)
{
this.endContainer = this.startContainer = nodeID.parentNode;
this.endOffset = this.startOffset = 0;
}
}
else if (!nodeID.nextSibling)
{
this.endContainer = this.startContainer = null;
this.endOffset = this.startOffset = 0;
if(nodeID.previousSibling&&nodeID.previousSibling.nodeType==3)
{
this.endContainer = this.startContainer = nodeID.previousSibling;
for (var p=nodeID.previousSibling;p!=null;p=p.previousSibling)
{
if(p.nodeType==1)
break;
else if (p.nodeType==3)
this.endOffset += p.nodeValue.length;
}
this.startOffset = this.endOffset;
}
else if(nodeID.previousSibling&&nodeID.previousSibling.nodeType==1)
{
this.endContainer = this.startContainer = nodeID.parentNode;
this.endOffset = this.startOffset = Editor.Config.GetIndexNode(nodeID);
}
else if (!nodeID.previousSibling)
{
this.endContainer = this.startContainer = nodeID.parentNode;
this.endOffset = this.startOffset = 0;
}
}
nodeID.parentNode.removeChild(nodeID);
},
getNodeAndOffsetStart:function(nodeID)
{
if (nodeID.nextSibling)
{
this.firstNode = nodeID.nextSibling;
if (this.firstNode.nodeType==1)
{
this.startContainer = this.firstNode.parentNode;
this.startOffset = Editor.Config.GetIndexNode(nodeID);
}
else if(this.firstNode.nodeType==3)
{
this.startContainer = this.firstNode;
for (var p=nodeID.previousSibling;p!=null;p=p.previousSibling)
{
if(p.nodeType==1)
break;
else if(p.nodeType==3)
this.startOffset += p.nodeValue.length;
}
}
}
else
{
if (nodeID.previousSibling)
{
if (nodeID.previousSibling.nodeType==1)
{
this.startContainer = nodeID.parentNode;
this.startOffset = Editor.Config.GetIndexNode(nodeID)-1;
}
else if (nodeID.previousSibling.nodeType==3)
{
this.startContainer = nodeID.previousSibling;
for (var p=nodeID.previousSibling;p!=null;p=p.previousSibling)
{
if(p.nodeType==1)
break;
else if(p.nodeType==3)
this.startOffset += p.nodeValue.length;
}
}
}
}
nodeID.parentNode.removeChild(nodeID);
},
getNodeAndOffsetEnd:function(nodeID)
{
if (nodeID.previousSibling)
{
this.endOffset = 0;
this.lastNode = nodeID.previousSibling;
if (this.lastNode.nodeType==1)
{
this.endContainer = this.lastNode.parentNode;
this.endOffset = Editor.Config.GetIndexNode(this.lastNode)+1;//Has split to three node So -1
if (this.firstNode!=null&&this.firstNode.nodeType==3&&this.firstNode.previousSibling&&this.firstNode.previousSibling.nodeType==3)
this.endOffset --;
}
else if(this.lastNode.nodeType==3)
{
this.endContainer = this.lastNode;
for (var p=nodeID.previousSibling;p!=null;p=p.previousSibling)
{
if(p.nodeType==1)
break;
else if (p.nodeType==3)
this.endOffset += p.nodeValue.length;
}
}
}
else
{
if (nodeID.nextSibling)
{
if (nodeID.nextSibling.nodeType==1)
{
this.endContainer = nodeID.parentNode;
this.endOffset = 0;
}
else if (nodeID.nextSibling.nodeType==3)
{
this.endContainer = nodeID.nextSibling;
this.endOffset = 0
}
}
}
nodeID.parentNode.removeChild(nodeID);
},
clearDomrangeNode:function()
{
if (this.firstNode!=null&&this.firstNode.nodeType==3&&this.firstNode.previousSibling&&this.firstNode.previousSibling.nodeType==3)
{
this.firstNode.data = this.firstNode.previousSibling.data + this.firstNode.data;
this.firstNode.parentNode.removeChild(this.firstNode.previousSibling);
}
if (this.lastNode!=null&&this.lastNode.nodeType==3&&this.lastNode.nextSibling&&this.lastNode.nextSibling.nodeType==3)
{
this.lastNode.data = this.lastNode.data + this.lastNode.nextSibling.data;
this.lastNode.parentNode.removeChild(this.lastNode.nextSibling);
}
},
setState:function()
{
this.Rinit();
Editor.Config.HtmlEdit.focus();
if (window.isIE)
{
if (Editor.Config.Selection().type.toLowerCase()=='text')
{
var range = Editor.Config.GetRange().duplicate();
range.collapse(true);
range.pasteHTML('<b id=\'_____xeficghuionh83sloOxpddX__b___\'></b>');
var nodeID = Editor.Config.Document.getElementById('_____xeficghuionh83sloOxpddX__b___');
this.getNodeAndOffsetStart(nodeID);
range = Editor.Config.GetRange().duplicate();
range.collapse(false);
range.pasteHTML('<b id=\'_____xeficghuionh83sloOxpddX__b2___\'></b>');
var nodeID = Editor.Config.Document.getElementById('_____xeficghuionh83sloOxpddX__b2___');
this.getNodeAndOffsetEnd(nodeID);
this.clearDomrangeNode();
this.setStart(this.startContainer,this.startOffset);
this.setEnd(this.endContainer,this.endOffset);
Editor.Config.getSelection.addRange();
}
else if(Editor.Config.Selection().type.toLowerCase()=='none')
{
range = Editor.Config.GetRange().duplicate();
range.pasteHTML('<b id=\'_____xeficghuionh83sloOxpddX__b3___\'></b>');
var nodeID = Editor.Config.Document.getElementById('_____xeficghuionh83sloOxpddX__b3___'); //OK
if (!nodeID)
{
this.startContainer = this.endContainer = range.parentElement();
this.startOffset = this.endOffset = 0;
var N = Editor.Config.lookForwardById(range.parentElement(),'_____xeficghuionh83sloOxpddX__b3___');
N.parentNode.removeChild(N);
}
else
{
this.getNodeAndOffsetWhenFocus(nodeID);
}
this.setStart(this.startContainer,this.startOffset);
this.setEnd(this.endContainer,this.endOffset);
Editor.Config.getSelection.addRange();
}
else
{
var WERange = Editor.Config.GetRange();
this.firstNode = WERange.item(0);
this.lastNode = WERange.item(WERange.length-1);
this.startContainer = this.firstNode.parentNode;
this.endContainer = this.lastNode.parentNode;
this.startOffset = Editor.Config.GetIndexNode(this.firstNode);
this.endOffset = Editor.Config.GetIndexNode(this.lastNode)+1;
this.collapsed = false;
}
}
else //firefox
{
var range = Editor.Config.GetRange();
if (range.collapsed)
{
var rangeFocus = range.cloneRange();
var _B = Editor.Config.Document.createElement('B');
_B.id = '_____xeficghuionh83sloOxpddX__b3___';
rangeFocus.deleteContents();
rangeFocus.insertNode(_B);
rangeFocus.selectNode(_B);
var sel = Editor.Config.Selection();
sel.removeAllRanges();
sel.addRange(rangeFocus);
var nodeID = Editor.Config.Document.getElementById('_____xeficghuionh83sloOxpddX__b3___');
Editor.Config.DOMNodeFresh(nodeID);
this.getNodeAndOffsetWhenFocus(nodeID);
this.setStart(this.startContainer,this.startOffset);
this.setEnd(this.endContainer,this.endOffset);
Editor.Config.getSelection.addRange(rangeFocus);
}
else
{
this.startContainer = range.startContainer;
this.startOffset = range.startOffset;
this.endContainer = range.endContainer;
this.endOffset = range.endOffset;
this.collapsed = range.collapsed;
}
////////////////////////////////end//////////////////////////////////
}
}
},
DOMNodeFresh:function(nodeID) //firefox
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -