欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一款垃圾中的*HTML编辑器(兼容IE OR FF)

程序员文章站 2023-12-22 22:39:58
这东东实现了一些常用的功能,今天刚完成,大家多给点建议! 在ff下不完善,有些功能暂时还没实现! 这也是本人写的第一个编辑器(处女作哦),做得不好,千万别丢鸡蛋过来......
这东东实现了一些常用的功能,今天刚完成,大家多给点建议!
在ff下不完善,有些功能暂时还没实现!
这也是本人写的第一个编辑器(处女作哦),做得不好,千万别丢鸡蛋过来...
先闪人!
复制代码 代码如下:

/*******************************************
  一款垃圾中的*freeeditor(2006-08-22)
  this javascript was writen by cxp.
*******************************************/
var colortype;
var currentmode="design";    //当前模式
var ie=document.all?true:false;
document.onclick=function(){    
}
function format(str,arg){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var obj=window.frames["design"];
    if(!ie){
        var salert="";
        switch(str){
            case "cut":
                salert = "你的浏览器安全设置不允许编辑器自动执行剪切操作,请使用键盘快捷键(ctrl+x)来完成";
                break;
            case "copy":
                salert = "你的浏览器安全设置不允许编辑器自动执行拷贝操作,请使用键盘快捷键(ctrl+c)来完成";
                break;
            case "paste":
                salert = "你的浏览器安全设置不允许编辑器自动执行粘贴操作,请使用键盘快捷键(ctrl+v)来完成";
                break;
        }
        if(salert!=""){
            alert(salert);
            return;
        }
    }
    obj.focus();
    if(arg){
        return obj.document.execcommand(str,false,arg);
    }
    else{
        if(ie){
            obj.document.execcommand(str);
        }
        else{
            obj.document.execcommand(str,false,false);
        }
    }
    obj.focus();
}

//显示颜色菜单
function showcolormenu(obj,e){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var target=document.all?e.srcelement:e.target;
    var colormode=target.getattribute("title");
    switch(colormode){
        case "背景颜色":
            colortype="bodybackcolor";
            break;
        case "字体背景颜色":
            colortype="backcolor";
            break;
        case "字体颜色":
            colortype="forecolor";
            break;        
        default:
            return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        var db=window.frames["design"].document;
        var height=125;
        if(colortype=="forecolor"){
            height=145;
        }
        var color=showmodaldialog("dialog/color.html",window,"dialogwidth:263px;dialogheight:"+height+"px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";px");
        if(color!=null){            
            if(colortype=="bodybackcolor"){
                db.body.style.backgroundcolor=color;
                return;
            }
            else if(colortype=="forecolor"){
                    var arr=color.split("|");
                    if(arr.length==2){
                        format(colortype,arr[0]);
                        //设置发光滤镜,ieonly
                        if(db.selection.type.tolowercase()!="none"){
                            var selecttext=db.selection.createrange().htmltext;
                            selecttext="<span style=\"height:18px;filter:glow(color="+arr[0]+",strength=2);\">"+selecttext+"</span>";
                            inserthtml(selecttext);
                            return;
                        }
                    }
                }
            format(colortype,color);            
        }
    }
    else{
        if(colortype=="backcolor"){
            alert('待完善!');
            return;
        }    
        ffopenwidnow("dialog/color.html",263,112,top+25,left);
    }
}

//显示插入图片窗口
function showimagemenu(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        var path=showmodaldialog("dialog/image.html","","dialogwidth:390px;dialogheight:120px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";px");
        if(path==null){
            return;
        }
        format("insertimage",path);
    }
    else{
        ffopenwidnow("dialog/image.html",385,100,top+25,left);
    }
}

//显示插入flash窗口
function showflashmenu(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj);
    if(document.all){
        var path=showmodaldialog("dialog/flash.html","","dialogwidth:402px;dialogheight:139px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+(top+138)+";px");
        if(path==null){
            return;
        }
        var arr=path.split("|");
        var str="<object codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' width='"+arr[1]+"' height='"+arr[2]+"'><param name=movie value='"+arr[0]+"'><param name=quality value=high><embed src='"+arr[0]+"' quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?p1_prod_version=shockwaveflash' type='application/x-shockwave-flash' width='"+arr[1]+"' height='"+arr[2]+"'></embed></object>";
        inserthtml(str);
    }
    else{
        alert('待完善');
        return;
    }
}

//显示插入face窗口
function showfacemenu(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/face.html",window,"dialogwidth:280px;dialogheight:215px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";px");
    }
    else{
        ffopenwidnow("dialog/face.html",280,180,top+25,left);
    }
}

