wangeditor富文本编辑器的使用

news/2024/10/23 7:38:37/

官方文档: http://www.wangeditor.com/

1. 引入编辑器

npm install wangeditor

2. 使用编辑器

(1) 创建容器

<div id="wangeditor"><div ref="editorElem"></div>
</div>

(2) 创建并且实例化组件

  vue的使用方法

//vue的使用
import E from "wangeditor”;//导入组件
// 相当于js的变量设置
data() {
return {editor: null,
}}
//methods里创建调用、或是mounted里面直接生产
mounted(){this.editor = new E(this.$refs.editorElem);//获取组件并构造编辑器this.editor.create(); // 创建富文本实例
}

3. 配置菜单

// 这是默认的菜单配置就是全部的功能、不需要的话将其去掉即可.

mounted(){this.editor.customConfig.menus = ['head',  // 标题'bold',  // 粗体'fontSize',  // 字号'fontName',  // 字体'italic',  // 斜体'underline',  // 下划线'strikeThrough',  // 删除线'foreColor',  // 文字颜色'backColor',  // 背景颜色'link',  // 插入链接'list',  // 列表'justify',  // 对齐方式'quote',  // 引用'emoticon',  // 表情'image',  // 插入图片'table',  // 表格'video',  // 插入视频'code',  // 插入代码'undo',  // 撤销'redo'  // 重复] 
}

// 自定义设置

mounted(){// 自定义颜色this.editor.customConfig.colors = ["#000000","#333333",];// 自定义字体this.editor.customConfig.fontNames = ["PingFangSC",];//配置多种语言--就是将编辑器原本文字配置成你需要的文字、//***链接文字一定要在链接的上面this.editor.customConfig.lang = {'设置标题': 'title','正文': 'p','链接文字': 'link text','链接': 'link','上传图片': 'upload image','上传': 'upload','创建': 'init'// 还可自定添加更多
}}

4. 常用功能设置

//设置编辑器内容
this.editor.txt.html(“yanyanyan”);
// 编辑器的事件,每次改变会获取其html内容(html内容是带标签的)
this.editor.customConfig.onchange = html => {
_this.formValidate.content = html;
};
// 设置编辑器层级
this.editor.customConfig.zIndex = 10;
// 去除复制过来文本的默认样式
this.editor.customConfig.pasteFilterStyle = false;
//用户点击富文本区域会触发onfocus函数执行
this.editor.customConfig.onfocus = function () {console.log("onfocus")}
// 将图片大小限制为 3M
this.editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024
// 限制一次最多上传 5 张图片
this.editor.customConfig.uploadImgMaxLength = 5
//上传图片的错误提示默认使用alert弹出,也可以自定义用户体验更好的提示方式
editor.customConfig.customAlert = function (info) {// info 是需要提示的内容alert('自定义提示:' + info)
}

5. 关于本地上传图片

编辑器自带图片上传的上传的网站链接图片

本地上传图片需要自己设置

// 上传图片到服务器,对应的是controller层的@RequestMapping("/upload")
this.editor.customConfig.uploadImgServer = "/api/file/upload”;//接口名称
//自定义name,接收的时候图片文件的那么用这个,对应的是参数中的MultipartFile upimg名称,这个名称即上传到浏览器的参数名称
this.editor.customConfig.uploadFileName = "file_key”;//这个需要和后台商量上传图片的名称
// 上传图片的结果反馈
this.editor.customConfig.uploadImgHooks = {
before: function(xhr, editor, files) {
// 图片上传之前触发
// xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,files 是选择的图片文件
// 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
// return {
// prevent: true,
// msg: '放弃上传'
// }
// console.log("before:",xhr)
},
success: function(xhr, editor, result) {
// 图片上传并返回结果,图片插入成功之后触发
// xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
// console.log("success:",result)
},
fail: function(xhr, editor, result) {
// 图片上传并返回结果,但图片插入错误时触发
// xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
},
error: function(xhr, editor) {
// 图片上传出错时触发
// xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
},
// 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
// (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
customInsert: function(insertImg, result, editor) {
// 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
// insertImg 是插入图片的函数,参数editor 是编辑器对象,result 是服务器端返回的结果
// 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
var url = result.result.remote_path;
insertImg(url);
// result 必须是一个 JSON 格式字符串!!!否则报错
}
};
// }

6. 其他

