⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weditor.js

📁 WAPmo手机网站管理平台是一款创建与管理维护WAP网站的的软件产品
💻 JS
字号:
window.WEditor = {};

WEditor.QueryString = function(name)
{
    var reg = new RegExp("[\\\?&]" + name + "=([^&]*)", "i");
    var arr = location.href.match(reg);
    return arr ? arr[1] : "";
};

WEditor.AddListener = function(obj, name, handler)
{
    if (obj.attachEvent)
    {
        obj.attachEvent("on" + name, handler);
    }
    else
    {
        obj.addEventListener(name, handler, false);
    }
};

WEditor.Initialize = function()
{
    WEditor.name = WEditor.QueryString("Name");
    WEditor.context = window.parent ? window.parent.document.getElementById(WEditor.name) : null;
    WEditor.value = WEditor.context ? WEditor.context.value : "";
    WEditor.UA = window.navigator.userAgent;
    WEditor.isMO = (/gecko/i.test(WEditor.UA));
    WEditor.isOA = (/opera/i.test(WEditor.UA));
    WEditor.isIE = (WEditor.isOA == false) && (/msie/i.test(WEditor.UA));
    WEditor.mode = (!WEditor.isOA);
    WEditor.ieVersion = WEditor.isIE ? parseInt(WEditor.UA.match(/msie\s(\d)/i)[1]) : 7;
    WEditor.toolbar = document.getElementById("WEditorToolbar");
    WEditor.area = document.getElementById("WEditorArea");
    WEditor.intel = window.setInterval(WEditor.Create, 100);
    WEditor.ubb = {};
    WEditor.ubb["Bold"] = "b";
    WEditor.ubb["Italic"] = "i";
    WEditor.ubb["Underline"] = "u";
    WEditor.ubb["JustifyLeft"] = "left";
    WEditor.ubb["JustifyCenter"] = "center";
    WEditor.ubb["JustifyRight"] = "right";
    WEditor.ubb["InsertImage"] = "img";
    WEditor.ubb["FontName"] = "name";
    WEditor.ubb["FontSize"] = "size";
    WEditor.ubb["FormatBlock"] = "format";
    WEditor.ubb["ForeColor"] = "fore";
    WEditor.ubb["BackColor"] = "back";
};

WEditor.setValue = function(val)
{
    WEditor.context.value = WEditor.value = val;
};

WEditor.Create = function()
{
    window.clearInterval(WEditor.intel);
    if (WEditor.isMO || WEditor.isIE) WEditor.CreateToolbar();
    //WEditor.CreateToolbar();
    WEditor.CreateWorkspace();
};

WEditor.CreateToolbar = function()
{
    WEditor.toolbar.style.display = "";
    WEditor.ToolbarStart();
    WEditor.ToolbarEnd();
};

WEditor.ToolbarStart = function()
{
    WEditor.table = document.createElement("TABLE");
    WEditor.table.border = "0";
    WEditor.table.cellPadding = "0";
    WEditor.table.cellSpacing = "0";
    WEditor.table.unselectable = "on";
    WEditor.rows = 0;
    WEditor.cells = 0;
    WEditor.row = WEditor.table.insertRow(WEditor.rows++);
    WEditor.row.unselectable = "on";
    WEditor.tools = [];
    WEditor.tools.push(["Button", "Source", "源代码", "源代码", null]);
    WEditor.tools.push(["Button", "Bold", "加粗", null, null]);
    WEditor.tools.push(["Button", "Italic", "倾斜", null, null]);
    WEditor.tools.push(["Button", "Underline", "下划线", null, null]);
    WEditor.tools.push(["Button", "JustifyLeft", "向左对齐", null, null]);
    WEditor.tools.push(["Button", "JustifyCenter", "居中对齐", null, null]);
    WEditor.tools.push(["Button", "JustifyRight", "向右对齐", null, null]);
    WEditor.tools.push(["Button", "InsertImage", "插入图片", null, null]);
    WEditor.tools.push(["Combo", "FontName", "字体", "宋体|黑体|楷体|仿宋|隶书|幼圆|新宋体|细明体|Arial|Arial Black|Arial Narrow|Bradley Hand ITC|Brush Script MT|Century Gothic|Comic Sans MS|Courier|Courier New|MS Sans Serif|Script|System|Times New Roman|Viner Hand ITC|Verdana|Wide Latin|Wingdings", null]);
    WEditor.tools.push(["Combo", "FontSize", "大小", "非常小=1|很小=2|小=3|中等=4|大=5|很大=6|非常大=7", null]);
    WEditor.tools.push(["Combo", "FormatBlock", "格式化", "普通=<p>|段落=" + (WEditor.isMO ? "div" : "<div>") + "|已编排的格式=<pre>|地址=<address>|标题1=<H1>|标题2=<H2>|标题3=<H3>|标题4=<H4>|标题5=<H5>|标题6=<H6>", null]);
    WEditor.tools.push(["Button", "ForeColor", "文本颜色", null, null]);
    WEditor.tools.push(["Button", "BackColor", "背景颜色", null, null]);
    WEditor.tools.push(["Button", "RemoveFormat", "清除格式", null, null]);
    for (var i = 0; i < WEditor.tools.length; i++)
    {
        var arr = WEditor.tools[i];
        eval("arr[4] = WEditor." + arr[0] + "(arr[1], arr[2], arr[3])");
        WEditor.cell = WEditor.row.insertCell(WEditor.cells++);
        WEditor.cell.unselectable = "on";
        WEditor.cell.appendChild(arr[4]);
    }
};

