📄 webeditor.js
字号:
function getHTML() {
var html;
if(bEditMode){
html = eWebEditor.document.body.innerHTML;
}else{
html = eWebEditor.document.body.innerText;
}
var re = new RegExp(sBaseUrl.replace(/\//,"\/"),"gi");
//alert("re:"+re);
html = html.replace(re, "");
if ((html.toLowerCase()=="<p> </p>")||(html.toLowerCase()=="<p></p>")){
html = "";
}
return html;
}
// 在尾部追加内容
function appendHTML(html) {
if (!validateMode()) return;
if(bEditMode){
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 (bEditMode) return true;
alert("需转换为编辑状态后才能使用编辑功能!");
eWebEditor.focus();
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){
var sBody = "";
var msg="";
switch(sCurrMode){
case "CODE":
sBody = eWebEditor.document.body.innerText;
break;
case "EDIT":
case "VIEW":
sBody = eWebEditor.document.body.innerHTML;
break;
//刚打开页面
default:
sBody = ContentEdit.value;
break;
}
// 更换状态按钮样式。(换图片?)
try{
document.all["eWebEditor_CODE"].className = "p-editor-statusbar-off";
document.all["eWebEditor_EDIT"].className = "p-editor-statusbar-off";
document.all["eWebEditor_VIEW"].className = "p-editor-statusbar-off";
document.all["eWebEditor_"+NewMode].className = "p-editor-statusbar-on";
}
catch(e){
}
// 换内容
switch (NewMode){
case "CODE":
eWebEditor.document.designMode="On";
eWebEditor.document.open();
eWebEditor.document.write(config.StyleEditorHeader);
//$(#eWebEditor).text(sBody);
eWebEditor.document.body.innerText=sBody;
eWebEditor.document.body.contentEditable="true";
eWebEditor.document.oncontextmenu=new Function("return showContextMenu(eWebEditor.event);");
eWebEditor.document.close();
bEditMode=false;
break;
case "EDIT":
eWebEditor.document.designMode="On";
eWebEditor.document.open();
eWebEditor.document.write(config.StyleEditorHeader);
eWebEditor.document.body.innerHTML=sBody;
eWebEditor.document.body.contentEditable="true";
eWebEditor.document.oncontextmenu=new Function("return showContextMenu(eWebEditor.event);");
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;
break;
case "VIEW":
eWebEditor.document.designMode="off";
eWebEditor.document.open();
eWebEditor.document.write(config.StyleEditorHeader+sBody);
eWebEditor.document.body.contentEditable="false";
eWebEditor.document.close();
bEditMode=false;
break;
}
sCurrMode=NewMode;
// TODO disableChildren(eWebEditor_Toolbar);
if ((borderShown != "no")&&bEditMode) {
borderShown = "no";
showBorders()
}
}
//eWebEditor.focus();
}
// 使工具栏无效
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 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 "page": // 插入分页符
insertHTML("[NextPage]");
break;
case "br": // 插入换行符
insertHTML("<br/>");
break;
case "quote": // 引用片段样式
insertHTML('<table width=95% border="0" align="Center" cellpadding="6" cellspacing="0" style="border: 1px Dotted #CCCCCC; TABLE-LAYOUT: fixed"><tr><td bgcolor=#F3F3F3 style="WORD-WRAP: break-word"><font style="color: #990000;font-weight:bold">以下是引用片段:</font><br>'+HTMLEncode(sel.text)+'</td></tr></table>');
break;
case "big": // 字体变大
insertHTML("<big>" + sel.text + "</big>");
break;
case "small": // 字体变小
insertHTML("<small>" + sel.text + "</small>");
break;
default:
alert("错误参数调用!");
break;
}
sel=null;
}
// 显示或隐藏指导方针
var borderShown = "no";
function showBorders() {
if (!validateMode()) return;
var allForms = eWebEditor.document.body.getElementsByTagName("FORM");
var allInputs = eWebEditor.document.body.getElementsByTagName("INPUT");
var allTables = eWebEditor.document.body.getElementsByTagName("TABLE");
var allLinks = eWebEditor.document.body.getElementsByTagName("A");
// 表单
for (a=0; a < allForms.length; a++) {
if (borderShown == "no") {
allForms[a].runtimeStyle.border = "1px dotted #FF0000"
} else {
allForms[a].runtimeStyle.cssText = ""
}
}
// Input Hidden类
for (b=0; b < allInputs.length; b++) {
if (borderShown == "no") {
if (allInputs[b].type.toUpperCase() == "HIDDEN") {
allInputs[b].runtimeStyle.border = "1px dashed #000000"
allInputs[b].runtimeStyle.width = "15px"
allInputs[b].runtimeStyle.height = "15px"
allInputs[b].runtimeStyle.backgroundColor = "#FDADAD"
allInputs[b].runtimeStyle.color = "#FDADAD"
}
} else {
if (allInputs[b].type.toUpperCase() == "HIDDEN")
allInputs[b].runtimeStyle.cssText = ""
}
}
// 表格
for (i=0; i < allTables.length; i++) {
if (borderShown == "no") {
allTables[i].runtimeStyle.border = "1px dotted #BFBFBF"
} else {
allTables[i].runtimeStyle.cssText = ""
}
allRows = allTables[i].rows
for (y=0; y < allRows.length; y++) {
allCellsInRow = allRows[y].cells
for (x=0; x < allCellsInRow.length; x++) {
if (borderShown == "no") {
allCellsInRow[x].runtimeStyle.border = "1px dotted #BFBFBF"
} else {
allCellsInRow[x].runtimeStyle.cssText = ""
}
}
}
}
// 链接 A
for (a=0; a < allLinks.length; a++) {
if (borderShown == "no") {
if (allLinks[a].href.toUpperCase() == "") {
allLinks[a].runtimeStyle.border = "1px dashed #000000"
allLinks[a].runtimeStyle.width = "20px"
allLinks[a].runtimeStyle.height = "16px"
allLinks[a].runtimeStyle.backgroundColor = "#FFFFCC"
allLinks[a].runtimeStyle.color = "#FFFFCC"
}
} else {
allLinks[a].runtimeStyle.cssText = ""
}
}
if (borderShown == "no") {
borderShown = "yes"
} else {
borderShown = "no"
}
scrollUp()
}
// 返回页面最上部
function scrollUp() {
eWebEditor.scrollBy(0,0);
}
// 缩放操作
var nCurrZoomSize = 100;
var aZoomSize = new Array(10, 25, 50, 75, 100, 150, 200, 500);
function doZoom(size) {
eWebEditor.document.body.runtimeStyle.zoom = size + "%";
nCurrZoomSize = size;
}
// 拼写检查
function spellCheck(){
ShowDialog('dialog/spellcheck.htm', 300, 220, true)
}
function mouseOver(o){
$(o).attr("class","p-editor-mouse-over");
}
function mouseOut(o){
$(o).attr("class","p-editor-mouse-normal");
}
function mouseDown(o){
$(o).attr('class','p-editor-mouse-down');
}
function mouseUp(o){
$(o).attr('class','p-editor-mouse-normal');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -