当前位置:   article > 正文

让UEditor支持表格本身的对齐方式(不是td内容)_ueditor 表格

ueditor 表格

1.第一步,修改queryCommandState函数,让选择表格时,对齐插件处于可用状态。
2.第二步,修改doJustify函数,判断是表格元素时,设置对应的样式。

UE.plugins['justify']=function(){
        var me=this,
            block = domUtils.isBlockElm,
            defaultValue = {
                left:1,
                right:1,
                center:1,
                justify:1
            },
            doJustify = function (range, style) {
                var bookmark = range.createBookmark(),
                    filterFn = function (node) {
                        return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' && !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace(node);
                    };
    
                range.enlarge(true);
                var bookmark2 = range.createBookmark(),
                    current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
                    tmpRange = range.cloneRange(),
                    tmpNode;
                while (current && !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
                    if(current.nodeName.toLowerCase()=="table"){
                        if(style.toLowerCase()=="left"){
                            current.setAttribute("style","​margin:0px auto 0px 0px;");
                        }else if(style.toLowerCase()=="center"){
                            current.setAttribute("style","​margin:0px auto 0px auto;");
                        }else if(style.toLowerCase()=="right"){
                            current.setAttribute("style","​margin:0px 0px 0px auto;​");
                        }else{
                            current.setAttribute("style","​margin:auto;​");
                        }
                    }
                    if (current.nodeType == 3 || !block(current)) {
                        tmpRange.setStartBefore(current);
                        while (current && current !== bookmark2.end && !block(current)) {
                            tmpNode = current;
                            current = domUtils.getNextDomNode(current, false, null, function (node) {
                                return !block(node);
                            });
                        }
                        tmpRange.setEndAfter(tmpNode);
                        var common = tmpRange.getCommonAncestor();
                        if (!domUtils.isBody(common) && block(common)) {
                            domUtils.setStyles(common, utils.isString(style) ? {'text-align':style} : style);
                            current = common;
                        } else {
                            var p = range.document.createElement('p');
                            domUtils.setStyles(p, utils.isString(style) ? {'text-align':style} : style);
                            var frag = tmpRange.extractContents();
                            p.appendChild(frag);
                            tmpRange.insertNode(p);
                            current = p;
                        }
                        current = domUtils.getNextDomNode(current, false, filterFn);
                    } else {
                        current = domUtils.getNextDomNode(current, true, filterFn);
                    }
                }
                return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
            };
    
        UE.commands['justify'] = {
            execCommand:function (cmdName, align) {
                var range = this.selection.getRange(),
                    txt;
    
                //闭合时单独处理
                if (range.collapsed) {
                    txt = this.document.createTextNode('p');
                    range.insertNode(txt);
                }
                doJustify(range, align);
                if (txt) {
                    range.setStartBefore(txt).collapse(true);
                    domUtils.remove(txt);
                }
    
                range.select();
    
    
                return true;
            },
            queryCommandValue:function () {
                var startNode = this.selection.getStart(),
                    value = domUtils.getComputedStyle(startNode, 'text-align');
                return defaultValue[value] ? value : 'left';
            },
            queryCommandState:function () {
                // var start = this.selection.getStart(),
                //     cell = start && domUtils.findParentByTagName(start, ["td", "th","caption"], true);
                // return cell? -1:0;
                return 0;//不管选择什么元素都能设置对齐方式
            }
    
        };
    };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/299191
推荐阅读
相关标签
  

闽ICP备14008679号