font.js 14.2 KB
Newer Older
C
campaign 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
///import core
///import plugins\removeformat.js
///commands 字体颜色,背景色,字号,字体,下划线,删除线
///commandsName  ForeColor,BackColor,FontSize,FontFamily,Underline,StrikeThrough
///commandsTitle  字体颜色,背景色,字号,字体,下划线,删除线
/**
 * @description 字体
 * @name baidu.editor.execCommand
 * @param {String}     cmdName    执行的功能名称
 * @param {String}    value             传入的值
 */
UE.plugins['font'] = function() {
    var me = this,
        fonts = {
            'forecolor':'color',
            'backcolor':'background-color',
            'fontsize':'font-size',
            'fontfamily':'font-family',
            'underline':'text-decoration',
C
campaign 已提交
20 21 22 23
            'strikethrough':'text-decoration',
            'fontborder':'border'
        },
        needCmd = {'underline':1,'strikethrough':1,'fontborder':1};
C
campaign 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    me.setOpt({
        'fontfamily':[
            { name:'songti',val:'宋体,SimSun'},
            { name:'yahei',val:'微软雅黑,Microsoft YaHei'},
            { name:'kaiti',val:'楷体,楷体_GB2312, SimKai'},
            { name:'heiti',val:'黑体, SimHei'},
            { name:'lishu',val:'隶书, SimLi'},
            { name:'andaleMono',val:'andale mono'},
            { name:'arial',val:'arial, helvetica,sans-serif'},
            { name:'arialBlack',val:'arial black,avant garde'},
            { name:'comicSansMs',val:'comic sans ms'},
            { name:'impact',val:'impact,chicago'},
            { name:'timesNewRoman',val:'times new roman'}
          ],
        'fontsize':[10, 11, 12, 14, 16, 18, 20, 24, 36]
    });
C
campaign 已提交
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    function mergesibling(range){
        var collapsed = range.collapsed,
            bk = range.createBookmark(),common;
        if(collapsed){
            common = bk.start.parentNode;
            while(dtd.$inline[common.tagName]){
                common = common.parentNode;
            }
        }else{

            common = domUtils.getCommonAncestor(bk.start,bk.end);
        }

        utils.each(domUtils.getElementsByTagName(common,'span'),function(node){
            if(!node.parentNode){
                return;
            }
            function isBorder(n){
                if(!n)
                    return false;
                if(n.tagName != 'SPAN')
                    return false;
                var val;
                if(val = domUtils.getComputedStyle( n, 'border' )){
                    if(/1px/.test(val) && /solid/.test(val)){
                        return true;
                    }
                }
                return false;
            }
            function nextSibling(node){
                var next = node.nextSibling;
                if(next && domUtils.isBookmarkNode(next)){
                    next = next.nextSibling;
                }
                return next;
            }

            var span = me.document.createElement('span');
            span.style.cssText = 'border:1px solid #000';
            node.parentNode.insertBefore(span,node);
            while(isBorder(node) && isBorder(nextSibling(node))){
                var val = utils.trim(node.style.cssText.replace(/border[^:]+:[^;]+;?/gi,''));
                node.style.cssText = val;
                var tmp  = node.nextSibling;
                if(!val){
                    while(node.firstChild){
                        if(isBorder(node.firstChild)){
                            node.firstChild.style.cssText = node.firstChild.style.cssText.replace(/border[^:]+:[^;]+;?/gi,'');
                        }
                        span.appendChild(node.firstChild)
                    }
                    domUtils.remove(node)
                }else{
                    span.appendChild(node);
                }
                if(domUtils.isBookmarkNode(tmp)){
                    node = tmp.nextSibling;
                    span.appendChild(tmp)
                }else{
                    node = tmp;
                }
            }
            if(!span.firstChild){
                domUtils.remove(span)
            }else{
                if(isBorder(span) && isBorder(nextSibling(span))){
                    node = span.nextSibling;
                    var val = utils.trim(node.style.cssText.replace(/border[^:]+:[^;]+;?/gi,''));
                    node.style.cssText = val;

                    if(!val){
                        while(node.firstChild){
                            if(isBorder(node.firstChild)){
                                node.firstChild.style.cssText = node.firstChild.style.cssText.replace(/border[^:]+:[^;];?/,'');
                            }
                            span.appendChild(node.firstChild)
                        }
                        domUtils.remove(node)
                    }else{
                        span.appendChild(node);
                    }
                }
            }
            if(isBorder(node)){
                var tmpParent = node.parentNode;
                while(dtd.$inline[tmpParent.tagName]){
                    if(isBorder(tmpParent)){
                        var val = utils.trim(node.style.cssText.replace(/border[^:]+:[^;]+;?/gi,''));
                        node.style.cssText = val;
                        if(!val){
                            domUtils.remove(node,true)
                        }
                        break;
                    }
                    tmpParent = tmpParent.parentNode;

                }

            }

        });
        range.moveToBookmark(bk);
    }
C
campaign 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    me.addInputRule(function(root){
        utils.each(root.getNodesByTagName('u s del font'),function(node){
            if(node.tagName == 'font'){
                var cssStyle = [];
                for(var p in node.attrs){
                    switch (p){
                        case 'size':
                            cssStyle.push('font-size:' + node.attrs[p] + 'px');
                            break;
                        case 'color':
                            cssStyle.push('color:'+ node.attrs[p]);
                            break;
                        case 'face':
                            cssStyle.push('font-family:'+ node.attrs[p]);
                            break;
                        case 'style':
                            cssStyle.push(node.attrs[p]);
                    }
                }
                node.attrs = {
                    'style' : cssStyle.join(';')
                };
            }else{
                var val = node.tagName == 'u' ? 'underline' : 'line-through';
                node.attrs = {
                    'style' : (node.getAttr('style') || '') + 'text-decoration:'+val+';'
                }
            }
            node.tagName = 'span';
        });
    });
C
campaign 已提交
175 176 177 178
    for ( var p in fonts ) {
        (function( cmd, style ) {
            UE.commands[cmd] = {
                execCommand : function( cmdName, value ) {
C
campaign 已提交
179 180 181
                    value = value || (this.queryCommandState(cmdName) ? 'none' : cmdName == 'underline' ? 'underline' :
                        cmdName == 'fontborder' ? '1px solid #000' :
                       'line-through');
C
campaign 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
                    var me = this,
                        range = this.selection.getRange(),
                        text;

                    if ( value == 'default' ) {

                        if(range.collapsed){
                            text = me.document.createTextNode('font');
                            range.insertNode(text).select();

                        }
                        me.execCommand( 'removeFormat', 'span,a', style);
                        if(text){
                            range.setStartBefore(text).setCursor();
                            domUtils.remove(text);
                        }


                    } else {
                            if ( !range.collapsed ) {
C
campaign 已提交
202
                                if(needCmd[cmd] && me.queryCommandValue(cmd)){
C
campaign 已提交
203 204 205 206
                                     me.execCommand( 'removeFormat', 'span,a', style );
                                }
                                range = me.selection.getRange();

C
campaign 已提交
207 208 209
                                range.applyInlineStyle( 'span', {'style':style + ':' + value} );
                                mergesibling(range);
                                range.select();
C
campaign 已提交
210 211 212 213 214 215 216
                            } else {

                                var span = domUtils.findParentByTagName(range.startContainer,'span',true);
                                text = me.document.createTextNode('font');
                                if(span && !span.children.length && !span[browser.ie ? 'innerText':'textContent'].replace(fillCharReg,'').length){
                                    //for ie hack when enter
                                    range.insertNode(text);
C
campaign 已提交
217
                                     if(needCmd[cmd]){
C
campaign 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
                                         range.selectNode(text).select();
                                         me.execCommand( 'removeFormat','span,a', style, null );

                                         span = domUtils.findParentByTagName(text,'span',true);
                                         range.setStartBefore(text);

                                    }
                                    span.style.cssText += ';' + style + ':' + value;
                                    range.collapse(true).select();


                                }else{
                                    range.insertNode(text);
                                    range.selectNode(text).select();
                                    span = range.document.createElement( 'span' );

C
campaign 已提交
234
                                    if(needCmd[cmd]){
C
campaign 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                                        //a标签内的不处理跳过
                                        if(domUtils.findParentByTagName(text,'a',true)){
                                            range.setStartBefore(text).setCursor();
                                             domUtils.remove(text);
                                             return;
                                         }
                                         me.execCommand( 'removeFormat','span,a', style );
                                    }

                                    span.style.cssText = style + ':' + value;


                                    text.parentNode.insertBefore(span,text);
                                    //修复,span套span 但样式不继承的问题
                                    if(!browser.ie || browser.ie && browser.version == 9){
                                        var spanParent = span.parentNode;
                                        while(!domUtils.isBlockElm(spanParent)){
                                            if(spanParent.tagName == 'SPAN'){
                                                //opera合并style不会加入";"
                                                span.style.cssText = spanParent.style.cssText + ";" + span.style.cssText;
                                            }
                                            spanParent = spanParent.parentNode;
                                        }
                                    }


C
campaign 已提交
261

C
campaign 已提交
262 263
                                    if(opera){
                                        setTimeout(function(){
C
campaign 已提交
264 265 266
                                            range.setStart(span,0).collapse(true);
                                            mergesibling(range);
                                            range.select();
C
campaign 已提交
267 268
                                        });
                                    }else{
C
campaign 已提交
269 270 271
                                        range.setStart(span,0).collapse(true);
                                        mergesibling(range);
                                        range.select();
C
campaign 已提交
272 273 274 275 276 277 278
                                    }

                                    //trace:981
                                    //domUtils.mergeToParent(span)
                                }
                                domUtils.remove(text);
                            }
C
campaign 已提交
279

C
campaign 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

                    }
                    return true;
                },
                queryCommandValue : function (cmdName) {
                    var startNode = this.selection.getStart();

                    //trace:946
                    if(cmdName == 'underline'||cmdName=='strikethrough' ){
                        var tmpNode = startNode,value;
                        while(tmpNode && !domUtils.isBlockElm(tmpNode) && !domUtils.isBody(tmpNode)){
                            if(tmpNode.nodeType == 1){
                                value = domUtils.getComputedStyle( tmpNode, style );

                                if(value != 'none'){
                                    return value;
                                }
                            }

                            tmpNode = tmpNode.parentNode;
                        }
                        return 'none';
                    }
C
campaign 已提交
303 304 305 306 307 308 309 310 311 312 313 314
                    if(cmdName == 'fontborder'){
                        var tmp = startNode,val;
                        while(tmp && dtd.$inline[tmp.tagName]){
                            if(val = domUtils.getComputedStyle( tmp, 'border' )){
                                if( /1px/.test(val) && /solid/.test(val)){
                                    return val;
                                }
                            }
                            tmp = tmp.parentNode;
                        }
                        return ''
                    }
C
campaign 已提交
315 316 317
                    return  domUtils.getComputedStyle( startNode, style );
                },
                queryCommandState : function(cmdName){
C
campaign 已提交
318
                    if(!needCmd[cmdName])
C
campaign 已提交
319
                        return 0;
C
campaign 已提交
320 321 322 323 324 325
                    var val = this.queryCommandValue(cmdName);
                    if(cmdName == 'fontborder'){
                        return /1px/.test(val) && /solid/.test(val)
                    }else{
                        return  val == (cmdName == 'underline' ?
                            'underline' : 'line-through');
C
campaign 已提交
326
                    }
C
campaign 已提交
327

C
campaign 已提交
328 329 330 331 332
                }
            };
        })( p, fonts[p] );
    }
};