paragraph.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * 段落样式
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. /**
  7. * 段落格式
  8. * @command paragraph
  9. * @method execCommand
  10. * @param { String } cmd 命令字符串
  11. * @param {String} style 标签值为:'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
  12. * @param {Object} attrs 标签的属性
  13. * @example
  14. * ```javascript
  15. * editor.execCommand( 'Paragraph','h1','{
  16. * class:'test'
  17. * }' );
  18. * ```
  19. */
  20. /**
  21. * 返回选区内节点标签名
  22. * @command paragraph
  23. * @method queryCommandValue
  24. * @param { String } cmd 命令字符串
  25. * @return { String } 节点标签名
  26. * @example
  27. * ```javascript
  28. * editor.queryCommandValue( 'Paragraph' );
  29. * ```
  30. */
  31. UE.plugins['paragraph'] = function() {
  32. var me = this,
  33. block = domUtils.isBlockElm,
  34. notExchange = ['TD','LI','PRE'],
  35. doParagraph = function(range,style,attrs,sourceCmdName){
  36. var bookmark = range.createBookmark(),
  37. filterFn = function( node ) {
  38. return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' && !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace( node );
  39. },
  40. para;
  41. range.enlarge( true );
  42. var bookmark2 = range.createBookmark(),
  43. current = domUtils.getNextDomNode( bookmark2.start, false, filterFn ),
  44. tmpRange = range.cloneRange(),
  45. tmpNode;
  46. while ( current && !(domUtils.getPosition( current, bookmark2.end ) & domUtils.POSITION_FOLLOWING) ) {
  47. if ( current.nodeType == 3 || !block( current ) ) {
  48. tmpRange.setStartBefore( current );
  49. while ( current && current !== bookmark2.end && !block( current ) ) {
  50. tmpNode = current;
  51. current = domUtils.getNextDomNode( current, false, null, function( node ) {
  52. return !block( node );
  53. } );
  54. }
  55. tmpRange.setEndAfter( tmpNode );
  56. para = range.document.createElement( style );
  57. if(attrs){
  58. domUtils.setAttributes(para,attrs);
  59. if(sourceCmdName && sourceCmdName == 'customstyle' && attrs.style){
  60. para.style.cssText = attrs.style;
  61. }
  62. }
  63. para.appendChild( tmpRange.extractContents() );
  64. //需要内容占位
  65. if(domUtils.isEmptyNode(para)){
  66. domUtils.fillChar(range.document,para);
  67. }
  68. tmpRange.insertNode( para );
  69. var parent = para.parentNode;
  70. //如果para上一级是一个block元素且不是body,td就删除它
  71. if ( block( parent ) && !domUtils.isBody( para.parentNode ) && utils.indexOf(notExchange,parent.tagName)==-1) {
  72. //存储dir,style
  73. if(!(sourceCmdName && sourceCmdName == 'customstyle')){
  74. parent.getAttribute('dir') && para.setAttribute('dir',parent.getAttribute('dir'));
  75. //trace:1070
  76. parent.style.cssText && (para.style.cssText = parent.style.cssText + ';' + para.style.cssText);
  77. //trace:1030
  78. parent.style.textAlign && !para.style.textAlign && (para.style.textAlign = parent.style.textAlign);
  79. parent.style.textIndent && !para.style.textIndent && (para.style.textIndent = parent.style.textIndent);
  80. parent.style.padding && !para.style.padding && (para.style.padding = parent.style.padding);
  81. }
  82. //trace:1706 选择的就是h1-6要删除
  83. if( attrs && /h\d/i.test(parent.tagName) && !/h\d/i.test(para.tagName) ){
  84. domUtils.setAttributes(parent,attrs);
  85. if(sourceCmdName && sourceCmdName == 'customstyle' && attrs.style){
  86. parent.style.cssText = attrs.style;
  87. }
  88. domUtils.remove(para.parentNode,true);
  89. para = parent;
  90. }else{
  91. domUtils.remove( para.parentNode, true );
  92. }
  93. }
  94. if( utils.indexOf(notExchange,parent.tagName)!=-1){
  95. current = parent;
  96. }else{
  97. current = para;
  98. }
  99. current = domUtils.getNextDomNode( current, false, filterFn );
  100. } else {
  101. current = domUtils.getNextDomNode( current, true, filterFn );
  102. }
  103. }
  104. return range.moveToBookmark( bookmark2 ).moveToBookmark( bookmark );
  105. };
  106. me.setOpt('paragraph',{'p':'', 'h1':'', 'h2':'', 'h3':'', 'h4':'', 'h5':'', 'h6':''});
  107. me.commands['paragraph'] = {
  108. execCommand : function( cmdName, style,attrs,sourceCmdName ) {
  109. var range = this.selection.getRange();
  110. //闭合时单独处理
  111. if(range.collapsed){
  112. var txt = this.document.createTextNode('p');
  113. range.insertNode(txt);
  114. //去掉冗余的fillchar
  115. if(browser.ie){
  116. var node = txt.previousSibling;
  117. if(node && domUtils.isWhitespace(node)){
  118. domUtils.remove(node);
  119. }
  120. node = txt.nextSibling;
  121. if(node && domUtils.isWhitespace(node)){
  122. domUtils.remove(node);
  123. }
  124. }
  125. }
  126. range = doParagraph(range,style,attrs,sourceCmdName);
  127. if(txt){
  128. range.setStartBefore(txt).collapse(true);
  129. pN = txt.parentNode;
  130. domUtils.remove(txt);
  131. if(domUtils.isBlockElm(pN)&&domUtils.isEmptyNode(pN)){
  132. domUtils.fillNode(this.document,pN);
  133. }
  134. }
  135. if(browser.gecko && range.collapsed && range.startContainer.nodeType == 1){
  136. var child = range.startContainer.childNodes[range.startOffset];
  137. if(child && child.nodeType == 1 && child.tagName.toLowerCase() == style){
  138. range.setStart(child,0).collapse(true);
  139. }
  140. }
  141. //trace:1097 原来有true,原因忘了,但去了就不能清除多余的占位符了
  142. range.select();
  143. return true;
  144. },
  145. queryCommandValue : function() {
  146. var node = domUtils.filterNodeList(this.selection.getStartElementPath(),'p h1 h2 h3 h4 h5 h6');
  147. return node ? node.tagName.toLowerCase() : '';
  148. }
  149. };
  150. };