CKEditor 添加自訂按鈕

來源:互聯網
上載者:User

標籤:style   http   使用   檔案   io   for   cti   html   

1、下載ckeditor,我這裡下載的是CKEditor 3.6.2。

2、裡邊有壓縮過的代碼及源碼,源碼所在目錄為_source,還有一些使用例子,具體不做累述

此處主要講的是在使用過程需要添加自訂按鈕。

 

2. 比如我要添加“插入代碼”的按鈕,起名為code。在ckeditor/plugins下建立檔案夾code,在code檔案夾裡加入一個小圖片如code.gif,然後在code檔案夾裡建立plugin.js檔案,內容如下:

//一鍵排版
(function () {

 b = ‘format_wz‘;

 CKEDITOR.plugins.add(b, {

       requires: [‘styles‘, ‘button‘],

       init: function (a) {

               a.addCommand(b, CKEDITOR.plugins.autoformat.commands.autoformat);

               a.ui.addButton(‘format_wz‘, {

                      label: "一鍵排版",

                      command: ‘format_wz‘,

                      icon: this.path + "format_wz.png"

          });

       }

 });

 CKEDITOR.plugins.autoformat = {

     commands: {

          autoformat: {

                exec: function (editor) {

                     formatText(editor);

               }

         }

    }

};

  //執行的方法
    function formatText(editor) {

         var myeditor = editor;

          //得到要編輯的原始碼       
          var editorhtml = myeditor.getData();
         //在這裡執行你的操作。。。。。

      editorhtml= editorhtml.replace(/(<\/?(?!br|p|img|a|h1|h2|h3|h4|h5|h6)[^>\/]*)\/?>/gi,‘‘);    
        //在p標籤處添加樣式,使每個段落自動縮排兩個字元
        editorhtml= editorhtml.replace(/\<[p|P]>/g,"<p style=‘text-indent: 2em‘>");

           //再將內容放回到編輯器中
          editor.setData(html);

  }

 })();

3. 修改config.js來註冊code外掛程式。用如下代碼替換config.js原來內容:

 

CKEDITOR.editorConfig = function( config ) {  

 //配置CKFinder

 config.extraPlugins = "code,uploadImg";

//建立外掛程式  config.toolbar_temp =         [       

   { name: ‘document‘, items: [‘code‘,‘format_wz‘, ‘Save‘] },        

         { name: ‘styles‘, items: [‘Styles‘, ‘Format‘, ‘Font‘, ‘FontSize‘] }         ];

   config.toolbar_Full =         [        

  { name: ‘document‘, items: [‘code‘, ‘Source‘,‘-‘,‘format_wz‘,‘-‘, ‘Save‘, ‘NewPage‘, ‘DocProps‘, ‘Preview‘, ‘Print‘, ‘-‘, ‘Templates‘] },            

     { name: ‘tools‘, items: [‘Maximize‘, ‘ShowBlocks‘, ‘-‘, ‘About‘] }         ];    

};

注意我的CKEditor配置都是通過修改config.js來完成

4. 安裝CKEditor,在要引入CKEditor的頁面中script標籤內添加如下js代碼:

var editor=CKEDITOR.replace(‘p_Content_content‘,{
          fullPage: true,
          extraPlugins: ‘uploadImg,format_wz,docprops‘
      });
      CKFinder.setupCKEditor(editor, ‘ckfinder‘);

5.重新整理頁面,就能看到自己後添加的按鈕了。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.