百度编辑器ueditor的部署和二次开发

介绍了UEditor 1.3.6版本的升级步骤及二次开发过程,包括配置文件路径、自定义功能的实现方法等。


升级:

1.从官网下载最新的1.3.6 jsp utf-8版 http://ueditor.baidu.com/website/download.html

2.借鉴同事(陶总)的思想,可以在baidu_editor下增加一个jsp页面,不妨取名include_all_for_struts_tag.jsp,引用编辑器在使用时所需的js


<script type="text/javascript" >
  var contextPath = "<%=request.getContextPath() %>";
 </script>
 
 
<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath() %>/baidu_editor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath() %>/baidu_editor/ueditor.all.js"> </script>
<script type="text/javascript" charset="utf-8" src="<%=request.getContextPath() %>/baidu_editor/lang/zh-cn/zh-cn.js"></script>


    <style type="text/css">
        .clear {
            clear: both;
        }
</style>


3.修改ueditor.config.js中的上传请求地址,将图片、文件的上传请求地址修改为servlet处理(改为java处理,修改存储路径时更为灵活)



之后,就可以编辑器就可以使用了,还有一些bug要处理:

a.针对“请等待保存目录就绪”的提示,可以将/dialogs/image/image.html进行修改!把上述image.html中的保存目录写死(saveDir标签)


b.对于发帖后的内容重复两遍的问题:

                  <%@include file="/baidu_editor/include_all_for_struts_tag.jsp" %>
                  <script type="text/plain" id="_postEdit"  name="detail"></script>
                  <script type="text/javascript" >
                var editor = UE.getEditor ( "_postEdit" );
                  </script >
                   <input type="hidden" name="detail" id="hiddenDetail"/>
需要把script中的name属性去掉






在web应用中的引用:

1.       baidu_editor放在WebRoot

2.       ueditor包放在src下(UeditorFileUpload.java/UeditorImageUpload.java/UeditorScrawlUpload.java/Uploader.java);

3.       把相关jar包放在WEB-INF/lib下(commons-fileupload-1.2.jar);

4.       修改一系列文件中的配置项:

修改web.xml ,配置baidu_editorservlet,处理上传文件和图片;

  
  <!--  百度编辑器文件上传 -->
  <servlet>
     <servlet-name>baiduFileUpload</servlet-name>
     <servlet-class>ueditor.UeditorFileUpload</servlet-class>
   </servlet>
  <servlet-mapping>
     <servlet-name>baiduFileUpload</servlet-name>
     <url-pattern>/baiduFileUpload</url-pattern>
  </servlet-mapping>
  
  <!--  百度编辑器图片上传 -->
   <servlet>
     <servlet-name>baiduImageUpload</servlet-name>
     <servlet-class>ueditor.UeditorImageUpload</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>baiduImageUpload</servlet-name>
     <url-pattern>/baiduImageUpload</url-pattern>
  </servlet-mapping>

    ② 可以通过在baidu_editor/ueditor.config.js中去掉toolbars中的一些项来屏蔽一些不用的功能;

       ③通过编辑器上传的文件的存储路径设置:

    在ueditor/Uploader.java中通过修改savePath属性值和getFolder私有方法即可灵活配置文件的保存路径。

5.       在页面上进行引用(见附件4_test_page):

<html>
<head>
    <title>demo</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <%@include file="/baidu_editor/include_all_for_struts_tag.jsp" %>

    <style type="text/css">
        .clear {
            clear: both;
        }
    </style>
</head>
<body>
<div>
    <h1>demo</h1>
    <script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
</div>


</body>
<script type="text/javascript">

    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例
    var editor = UE.getEditor('editor');


</script>
</html>


二次开发:

所谓编辑器的二次开发,可以理解为“如何在编辑器中新增一个自定义的功能”,下边以数学公式的开发为例来加以说明(ueditor版本为目前最新的1.3.6)

首先要认清其实质,其实就是在点击图标时触发打开了一个窗口,窗口中的内容为一个自定义的html

然后需要从以下几步去实现:

1.在~/ueditor.config.js中的toolbars数组中新增功能的定义‘math’

其中的“~”代表编辑器的家目录(如ueditor)

 

