本文主要介紹vue移動端使用富文本編輯器的使用及常見問題處理。參考組件vue-html5-editor。javascript
本例主要基於vue-cli腳手架建立。更多vue相關應用請參考:https://github.com/JerryYuanJ/a-vue-app-templatecss
建立一個vue-cli項目,建議在安裝的時候不要使用ESLINT作代碼檢查,練習的項目不須要這種檢查機制,會很浪費時間。還有一些自動化測試的插件也最好不要裝,影響效率。html
在安裝好腳手架的依賴後,要執行 npm install vue-html5-editor -S 來安裝這個富文本插件,因爲這個富文本插件的圖標是依賴font-awesome.css的,因此要npm install font-awesome.css 安裝這個css而後在main.js中引入這個css import "font-awesome/css/font-awesome.css"。vue
新建一個common文件夾用於存放咱們的工具類js文件,而後將下面的代碼copy進去:html5
/** * author: Joker * creationDate: 2018/1/22 * usage: */ import Vue from 'vue' import VueHtml5Editor from 'vue-html5-editor' export default function () { let opt = { // 全局組件名稱,使用new VueHtml5Editor(options)時該選項無效 name: "vue-html5-editor", // 是否顯示模塊名稱,開啓的話會在工具欄的圖標後臺直接顯示名稱 showModuleName: true, // 自定義各個圖標的class,默認使用的是font-awesome提供的圖標 icons: { text: "fa fa-pencil", color: "fa fa-paint-brush", font: "fa fa-font", align: "fa fa-align-justify", list: "fa fa-list", link: "fa fa-chain", unlink: "fa fa-chain-broken", tabulation: "fa fa-table", image: "fa fa-file-image-o", hr: "fa fa-minus", eraser: "fa fa-eraser", undo: "fa-undo fa", "full-screen": "fa fa-arrows-alt", info: "fa fa-info", }, // 配置圖片模塊 image: { // 文件最大致積,單位字節 sizeLimit: 512 * 1024 * 10, // 上傳參數,默認把圖片轉爲base64而不上傳 // upload config,default null and convert image to base64 upload: { url: null, headers: {}, params: {}, fieldName: {} }, // 壓縮參數,默認使用localResizeIMG進行壓縮,設置爲null禁止壓縮 // width和height是文件的最大寬高 compress: { width: 600, height: 600, quality: 80 }, // 響應數據處理,最終返回圖片連接 uploadHandler(responseText){ //default accept json data like {ok:false,msg:"unexpected"} or {ok:true,data:"image url"} var json = JSON.parse(responseText); console.info(json); if (!json.ok) { alert(json.msg) } else { return json.data } } }, // 語言,內建的有英文(en-us)和中文(zh-cn) language: "zh-cn", // 自定義語言 i18n: { "zh-cn": { "align": "對齊方式", "image": "圖片", "list": "列表", "link": "連接", "unlink": "去除連接", "table": "表格", "font": "文字", "full screen": "全屏", "text": "排版", "eraser": "格式清除", "info": "關於", "color": "顏色", "please enter a url": "請輸入地址", "create link": "建立連接", "bold": "加粗", "italic": "傾斜", "underline": "下劃線", "strike through": "刪除線", "subscript": "上標", "superscript": "下標", "heading": "標題", "font name": "字體", "font size": "文字大小", "left justify": "左對齊", "center justify": "居中", "right justify": "右對齊", "ordered list": "有序列表", "unordered list": "無序列表", "fore color": "前景色", "background color": "背景色", "row count": "行數", "column count": "列數", "save": "肯定", "upload": "上傳", "progress": "進度", "unknown": "未知", "please wait": "請稍等", "error": "錯誤", "abort": "中斷", "reset": "重置" } }, // 隱藏不想要顯示出來的模塊 hiddenModules: [], // 自定義要顯示的模塊,並控制順序 visibleModules: [ "text", "color", "font", "align", "list", "link", "unlink", "tabulation", "image", "hr", "eraser", "undo", "full-screen", "info", ], // 擴展模塊,具體能夠參考examples或查看源碼 // extended modules modules: { //omit,reference to source code of build-in modules } }; Vue.use(VueHtml5Editor, opt); }
接着在main.js中引入這個初始化的函數:java
import initRichText from './common/initHTMLEditor'; initRichText();
準備工做已經完成了,咱們可使用這個組件了:git
<template> <div class="content"> <vue-html5-editor :content="content" :height="400" @change="updateData"></vue-html5-editor> </div> </template> <style scoped> </style> <script> export default { data(){ return {content: '請輸入文章內容'} }, methods: { updateData(e = ''){ this.content = e; console.info(e); } } } </script>
這個height屬性是設置內容區的高度,content是內容區的數據內容,@change事件是內容區的監聽事件,會在發生變化時觸發,該函數接收一個參數,表示當前編輯器中的內容。運行結果以下(這裏對圖片的操做是轉成base64的字符串):github
a.自定義工具欄的模塊web
若是不想要顯示這麼多的工具,則只要配置visibleModules便可:vue-cli
b.工具欄的樣式修改
在移動端咱們一般但願工具欄能夠固定不動,而且顯示在頁面最下方,這時候咱們要修改該組件的核心js裏面的樣式代碼:
主要是將
var template$9 = "<div class=\"vue-html5-editor\" :class=\"{'full-screen':fullScreen}\".......省略很長的代碼....)
和
__$styleInject(`.vue-html5-editor,.vue-html5-editor *{box-sizing:border-box}.....省略很長的代碼......)
分別用下面兩個代替:
1.組件的字符串模板
var temlate$9=` <div class="vue-html5-editor" :class="{'full-screen':fullScreen}" :style="{'z-index':zIndex}"> <div class="content" ref="content" :style="contentStyle" contenteditable @click="toggleDashboard(dashboard)"></div> <div class="toolbar" :style="{'z-index':zIndex+1}" ref="toolbar"> <ul> <template v-for="module in modules"> <li :title="locale[module.i18n]" @click="activeModule(module)"><span class="icon" :class="module.icon"></span> <template v-if="showModuleName === undefined ? defaultShowModuleName : showModuleName"> {{locale[module.i18n]}} </template> </li> </template> </ul> <div class="dashboard" v-show="dashboard" ref="dashboard"> <keep-alive> <div v-show="dashboard" :is="dashboard"></div> </keep-alive> </div> </div> </div> `
和 2.主要的樣式字符串
` .vue-html5-editor, .vue-html5-editor * { box-sizing: border-box } .vue-html5-editor { font-size: 14px; line-height: 1.5; background-color: #fff; color: #333; border: 1px solid #ddd; text-align: left; border-radius: 5px; overflow: hidden } .vue-html5-editor.full-screen { position: fixed !important; top: 0 !important; left: 0 !important; bottom: 0 !important; right: 0 !important; border-radius: 0 } .vue-html5-editor > .toolbar { position: relative; background-color: inherit; border-top:1px solid #ccc } .vue-html5-editor > .toolbar > ul { list-style: none; padding: 0; margin: 0; border-bottom: 1px solid #ddd } .vue-html5-editor > .toolbar > ul > li { display: inline-block; cursor: pointer; text-align: center; line-height: 36px; padding: 0 10px } .vue-html5-editor > .toolbar > ul > li .icon { height: 16px; width: 16px; display: inline-block; vertical-align: middle } .vue-html5-editor > .toolbar > .dashboard { background-color: inherit; border-top: 1px solid #ddd; padding: 10px; position: absolute; bottom: 100%; left: 0; right: 0; overflow: auto } .vue-html5-editor > .toolbar > .dashboard input[type=text], .vue-html5-editor > .toolbar > .dashboard input[type=number], .vue-html5-editor > .toolbar > .dashboard select { padding: 6px 12px; color: inherit; background-color: transparent; border: 1px solid #ddd; border-radius: 5px } .vue-html5-editor > .toolbar > .dashboard input[type=text]:hover, .vue-html5-editor > .toolbar > .dashboard input[type=number]:hover, .vue-html5-editor > .toolbar > .dashboard select:hover { border-color: #bebebe } .vue-html5-editor > .toolbar > .dashboard input[type=text][disabled], .vue-html5-editor > .toolbar > .dashboard input[type=text][readonly], .vue-html5-editor > .toolbar > .dashboard input[type=number][disabled], .vue-html5-editor > .toolbar > .dashboard input[type=number][readonly], .vue-html5-editor > .toolbar > .dashboard select[disabled], .vue-html5-editor > .toolbar > .dashboard select[readonly] { background-color: #eee; opacity: 1 } .vue-html5-editor > .toolbar > .dashboard input[type=text][disabled], .vue-html5-editor > .toolbar > .dashboard input[type=number][disabled], .vue-html5-editor > .toolbar > .dashboard select[disabled] { cursor: not-allowed } .vue-html5-editor > .toolbar > .dashboard button { color: inherit; background-color: inherit; padding: 6px 12px; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; border: 1px solid #ddd; border-radius: 5px; margin-right: 4px; margin-bottom: 4px } .vue-html5-editor > .toolbar > .dashboard button:hover { border-color: #bebebe } .vue-html5-editor > .toolbar > .dashboard button[disabled] { cursor: not-allowed; opacity: .68 } .vue-html5-editor > .toolbar > .dashboard button:last-child { margin-right: 0 } .vue-html5-editor > .toolbar > .dashboard label { font-weight: bolder } .vue-html5-editor > .content { overflow: scroll; padding: 10px; max-height:500px } .vue-html5-editor > .content:focus { outline: 0 }`
修改的地方很少,請參照具體的樣式本身配置自定義的模板。
我這個修改完之後顯示以下。並且輸入很長的數據後,只是內容區變成上下滾動的,工具欄不動。
c.移動端圖片上傳的處理
這裏我沒有配置服務端的上傳文件的接口,因此我就直接將圖片轉成base64的來處理。可是這樣會有問題,在PC端圖片是能夠修改大小的,可是在移動端上傳的圖片上是原圖,也就是很大,圖文混排的時候很是很差看,也很差編輯。這時候我作的處理挺投機取巧的,可是也是可行的方法。咱們知道change函數是在內容發生變化時觸發的,這時候咱們只要將獲取到的內容作一下修改便可,看代碼:
updateData(e = ''){ let c1 = e.replace(/<img width="100%"/g, '<img'); let c2 = c1.replace(/<img/g, '<img width="100%"'); this.content = c2; }
這樣就OK了。
看移動端的效果圖:圖片是按寬100%自適應縮放的。效果達到。
本項目的git地址:https://github.com/JerryYuanJ/a-vue-app-template
主要參考 /src/pages/tool/RichTextTest.vue (使用),/src/init-plugins.js(配置)
若是有bug歡迎指正,不勝感激;若是對您有幫助,給個star,謝謝~~