paragraph.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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
  39. ? node.tagName.toLowerCase() != "br" &&
  40. !domUtils.isBookmarkNode(node)
  41. : !domUtils.isWhitespace(node);
  42. },
  43. para;
  44. range.enlarge(true);
  45. var bookmark2 = range.createBookmark(),
  46. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  47. tmpRange = range.cloneRange(),
  48. tmpNode;
  49. while (
  50. current &&
  51. !(
  52. domUtils.getPosition(current, bookmark2.end) &
  53. domUtils.POSITION_FOLLOWING
  54. )
  55. ) {
  56. if (current.nodeType == 3 || !block(current)) {
  57. tmpRange.setStartBefore(current);
  58. while (current && current !== bookmark2.end && !block(current)) {
  59. tmpNode = current;
  60. current = domUtils.getNextDomNode(current, false, null, function(
  61. node
  62. ) {
  63. return !block(node);
  64. });
  65. }
  66. tmpRange.setEndAfter(tmpNode);
  67. para = range.document.createElement(style);
  68. if (attrs) {
  69. domUtils.setAttributes(para, attrs);
  70. if (
  71. sourceCmdName &&
  72. sourceCmdName == "customstyle" &&
  73. attrs.style
  74. ) {
  75. para.style.cssText = attrs.style;
  76. }
  77. }
  78. para.appendChild(tmpRange.extractContents());
  79. //需要内容占位
  80. if (domUtils.isEmptyNode(para)) {
  81. domUtils.fillChar(range.document, para);
  82. }
  83. tmpRange.insertNode(para);
  84. var parent = para.parentNode;
  85. //如果para上一级是一个block元素且不是body,td就删除它
  86. if (
  87. block(parent) &&
  88. !domUtils.isBody(para.parentNode) &&
  89. utils.indexOf(notExchange, parent.tagName) == -1
  90. ) {
  91. //存储dir,style
  92. if (!(sourceCmdName && sourceCmdName == "customstyle")) {
  93. parent.getAttribute("dir") &&
  94. para.setAttribute("dir", parent.getAttribute("dir"));
  95. //trace:1070
  96. parent.style.cssText &&
  97. (para.style.cssText =
  98. parent.style.cssText + ";" + para.style.cssText);
  99. //trace:1030
  100. parent.style.textAlign &&
  101. !para.style.textAlign &&
  102. (para.style.textAlign = parent.style.textAlign);
  103. parent.style.textIndent &&
  104. !para.style.textIndent &&
  105. (para.style.textIndent = parent.style.textIndent);
  106. parent.style.padding &&
  107. !para.style.padding &&
  108. (para.style.padding = parent.style.padding);
  109. }
  110. //trace:1706 选择的就是h1-6要删除
  111. if (
  112. attrs &&
  113. /h\d/i.test(parent.tagName) &&
  114. !/h\d/i.test(para.tagName)
  115. ) {
  116. domUtils.setAttributes(parent, attrs);
  117. if (
  118. sourceCmdName &&
  119. sourceCmdName == "customstyle" &&
  120. attrs.style
  121. ) {
  122. parent.style.cssText = attrs.style;
  123. }
  124. domUtils.remove(para.parentNode, true);
  125. para = parent;
  126. } else {
  127. domUtils.remove(para.parentNode, true);
  128. }
  129. }
  130. if (utils.indexOf(notExchange, parent.tagName) != -1) {
  131. current = parent;
  132. } else {
  133. current = para;
  134. }
  135. current = domUtils.getNextDomNode(current, false, filterFn);
  136. } else {
  137. current = domUtils.getNextDomNode(current, true, filterFn);
  138. }
  139. }
  140. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  141. };
  142. me.setOpt("paragraph", {
  143. p: "",
  144. h1: "",
  145. h2: "",
  146. h3: "",
  147. h4: "",
  148. h5: "",
  149. h6: ""
  150. });
  151. me.commands["paragraph"] = {
  152. execCommand: function(cmdName, style, attrs, sourceCmdName) {
  153. var range = this.selection.getRange();
  154. //闭合时单独处理
  155. if (range.collapsed) {
  156. var txt = this.document.createTextNode("p");
  157. range.insertNode(txt);
  158. //去掉冗余的fillchar
  159. if (browser.ie) {
  160. var node = txt.previousSibling;
  161. if (node && domUtils.isWhitespace(node)) {
  162. domUtils.remove(node);
  163. }
  164. node = txt.nextSibling;
  165. if (node && domUtils.isWhitespace(node)) {
  166. domUtils.remove(node);
  167. }
  168. }
  169. }
  170. range = doParagraph(range, style, attrs, sourceCmdName);
  171. if (txt) {
  172. range.setStartBefore(txt).collapse(true);
  173. pN = txt.parentNode;
  174. domUtils.remove(txt);
  175. if (domUtils.isBlockElm(pN) && domUtils.isEmptyNode(pN)) {
  176. domUtils.fillNode(this.document, pN);
  177. }
  178. }
  179. if (
  180. browser.gecko &&
  181. range.collapsed &&
  182. range.startContainer.nodeType == 1
  183. ) {
  184. var child = range.startContainer.childNodes[range.startOffset];
  185. if (
  186. child &&
  187. child.nodeType == 1 &&
  188. child.tagName.toLowerCase() == style
  189. ) {
  190. range.setStart(child, 0).collapse(true);
  191. }
  192. }
  193. //trace:1097 原来有true,原因忘了,但去了就不能清除多余的占位符了
  194. range.select();
  195. return true;
  196. },
  197. queryCommandValue: function() {
  198. var node = domUtils.filterNodeList(
  199. this.selection.getStartElementPath(),
  200. "p h1 h2 h3 h4 h5 h6"
  201. );
  202. return node ? node.tagName.toLowerCase() : "";
  203. }
  204. };
  205. };