vue3的富文本編輯器-Tinymce

記錄tinymce的使用方法,本地代理模式的。圖片上傳到自己的服務(wù)器。中文版的??梢钥吹皆创a的,并且可以編輯的

1.下載https://github.com/tinymce/tinymce
2.將下載tinymce-main.zip放到自己的vue的public目錄下
3.安裝依賴包 npm install @tinymce/tinymce-vue 
本頁面可以直接拿去用,修改下上傳地址為自己的就可以。我是將這個(gè)頁面做成了組件tinyEditor/index.vue

<template>
  <div class="tiny-editor-container">
    <Editor
      tinymce-script-src="/tinymce/tinymce.min.js"
      license-key="gpl"
      v-model="content"
      :init="initOptions"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, watch, computed } from 'vue';
import Editor from '@tinymce/tinymce-vue';
import axios from 'axios';
import { uploadUrl } from "@/config/base"; //上傳地址
import { SPContants } from '@/config/sp_contants'; //我記錄token的常量
import { getImgUrl } from '@/utils/tool_utils';//顯示圖片地址的(我的展示和上傳不是第一個(gè)地址。你們是同一個(gè)的忽略)

const props = defineProps({
  modelValue: {
    type: String,
    default: '',
  },
  height: {
    type: [Number, String],
    default: 500,
  },
  plugins: {
    type: [String, Array],
    default: () => [
      'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'preview', 'anchor', 'searchreplace', 'visualblocks', 'code', 'fullscreen', 'insertdatetime', 'media', 'table', 'help', 'wordcount', 'emoticons'
    ],
  },
  toolbar: {
    type: String,
    default: 'undo redo | blocks fontfamily fontsize | bold italic underline strikethrough | align left center right justify | bullist numlist outdent indent | link image media table | emoticons charmap | removeformat preview code fullscreen',
  }
});

const emit = defineEmits(['update:modelValue']);

const content = ref(props.modelValue);

watch(() => props.modelValue, (newValue) => {
  if (newValue !== content.value) {
    content.value = newValue;
  }
});

watch(content, (newValue) => {
  emit('update:modelValue', newValue);
});

//圖片上傳
const handleImageUpload = (blobInfo: any, progress: any) => new Promise((resolve, reject) => {
  const formData = new FormData();
  formData.append('file', blobInfo.blob(), blobInfo.filename());

  const tokenName = localStorage.getItem(SPContants.TokenName);
  const token = localStorage.getItem(SPContants.Token);
  const headers: any = {};
  if (tokenName && token) {
    headers[tokenName] = token;
  }

  axios.post(uploadUrl, formData, {
    headers: {
      ...headers,
      'Content-Type': 'multipart/form-data'
    },
    onUploadProgress: (e) => {
      if (e.total) {
        progress(Math.round((e.loaded / e.total) * 100));
      }
    }
  }).then(res => {
    if (res.data && res.data.data) {
      resolve(getImgUrl(res.data.data));
    } else {
      reject('上傳失敗');
    }
  }).catch(err => {
    reject('上傳出錯(cuò):' + err.message);
  });
});

const initOptions = computed(() => ({
  height: props.height,
  language: 'zh_CN',
  language_url: '/tinymce/langs/zh_CN.js',
  menubar: false,
  plugins: props.plugins,
  toolbar: props.toolbar,
  toolbar_mode: 'sliding',
  branding: false,
  promotion: false,
  elementpath: false,
  statusbar: true,
  // 使用本地資源時(shí),可以添加額外的配置
  base_url: '/tinymce', // 確保 TinyMCE 知道去哪里找主題和皮膚
  suffix: '.min',
  images_upload_handler: handleImageUpload,
}));
</script>

<style scoped>
</style>
4.使用方式
//導(dǎo)入
import Editor from "@/components/tinyEditor/index.vue";
//使用
 <Editor v-model="content" :min-height="192"/>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容