//显示fieldset窗口
function showfieldsetmenu(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/fieldset.html",window,"dialogwidth:330px;dialogheight:120px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";px");
    }
    else{
        ffopenwidnow("dialog/fieldset.html",330,100,top+25,left);
    }
}

function getwidth(obj){
    var parentobj=obj;
    var left=obj.offsetleft;
    while(parentobj=parentobj.offsetparent){
        left+=parentobj.offsetleft;
    }
    return left;
}

function getheight(obj){
    var parentobj=obj;
    var top=obj.offsettop;
    while(parentobj=parentobj.offsetparent){
        top+=parentobj.offsettop;
    }
    return top;
}

function showmediamenu(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj);
    if(document.all){
        showmodaldialog("dialog/media.html",window,"dialogwidth:330px;dialogheight:119px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+(top+138)+";");
    }
    else{
        alert('待完善');
    }
}

//显示插入文件窗口
function showfilemenu(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/file.html",window,"dialogwidth:390px;dialogheight:115px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/file.html",380,100,top+25,left);
    }
}

function setcolor(color){    
    if(color==""){
        return;
    }
    if(colortype=="bodybackcolor"){
        window.frames["design"].document.body.style.backgroundcolor=color;
        return;
    }
    format(colortype,color);
}

//插入字幕
function showmarqueemenu(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/marquee.html",window,"dialogwidth:439px;dialogheight:260px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/marquee.html",435,220,top+25,left);
    }
}

//插入特殊字符
function showsymbolmenu(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/symbol.html",window,"dialogwidth:385px;dialogheight:235px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/symbol.html",370,190,top+25,left);
    }
}

//插入文件
function insertfile(txt){
    var arr=txt.split(".");
    var filename=getfilename(arr[arr.length-1]);
    var str="<img src=\"img\/file\/"+filename+"\" border=0><a href=\""+txt+"\" target=\"_blank\">";
    arr=txt.split("/");
    filename=arr[arr.length-1];
    str+=filename+"</a>";
    var db=window.frames["design"].document;
    inserthtml(str);
}

//插入当前时间
function inserttime(b){
    var d=new date();
    var year,month,day,hour,minute,second;
    year=d.getfullyear();
    month=d.getmonth();
    day=d.getdate();
    hour=d.gethours();
    minute=d.getminutes();
    second=d.getseconds();
    var str;
    if(b){
        str=year+"-"+month+"-"+day;
    }
    else{
        str=hour+":"+minute+":"+second;
    }
    inserthtml(str);
}

//插入引用
function insertquote(){
    var str="<table style=\"border-right: #cccccc 1px dotted; table-layout: fixed; border-top: #cccccc 1px dotted; border-left: #cccccc 1px dotted; border-bottom: #cccccc 1px dotted\" cellspacing=0 cellpadding=6 width=\"95%\" align=center border=0><tbody><tr><td style=\"word-wrap: break-word\" bgcolor=\"#f3f3f3\" valign=\"top\"><font style=\"font-weight: bold; color: green;font-size:13px;\">引用:</font><br></td></tr></tbody></table>";
    inserthtml(str);
}

//插入单选框
function insertradio(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/radio.html",window,"dialogwidth:275px;dialogheight:156px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/radio.html",270,125,top+25,left);
    }
}

//插入复选框
function insertcheckbox(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/checkbox.html",window,"dialogwidth:275px;dialogheight:156px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/checkbox.html",270,125,top+25,left);
    }
}

//插入复选框
function insertinputtext(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/text.html",window,"dialogwidth:275px;dialogheight:156px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/text.html",270,125,top+25,left);
    }
}

//插入多行文本框
function inserttextarea(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/textarea.html",window,"dialogwidth:305px;dialogheight:250px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/textarea.html",300,250,top+25,left,true);
    }
}

//插入表格
function inserttable(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/table.html",window,"dialogwidth:265px;dialogheight:239px;help:no;status:no;scroll:no;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/table.html",256,210,top+25,left,true);
    }
}

//插入下拉列表
function insertselect(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/select.html",window,"dialogwidth:380px;dialogheight:250px;help:no;status:no;scroll:auto;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/select.html",380,250,top+25,left);
    }
}

//插入按钮
function insertbutton(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/button.html",window,"dialogwidth:239px;dialogheight:170px;help:no;status:no;scroll:auto;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/button.html",236,139,top+25,left);
    }
}

//插入表单
function insertform(obj){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    var left,top;
    left=getwidth(obj);
    top=getheight(obj)+138;
    if(document.all){
        showmodaldialog("dialog/form.html",window,"dialogwidth:239px;dialogheight:170px;help:no;status:no;scroll:auto;dialogleft:"+left+";dialogtop:"+top+";");
    }
    else{
        ffopenwidnow("dialog/form.html",236,139,top+25,left);
    }
}

