ckeditor自定義插件使用方法詳解
ckeditor是一款功能很強大的富文本編輯的工具,給我們提供了絕大多數(shù)功能,滿足我們?nèi)粘i_發(fā)所用,但由于特殊情況,可能會需要修改ckeditor的插件。ckeditor提供了給我們很方便擴展插件的接口。
最經(jīng)由于項目的需要,需要重寫ckeditor的上傳圖片的功能,以下是自定義圖片上傳功能的部分代碼:
1、在ckeditor/plugins/目錄下新建editorupload目錄,用來存放自定義插件;在該目錄下新建目錄images用來存放自定以圖片,在images目錄下放入插件圖片image.png.

2、在editorupload目錄下新建plugin.js:
(function () {
var a = {
exec: function (editor) {
//調(diào)用jsp中的函數(shù)彈出上傳框,
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: '添加圖片', //鼠標(biāo)懸停在插件上時顯示的名字
icon: 'plugins/editorupload/images/image.png', //自定義圖標(biāo)的路徑
command: b
});
}
});
})();
在上面代碼中,新建了一個upload.jsp頁面用來上傳圖片,使用了openDialog彈出一個新的窗口,設(shè)置了彈出框的高度和寬度。
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中。
以下是實現(xiàn)的部分截圖:


實現(xiàn)總結(jié):在自定義插件過程中,必須把原插件的圖片插入的功能給打開,負(fù)責(zé)上傳的圖片不會被放入到ckeditor中,圖片地址會被自動的過濾掉。這可能是ckeditor版本的bug導(dǎo)致。有解決方案的歡迎指導(dǎo)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
CKEditor中加入syntaxhighlighter代碼高亮插件
CKEditor是新一代的FCKeditor,是一個重新開發(fā)的版本。CKEditor是全球最優(yōu)秀的網(wǎng)頁在線文字編輯器之一,因其驚人的性能與可擴展性而廣泛的被運用于各大網(wǎng)站2014-12-12
ajax php實現(xiàn)給fckeditor文本編輯器增加圖片刪除功能
工作需要需要fck編輯器的服務(wù)器瀏覽加個圖片刪除的功能,我們利用ajax php實現(xiàn)的有需要的朋友可以參考下2012-12-12
SyntaxHighlighter 去掉右側(cè)滾動條的方法
SyntaxHighlighter這個是一個高亮插件。現(xiàn)在被用于很多網(wǎng)站的代碼顯示。但是SyntaxHighlighter3.0.83,由于自適應(yīng)寬和高,導(dǎo)致一直有滾動條的問題2020-03-03
讓谷歌瀏覽器Google Chrome支持eWebEditor的方法
這篇文章主要介紹了讓谷歌瀏覽器Google Chrome支持eWebEditor的方法,默認(rèn)情況是不顯示的, 還需要安裝組件2016-01-01
關(guān)于jsp版ueditor1.2.5的部分問題解決(上傳圖片失敗)
這篇文章主要介紹大家在使用jsp版ueditor1.2.5的碰到的一些問題解決方法,需要的朋友可以參考下2013-06-06
fckeditor在ie9中無法彈出對話框的解決方法(彈出層兼容問題)
升級到 IE 9后,fckeditor在IE 9里的彈出浮動層會出現(xiàn)bug,里面的內(nèi)容不會出現(xiàn)2012-04-04

