一、 什么是TinyMCE
TinyMCE是一款易用、且功能強大的所見即所得的富文本編輯器。同類程序有:UEditor、Kindeditor、Simditor、CKEditor、wangEditor、Suneditor、froala等等。
TinyMCE的優(yōu)勢:
開源可商用,基于LGPL2.1
插件豐富,自帶插件基本涵蓋日常所需功能(示例看下面的Demo-2)
接口豐富,可擴展性強,有能力可以無限拓展功能
界面好看,符合現(xiàn)代審美
提供經(jīng)典、內(nèi)聯(lián)、沉浸無干擾三種模式(詳見“介紹與入門”)
對標準支持優(yōu)秀(自v5開始)
多語言支持,官網(wǎng)可下載幾十種語言。
- tinymce英文文檔:https://www.tiny.cloud/docs/api/tinymce/root_tinymce/
- tinymce中文文檔:http://tinymce.ax-z.cn/
拓展:
除了TinyMCE,還有一款適用于Vue的富文本編輯器:Vue-Quill-Editor,使用方法請參考文檔。
二、如何使用TinyMCE
1. 安裝TinyMCE
若項目是vue3.x,則:
npm install tinymce -S
如項目是vue2.x,則:
npm install tinymce@5.1.0 -S
2. 安裝tinymce-vue
若項目是vue3.x,則:
npm install @tinymce/tinymce-vue -S
若項目是vue2.x,則:
npm install @tinymce/tinymce-vue@3.0.1 -S
3. 下載中文語言包
tinymce提供了很多的語言包,這里我們下載中文語言包
地址:https://www.tiny.cloud/get-tiny/language-packages/

image
下載完后放到靜態(tài)文件public目錄下
1、在public目錄下新建tinymce,將上面下載的語言包解壓到該目錄
2、在node_modules里面找到tinymce,將skins目錄復(fù)制到public/tinymce里面

image
4. Vue項目集成TinyMCE
1)封裝成組件
<template>
<div class="tinymce-box">
<editor v-model="myValue"
:init="init"
:disabled="disabled"
@onClick="onClick">
</editor>
</div>
</template>
<script>
import tinymce from 'tinymce/tinymce' //tinymce默認hidden,不引入不顯示
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver'
// 編輯器插件plugins
// 更多插件參考:https://www.tiny.cloud/docs/plugins/
import 'tinymce/plugins/image'// 插入上傳圖片插件
import 'tinymce/plugins/media'// 插入視頻插件
import 'tinymce/plugins/table'// 插入表格插件
import 'tinymce/plugins/lists'// 列表插件
import 'tinymce/plugins/wordcount'// 字數(shù)統(tǒng)計插件
export default {
components:{
Editor
},
name:'tinymce',
props: {
value: {
type: String,
default: ''
},
disabled: {
type: Boolean,
default: false
},
plugins: {
type: [String, Array],
default: 'lists image media table wordcount'
},
toolbar: {
type: [String, Array],
default: 'undo redo | formatselect | bold italic forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table | removeformat'
}
},
data(){
return{
init: {
language_url: '/tinymce/langs/zh_CN.js',
language: 'zh_CN',
skin_url: '/tinymce/skins/ui/oxide',
// skin_url: 'tinymce/skins/ui/oxide-dark',//暗色系
height: 300,
plugins: this.plugins,
toolbar: this.toolbar,
branding: false,
menubar: false,
// 此處為圖片上傳處理函數(shù),這個直接用了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)
}
},
myValue: this.value
}
},
mounted () {
tinymce.init({})
},
methods: {
// 添加相關(guān)的事件,可用的事件參照文檔=> https://github.com/tinymce/tinymce-vue => All available events
// 需要什么事件可以自己增加
onClick (e) {
this.$emit('onClick', e, tinymce)
},
// 可以添加一些自己的自定義事件,如清空內(nèi)容
clear () {
this.myValue = ''
}
},
watch: {
value (newValue) {
this.myValue = newValue
},
myValue (newValue) {
this.$emit('input', newValue)
}
}
}
</script>
<style scoped>
</style>
2)組件引用
<template>
<div class="home">
<tinymce
ref="editor"
v-model="msg"
:disabled="disabled"
@onClick="onClick"
/>
<button @click="clear">清空內(nèi)容</button>
<button @click="disabled = true">禁用</button>
</div>
</template>
<script>
import tinymce from '@/components/tinymce.vue'
export default {
name: 'home',
components: {
tinymce
},
data(){
return{
msg: 'Welcome to Use Tinymce Editor',
disabled: false
}
},
methods: {
// 鼠標單擊的事件
onClick (e, editor) {
console.log('Element clicked')
console.log(e)
console.log(editor)
},
// 清空內(nèi)容
clear () {
this.$refs.editor.clear()
}
}
}
</script>