富文本使用wangEditor,vue中的使用方法

wangEditor官網:http://www.wangeditor.com/index.html
注:請先熟悉使用wangEditor,在開始在vue中使用,這樣更加快捷應用
首先先進行下載
npm install wangeditor
在使用的頁面中,按照如下方法使用
這樣就完成了在vue中的使用了。無需別的操作了

<template>
    <div>
        <div ref="editor" style="text-align:left" class="textBox">
          <p>嘴上說著愛你,心里罵你傻逼</p>
        </div>
        <button v-on:click="getContent">查看內容</button>
    </div>
</template>

<script>
    import E from 'wangeditor'
    export default {
      data () {
        return {
          editorContent: '',
          editor:null
        }
      },
      methods: {
        getContent: function () {
            // alert(this.editorContent)
          var json = this.editor.txt.getJSON()  // 獲取 JSON 格式的內容
          var jsonStr = JSON.stringify(json)
          console.log(json)
          console.log(jsonStr)
          console.log(this.editorContent,'----------------------')
        }
      },
      mounted() {
        this.editor = new E(this.$refs.editor)
        this.editor.customConfig.onchange = (html,a) => {
        // console.log(html,'----------------',a)
          this.editorContent = html
        }
        // this.editor.customConfig.uploadImgShowBase64 = true // 使用base64保存圖片  上下兩者不可同用
        this.editor.customConfig.uploadImgServer = 'http://chuantu.xyz/'  // 上傳圖片到服務器
        // 隱藏“網絡圖片”tab
        this.editor.customConfig.showLinkImg = false
        this.editor.create()
      }
    }
</script>

<style scoped>
</style>

官網中的api文檔重點介紹

基礎使用
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>wangEditor 一個頁面多個編輯器</title>
<style type="text/css">
.toolbar {
background-color: #f1f1f1;
border: 1px solid #ccc;
}
.text {
border: 1px solid #ccc;
height: 200px;
}
</style>
</head>
<body>
<div id="div1" class="toolbar">
</div>
<div style="padding: 5px 0; color: #ccc">中間隔離帶</div>
<div id="div2" class="text">
<p>第一個 demo(菜單和編輯器區域分開)</p>
</div>

<div id="div3">
    <p>第二個 demo(常規)</p>
</div>

<!-- 引用js -->
<script type="text/javascript" src="/wangEditor.min.js"></script>

-------------------多個編輯器--------------------
<script type="text/javascript">
var E = window.wangEditor

    var editor1 = new E('#div1', '#div2')
    editor1.create()

    var editor2 = new E('#div3')
    editor2.create()
</script>

-----------------------最基礎----------------------
<div id="editor">
<p>歡迎使用 <b>wangEditor</b> 富文本編輯器</p>
</div>
<script type="text/javascript">
var E = window.wangEditor
var editor = new E('#editor')
// 或者 var editor = new E( document.getElementById('editor') )
editor.create()
</script>
</body>
</html>


// 禁用編輯功能
editor.$textElem.attr('contenteditable', false)

// 開啟編輯功能
editor.$textElem.attr('contenteditable', true)


//設置內容
editor.txt.html(...)
//追加內容
editor.txt.html(...)
//清空內容
editor.txt.clear()
--------------讀取內容---------------
document.getElementById('btn1').addEventListener('click', function () {
// 讀取 html
alert(editor.txt.html())
}, false)

document.getElementById('btn2').addEventListener('click', function () {
    // 讀取 text
    alert(editor.txt.text())
}, false)

-------------------vue中讀取帶標簽的內容mounted() {}中操作----------------
editor.customConfig.onchange = (html) => {
//監控變化,同步更新到text area
this.editorContent = html //帶標簽的內容html賦值得data中的變量editorContent
}
-------------------------自定義菜單-------------------------
// 自定義菜單配置
editor.customConfig.menus = [
'head',
'bold',
'italic',
'underline'
]
//默認菜單
[
'head', // 標題
'bold', // 粗體
'fontSize', // 字號
'fontName', // 字體
'italic', // 斜體
'underline', // 下劃線
'strikeThrough', // 刪除線
'foreColor', // 文字顏色
'backColor', // 背景顏色
'link', // 插入鏈接
'list', // 列表
'justify', // 對齊方式
'quote', // 引用
'emoticon', // 表情
'image', // 插入圖片
'table', // 表格
'video', // 插入視頻
'code', // 插入代碼
'undo', // 撤銷
'redo' // 重復
]
-----------------debug模式---------------------
// 通過 url 參數配置 debug 模式。url 中帶有 wangeditor_debug_mode=1 才會開啟 debug 模式
editor.customConfig.debug = location.href.indexOf('wangeditor_debug_mode=1') > 0


