vue使用mavon-editor保存md到数据库,页面回显,二次编辑

28 篇文章 0 订阅
6 篇文章 0 订阅

vue使用mavon-editor保存md到数据库,页面回显,二次编辑

场景:

最近研究markdown的编辑器,使用他进行上传图片,编写文本和代码,并保存到数据库。调用接口查询进行页面回显。保存到数据库的md格式的文本,可重新渲染到编辑器进行二次编辑

效果图:
保存数据库之前:

在这里插入图片描述

保存的东西:

在这里插入图片描述

数据库存了两种结构:

因为发现回显需要的是html结构的,而重新填回编辑器需要的是md格式的
在这里插入图片描述

回显:

用的是html字符格式的,也就是docxContentHtml这个字段
在这里插入图片描述

二次编辑:

用的是md格式的,也就是docxContent这个字段
在这里插入图片描述

代码实现:
<mavon-editor 
  :codeStyle="codeStyle" 
  :toolbars="toolbars" 
  v-model="POSTDocx.input.value.docxContent" 
  :ishljs="true" 
  ref="md" 
  @change="change"
  @imgAdd="handleEditorImgAdd"
  @imgDel="handleEditorImgDel"
/>
 //工具栏
  toolbars: {
    bold: true, // 粗体
    italic: true, // 斜体
    header: true, // 标题
    underline: true, // 下划线
    strikethrough: true, // 中划线
    mark: true, // 标记
    superscript: true, // 上角标
    subscript: true, // 下角标
    quote: true, // 引用
    ol: true, // 有序列表
    ul: true, // 无序列表
    link: true, // 链接
    imagelink: true, // 图片链接
    code: true, // code
    table: true, // 表格
    fullscreen: true, // 全屏编辑
    readmodel: true, // 沉浸式阅读
    htmlcode: true, // 展示html源码
    help: true, // 帮助
    undo: true, // 上一步
    redo: true, // 下一步
    trash: true, // 清空
    save: true, // 保存(触发events中的save事件)
    navigation: true, // 导航目录
    alignleft: true, // 左对齐
    aligncenter: true, // 居中
    alignright: true, // 右对齐
    subfield: true, // 单双栏模式
    preview: true // 预览
  },
  //主题
  codeStyle:'monokai-sublime',
  POSTDocx:{
    input:{
      value:{
        docxTitle:undefined,
        docxBrief:undefined,
        docxContent:`请输入文章正文`,
        categoryId:undefined,
        docxTime:new Date().format('yyyy/MM/dd hh:mm:ss'),
      }
    },
    format_html:undefined,    //这是html结构的用于详情页展示
  },

//这两个就可以分别拿去存到数据库,针对不同场景进行使用
console.log('html格式',this.$refs.md.d_render)   
console.log('md格式',this.$refs.md.d_value)
图片上传:
 //上传图片
  handleEditorImgAdd(place,$file){
    let formdata = new FormData()
    formdata.append('file', $file)
    this.http({
      cmd: 'POSTImgUpload',   //POSTAdminInvitationAdd
      input: formdata,
      success (res) {
        if (res.code === 200) {
          this.$message.success('上传成功')
          let url = res.data.url.replace(/\\/g,"/")
          //第二步.将返回的url替换到文本原位置![...](0) -> ![...](url)  这里是必须要有的
          this.$refs.md.$img2Url(place, window.config.host+url);
        }else{
          this.$message.error(res.msg)
        }
      }
    })
  }
  • 1
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_小郑有点困了

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

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

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

打赏作者

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

抵扣说明:

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

余额充值