程序员

nicedit 轻量级编辑器 使用心得

作者:admin 2021-04-23 我要评论

NicEdit的Javascript集成到任何网站在几秒钟内作出的任何元素/区块编辑或转换标准textareas来丰富文本编辑。 How to use http://nicedit.com/demos.php 给出了几...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
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 你可以根据你的功能需要,来定制一个下载。
本站下载地址

本文转载自网络,原文链接:https://m.jb51.net/article/18503.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 安卓版谷歌 Chrome 浏览器已支持“冻结

    安卓版谷歌 Chrome 浏览器已支持“冻结

  • 免费开源!经典压缩软件7-Zip首次登陆L

    免费开源!经典压缩软件7-Zip首次登陆L

  • Google确认Chrome 89新版:大幅减少内

    Google确认Chrome 89新版:大幅减少内

  • 鸿蒙第三方组件*SwipeCaptcha滑动拼图

    鸿蒙第三方组件*SwipeCaptcha滑动拼图

腾讯云代理商
精彩导读
海外云服务器
热门资讯
腾讯云代理商