vue項目中推薦tinymce


之前在開發中也用過其它的編輯器,但是覺得不比tinymce功能多,而且不依賴后端,比如圖片的上傳,圖片大小可調整等,保留復制時的樣式等,具體使用方法如下:

一.安裝tinymce資源 npm install @tinymce/tinymce-vue -S

tinymce 默認是英文界面,所以還需要下載一個中文語言包,在腳手架Vue-cli3下需要把語言包放在public下,我的做法時在public目錄下新建一個tinymce文件夾,然后把語言包放在tinnymce文件夾里,

二. 把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";
// 編輯器插件plugins
// 更多插件參考:https://www.tiny.cloud/docs/plugins/
import "tinymce/plugins/lists";
import "tinymce/plugins/image"; // 插入上傳圖片插件
import "tinymce/plugins/imagetools";
import "tinymce/plugins/table"; // 插入表格插件
import "tinymce/plugins/lists"; // 列表插件
import "tinymce/plugins/emoticons"; //表情插件
import "tinymce/plugins/link"; //插入鏈接
import "tinymce/plugins/fullscreen";
import "tinymce/plugins/anchor";
// import "tinymce/plugins/autosave"
import "tinymce/plugins/autoresize"; //內容自適應插件
import {getUrl2} from '../utils/request.js'
export default {
  components: {
    Editor
  },
  props: {
    value: {
      type: String,
      default: ""
    },
    // 基本路徑,默認為空根目錄,如果你的項目發布后的地址為目錄形式,
    // 即abc.com/tinymce,baseUrl需要配置成tinymce,不然發布后資源會找不到
    baseUrl: {
      type: String,
      default: ""
    },
    disabled: {
      type: Boolean,
      default: false
    },
    plugins: {
      type: [String, Array],
      default:
        "lists  image imagetools   link  fullscreen anchor autoresize"
    },
    toolbar: {
      type: [String, Array],
      default:
        "bold italic forecolor  backcolor | styleselect|  bullist numlist  | lists image link   | removeformat|fullscreen"
    }
  },
  data() {
    return {
      init: {
        language_url: "/tinymce/langs/zh_CN.js",
        language: "zh_CN",
        skin_url: "/tinymce/skins/ui/oxide",
        emoticons_database_url: "/tinymce/emoticons/js/emojis.js",
        content_css: "/tinymce/skins/content/default/content.min.css",
        // skin_url: `${this.baseUrl}/tinymce/skins/ui/oxide-dark`, // 暗色系
        // content_css: `${this.baseUrl}/tinymce/skins/content/dark/content.css`, // 暗色系
        statusbar: false, // 隱藏編輯器底部的狀態欄
        selector: "textarea", // change this value according to your HTML
        plugins: this.plugins,
        min_height: 240,
        toolbar: this.toolbar,
        branding: false,
        menubar: false,
        // 此處為圖片上傳處理函數,這個直接用了base64的圖片形式上傳圖片,
        // 如需ajax上傳可參考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
        images_upload_handler: (blobInfo, success, failure) => {
          //const img = "data:image/jpeg;base64," + blobInfo.base64();
          // success(img);
          var xhr, formData;
          xhr = new XMLHttpRequest();
          xhr.withCredentials = false;
          xhr.open("POST", `${getUrl2()}/comment/uploadFtp`);
          xhr.setRequestHeader("Authorization",localStorage.getItem("Authorization"));  //設置請求頭
          xhr.onload = function() {
            var json;
            if (xhr.status != 200) {
              failure("HTTP Error: " + xhr.status);
              return;
            }
            json = JSON.parse(xhr.responseText);
            if (!json || typeof json.object!= "string") {
              failure("Invalid JSON: " + xhr.responseText);
              return;
            }
            success(json.object);
          };
          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 = "";
    },
    onChage(e) {},
    async uploadImg(FormData) {
      let res = await this.$api2.upload_img(FormData);
    }
  },
  watch: {
    value(newValue) {
      this.myValue = newValue;
    },
    myValue(newValue) {
      this.$emit("input", newValue);
    }
  }
};
</script>

三.效果圖如下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM