vue3項(xiàng)目中使用tinymce的方法
tinymce是一個(gè)功能齊全的富文本編輯器插件,但在vue中引入tinymce并不像別的Vue富文本插件一樣那么順利,tinymce本身并不適配Vue,還需要引入@tinymce/tinymce-vue,并且它是國(guó)外的富文本插件,沒(méi)有通過(guò)中文版本,需要在其官網(wǎng)下載翻譯包(可能需要翻墻)。下面給大家介紹下vue3項(xiàng)目中使用tinymce的方法。
1、安裝相關(guān)依賴
npm install tinymce -S npm install @tinymce/tinymce-vue -S
2、下載中文包
地址 https://www.tiny.cloud/get-tiny/language-packages/
3. 引入皮膚和漢化包
在項(xiàng)目public文件夾下新建tinymce文件夾,
將下載的漢化包解壓到此文件夾
然后在node_modules/tinymce中找到skins文件夾,也復(fù)制到public/tinymce里

4. 封裝組件:在src/components下新建TEditor.vue,并寫入以下代碼
<template>
<editor v-model="myValue" :init="init" :disabled="disabled" :id="tinymceId"></editor>
</template>
<script setup lang="ts">
//JS部分
//在js中引入所需的主題和組件
import tinymce from 'tinymce/tinymce'
import 'tinymce/skins/content/default/content.css'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver'
import 'tinymce/themes/silver/theme'
import 'tinymce/icons/default'; //引入編輯器圖標(biāo)icon,不引入則不顯示對(duì)應(yīng)圖標(biāo)
import 'tinymce/models/dom' // 這里是個(gè)坑 一定要引入
//在TinyMce.vue中接著引入相關(guān)插件
import "tinymce/icons/default/icons"
// import "tinymce/plugins/image" // 插入上傳圖片插件
// import "tinymce/plugins/media" // 插入視頻插件
import "tinymce/plugins/table" // 插入表格插件
import "tinymce/plugins/lists" // 列表插件
import "tinymce/plugins/wordcount" // 字?jǐn)?shù)統(tǒng)計(jì)插件
import "tinymce/plugins/code" // 源碼
// import "tinymce/plugins/fullscreen" //全屏
//接下來(lái)定義編輯器所需要的插件數(shù)據(jù)
import { reactive, ref } from "vue"
import { onMounted, defineEmits, watch } from "@vue/runtime-core"
import axios from 'axios'
// import { updateImg } from '@/api/order/order'
const emits = defineEmits(["getContent"])
//這里我選擇將數(shù)據(jù)定義在props里面,方便在不同的頁(yè)面也可以配置出不同的編輯器,當(dāng)然也可以直接在組件中直接定義
const props = defineProps({
value: {
type: String,
default: () => {
return ""
},
},
baseUrl: {
type: String,
default: "",
},
disabled: {
type: Boolean,
default: false,
},
plugins: {
type: [String, Array],
default: "lists table",
},//必填
toolbar: {
type: [String, Array],
default:
"codesample bold italic underline alignleft aligncenter alignright alignjustify | undo redo | formatselect | fontselect | fontsizeselect | forecolor backcolor | bullist numlist outdent indent | lists link table code | removeformat ",
},//必填
})
//用于接收外部傳遞進(jìn)來(lái)的富文本
const myValue = ref(props.value)
const tinymceId = ref("vue-tinymce-" + +new Date() + ((Math.random() * 1000).toFixed(0) + ""))
//定義一個(gè)對(duì)象 init初始化
const init = reactive({
selector: '#' + tinymceId.value, //富文本編輯器的id,
language_url: "/tinymce/langs/zh_CN.js", // 語(yǔ)言包的路徑,具體路徑看自己的項(xiàng)目,文檔后面附上中文js文件
language: "zh_CN", //語(yǔ)言
skin_url: "/tinymce/skins/ui/oxide", // skin路徑,具體路徑看自己的項(xiàng)目
height: 400, //編輯器高度
branding: false, //是否禁用“Powered by TinyMCE”
menubar: true, //頂部菜單欄顯示
image_dimensions: false, //去除寬高屬性
plugins: props.plugins, //這里的數(shù)據(jù)是在props里面就定義好了的
toolbar: props.toolbar, //這里的數(shù)據(jù)是在props里面就定義好了的
font_formats: 'Arial=arial,helvetica,sans-serif; 宋體=SimSun; 微軟雅黑=Microsoft Yahei; Impact=impact,chicago;', //字體
fontsize_formats: '11px 12px 14px 16px 18px 24px 36px 48px 64px 72px', //文字大小
// paste_convert_word_fake_lists: false, // 插入word文檔需要該屬性
paste_webkit_styles: "all",
paste_merge_formats: true,
nonbreaking_force_tab: false,
paste_auto_cleanup_on_paste: false,
file_picker_types: 'file',
content_css: '/tinymce/skins/content/default/content.css', //以css文件方式自定義可編輯區(qū)域的css樣式,css文件需自己創(chuàng)建并引入
//圖片上傳
images_upload_handler: (blobInfo, progress) => new Promise((resolve, reject) => {
if (blobInfo.blob().size / 1024 / 1024 > 2) {
reject({ message: '上傳失敗,圖片大小請(qǐng)控制在 2M 以內(nèi)', remove: true })
return
} else {
const ph = import.meta.env.VITE_BASE_PATH + ":" + import.meta.env.VITE_SERVER_PORT + "/"
let params = new FormData()
params.append('file', blobInfo.blob())
let config = {
headers: {
"Content-Type": "multipart/form-data",
}
}
axios.post('xxxx', params, config).then(res => {
if (res.data.code == 200) {
resolve(ph + res.data.msg) //上傳成功,在成功函數(shù)里填入圖片路徑
} else {
reject('HTTP Error: 上傳失敗' + res.data.code);
return
}
}).catch(() => {
reject('上傳出錯(cuò),服務(wù)器開小差了呢')
return
})
}
}),
// 文件上傳
file_picker_callback: (callback, value, meta) => {
// Provide file and text for the link dialog
if (meta.filetype == 'file') {
callback('mypage.html', { text: 'My text' });
}
// Provide image and alt text for the image dialog
if (meta.filetype == 'image') {
callback('myimage.jpg', { alt: 'My alt text' });
}
// Provide alternative source and posted for the media dialog
if (meta.filetype == 'media') {
callback('movie.mp4', { source2: 'alt.ogg', poster: 'image.jpg' });
}
}
})
//監(jiān)聽外部傳遞進(jìn)來(lái)的的數(shù)據(jù)變化
watch(
() => props.value,
() => {
myValue.value = props.value
emits("getContent", myValue.value)
}
)
//監(jiān)聽富文本中的數(shù)據(jù)變化
watch(
() => myValue.value,
() => {
emits("getContent", myValue.value)
}
)
//在onMounted中初始化編輯器
onMounted(() => {
tinymce.init({})
})
</script>
5. 注冊(cè)及使用組件
// 使用
<TEditor ref="editor" v-model="formState.content" :disabled='disabled' @getContent="getContent"/>
<script setup lang="ts">
import { reactive } from "vue";
// 引入
import TEditor from '@/components/TEditor.vue';
const formState = reactive({contents :''})
const getContent = (v: string) => {
formState.contents = v
}
</script>Tinymce 版本
"@tinymce/tinymce-vue": "^5.0.0"
"tinymce": "^6.0.3"
參考文章
https://blog.csdn.net/weixin_47180815/article/details/121748486
到此這篇關(guān)于vue3使用tinymce的文章就介紹到這了,更多相關(guān)vue3使用tinymce內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 組件的封裝之基于axios的ajax請(qǐng)求方法
今天小編就為大家分享一篇vue 組件的封裝之基于axios的ajax請(qǐng)求方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
解決vue中使用history.replaceState?更改url?vue?router?無(wú)法感知的問(wèn)題
這篇文章主要介紹了vue中使用history.replaceState?更改url?vue?router?無(wú)法感知的問(wèn)題,本文給大家分享修復(fù)這個(gè)問(wèn)題的方法,需要的朋友可以參考下2022-09-09
Vue.js devtool插件安裝后無(wú)法使用的解決辦法
Vue.js devtool插件最近在開發(fā)人員中很火,這篇文章主要為大家詳細(xì)介紹了Vue.js devtool插件安裝后無(wú)法使用,出現(xiàn)提示“vue.js not detected”的解決辦法2017-11-11
關(guān)于vue屬性使用和不使用冒號(hào)的區(qū)別說(shuō)明
這篇文章主要介紹了關(guān)于vue屬性使用和不使用冒號(hào)的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Vue中搭配Bootstrap實(shí)現(xiàn)Vue的列表增刪功能
日常開發(fā)中,我們可以用?“拿來(lái)主義”?借助Bootstarp現(xiàn)成的一些樣式,快速生成我們想要的頁(yè)面布局,避免書寫大量的HTML和CSS代碼,省下了許多不必要的時(shí)間,可以直接搭配vue使用2022-11-11
Vue3使用Swiper實(shí)現(xiàn)輪播圖示例詳解
這篇文章主要為大家介紹了Vue3使用Swiper實(shí)現(xiàn)輪播圖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