//设置模式
function setmode(modetype,btnobj){
    if(currentmode==modetype){
        return;
    }
    btnobj.style.border="1px solid #000000";
    var cobj;
    if(currentmode=="preview"){
        cobj=document.getelementbyid("preview");
    }
    else if(currentmode=="design"){
        cobj=document.getelementbyid("design");
    }
    else{
        cobj=document.getelementbyid("textcode");
    }
    document.getelementbyid("bnt"+currentmode).style.border="1px solid #999999";
    switch(modetype){
        case "code":    //代码    
            var obj=document.getelementbyid("textcode");
            var db=window.frames["design"].document.body;
            var htmlcode=db.innerhtml;
            //防止ff在没有任何代码前产生的换行
            if(htmlcode=="<br>\n" || htmlcode=="<br>"){
                htmlcode="";
            }
            obj.value=htmlcode;
            cobj.style.display="none";
            obj.style.display="block";
            break;
        case "design":    //设计    
            cobj.style.display="none";
            document.getelementbyid("design").style.display="block";
            break;
        case "preview":
            var obj=window.frames["preview"].document.body;
            var db=window.frames["design"].document.body;
            obj.style.backgroundcolor=db.style.backgroundcolor;
            if(currentmode=="code"){
                obj.innerhtml=cobj.value;
            }
            else{
                var str=db.innerhtml;
                obj.innerhtml=str;
            }
            cobj.style.display="none";
            document.getelementbyid("preview").style.display="block";
            break;
    }
    currentmode=modetype;    //设置当前模式
}

//创建链接
function createlink(){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    if(document.all){
        format("createlink");
    }
    else{
        var str=window.prompt("enter link location (example:http://www.baidu.com/ or ftp://127.0.0.1/)","http://");
        if(str==null || str=="http://" || str=="")    return;
        format("createlink", str);
    }
}

//新建
function newpage(){
    if(currentmode!="design"){    //非设计模式
        return;
    }
    window.frames["design"].document.body.innerhtml="";
}

//设置字体
function setfacefamily(obj){
    var val=obj.options[obj.selectedindex].value;
    if(val=="")    return;
    format("fontname",val);
}

//设置字号
function setfacesize(obj){
    var val=obj.options[obj.selectedindex].text;
    if(val=="字号")    return;
    format("fontsize",val);
}
function getfilename(str){
    str=str.tolowercase();
    var name;
    switch(str){
        case "avi":
            name="avi.gif";
            break;
        case "bmp":
            name="bmp.gif";
            break;
        case "chm":
            name="chm.gif";
            break;
        case "doc":
            name="doc.gif";
            break;
        case "exe":
            name="exe.gif";
            break;
        case "gif":
            name="gif.gif";
            break;
        case "html":
            name="html.gif";
            break;
        case "htm":
            name="html.gif";
            break;
        case "jpg":
            name="jpg.gif";
            break;
        case "mdb":
            name="mdb.gif";
            break;
        case "mid":
            name="mid.gif";
            break;
        case "mp3":
            name="mp3.gif";
            break;
        case "pdf":
            name="pdf.gif";
            break;
        case "ppt":
            name="ppt.gif";
            break;
        case "rar":
            name="rar.gif";
            break;
        case "rm":
            name="rm.gif";
            break;
        case "rmvb":
            name="rm.gif";
            break;
        case "swf":
            name="swf.gif";
            break;
        case "txt":
            name="txt.gif";
            break;
        case "xls":
            name="xls.gif";
            break;
        case "zip":
            name="zip.gif";
            break;
        default:
            name="unknow.gif";
            break;
    }
    return name;
}

function setfocus(){
    window.frames["design"].focus();
}

//插入html代码
function inserthtml(str){
    if(ie){
        setfocus();
        window.frames["design"].document.selection.createrange().pastehtml(str);        
    }
    else{
        var newstr=window.frames["design"].document.body.innerhtml;
        if(newstr.tolowercase()=="<br>" || newstr.tolowercase()=="<br>\n"){
            window.frames["design"].document.body.innerhtml=str;
        }
        else{
            window.frames["design"].document.body.innerhtml+=str;
        }
    }
    setfocus();
}

//ff打开窗口
function ffopenwidnow(url,w,h,top,left){    
    window.open(url,"","location=no,menubar=no,toolbar=no,dependent=yes,dialog=yes,minimizable=no,modal=yes,alwaysraised=yes,resizable=no,width="+w+",height="+h+",screenx="+left+",screeny="+top);         
}

本地下载

上一篇:

下一篇: