記錄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"/>