,toolbars:[
                   ['//fullscreen', 
                    'source', '|', 'undo', 'redo', '|',
                       'bold',  'underline', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch','autotypeset','blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist','selectall', 'cleardoc', '|',
                       'rowspacingtop', 'rowspacingbottom','lineheight','|',
                       'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
                       'directionalityltr', 'directionalityrtl', 'indent', '|',
                       'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|','touppercase','tolowercase','|',
                       'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright','imagecenter', '|',
                       'insertimage', 'emotion','math','attachment', 'map',  'insertframe','highlightcode','template', '|',
                       'horizontal', 'date', 'time', 'spechars','snapscreen', 'wordimage', '|',
                       'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', '|',
                       'print', 'preview', 'searchreplace','help']
               ]

2.在~/ueditor.all.js中设置弹窗的基本样式(有无确认按钮)以及相关联的html页面路径

 

①有无确认按钮的设置:

 var dialogBtns = {
        noOk:['searchreplace', 'help', 'spechars', 'webapp','preview','math','insertvideo','gmap','test'],
        ok:['attachment', 'anchor', 'link', 'insertimage', 'map',  'insertframe', 'wordimage',
             'insertframe', 'edittip', 'edittable', 'edittd', 'scrawl', 'template', 'background', 'charts','music']

    };


②相关联的html页面路径的设置:

把自定义的math.html页面放在相应的路径下(它所依赖的js、images、css等也可以放在math.html所在目录下)

    var iframeUrlMap = {
       
        'scrawl':'~/dialogs/scrawl/scrawl.html',
        'math':'~/dialogs/math/math.html',
        
        'charts': '~/dialogs/charts/charts.html'
    };


3.~/themes/default/css/ueditor.css中对功能的图标和功能对应的窗口大小进行设置

①功能的图标设置:

图标是在~/themes/default/images/icons.gif或icons.png中定义的。各个功能的图标可以是通过设置该图标在整个图标大图中左上方的偏移量来设置的,以math采用的图标为例,它在整图的偏移像素数。

  .edui-default  .edui-for-math .edui-icon {
    background-position: -18px -40px
}

②功能对应的窗口大小设置:

/*math-dialog*/
.edui-default .edui-for-math .edui-dialog-content {
    width: 830px;
    height: 360px;
}

4.在~/lang/zh-cn/zh-cn.js中可以配置dialog窗口的名称

新增  math:'数学公式'

'labelMap':{
        'anchor':'锚点','scrawl':'涂鸦','math':'数学公式'
    },

这样一来,刷新页面已经可以编辑器中看到新添的数学公式图标了,

 

点击后,就能看到830*360大小的弹窗了

 

剩下的工作就是个性化设计该弹窗页面了,附上ueditor常用的api

①关闭dialog窗口

var dialog = editor.getDialog("gmap");

dialog.close(); 

 

②插入一段html

editor.execCommand("inserthtml",html);

值得一提的是,页面需要引用~/dialogs/internal.js才能使用editor元素,不然会报对象找不到

 

更多接口可以在http://ueditor.baidu.com/doc/查询




UMeditor,简称UM,是为满足广大门户网站对于简单发帖框,或者回复框需求所定制的在线富文本编辑器。 UM的主要特点就是容量加载速度上的改变,全版本的代码量为125k,而且放弃了使用传统的iframe模式,采用了div的加载方式, 以达到更快的加载速度零加载失败率。现在UM的第一个使用者是百度贴吧,贴吧每天几亿的pv是对UM各种指标的最好测试平台。 当然随着代码的减少,UM的功能对于UE来说还是有所减少,但我们经过调研大家对于UM提出的各种意见,提供了现在UM的功能版本, 虽然有删减,但也有增加,比如拖拽图片上传,chrome的图片拖动改变大小等。让UM能在功能体积上达到一个平衡。我们的目标不仅是要提高在线编辑的 编辑体验,也希望能改变前端技术中关于富文本技术的门槛,让大家不再觉得这块是个大坑。1.2.2更新说明: ### 功能更新 1. **添加插入数学公式** 2. 支持插入动态地图 3. 支持复制图片、截屏图片的粘贴 4. 添加自动保存插件,支持草稿箱功能 5. 支持拖放图片上传并插入 6. 优化了对IE11的支持 7. 支持asp后台### 问题修复 1. 修复表情本地化出错问题 2. 修复jquery的$变量冲突问题 3. 修复多编辑器,弹层被遮挡出错问题 4. 解决于bootstrap的冲突 5. 修复粘贴后光标定位错误的问题 6. 修复一坨坨的细节BUG
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值