详情页

帝国cms后台正文编辑器中的一键排版功能导致加粗字体移位的解决方法

时间:2024年03月28日

编辑:佚名

帝国cms后台编辑器使用ckeditor,其中自带的一键排版功能可以很方便的把文章格式化,
但如果遇到加粗字体,比如b或strong标签,这部分文字会自动移位,如下面的效果:

帝国cms后台正文编辑器中的一键排版功能导致加粗字体移位的解决方法图1
执行一键排版后,加粗部分变成了独立行:

帝国cms后台正文编辑器中的一键排版功能导致加粗字体移位的解决方法图2
这样同一行中有加粗字时会造成困扰,可以通过下面的方法解决。
打开/e/admin/ecmseditor/infoeditor/plugins/autoformat/plugin.js,注释掉加粗字的处理部分,修改后完整的代码如下:
CKEDITOR.plugins.add('autoformat',
{
    init: function(editor)
    {
        //plugin code goes here
        var pluginName = 'autoformat';
        editor.addCommand(pluginName, new CKEDITOR.autoformatCommand());
        editor.ui.addButton('autoformat',
        {
            label: '一键排版',
            command: 'autoformat',
            icon: CKEDITOR.plugins.getPath('autoformat') + 'images/autoformat.gif'
        });
    }
});
CKEDITOR.autoformatCommand = function(){};
CKEDITOR.autoformatCommand.prototype =
{
    async:true,
    exec : function( editor )
    {
        FormatText(editor);
    }
};
if(window.HTMLElement) {
    HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML){
        var r=this.ownerDocument.createRange();
        r.setStartBefore(this);
        var df=r.createContextualFragment(sHTML);
        this.parentNode.replaceChild(df,this);
        return sHTML;
        });
    HTMLElement.prototype.__defineGetter__("outerHTML",function(){
     var attr;
        var attrs=this.attributes;
        var str="<"+this.tagName.toLowerCase();
        for(var i=0;i<attrs.length;i++){
            attr=attrs[i];
            if(attr.specified)
                str+=" "+attr.name+'="'+attr.value+'"';
            }
        if(!this.canHaveChildren)
            return str+">";
        return str+">"+this.innerHTML+"</"+this.tagName.toLowerCase()+">";
        });
 HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){
  switch(this.tagName.toLowerCase()){
            case "area":
            case "base":
         case "basefont":
            case "col":
            case "frame":
            case "hr":
            case "img":
            case "br":
            case "input":
            case "isindex":
            case "link":
            case "meta":
            case "param":
            return false;
        }
        return true;
     });
    HTMLElement.prototype.__defineGetter__("innerText",
        function(){
            var anyString = "";
            var childS = this.childNodes;
            for(var i=0; i<childS.length; i++) {
                if(childS[i].nodeType==1){
                    anyString += childS[i].tagName=="BR" ? 'n' : childS[i].innerText;
                }else if(childS[i].nodeType==3){
                    anyString += childS[i].nodeValue;
                }
            }
            return anyString;
        }
    );
    HTMLElement.prototype.__defineSetter__("innerText",
        function(sText){
            this.textContent=sText;
        }
    );
}
//格式化
function FormatText(editor) {
   var myeditor = editor;
   if (myeditor.mode=="wysiwyg"){
        var tempimg = new Array();
        var temptable = new Array();
        var tempobject = new Array();
        var isPart = false; //暂时无法实现局部格式化
        if (!isPart) {
            var tmpDiv=document.createElement("DIV");
            var editorhtml = myeditor.getData();
            //page
            //editorhtml = editorhtml.replace(/<div style="page-break-after: always;?">s*<span style="display: none;?">&nbsp;</span>s*</div>/gi,'<p>                                      
相关文章
猜你需要