Discuz教程网

[代码修改] Discuz X2 运行代码框(附:手改方法)

[复制链接]
authicon dly 发表于 2011-7-4 10:27:45 | 显示全部楼层 |阅读模式
这个运行代码框给需要的技术类网站使用,如果你用的比较少还是不要安装了
=========================================
新增加2个实现此功能的方案
后台添加discuz!自定义代码增加“运行代码框”功能 https://www.discuz.1314study.com/thread-16376-1-1.html
通用的,让网页支持“运行代码框”的操作设置!https://www.discuz.1314study.com/thread-16377-1-1.html
=========================================
贴内效果
运行代码框forDZ x2.0(附:手改方法)
演示地址http://www.173orz.com/t49-1-1.html
编辑器效果

贴内效果

154909amwrlaz0mprm6fuz_jpg_thumb.jpg


附件中的文件已经修改好了,如果你的dx2.0程序下面涉及的文件没有做过修改的话,可以直接覆盖使用!(最新官方0629补丁)

PS:如果你的风格使用的不是官方的默认风格,请务必按说明同时修改你的风格下的common/common.css、common/editor.css、forum/post_editor_body.htm这3个文件。
下面是修改方法,需要修改程序的多个文件,请自行备份以防修改失误后能及时恢复!
涉及修改文件列表:





source/function/function_discuzcode.php
static/js/seditor.js
static/js/editor.js
static/js/forum.js
template/default/common/common.css
template/default/common/editor.css
template/default/forum/post_editor_body.htm

手改方法看這裡吧

附件中的文件已经修改好了,如果你的dx2.0程序下面涉及的文件没有做过修改的话,可以直接覆盖使用!

附件下载:
游客,如果您要查看本帖隐藏内容请回复

PS:如果你的风格使用的不是官方的默认风格,请务必按说明同时修改你的风格下的common/common.css、common/editor.css、forum/post_editor_body.htm这3个文件。

下面是修改方法,需要修改程序的多个文件,请自行备份以防修改失误后能及时恢复!

涉及修改文件列表:

source/function/function_discuzcode.php
static/js/seditor.js
static/js/editor.js
static/js/forum.js
template/default/common/common.css
template/default/common/editor.css
template/default/forum/post_editor_body.htm

source/function/function_discuzcode.php 查找
  1. 'codehtml' => '',
复制代码

下面添加
  1. /*运行代码star*/
  2. 'phtmlcount' => -1,
  3. 'htmlcount' => 0,
  4. 'htmlhtml' => '',
  5. /*运行代码end*/
复制代码

继续查找
  1. return tpl_hide_credits_hidden($creditsrequire);
  2. }
  3. }
复制代码

下面添加
  1. /*运行代码star*/
  2. function htmldisp($code) {
  3. global $_G;
  4. $_G['forum_discuzcode']['phtmlcount']++;
  5. $code = dhtmlspecialchars(str_replace('\"', '"', preg_replace("/^[\n\r]*(.+?)[\n\r]*$/is", "\\1", $code)));
  6. $_G['forum_discuzcode']['htmlhtml'][$_G['forum_discuzcode']['phtmlcount']] = '<div class="runtxt"><textarea name="run'.$_G['forum_discuzcode'][htmlcount].'" id="run'.$_G['forum_discuzcode'][htmlcount].'" rows=12 cols=110>'.$code.'</textarea><br><input type="button" value="运行代码" onclick=runCode(run'.$_G['forum_discuzcode'][htmlcount].')> <input type="button" value="复制代码" onclick=copycode(run'.$_G['forum_discuzcode'][htmlcount].')> <input type="button" value="另存代码" onclick=saveCode(run'.$_G['forum_discuzcode'][htmlcount].')> <span>提示:您可以先修改部分代码再运行!<font color="red">目前暂时仅支持IE浏览器!</font></span></div><script language=JavaScript>ffcod=document.getElementById("run'.$_G['forum_discuzcode'][htmlcount].'").value;ffcod=ffcod.replace(/<br(.*)\/>/g,"");document.getElementById("run'.$_G['forum_discuzcode'][htmlcount].'").value=ffcod;</script>';
  7. $_G['forum_discuzcode']['htmlcount']++;
  8. return "[\tDISCUZ_HTML_".$_G['forum_discuzcode']['phtmlcount']."\t]";

  9. }
  10. /*运行代码end*/
