angeditor是个很好的编辑器。小巧玲珑,惹人怜爱~
但是在使用的过程中,我们往往会复制word的文字到编辑器中,如果wangeditor没有做任何处理,会导致复制的内容比较错乱,甚至无法添加到数据库中。
为了解决这个问题,我们需要对从word中粘贴的内容进行处理,吧多余的代码剔除。
下面是我再实际应用中处理的代码分享给大家。
window.wangEditor.fullscreen={ // editor create之后调用 init: function(editorSelector){ $(editorSelector + " .w-e-toolbar").append('<div class="w-e-menu"><a class="_wangEditor_btn_fullscreen" href="###" onclick="window.wangEditor.fullscreen.toggleFullscreen(\'' + editorSelector + '\')">全屏</a></div>'); }, toggleFullscreen: function(editorSelector){ $(editorSelector).toggleClass('fullscreen-editor'); if($(editorSelector + ' ._wangEditor_btn_fullscreen').text()=='全屏'){ $(editorSelector + ' ._wangEditor_btn_fullscreen').text('退出全屏'); }else{ $(editorSelector + ' ._wangEditor_btn_fullscreen').text('全屏'); } } }; var E=window.wangEditor var editor=new E('#editor') // 或者 var editor=new E( document.getElementById('editor') ) // 配置服务器端地址 editor.customConfig.uploadFileName='file' editor.customConfig.uploadImgServer="{:url('Upload/wang_editor_upload')}" //去除富文本中粘贴word样式问题 editor.customConfig.pasteTextHandle=function (content) { // content 即粘贴过来的内容(html 或 纯文本),可进行自定义处理然后返回 if (content=='' && !content) return '' var str=content str=str.replace(/<xml>[\s\S]*?<\/xml>/ig, '') str=str.replace(/<style>[\s\S]*?<\/style>/ig, '') str=str.replace(/<\/?[^>]*>/g, '') str=str.replace(/[ | ]*\n/g, '<p>') str=str.replace(/ /ig, '') console.log('****', content) console.log('****', str) return str } editor.create() E.fullscreen.init('#editor');
这样复制过来的文字是呆有p标签可以换行的。
预览一下
还可以吧~,来源也是从网上复制的,大家喜欢可以自己拿去修改一下咯~
angEditor.vue组件
<template>
<div id="editor"></div>
</template>
<script>
import editor from 'wangeditor';
export default {
name: 'article-manage',
data(){
return{
nEditor:null,
}
},
mounted() {
this.nEditor=new editor('#editor');
this.nEditor.customConfig.onchange=html=> {
this.$emit("getContent",html);
};
this.nEditor.customConfig.uploadImgShowBase64=true; // base 64 存储图片
this.nEditor.customConfig.menus=[
//菜单配置
"head", // 标题
"bold", // 粗体
"fontSize", // 字号
"fontName", // 字体
"italic", // 斜体
"underline", // 下划线
"strikeThrough", // 删除线
"foreColor", // 文字颜色
"backColor", // 背景颜色
"link", // 插入链接
"list", // 列表
"justify", // 对齐方式
"quote", // 引用
"emoticon", // 表情
"image", // 插入图片
"table", // 表格
// "video", // 插入视频
"code", // 插入代码
"undo", // 撤销
"redo", // 重复
];
this.nEditor.customConfig.uploadImgMaxLength=1;
this.nEditor.customConfig.uploadImgServer='/* 上传图片后台接口 */';
this.nEditor.create();
this.creatBtn("#editor");
},
methods:{
// 设置编辑器内容
setEditorContent(content){
this.nEditor.txt.html(content);
},
// 获取编辑器内容
getEditorContent(){
this.$emit("getContent",this.nEditor.txt.html());
},
// 添加显示源码按钮
creatBtn(editorSelector){
let _this=this;
function toggleViewsource(editorSelector,that){
var editorHtml=that.nEditor.txt.html();
if($(editorSelector + ' ._wangEditor_btn_viewsource').text()==''){
editorHtml=editorHtml.replace(/</g, "<").replace(/>/g, ">").replace(/ /g, " ");
$(editorSelector + ' ._wangEditor_btn_viewsource').addClass('yuanma-active');
$(editorSelector + ' ._wangEditor_btn_viewsource').text(' ');
}else{
editorHtml=that.nEditor.txt.text().replace(/</ig, "<").replace(/>/ig, ">").replace(/ /ig, " ");
$(editorSelector + ' ._wangEditor_btn_viewsource').removeClass('yuanma-active');
$(editorSelector + ' ._wangEditor_btn_viewsource').text('');
}
that.nEditor.txt.html(editorHtml);
that.nEditor.change && that.nEditor.change();// 更新编辑器的内容
}
$(editorSelector + " .w-e-toolbar").prepend('<div class="w-e-menu"><span class="_wangEditor_btn_viewsource yuanma" href="###"></span></div>');
$(editorSelector).on("click",".yuanma",function(){
toggleViewsource(editorSelector,_this);
});
},
},
}
</script>
<style scoped>
#editor >>> .yuanma{display: inline-block;width: 14px;height: 14px;background: url(/* 此处放置源码按钮灰色图标 */) no-repeat center center;background-size: contain;}
#editor >>> .yuanma-active{display: inline-block;width: 14px;height: 14px;background: url(/* 此处放置源码按钮有色图标 */) no-repeat center center;background-size: contain;}
#editor >>> .yuanma:hover{background: url(/* 此处放置源码按钮有色图标 */) no-repeat center center;background-size: contain;}
</style>
页面中使用的方法:
近公司内部要优化创作者后台,关于后台编辑器部分,领导委托我做了一个简单的调研。
基于github和百度搜索的数据,收集整理出以下几款编辑器的数据
github repository: https://github.com/quilljs/quill/
github stars: 31.1k
github contributors: 127
官网: https://quilljs.com/
Quill是一个跨平台的功能强大的富文本编辑器。开发者可以通过简单的API来控制编辑器的内容。主流的黑白清新风,美观,功能上虽然不是很多,甚至还没有表格,但它的代码高亮是最完美的,因为它本身就支持了hignlight.js,同样支持行内编辑模式,可自定义。
github repository: https://github.com/basecamp/trix
github stars: 16.5k
github contributors: 37
官网: https://trix-editor.org/
在Web应用程序中编写格式精美的文本。Trix是一个WYSIWYG编辑器,用于编写消息,评论,文章和列表 - 大多数Web应用程序的简单文档。它具有复杂的文档模型,支持嵌入式附件,并输出简洁和一致的HTML。
Trix是来自Basecamp的开源项目,Basecamp是Ruby on Rails的创建者。数百万人信任他们的文本发送到Basecamp,Trix给了他们最好的编辑体验。
大多数WYSIWYG编辑器都是HTML contenteditable和execCommandAPI的包装器,他们是由Microsoft设计,支持在Internet Explorer 5.5中实时编辑网页,最终由其他浏览器进行反向设计和复制。
因为这些API从未完全规定好或文档化,并且因为WYSIWYG HTML编辑器的范围很广,所以每个浏览器的实现都有自己的一组错误和怪癖,需要JavaScript开发人员可以解决这些不一致问题。
Trix通过将contenteditable视为I / O设备来回避这些不一致:当输入进入编辑器时,Trix该输入转换为其内部文档模型的编辑操作,然后将该文档重新呈现回编辑器。这使得Trix可以完全控制每次击键后发生的事情,并且完全无需使用execCommand。
github repository: https://github.com/wangeditor-team/wangEditor
github stars: 12.5k
github contributors: 44
官网: https://www.wangeditor.com/
由一个20个人组合的国内团队维护,轻量级,小巧实用,配置方便,使用简单。可以自定义皮肤功能,免费开源,用户数量也较多。
wangEditor是用javascript编写的 轻量级web富文本编辑器 ,依赖于jQuery和fontAwesome字体库, 支持所有浏览器 。使用 wangEditor 可以轻松创建web富文本框,并可以自定义扩展菜单功能。wangEditor所有源码都已经在github上开源下载。
缺点:Demo、文档不是很全。若想更好的使用,还需阅读一下源码,好在作者代码注释写得很全。
优点:作者一直有更新,并且有活跃的技术QQ群: 164999061(人已满),710646022(人已满),901247714(人已满),606602511,作者能及时的回答问题。源码注释非常好。
wangEditor是我们项目目前在用的编辑器。
github repository: https://github.com/ckeditor/
github contributors: 130+74
github stars: 5.5k+5.2k
官网: https://ckeditor.com/
非常经典的富文本编辑器,官方下载量过千万,有高性能的实时预览,它特有行内编辑功能。
CKEditor 是新一代的 FCKeditor,是一个重新开发的版本。CKEditor 是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛地被运用于各大网站。
可配合使用的扩展有文件管理器 KCFinder。
github上,目前拆分为ckeditor4和ckeditor5两个主要仓库分别维护,以上数据分别取自这两个仓库
github repository: https://github.com/summernote/summernote/
github stars: 10.4k
github contributors: 278
官网: https://summernote.org/
Summernote 是一个简单灵活的所见即所得的 HTML 在线编辑器,比较容易上手,使用体验流畅,支持各种主流浏览器,基于 jQuery 和 Bootstrap 构建,支持快捷键操作,提供大量可定制的选项。
本次调研中github贡献者最多的一个。
github repository: https://github.com/tinymce/
github stars: 9.9k
github contributors: 215
官网: https://www.tiny.cloud/
TinyMCE是一个轻量级的,基于浏览器的,所见即所得编辑器,支持目前流行的各种浏览器,支持图片在线处理,插件多,功能非常强大,易于集成,并且拥有可定制的主题。由JavaScript写成。功能配置灵活简单(两行代码就可以将编辑器嵌入网页中),支持AJAX。另一特点是加载速度非常快,如果你的服务器采用的脚本语言是 PHP,那还可以进一步优化。最重要的是,TinyMCE是一个根据LGPL license发布的自由软件,你可以把它用于商业应用。
github repository: https://github.com/fex-team/ueditor
github stars: 6k
github contributors: 45
官网: https://ueditor.baidu.com/website/
UEditor 是由百度 web 前端研发部开发所见即所得富文本web编辑器,具有轻量、可定制、注重用户体验等特点。
代码精简,加载迅速。
全新的分层理念,满足多元化的需求。采用三层架构:
兼容Mozilla, MSIE, FireFox, Maxthon,Safari 和Chrome,实现浏览器无差别化。
经过专业的QA团队测试,通过上千个测试用例,包括自动化用例和手动用例,目前仍然在不断完善中。
以上数据部分均取自github,时间为2021年10月21日
观点部分,只代表个人观点,有什么建议和意见欢迎指正
*请认真填写需求信息,我们会在24小时内与您取得联系。