用戶自己使用 JS 修改了div1的innerHTML,不會自動觸發onchange函數,此時你可以通過執行editor.change()來手動觸發onchange函數的執行。
editor.customConfig.onchange = function (html) {
// html 即變化之后的內容
console.log(html)
}
editor.create()

document.getElementById('btn1').addEventListener('click', function () {
    // 如果未配置 editor.customConfig.onchange,則 editor.change 為 undefined
    editor.change && editor.change()
})

--------------------配置編輯區域的z-index----------------
editor.customConfig.zIndex = 100


// 關閉粘貼樣式的過濾
editor.customConfig.pasteFilterStyle = false
// 忽略粘貼內容中的圖片
editor.customConfig.pasteIgnoreImg = true
// 自定義處理粘貼的文本內容
editor.customConfig.pasteTextHandle = function (content) {
    // content 即粘貼過來的內容(html 或 純文本),可進行自定義處理然后返回
    return content + '<p>在粘貼內容后面追加一行</p>'
}

-------------網絡圖片------------
--------插入網絡圖片的回調
editor.customConfig.linkImgCallback = function (url) {
console.log(url) // url 即插入圖片的地址
}
--------插入鏈接的校驗
editor.customConfig.linkCheck = function (text, link) {
console.log(text) // 插入的文字
console.log(link) // 插入的鏈接

return true // 返回 true 表示校驗成功
// return '驗證失敗' // 返回字符串,即校驗失敗的提示信息

}
--------插入網絡圖片的校驗
editor.customConfig.linkImgCheck = function (src) {
console.log(src) // 圖片的鏈接

return true // 返回 true 表示校驗成功
// return '驗證失敗' // 返回字符串,即校驗失敗的提示信息

}
-----------用戶點擊富文本區域會觸發onfocus
editor.customConfig.onfocus = function () {
console.log("onfocus")
}
-----------手動獲取焦點的富文本并且鼠標點擊富文本以外的區域,則會觸發onblur函數執行
editor.customConfig.onblur = function (html) {
// html 即編輯器中的內容
console.log('onblur', html)
}


// 自定義配置顏色(字體顏色、背景色)
editor.customConfig.colors = [
    '#000000',
    '#eeece0',
    '#1c487f',
    '#4d80bf',
    '#c24f4a',
    '#8baa4a',
    '#7b5ba1',
    '#46acc8',
    '#f9963b',
    '#ffffff'
]

// 表情面板可以有多個 tab ,因此要配置成一個數組。數組每個元素代表一個 tab 的配置
editor.customConfig.emotions = [
{
// tab 的標題
title: '默認',
// type -> 'emoji' / 'image'
type: 'image',
// content -> 數組
content: [
{
alt: '[壞笑]',
src: 'http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/50/pcmoren_huaixiao_org.png'
},
{
alt: '[舔屏]',
src: 'http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/40/pcmoren_tian_org.png'
}
]
},
{
// tab 的標題
title: 'emoji',
// type -> 'emoji' / 'image'
type: 'emoji',
// content -> 數組
content: ['??', '??', '??', '??', '??']
}
]


// 自定義字體
editor.customConfig.fontNames = [
    '宋體',
    '微軟雅黑',
    'Arial',
    'Tahoma',
    'Verdana'
]

------------------------------------------上傳圖片-服務器---------------------
上傳圖片
參考如下代碼

<div id="div1">
<p>歡迎使用 wangEditor 富文本編輯器</p>
</div>

<script type="text/javascript" src="/wangEditor.min.js"></script>
<script type="text/javascript">
var E = window.wangEditor
var editor = new E('#div1')

// 配置服務器端地址
editor.customConfig.uploadImgServer = '/upload'

// 進行下文提到的其他配置
// ……
// ……
// ……

editor.create()

</script>
其中/upload是上傳圖片的服務器端接口,接口返回的數據格式如下(實際返回數據時,不要加任何注釋!!!)

{
// errno 即錯誤代碼,0 表示沒有錯誤。
// 如果有錯誤,errno != 0,可通過下文中的監聽函數 fail 拿到該錯誤碼進行自定義處理
"errno": 0,

// data 是一個數組,返回若干圖片的線上地址
"data": [
    "圖片1地址",
    "圖片2地址",
    "……"
]

}
限制圖片大小
默認限制圖片大小是 5M

