font.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. { name:'songti',val:'宋体,SimSun'},
  25. { name:'yahei',val:'微软雅黑,Microsoft YaHei'},
  26. { name:'kaiti',val:'楷体,楷体_GB2312, SimKai'},
  27. { name:'heiti',val:'黑体, SimHei'},
  28. { name:'lishu',val:'隶书, SimLi'},
  29. { name:'andaleMono',val:'andale mono'},
  30. { name:'arial',val:'arial, helvetica,sans-serif'},
  31. { name:'arialBlack',val:'arial black,avant garde'},
  32. { name:'comicSansMs',val:'comic sans ms'},
  33. { name:'impact',val:'impact,chicago'},
  34. { name:'timesNewRoman',val:'times new roman'}
  35. ],
  36. 'fontsize':[10, 11, 12, 14, 16, 18, 20, 24, 36]
  37. });
  38. for ( var p in fonts ) {
  39. (function( cmd, style ) {
  40. UE.commands[cmd] = {
  41. execCommand : function( cmdName, value ) {
  42. value = value || (this.queryCommandState(cmdName) ? 'none' : cmdName == 'underline' ? 'underline' : 'line-through');
  43. var me = this,
  44. range = this.selection.getRange(),
  45. text;
  46. if ( value == 'default' ) {
  47. if(range.collapsed){
  48. text = me.document.createTextNode('font');
  49. range.insertNode(text).select();
  50. }
  51. me.execCommand( 'removeFormat', 'span,a', style);
  52. if(text){
  53. range.setStartBefore(text).setCursor();
  54. domUtils.remove(text);
  55. }
  56. } else {
  57. if ( !range.collapsed ) {
  58. if((cmd == 'underline'||cmd=='strikethrough') && me.queryCommandValue(cmd)){
  59. me.execCommand( 'removeFormat', 'span,a', style );
  60. }
  61. range = me.selection.getRange();
  62. range.applyInlineStyle( 'span', {'style':style + ':' + value} ).select();
  63. } else {
  64. var span = domUtils.findParentByTagName(range.startContainer,'span',true);
  65. text = me.document.createTextNode('font');
  66. if(span && !span.children.length && !span[browser.ie ? 'innerText':'textContent'].replace(fillCharReg,'').length){
  67. //for ie hack when enter
  68. range.insertNode(text);
  69. if(cmd == 'underline'||cmd=='strikethrough'){
  70. range.selectNode(text).select();
  71. me.execCommand( 'removeFormat','span,a', style, null );
  72. span = domUtils.findParentByTagName(text,'span',true);
  73. range.setStartBefore(text);
  74. }
  75. span.style.cssText += ';' + style + ':' + value;
  76. range.collapse(true).select();
  77. }else{
  78. range.insertNode(text);
  79. range.selectNode(text).select();
  80. span = range.document.createElement( 'span' );
  81. if(cmd == 'underline'||cmd=='strikethrough'){
  82. //a标签内的不处理跳过
  83. if(domUtils.findParentByTagName(text,'a',true)){
  84. range.setStartBefore(text).setCursor();
  85. domUtils.remove(text);
  86. return;
  87. }
  88. me.execCommand( 'removeFormat','span,a', style );
  89. }
  90. span.style.cssText = style + ':' + value;
  91. text.parentNode.insertBefore(span,text);
  92. //修复,span套span 但样式不继承的问题
  93. if(!browser.ie || browser.ie && browser.version == 9){
  94. var spanParent = span.parentNode;
  95. while(!domUtils.isBlockElm(spanParent)){
  96. if(spanParent.tagName == 'SPAN'){
  97. //opera合并style不会加入";"
  98. span.style.cssText = spanParent.style.cssText + ";" + span.style.cssText;
  99. }
  100. spanParent = spanParent.parentNode;
  101. }
  102. }
  103. if(opera){
  104. setTimeout(function(){
  105. range.setStart(span,0).setCursor();
  106. });
  107. }else{
  108. range.setStart(span,0).setCursor();
  109. }
  110. //trace:981
  111. //domUtils.mergeToParent(span)
  112. }
  113. domUtils.remove(text);
  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(!(cmdName == 'underline'||cmdName=='strikethrough')){
  138. return 0;
  139. }
  140. return this.queryCommandValue(cmdName) == (cmdName == 'underline' ? 'underline' : 'line-through');
  141. }
  142. };
  143. })( p, fonts[p] );
  144. }
  145. };