首頁 > web前端 > js教程 > 主體

ckeditor自訂外掛程式使用方法解析

赶牛上岸
發布: 2018-03-06 13:50:35
原創
2515 人瀏覽過

CKEditor即大名鼎鼎的FCKeditor,該公司的另一個產品為CKFinder(一個Ajax檔案管理器)。 ckeditor是一款功能強大的富文本編輯工具,這篇文章主要為大家詳細介紹了ckeditor自訂插件的使用方法,具有一定的參考價值,有興趣的小伙伴們可以參考一下

ckeditor是一款功能很強大的富文本編輯的工具,給我們提供了絕大多數功能,滿足我們日常開發所用,但由於特殊情況,可能會需要修改ckeditor的插件。 ckeditor提供了給我們很方便擴充插件的介面。

最經由於專案的需要,需要重寫ckeditor的上傳圖片的功能,以下是自訂圖片上傳功能的部分程式碼:

1、在ckeditor/plugins/目錄下新建editorupload目錄,用來存放自訂插件;在該目錄下新建目錄images用來存放自定以圖片,在images目錄下放入插件圖片image.png.

2、在editorupload目錄下新建plugin.js:

#
(function () {
  var a = {
      exec: function (editor) {
        //调用jsp中的函数弹出上传框,
        var url = '../view/fileupload/upload.jsp';
        openDialog({  //openDialog打开一个新窗口
          title: '插入图片',
          url: url,
          height: 600,
          width: 900,
          callback:function(){
          }
        });
      }
    },
    b = 'editorupload';
  CKEDITOR.plugins.add('editorupload', {
    init: function (editor) {
      editor.addCommand(b, a);
      editor.ui.addButton('editorupload', {
        label: '添加图片', //鼠标悬停在插件上时显示的名字
        icon: 'plugins/editorupload/images/image.png',  //自定义图标的路径
        command: b
      });
    }
  });
})();
登入後複製

在上面程式碼中,新建了一個upload.jsp頁面用來上傳圖片,使用了openDialog彈出一個新的窗口,設定了彈出框的高度和寬度。
CKEDITOR.plugins.add將自訂的editorupload加入ckeditor中。

下面是部分upload.jsp頁面程式碼:


#
<p id="mainContent">
  </p>
  <p class=" box">
    <table class=" m-table">
      <colgroup>
        <col width="20%"/>
        <col width="80%"/>
      </colgroup>
      <tr>
        <td style="vertical-align:top;"><label class="module-name">图片说明</label></td>
        <td>
          <ul>
            <li>1、《PC首页轮播图片》长宽为666×250显示效果最好;《APP首页轮播图片》长宽为422×262显示效果最好;</li>
            <li>3、图片提交才会在首页生效;</li>
          </ul>
        </td>
      </tr>
    </table>
  </p>
  <p id="Pictures" class="detailWraper nopadding" style="display: none;height: auto;">
    <input id="hidPicturesStatus" type="hidden" value="0"/>
    <input id="hidCurrPictures" type="hidden" value=&#39;&#39;/>
    <input id="hidDictSuggestion" type="hidden" value=&#39;&#39;/>
    <table>
      <tr>
        <td>
          <p id="fileQueue"></p>
          <p id="picWrapper"></p>
          <a id="fake-dlg-bigPic" href="javascript:void(0)" style="display: none;"></a>
          <p id="dlg-bigPic" class="popImg" style="display: none;">
            <a class="leftBtn" href="javascript:void(0)"></a>
            <a class="rightBtn" href="javascript:void(0)"></a>
            <a class="closeImgBtn" href="javascript:void(0)"></a>
            <p class="imgList">
              <ul></ul>
            </p>
          </p>
          <p class="validation-summary-valid">
            <ul>
              <li style="display: none"></li>
            </ul>
          </p>
        </td>
      </tr>
    </table>
  </p>
  <p>
    <button id="fileUpload">批量上传</button>
    <button id="submit" class="btn btn-primary" style="vertical-align: top;line-height:23px;width:112px;height: 35px;">提交照片
    </button>
  </p>
</p>
登入後複製

upload.jps頁面部分的js程式碼:

