UEditor添加一个普通按钮

添加一个名叫“hougelou”的普通按钮

附言:以下是以UEditor .Net版本举例说明。

第一步:找到ueditor.config.js文件中的toolbars数组,增加一个“hougelou”字符串,然后找到labelMap数组,对应着添加一个labelMap,用于鼠标移上按钮时的提示。

//工具栏上的所有的功能按钮和下拉框,可以在new编辑器的实例时选择自己需要的从新定义
        , toolbars:[
            ['fullscreen', 'source', '|', 'undo', 'redo', '|',
                'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'hougelou', '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', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe','insertcode', 'webapp', 'pagebreak', 'template', 'background', '|',
                'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
                'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', '|',
                'print', 'preview', 'searchreplace', 'help']
        ]
        //当鼠标放在工具栏上时显示的tooltip提示,留空支持自动多语言配置,否则以配置值为准
        ,labelMap:{
        'hougelou': 'hello,后阁楼'
        }

第二步:找到你所引用的ueditor.all.js文件中的btnCmds数组,在其中同样增加一个“hougelou”字符串。

第三步:清空缓存刷新下页面吧!工具栏的对应位置是否出现了一个自己定义的按钮呢?如下图所示:

由于此时未设置对应按钮的图片样式,所以会显示默认的“B”字符。要想让其显示成自己需要的图标样式,接着按照下面的步骤动手吧。

第四步:找到themes/default/css/ueditor.css文件,增加一条样式定义:

.edui-for-hougelou .edui-icon {
    background-position: -700px -40px;
}

此处的样式定义了showmsg图标在UEditor默认的精灵Icon图片(themes/default/images/icons.png)中的位置偏移。如需更改成另外图标,只需添加图标到该图片文件中,然后设置偏移值即可。

第五步:到此为止,在UI层面已经完成了一个工具栏图标的显示和各种状态变化的逻辑,但是我们发现点击按钮之后毫无反应。那是必然的,我们还必须为该按钮绑定属于它自己的事件处理方法。

实质上,此时一个默认的事件处理方法已经被UEditor绑定到按钮上了,只不过由于我们还没有定义该方法的具体内容,所以点击之后无任何变化。

下面我们就来定义该方法的具体内容:

在初始化编辑器的时候,加上自己的事件处理(插入一张图片),如下代码:

ueditor = UE.getEditor('txtContent', {
            "initialFrameHeight": "200",
            toolbars: [['fullscreen', 'source', 'hougelou', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|', 'indent', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|', 'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|', 'insertimage', 'emotion', 'insertvideo', 'music', 'insertcode', 'background', '|', 'horizontal', 'date', 'time', 'spechars', '|', 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', '|', 'preview', 'searchreplace', 'help']],
            enterTag: " "
        }); //回车的时候用换行不用段落标签
        //实现插件的功能代码
        baidu.editor.commands['hougelou'] = { execCommand: function() { this.execCommand('insertHtml', "<img src='http://www.hougelou.com/images/logo.png' />"); return true; }, queryCommandState: function() { } };

 



End

 

 

 

 

 -------------------------------------------------------------------------------

 在images.ashx里发现百度编辑器返回图片路径是用分隔符“ue_separate_ue”连起来的。

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
对于此功能,需要使用ueditor提供的自定义插件和事件来实现。 1. 编写自定义插件 在ueditor的plugins目录下新建一个文件夹,比如命名为myplugin,然后在该文件夹中新建一个js文件,比如命名为myplugin.js。在myplugin.js中编写插件的代码,具体实现方式可以参考ueditor官方文档中的插件开发部分。 2. 注册插件 在ueditor的配置文件中,添加以下代码,将myplugin插件注册到ueditor中: ```javascript UEDITOR_CONFIG.toolbars = [ ['myplugin'] ]; UEDITOR_CONFIG.UEDITOR_HOME_URL = '/static/ueditor/'; //ueditor的根目录,需要根据实际情况修改 UEDITOR_CONFIG.serverUrl = '/upload/'; //ueditor上传文件的后端接口,需要根据实际情况修改 UEDITOR_CONFIG.autoHeightEnabled = true; //自适应高度 UEDITOR_CONFIG.maximumWords = 100000; //最大允许输入的字数 UEDITOR_CONFIG.wordCountMsg = '已输入{#count}个字符,还可以输入{#leave}个字符'; //输入字数提示 UEDITOR_CONFIG.wordOverFlowMsg = '已超出{#overflow}个字符'; //超出字数提示 UEDITOR_CONFIG.elementPathEnabled = false; //禁用元素路径 UEDITOR_CONFIG.toolbarTopOffset = 30; //顶部工具栏距离顶部的距离 UEDITOR_CONFIG.plugins = ['myplugin']; //注册插件 ``` 3. 绑定事件 在页面中,使用以下代码绑定点击事件: ```javascript UE.getEditor('editor').addListener('click', function () { var range = this.selection.getRange(); var text = range.getText(); if (/关键字/.test(text)) { //判断是否包含关键字 var keywords = ['关键字1', '关键字2', '关键字3']; //下拉框中的关键字列表 var html = ''; for (var i = 0; i < keywords.length; i++) { html += '<li>' + keywords[i] + '</li>'; } var $menu = $('<ul class="menu">' + html + '</ul>'); //创建下拉框 $menu.css({ position: 'absolute', top: range.startContainer.offsetTop + range.endContainer.offsetTop + range.endOffset * 20 + 10, left: range.startContainer.offsetLeft + (range.endContainer.offsetLeft - range.startContainer.offsetLeft) * range.startOffset / range.startContainer.innerText.length }); $menu.appendTo('body'); $menu.find('li').click(function () { range.selectNodeContents(range.startContainer); range.deleteContents(); range.insertNode(document.createTextNode($(this).text())); $menu.remove(); }); $(document).click(function (e) { if ($(e.target).closest('.menu').length === 0) { $menu.remove(); } }); } }); ``` 以上代码中,需要将editor替换成实际的编辑器id。关键字列表和下拉框的样式可以根据实际情况进行修改。代码中的注释已经说明了各个部分的功能。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值