TinyMCE是一款易用狼电、且功能強(qiáng)大的所見即所得的富文本編輯器,在移動(dòng)端還是網(wǎng)頁(yè)端都運(yùn)用十分廣泛,這篇文章主要講述的是結(jié)合vue項(xiàng)目和tinymce5的使用方法弦蹂,這個(gè)是結(jié)合了網(wǎng)上的多篇優(yōu)質(zhì)博客加上自己的實(shí)踐總結(jié)得到的一寫經(jīng)驗(yàn)肩碟,希望它能幫到一些人解決自己的問題。
其中文參考文檔:http://tinymce.ax-z.cn/
插件安裝
// 安裝 tinymce-vue
npm install @tinymce/tinymce-vue -S
// 安裝 tinymce
npm install tinymce -S
當(dāng)然可能會(huì)有些小伙伴在安裝的時(shí)候一直報(bào)錯(cuò)(沒錯(cuò)凸椿,說得就是我)削祈,所以提供以下解決方案:
在package.json文件中的dependencies配置中加入以下內(nèi)容:
"dependencies": {
"@tinymce/tinymce-vue": "^2.0.0",
"tinymce": "^5.0.3",
}
再去執(zhí)行 npm install
即可
使用前操作
- 下載語(yǔ)言包 https://liubing.me/goto/https://www.tiny.cloud/get-tiny/language-packages/
- 下載后文件解壓后放入vue項(xiàng)目中的static文件夾中新建的tinymce文件夾中
- 在node_modules包中尋找tinymce文件夾中skin文件夾也移動(dòng)到static文件夾中新建的tinymce文件夾中
原因:安裝的依賴包里面默認(rèn)么有l(wèi)ang這個(gè)文件夾包,所以需要手動(dòng)下載脑漫,而skin的放置則是因?yàn)閠inymce富文本器一定要去skin這個(gè)配置正確才可以顯示出髓抑。 放置在static是保證在項(xiàng)目打包是也可以找到配置文件
結(jié)合官方文檔封裝tinymce組件
<template>
<div class="tinymce-editor">
<editor
v-model="myValue"
:init="init"
:disabled="disabled"
@onClick="onClick"
>
</editor>
</div>
</template>
<script>
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/themes/silver/theme";
// 編輯器插件plugins
// 更多插件參考:https://www.tiny.cloud/docs/plugins/
import "tinymce/plugins/image"; // 插入上傳圖片插件
import "tinymce/plugins/table"; // 插入表格插件
import "tinymce/plugins/lists"; // 列表插件
import "tinymce/plugins/wordcount"; // 字?jǐn)?shù)統(tǒng)計(jì)插件
export default {
components: {
Editor
},
props: {
disabled: {
type: Boolean,
default: false
},
plugins: {
type: [String, Array],
default: "lists image 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: "/static/tinymce/langs/zh_CN.js",
language: "zh_CN",
convert_urls:false ,
skin_url: "/static/tinymce/skins/ui/oxide",
content_css: "/static/tinymce/skins/content/default/content.css",
height: 300,
plugins: this.plugins,
toolbar: this.toolbar,
branding: false,
menubar: false,
// 如需ajax上傳可參考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
images_upload_handler: (blobInfo, success, failure) => {
var xhr, formData;
xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open("POST", "上傳文件的地址");
xhr.onload = function() {
var json;
if (xhr.status != 200) {
failure("HTTP Error: " + xhr.status);
return;
}
json = JSON.parse(xhr.responseText);
// this.imgsUrl[this.imgsUrl.length - 1] = json["data"];
success(json["data"]);
};
formData = new FormData();
formData.append("file", blobInfo.blob(), blobInfo.filename());
xhr.send(formData);
}
},
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>
vue引用組件
<template>
<tinymce-editor
ref="editor"
v-model="content"
>
</tinymce-editor>
</template>
<script>
import TinymceEditor from "../commment/tinymce-editor";
export default {
components: {
TinymceEditor
},
data() {
return {
content: "",
}
}
}
</script>
注意:上傳圖片時(shí)需要實(shí)現(xiàn)本地上傳队秩,只要編寫了upload_handler函數(shù)即可出現(xiàn)本地上傳圖片的功能馍资,根據(jù)服務(wù)器響應(yīng)后返回的數(shù)據(jù),需要在success(data.imgUrl)發(fā)送返回的地址才可以正確渲染到富文本編輯器中