//提交照片
    photoTaskDetail.submit = function () {
      var pictures = window.picManager._getPictures();
      if (pictures.length < 1) {
        alert(&#39;请至少上传1张图片&#39;);
        return false;
      }
      for (var i in pictures) {
        var imgPath = "<img src=&#39;" + staticFileRoot + pictures[i].URL + "&#39;/>";
        var element = window.parent.CKEDITOR.dom.element.createFromHtml(imgPath);
        window.parent.CKEDITOR.instances.editorContent.insertElement(element);
      }
      parent.closeDialog(false);
    }
登入後複製

上面程式碼中,可以上傳多張照片,分別將照片放入ckeditor中。
設定ckeditor的config.js:

config.extraPlugins += (config.extraPlugins ? &#39;,editorupload&#39; : &#39;editorupload&#39;);
CKEDITOR.editorConfig = function( config ) {
  config.font_names= &#39;宋体/宋体;黑体/黑体;仿宋/仿宋_GB2312;楷体/楷体_GB2312;隶书/隶书;幼圆/幼圆;微软雅黑/微软雅黑;&#39;+ config.font_names;
  config.language = &#39;zh-cn&#39;;
  config.extraPlugins += (config.extraPlugins ? &#39;,lineheight&#39; : &#39;lineheight&#39;);
  config.extraPlugins += (config.extraPlugins ? &#39;,editorupload&#39; : &#39;editorupload&#39;);
  CKEDITOR.config.lineheight_sizes = CKEDITOR.config.lineheight_sizes +  &#39;30px&#39;; 
  config.height = 650;
  config.toolbarCanCollapse = true;
  config.uiColor = &#39;#90B8E9&#39;;
  config.toolbar = &#39;Full&#39;;
  config.toolbar_Full = [
   { name: &#39;document&#39;,  items: [ &#39;Source&#39;,&#39;-&#39;,&#39;Save&#39;,&#39;NewPage&#39;,&#39;DocProps&#39;,&#39;Preview&#39;,&#39;Print&#39;,&#39;-&#39;,&#39;Templates&#39; ] },
   { name: &#39;clipboard&#39;,  items: [ &#39;Cut&#39;,&#39;Copy&#39;,&#39;Paste&#39;,&#39;PasteText&#39;,&#39;PasteFromWord&#39;,&#39;-&#39;,&#39;Undo&#39;,&#39;Redo&#39; ] },
   { name: &#39;links&#39;,    items:[&#39;Link&#39;,&#39;Unlink&#39;]},
   { name: &#39;insert&#39;,   items:[&#39;HorizontalRule&#39;,&#39;Table&#39;,&#39;Image&#39;] },
   &#39;/&#39;,
   { name: &#39;basicstyles&#39;, items: [ &#39;Bold&#39;,&#39;Underline&#39;,&#39;Strike&#39;,&#39;Subscript&#39;,&#39;Superscript&#39;,&#39;-&#39;,&#39;RemoveFormat&#39;] },
   { name: &#39;paragraph&#39;,  items: [ &#39;list&#39;, &#39;indent&#39;, &#39;blocks&#39;, &#39;align&#39;, &#39;bidi&#39; ] },
   { name: &#39;styles&#39;,items: [&#39;lineheight&#39;,&#39;Format&#39;,&#39;Font&#39;,&#39;FontSize&#39;]},
   { name: &#39;colors&#39;,items: [&#39;TextColor&#39;, &#39;BGColor&#39;]},
   { name: &#39;tools&#39;, items : [ &#39;Maximize&#39;,&#39;editorupload&#39;] }
];
登入後複製

將editorupload外掛程式加入ckeditor。
以下是實現的部分截圖:

# 實作總結:在自訂插件過程中,必須把原插件的圖片插入的功能給打開,負責上傳的圖片不會被放入到ckeditor中,圖片地址會被自動的過濾掉。這可能是ckeditor版本的bug導致。有解決方案的歡迎指導。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持php中文網。

相關推薦:

django教你如何輕鬆使用富文本編輯器CKEditor

django教你熟練富文本編輯器CKEditor的方法

Python的Flask框架中整合CKeditor富文本編輯器

以上是ckeditor自訂外掛程式使用方法解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!