2014-04-10 18:47
77人阅读
收藏
举报
CKEditor是目前不错的一个在线Html可视化编辑工具,但是他的图片上传功能有点弱了,只能单张单张的上传,很麻烦。
偶尔一次发现UEditor(http://ueditor.baidu.com/website/),这个百度开源出来的工具,非常不错,微信公
众平台也在用他,尤其是多图片上传的功能,非常不错,那么如果将UEditor中的多图片功能嫁接到CKEditor,是不是就完美了,那就行动吧。
首先,我们得了解CKEditor插件的开发流程。
插件的实现需要放到CKEditor/plugins目录下,建一个我们插件的目录,如multiimg,目录结构看下图:

其中plugin.js是关键的插件描述文件,内容如下:
- (function() {
- CKEDITOR.plugins.add("multiimg", {
- requires: ["dialog"],
- init: function(a) {
- a.addCommand("multiimg", new CKEDITOR.dialogCommand("multiimg"));
- a.ui.addButton("multiimg", {
- label: "批量上传图片",
- command: "multiimg",
- icon: this.path + "g.ico"
-
- });
- CKEDITOR.dialog.add("multiimg", this.path + "dialogs/multiimg.js")
-
- }
-
- })
-
- })();
如上所述,plugin.js文件定义了插件的基本信息,这里,我们的插件在CKEditor上添加了一个多图片上传的按钮,点击按钮后,他会打开一个对话框,对话框加载dialogs下的multiimg.js,我们来看一下这个js的内容:
- (function() {
- CKEDITOR.dialog.add("multiimg",
- function(a) {
- return {
- title: "批量上传图片",
- minWidth: "660px",
- minHeight:"400px",
- contents: [{
- id: "tab1",
- label: "",
- title: "",
- expand: true,
- width: "420px",
- height: "300px",
- padding: 0,
- elements: [{
- type: "html",
- style: "width:660px;height:400px",
- html: '<iframe id="uploadFrame" src="/image/image.html?v=' +new Date().getSeconds() + '" frameborder="0"></iframe>'
- }]
- }],
- onOk: function() {
- var ins = a;
- var num = window.imgs.length;
- if(window.duiqi == undefined || window.duiqi == null){
- window.duiqi = "none";
- }
- for(var i=0;i<num;i++){
- var imgHtml = "<p";
- if("center" == window.duiqi){
- imgHtml += " style=\"text-align:center\">";
- }else{
- imgHtml += ">";
- }
-
- imgHtml += "<img src=\"" + window.imgs[i].url + "\" ";
- if("none" != window.duiqi && "center" != window.duiqi){
- imgHtml += "style=\"float: " + window.duiqi + ";\"/>";
- }else{
- imgHtml += "/>";
- }
- imgHtml += "</p>";
- ins.insertHtml(imgHtml);
- }
- window.imgs = new Array();
-
-
- },
- onShow: function () {
- document.getElementById("uploadFrame").setAttribute("src","/image/image.html?v=' +new Date().getSeconds() + '");
- }
- }
- })
- })();
我们的实现也比较简单,对话框加载了一个iframe,iframe就默认打开UEditor的图片上传控件。
启程上文,我们继续来讲如何在CKEditor中添加多图片上传控件。
我们先贴一张最终的演示图给大家看一下:

点击CKEditor上的上传多图片按钮,就会弹出这个对话框,这里我们使用的是UEditor中的多图片上传控件。
如何实现呢,其实就是从UEditor中把这块扣出来单用了。
我们来贴一下iframe下的页面吧:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www./TR/html4/loose.dtd">
- <html>
- <head>
- <title></title>
- <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
- <link rel="stylesheet" href="image.css" type="text/css"/>
- <script type="text/javascript" src="/js/login/jquery-1.7.2.min1c6c0c.js"></script>
- <script type="text/javascript" src="Default.js"></script>
- <style type="text/css">
- body{ font-size:12px; font-family:微软雅黑;}
- .up_tit{ width:620px; line-height:30px; display:table; height:30px; background-color:#ccc;}
- .up_ddl{ float:left;}
- .up_ddl div{ display:inline-block;}
- .up_sy{ float:right;}
- #updisable{ color:#666;}
- #upload {
- cursor: pointer;
- float: right;
- height: 30px;
- margin: 3px 6px 0 0;
- width: 100px;
- }
- </style>
-
- </head>
- <body>
- <div class="wrapper">
- <div id="imageTab">
- <div id="tabHeads" class="tabhead">
- <span tabSrc="local"><var id="lang_tab_local"></var></span>
- </div>
- <div id="tabBodys" class="tabbody">
-
- <input id="curCount" name="curCount" type="hidden" value="0" />
- <div class="panel" id="local" style="z-index: 200;">
- <div id="flashContainer">
- <object width="608" height="272" align="middle" id="flash"
- codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
- classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">
- <param value="window" name="wmode">
- <param value="imageUploader.swf" name="movie">
- <param value="container=flashContainer&url=/imageUp.jsp&ext=%7B%22fileNameFormat%22%3A%22%7Btime%7D%7Brand%3A6%7D%22%7D&fileType=%7B%22description%22%3A%22%E5%9B%BE%E7%89%87%22%2C%20%22extension%22%3A%22*.gif%3B*.jpeg%3B*.png%3B*.jpg%22%7D&flashUrl=imageUploader.swf&width=608&height=272&gridWidth=121&gridHeight=120&picWidth=100&picHeight=100&uploadDataFieldName=upfile&picDescFieldName=pictitle&maxSize=4&compressSize=2&maxNum=32&compressSide=0&compressLength=900"
- name="flashvars">
- <embed width="608" height="272" align="middle"
- pluginspage="http://www.macromedia.com/go/getflashplayer"
- type="application/x-shockwave-flash" name="flash" src="imageUploader.swf"
- flashvars="container=flashContainer&url=/imageUp.jsp&ext=%7B%22fileNameFormat%22%3A%22%7Btime%7D%7Brand%3A6%7D%22%7D&fileType=%7B%22description%22%3A%22%E5%9B%BE%E7%89%87%22%2C%20%22extension%22%3A%22*.gif%3B*.jpeg%3B*.png%3B*.jpg%22%7D&flashUrl=imageUploader.swf&width=608&height=272&gridWidth=121&gridHeight=120&picWidth=100&picHeight=100&uploadDataFieldName=upfile&picDescFieldName=pictitle&maxSize=4&compressSize=2&maxNum=32&compressSide=0&compressLength=900"
- ver="10.0.0" wmode="window" errormessage="Flash插件初始化失败,请更新您的FlashPlayer版本之后重试!">
- </object>
- </div>
- <div><div style="background: url('images/upload.png') repeat scroll 0% 0% transparent;" id="upload"></div><div id="duiqi" style="background: url('images/imglabel.png') no-repeat scroll -12px 2px transparent;"></div><div id="localFloat"></div></div>
- </div>
- </div>
- </div>
- </div>
- <script type="text/javascript">
- $(function(){
- var ci = "#localFloat";
- var floatContainer = $(ci),
- nameMaps = {"none":"默认", "left":"左浮动", "right":"右浮动", "center":"居中"};
- for (var j in nameMaps) {
- var div = document.createElement("div");
- div.setAttribute("name", j);
- if (j == "none") div.className = "focus";
-
- div.style.cssText = "background:url(images/" + j + "_focus.jpg);";
- div.setAttribute("title", nameMaps[j]);
- floatContainer.append(div);
- }
- switchSelect(ci);
- });
- /**
- * 选择切换,传入一个container的ID
- * @param selectParentId
- */
- function switchSelect(selectParentId) {
- var select = $(selectParentId);
- select.on("click", function (evt) {
- var tar = evt.srcElement || evt.target;
- select.children().attr("class","");
- tar.className = "focus";
- parent.duiqi = tar.getAttribute("name");
- });
- }
- </script>
- </body>
-
- </html>
再来看一下Default.js文件:
到此,所有的功能就实现了!
|