tinymce富文本框在vue項(xiàng)目中的應(yīng)用

在項(xiàng)目中應(yīng)用tinymce富文本框的使用榛了,首先安裝tinymce沪伙、tinymce-vue這兩個(gè)插件,

npm install tinymce -S

npm install @tinymce/tinymce-vue -S

安裝完成之后在node_modules中找到tinymce文件夾中找到skins疟羹、jquery.tinymce.js主守、jquery.tinymce.min.js,之后安裝中文語(yǔ)言包https://www.tiny.cloud/get-tiny/language-packages/榄融;把這些文件放進(jìn)public文件夾內(nèi)参淫,在components中新建Tinymce組件;在頁(yè)面中引用跟正常組件一樣引入注冊(cè)使用就可以了愧杯。

<template>

? <div class="tinymce-editor">

? ? <editor

? ? ? v-model="myValue"

? ? ? :init="init"

? ? ? :api-key="apiKey"

? ? ? :disabled="disabled"

? ? />

? </div>

</template>

<script>

// import { fileUpload } from '@/api/cms'

import tinymce from "tinymce/tinymce";

import Editor from "@tinymce/tinymce-vue";

// import 'tinymce/themes/modern/theme'

import "tinymce/themes/silver/theme";

import "tinymce/icons/default/icons";

import "tinymce/plugins/image"; // 插入上傳圖片插件

import "tinymce/plugins/media"; // 插入視頻插件

import "tinymce/plugins/table"; // 插入表格插件

import "tinymce/plugins/link"; // 超鏈接插件

import "tinymce/plugins/code"; // 代碼塊插件

import "tinymce/plugins/lists"; // 列表插件

import "tinymce/plugins/contextmenu"; // 右鍵菜單插件

import "tinymce/plugins/wordcount"; // 字?jǐn)?shù)統(tǒng)計(jì)插件

import "tinymce/plugins/colorpicker"; // 選擇顏色插件

import "tinymce/plugins/textcolor"; // 文本顏色插件

import "tinymce/plugins/fullscreen"; // 全屏

import "tinymce/plugins/help"; // 幫助

import "tinymce/plugins/charmap";

import "tinymce/plugins/paste";

import "tinymce/plugins/print"; // 打印

import "tinymce/plugins/preview"; // 預(yù)覽

import "tinymce/plugins/hr"; // 水平線

import "tinymce/plugins/anchor";

import "tinymce/plugins/pagebreak";

import "tinymce/plugins/spellchecker";

import "tinymce/plugins/searchreplace";

import "tinymce/plugins/visualblocks";

import "tinymce/plugins/visualchars";

import "tinymce/plugins/insertdatetime";

import "tinymce/plugins/nonbreaking";

import "tinymce/plugins/autosave";

import "tinymce/plugins/fullpage";

import "tinymce/plugins/toc";

import "tinymce/plugins/advlist";

import "tinymce/plugins/autolink";

import "tinymce/plugins/codesample";

import "tinymce/plugins/directionality";

import "tinymce/plugins/imagetools";

import "tinymce/plugins/noneditable";

import "tinymce/plugins/save";

import "tinymce/plugins/tabfocus";

import "tinymce/plugins/textpattern";

import "tinymce/plugins/template";

