justify.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ///import core
  2. ///commands 段落格式,居左,居右,居中,两端对齐
  3. ///commandsName JustifyLeft,JustifyCenter,JustifyRight,JustifyJustify
  4. ///commandsTitle 居左对齐,居中对齐,居右对齐,两端对齐
  5. /**
  6. * @description 居左右中
  7. * @name baidu.editor.execCommand
  8. * @param {String} cmdName justify执行对齐方式的命令
  9. * @param {String} align 对齐方式:left居左,right居右,center居中,justify两端对齐
  10. * @author zhanyi
  11. */
  12. (function(){
  13. var block = domUtils.isBlockElm,
  14. defaultValue = {
  15. left : 1,
  16. right : 1,
  17. center : 1,
  18. justify : 1
  19. },
  20. doJustify = function(range,style){
  21. var bookmark = range.createBookmark(),
  22. filterFn = function( node ) {
  23. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' && !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace( node )
  24. };
  25. range.enlarge(true);
  26. var bookmark2 = range.createBookmark(),
  27. current = domUtils.getNextDomNode(bookmark2.start,false,filterFn),
  28. tmpRange = range.cloneRange(),
  29. tmpNode;
  30. while(current && !(domUtils.getPosition(current,bookmark2.end)&domUtils.POSITION_FOLLOWING)){
  31. if(current.nodeType == 3 || !block(current)){
  32. tmpRange.setStartBefore(current);
  33. while(current && current!==bookmark2.end && !block(current)){
  34. tmpNode = current;
  35. current = domUtils.getNextDomNode(current,false,null,function(node){
  36. return !block(node)
  37. });
  38. }
  39. tmpRange.setEndAfter(tmpNode);
  40. var common = tmpRange.getCommonAncestor();
  41. if( !domUtils.isBody(common) && block(common)){
  42. domUtils.setStyles(common,utils.isString(style) ? {'text-align':style} : style);
  43. current = common;
  44. }else{
  45. var p = range.document.createElement('p');
  46. domUtils.setStyles(p,utils.isString(style) ? {'text-align':style} : style);
  47. var frag = tmpRange.extractContents();
  48. p.appendChild(frag);
  49. tmpRange.insertNode(p);
  50. current = p;
  51. }
  52. current = domUtils.getNextDomNode(current,false,filterFn);
  53. }else{
  54. current = domUtils.getNextDomNode(current,true,filterFn);
  55. }
  56. }
  57. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark)
  58. };
  59. UE.commands['justify'] = {
  60. execCommand : function( cmdName,align ) {
  61. var range = this.selection.getRange(),
  62. txt;
  63. if(this.currentSelectedArr && this.currentSelectedArr.length > 0){
  64. for(var i=0,ti;ti=this.currentSelectedArr[i++];){
  65. if(domUtils.isEmptyNode(ti)){
  66. txt = this.document.createTextNode('p');
  67. range.setStart(ti,0).collapse(true).insertNode(txt).selectNode(txt);
  68. }else{
  69. range.selectNodeContents(ti)
  70. }
  71. doJustify(range,align);
  72. txt && domUtils.remove(txt);
  73. }
  74. range.selectNode(this.currentSelectedArr[0]).select()
  75. }else{
  76. //闭合时单独处理
  77. if(range.collapsed){
  78. txt = this.document.createTextNode('p');
  79. range.insertNode(txt);
  80. }
  81. doJustify(range,align);
  82. if(txt){
  83. range.setStartBefore(txt).collapse(true);
  84. domUtils.remove(txt);
  85. }
  86. range.select();
  87. }
  88. return true;
  89. },
  90. queryCommandValue : function() {
  91. var startNode = this.selection.getStart(),
  92. value = domUtils.getComputedStyle(startNode,'text-align');
  93. return defaultValue[value] ? value : 'left';
  94. },
  95. queryCommandState : function(){
  96. return this.highlight ? -1 : 0;
  97. }
  98. }
  99. })();