复制代码

继续查找
  1. $message = preg_replace("/\s?\[code\](.+?)\[\/code\]\s?/ies", "codedisp('\\1')", $message);
  2. }
复制代码

下面添加
  1. /*运行代码star*/
  2. if($parsetype != 1 && !$bbcodeoff && $allowbbcode && (strpos($message, '[/html]') || strpos($message, '[/HTML]')) !== FALSE) {
  3. $message = preg_replace("/\s?\[html\](.+?)\[\/html\]\s?/ies", "htmldisp('\\1')", $message);
  4. }
  5. /*运行代码end*/
复制代码

继续查找
  1. for($i = 0; $i <= $_G['forum_discuzcode']['pcodecount']; $i++) {
  2. $message = str_replace("[\tDISCUZ_CODE_$i\t]", $_G['forum_discuzcode']['codehtml'][$i], $message);
  3. }
复制代码

下面添加
  1. /*运行代码star*/
  2. for($i = 0; $i <= $_G['forum_discuzcode']['phtmlcount']; $i++) {
  3. $message = str_replace("[\tDISCUZ_HTML_$i\t]", $_G['forum_discuzcode']['htmlhtml'][$i], $message);
  4. }
  5. /*运行代码end*/
复制代码

static/js/seditor.js查找
  1. var tagl = {'quote' : '请输入要插入的引用', 'code' : '请输入要插入的代码'};
复制代码

修改为
  1. var tagl = {'quote' : '请输入要插入的引用', 'code' : '请输入要插入的代码', 'html' : '请输入要插入的代码'};
复制代码

static/js/editor.js查找
  1. insertText(text, strlen(opentag), strlen(closetag), in_array(tagname, ['code', 'quote', 'free', 'hide']) ? true : false);
复制代码

修改为
  1. insertText(text, strlen(opentag), strlen(closetag), in_array(tagname, ['code', 'quote', 'html', 'free', 'hide']) ? true : false);
复制代码

继续查找
  1. if(in_array(cmd, ['sml', 'url', 'quote', 'code', 'free', 'hide', 'aud', 'vid', 'fls', 'attach', 'image', 'pasteword']) || cmd == 'tbl' || in_array(cmd, ['fontname', 'fontsize', 'forecolor', 'backcolor']) && !arg) {
复制代码

修改为
  1. if(in_array(cmd, ['sml', 'url', 'quote', 'code', 'html','free', 'hide', 'aud', 'vid', 'fls', 'attach', 'image', 'pasteword']) || cmd == 'tbl' || in_array(cmd, ['fontname', 'fontsize', 'forecolor', 'backcolor']) && !arg) {
复制代码

继续查找
  1. opentag = '<div class="blockcode"><blockquote>';
  2. closetag = '</blockquote></div><br />';
  3. }
复制代码

下面添加
  1. case 'html':
复制代码

继续查找
  1. var lang = {'quote' : '请输入要插入的引用', 'code' : '请输入要插入的代码', 'hide' : '请输入要隐藏的信息内容', 'free' : '如果您设置了帖子售价,请输入购买前免费可见的信息内容'};
复制代码

修改为
  1. var lang = {'quote' : '请输入要插入的引用', 'code' : '请输入要插入的代码', 'html' : '请输入要插入的代码', 'hide' : '请输入要隐藏的信息内容', 'free' : '如果您设置了帖子售价,请输入购买前免费可见的信息内容'};
复制代码

继续查找
  1. selection = selection ? selection : '\n';
  2. }
  3. }
复制代码

这里有2个相同的,在第一个的下面添加
  1. case 'html':
