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

📄 coreeditor.js

📁 asp的bbs程序
💻 JS
📖 第 1 页 / 共 3 页
字号:
            if (this.EditType == Config.UBB) {
                content = this.GetXHtmlFromUBB(true);
            }
            else {
                if (!this.JudgeNotAllowHtmlTag(this.Document.body.innerHTML)) return false;
                content = UBB.imageHTMLUrlToSign(UBB.multimediaHtmlToUbb(this.GetXhtmlFromHtml(false)));
            }
            this.TextRange = null;
            this.CreateTextArea(content, type);
            this.SelectOneTextArea();
        }
        // to UBB
        else if (type == Config.UBB && this.EditType != Config.UBB) {
            if (!this.AllowUBB) return false;
            if (this.EditType == Config.XHTML) {
                content = this.GetUBBFromXHtml(true);
            }
            else {
                if (!this.JudgeNotAllowHtmlTag(this.Document.body.innerHTML)) return false;
                content = UBB.imageUBBUrlToSign(this.GetUBBFromXHtml(false));
            }
            this.TextRange = null;
            this.CreateTextArea(content, type);
            this.TextAreaBindEvent();
        }

        // 设置当前状态
        this.EditType = type;
        Toolbar.responseAll(type == Config.XHTML);
        
        // 文本域更新调用
        this.EditorObj.AfterChange({DMode:type,AllowHTML:this.AllowHTML,AllowUBB:this.AllowUBB});

        // 设置文本选择域为空
        //this.TextRange = null;

        this.Resize();
        return true;
    },
    ChangeStateAndButton : function (type) {
        type = type.toLowerCase();
        this.ChangeState(type);
        switch (type) {
            case Config.WYSIWYG:
                Toolbar.wysiwygClick();
                break;
            case Config.XHTML:
                this.AllowHTML && Toolbar.xhtmlClick();
                break;
            case Config.UBB:
                this.AllowUBB && Toolbar.ubbClick();
                break;
        }
    },

    JudgeNotAllowHtmlTag : function (content) {
        if (Config.NotAllowHtmlTag().test(content)) {
            return confirm('内容包含不允许的标签。\n是否继续转换?');
        }
        return true;
    },

    /*
       content edit
    */
    // 获取文本选取对象
    // 选取范围
    GetRange : function () {
        // 设置选择区对象
        if (Browser.IsIE) {
            this.SelectRange = this.Document.selection;
            this.TextRange = this.SelectRange.createRange();
        }
        else {
            this.SelectRange = this.Window.getSelection();
        }
        return this.SelectRange;
    },

    // 文本选择区对象
    GetTextRange : function () {
        if (Browser.IsIE) {
            this.TextRange = document.selection.createRange();
        }
        else {
            this.TextRangeStart = this.TextArea.selectionStart;
            this.TextRangeEnd = this.TextArea.selectionEnd;
        }
        return this.TextRange;
    },

    // [ie] 选择内容
    SelectTextRange : function (isCollapse) {
        if (!Browser.IsIE) return;
        if (this.EditType != Config.WYSIWYG) return;
        try {
            if (this.TextRange) {
                this.TextRange.collapse(false);
                isCollapse && this.TextRange.select();
            }
            else {
                this.Focus();
            }
        }
        catch (e) {}
    },

    // 对编辑器内容应用样式
    SetStyle : function (type, value) {
        var _status;
        if (! Browser.IsIELike && type == 'HiliteColor') {
            this.SetStyle('styleWithCSS', true);
        }
        try {
            this.Focus();
            _status = this.Document.execCommand(type, false, value);
        }
        catch (e) {}
        if (! Browser.IsIELike && type == 'HiliteColor') {
            this.SetStyle('styleWithCSS', false);
        }

        this.SelectTextRange();
        return _status;
    },

    // ubb方式替换内容
    // contents : [content, content1, content2, ..., contentN]
    //   eg:
    //       [b]   => ['[b]\x01[/b]', 'content1']
    //       [url] => ['[url=\x01]\x02[/url]', 'content1', 'content2']
    // 'content1'是文本域选择的文本,所以无需通过参数传入,所以参数参数传递应为:
    //   eg:
    //       [b]   => ['[b]\x01[/b]']
    //       [url] => ['[url=\x01]\x02[/url]', 'content2']
    // 'content1'在SetUbbStyle方法内获得
    SetUBBStyle : function () {
        var contents = arguments;
        var l = contents.length;
        var t = this.TextArea;

        var content;
        var _content = content = Browser.IsIE ? (this.TextRange ? this.TextRange.text : '')
                                   : t.value.substring(this.TextRangeStart, this.TextRangeEnd)
        var index = contents[0].indexOf('\x01');
        content = contents[0].replace(/\x01/g, content || contents[1] || '');
        for (var i = 1; i < l; i++) {
            content = content.replace(new RegExp('\\x0' + (i + 1), 'g'), contents[i]);
        }
        if (_content != '') index = 0;

        if (Browser.IsIE) {
            if (this.TextRange) {
                this.TextRange.text = content;
                if (index > 0) this.TextRange.move('character', index - content.length);
                this.TextRange.select();
            }
            else {
                t.value += content;
            }
        }
        else {
            t.value = t.value.substring(0, this.TextRangeStart) + content + t.value.substring(this.TextRangeEnd);

            var start = index > 0 ? this.TextRangeStart + index : this.TextRangeStart + content.length;
            t.setSelectionRange(start, start);
            this.TextRangeStart = this.TextRangeEnd = index > 0 ? this.TextRangeStart + index : start;
        }

        t.focus();
        this.GetTextRange();
    },

    // 插入内容
    InsertContent : function (content) {
        if (this.EditType != Config.WYSIWYG) return;
        if (Browser.IsIE)
            this.InsertContentIE(content);
        else {
            var c = this.Document.createElement('span');
            this.Document.body.appendChild(c);
            c.innerHTML = content;
            try {
                this.InsertContentGecko(c);
            }
            catch (e) {
                this.Document.body.appendChild(c);
                this.SelectOneEidtor();
            }
            while (c && c.firstChild) {
                var node = c.removeChild(c.firstChild);
                c.parentNode.insertBefore(node, c);
            }
            c.parentNode.removeChild(c);
        }
        
        // 提示一次,是否启用要切换至全屏
        if (this.FullScreenPrompt) {
            this.FullScreenPrompt = false;
            var sw = Global.GetClientWidth(this.Document);
            var nodes = this.Document.getElementsByTagName('*');
            for (var i = 0, len = nodes.length; i < len; i++) {
                if (nodes[i].offsetWidth > sw) {
                    // 提示
                    this.EditorObj.SwitchToFullScreen();
                    /*
                    if (this.EditorObj.SwitchToFullScreen()) {
                        Toolbar.SwitchToFullScreen();
                    }
                    */
                    break;
                }
            }
        }
    },

    // [ie] insert content 
    InsertContentIE : function (content) {
        try {
            if (this.SelectRange.type == 'None') {
                if (this.TextRange.parentElement().ownerDocument.body.className != 'maxcode-userenters')
                    throw new Error('parent document');
            }
            if ((this.SelectRange.type == 'Control' || this.TextRange.text != '') && this.SelectRange.clear)
                this.SelectRange.clear();
            this.TextRange.pasteHTML(content);
            this.TextRange.select();
            this.TextRange.collapse(false);
        }
        catch (e) {
            var c = this.Document.createElement('span');
            this.Document.body.appendChild(c);
            c.innerHTML = content;
            while (c.firstChild) {
                var node = c.removeChild(c.firstChild);
                c.parentNode.insertBefore(node, c);
            }
            c.parentNode.removeChild(c);
            this.SelectOneEidtor();
        }
        this.GetRange();
    },

    // [gecko] insert content 
    InsertContentGecko : function (insertNode) {
        // 获得当前选取对象
        var sel = this.SelectRange;

        // 获取第一个选取对象,仅仅只有一个
        var range = sel.getRangeAt(0);

        // 取消所有选取
        sel.removeAllRanges();

        // 从文档中删除选区的内容
        range.deleteContents();

        var container = range.startContainer;
        var pos = range.startOffset;

        // 创建新的Range
        range = this.Document.createRange();

        if (container.nodeType == 3 && insertNode.nodeType == 3) {
            // 如果插入点是文本节点,则只进行插入事件
            container.insertData(pos, insertNode.nodeValue);

            // 移动光标到插入的内容后面
            range.setEnd(container, pos + insertNode.length);
            range.setStart(container, pos + insertNode.length);
        }
        else {
            var afterNode;
            if (container.nodeType == 3) {
                // 当插入一个非文本节点到一个文本节点中,需要创建两个文本节点,然后在其间放入我们的节点

                var textNode = container;
                container = textNode.parentNode;
                var text = textNode.nodeValue;

                // 截取焦点之前的文本
                var textBefore = text.substr(0,pos);
                // 截取焦点之后的文本
                var textAfter = text.substr(pos);

                var beforeNode = this.Document.createTextNode(textBefore);
                afterNode = this.Document.createTextNode(textAfter);

                // insert the 3 new nodes before the old one
                container.insertBefore(afterNode, textNode);
                container.insertBefore(insertNode, afterNode);
                container.insertBefore(beforeNode, insertNode);

                // remove the old node
                container.removeChild(textNode);
            }
            else {
                // 否则仅仅插入节点
                afterNode = container.childNodes[pos];
                container.insertBefore(insertNode, afterNode);
            }

            try {range.setEnd(afterNode, 0);}catch (e) {}
            try {range.setStart(afterNode, 0);}catch (e) {}
        }

        try {
            sel.addRange(range);
            range.collapse(false);
            this.Focus();
        }
        catch (e) {}
    },

    // 菜单
    Menu : function (targetWindow, width, height, content, callback) {
        var targetDocument = targetWindow.document;
        var menu = document.createElement('div');
        menu.style.position = 'absolute';
        width && (menu.style.width = width + 'px');
        height && (menu.style.height = height + 'px');
        CSS.AddClass(menu, 'menu');

        if (typeof content == 'string') {
            var link = targetDocument.createElement('a');
            link.href = '#';
            link.onclick = function () {cEditor.setStyle('Bold')};
            link.appendChild(targetDocument.createTextNode(content));
            content = link;
        }
        content && menu.appendChild(content);
        targetDocument.body.appendChild(menu);

        /// 点击按钮关闭
        // 当前窗口点击
        var tempMouseDown = targetDocument.onmousedown || new Function();
        targetDocument.onmousedown = function (e) {
            //tempMouseDown();
            e = (e || targetWindow.event);
            var p = e.srcElement || e.target;
            if (p != menu) {
                while (p) {
                    if (p == menu) {
                        return;
                    }
                    p = p.offsetParent;
                }
            }
            menu.style.display = 'none';
            callback && callback();
        };
        // 编辑器点击
        try {
            var tempMouseDownEditor = this.Document.onmousedown || new Function();
            this.Document.onmousedown = function () {
                tempMouseDownEditor();
                menu.style.display = 'none';
                callback && callback();
                mouse_down();
            };
        }
        catch (e) {}
        try {
            this.Document.addEventListener('mousedown', function () {
                menu.style.display = 'none';
                callback && callback();
            }, false);
        }
        catch (e) {}
        // 父窗口点击
        var _parent = targetWindow.parent;
        function mouse_down () {
            menu.style.display = 'none';
            callback && callback();
            _parent.Events.RemoveEvent(_parent.document, 'mousedown', mouse_down);
        }
        _parent.Events.AttachEvent(_parent.document, 'mousedown', mouse_down);

        return menu;
    }
};

(function () {
    // 获取编辑文本框
    var Document = window.parent.document;
    cEditor.TextField = Document.getElementsByName(cEditor.Name)[0];
    cEditor._TextField = Document.getElementsByName('_' + cEditor.Name)[0];
    cEditor.EditorType = Document.getElementsByName(cEditor.Name + 'Type')[0];

    cEditor.EditorObj.EditorWindow = window;
    cEditor.EditorObj.FrameEditor = cEditor;

    // 聚焦
    Events.AttachEvent(window, 'focus', function () {
        cEditor.Focus();
    });
})();

⌨️ 快捷键说明

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