WEditor.ToolbarEnd = function()
{
    while (WEditor.toolbar.childNodes.length > 0)
    {
        WEditor.toolbar.removeChild(WEditor.toolbar.childNodes[0]);
    }
    WEditor.toolbar.appendChild(WEditor.table);
};

WEditor.Button = function(name, label, tooltip)
{
    var div = document.createElement("DIV");
    div.className = "WEditor_Button_Enable";
    div.title = label;
    div.onselectable = "on";
    if (tooltip)
    {
        var table = document.createElement("TABLE");
        var row = table.insertRow(0);
        var cell = row.insertCell(0);
        table.border = "0";
        table.cellPadding = "0";
        table.cellSpacing = "0";
        table.unselectable = "on";
        row.unselectable = "on";
        cell.unselectable = "on";
        cell.appendChild(WEditor.Image("images/we_" + name + ".gif"));
        cell = row.insertCell(1);
        cell.unselectable = "on";
        cell.className = "WEditor_Button_Text";
        cell.noWrap = true;
        cell.appendChild(document.createTextNode(tooltip));
        cell = row.insertCell(2);
        cell.unselectable = "on";
        cell.appendChild(WEditor.Image("images/we.spacer.gif"));
        div.appendChild(table);
    }
    else
    {
        div.appendChild(WEditor.Image("images/we_" + name + ".gif"));
    }
    WEditor.AddListener(div, "mouseover", function()
    {
        div.className = "WEditor_Button_Over";
    });
    WEditor.AddListener(div, "mouseout", function()
    {
        div.className = "WEditor_Button_Enable";
    });
    WEditor.AddListener(div, "click", function()
    {
        if (name == "Source")
        {
            WEditor.view();
        }
        else if (WEditor.mode)
        {
            WEditor.win.focus();
            var value = null;
            if (name == "InsertImage")
            {
                value = String(prompt("请输入图片URL地址", "http://"));
                if (value == "" || value.length < 8)
                {
                    return;
                }
            }
            WEditor.doc.execCommand(name, false, value);
            WEditor.setValue(WEditor.body.innerHTML);
        }
    });
    return div;
};

WEditor.Image = function(src)
{
    var img = document.createElement("IMG");
    img.border = "0";
    img.src = src;
    img.align = "absMiddle";
    img.unselectable = "on";
    var div = document.createElement("DIV");
    div.className = "WEditor_Button_Image";
    div.unselectable = "on";
    div.appendChild(img);
    return div;
};

WEditor.Combo = function(name, text, list)
{
    var div = document.createElement("DIV");
    div.className = "WEditor_Button_Enable";
    div.onselectable = "on";
    var obj = document.createElement("SELECT");
    var opt = obj.options;
    var arr = list.split(/\|/g);
    opt[0] = new Option(text, "");
    for (var i = 0; i < arr.length; i++)
    {
        var arr1 = arr[i].split(/=/);
        if (arr1.length == 2)
        {
            opt[i + 1] = new Option(arr1[0], arr1[1]);
        }
        else
        {
            opt[i + 1] = new Option(arr[i], arr[i]);
        }
    }
    WEditor.AddListener(obj, "change", function()
    {
        WEditor.win.focus();
        if (obj.selectedIndex > 0)
        {
            WEditor.doc.execCommand(name, false, obj.options[obj.selectedIndex].value);
        }
        else
        {
            WEditor.doc.execCommand("RemoveFormat");
        }
        WEditor.setValue(WEditor.body.innerHTML);
    });
    var table = document.createElement("TABLE");
    var row = table.insertRow(0);
    var cell = row.insertCell(0);
    table.border = "0";
    table.cellPadding = "0";
    table.cellSpacing = "0";
    table.unselectable = "on";
    row.unselectable = "on";
    cell.unselectable = "on";
    cell.appendChild(obj);
    cell = row.insertCell(1);
    cell.unselectable = "on";
    cell.appendChild(WEditor.Image("images/we.spacer.gif"));
    div.appendChild(table);
    return div;
};

