| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- ///import core
- ///commands 段落格式,居左,居右,居中,两端对齐
- ///commandsName JustifyLeft,JustifyCenter,JustifyRight,JustifyJustify
- ///commandsTitle 居左对齐,居中对齐,居右对齐,两端对齐
- /**
- * @description 居左右中
- * @name baidu.editor.execCommand
- * @param {String} cmdName justify执行对齐方式的命令
- * @param {String} align 对齐方式:left居左,right居右,center居中,justify两端对齐
- * @author zhanyi
- */
- (function(){
- var 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.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(this.currentSelectedArr && this.currentSelectedArr.length > 0){
- for(var i=0,ti;ti=this.currentSelectedArr[i++];){
- if(domUtils.isEmptyNode(ti)){
- txt = this.document.createTextNode('p');
- range.setStart(ti,0).collapse(true).insertNode(txt).selectNode(txt);
-
- }else{
- range.selectNodeContents(ti)
- }
- doJustify(range,align);
- txt && domUtils.remove(txt);
- }
- range.selectNode(this.currentSelectedArr[0]).select()
- }else{
- //闭合时单独处理
- 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(){
- return this.highlight ? -1 : 0;
-
- }
- }
- })();
|