basestyle.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * B、I、sub、super命令支持
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. UE.plugins["basestyle"] = function() {
  7. /**
  8. * 字体加粗
  9. * @command bold
  10. * @param { String } cmd 命令字符串
  11. * @remind 对已加粗的文本内容执行该命令, 将取消加粗
  12. * @method execCommand
  13. * @example
  14. * ```javascript
  15. * //editor是编辑器实例
  16. * //对当前选中的文本内容执行加粗操作
  17. * //第一次执行, 文本内容加粗
  18. * editor.execCommand( 'bold' );
  19. *
  20. * //第二次执行, 文本内容取消加粗
  21. * editor.execCommand( 'bold' );
  22. * ```
  23. */
  24. /**
  25. * 字体倾斜
  26. * @command italic
  27. * @method execCommand
  28. * @param { String } cmd 命令字符串
  29. * @remind 对已倾斜的文本内容执行该命令, 将取消倾斜
  30. * @example
  31. * ```javascript
  32. * //editor是编辑器实例
  33. * //对当前选中的文本内容执行斜体操作
  34. * //第一次操作, 文本内容将变成斜体
  35. * editor.execCommand( 'italic' );
  36. *
  37. * //再次对同一文本内容执行, 则文本内容将恢复正常
  38. * editor.execCommand( 'italic' );
  39. * ```
  40. */
  41. /**
  42. * 下标文本,与“superscript”命令互斥
  43. * @command subscript
  44. * @method execCommand
  45. * @remind 把选中的文本内容切换成下标文本, 如果当前选中的文本已经是下标, 则该操作会把文本内容还原成正常文本
  46. * @param { String } cmd 命令字符串
  47. * @example
  48. * ```javascript
  49. * //editor是编辑器实例
  50. * //对当前选中的文本内容执行下标操作
  51. * //第一次操作, 文本内容将变成下标文本
  52. * editor.execCommand( 'subscript' );
  53. *
  54. * //再次对同一文本内容执行, 则文本内容将恢复正常
  55. * editor.execCommand( 'subscript' );
  56. * ```
  57. */
  58. /**
  59. * 上标文本,与“subscript”命令互斥
  60. * @command superscript
  61. * @method execCommand
  62. * @remind 把选中的文本内容切换成上标文本, 如果当前选中的文本已经是上标, 则该操作会把文本内容还原成正常文本
  63. * @param { String } cmd 命令字符串
  64. * @example
  65. * ```javascript
  66. * //editor是编辑器实例
  67. * //对当前选中的文本内容执行上标操作
  68. * //第一次操作, 文本内容将变成上标文本
  69. * editor.execCommand( 'superscript' );
  70. *
  71. * //再次对同一文本内容执行, 则文本内容将恢复正常
  72. * editor.execCommand( 'superscript' );
  73. * ```
  74. */
  75. var basestyles = {
  76. bold: ["strong", "b"],
  77. italic: ["em", "i"],
  78. subscript: ["sub"],
  79. superscript: ["sup"]
  80. },
  81. getObj = function(editor, tagNames) {
  82. return domUtils.filterNodeList(
  83. editor.selection.getStartElementPath(),
  84. tagNames
  85. );
  86. },
  87. me = this;
  88. //添加快捷键
  89. me.addshortcutkey({
  90. Bold: "ctrl+66", //^B
  91. Italic: "ctrl+73", //^I
  92. Underline: "ctrl+85" //^U
  93. });
  94. me.addInputRule(function(root) {
  95. utils.each(root.getNodesByTagName("b i"), function(node) {
  96. switch (node.tagName) {
  97. case "b":
  98. node.tagName = "strong";
  99. break;
  100. case "i":
  101. node.tagName = "em";
  102. }
  103. });
  104. });
  105. for (var style in basestyles) {
  106. (function(cmd, tagNames) {
  107. me.commands[cmd] = {
  108. execCommand: function(cmdName) {
  109. var range = me.selection.getRange(),
  110. obj = getObj(this, tagNames);
  111. if (range.collapsed) {
  112. if (obj) {
  113. var tmpText = me.document.createTextNode("");
  114. range.insertNode(tmpText).removeInlineStyle(tagNames);
  115. range.setStartBefore(tmpText);
  116. domUtils.remove(tmpText);
  117. } else {
  118. var tmpNode = range.document.createElement(tagNames[0]);
  119. if (cmdName == "superscript" || cmdName == "subscript") {
  120. tmpText = me.document.createTextNode("");
  121. range
  122. .insertNode(tmpText)
  123. .removeInlineStyle(["sub", "sup"])
  124. .setStartBefore(tmpText)
  125. .collapse(true);
  126. }
  127. range.insertNode(tmpNode).setStart(tmpNode, 0);
  128. }
  129. range.collapse(true);
  130. } else {
  131. if (cmdName == "superscript" || cmdName == "subscript") {
  132. if (!obj || obj.tagName.toLowerCase() != cmdName) {
  133. range.removeInlineStyle(["sub", "sup"]);
  134. }
  135. }
  136. obj
  137. ? range.removeInlineStyle(tagNames)
  138. : range.applyInlineStyle(tagNames[0]);
  139. }
  140. range.select();
  141. },
  142. queryCommandState: function() {
  143. return getObj(this, tagNames) ? 1 : 0;
  144. }
  145. };
  146. })(style, basestyles[style]);
  147. }
  148. };