export default {

? components: {

? ? Editor,

? },

? props: {

? ? // 傳入一個(gè)value涎才,使組件支持v-model綁定

? ? value: {

? ? ? type: String,

? ? ? default: "",

? ? },

? ? disabled: {

? ? ? type: Boolean,

? ? ? default: false,

? ? },

? ? plugins: {

? ? ? type: [String, Array],

? ? ? default:

? ? ? ? "lists image media table textcolor wordcount contextmenu preview",

? ? },

? ? toolbar: {

? ? ? type: [String, Array],

? ? ? default:

? ? ? ? "undo redo | ?formatselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table | removeformat table| fontsizeselect | fontselect'",

? ? },

? },

? data() {

? ? return {

? ? ? apiKey: "jzlds2e6urz6akm9wxp4f70mnwg83d8fovsialqazxowyity",

? ? ? // 配置文件服務(wù)器的靜態(tài)訪問(wèn)路徑前綴

? ? ? // static_web_preurl: 'http://localhost/files/hxzy_img/',

? ? ? // 初始化配置

? ? ? init: {

? ? ? ? placeholder: "請(qǐng)輸入內(nèi)容",

? ? ? ? // language_url: require("../assets/langs/zh-Hans.js"), // 這個(gè)文件會(huì)放在下面

? ? ? ? language_url: require("../../../public/tinymce/langs/zh_CN"),

? ? ? ? language: "zh_CN",

? ? ? ? skin_url: "/tinymce/skins/ui/oxide",

? ? ? ? height: 300,

? ? ? ? end_container_on_empty_block: true,

? ? ? ? powerpaste_word_import: "clean",

? ? ? ? advlist_bullet_styles: "square",

? ? ? ? advlist_number_styles: "default",

? ? ? ? imagetools_cors_hosts: ["www.tinymce.com", "codepen.io"],

? ? ? ? default_link_target: "_blank",

? ? ? ? link_title: false,

? ? ? ? media_live_embeds: true,

? ? ? ? content_style: "img {max-width:100%;}", // 直接自定義可編輯區(qū)域的css樣式

? ? ? ? nonbreaking_force_tab: true, // inserting nonbreaking space &nbsp; need Nonbreaking Space Plugin

? ? ? ? // plugins: this.plugins,

? ? ? ? // toolbar: this.toolbar,

? ? ? ? // @ts-nocheckplugins: 'link lists image code table colorpicker textcolor wordcount contextmenu',

? ? ? ? plugins:

? ? ? ? ? "advlist anchor autolink autosave code codesample colorpicker ?contextmenu directionality ?fullscreen hr image imagetools insertdatetime link lists media nonbreaking noneditable pagebreak paste preview print save searchreplace spellchecker tabfocus table template textcolor textpattern visualblocks visualchars wordcount",

? ? ? ? // toolbar:'bold italic underline strikethrough | fontsizeselect | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent blockquote | undo redo | link unlink image code | removeformat | table',

? ? ? ? toolbar: [

? ? ? ? ? "searchreplace bold italic underline strikethrough fontselect fontsizeselect ?alignleft aligncenter alignright outdent indent ?blockquote undo redo removeformat subscript superscript code codesample",

? ? ? ? ? "hr bullist numlist link image charmap preview anchor pagebreak insertdatetime media table emoticons forecolor backcolor fullscreen",

? ? ? ? ],

? ? ? ? fontsize_formats: "8pt 10pt 12pt 14pt 18pt 24pt 36pt", // 第二步

? ? ? ? font_formats:

? ? ? ? ? "微軟雅黑='微軟雅黑';宋體='宋體';黑體='黑體';仿宋='仿宋';楷體='楷體';隸書(shū)='隸書(shū)';幼圓='幼圓';Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings",

? ? ? ? branding: false,

? ? ? ? menubar: true,

? ? ? ? elementpath: false,

? ? ? ? file_picker_types: "media",

? ? ? ? // 此處為圖片上傳處理函數(shù),這個(gè)直接用了base64的圖片形式上傳圖片力九,

? ? ? ? // 如需ajax上傳可參考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler

? ? ? ? /* images_upload_handler: async(blobInfo, success, failure) => {

? ? ? ? ? // ?let formdata = new FormData()

? ? ? ? ? // formdata.set('file', blobInfo.blob())

? ? ? ? ? const { url, name } = await this.uploadFile(blobInfo.blob(), 'image')

? ? ? ? ? success(url, { title: name })

? ? ? ? ? // this.handleImgUpload(blobInfo, success, failure)

? ? ? ? }*/

? ? ? ? // file_picker_callback: (cb, value, meta) => {

? ? ? ? // ? // 當(dāng)點(diǎn)擊meidia圖標(biāo)上傳時(shí),判斷meta.filetype == 'media'有必要耍铜,因?yàn)閒ile_picker_callback是media(媒體)、image(圖片)畏邢、file(文件)的共同入口

? ? ? ? // ? if (meta.filetype == 'media') {

? ? ? ? // ? ? // 創(chuàng)建一個(gè)隱藏的type=file的文件選擇input

? ? ? ? // ? ? const input = document.createElement('input')

? ? ? ? // ? ? input.setAttribute('type', 'file')

? ? ? ? // ? ? input.setAttribute('accept', 'video/*')

? ? ? ? // ? ? input.onchange = async(e) => {

? ? ? ? // ? ? ? const file = e.path[0].files[0]// 只選取第一個(gè)文件业扒。如果要選取全部,后面注意做修改

? ? ? ? // ? ? ? if (this.validateVideo(file)) {

? ? ? ? // ? ? ? ? const { url, name } = await this.uploadFile(file, 'video')

? ? ? ? // ? ? ? ? cb(url, { title: name })

? ? ? ? // ? ? ? }

? ? ? ? // ? ? }

? ? ? ? // ? ? // 觸發(fā)點(diǎn)擊

? ? ? ? // ? ? input.click()

? ? ? ? // ? }

? ? ? ? // }

? ? ? },

? ? ? myValue: this.value,

? ? };

? },

? watch: {

? ? value(newValue) {

? ? ? this.myValue = newValue;

? ? },

? ? myValue(newValue) {

? ? ? this.$emit("input", newValue);

? ? },

? },

? mounted() {

? ? tinymce.init({});

? },

? methods: {

? ? // 校驗(yàn)視頻

? ? /* async validateVideo(file) {

? ? ? const isMP4 = file.type === 'video/mp4'

? ? ? const isLt3M = file.size / 1024 / 1024 < 3

? ? ? if (!isMP4) {

? ? ? ? this.$message.error('上傳視頻必須為 MP4 格式舒萎!')

? ? ? ? return false

? ? ? }

? ? ? if (!isLt3M) {

? ? ? ? this.$message.error('上傳視頻大小限制 3M 以內(nèi)程储!')

? ? ? ? return false

? ? ? }

? ? ? // const duration = await this.getVideoDuration(file)

? ? ? // if (duration > 60) {

? ? ? // ? this.$message.error('上傳視頻時(shí)長(zhǎng)不能超過(guò) 60 秒蹭沛!')

? ? ? // ? return false

? ? ? // }

? ? ? return true

? ? },*/

? ? /**

? ? ?* @description 獲取視頻時(shí)長(zhǎng)

? ? ?* @param {File} file - 要上傳的文件

? ? ?* @returns {Promise<number>}

? ? ?*/

? ? /* ?getVideoDuration(file) {

? ? ? return new Promise(resolve => {

? ? ? ? const videoElement = document.createElement('video')

? ? ? ? videoElement.src = URL.createObjectURL(file)

? ? ? ? videoElement.addEventListener('loadedmetadata', () => {

? ? ? ? ? resolve(videoElement.duration)

? ? ? ? })

? ? ? })

? ? }*/

? ? /**

? ? ?* @description 上傳文件

? ? ?* @param {File} file - 要上傳的文件

? ? ?* @param {string} type - 文件類型

? ? ?* @returns {Object}

? ? ?*/

? ? /* ? ? async uploadFile(file, type = 'images') {

? ? ? const loading = this.$loading({

? ? ? ? lock: true,

? ? ? ? text: 'Loading',

? ? ? ? spinner: 'el-icon-loading',

? ? ? ? background: 'rgba(0, 0, 0, 0.7)'

? ? ? })

? ? ? const formData = new FormData()

? ? ? formData.append('file', file)

? ? ? // // 注:此為調(diào)用后端上傳接口,需根據(jù)實(shí)際情況進(jìn)行調(diào)整

? ? ? // const res = await fileUpload(formData)

? ? ? // loading.close()

? ? ? // if (res.code !== 1) return this.$message.error(res.$message)

? ? ? // return {

? ? ? // ? url: `${ROOT}/${res.data}`,

? ? ? // ? name: file.name

? ? ? // }

? ? },*/

? ? // 添加相關(guān)的事件章鲤,可用的事件參照文檔=> https://github.com/tinymce/tinymce-vue => All available events

? ? // 需要什么事件可以自己增加

? ? onClick(e) {

? ? ? this.$emit("onClick", e, tinymce);

? ? },

? ? // 可以添加一些自己的自定義事件摊灭,如清空內(nèi)容

? ? clear() {

? ? ? this.myValue = "";

? ? },

? },

};

