font.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ///import core
  2. ///import plugins\removeformat.js
  3. ///commands 字体颜色,背景色,字号,字体,下划线,删除线
  4. ///commandsName ForeColor,BackColor,FontSize,FontFamily,Underline,StrikeThrough
  5. ///commandsTitle 字体颜色,背景色,字号,字体,下划线,删除线
  6. /**
  7. * @description 字体
  8. * @name baidu.editor.execCommand
  9. * @param {String} cmdName 执行的功能名称
  10. * @param {String} value 传入的值
  11. */
  12. UE.plugins['font'] = function() {
  13. var me = this,
  14. fonts = {
  15. 'forecolor':'color',
  16. 'backcolor':'background-color',
  17. 'fontsize':'font-size',
  18. 'fontfamily':'font-family',
  19. 'underline':'text-decoration',
  20. 'strikethrough':'text-decoration'
  21. };
  22. me.setOpt({
  23. 'fontfamily':[
  24. ['宋体',['宋体', 'SimSun']],
  25. ['楷体',['楷体', '楷体_GB2312', 'SimKai']],
  26. ['黑体',['黑体', 'SimHei']],
  27. ['隶书',['隶书', 'SimLi']],
  28. ['andale mono',['andale mono']],
  29. ['arial',['arial', 'helvetica', 'sans-serif']],
  30. ['arial black',['arial black', 'avant garde']],
  31. ['comic sans ms',['comic sans ms']],
  32. ['impact',['impact', 'chicago']],
  33. ['times new roman',['times new roman']]
  34. ],
  35. 'fontsize':[10, 11, 12, 14, 16, 18, 20, 24, 36]
  36. });
  37. for ( var p in fonts ) {
  38. (function( cmd, style ) {
  39. UE.commands[cmd] = {
  40. execCommand : function( cmdName, value ) {
  41. value = value || (this.queryCommandState(cmdName) ? 'none' : cmdName == 'underline' ? 'underline' : 'line-through');
  42. var me = this,
  43. range = this.selection.getRange(),
  44. text;
  45. if ( value == 'default' ) {
  46. if(range.collapsed){
  47. text = me.document.createTextNode('font');
  48. range.insertNode(text).select()
  49. }
  50. me.execCommand( 'removeFormat', 'span,a', style);
  51. if(text){
  52. range.setStartBefore(text).setCursor();
  53. domUtils.remove(text)
  54. }
  55. } else {
  56. if(me.currentSelectedArr && me.currentSelectedArr.length > 0){
  57. for(var i=0,ci;ci=me.currentSelectedArr[i++];){
  58. range.selectNodeContents(ci);
  59. range.applyInlineStyle( 'span', {'style':style + ':' + value} );
  60. }
  61. range.selectNodeContents(this.currentSelectedArr[0]).select();
  62. }else{
  63. if ( !range.collapsed ) {
  64. if((cmd == 'underline'||cmd=='strikethrough') && me.queryCommandValue(cmd)){
  65. me.execCommand( 'removeFormat', 'span,a', style );
  66. }
  67. range = me.selection.getRange();
  68. range.applyInlineStyle( 'span', {'style':style + ':' + value} ).select();
  69. } else {
  70. var span = domUtils.findParentByTagName(range.startContainer,'span',true);
  71. text = me.document.createTextNode('font');
  72. if(span && !span.children.length && !span[browser.ie ? 'innerText':'textContent'].replace(fillCharReg,'').length){
  73. //for ie hack when enter
  74. range.insertNode(text);
  75. if(cmd == 'underline'||cmd=='strikethrough'){
  76. range.selectNode(text).select();
  77. me.execCommand( 'removeFormat','span,a', style, null );
  78. span = domUtils.findParentByTagName(text,'span',true);
  79. range.setStartBefore(text)
  80. }
  81. span.style.cssText = span.style.cssText + ';' + style + ':' + value;
  82. range.collapse(true).select();
  83. }else{
  84. range.insertNode(text);
  85. range.selectNode(text).select();
  86. span = range.document.createElement( 'span' );
  87. if(cmd == 'underline'||cmd=='strikethrough'){
  88. //a标签内的不处理跳过
  89. if(domUtils.findParentByTagName(text,'a',true)){
  90. range.setStartBefore(text).setCursor();
  91. domUtils.remove(text);
  92. return;
  93. }
  94. me.execCommand( 'removeFormat','span,a', style );
  95. }
  96. span.style.cssText = style + ':' + value;
  97. text.parentNode.insertBefore(span,text);
  98. //修复,span套span 但样式不继承的问题
  99. if(!browser.ie || browser.ie && browser.version == 9){
  100. var spanParent = span.parentNode;
  101. while(!domUtils.isBlockElm(spanParent)){
  102. if(spanParent.tagName == 'SPAN'){
  103. span.style.cssText = spanParent.style.cssText + span.style.cssText;
  104. }
  105. spanParent = spanParent.parentNode;
  106. }
  107. }
  108. range.setStart(span,0).setCursor();
  109. //trace:981
  110. //domUtils.mergToParent(span)
  111. }
  112. domUtils.remove(text)
  113. }
  114. }
  115. }
  116. return true;
  117. },
  118. queryCommandValue : function (cmdName) {
  119. var startNode = this.selection.getStart();
  120. //trace:946
  121. if(cmdName == 'underline'||cmdName=='strikethrough' ){
  122. var tmpNode = startNode,value;
  123. while(tmpNode && !domUtils.isBlockElm(tmpNode) && !domUtils.isBody(tmpNode)){
  124. if(tmpNode.nodeType == 1){
  125. value = domUtils.getComputedStyle( tmpNode, style );
  126. if(value != 'none'){
  127. return value;
  128. }
  129. }
  130. tmpNode = tmpNode.parentNode;
  131. }
  132. return 'none'
  133. }
  134. return domUtils.getComputedStyle( startNode, style );
  135. },
  136. queryCommandState : function(cmdName){
  137. if(this.highlight){
  138. return -1;
  139. }
  140. if(!(cmdName == 'underline'||cmdName=='strikethrough'))
  141. return 0;
  142. return this.queryCommandValue(cmdName) == (cmdName == 'underline' ? 'underline' : 'line-through')
  143. }
  144. }
  145. })( p, fonts[p] );
  146. }
  147. };