📄 webeditor.js
字号:
// 当前模式
var sCurrMode = null;
var bEditMode = null;
// 连接对象
var oLinkField = null;
var sBaseUrl = document.location.protocol + '//' + document.location.host ;
// 浏览器版本检测
var BrowserInfo = new Object();
BrowserInfo.MajorVer = navigator.appVersion.match(/MSIE (.)/)[1] ;
BrowserInfo.MinorVer = navigator.appVersion.match(/MSIE .\.(.)/)[1] ;
BrowserInfo.IsIE55OrMore = BrowserInfo.MajorVer >= 6 || ( BrowserInfo.MajorVer >= 5 && BrowserInfo.MinorVer >= 5 ) ;
var yToolbars = new Array(); // 工具栏数组
// 当文档完全调入时,进行初始化
var bInitialized = false;
$(function(){
var i, s, curr;
if (bInitialized) return;
eWebEditor=document.getElementById("eWebEditor");
// 初始每个工具栏
for (i=0; i<document.body.all.length;i++){
curr=document.body.all[i];
if (curr.className == "yToolbar"){
InitTB(curr);
}
}
//关联的字段
oLinkField = parent.document.getElementsByName(sLinkFieldName)[0];
//获取关联字段的内容
ContentFlag = document.getElementById("ContentFlag");
ContentEdit = document.getElementById("ContentEdit");
ContentLoad = document.getElementById("ContentLoad");
if (ContentFlag.value=="0") {
ContentEdit.value = oLinkField.value;
ContentLoad.value = oLinkField.value;
ContentFlag.value = "1";
}
//编辑器的模式及处理内容
setMode(config.InitMode);
setLinkedField();
eWebEditor.document.body.onpaste = onPaste ;
})
// 初始化一个工具栏上的按钮
function InitBtn(btn) {
btn.onmouseover = BtnMouseOver;
btn.onmouseout = BtnMouseOut;
btn.onmousedown = BtnMouseDown;
btn.onmouseup = BtnMouseUp;
btn.ondragstart = YCancelEvent;
btn.onselectstart = YCancelEvent;
btn.onselect = YCancelEvent;
btn.YUSERONCLICK = btn.onclick;
btn.onclick = YCancelEvent;
btn.YINITIALIZED = true;
return true;
}
//Initialize a toolbar.
function InitTB(y) {
// Set initial size of toolbar to that of the handle
y.TBWidth = 0;
// Populate the toolbar with its contents
if (! PopulateTB(y)) return false;
// Set the toolbar width and put in the handle
y.style.posWidth = y.TBWidth;
return true;
}
// Hander that simply cancels an event
function YCancelEvent() {
event.returnValue=false;
event.cancelBubble=true;
return false;
}
// Toolbar button onmouseover handler
function BtnMouseOver() {
if (event.srcElement.tagName != "IMG") return false;
var image = event.srcElement;
var element = image.parentElement;
// Change button look based on current state of image.
if (image.className == "Ico") element.className = "BtnMouseOverUp";
else if (image.className == "IcoDown") element.className = "BtnMouseOverDown";
event.cancelBubble = true;
}
// Toolbar button onmouseout handler
function BtnMouseOut() {
if (event.srcElement.tagName != "IMG") {
event.cancelBubble = true;
return false;
}
var image = event.srcElement;
var element = image.parentElement;
yRaisedElement = null;
element.className = "Btn";
image.className = "Ico";
event.cancelBubble = true;
}
// Toolbar button onmousedown handler
function BtnMouseDown() {
if (event.srcElement.tagName != "IMG") {
event.cancelBubble = true;
event.returnValue=false;
return false;
}
var image = event.srcElement;
var element = image.parentElement;
element.className = "BtnMouseOverDown";
image.className = "IcoDown";
event.cancelBubble = true;
event.returnValue=false;
return false;
}
// Toolbar button onmouseup handler
function BtnMouseUp() {
if (event.srcElement.tagName != "IMG") {
event.cancelBubble = true;
return false;
}
var image = event.srcElement;
var element = image.parentElement;
if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous()");
element.className = "BtnMouseOverUp";
image.className = "Ico";
event.cancelBubble = true;
return false;
}
// Populate a toolbar with the elements within it
function PopulateTB(y) {
var i, elements, element;
// Iterate through all the top-level elements in the toolbar
elements = y.children;
for (i=0; i<elements.length; i++) {
element = elements[i];
if (element.tagName == "SCRIPT" || element.tagName == "!") continue;
switch (element.className) {
case "Btn":
if (element.YINITIALIZED == null) {
if (! InitBtn(element)) {
alert("Problem initializing:" + element.id);
return false;
}
}
element.style.posLeft = y.TBWidth;
y.TBWidth += element.offsetWidth + 1;
break;
case "TBGen":
element.style.posLeft = y.TBWidth;
y.TBWidth += element.offsetWidth + 1;
break;
case "TBHandle":
element.style.posLeft = 2;
y.TBWidth += element.offsetWidth + 7;
break;
default:
alert("Invalid class: " + element.className + " on Element: " + element.id + " <" + element.tagName + ">");
return false;
}
}
y.TBWidth += 1;
return true;
}
// 设置所属表单的提交或reset事件
function setLinkedField() {
if (! oLinkField) return ;
var oForm = oLinkField.form ;
if (!oForm) return ;
// 附加reset、submit事件
oForm.attachEvent("onreset", AttachReset) ;
oForm.attachEvent("onsubmit", AttachSubmit) ;
//附加AttachReset事件到resetEditor属性上
if (! oForm.resetEditor) oForm.resetEditor = new Array() ;
oForm.resetEditor[oForm.resetEditor.length] = AttachReset ;
if (! oForm.originalReset) {
oForm.originalReset = oForm.reset ;
oForm.reset = function() {
if (this.resetEditor) {
for (var i = 0 ; i < this.resetEditor.length ; i++) {
this.resetEditor[i]() ;
}
}
this.originalReset() ;
}
}
}
// 附加submit提交事件,大表单数据提交,远程文件获取,保存eWebEditor中的内容
var bDoneAutoRemote = false;
function AttachSubmit() {
var oForm = oLinkField.form ;
if (!oForm) return;
var html = getHTML();
ContentEdit.value = html;
if (sCurrMode=="TEXT"){
html = HTMLEncode(html);
}
if(oLinkField.value!=null){
oLinkField.value = html;
}
}
// 附加Reset事件
function AttachReset() {
if (!bEditMode) setMode('EDIT');
if(bEditMode){
eWebEditor.document.body.innerHTML = ContentLoad.value;
}else{
eWebEditor.document.body.innerText = ContentLoad.value;
}
}
// 粘贴时自动检测是否来源于Word格式
function onPaste() {
if (config.AutoDetectPasteFromWord && BrowserInfo.IsIE55OrMore) {
var sHTML = GetClipboardHTML() ;
var re = /<\w[^>]* class="?MsoNormal"?/gi ;
if ( re.test( sHTML ) )
{
if ( confirm( "你要粘贴的内容好象是从Word中拷出来的,是否要先清除Word格式再粘贴?" ) )
{
cleanAndPaste( sHTML ) ;
return false ;
}
}
}
else
return true ;
}
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 ;
}
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 (!validateMode()) return;
if (eWebEditor.document.selection.type.toLowerCase() != "none")
eWebEditor.document.selection.clear() ;
eWebEditor.document.selection.createRange().pasteHTML(html) ;
}
// 设置编辑器的内容
function setHTML(html) {
if (!validateMode()) return;
ContentEdit.value = html;
if(bEditMode){
eWebEditor.document.body.innerHTML = html;
}else{
eWebEditor.document.body.innerText = html;
}
}
// 取编辑器的内容
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -