当前位置: 移动技术网 > IT编程>脚本编程>编辑器 > nicedit 轻量级编辑器 使用心得

nicedit 轻量级编辑器 使用心得

2017年12月08日  | 移动技术网IT编程  | 我要评论
nicedit的javascript集成到任何网站在几秒钟内作出的任何元素/区块编辑或转换标准textareas来丰富文本编辑。
how to use
http://nicedit.com/demos.php 给出了几个demo,包括最傻瓜式的把textarea转换成niceditor,简单配置编辑区和命令按钮,以及不同风格的编辑界面。
deployment
nicedit 可能是引用文件最少的在线编辑器,原因是不能更少了,一个js,一个图标文件。这两者的目录结构修改配置。
new niceditor({iconspath : '../niceditoricons.gif'})
source code
nicedit 的源码是面向对象的,这使的本来就少至1300多行的代码更容易阅读,当然还有修改。
以一个添加图片的按钮为例:
/* start config */
var nicimageoptions = {
buttons : {
'image' : {name : 'add image', type : 'nicimagebutton', tags : ['img']}
}
};
/* end config */
var nicimagebutton = niceditoradvancedbutton.extend({
addpane : function() {
this.im = this.ne.selectedinstance.selelm().parenttag('img');
this.addform({
'' : {type : 'title', txt : 'add/edit image'},
'src' : {type : 'text', txt : 'url', 'value' : 'http://', style : {width: '150px'}},
'alt' : {type : 'text', txt : 'alt text', style : {width: '100px'}},
'align' : {type : 'select', txt : 'align', options : {none : 'default','left' : 'left', 'right' : 'right'}}
},this.im);
},
submit : function(e) {
var src = this.inputs['src'].value;
if(src == "" || src == "http://") {
alert("you must enter a image url to insert");
return false;
}
this.removepane();
if(!this.im) {
var tmp = 'javascript:nicimtemp();';
this.ne.niccommand("insertimage",tmp);
this.im = this.findelm('img','src',tmp);
}
if(this.im) {
this.im.setattributes({
src : this.inputs['src'].value,
alt : this.inputs['alt'].value,
align : this.inputs['align'].value
});
}
}
});
niceditors.registerplugin(nicplugin,nicimageoptions);
/* start config *//* end config */
之间是添加图片按钮在按钮条上的配置,之后代码控制是添加图片按钮点击后浮出相应的面板,用来接收输入以在编辑器里插入图片。最后一句代码是把该按钮注册到按钮条上。
事实上,你也可以完全不使用nicedit的按钮条,而自己调用命令,即这行代码,
ne.niccommand("insertimage",tmp);
这里的ne对象是nicedit的编辑区,它可以通过这种方式获得
myniceditor = new niceditor();
myniceditor.addinstance('editordiv');
ed = myniceditor.nicinstances[0];
需要注意的是,你要是这么简单的调用的话,和可能是没有任何效果的。你还需要在nicedit编辑区onblur前,比如你是在用户点击一个div的时候来插入图片,这时你需要在这个div onmousedown的时候执行
ed.saverng();
来保存键盘在编辑器上的焦点,并在从用户那里得到了想要的输入以后,比如div的onclick,或是select的onchange以后,执行
ed.restorerng();
以恢复焦点,只有这样,才能正确的位置插入图片。
download
nicedit提供了插件机制,非常容易拓展,在http://nicedit.com/download.php 你可以根据你的功能需要,来定制一个下载。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网