常用 API
属性
* 获取编辑器的唯一标识 editor.id
* 获取编辑区域 DOM 节点 editor.$textElem[0]
* 获取菜单栏 DOM 节点 editor.$toolbarElem[0]
* 获取编辑器配置信息 editor.config
* 获取编辑区域 DOM 节点 ID editor.textElemId
* 获取菜单栏 DOM 节点 ID editor.toolbarElemId
* 获取菜单栏中“图片”菜单的 DOM 节点 ID editor.imgMenuId
方法
选取操作
* 获取选中的文字 editor.selection.getSelectionText()
* 获取选取所在的 DOM 节点 editor.selection.getSelectionContainerElem()[0]* 开始节点 editor.selection.getSelectionStartElem()[0]* 结束节点 editor.selection.getSelectionEndElem()[0]
* 折叠选取 editor.selection.collapseRange()
* 更多可参见源码中定义的方法
编辑内容操作
* 插入 HTML editor.cmd.do('insertHTML', '<p>...</p>')
* 可通过editor.cmd.do(name, value)来执行document.execCommand(name, false, value)的操作

问题解决: 

1、标题样式、字体样式不生效 等样式问题

原因:设置的全局样式导致样式失效
解决:重新对编辑器的样式进行设置优先级高于全局即可
2、编辑器实例化不成功报错

解决:使用定时器变成异步操作即可
3、图片上传,复制的内容没有自动添加到服务器

需要自动识别添加图片并上传或者是和由后台自动获取保存

其他: 

  • wangEditor5 新版已经正式发布了 ,大概看了下官方文档(最上方有链接),新手超级友好,也更加适应框架特性,还有相应的react和vue组件
  • react组件 @wangeditor/editor-for-react、vue组件 @wangeditor/editor-for-vue
  • 新编辑器加了两种mode,default 默认模式 - 集成了 wangEditor 所有功能,simple 简洁模式 - 仅有部分常见功能,简洁易用;这是官方示例demo,wangeditor5 的问题官方合集,感兴趣可以看看~

 


http://www.ppmy.cn/news/651683.html

相关文章

Vue3 富文本编辑器插件wangEditor

先上Demo 最近笔者在Vue3开发过程中需要使用到富文本编辑器&#xff0c;最后找到这个插件&#xff0c;觉得挺不错&#xff0c;如果有开发者也有这样的需求&#xff0c;可以试试 先上demo: Vue3使用方法 安装 yarn add wangeditor/editor # 或者 npm install wangeditor/edi…

wangEditor的完整使用(二)

上一篇博客讲了wangEditor的使用&#xff0c;但是有一个问题就是在富文 本编辑器里编辑时不能看到文章的预览效果。 下面我来讲解下如何实现在编辑时也可以看到预览效果。 设置一个按钮 <div class"layui-inline layui-show-xs-block"><button type&quo…

wangEditor 富文本编辑器使用

wangEditor 富文本编辑器使用 框架&#xff1a; react hooks 链接: 官方文档参考 富文本编辑器组件 import wangeditor/editor/dist/css/style.css; // 引入 css import { useState, useEffect } from react; import { Editor, Toolbar } from wangeditor/editor-for-react;…

vue 中使用 wangeditor

项目中用到了富文本&#xff0c;选来选去选择了wangeditor,先写了demo&#xff0c;用起来还算比较简单 用法 安装 npm install wangeditor/editor --save npm install wangeditor/editor-for-vue --save 空白编辑器 <template><div id"app"><div…

wangEditor的使用及上传图片(一)

由于业务需要&#xff0c;最近新入手了一款富文本编辑器wangEditor&#xff0c;这是一款轻量级的富文本编辑器&#xff0c;比起百度的ueditor&#xff0c;这款编辑器的界面更加简单&#xff0c;文档也很详细。对于需求不是很高的功能来说&#xff0c;这款编辑器实在是不二之选。…

wangEditor的完整使用(一)

最近在做一个类似博客之类的页面&#xff0c;所以就打算借助wangEditor这一富文本编辑器来实现项目的需求。主要使用SpringBootJSPLayUi来完成。一.导入wangEditor.js 我自己使用的是wangEditor.min.js,通过手工导入的方式放在项目的静态文件下来引用。 下载地址&#xff1a;…

对wangeditor进行扩展---- 源代码

看到有人对我做的WangEditor比较感兴趣&#xff0c;问了一些问题。但由于我并不常来&#xff0c;所以就没能及时答复&#xff0c;抱歉了。 未避免以后类似问题发生&#xff0c;我将我修改的wangeditor.js直接发在这里&#xff0c;有兴趣的可以下载后自己分析。希望能帮到需要的…

vue 使用 wangeditor 富文本编辑器

wangeditor 是一个轻量级 web 富文本编辑器&#xff0c;配置方便&#xff0c;使用简单。 1&#xff09;安装 wangeditor 终端安装 wangeditor 库&#xff1a; yarn add wangeditor/editor # 或者 npm install wangeditor/editor --save2&#xff09;页面绑定 创建一个 xxx.…