</script>

<style scoped>

.tinymce-editor {

? width: 100%;

? height: 100%;

}

</style>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市败徊,隨后出現(xiàn)的幾起案子帚呼,更是在濱河造成了極大的恐慌,老刑警劉巖皱蹦,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件煤杀,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡沪哺,警方通過(guò)查閱死者的電腦和手機(jī)沈自,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)辜妓,“玉大人枯途,你說(shuō)我怎么就攤上這事〖危” “怎么了酪夷?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)孽惰。 經(jīng)常有香客問(wèn)我晚岭,道長(zhǎng),這世上最難降的妖魔是什么灰瞻? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任腥例,我火速辦了婚禮,結(jié)果婚禮上酝润,老公的妹妹穿的比我還像新娘燎竖。我一直安慰自己,他們只是感情好要销,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布构回。 她就那樣靜靜地躺著,像睡著了一般疏咐。 火紅的嫁衣襯著肌膚如雪纤掸。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,482評(píng)論 1 302
  • 那天浑塞,我揣著相機(jī)與錄音借跪,去河邊找鬼。 笑死酌壕,一個(gè)胖子當(dāng)著我的面吹牛掏愁,可吹牛的內(nèi)容都是我干的歇由。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼果港,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼沦泌!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起辛掠,我...
    開(kāi)封第一講書(shū)人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤谢谦,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后萝衩,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體回挽,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年猩谊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了厅各。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡预柒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出袁梗,到底是詐尸還是另有隱情宜鸯,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布遮怜,位于F島的核電站淋袖,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏荸频。R本人自食惡果不足惜鞠鲜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一唉俗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧剥懒,春花似錦、人聲如沸合敦。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)充岛。三九已至保檐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間崔梗,已是汗流浹背夜只。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蒜魄,地道東北人扔亥。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓场躯,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親砸王。 傳聞我的和親對(duì)象是個(gè)殘疾皇子推盛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容