1. 程式人生 > 其它 >vue專案引入TinyMCE

vue專案引入TinyMCE

1.安裝

npm install @tinymce/[email protected] -S

2.配置

<template>
  <!-- 富文字 -->
  <div id="tied">
    <Editor :id="id" :init="init" v-model="content" ></Editor>
  </div>
</template>
 
 
<script>
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/icons/default/icons";
import "tinymce/themes/silver";
import '../../../public/tinymce/skins/ui/oxide/skin.css'  //解決引入後不顯示的問題,地址更改為自己的專案目錄找到這個檔案

import "tinymce/plugins/image";
import "tinymce/plugins/media";
import "tinymce/plugins/table";
import "tinymce/plugins/lists";
import "tinymce/plugins/contextmenu";
import "tinymce/plugins/wordcount";
import "tinymce/plugins/colorpicker";
import "tinymce/plugins/textcolor";
import "tinymce/plugins/preview";
import "tinymce/plugins/code";
import "tinymce/plugins/link";
import "tinymce/plugins/advlist";
import "tinymce/plugins/codesample";
import "tinymce/plugins/hr";
import "tinymce/plugins/fullscreen";
import "tinymce/plugins/textpattern";
import "tinymce/plugins/searchreplace";
import "tinymce/plugins/autolink";
import "tinymce/plugins/directionality";
import "tinymce/plugins/visualblocks";
import "tinymce/plugins/visualchars";
import "tinymce/plugins/template";
import "tinymce/plugins/charmap";
import "tinymce/plugins/nonbreaking";
import "tinymce/plugins/insertdatetime";
import "tinymce/plugins/imagetools";
import "tinymce/plugins/autosave";
import "tinymce/plugins/autoresize";


 
export default {
  components: {
    Editor
  },
 
  props: {
     id: {
      type: String,
      default: 'tinymceEditor'
    },
    value: {
      type: String,
      default: ""
    },
    disabled: {
      type: Boolean,
      default: false
    },
    plugins: {
      type: [String, Array],
      default:
        "preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template code codesample table charmap hr nonbreaking insertdatetime advlist lists wordcount imagetools textpattern autosave autoresize"
    },
    toolbar: {
      type: [String, Array],
      default:
        "code undo redo restoredraft | cut copy paste pastetext | forecolor backcolor bold italic underline strikethrough link codesample | alignleft aligncenter alignright alignjustify outdent indent formatpainter | \
    styleselect formatselect fontselect fontsizeselect | bullist numlist | blockquote subscript superscript removeformat | \
    table image media charmap hr pagebreak insertdatetime | fullscreen preview"
    },
  },
  data() {
    return {
      //初始化配置
      init: {
        menubar:false,
        selector: 'Editor',
       // menubar: ture, // 選單欄顯隱
        //language_url: "/public/tinymce/langs/zh_CN.js",
        language_url: '/tinymce/langs/zh_CN.js', // vue-cli2.x
        language: "zh_CN",
        //skin_url: "/pulic/tinymce/skins/ui/oxide",
        skin_url: '../../../public/tinymce/skins/ui/oxide', // vue-cli2.x
        content_css: '../../../public/tinymce/skins/content/default/content.css',// vue-cli2.x
        height: 800,
        width:1050,
        min_height: 800,
        max_height: 2070,
        toolbar_mode: "wrap",
        plugins: this.plugins,
        toolbar: this.toolbar,
        content_style: "p {margin: 5px 0;}",
        fontsize_formats: "12px 14px 16px 18px 24px 36px 48px 56px 72px",
        font_formats:
          "微軟雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;蘋果蘋方=PingFang SC,Microsoft YaHei,sans-serif;宋體=simsun,serif;仿宋體=FangSong,serif;黑體=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;",
        branding: false,
        // 圖片上傳
        images_upload_handler: (blobInfo, success, failure) => {
          // const img = 'data:image/jpeg;base64,' + blobInfo.base64()
          // success(img)
          if(blobInfo.blob().size/1024/1024>2){
                failure("上傳失敗,圖片大小請控制在 2M 以內")
          }else{
                let params=new FormData()
                params.append('file',blobInfo.blob())
                let config={
                      headers:{
                            "Content-Type":"multipart/form-data"
                      }
                }
                this.axios.post(`/article/upload`,params,config).then(res=>{
                    success(res.data)
                }).catch(()=>{
                    failure("上傳出錯,伺服器開小差了呢")
                })
          }
        }
      },
      content: this.value
    };
  },
  mounted() {
    tinymce.init({});
  },
  methods: {
    
  },
  watch: {
    value(newValue) {
      this.content = newValue;
    },
    content(newValue) {
      this.$emit("input", newValue);
    }
  }
};
</script>
  <style scoped lang="scss">
</style>