复制代码

static/js/forum.js最后添加
  1. function copyCode(obj) {
  2. var rng = document.body.createTextRange();
  3. rng.moveToElementText(obj);
  4. rng.scrollIntoView();
  5. rng.select();
  6. rng.execCommand("Copy");
  7. rng.collapse(false);
  8. }

  9. function runCode(obj)

  10. {

  11. var code=obj.value;

  12. var newwin=window.open('','','');

  13. newwin.opener = null;

  14. newwin.document.write(code);

  15. newwin.document.close();

  16. }

  17. function saveCode(obj) {

  18. var newwin = window.open('', '_blank', 'top=10000');

  19. newwin.document.open('text/html', 'replace');

  20. newwin.document.write(obj.value);

  21. newwin.document.execCommand('saveas','','code.htm');

  22. newwin.close();

  23. }
复制代码

template/default/common/common.css最后添加
  1. .runtxt{background:{COMMONBG};padding:10px;border:1px solid {COMMONBORDER};}
  2. .runtxt textarea{font-family:Courier New,Courier,monospace;border:1px solid #ccc;width:100%;font-size:12px;color:#333}
  3. .runtxt input{ margin-top:5px; border-left:1px solid {INPUTBORDER};border-right:1px solid {INPUTBORDERDARKCOLOR};border-top:1px solid {INPUTBORDER};border-bottom:1px solid {INPUTBORDERDARKCOLOR};background:{INPUTBG};color:{LIGHTLINK};}
  4. .runtxt input{border-color:{INPUTBORDER} {INPUTBORDERDARKCOLOR} {INPUTBORDERDARKCOLOR} {INPUTBORDER};background:{MENUTEXT};}
  5. .runtxt span{font-size:12px;color:#888;}
复制代码

template/default/common/editor.css查找
  1. #e_code { background-position: -123px -80px; }
  2. .b2r #e_code{ background-position: -120px -20px; }
复制代码

下面添加
  1. .b2r #e_html{ background:url(static/image/editor/html1.gif) no-repeat center center; }
复制代码

template/default/forum/post_editor_body.htm查找
  1. <div class="b2r{if $_G['cache']['bbcodes_display'][$_G['groupid']]} nbr{/if}" id="{$editorid}_adv_6">
  2. <p>
  3. <a id="{$editorid}_code" title="{lang e_code_title}">{lang e_code}</a>
复制代码

下面添加
  1. <a id="{$editorid}_html" title="添加运行的HTML代码">HTML</a>
复制代码

html1.gif
图标上传到static/image/editor/里




上一篇:Discuz X2 通用的,让网页支持“运行代码框”的操作设置!
下一篇:Discuz X1.5 教你设置边栏宽度
authicon 让测试飞起来 发表于 2011-7-6 02:20:40 | 显示全部楼层
这个不错,谢了
authicon test 发表于 2011-7-6 08:30:17 | 显示全部楼层
好像挺不错的。
authicon 龙盘凤逸 发表于 2011-7-28 19:18:50 | 显示全部楼层
支持!收下了。谢谢楼主的分享!
authicon cutiemimi 发表于 2011-8-9 15:47:18 | 显示全部楼层
楼主威武
authicon 3014527 发表于 2011-12-1 04:01:33 | 显示全部楼层
谢谢分享。辛苦了!!
authicon 悦芯舞 发表于 2012-2-13 11:24:33 | 显示全部楼层
谢谢分享。辛苦了!!
authicon mycdc 发表于 2014-10-12 11:13:39 | 显示全部楼层
找这个教程很久了!爱折腾,就是不知道是否支持 x 3.2 版本不!冒险尝试一下!
authicon luojunjin 发表于 2014-12-30 08:58:57 | 显示全部楼层
很        好的
authicon 1972260556 发表于 2015-11-1 14:04:53 | 显示全部楼层
啊万网顶顶顶顶顶顶顶顶
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2024-5-15 09:31

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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