vue + ElementUI配合使用富文本编辑器 —— mavon-editor篇

简介

mavon-editor基于vue的markdown编辑器,比较适合博客系统 ,相对比较简洁,功能相对单一。插入的图片无法直接更改大小,创建表格很麻烦,但基本的markdown样式还是有的

开启mavon-editor之旅

安装mavon-editor

cnpm install mavon-editor --save

在main.js中全局注册

import Vue from 'vue'
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
Vue.use(mavonEditor)

页面中使用该组件

<mavon-editor ref=md style="height: 400px" v-model="desc" @imgAdd="$imgAdd" @imgDel="$imgDel"></mavon-editor>

图片插入与回显,在此过程中我加入图片高度的设置,可手动修改图片高度

$imgAdd (pos, $file) {
      this.$prompt('请设置图片高度(可更改)', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputErrorMessage: '输入不能为空',
        inputValue: '500',
        inputValidator: (value) => {
          if (!value) {
            return '输入不能为空'
          }
        }
      }).then(({ value }) => {
        // 第一步.将图片上传到服务器.
        // const _this = this
        const multipartFile = new FormData()
        console.log('名字', this.addForm1.name)
        if (this.addForm1.name === '' || this.addForm1.name === 'undefined') {
          multipartFile.append('name', this.realname)
        } else {
          multipartFile.append('name', this.addForm1.name)
        }
        console.log(value)
        const height = value
        multipartFile.append('height', height)
        console.log('长度', height)
        multipartFile.append('img', $file)
        this.$http.post('imgs/', multipartFile)
          .then((res) => {
            console.log(res)
            this.url = res.data.data.img
            this.urlsplit = (this.url).split('/')
            this.picname = this.urlsplit[this.urlsplit.length - 1]
            this.picnameURL = this.picname
            console.log('配置name', this.picnameURL)
            console.log('url', this.url)
            console.log('imagId', res.data.data.id)
            // 第二步.将返回的url替换到文本原位置![...](0) -> ![...](url)
            /**
             * $vm 指为mavonEditor实例,可以通过如下两种方式获取
             * 1. 通过引入对象获取: `import {mavonEditor} from ...` 等方式引入后,`$vm`为`mavonEditor`
             * 2. 通过$refs获取: html声明ref : `<mavon-editor ref=md ></mavon-editor>,`$vm`为 `this.$refs.md`
             */
            //  $vm.$img2Url(pos, url);
            // console.log('上传图片了哦')
            // console.log(res)
            // console.log('res==', res.results.data.image_url[0])
            this.$refs.md.$img2Url(pos, this.url)
          })
      }).catch(() => {
        this.$message.error('上传图片失败,请删除左侧图片信息后重试!')
        this.$refs.md.$img2Url(pos, '该数据无效,请删除该条数据重新上传图片!')
      })
    },

图片的删除

$imgDel (pos) {
      delete this.img_file[pos]
},

以上就是mavon-editor的内容,后续会更新功能更丰富的tinymce组件 vue + ElementUI配合使用富文本编辑器 —— tinymce篇

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃花椒的小喵酱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值