// 將圖片大小限制為 3M
editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024
限制一次最多能傳幾張圖片
默認為 10000 張(即不限制),需要限制可自己配置

// 限制一次最多上傳 5 張圖片
editor.customConfig.uploadImgMaxLength = 5
自定義上傳參數
上傳圖片時可自定義傳遞一些參數,例如傳遞驗證的token等。參數會被添加到formdata中。

editor.customConfig.uploadImgParams = {
// 如果版本 <=v3.1.0 ,屬性值會自動進行 encode ,此處無需 encode
// 如果版本 >=v3.1.1 ,屬性值不會自動 encode ,如有需要自己手動 encode
token: 'abcdef12345'
}
如果還需要將參數拼接到 url 中,可再加上如下配置

editor.customConfig.uploadImgParamsWithUrl = true
自定義 fileName
上傳圖片時,可自定義filename,即在使用formdata.append(name, file)添加圖片文件時,自定義第一個參數。

editor.customConfig.uploadFileName = 'yourFileName'
自定義 header
上傳圖片時刻自定義設置 header

editor.customConfig.uploadImgHeaders = {
'Accept': 'text/x-json'
}
withCredentials(跨域傳遞 cookie)
跨域上傳中如果需要傳遞 cookie 需設置 withCredentials

editor.customConfig.withCredentials = true
自定義 timeout 時間
默認的 timeout 時間是 10 秒鐘

// 將 timeout 時間改為 3s
editor.customConfig.uploadImgTimeout = 3000
監聽函數
可使用監聽函數在上傳圖片的不同階段做相應處理

editor.customConfig.uploadImgHooks = {
before: function (xhr, editor, files) {
// 圖片上傳之前觸發
// xhr 是 XMLHttpRequst 對象,editor 是編輯器對象,files 是選擇的圖片文件

    // 如果返回的結果是 {prevent: true, msg: 'xxxx'} 則表示用戶放棄上傳
    // return {
    //     prevent: true,
    //     msg: '放棄上傳'
    // }
},
success: function (xhr, editor, result) {
    // 圖片上傳并返回結果,圖片插入成功之后觸發
    // xhr 是 XMLHttpRequst 對象,editor 是編輯器對象,result 是服務器端返回的結果
},
fail: function (xhr, editor, result) {
    // 圖片上傳并返回結果,但圖片插入錯誤時觸發
    // xhr 是 XMLHttpRequst 對象,editor 是編輯器對象,result 是服務器端返回的結果
},
error: function (xhr, editor) {
    // 圖片上傳出錯時觸發
    // xhr 是 XMLHttpRequst 對象,editor 是編輯器對象
},
timeout: function (xhr, editor) {
    // 圖片上傳超時時觸發
    // xhr 是 XMLHttpRequst 對象,editor 是編輯器對象
},

// 如果服務器端返回的不是 {errno:0, data: [...]} 這種格式,可使用該配置
// (但是,服務器端返回的必須是一個 JSON 格式字符串!!!否則會報錯)
customInsert: function (insertImg, result, editor) {
    // 圖片上傳并返回結果,自定義插入圖片的事件(而不是編輯器自動插入圖片!!!)
    // insertImg 是插入圖片的函數,editor 是編輯器對象,result 是服務器端返回的結果

    // 舉例:假如上傳圖片成功后,服務器端返回的是 {url:'....'} 這種格式,即可這樣插入圖片:
    var url = result.url
    insertImg(url)

    // result 必須是一個 JSON 格式字符串!!!否則報錯
}
}

}
自定義提示方法
上傳圖片的錯誤提示默認使用alert彈出,你也可以自定義用戶體驗更好的提示方式

editor.customConfig.customAlert = function (info) {
// info 是需要提示的內容
alert('自定義提示:' + info)
}
自定義上傳圖片事件
如果想完全自己控制圖片上傳的過程,可以使用如下代碼

editor.customConfig.customUploadImg = function (files, insert) {
// files 是 input 中選中的文件列表
// insert 是獲取圖片 url 后,插入到編輯器的方法

// 上傳代碼返回結果之后,將圖片插入到編輯器中
insert(imgUrl)

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,431評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,637評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,555評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,900評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,629評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,976評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,976評論 3 448
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,139評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,686評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,411評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,641評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,129評論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,820評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,233評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,567評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,362評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,604評論 2 380