WEditor.view = function()
{
    if (WEditor.mode)
    {
        WEditor.setValue(WEditor.LCase(WEditor.body.innerHTML));
        WEditor.InitBox();
    }
    else
    {
        WEditor.setValue(WEditor.box.value);
        WEditor.InitFrame();
    }
    WEditor.mode = (WEditor.mode == false);
};

WEditor.LCase = function(str)
{
    var reg = /<(\w+)([^>]*)>|<\/(\w+)>/g;
    var arr;
    var pos = 0;
    var ret = "";
    while ((arr = reg.exec(str)) != null)
    {
        ret += str.substring(pos, arr.index);
        pos = arr.index + arr[0].length;
        if (arr[1] != "" && arr[1] != undefined)
        {
            ret += "<" + arr[1].toLowerCase() + arr[2] + ">";
        }
        else
        {
            ret += "</" + arr[3].toLowerCase() + ">";
        }
    }
    ret += str.substring(pos);
    return ret;
};

WEditor.CreateWorkspace = function()
{
    WEditor.frame = document.createElement("IFRAME");
    WEditor.frame.src = "javascript:void(0)";
    WEditor.frame.frameBorder = 0;
    WEditor.frame.width = "100%";
    WEditor.frame.height = "100%";
    WEditor.box = document.createElement("TEXTAREA");
    WEditor.box.className = "WEditor_Box";
    WEditor.AddListener(WEditor.box, "keyup", function()
    {
        WEditor.setValue(WEditor.box.value);
    });
    if (WEditor.isOA)
    {
        WEditor.InitBox();
    }
    else
    {
        WEditor.InitFrame();
    }
};

WEditor.InitFrame = function()
{
    while (WEditor.area.childNodes.length > 0) WEditor.area.removeChild(WEditor.area.childNodes[0]);
    WEditor.area.appendChild(WEditor.frame);
    WEditor.win = WEditor.frame.contentWindow;
    WEditor.doc = WEditor.win.document;
    WEditor.doc.designMode = "on";
    WEditor.doc.contentEditable = true;
    WEditor.doc.open();
    WEditor.doc.write("<html><head><title>WEditor</title></head><body></body></html>");
    WEditor.doc.write(WEditor.value);
    WEditor.doc.close();
    WEditor.body = WEditor.doc.body;
    WEditor.body.style.fontSize = "10pt";
    WEditor.body.style.fontFamily = "Courier New";
    WEditor.body.style.padding = "2px";
    WEditor.body.style.margin = "0px";
    WEditor.AddListener(WEditor.doc, "keyup", function()
    {
        WEditor.setValue(WEditor.body.innerHTML);
    });
    WEditor.intel = window.setInterval(function()
    {
        window.clearInterval(WEditor.intel);
        WEditor.win.focus();
    }, 100);
};

WEditor.InitBox = function()
{
    while (WEditor.area.childNodes.length > 0) WEditor.area.removeChild(WEditor.area.childNodes[0]);
    WEditor.box.value = WEditor.value;
    WEditor.area.appendChild(WEditor.box);
    WEditor.intel = window.setInterval(function()
    {
        window.clearInterval(WEditor.intel);
        WEditor.box.focus();
    }, 100);
};

WEditor.InsertText = function(str)
{
    if (document.selection)
    {
        if (WEditor.mode)
        {
            WEditor.win.focus();
        }
        else
        {
            WEditor.box.focus();
        }
        var ptr = document.selection.createRange();
        ptr.text = str;
        if (WEditor.isOA)
        {
            WEditor.setValue(WEditor.box.value);
        }
        else
        {
            WEditor.setValue(WEditor.body.innerHTML);
        }
    }
    else
    {
        if (WEditor.mode)
        {
            WEditor.win.focus();
            WEditor.body.innerHTML = WEditor.body.innerHTML.substring(0, WEditor.body.selectionStart) + str + WEditor.body.innerHTML.substring(WEditor.body.selectionEnd);
        }
        else
        {
            WEditor.box.focus();
            WEditor.box.value = WEditor.box.value.substring(0, WEditor.box.selectionStart) + str + WEditor.box.value.substring(WEditor.box.selectionEnd);
        }
        WEditor.setValue(WEditor.body.innerHTML);
    }
};

WEditor.AddListener(window, "load", WEditor.Initialize);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -