UEditor百度編輯器,工具欄自定義添加一個普通按鈕


 

根據網上前輩提供的,還真的不錯,下面也整理一下

 

添加一個名叫“macros”的普通按鈕在工具欄上:

第一步:找到ueditor.config.js文件中的toolbars數組,增加一個“macros”字符串,

              然后找到labelMap數組,對應着添加一個labelMap,用於鼠標移上按鈕時的提示。

//工具欄上的所有的功能按鈕和下拉框,可以在new編輯器的實例時選擇自己需要的從新定義
        , toolbars:[
            ['fullscreen', 'source', '|', 'undo', 'redo', '|',
                'bold', 'italic', 'underline', 'fontborder', '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', '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', 'macros',
] ] 

//當鼠標放在工具欄上時顯示的tooltip提示,留空支持自動多語言配置,否則以配置值為准
,labelMap:{ 'macros': 'hello' }

第二步:找到你所引用的ueditor.all.js文件中的btnCmds數組,在其中同樣增加一個“macros”字符串。

第三步:清空緩存或刷新,工具欄的對應位置是否出現了一個自己定義的按鈕呢

由於此時未設置對應按鈕的圖片樣式,所以會顯示默認的“B”字符。要想讓其顯示成自己需要的圖標樣式,接着按照下面的步驟動手吧。

第四步:找到themes/default/css/ueditor.css文件,增加一條樣式定義:

.edui-for-macros .edui-icon {
    background-position: -680px -40px;
}

 此處的樣式定義了showmsg圖標在UEditor默認的精靈Icon圖片(themes/default/images/icons.png)中的位置偏移。如需更改成另外圖標,只需添加圖標到該圖片文件中,然后設置偏移值即可。

 

第五步:到此為止,在UI層面已經完成了一個工具欄圖標的顯示和各種狀態變化的邏輯,但是我們發現點擊按鈕之后毫無反應。那是必然的,我們還必須為該按鈕綁定屬於它自己的事件處理方法。

實質上,此時一個默認的事件處理方法已經被UEditor綁定到按鈕上了,只不過由於我們還沒有定義該方法的具體內容,所以點擊之后無任何變化。

 

下面我們就來定義該方法的具體內容:

在初始化編輯器的時候,加上自己的事件處理(插入一張圖片),如下代碼:

var ue = UE.getEditor('editor');
ue.commands['macros'] = { 
          execCommand: function() {
            this.execCommand('insertHtml', "<img src='https://www.baidu.com/img/bd_logo1.png' />"); 
            return true; 
          }, 
          queryCommandState: function() { } 
        };
點擊執行動作代碼和效果圖片:
exec:function () {

this.execCommand('insertHtml', "<input type='button' class='M_scrawl' value='聯系人稱謂'>");
return true;
}

 

 

 附: 百度編輯器官方也提供有接口,功能是可以,體驗不好,如:1. 不能自定義工具欄toolbars數組配置顯示;2. 源碼編輯時還能點


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM