highlight.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ///import core
  2. ///import plugins/inserthtml.js
  3. ///commands 插入代码
  4. ///commandsName HighlightCode
  5. ///commandsTitle 插入代码
  6. ///commandsDialog dialogs\code\code.html
  7. UE.plugins['highlight'] = function() {
  8. var me = this;
  9. if(!/highlightcode/i.test(me.options.toolbars.join('')))return;
  10. me.commands['highlightcode'] = {
  11. execCommand: function (cmdName, code, syntax) {
  12. if(code && syntax){
  13. var pre = document.createElement("pre");
  14. pre.className = "brush: "+syntax+";toolbar:false;";
  15. pre.style.display = "";
  16. pre.appendChild(document.createTextNode(code));
  17. document.body.appendChild(pre);
  18. if(me.queryCommandState("highlightcode")){
  19. me.execCommand("highlightcode");
  20. }
  21. me.execCommand('inserthtml', SyntaxHighlighter.highlight(pre,null,true),true);
  22. var div = me.document.getElementById(SyntaxHighlighter.getHighlighterDivId());
  23. div.setAttribute('highlighter',pre.className);
  24. domUtils.remove(pre);
  25. adjustHeight()
  26. }else{
  27. var range = this.selection.getRange(),
  28. start = domUtils.findParentByTagName(range.startContainer, 'table', true),
  29. end = domUtils.findParentByTagName(range.endContainer, 'table', true),
  30. codediv;
  31. if(start && end && start === end && start.parentNode.className.indexOf("syntaxhighlighter")>-1){
  32. codediv = start.parentNode;
  33. if(domUtils.isBody(codediv.parentNode)){
  34. var p = me.document.createElement('p');
  35. p.innerHTML = browser.ie ? '' : '<br/>';
  36. me.body.insertBefore(p,codediv);
  37. range.setStart(p,0)
  38. }else{
  39. range.setStartBefore(codediv)
  40. }
  41. range.setCursor();
  42. domUtils.remove(codediv);
  43. }
  44. }
  45. },
  46. queryCommandState: function(){
  47. var range = this.selection.getRange(),start,end;
  48. range.adjustmentBoundary();
  49. start = domUtils.findParent(range.startContainer,function(node){
  50. return node.nodeType == 1 && node.tagName == 'DIV' && domUtils.hasClass(node,'syntaxhighlighter')
  51. },true);
  52. end = domUtils.findParent(range.endContainer,function(node){
  53. return node.nodeType == 1 && node.tagName == 'DIV' && domUtils.hasClass(node,'syntaxhighlighter')
  54. },true);
  55. return start && end && start == end ? 1 : 0;
  56. }
  57. };
  58. me.addListener('beforeselectionchange',function(){
  59. me.highlight = me.queryCommandState('highlightcode');
  60. });
  61. me.addListener('afterselectionchange',function(){
  62. me.highlight = 0;
  63. });
  64. me.addListener("ready",function(){
  65. //避免重复加载高亮文件
  66. if(typeof XRegExp == "undefined"){
  67. var obj = {
  68. id : "syntaxhighlighter_js",
  69. src : me.options.highlightJsUrl || me.options.UEDITOR_HOME_URL + "third-party/SyntaxHighlighter/shCore.js",
  70. tag : "script",
  71. type : "text/javascript",
  72. defer : "defer"
  73. };
  74. utils.loadFile(document,obj,function(){
  75. changePre();
  76. });
  77. }
  78. if(!me.document.getElementById("syntaxhighlighter_css")){
  79. var obj = {
  80. id : "syntaxhighlighter_css",
  81. tag : "link",
  82. rel : "stylesheet",
  83. type : "text/css",
  84. href : me.options.highlightCssUrl ||me.options.UEDITOR_HOME_URL + "third-party/SyntaxHighlighter/shCoreDefault.css"
  85. };
  86. utils.loadFile(me.document,obj);
  87. }
  88. });
  89. me.addListener("beforegetcontent",function(type,cmd){
  90. for(var i=0,di,divs=domUtils.getElementsByTagName(me.body,'div');di=divs[i++];){
  91. if(di.className == 'container'){
  92. var pN = di.parentNode;
  93. while(pN){
  94. if(pN.tagName == 'DIV' && /highlighter/.test(pN.id)){
  95. break;
  96. }
  97. pN = pN.parentNode;
  98. }
  99. if(!pN)return;
  100. var pre = me.document.createElement('pre');
  101. for(var str=[],c=0,ci;ci=di.childNodes[c++];){
  102. str.push(ci[browser.ie?'innerText':'textContent']);
  103. }
  104. pre.appendChild(me.document.createTextNode(str.join('\n')));
  105. pre.className = pN.getAttribute('highlighter');
  106. pN.parentNode.insertBefore(pre,pN);
  107. domUtils.remove(pN);
  108. }
  109. }
  110. });
  111. me.addListener("aftergetcontent",function(type,cmd){
  112. changePre();
  113. });
  114. function adjustHeight(){
  115. setTimeout(function(){
  116. var div = me.document.getElementById(SyntaxHighlighter.getHighlighterDivId());
  117. if(div){
  118. var tds = div.getElementsByTagName('td');
  119. for(var i=0,li,ri;li=tds[0].childNodes[i];i++){
  120. ri = tds[1].firstChild.childNodes[i];
  121. //trace:1949
  122. if(ri){
  123. ri.style.height = li.style.height = ri.offsetHeight + 'px';
  124. }
  125. }
  126. }
  127. });
  128. }
  129. function changePre(){
  130. for(var i=0,pr,pres = domUtils.getElementsByTagName(me.document,"pre");pr=pres[i++];){
  131. if(pr.className.indexOf("brush")>-1){
  132. var pre = document.createElement("pre"),txt,div;
  133. pre.className = pr.className;
  134. pre.style.display = "none";
  135. pre.appendChild(document.createTextNode(pr[browser.ie?'innerText':'textContent']));
  136. document.body.appendChild(pre);
  137. try{
  138. txt = SyntaxHighlighter.highlight(pre,null,true);
  139. }catch(e){
  140. domUtils.remove(pre);
  141. return ;
  142. }
  143. div = me.document.createElement("div");
  144. div.innerHTML = txt;
  145. div.firstChild.setAttribute('highlighter',pre.className);
  146. pr.parentNode.insertBefore(div.firstChild,pr);
  147. domUtils.remove(pre);
  148. domUtils.remove(pr);
  149. adjustHeight()
  150. }
  151. }
  152. }
  153. me.addListener("aftersetcontent",function(){
  154. changePre();
  155. });
  156. //全屏时,重新算一下宽度
  157. me.addListener('fullscreenchanged',function(){
  158. var div = domUtils.getElementsByTagName(me.document,'div');
  159. for(var j=0,di;di=div[j++];){
  160. if(/^highlighter/.test(di.id)){
  161. var tds = di.getElementsByTagName('td');
  162. for(var i=0,li,ri;li=tds[0].childNodes[i];i++){
  163. ri = tds[1].firstChild.childNodes[i];
  164. ri.style.height = li.style.height = ri.offsetHeight + 'px';
  165. }
  166. }
  167. }
  168. })
  169. };