vue中使用tinymce富文本

安装
方式一:
npm install @tinymce/tinymce-vue -S
npm install tinymce -S
方式二:

将下面的配置粘到package.json文件dependencies中执行npm install

"@tinymce/tinymce-vue": "^2.0.0",
"tinymce": "^5.0.3"
下载中文包

tinymce官网

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OZ5wMcCq-1614070815651)(C:\Users\86187\AppData\Roaming\Typora\typora-user-images\image-20210223164438778.png)]

使用

public目录下新建tinymce目录,将中文包skins目录(node_modules->tinymce->skins)放到该目录
在这里插入图片描述
简单封装成了组件

<template>
  <div>
    <editor id="TinymceEditor" 
    v-model="editorValue" 
    :init="init" 
    :disabled="disabled"
    :key="editorKey">
    </editor>
  </div>
</template>
<script>
// 富文本
import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver'  // 主题
import 'tinymce/icons/default/icons.js'  // 图标
// 富文本插件
import 'tinymce/plugins/image'// 插入上传图片插件
import 'tinymce/plugins/table'// 插入表格插件
import 'tinymce/plugins/lists'// 列表插件
import 'tinymce/plugins/wordcount'// 字数统计插件
export default {
  name:'TinymceEditor',
  props:{
    value: {
      type: String,
      default: ''
    },
    // 是否禁止编辑
    disabled: {
      type: Boolean,
      default: false
    },
    plugins: {
      type: [String, Array],
      default: 'lists image table wordcount'
    },
    toolbar: {
      type: [String, Array],
      default: 'undo redo |  formatselect | bold italic forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image table | removeformat'
    }
  },
  components: {
    Editor
  }, 
  data(){
    return {
      editorKey: 1,
      editorValue: this.value,
      init: {}
    }
  },
  created() {
    this.init = {
      language_url: `${window.global_url.BASE_URL}/tinymce/langs/zh_CN.js`, //语言包的路径
      language: 'zh_CN', //语言
      skin_url: `${window.global_url.BASE_URL}/tinymce/skins/ui/oxide`, //skin路径
      height: document.body.offsetHeight - 350, //编辑器高度
      menubar: false, //顶部菜单栏显示
      browser_spellcheck: true, // 拼写检查
      branding: false, // 去水印
      statusbar: true, // 隐藏编辑器底部的状态栏
      paste_data_images: true, // 允许粘贴图像
      plugins: this.plugins,
      toolbar: this.toolbar,
      images_upload_handler: (blobInfo, success, failure) => {
        let base64 = "data:image/png;base64," + blobInfo.base64();
      //编辑器的上传图片内容被处理为<img src="success方法里面的参数" />
        success(base64);
      }
    }
  },
  activated () {
    this.editorKey++
  },
  watch: {
    value(newVal) {
      this.editorValue = newVal
    },
    editorValue(newVal) {
      this.$emit('input', newVal)
    }
  }
}
</script>
<style lang="scss">
// 处理富文本下拉框在dialog中被挡住的问题
.tox-tinymce-aux{
  z-index: 3000 !important;
}
</style>

使用

// html
<tinymce-editor 
  v-model="data" 
  :disabled="disabled"
  @input="editorValueChange">
</tinymce-editor>
// js
 methods: {
    // 监听编辑器数据
    editorValueChange(val) {
      this.data = val
    }
 }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值