vue3項(xiàng)目中使用tinymce的方法
tinymce是一個(gè)功能齊全的富文本編輯器插件,但在vue中引入tinymce并不像別的Vue富文本插件一樣那么順利,tinymce本身并不適配Vue,還需要引入@tinymce/tinymce-vue,并且它是國(guó)外的富文本插件,沒有通過(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全家桶實(shí)踐項(xiàng)目總結(jié)(推薦)
本篇文章主要介紹了Vue全家桶實(shí)踐項(xiàng)目總結(jié)(推薦),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
如何解決Vue3組合式API模式下動(dòng)態(tài)組件不渲染問題
這篇文章主要介紹了如何解決Vue3組合式API模式下動(dòng)態(tài)組件不渲染問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教<BR>2024-03-03
vueJS簡(jiǎn)單的點(diǎn)擊顯示與隱藏的效果【實(shí)現(xiàn)代碼】
下面小編就為大家?guī)?lái)一篇vueJS簡(jiǎn)單的點(diǎn)擊顯示與隱藏的效果【實(shí)現(xiàn)代碼】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,一起跟隨小編過(guò)來(lái)看看吧2016-05-05
Vue.js實(shí)現(xiàn)對(duì)視頻預(yù)覽的示例代碼
本文主要介紹了Vue.js實(shí)現(xiàn)對(duì)視頻預(yù)覽的示例代碼,通過(guò)監(jiān)聽文件選擇事件和使用FileReader API,可以實(shí)現(xiàn)視頻文件的預(yù)覽功能,感興趣的可以了解一下2025-01-01
在vscode 中設(shè)置 vue模板內(nèi)容的方法
這篇文章主要介紹了在vscode 中設(shè)置 vue模板內(nèi)容的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
vue響應(yīng)式原理與雙向數(shù)據(jù)的深入解析
Vue 最獨(dú)特的特性之一,是其非侵入性的響應(yīng)式系統(tǒng)。下面這篇文章主要給大家介紹了關(guān)于vue響應(yīng)式原理與雙向數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2021-06-06
Vue3使用ECharts實(shí)現(xiàn)?;鶊D的代碼示例
?;鶊D是一種用于直觀顯示流向數(shù)據(jù)的可視化工具,特別適合展示復(fù)雜的網(wǎng)絡(luò)關(guān)系和資源流動(dòng),在前端項(xiàng)目中,通過(guò)結(jié)合?Vue?3?和?ECharts,可以快速實(shí)現(xiàn)交互性強(qiáng)、樣式美觀的桑基圖,本文將通過(guò)完整的代碼示例,帶你一步步完成一個(gè)?;鶊D的實(shí)現(xiàn),需要的朋友可以參考下2025-01-01

