ckeditor最近修改一個上傳的,,原來的Image的上傳插件功能很多,,但是自己用,沒有必要,,就進(jìn)行了修改,,后來就改成了目前的樣子,根據(jù)_samples/api_dialog.html 進(jìn)行了修改,,把頁面里面的調(diào)用都進(jìn)行了修改.
1.添加網(wǎng)址和上傳在一個tab中
2.圖片上傳之后會直接把生成的值放到圖片網(wǎng)址的input中,。
1.index.html調(diào)用頁面
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www./1999/xhtml">
- <head>
- <title>Using API to customize dialogs - CKEditor Sample</title>
- <meta content="text/html; charset=utf-8" http-equiv="content-type" />
- <script type="text/javascript" src="./ckeditor.js"></script>
- <script type="text/javascript" src="./mydialog.js"></script>
- </head>
- <body>
- <h1>
- CKEditor Sample
- </h1>
- <textarea cols="80" id="editor1" name="editor1" rows="10"><p>This is some <strong>sample text</strong>. You are using <a href="http:///">CKEditor</a>.</p></textarea>
- <script type="text/javascript">
- //調(diào)用封裝的函數(shù)
- makeEditor('editor1');
- </script>
- </body>
- </html>
2. mydialog.js
- //外部調(diào)用函數(shù)
- function makeEditor(id) {
- CKEDITOR.on( 'dialogDefinition', function( ev )
- {
- var dialogName = ev.data.name;
- var dialogDefinition = ev.data.definition;
- if ( dialogName == 'link' )
- {
- var infoTab = dialogDefinition.getContents( 'info' );
- //刪除不要的標(biāo)簽頁中選項
- infoTab.remove( 'linkType' );
- infoTab.remove( 'browse' );
- var urlField = infoTab.get( 'url' );
- //更改鏈接的文字
- urlField['label'] = '鏈接地址';
- //刪除不要的tab標(biāo)簽頁
- dialogDefinition.removeContents( 'target' );
- dialogDefinition.removeContents( 'advanced' );
- //由于filebrowserUploadUrl的使用,刪除鏈接dialog中出現(xiàn)的upload標(biāo)簽頁
- dialogDefinition.removeContents( 'upload' );
- }
- });
- var editor = CKEDITOR.replace( id,
- {
- toolbar : [[ 'Source','-','Bold','Italic','Underline','Strike','-','Link','-','Unlink','-','AddImage']],
- //引入上傳
- filebrowserUploadUrl : 'http://127.0.0.1/editor/upload.php'
- });
- editor.on( 'pluginsLoaded', function( ev )
- {
- if ( !CKEDITOR.dialog.exists( 'myAddImage' ) )
- {
- //生成調(diào)用js的地址 窗體函數(shù)
- var href = 'http://' + window.location.host + '/editor/myAddImage.js';
- CKEDITOR.dialog.add( 'myAddImage', href );
- }
- editor.addCommand( 'myImageCmd', new CKEDITOR.dialogCommand( 'myAddImage' ) );
- editor.ui.addButton( 'AddImage',
- {
- label : '圖片',
- icon:'images/images.jpg', //增加按鈕圖標(biāo)
- command : 'myImageCmd'
- });
- });
- }
-
- //獲取CKEditorFuncNum的值
- function getUrlParam(url)
- {
- var reParam = new RegExp('(?:[\?&]|&)CKEditorFuncNum=([^&]+)', 'i') ;
- var match = url.match(reParam) ;
-
- return (match && match.length > 1) ? match[1] : '' ;
- }
- /*
- * iframe的onload
- * params:
- * t obj iframe
- * num int anonymous function number used to pass the url of a file to CKEditor (random number)
- */
- function iframeLoad(t, num){
- t.style.display = 'none';
- var ret = t.contentWindow.document.body.innerHTML;
- var fchild = t.contentWindow.document.body.firstChild;
- // fchild.nodeType { 1 => form, 3 => textNode}
- if (fchild.nodeType == 3) {
- //我返回的ret是json數(shù)據(jù),進(jìn)行處理
- var data = eval("("+ret+")");
- if(data.picurl) {
- picurl = data.picurl;
- //觸發(fā)filebrowser
- CKEDITOR.tools.callFunction(num, picurl);
- } else if(data.error) {
- CKEDITOR.tools.callFunction(num, '', '上傳失敗'+data.error);
- }
- }
- t.style.display = '';
- }
3. myAddImage.js
- CKEDITOR.dialog.add( 'myAddImage', function( editor )
- {
- var ADDIMAGE = 1,
- regexGetSizeOrEmpty = /(^\s*(\d+)((px)|\%)?\s*$)|^$/i;
- return {
- title : '添加圖片',
- minWidth : 400,
- minHeight : 200,
- contents :
- [
- {
- id : 'addImage',
- label : '添加圖片',
- title : '添加圖片',
- filebrowser : 'uploadButton',
- elements :
- [
- {
- id : 'txtUrl',
- type : 'text',
- label : '圖片網(wǎng)址',
- required: true
- },
- {
- id : 'photo',
- type : 'file',
- label : '上傳圖片',
- style: 'height:40px',
- size : 38
- },
- {
- type : 'fileButton',
- id : 'uploadButton',
- label : '上傳',
- filebrowser :
- {
- action : 'QuickUpload',
- target : 'addImage:txtUrl'//更新的文本標(biāo)簽
- },
- onClick: function(){
- var d = this.getDialog();
- var _txtUrl = d.getContentElement('addImage','txtUrl');
- var _photo = d.getContentElement('addImage','photo');
- var _frameId = _photo._.frameId;
- var _iframe = CKEDITOR.document.getById(_frameId);
- //給iframe添加onload事件
- _iframe.setAttribute('onload',
- 'getAjaxResult(this,'+getUrlParam(_photo.action)+')');
- },
- 'for' : [ 'addImage', 'photo']
- }
- ]
- }
- ],
- onOk : function(){
- _src = this.getContentElement('addImage', 'txtUrl').getValue();
- if (_src.match(regexGetSizeOrEmpty)) {
- alert('請輸入網(wǎng)址或者上傳文件');
- return false;
- }
- this.imageElement = editor.document.createElement( 'img' );
- this.imageElement.setAttribute( 'alt', '' );
- this.imageElement.setAttribute( 'src', _src );
- editor.insertElement( this.imageElement );
- }
- };
- });
4. upload.php頁面,就直接返回了些數(shù)據(jù),,php的上傳程序就略過了
- <?php
- $str = '{"picurl":/l.jpg"}';
- $str = '{"error":-304}';
- echo $str;
- ?>
生成的dialog的樣子和editor
|