Vue3写一个后台管理系统(5)富文本编辑器处理方案

2,586 阅读2分钟

25cfc38fb835e407d4512ee52486020.jpg

编辑库选择标准

对于现在的前端编辑库富文本而言,如果仅从功能上来去看的话,那么其实都是相差无几的。随便从 github 中挑选编辑库,只要 star10K(保守些) 以上的,编辑器之上的常用功能一应俱全。富文本我们使用 wangEditor,因为 wangEditor是国产框架、文档越详尽,提供了中文文档(英文好的可以忽略)、更新快速,不像谷歌开源的markdown-here,github上都好多年没更新了。 image.png image.png

所以我们得先去下载 wangEditor

npm i wangeditor@4.7.6
  • 我们这里使用的是4.x版本的,当然你也可以使用最新v5版本的,4.x版本我觉得api使用起来更舒服,因人而异吧。安装完成之后,我们把editor封装成一个组件,代码逻辑和注释如下:
<template>
  <div class="editor-container">
    <div id="editor-box"></div>
    <div class="bottom">
      <el-button type="primary" @click="onSubmitClick">提交</el-button>
    </div>
  </div>
</template>

<script setup>
import E from 'wangeditor'
import { onMounted, defineProps, defineEmits, watch } from 'vue'
import { useStore } from 'vuex'
import conf from "@/utils/config.js";
import {getAdmintorList, publicUploadFile} from "@/api/api";

const props = defineProps({
  title: {
    required: true,
    type: String
  },
  detail: {
    type: Object
  }
})

const emits = defineEmits(['onSuccess'])

const store = useStore()

// Editor实例
let editor
// 处理离开页面切换语言导致 dom 无法被获取
let el
onMounted(() => {
  el = document.querySelector('#editor-box')
  initEditor()
})

const initEditor = () => {
  editor = new E(el)
  editor.config.zIndex = 1
  // 菜单栏提示
  editor.config.showMenuTooltips = true
  editor.config.menuTooltipPosition = 'down'
  // 一次最多上传图片的数量
  editor.config.uploadImgMaxLength = 1;
  // 设置编辑区域高度为 500px
  editor.config.height = 700
  // 限制上传图片格式
  editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];
  editor.config.showLinkImg = false
// 默认情况下,显示所有菜单
  editor.config.menus = [
    'head',
    'bold',
    'fontSize',
    'fontName',
    'italic',
    'underline',
    'strikeThrough',
    'indent',
    'lineHeight',
    'foreColor',
    'backColor',
    'link',
    'list',
    'todo',
    'justify',
    'quote',
    'emoticon',
    'image',
    // 'video',
    'table',
    'code',
    'splitLine',
    'undo',
    'redo',
  ]
  // 自定义上传图片
  editor.config.customUploadImg = (resultFiles, insertImgFn) => {
// resultFiles 是 input 中选中的文件列表
// insertImgFn 是获取图片 url 后,插入到编辑器的方法
    resultFiles.forEach((item, i) => {
      const formData = new FormData()
      formData .append('file', item)
      publicUploadFile(formData)
        .then(res => {
              insertImgFn(res.bizobj.src)
        })
        .catch(err => {
        })

    })
  }







  editor.create()
}

// 编辑相关
watch(
  () => props.detail,
  val => {
    if (val && val.content) {
      editor.txt.html(val.content)
    }
  },
  {
    immediate: true
  }
)

const onSubmitClick = async () => {
  console.log(editor.txt.html())
  editor.txt.html('')
  emits('onSuccess')
}
</script>

<style lang="scss" scoped>
.editor-container {
  .bottom {
    margin-top: 20px;
    text-align: right;
  }
}
</style>


  • 页面中使用
<template>
  <div class="container">
    <editor></editor>
  </div>
</template>

<script setup>
import Editor from '../components/Editor.vue'
import { ref } from 'vue'


</script>

<style lang="scss" scoped>
.container {
}
</style>


效果

image.png

项目地址:github.com/wudengyao/a…

总结

对于大家而言,不一定非要使用我们在文章中使用的这个编辑器库。

因为对于编辑器库而言,它的使用方式都是大同小异的,大家只需要根据我们实际业务需求和使用习惯来选择使用自己当前情况的编辑器库即可