Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

[求助] 如何实现卡饭论坛纯文本查看 / 双击代码区域 Ctrl+A快速复制

[复制链接]
liesuen 发表于 2015-5-30 13:58:26 | 显示全部楼层 |阅读模式
本帖最后由 liesuen 于 2015-5-30 17:42 编辑

如题。代码很多的时候高度自定义,有下拉导航条





原地址http://bbs.kafan.cn/thread-1816123-1-1.html

上图:

点击纯文本查看,是一个弹窗,如图:


查看他网页源代码是这样的
  1. <div style="padding:15px 0;"><div style="font-size:12px;">[Asm] <em class="viewsource" style="cursor:pointer;font-size:12px;color:#369 !important;">纯文本查看</em> <em class="copycode" style="cursor:pointer;font-size:12px;color:#369 !important;">复制代码</em></div><pre class="brush: asm; gutter: true">
  2. </pre>
  3. 2
  4. 1
  5. 2
  6. 代码这里</div>
复制代码

还有一个JS代码如下:
  1. <script type="text/javascript">
  2. function mw_syntaxhighlighter_show_editor_codebox(editortype) {
  3. var tag = 'code';
  4. var mw_syntaxhighlighter_tag = 'mw_shl_code';
  5. var str = '请输入要插入的代码<br>选择语言:';
  6. str += '<select id="brush_lang" name="lang">';str += '<option value="asm">Asm</option>';
  7. str += '<option value="applescript">AppleScript</option>';
  8. str += '<option value="actionscript3">Actionscript3</option>';
  9. str += '<option value="bash">Bash shell</option>';
  10. str += '<option value="coldfusion">ColdFusion</option>';
  11. str += '<option value="c">C</option>';
  12. str += '<option value="cpp">C++</option>';
  13. str += '<option value="csharp">C#</option>';
  14. str += '<option value="css">CSS</option>';
  15. str += '<option value="delphi">Delphi</option>';
  16. str += '<option value="diff">Diff</option>';
  17. str += '<option value="erlang">Erlang</option>';
  18. str += '<option value="groovy">Groovy</option>';
  19. str += '<option value="html">HTML</option>';
  20. str += '<option value="java">Java</option>';
  21. str += '<option value="javafx">JavaFX</option>';
  22. str += '<option value="javascript">JavaScript</option>';
  23. str += '<option value="pascal">Pascal</option>';
  24. str += '<option value="patch">Patch</option>';
  25. str += '<option value="perl">Perl</option>';
  26. str += '<option value="php">PHP</option>';
  27. str += '<option value="text">Plain Text</option>';
  28. str += '<option value="powershell">PowerShell</option>';
  29. str += '<option value="python">Python</option>';
  30. str += '<option value="ruby">Ruby</option>';
  31. str += '<option value="rails">Ruby on Rails</option>';
  32. str += '<option value="sass">Sass</option>';
  33. str += '<option value="scala">Scala</option>';
  34. str += '<option value="scss">Scss</option>';
  35. str += '<option value="shell">Shell</option>';
  36. str += '<option value="sql">SQL</option>';
  37. str += '<option value="vb">Visual Basic</option>';
  38. str += '<option value="vbnet">Visual Basic .NET</option>';
  39. str += '<option value="xhtml">XHTML</option>';
  40. str += '<option value="xml">XML</option>';
  41. str += '<option value="xslt">XSLT</option>';
  42. str += '<option value="objc">Objective-C</option>';
  43. str += '<option value="aauto">AAuto</option>';
  44. str += '<option value="golang">Golang</option>';

  45. str += '</select><br>';

  46. str += '显示行号:';
  47. str += '<input type="checkbox" id="show_gutter" name="show_gutter" value="1" checked><br>';


  48. if(editortype == 'newthread') {

  49. var sel, selection;
  50. var str1 = '', strdialog = 0, stitle = '';
  51. var ctrlid = editorid + '_' + tag;
  52. var menu = $(ctrlid + '_menu');
  53. var pos = [0, 0];
  54. var menuwidth = 270;
  55. var menupos = '43!';
  56. var menutype = 'menu';

  57. str += '<div id="mw_codearea">你的代码:<br>';
  58. str += '<textarea id="' + ctrlid + '_param_1" style="width: 98%" cols="50" rows="5" class="txtarea"></textarea></div>';

  59. //note 修改了ie下的不兼容问题
  60. if(BROWSER.ie) {
  61. if(wysiwyg) {
  62. editdoc.body.focus();
  63. sel = editdoc.selection.createRange();
  64. } else {
  65. sel = document.selection.createRange();
  66. }
  67. pos = getCaret();
  68. }

  69. selection = sel ? (wysiwyg ? sel.text : sel.text) : mw_highlighter_getSel();//note 修改sel.HtmlText修改为Text,编辑器中的可视化用插件实现比较困难
  70. if(menu) {
  71. if($(ctrlid).getAttribute('menupos') !== null) {
  72. menupos = $(ctrlid).getAttribute('menupos');
  73. }
  74. if($(ctrlid).getAttribute('menuwidth') !== null) {
  75. menu.style.width = $(ctrlid).getAttribute('menuwidth') + 'px';
  76. }

  77. if(selection) {//note 如果选择了内容的处理
  78. $('mw_codearea').style.display = 'none';
  79. }

  80. showMenu({'ctrlid':ctrlid,'evt':'click','pos':menupos,'timeout':250,'duration':in_array(tag, ['fontname', 'fontsize', 'sml']) ? 2 : 3,'drag':1});
  81. } else {

  82. var menu = document.createElement('div');
  83. menu.id = ctrlid + '_menu';
  84. menu.style.display = 'none';
  85. menu.className = 'p_pof upf';
  86. menu.style.width = menuwidth + 'px';

  87. s = '<div class="p_opt cl"><span class="y" style="margin:-10px -10px 0 0"><a onclick="hideMenu();return false;" class="flbc" href="javascript:;">关闭</a></span><div>' + str + '</div><div class="pns mtn"><button type="submit" id="' + ctrlid + '_submit" class="pn pnc"><strong>提交</strong></button></div></div>';
  88. menu.innerHTML = s;
  89. $(editorid + '_editortoolbar').appendChild(menu);

  90. if(selection) {//note 如果选择了内容的处理
  91. $('mw_codearea').style.display = 'none';
  92. }

  93. showMenu({'ctrlid':ctrlid,'mtype':menutype,'evt':'click','duration':3,'cache':0,'drag':1,'pos':menupos});
  94. }

  95. try {
  96. if($(ctrlid + '_param_1')) {
  97. $(ctrlid + '_param_1').focus();
  98. }
  99. } catch(e) {}
  100. var objs = menu.getElementsByTagName('*');
  101. for(var i = 0; i < objs.length; i++) {
  102. _attachEvent(objs[i], 'keydown', function(e) {
  103. e = e ? e : event;
  104. obj = BROWSER.ie ? event.srcElement : e.target;
  105. if((obj.type == 'text' && e.keyCode == 13) || (obj.type == 'textarea' && e.ctrlKey && e.keyCode == 13)) {
  106. if($(ctrlid + '_submit') && tag != 'image') $(ctrlid + '_submit').click();
  107. doane(e);
  108. } else if(e.keyCode == 27) {
  109. hideMenu();
  110. doane(e);
  111. }
  112. });
  113. }
  114. if($(ctrlid + '_submit')) $(ctrlid + '_submit').onclick = function() {
  115. checkFocus();
  116. if(BROWSER.ie && wysiwyg) {
  117. setCaret(pos[0]);
  118. }
  119. if(wysiwyg) {
  120. if(!BROWSER.ie) {
  121. selection = selection ? selection : '';
  122. }
  123. }
  124. str1 = $(ctrlid + '_param_1') && $(ctrlid + '_param_1').value ? $(ctrlid + '_param_1').value : (selection ? selection : '');

  125. var opentag = '[' + mw_syntaxhighlighter_tag + '=' + $('brush_lang').value + ',' + (($('show_gutter') && $('show_gutter').checked) ? 'true' : 'false') + ']';
  126. var closetag = '[/' + mw_syntaxhighlighter_tag + ']';

  127. if(wysiwyg) {
  128. str1 = preg_replace(['<', '>'], ['<', '>'], str1);

  129. str1 = str1.replace(/\r?\n/g, '<br />');
  130. }
  131. str1 = opentag + str1 + closetag;
  132. insertText(str1, strlen(opentag), strlen(closetag), false, sel);
  133. hideMenu();
  134. };

  135. } else if(editortype == 'fastpost' || editortype == 'post') {

  136. var sel = false;
  137. var seditorkey = editortype;
  138. if(!isUndefined($(seditorkey + 'message').selectionStart)) {
  139. sel = $(seditorkey + 'message').selectionEnd - $(seditorkey + 'message').selectionStart;
  140. } else if(document.selection && document.selection.createRange) {
  141. $(seditorkey + 'message').focus();
  142. var sel = document.selection.createRange();
  143. $(seditorkey + 'message').sel = sel;
  144. sel = sel.text ? true : false;
  145. }

  146. var ctrlid = seditorkey + tag;
  147. var menuid = ctrlid + '_menu';

  148. str += '<div id="mw_codearea">你的代码:<br>';
  149. str += '<textarea id="' + ctrlid + '_param_1" style="width: 98%" cols="50" rows="5" class="txtarea"></textarea></div>';

  150. if(!$(menuid)) {
  151. var submitstr = "seditor_insertunit('" + seditorkey + "', '[" + mw_syntaxhighlighter_tag + "=' + $('brush_lang').value + ',' + (($('show_gutter') && $('show_gutter').checked) ? 'true' : 'false') + ']'+$('" + ctrlid + "_param_1').value, '[/" + mw_syntaxhighlighter_tag + "]', null, 1);hideMenu();";
  152. var menu = document.createElement('div');
  153. menu.id = menuid;
  154. menu.style.display = 'none';
  155. menu.className = 'p_pof upf';
  156. menu.style.width = '270px';
  157. $('append_parent').appendChild(menu);
  158. menu.innerHTML = '<span class="y"><a onclick="hideMenu()" class="flbc" href="javascript:;">关闭</a></span><div class="p_opt cl"><form onsubmit="' + submitstr + ';return false;" autocomplete="off"><div>' + str + '</div><div class="pns mtn"><button type="submit" id="' + ctrlid + '_submit" class="pn pnc"><strong>提交</strong></button><button type="button" onClick="hideMenu()" class="pn"><em>取消</em></button></div></form></div>';
  159. }
  160. if(sel) {//note 如果选择了内容的处理
  161. $('mw_codearea').style.display = 'none';
  162. }
  163. showMenu({'ctrlid':ctrlid,'evt':'click','duration':3,'cache':0,'drag':1});
  164. }
  165. }

  166. function mw_highlighter_getSel() {
  167. if(wysiwyg) {
  168. try {
  169. selection = editwin.getSelection();
  170. return selection.toString();
  171. //checkFocus();
  172. //range = selection ? selection.getRangeAt(0) : editdoc.createRange();
  173. //return readNodes(range.cloneContents(), false);
  174. } catch(e) {
  175. try {
  176. var range = editdoc.selection.createRange();
  177. if(range.htmlText && range.text) {
  178. return range.text;
  179. } else {
  180. var htmltext = '';
  181. for(var i = 0; i < range.length; i++) {
  182. htmltext += range.item(i).outerHTML;
  183. }
  184. return htmltext;
  185. }
  186. } catch(e) {
  187. return '';
  188. }
  189. }
  190. } else {
  191. if(!isUndefined(editdoc.selectionStart)) {
  192. return editdoc.value.substr(editdoc.selectionStart, editdoc.selectionEnd - editdoc.selectionStart);
  193. } else if(document.selection && document.selection.createRange) {
  194. return document.selection.createRange().text;
  195. } else if(window.getSelection) {
  196. alert(editdoc);
  197. return window.getSelection() + '';
  198. } else {
  199. return false;
  200. }
  201. }
  202. }
  203. </script>
复制代码

我的意思就是我们现在这样发的这么一段js代码很长 很多,暂用了网页很长一段,不利于用户体验,要是能乡他一样吧这段文字代码收缩起来就好多了




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-11-16 02:20 , Processed in 0.031397 second(s), 3 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表