vue与tinymce5富文本编辑器的结合使用

TinyMCE是一款易用、且功能强大的所见即所得的富文本编辑器,在移动端还是网页端都运用十分广泛,这篇文章主要讲述的是结合vue项目和tinymce5的使用方法,这个是结合了网上的多篇优质博客加上自己的实践总结得到的一写经验,希望它能帮到一些人解决自己的问题。
其中文参考文档:http://tinymce.ax-z.cn/

插件安装

// 安装 tinymce-vue
npm install @tinymce/tinymce-vue -S
// 安装 tinymce
npm install tinymce -S

当然可能会有些小伙伴在安装的时候一直报错(没错,说得就是我),所以提供以下解决方案:
在package.json文件中的dependencies配置中加入以下内容:

 "dependencies": {
    "@tinymce/tinymce-vue": "^2.0.0",
    "tinymce": "^5.0.3",
  }

再去执行 npm install 即可

使用前操作

原因:安装的依赖包里面默认么有lang这个文件夹包,所以需要手动下载,而skin的放置则是因为tinymce富文本器一定要去skin这个配置正确才可以显示出。 放置在static是保证在项目打包是也可以找到配置文件

结合官方文档封装tinymce组件

<template>
  <div class="tinymce-editor">
    <editor
      v-model="myValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick"
    >
    </editor>
  </div>
</template>
<script>
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/themes/silver/theme";
// 编辑器插件plugins
// 更多插件参考:https://www.tiny.cloud/docs/plugins/
import "tinymce/plugins/image"; // 插入上传图片插件
import "tinymce/plugins/table"; // 插入表格插件
import "tinymce/plugins/lists"; // 列表插件
import "tinymce/plugins/wordcount"; // 字数统计插件
export default {
  components: {
    Editor
  },
  props: {

    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 media table | removeformat"
    }
  },
  data() {
    return {
      init: {
        language_url: "/static/tinymce/langs/zh_CN.js",
        language: "zh_CN",
        convert_urls:false ,
        skin_url: "/static/tinymce/skins/ui/oxide",
        content_css: "/static/tinymce/skins/content/default/content.css",
        height: 300,
        plugins: this.plugins,
        toolbar: this.toolbar,
        branding: false,
        menubar: false,
        // 如需ajax上传可参考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
        images_upload_handler: (blobInfo, success, failure) => {
          var xhr, formData;
          xhr = new XMLHttpRequest();
          xhr.withCredentials = false;
          xhr.open("POST", "上传文件的地址");
          xhr.onload = function() {
            var json;
            if (xhr.status != 200) {
              failure("HTTP Error: " + xhr.status);
              return;
            }
            json = JSON.parse(xhr.responseText);
            // this.imgsUrl[this.imgsUrl.length - 1] = json["data"];
            success(json["data"]);   
          };
          formData = new FormData();
          formData.append("file", blobInfo.blob(), blobInfo.filename());
          xhr.send(formData);
        }
      },
      myValue: this.value
    };
  },
  mounted() {
    tinymce.init({});
  },
  methods: {
    // 添加相关的事件,可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
    // 需要什么事件可以自己增加
    onClick(e) {
      this.$emit("onClick", e, tinymce);
    },
    // 可以添加一些自己的自定义事件,如清空内容
    clear() {
      this.myValue = "";
    }
  },
  watch: {
    value(newValue) {
      this.myValue = newValue;
    },
    myValue(newValue) {
      this.$emit("input", newValue);
    }
  }
};
</script>

vue引用组件

<template>
     <tinymce-editor
            ref="editor"
            v-model="content"
          >
          </tinymce-editor>
</template>
<script>
import TinymceEditor from "../commment/tinymce-editor";
export default {
  components: {
    TinymceEditor
  },
  data() {
    return {
      content: "",
   }
 }
}
</script>

注意:上传图片时需要实现本地上传,只要编写了upload_handler函数即可出现本地上传图片的功能,根据服务器响应后返回的数据,需要在success(data.imgUrl)发送返回的地址才可以正确渲染到富文本编辑器中

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容