`
hegz
  • 浏览: 439453 次
  • 性别: Icon_minigender_1
  • 来自: 茂名
社区版块
存档分类
最新评论

在xheditor编辑器的WYSWYG编辑模式下增加插入代码功能

阅读更多

xhEditor HTML编辑器为国人所开发,是一个基于jQuery开发的简单迷你并且高效的可视化XHTML编辑器,基于网络访问并且兼容IE 6.0+,Firefox 3.0+,Opera 9.6+,Chrome 1.0+,Safari 3.22+。
经过实际使用,取代FCKeditor/CKeditor或其它HTML编辑器是没有任何问题的。


 
xhEditor编辑器官方网站:http://xheditor.com/

Features(特点):

    * Small:初始加载4个文件,包括:1个js(50k)+2个css(10k)+1个图片(5k),总共 65k。若js和css文件进行gzip压缩传输,可以进一步缩减为24k左右。加上jQuery的24K,总共仅加载48K。
    * Fast:基于jQuery引擎开发,提供高效的代码执行效率。
    * Simple:简单的调用方式,加一个class属性就能将您的textarea立马变成一个功能丰富的可视化编辑器。
    * Upload:内置Ajax上传和HTML5上传支持(多文件上传、真实上传进度及文件拖放上传),追求完美的用户上传体验。
    * Word:实现Word代码自动检测并清理,提供高效完美的Word代码过滤方案,生成代码最优化精简,但是却不丢失任何细节效果。
    * UBB:提供完美的UBB可视化编辑解决方案,在您获得安全高效代码存储的同时,又能享受可视化编辑的便捷。

演示实例:

    * 默认模式:http://xheditor.com/demos/demo01.html
    * 自定义按钮:http://xheditor.com/demos/demo02.html
    * 皮肤选择:http://xheditor.com/demos/demo03.html
    * 其它选项:http://xheditor.com/demos/demo04.html
    * Javascript交互:http://xheditor.com/demos/demo05.html
    * 非utf-8 编码网页调用:http://xheditor.com/demos/demo06.html
    * UBB可视化编辑:http://xheditor.com/demos/demo07.html
    * Ajax文件上传:http://xheditor.com/demos/demo08.html
    * 插件扩展:http://xheditor.com/demos/demo09.html
    * iframe 调用文件上传:http://xheditor.com/demos/demo10.html
    * 异步加载:http://xheditor.com/demos/demo11.html

下载网址:http://code.google.com/p/xheditor/downloads/list

使用帮助:http://code.google.com/p/xheditor/wiki/Help

xhEidtor的最新版本为v1.0.0 RC3 (build 100506),功能及稳定性已能满足日常使用需要,但插入代码功能只在UBB编辑模式下提供,在所见即所得模式下却没有,令人觉得有些少遗憾及使用上的不便。

为了弥补遗憾,本人决定增加所见即所得编辑模式下的代码插入功能。其实现过程如下:

首先,下载xhEditor最新代码,解压到合适目录下。
然后在编辑器模板文件里增加下面代码。如下所示:

<head>
<!-- // 引入 jQuery 框架文件 -->
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="xheditor/xheditor-zh-cn.min.js"></script>

<style type="text/css">
<!--
/* 增加插入代码工具图标 */
.btnCode {
	background:transparent url(syntaxhighlighter/styles/code.gif) no-repeat 20px 20px;
	background-position:3px 3px;
}
-->
</style>

<script type="text/javascript">
	$(function() {

        var ssid = $("#ssid").val();

        // 创建xhEditor编辑器
        /*
		// v1.0.0 RC2以前版本使用
		var tools = "GStart,Cut,Copy,Paste,Pastetext,GEnd,Separator,";
			tools += "GStart,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,Removeformat,GEnd,Separator,";
			tools += "GStart,Align,List,Outdent,Indent,GEnd,Separator,";
			tools += "GStart,Link,Unlink,Img,Table,Code,GEnd,Separator,";
			tools += "GStart,Source,Fullscreen,About,GEnd";
		*/
		// v1.0.0 RC3版本使用
        var tools =  "Cut,Copy,Paste,Pastetext,Separator,";
			tools += "Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,SelectAll,Removeformat,Separator,";
			tools += "Align,List,Outdent,Indent,Separator,";
			tools += "Link,Unlink,Img,Table,Code,Separator,";
			tools += "Source,Fullscreen,About";
		$("#editor").xheditor({
			tools:tools,
			width:'100%',
			height:'400',
			skin:'default',     //default(默认风格),o2007blue(Office 2007 蓝色),o2007silver(Office 2007 银色)
			//forcePtag:false,      //强制P标签(true)
			hoverExecDelay:3000,
			layerShadow:2,
			upLinkUrl:"{{ url controller='Article' action='FileUpload' }}" + '&ssid=' + ssid,
			upLinkExt:'zip,rar,txt,doc,xls,chm,pdf',
			upImgUrl:"{{ url controller='Article' action='ImgUpload' }}" + '&ssid=' + ssid,
			//emots:{qq:{name:'QQ',count:55,width:25,height:25,line:11},msn:{name:'MSN',count:40,width:22,height:22,line:8}}  //自定义表情
            plugins:{
                Code:{
                    c:'btnCode',t:'插入代码',h:1,e:function(){
                        var _this=this;
                        var htmlCode='<div><select id="xheCodeType"><option value="html">HTML/XML</option><option value="javascript">JavaScript</option><option value="css">CSS</option><option value="php">PHP</option><option value="csharp">C#</option><option value="cpp">C++</option><option value="java">Java</option><option value="perl">Perl</option><option value="python">Python</option><option value="ruby">Ruby</option><option value="vb">Visual Basic</option><option value="delphi">Delphi</option><option value="as3">Action Script 3</option><option value="sql">SQL</option><option value="plain">其它</option></select></div><div><textarea id="xheCodeValue" wrap="soft" spellcheck="false" style="width:300px;height:100px;" /></div><div style="text-align:right;"><input type="button" id="xheSave" value="确定" /></div>';
                        var jCode=$(htmlCode),jType=$('#xheCodeType',jCode),jValue=$('#xheCodeValue',jCode),jSave=$('#xheSave',jCode);
                        jSave.click(function(){
                            _this.loadBookmark();
                            _this.pasteText('<pre class="brush: '+jType.val()+';">\r\n'+jValue.val()+'\r\n</pre>');
                            _this.hidePanel();
                            return false;	
                        });
                        _this.showDialog(jCode);
                    }
                }
            }
		});
	});
</script>
</head>

<body>
......
<textarea name="editor" id="editor" />{{$art.content}}</textarea>
......
</body>
 

xhEditor编辑界面效果图:


 
其次,后台保存数据时,预先将数据预处理,以确保“<pre>”不被编码成“&lt;pre&gt;”。否则,无法正确显示高亮代码。代码如下:

// 确保“<pre>”不被编码成“&lt;pre&gt;”
$data['content'] = html_entity_decode($_POST['editor']); 
 

 最后,下载syntaxhighlighter代码高亮处理代码(下载地址:http://alexgorbatchev.com/wiki/SyntaxHighlighter:Download ),解压缩到合适目录下。
然后在代码浏览页面模板上增加下面代码。如下所示:

<head>
<!-- // 引入 jQuery 框架文件 -->
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>

<script type="text/javascript" src="syntaxhighlighter/scripts/shCore.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushBash.js"></script>
<!--
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushCpp.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushDelphi.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushDiff.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushGroovy.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushJava.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushRuby.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushVb.js"></script>
-->
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushCss.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushCSharp.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushJScript.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushPhp.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushPlain.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushPython.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushScala.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushSql.js"></script>	
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushXml.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushPerl.js"></script>
<link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shCore.css"/>
<link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shThemeDefault.css"/>
<script type="text/javascript">
$(function(){
    SyntaxHighlighter.config.clipboardSwf = '../libs/syntaxhighlighter/scripts/clipboard.swf';
    SyntaxHighlighter.config.strings = {
        expandSource: '展开代码',
        viewSource: '查看代码',
        copyToClipboard: '复制代码',
        copyToClipboardConfirmation: '代码复制成功',
        print: '打印',
        help: '帮助',
        //alert: '语法高亮\n\n',
        noBrush: '没有找到刷子:',
        brushNotHtmlScript: '没有为html-script选项配置刷子:',
        aboutDialog: '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>About SyntaxHighlighter</title></head><body style="font-family:Geneva,Arial,Helvetica,sans-serif;background-color:#fff;color:#000;font-size:1em;text-align:center;"><div style="text-align:center;margin-top:3em;"><div style="font-size:xx-large;">SyntaxHighlighter</div><div style="font-size:.75em;margin-bottom:4em;"><div>version 2.1.364 (October 15 2009)</div><div><a href="http://alexgorbatchev.com" target="_blank" style="color:#0099FF;text-decoration:none;">http://alexgorbatchev.com</a></div><div>If you like this script, please <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=2930402" style="color:#0099FF;text-decoration:none;">donate</a> to keep development active!</div></div><div>JavaScript code syntax highlighter.</div><div>Copyright 2004-2009 Alex Gorbatchev.</div></div></body></html>'
    }
    //SyntaxHighlighter.config.tagName = 'pre';
    SyntaxHighlighter.config.bloggerMode = true;  // 去掉代码行尾的<br>标识,并准确换行。
    //SyntaxHighlighter.config.stripBrs = false;
    SyntaxHighlighter.all();
});
</script>
</head>
 

代码高亮效果图:

 

论坛帖:http://qeephp.com/bbs/viewthread.php?tid=10990&page=1&extra=#pid56781

 

 

 

  • 大小: 11 KB
  • 大小: 18.6 KB
  • 大小: 4.3 KB
  • Article.zip (4.8 KB)
  • 描述: 控制器文件
  • 下载次数: 28
  • artview.zip (1.5 KB)
  • 描述: 模板文件
  • 下载次数: 27
1
1
分享到:
评论

相关推荐

    xhEditor实现插入代码功能

    《xhEditor实现插入代码功能详解》 在Web开发中,编辑器是不可或缺的一部分,它为用户提供了方便的文本输入和格式化功能。xhEditor是一款功能强大的富文本编辑器,广泛应用于各种在线编辑场景。本文将深入探讨如何...

    xheditor文本编辑器插件制作html编辑器使用代码

    初始化xheditor编辑器通常在文档加载完成后进行,可以使用JavaScript的`$(document).ready()`或`window.onload`事件。例如: ```javascript $(document).ready(function(){ $("#myEditor").xheditor({tools:'...

    用于xheditor编辑器上传功能的配置文件

    用于xheditor编辑器上传功能的配置文件 upload.aspx,直接下载拷贝到项目根目录下即可。 里面的该行: string attachdir ="../upload"; 是上传路径,根据实际引用xheditor的页面位置,要进行相应的修改,要不然无法...

    xheditor 在线编辑器

    例如,引入`xheditor-1.1.4`目录下的`xheditor.css`和`xheditor.js`,然后在元素上设置`class="xheditor"`或`id="xheditor"`即可初始化编辑器。 3. **配置与扩展** XHEditor支持通过JavaScript进行配置,例如设定...

    xhEditor在线编辑器完整实例

    它包含了xhEditor的核心功能,如文本格式化、图片上传、链接插入等,开发者可以通过它深入理解编辑器的工作原理并进行自定义扩展。 xhEditor的特点包括但不限于: - **多语言支持**:xhEditor支持多种语言,便于...

    xheditor编辑器asp.net源码

    《深入解析xheditor编辑器在ASP.NET中的应用》 xheditor是一款功能强大的富文本编辑器,广泛应用于网页内容的编辑与发布。它提供了丰富的编辑功能,包括文本格式化、图片上传、表情插入等,极大地提升了用户在网页...

    xheditor编辑器实现图片上传

    在IT行业中,富文本编辑器是网页开发中的一个重要组成部分,它们允许用户以类似Word的方式编辑内容,同时可以插入图片、链接等元素。XHEditor是一款基于JavaScript的开源富文本编辑器,它轻量级且功能强大,适用于...

    xhEditor编辑器V1.17示例webform

    《xhEditor编辑器V1.17在Web表单中的应用详解》 xhEditor是一款功能强大的富文本编辑器,适用于Web开发中对文本内容的编辑与展示。其版本V1.17提供了丰富的示例,帮助开发者更好地理解和运用这款编辑器。本文将深入...

    最好最轻便的编辑器xheditor简易配置实例

    在需要展示编辑器的地方,使用JavaScript代码进行初始化。以下是一个简单的例子: ```html &lt;textarea id="myEditor"&gt;&lt;/textarea&gt; $('#myEditor').xheditor({tools: 'full'}); ``` 这里,`tools: 'full'`...

    可以插入本地图片的在线编辑器(xheditor)

    【标题】"可以插入本地图片的在线编辑器(xheditor)" 描述了一款名为 xheditor 的在线文本编辑工具,它具有独特的功能,即允许用户上传并插入本地图片到编辑内容中。这一特性在许多网站内容管理系统、论坛或者博客...

    xheditor 文本编辑器插件

    总结,xheditor作为一款老牌的JavaScript文本编辑器插件,以其丰富的功能和易用性在Web开发中占据一席之地。尽管如今有更多新的编辑器出现,但xheditor的历史地位和对Web开发者的贡献不容忽视。

    xheditor编辑器

    在这个版本中,1.2.1代表了编辑器的版本号,通常新版本会修复已知问题并增加新的功能。 在文件列表中,"说明.htm"是一个非常重要的文档,它是XHEditor的使用指南和API参考。通过阅读这个文件,我们可以了解到如何...

    xheditor-1.1.7 编辑器插件

    xheditor-1.1.7 编辑器插件 包含xheditor所含关键文件及jquery,导入web工程即可使用

    xheditor 编辑器

    xheditor通过提供丰富的文本格式化选项、图片上传、链接插入等功能,极大地提升了用户的编辑体验。 **1. 功能特性** - **富文本编辑**: 支持字体、字号、颜色、背景色、对齐方式、段落样式等基本文本格式化操作。 ...

    xhEditor编辑器V0.97示例webform.rar

    在xhEditor V0.97版本中,其丰富的功能和易用性使其成为许多网站和应用程序的首选编辑器。本文将深入探讨xhEditor V0.97在Web表单中的应用,以及如何通过"WebFormExample"这个示例来理解和实践。 一、xhEditor简介 ...

    在xheditor在线编辑器下,ASP提交远程图片自动上传到服务器

    本话题主要关注如何在XHEditor在线编辑器下,通过ASP(Active Server Pages)实现远程图片的自动上传到服务器的功能。 首先,XHEditor是一款强大的JavaScript富文本编辑器,它提供了丰富的编辑功能,如文字格式化、...

Global site tag (gtag.js) - Google Analytics