当前位置:   article > 正文

vue项目:集成富文本编辑器 - 百度ueditor(vue-ueditor-wrap)

vue-ueditor-wrap

一、背景

集成百度ueditor,实现图文编辑

二、项目介绍

vue2-nuxt2项目

vue项目:ueditor(vue-ueditor-wrap)

三、集成步骤

3.1、下载富文本编辑器

GitHub - fex-team/ueditor: rich text 富文本编辑器

3.2、下载后放在static目录下

 3.3、vue项目 安装插件vue-ueditor-wrap

yarn add vue-ueditor-wrap

3.4、创建配置文件

  1. import cookie from 'js-cookie'
  2. export const a = 1
  3. // 百度富文本配置项
  4. export const ueditorConfig = {
  5. //是否开启字数统计
  6. wordCount:true,
  7. //允许输入的最大字符数
  8. maximumWords:50000,
  9. // 编辑器不自动被内容撑高
  10. autoHeightEnabled: false,
  11. // 初始容器高度
  12. initialFrameHeight: 240,
  13. // 初始容器宽度
  14. initialFrameWidth: '100%',
  15. // 上传文件接口
  16. serverUrl: 'XXXXX/api/v1/files/upload',
  17. // UEditor 资源文件的存放路径,通常Nuxt项目设置为/UEditor/即可
  18. // UEDITOR_HOME_URL: `/UEditor/`,
  19. UEDITOR_HOME_URL: `UEditor/`,
  20. // 配合最新编译的资源文件,你可以实现添加自定义Request Headers,详情https://github.com/HaoChuan9421/ueditor/commits/dev-1.4.3.3
  21. headers: {
  22. Authorization: `Bearer ${cookie.get('token')}`,
  23. tenantId: cookie.get('tenantId')
  24. },
  25. readonly: false,
  26. focus: true
  27. }

四、使用步骤

4.1、在开发的业务文件引入vue-ueditor-wrap、配置文件

 import VueUeditorWrap from 'vue-ueditor-wrap';

import { ueditorConfig } from '../ueditor/index';

 4.2、使用

  1. <vue-ueditor-wrap
  2. ref="ueditor_v"
  3. v-model="msg"
  4. class="preview"
  5. :config="config"
  6. @ready="ready"
  7. />

4.3、数据、配置 

 ready()

  1. // 可以在ready方法中拿到editorInstance实例,所有API和官方的实例是一样了。http://fex.baidu.com/ueditor/#api-common
  2. ready(editorInstance) {
  3. console.log(`实例${editorInstance.key}已经初始化:`, editorInstance);
  4. },

 四、配置插入图片

4.1、ueditor.config.js

  1. imageActionName: 'uploadimage',
  2. imageAllowFiles: [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
  3. imageUrlPrefix: "", /* 图片访问路径前缀 */
  4. imagePathFormat: "/editor/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */

4.2、上文3.4配置serverurl,文件上传地址

4.3、上传接口返回数据结构应为:

  1. {
  2. state: 'SUCCESS'
  3. url: 'https://123.png'
  4. }

五、 至此配置完毕,可以进行编辑、插入图片。

六、设置禁止编辑

6.1、需求

内容区域设置禁止编辑

 6.2、实现思路(一)

6.2.1、通过编辑器提供的方法进行配置,没找到方法

6.2.2、通过JS去设置

6.2.2.1、打开元素后发现是一个iframe,那么我们首先获取iframe

 document.getElementById('ueditor_0')

6.2.2.2、获取iframe内body

document.getElementById('ueditor_0').contentWindow.document.getElementsByTagName('body')[0]

6.2.2.3、body上属性contentEditable显示为true,,设置为false即可禁止编辑

document.getElementById('ueditor_0').contentWindow.document.getElementsByTagName('body')[0].contentEditable = false

6.2.2.4、在ready方法调用(递归)方法,同时处理ueditor编辑器id后最不为0的情况

  1. // 可以在ready方法中拿到editorInstance实例,所有API和官方的实例是一样了。http://fex.baidu.com/ueditor/#api-common
  2. ready(editorInstance) {
  3. console.log(`实例${editorInstance.key}已经初始化:`, editorInstance);
  4. // 不是编辑状态,禁用编辑,隐藏工具栏
  5. this.isEditUeditor();
  6. },
  7. isEditUeditor() {
  8. if (!this.edit && document.getElementById(`ueditor_${this.editIndex}`)) {
  9. document.getElementById('edui1_toolbarbox').style.display = 'none';
  10. document
  11. .getElementById('ueditor_0')
  12. .contentWindow.document.getElementsByTagName(
  13. 'body'
  14. )[0].contentEditable = false;
  15. }
  16. // ueditor_0 通常id后缀为0,处理不为0的情况
  17. if (
  18. !this.edit &&
  19. !document.getElementById(`ueditor_${this.editIndex}`) &&
  20. this.editIndex < 20
  21. ) {
  22. this.editIndex += 1;
  23. this.isEditUeditor();
  24. }
  25. },

6.2.2.5、效果

6.3、实现思路(二)

通过组件的ref属性,逐层找到contentEditable,设置为false

 

  1. <vue-ueditor-wrap
  2. ref="ueditor_v"
  3. v-model="addContentForm.articleContent"
  4. class="preview"
  5. :config="config"
  6. @ready="ready"
  7. />
  1. ready(editorInstance) {
  2. console.log(`实例${editorInstance.key}已经初始化:`, editorInstance);
  3. // 不是编辑状态,禁用编辑,隐藏工具栏
  4. console.log('377this.$refs.ueditor_v', this.$refs.ueditor_v)
  5. this.$refs.ueditor_v.editor.body.contentEditable = false
  6. document.getElementById('edui1_toolbarbox').style.display = 'none';
  7. },

 同样可以实现,效果如图:

七、过程记录

7.1、解决section标签没有style、class的问题,否则加了样式也不能生效

section:['class', 'style'],

经过测试,成功。

八、欢迎关注、点赞、评论、交流指正。

vue+vue-ueditor-wrap+秀米 - 简书

百度富文本框上传图片路径前缀配置问题 - 锋齐叶落 - 博客园

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/733904
推荐阅读
相关标签
  

闽ICP备14008679号