基于Ckeditor的表单设计器的开发(三)

本文介绍了如何基于CKeditor开发自定义插件,以创建表单设计器中的文本框组件为例。通过在plugins目录下新建相应文件并编写核心代码,实现了点击确定按钮插入文本框的功能。相关代码后续将开源,对于英文较好的读者,建议查阅CKeditor官方SDK样例文章以获取更多信息。
摘要由CSDN通过智能技术生成

前面基本环境都做得差不多了,这篇我们来介绍自定义插件的开发。


我们以"文本框“为例,

先来看效果:



点击确定,即可插入一个文本框


实现方式:


1、在ckeditor目录下 plugins文件夹下,新建如下结构:



plugin.js


CKEDITOR.plugins.add( 'ths_textfield', {
	icons: 'ths_textfield',
	init: function( editor ) {   //初始化
		var pluginName = 'ths_textfield'; //控件名称
		editor.addCommand( pluginName, new CKEDITOR.dialogCommand( pluginName ) ); //给编辑器注册一个打开弹出窗命令
               
		editor.ui.addButton(pluginName, {  //在工具栏上增加一个按钮,绑定按钮事件
			label: '单行文本框',
			command: pluginName
		});
	
		if ( editor.contextMenu ) {  //为文本框加右键属性菜单
			editor.addMenuGroup( 'textFieldGroup' );
		     editor.addMenuItem( 'textFieldItem', {
			        label: '文本框属性',
			        command:pluginName,
			        group: 'textFieldGroup'
			    });
		      //右键菜单的监听器,判断是否显示菜单	    
		      editor.contextMenu.addListener( function( element ) {
			    	if ( element && !element.isReadOnly() ) {
						var name = element.getName();
						if ( name == 'input' ) {
							var type = element.getAttribute( 'type' ) || 'text';
							if ( type=='text' ){
								   return { textFieldItem: CKEDITOR.TRISTATE_OFF };
							}
						}
					}
			    });
		}
                //增加弹出窗
		CKEDITOR.dialog.add( pluginName, this.path + 'dialogs/'+pluginName+'.js' );
		//为文本框双击事件绑定一个事件,即显示弹出窗
		editor.on( 'doubleclick', function( evt ) {
			var element = evt.data.element;
			
			 if ( element.is( 'input' ) ) {
					var type = element.getAttribute( 'type' ) || 'text';
					if ( type=='text' ){
						evt.data.dialog =pluginName;
					}
			}
		})
		
	}
});


代码不过多解释,请看注释
不喜欢讲代码,大家结合文档看注释


ths_textfield.js

CKEDITO
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值