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

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

作者: 程序员三千_ | 来源:发表于2023-03-22 17:20 被阅读0次

编辑库选择标准

对于现在的前端编辑库富文本而言,如果仅从功能上来去看的话,那么其实都是相差无几的。随便从 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

总结

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

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

相关文章

  • 2020-04-20

    1.下载xadmin后台管理以及DjangoUeditor富文本编辑器 2.给4个app分别配置后台管理系统 ap...

  • wangEditor3的简单使用(包括配置菜单、颜色、表情、字体

    最近想做一个新闻后台管理系统,需要富文本编辑器编辑新闻内容然后上传,查询了许多的编辑器,最后选择wangEd...

  • CKEditor富文本编辑器

    前言 后台管理系统是电商系统不可或缺的部分。而富文本编辑器在电商后台里又是非常常见的一个功能。 自从前后端分离成为...

  • FastAdmin-CMS模版制作(2)-系统插件配置

    一、富文本编辑器 1.进入后台,插件管理=>编辑器;选择项目合适的富文本编辑器进行安装,安装完成之后一定要清除浏览...

  • Tinymce的使用遇到的坑

    一、 网上有很多的富文本编辑器,我用的这个是从一个后台管理系统的模板里面(https://github.com/P...

  • 改造百度ueditor

    背景 富文本编辑是管理后台(cms)系统中的重要功能,编辑器的选择也非常多,如今大多编辑器都是走的简约路线,遇上挑...

  • 富文本编辑器-1-选型

    现有问题 团队的业务多为后台管理系统,部分业务需要使用富文本编辑器。早期团队选用了百度编辑器,但存在bug多、无人...

  • vue-quill-editor富文本编辑器基本的使用与步骤

    一、背景 最近正在开发一个后台管理系统,公司希望用户可以随时发布一些新闻、活动之类的,所以,需要一个富文本框编辑器...

  • jquery的富文本编辑器在react中的使用

    我写后台系统会遇到富文本编辑器,下面就是介绍怎么在react中实现这个功能。 首先,打开网址https://sim...

  • vue使用wangEditor编辑器

    最近写后台系统,有编辑器的要求,我在网上看了半天大概这几种知名开源富文本编辑器记录和对比(仅供参考) 1、UEdi...

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/rzcbrdtx.html