`
jaky821
  • 浏览: 42545 次
  • 性别: Icon_minigender_1
  • 来自: contry
文章分类
社区版块
存档分类
最新评论

扩展一下FCK HtmlEditor 增加图片上传、代码插入功能

    博客分类:
  • ext
阅读更多

  Ext自己带了一个HtmlEditor,具备基本的编辑功能,而且使用起来非常方便,在web应用起来感觉还不错,但是他缺少了上传功能,这样使得编辑文字的时候不能图文并茂,写一个小工程个人认为不需要集成FCK或者其他的编辑器,使用这个足矣,所以在闲暇之余给HtmlEditor写了一个扩展代码,使得htmlEditor具备图片上传功能,并及时给图片插入到文字中间,代码如下:

Js代码 复制代码
  1. HTMLEditor=Ext.extend(Ext.form.HtmlEditor,{   
  2.      codeStyle:'<br/><pre style="border-right: #999999 1px dotted; padding-right: 5px; border-top: #999999 1px dotted; padding-left: 5px; font-size: 12px; padding-bottom: 5px; margin-left: 10px; border-left: #999999 1px dotted; margin-right: 10px; padding-top: 5px; border-bottom: #999999 1px dotted; background-color: #eeeeee">{0}</pre><br/>',   
  3.      onRender : function(ct, position){        
  4.          HTMLEditor.superclass.onRender.call(this, ct, position);   
  5.          if(this.keys){   
  6.             if(!this.keys.length){   
  7.             this.keyMap = new Ext.KeyMap(this.getEditorBody(), this.keys);   
  8.             }   
  9.             else{                  
  10.                 this.keyMap = new Ext.KeyMap(this.getEditorBody(),this.keys[0]);   
  11.                 for(var i=1;i<this.keys.length;i++)this.keyMap.addBinding(this.keys[i]);   
  12.             }   
  13.             this.keyMap.stopEvent=true;   
  14.          }   
  15.      },   
  16.        addImage:function(){   
  17.         function insertImage(){   
  18.             var editor=this;   
  19.             //editor.insertAtCursor("<br/><img src='upload/IMG_1198.JPG'/></br>");   
  20.             win.upload(function(ret){   
  21.                 if(ret){                       
  22.                     s="<br/><img src="+ret.path;   
  23.                     if(ret.width)s+=" width="+ret.width;   
  24.                     if(ret.height)s+=" height="+ret.height;   
  25.                     s+=" /><br/>";   
  26.                     editor.insertAtCursor(s);   
  27.                     win.close();   
  28.                 }                  
  29.                 });   
  30.         };   
  31.     var win=new UploadImageWindow({modal:false,iconCls:"icon-img",   
  32.         buttons:[{text:"确定",handler:insertImage,scope:this},{text:"取消",handler:function(){win.close();}}]});   
  33.         win.show();   
  34.     },   
  35.     addCode:function(){   
  36.         function insertCode(){   
  37.             var value=win.getComponent("codes").getValue();   
  38.             this.insertAtCursor(String.format(this.codeStyle,value));   
  39.             win.close();   
  40.         };   
  41.         var win=new Ext.Window({title:"添加代码",   
  42.                 width:500,   
  43.                 height:300,   
  44.                 modal:true,   
  45.                 iconCls:"icon-code",   
  46.                 layout:"fit",   
  47.                 items:{xtype:"textarea",id:"codes"},   
  48.                 buttons:[{text:"确定",handler:insertCode,scope:this},{text:"取消",handler:function(){win.close();}}]});   
  49.         win.show();   
  50.     },     
  51.     createToolbar:function(editor){   
  52.         HTMLEditor.superclass.createToolbar.call(this,editor);   
  53.         this.tb.insertButton(16,{cls:"x-btn-icon",   
  54.             icon:"resources/resources/images/default/dd/img.png",handler:this.addImage,scope:this});       
  55.         this.tb.insertButton(18,{cls:"x-btn-icon",   
  56.             icon:"resources/resources/images/default/dd/code.png",handler:this.addCode,scope:this});   
  57.                    
  58.     },   
  59.     validateValue : function(value){   
  60.          if(value.length > this.maxLength){   
  61.             var s=String.format(this.maxLengthText, this.maxLength);   
  62.             this.markInvalid(s);   
  63.             return false;   
  64.         }   
  65.          return true;   
  66.     }   
  67. });   
  68. UploadImageWindow=Ext.extend(Ext.Window,{   
  69.     title : '上传图片',        
  70.     width : 345,               
  71.     height : 210,      
  72.     defaults : {               
  73.         border : false  
  74.     },   
  75.     buttonAlign : 'center',    
  76.     createFormPanel :function() {   
  77.         return new Ext.form.FormPanel({            
  78.             defaultType : 'textfield',   
  79.             labelAlign : 'right',   
  80.             fileUpload:true,   
  81.             labelWidth : 75,               
  82.             frame : true,   
  83.             defaults : {               
  84.                 width : 220        
  85.             },   
  86.             items : [{                     
  87.                     name : 'pathFile',   
  88.                     fieldLabel : '选择图片',   
  89.                     inputType : 'file'  
  90.                     },   
  91.                     {                      
  92.                         name : 'title',   
  93.                         fieldLabel : '说明'  
  94.                     },   
  95.                     {                      
  96.                         name : 'path',   
  97.                         fieldLabel : 'URL'                 
  98.                     },   
  99.                     {   
  100.                         name : 'width',   
  101.                         fieldLabel : '宽度',   
  102.                         value:'240'                
  103.                     },   
  104.                     {   
  105.                         name : 'height',   
  106.                         fieldLabel : '高度',   
  107.                         value:'200'            
  108.                     }   
  109.                 ]   
  110.         });   
  111.     },                     
  112.     upload:function(fn) {   
  113.             this.fp.form.submit({   
  114.                     waitTitle:"Ժ",   
  115.                     waitMsg : '正在上传......',   
  116.                     method:"POST",   
  117.                     url : 'xxxx',//上传的action   
  118.                     success : function(form, action) {   
  119.                         this.fp.form.findField("path").setValue("/upload/"+action.result.data.path);   
  120.                         var obj={title:this.fp.form.findField("title").getValue(),   
  121.                            path:this.fp.form.findField("path").getValue(),   
  122.                            width:this.fp.form.findField("width").getValue(),   
  123.                            height:this.fp.form.findField("height").getValue()   
  124.                            }                   
  125.                         fn(obj);                           
  126.                     },   
  127.                     failure : function(form, action) {                         
  128.                         if (action.failureType == Ext.form.Action.SERVER_INVALID)   
  129.                             Ext.MessageBox.alert('', action.result.errors.msg);   
  130.                         fn(false);                 
  131.                     },   
  132.                     scope:this  
  133.                 });   
  134.         },   
  135.     initComponent : function(){   
  136.         UploadImageWindow.superclass.initComponent.call(this);          
  137.         this.fp=this.createFormPanel();   
  138.         this.add(this.fp);   
  139.      }     
  140.  });    
  141. //   
  142. Ext.reg('myhtmleditor',HTMLEditor);  
HTMLEditor=Ext.extend(Ext.form.HtmlEditor,{
	 codeStyle:'<br/><pre style="border-right: #999999 1px dotted; padding-right: 5px; border-top: #999999 1px dotted; padding-left: 5px; font-size: 12px; padding-bottom: 5px; margin-left: 10px; border-left: #999999 1px dotted; margin-right: 10px; padding-top: 5px; border-bottom: #999999 1px dotted; background-color: #eeeeee">{0}</pre><br/>',
	 onRender : function(ct, position){	 	
	 	 HTMLEditor.superclass.onRender.call(this, ct, position);
	 	 if(this.keys){
	 	 	if(!this.keys.length){
	 	 	this.keyMap = new Ext.KeyMap(this.getEditorBody(), this.keys);
	 	 	}
	 	 	else{	 	 		
	 	 		this.keyMap = new Ext.KeyMap(this.getEditorBody(),this.keys[0]);
	 	 		for(var i=1;i<this.keys.length;i++)this.keyMap.addBinding(this.keys[i]);
	 	 	}
	 	 	this.keyMap.stopEvent=true;
	 	 }
	 },
       addImage:function(){
		function insertImage(){
			var editor=this;
			//editor.insertAtCursor("<br/><img src='upload/IMG_1198.JPG'/></br>");
			win.upload(function(ret){
				if(ret){					
					s="<br/><img src="+ret.path;
					if(ret.width)s+=" width="+ret.width;
					if(ret.height)s+=" height="+ret.height;
					s+=" /><br/>";
					editor.insertAtCursor(s);
					win.close();
				}				
				});
		};
	var win=new UploadImageWindow({modal:false,iconCls:"icon-img",
		buttons:[{text:"确定",handler:insertImage,scope:this},{text:"取消",handler:function(){win.close();}}]});
		win.show();
	},
	addCode:function(){
		function insertCode(){
			var value=win.getComponent("codes").getValue();
			this.insertAtCursor(String.format(this.codeStyle,value));
			win.close();
		};
		var win=new Ext.Window({title:"添加代码",
				width:500,
				height:300,
				modal:true,
				iconCls:"icon-code",
				layout:"fit",
				items:{xtype:"textarea",id:"codes"},
				buttons:[{text:"确定",handler:insertCode,scope:this},{text:"取消",handler:function(){win.close();}}]});
		win.show();
	},	
	createToolbar:function(editor){
		HTMLEditor.superclass.createToolbar.call(this,editor);
		this.tb.insertButton(16,{cls:"x-btn-icon",
			icon:"resources/resources/images/default/dd/img.png",handler:this.addImage,scope:this});	
		this.tb.insertButton(18,{cls:"x-btn-icon",
			icon:"resources/resources/images/default/dd/code.png",handler:this.addCode,scope:this});
				
	},
	validateValue : function(value){
		 if(value.length > this.maxLength){
		 	var s=String.format(this.maxLengthText, this.maxLength);
            this.markInvalid(s);
            return false;
        }
		 return true;
	}
});
UploadImageWindow=Ext.extend(Ext.Window,{
 	title : '上传图片',		
	width : 345,			
	height : 210,	
	defaults : {			
		border : false
	},
	buttonAlign : 'center',	
	createFormPanel :function() {
		return new Ext.form.FormPanel({			
			defaultType : 'textfield',
			labelAlign : 'right',
			fileUpload:true,
			labelWidth : 75,			
			frame : true,
			defaults : {			
				width : 220		
			},
			items : [{					
					name : 'pathFile',
					fieldLabel : '选择图片',
					inputType : 'file'
					},
					{					
						name : 'title',
						fieldLabel : '说明'
					},
					{					
						name : 'path',
						fieldLabel : 'URL'				
					},
					{
						name : 'width',
						fieldLabel : '宽度',
						value:'240'				
					},
					{
						name : 'height',
						fieldLabel : '高度',
						value:'200'			
					}
				]
		});
	},					
	upload:function(fn) {
			this.fp.form.submit({
					waitTitle:"Ժ",
					waitMsg : '正在上传......',
					method:"POST",
					url : 'xxxx',//上传的action
					success : function(form, action) {
						this.fp.form.findField("path").setValue("/upload/"+action.result.data.path);
						var obj={title:this.fp.form.findField("title").getValue(),
						   path:this.fp.form.findField("path").getValue(),
						   width:this.fp.form.findField("width").getValue(),
						   height:this.fp.form.findField("height").getValue()
						   }				
						fn(obj);						
					},
					failure : function(form, action) {						
						if (action.failureType == Ext.form.Action.SERVER_INVALID)
							Ext.MessageBox.alert('', action.result.errors.msg);
						fn(false);				
					},
					scope:this
				});
		},
	initComponent : function(){
        UploadImageWindow.superclass.initComponent.call(this);       
        this.fp=this.createFormPanel();
        this.add(this.fp);
	 } 	
 }); 
//
Ext.reg('myhtmleditor',HTMLEditor);

 

 

 使用Ext.extends来扩展一下自定义的htmlEditor,在扩展中,首先建立一个窗口,在图片上传窗口中很简单,就是再次建立一个上传的FormPanel,获取这个表单上传的结果(结果为js数组或者json)。在最后在使用Ext.reg('myhtmleditor',HTMLEditor)注册一下自定义的htmlEditor名称为myhtmleditor,这样在代码中就可以使用myhtmleditor类型了,如下代码:

Java代码 复制代码
  1. {   
  2. xtype:"myhtmleditor",   
  3. name:"content",   
  4. value:"",   
  5. fieldLabel:"内容",   
  6. width:550,   
  7. height:400  
  8. }  
{
xtype:"myhtmleditor",
name:"content",
value:"",
fieldLabel:"内容",
width:550,
height:400
}

 运行结果如下:
 

整体截图



 图片上传图片窗口


 

代码插入窗口

其实扩展Ext的组件很简单,例如还需要继续扩展HtmlEditor,可以在插入图片创建的window中重新,自己可以任意定义图片插入的窗口,可以设定任何属性,然后给相应的值转化为相应的html代码插入到htmleditor中就可以了!插入代码的过程更加简单,就是向htmlEditor中插入相应的css代码,并给用户的代码嵌入到css中!

  • 大小: 11.2 KB

 

 

分享到:
评论

相关推荐

    FCK集成图片批量上传

    本文将深入探讨如何在.NET环境下,集成FCKeditor实现图片的批量上传功能,并介绍其中涉及到的加水印代码。 首先,FCKeditor本身并不直接支持批量上传功能,但可以通过自定义插件或者扩展其功能来实现。批量上传通常...

    帝国6.0FCK增强,代码、自定义插入等新功能

    【标题】"帝国6.0FCK增强"指的是在帝国CMS 6.0版本中对FCKeditor进行了功能扩展。FCKeditor是一个流行的开源富文本编辑器,常用于网站后台内容管理系统的编辑区域,便于用户在网页上创建和编辑富文本内容。这次增强...

    基于PHP的帝国6.0FCK增强,代码、自定义插入等新功能.zip

    【标题】:“基于PHP的帝国6.0FCK增强,代码、自定义插入等新功能.zip”这个压缩包文件显然涉及到的是PHP编程语言在网站内容管理系统(CMS)中的应用,特别是针对“帝国CMS”这款软件的扩展和优化。帝国CMS是一款...

    PHP实例开发源码-帝国6.0FCK增强,代码、自定义插入等新功能.zip

    PHP实例开发源码—帝国6.0FCK增强,代码、自定义插入等新功能.zip PHP实例开发源码—帝国6.0FCK增强,代码、自定义插入等新功能.zip PHP实例开发源码—帝国6.0FCK增强,代码、自定义插入等新功能.zip

    FCK(fckeditor)远程图片上传功能

    在FCKeditor中,远程图片上传是指用户在编辑文章时,可以直接输入一个远程图片的URL,编辑器会自动尝试将该图片抓取到本地服务器,然后插入到文章中。这样做的好处是用户可以快速引用网络上的图片资源,同时避免了因...

    基于PHP的帝国6.0FCK增强,代码、自定义插入等新功能源码.zip

    【标题】"基于PHP的帝国6.0FCK增强,代码、自定义插入等新功能源码",指的是一个针对帝国CMS系统(EmpireCMS)的6.0版本的FCKeditor编辑器进行了功能增强的PHP项目。FCKeditor是一款流行的开源富文本编辑器,它允许...

    FCK配置过的源代码

    标题中的"FCK配置过的源代码"指的是该压缩包包含了一份已经经过个性化配置的FCKeditor源代码,主要涉及了上传功能、字符编码、文件大小限制以及界面定制等方面。 1. **上传中文名乱码问题** 在处理非英文文件名时...

    FCK代码高亮插件

    而FCK代码高亮插件则是在此基础上,增加了代码语法着色的功能,使得代码在预览或编辑时更加清晰、美观。 **代码高亮**是一种通过使用不同的颜色、字体样式来突出显示代码中的关键字、变量、注释等元素的技术。这有...

    fck文件上传模板

    这个“fck文件上传模板”显然与FCK编辑器的功能扩展有关,尤其是其文件上传功能。在Web开发中,文件上传是一个常见的需求,用于让用户上传图片、文档等各类文件到服务器。 FCK编辑器的文件上传功能通常涉及到以下几...

    强大的FCK编辑器修改版兼容多个浏览器支持图片上传功能

    这个“强大的FCK编辑器修改版”针对不同的浏览器进行了优化,确保了在多种环境下都能稳定运行并提供图片上传功能。 首先,让我们详细了解一下FCK编辑器的核心特性: 1. **跨浏览器兼容性**:FCK编辑器原版已经支持...

    可以上传中文图片的FCK编辑器

    《掌握FCK编辑器:实现中文图片上传功能》 FCKeditor是一款强大的开源在线文本编辑器,它使得在网页上创建和编辑富文本内容变得轻松易行。然而,原版的FCKeditor存在一个限制,即仅支持数字或字母命名的图片上传。...

    FCk上传图片和文案

    这个名为"FCk上传图片和文案"的资源显然与使用FCKeditor在JSP(JavaServer Pages)环境中实现图片和文档上传的功能有关。下面我们将深入探讨这个主题。 FCKeditor是一个开源的JavaScript组件,它允许用户在网页上...

    fck编辑器 增加了表情

    在“fck编辑器增加了表情”这一主题中,我们主要关注的是FCKeditor如何扩展其功能,以支持表情的插入和管理。在默认情况下,FCKeditor可能只提供基本的文字编辑功能,但通过自定义和扩展,我们可以将其转变为一个...

    一款好的.net fck

    标签中的“FCK 源码”表明这个压缩包可能包含了FCKeditor的源代码,开发者可以查看、修改或扩展编辑器的功能,满足特定需求。对于学习富文本编辑器开发或者想要定制化FCKeditor的人来说,源码是十分宝贵的资源。 ...

    FCK 保存远程图片

    "FCK 保存远程图片"这一主题涉及到的是FCKeditor的一个功能,即如何将互联网上的远程图片保存到本地服务器的特定文件夹中,以便在编辑的内容中正常显示这些图片,同时避免因远程图片源的不可用而导致的显示问题。...

    FCK在服务器上上传图片问题的解决方法

    #### 二、配置FCK上传图片保存位置 在网站根目录下创建一个名为“UpFiles”的文件夹,用于存储用户通过FCKeditor上传的所有图片文件。接着,在项目的Web.config文件中添加以下配置代码: ```xml ~/fckeditor/" /...

    C# .NET FCK 编辑器例子(更新了不能在服务器上传图片的问题)

    在提供的`FCKDemo`压缩包中,可能包含了实现这一功能的示例代码,包括C#后台处理文件上传的代码、配置文件以及可能的前端交互脚本。通过研究这些文件,您可以更深入地了解如何在实际项目中应用FCKeditor。

    开源FCK编辑器源代码

    2. **多媒体支持**:可以插入图片、视频、音频等多媒体内容,并提供简单的编辑和管理功能。 3. **拖放功能**:用户可以通过拖放操作上传和排列内容,提升编辑效率。 4. **HTML清理**:自动修正和优化输入的HTML代码...

    给FCKeditor扩展插入FLV视频功能

    本文将深入讲解如何为FCKeditor扩展插入FLV视频功能,以及涉及到的相关技术知识点。 首先,FLV是一种流行的Flash视频格式,常用于在线流媒体播放。为了在FCKeditor中实现FLV视频的插入,我们需要对FCKeditor的源码...

    fck控件fck控件fck控件fck控件fck控件fck控件fck控件fck控件

    8. **插件扩展**:FCK编辑器支持自定义插件,可以扩展更多功能,如代码高亮、地图集成等。 使用FCK控件时,开发者通常会将其嵌入到网页的表单中,作为文本输入框的替代品,使得用户在填写内容时有更丰富的编辑选项...

Global site tag (gtag.js) - Google Analytics