項目需求可發佈文章 需求涉及到富文本編輯器 經過查閱我選擇了較爲簡便 不需要後端支持可獨立完成的tinymce框架 官方文檔也是相當完整 雖然都是全英文 但是有強大的 谷歌~ 沒問題的
編輯器,tinymce 不需要後端配合 只需要把最終編輯完的富文本傳給後端就好 很簡單
下載tinymce
npm install tinymce
安裝tinymce-vue
npm install @tinymce/tinymce-vue
下載完成後在node_modules 中找到 tinymce/skins目錄 ,複製下來 放置static
下載中文語言包
tinymce提供了很多的語言包,這裏我們下載語言包 全英文 不懂 自己谷歌打開 翻譯一下 選擇下載zh_CN
初始化
頁面引入文件
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/themes/silver";
注意tinymce的路徑
註冊使用
<editor :init="init" v-model="content" class="issue-editor"></editor>
components:{ editor: Editor}
初始化配置項 官方文檔
有使用powerPaste 將文件powerPaste 複製下來 放置static 引入即可使用
accept: "image/jpeg, image/png", //設置圖片上傳規則
maxSize: "2097152", //設置圖片大小
height:'500',//設置編輯框大小
content:'',//編輯的內容 可以設置監聽查看編輯輸入的值
init: {
content_style: `
* { padding:0; margin:0; }
html, body { height:100%; }
img { max-width:100%; display:block;height:auto; }
a { text-decoration: none; }
iframe { width: 100%; }
p { line-height:1.6; margin: 0px; }
table { word-wrap:break-word; word-break:break-all; max-width:100%; border:none; border-color:#999; }
.mce-object-iframe { width:100%; box-sizing:border-box; margin:0; padding:0; }
ul,ol { list-style-position:inside; }
`,///設置富文本的樣式初始化
skin_url: "/static/skins/ui/oxide", // tinymce UI引入
language_url: "/static/zh_CN.js", //轉中文文件
language: "zh_CN",
browser_spellcheck: true, // 拼寫檢查
branding: false, // 去水印
elementpath: false, //禁用編輯器底部的狀態欄
statusbar: false, // 隱藏編輯器底部的狀態欄
paste_data_images: true, // 允許粘貼圖像
menubar: false, // 隱藏最上方menu}
// plugins:
// "image wordcount ",
// toolbar: {
// type: [String, Array],
// default:
// "undo redo | formatselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table | removeformat"
// },
plugins:
"advlist link image code textcolor paste textcolor colorpicker",
// plugins 配置必須有paste 否則圖片無法黏貼成功 或者 改paste 爲powerpaste 可不用引入paste
// paste只能實現文字或者圖片單獨黏貼
// powerpaste 可以實現 文字和圖片一起黏貼 (本地圖片會變成base64直接呈現)需要powerPaste文件的可私聊我要
toolbar_items_size: "small",
block_formats:
"Paragraph=p;Heading 1=h1;Heading 2=h2;Heading 3=h3;Heading 4=h4;Heading 5=h5;Heading 6=h6;",
toolbar1:
"table |insertfile undo redo | formatselect | link unlink | uploadimg image media | name", // 工具欄1
toolbar2:
" fontsizeselect | forecolor backcolor | alignleft aligncenter alignright alignjustify | outdent indent | removeformat ", // 工具欄2
// 引入powerpaste 註冊 改plugins配置中的 paste爲powerpaste
external_plugins: {
powerpaste: "/static/skins/powerpaste/plugin.js"
},
powerpaste_allow_local_images: true, //powerpaste允許黏貼
powerpaste_word_import: "clean", //powerpaste黏貼的樣式獲取方式
// setup: editor => { //自定義添加按鈕
// editor.ui.registry.addButton("name", {
// tooltip: "選擇標籤",
// text: "選擇標籤",
// onAction: res => {
// console.log(res);
// self.labelDialogShow=true;
// },
// });
// },
//粘貼事件數據官方文檔
//黏貼內容時觸發 獲取元素 可自定義添加內容
paste_postprocess: function(pluginApi, data) {
// console.log(data);
// // Apply custom filtering by mutating data.node
// const additionalNode = document.createElement("div");
// console.log(additionalNode);
// additionalNode.innerHTML =
// "<p>This will go before the pasted content.</p>";
// data.node.insertBefore(additionalNode, data.node.firstElementChild);
},
//只要涉及上傳圖片就會觸發方法 toolbar1 :uploadimg image media 工具欄呈現調用圖片按鈕 plugins:"image wordcount " 最上方呈現圖片調用按鈕 menubar 本地圖片黏貼也是觸發這個方法
images_upload_handler: (blobInfo, success, failure) => {
// console.log(blobInfo, success, failure);
if (blobInfo.blob().size > self.maxSize) {
failure("文件體積過大");
}
if (self.accept.indexOf(blobInfo.blob().type) >= 0) {
//上傳圖片符合規則 調用圖片上傳 上傳成功 回調傳入success 自己的上傳圖片接口
self.handleImgUpload(blobInfo, success, failure);
} else {
failure("圖片格式錯誤");
}
// https://blog.taiorient.com/file/appletUpload
// const img = "data:image/jpeg;base64," + blobInfo.base64();
// success(img);
}
}
上傳圖片接口
//圖片上傳
handleImgUpload(blobInfo, success, failure) {
//繼承編輯器方法 blobInfo, success, failure
console.log(blobInfo.blob());
let formdata = new FormData();
formdata.set("upload_file", blobInfo.blob());
axios
.post("https://blogtiorent.com/file/apletUpload", formdata)
.then(res => {
console.log(res);
// 上傳成功 回調傳給編輯器
success(
"http://taioientcde.os-cn-senzhen.aiuncscom/image/" +
res.data.data
);
})
.catch(res => {
//失敗通知
failure("error");
});
},
擴展插件
引入
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' // 字數統計插件
import 'tinymce/plugins/colorpicker' //選擇顏色插件
import 'tinymce/plugins/textcolor' //文本顏色插件
import 'tinymce/plugins/fullscreen' //全屏
import "tinymce/plugins/preview"; //預覽插件
import "tinymce/plugins/code";//代碼塊插件
// import "tinymce/plugins/paste";//圖片黏貼插件
關於發佈的文章在手機上圖片過大處理
//在提交數據的時候 對img便籤進行篩選替換
save(status) {
// 處理圖片過大問題
this.content = this.content.replace(
/<img [^>]*src=['"]([^'"]+)[^>]*>/gi,
(mactch, capture) => {
if(mactch.indexOf('max-width:')!=-1){
}else if(mactch.indexOf('style=')!=-1){
mactch = mactch.replace('style="','<img style="max-width:100%;')
}else{
mactch = mactch.replace('<img','<img style="max-width:100%;" ')
}
let current = "";
this.newImgUrl.forEach(item => {
if (capture == item.oriUrl) {
current = item.filePath;
}
});
current = current ? current : capture;
return mactch.replace(
/src=[\'\"]?([^\'\"]*)[\'\"]?/i,
"src=" + current
);
}
);
//處理powerPaste div align="center" 無法達到文字居中效果
this.content = this.content.replace(
/align="center"/gi,
(mactch, capture) => {
return mactch.replace('align="center"', 'style="text-align:center"');
}
);
// // 匹配並替換 任意html元素中 url 路徑
// this.content = this.content.replace(
// /url\(['"](.+)['"]\)/gi,
// (mactch, capture) => {
// let current = "";
// this.newImgUrl.forEach(item => {
// if (capture == item.oriUrl) {
// current = item.filePath;
// }
// });
// current = current ? current : capture;
// return mactch.replace(
// /url\((['"])(.+)(['"])\)/i,
// `url($1${current}$3) `
// );
// }
// );
}
整理說明 圖片黏貼上傳
圖片黏貼上傳 tinymce 的 paste插件 就可以支持了 有個小問題就是本地圖片黏貼編輯器上傳 不能與圖文一起復制
0. 引入插件 import "tinymce/plugins/paste"
1. 基礎設置 paste_data_images: true, // 允許粘貼圖像
2.工具欄配置 uploadimg image media
3.通知使用插件 plugins: paste
3.上傳圖片 觸發方法 實現圖片自定義上傳 handleImgUpload(blobInfo, success, failure) { } 返回成功的success(url)
處理圖文一起復制 也就是Word 複製上傳 改paste 爲powerpaste
0.下載powerpaste文件放置自己項目 便於引用
1.改plugins內配置paste爲 powerpaste
2.引用添加自定義插件 external_plugins:{powerpaste: "/static/skins/powerpastes/plugin.min.js" }, ( 注:引用文件版本要對 不然報錯無法正常上傳圖片)
3.基礎配置 powerpaste_allow_local_images: true, //powerpaste允許黏貼 powerpaste_word_import: "clean", // 是否保留word粘貼樣式 clean | merge (具體可查看官方文檔)
到此完整解決 基本沒有什麼坑可以踩 很簡單