ckeditor自定义插件使用方法详解

所属分类: 网络编程 / 网页编辑器 阅读数: 1387
收藏 0 赞 0 分享

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页面代码:

<div id="mainContent">
  </div>
  <div 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>
  </div>

  <div id="Pictures" class="detailWraper nopadding" style="display: none;height: auto;">
    <input id="hidPicturesStatus" type="hidden" value="0"/>
    <input id="hidCurrPictures" type="hidden" value=''/>
    <input id="hidDictSuggestion" type="hidden" value=''/>
    <table>
      <tr>
        <td>
          <div id="fileQueue"></div>
          <div id="picWrapper"></div>
          <a id="fake-dlg-bigPic" href="javascript:void(0)" style="display: none;"></a>
          <div 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>
            <div class="imgList">
              <ul></ul>
            </div>
          </div>
          <div class="validation-summary-valid">
            <ul>
              <li style="display: none"></li>
            </ul>
          </div>

        </td>
      </tr>
    </table>
  </div>
  <div>
    <button id="fileUpload">批量上传</button>
    <button id="submit" class="btn btn-primary" style="vertical-align: top;line-height:23px;width:112px;height: 35px;">提交照片
    </button>
  </div>
</div>

upload.jps页面部分的js代码:

//提交照片
    photoTaskDetail.submit = function () {
      var pictures = window.picManager._getPictures();
      if (pictures.length < 1) {
        alert('请至少上传1张图片');
        return false;
      }
      for (var i in pictures) {
        var imgPath = "<img src='" + staticFileRoot + pictures[i].URL + "'/>";
        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 ? ',editorupload' : 'editorupload');
CKEDITOR.editorConfig = function( config ) {
  config.font_names= '宋体/宋体;黑体/黑体;仿宋/仿宋_GB2312;楷体/楷体_GB2312;隶书/隶书;幼圆/幼圆;微软雅黑/微软雅黑;'+ config.font_names;
  config.language = 'zh-cn';
  config.extraPlugins += (config.extraPlugins ? ',lineheight' : 'lineheight');
  config.extraPlugins += (config.extraPlugins ? ',editorupload' : 'editorupload');
  CKEDITOR.config.lineheight_sizes = CKEDITOR.config.lineheight_sizes +  '30px'; 
  config.height = 650;
  config.toolbarCanCollapse = true;
  config.uiColor = '#90B8E9';
  config.toolbar = 'Full';
  config.toolbar_Full = [
   { name: 'document',  items: [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },
   { name: 'clipboard',  items: [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
   { name: 'links',    items:['Link','Unlink']},
   { name: 'insert',   items:['HorizontalRule','Table','Image'] },

   '/',
   { name: 'basicstyles', items: [ 'Bold','Underline','Strike','Subscript','Superscript','-','RemoveFormat'] },
   { name: 'paragraph',  items: [ 'list', 'indent', 'blocks', 'align', 'bidi' ] },
   { name: 'styles',items: ['lineheight','Format','Font','FontSize']},
   { name: 'colors',items: ['TextColor', 'BGColor']},
   { name: 'tools', items : [ 'Maximize','editorupload'] }
];

将editorupload插件加入到ckeditor中。
以下是实现的部分截图:

实现总结:在自定义插件过程中,必须把原插件的图片插入的功能给打开,负责上传的图片不会被放入到ckeditor中,图片地址会被自动的过滤掉。这可能是ckeditor版本的bug导致。有解决方案的欢迎指导。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

整合ckeditor+ckfinder,解决上传文件路径问题

现在fckeditor已经改名为ckeditor,上传控件也分离为ckfinder,按照说明文档的默认配置会出现上传路径不正确的情况,因为我们的网站可以通过定义默认网站、虚拟目录、以及放在网站的子目录下进行访问
收藏 0 赞 0 分享

在kindEditor中获取当前光标的位置索引的实现代码

一直在用KindEditor,今天要用到光标的位置,然后就gg一下办法,后来发现这东西的编辑区域居然是iframe里面的一个body,不是textarea/input,后来就翻开了他的代码看,发现有个insertHtml
收藏 0 赞 0 分享

KindEditor 4.x 在线编辑器常用方法小结

要修改默认后台程序处理文件,修改plugins(插件文件夹)下的JavaScript内容fileManagerJson改为自己使用程序语言
收藏 0 赞 0 分享

javascript开发随笔3 开发iframe富文本编辑器的一点体会

前段时间有个需求是开发富文本编辑器,这个之前随做过,但看了需求,发现有些地方还需google
收藏 0 赞 0 分享

TinyMCE syntaxhl插入代码后换行的修改方法

上次搞了个整合insert code的TinyMCE编辑器,非常好用。但有一个缺点:每次插入代码后,光标都会停留在pre标记里面,打回车出不来,必须编辑HTML
收藏 0 赞 0 分享

Fckeditor编辑器内容长度限制统计实现方法

Fckeditor是一种大家常用的编辑器,但是他不能像网页表单那样利用document.getelementbyid能获取得到值了,它必须通过 FCKeditorAPI来操作,下面看看Fckeditor内容长度测试
收藏 0 赞 0 分享

PHP中CKEditor和CKFinder配置问题小结

PHP中CKEditor和CKFinder配置问题小结,使用CKEditor和CKFinder编辑器的朋友可以参考下
收藏 0 赞 0 分享

ckeditor的使用和配置方法分享

CKEditor是新一代的FCKeditor,是一个重新开发的版本。CKEditor是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛的被运用于各大网站
收藏 0 赞 0 分享

FCKeditor 2.6.6在ASP中的安装及配置方法分享

FCKeditor目前的最新版本是2.6.6,在网上搜索此版本的配置方法,发现很少有asp的配置方法,以下就把自己的一些配置经验分享给有需要的你。
收藏 0 赞 0 分享

FCKeditor 2.6.5 ASP环境安装配置使用说明

今天用到在线编辑器在asp环境下上传竟然无效。找了好久才找到这介绍,现备份于此,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多