Vue?quill-editor?編輯器使用及自定義toobar示例詳解
vue使用編輯器,這里講解編輯器quil-editor
官網(wǎng):https://quilljs.com/docs/modules/toolbar
1:安裝quill-eidtor
npm i quill@1.3.6 --save
2:創(chuàng)建一個(gè)頁(yè)面,再template里寫入
<template> <div class="quill-editor-box" :class="{ 'h-min-40': height === 40 }"> <div ref="myQuillEditor" :style="style" class="quill-editor"> <!--這個(gè)是輸入的各種按鈕,現(xiàn)在是簡(jiǎn)單實(shí)用,自定義的看后面--> <slot name="toolbar"></slot> <!--這個(gè)是編輯器--> <div ref="editor"></div> </div> </div> </template>
在script中引入依賴 及quill的toobar的各種按鈕的簡(jiǎn)單配置
<script> // 主庫(kù) import _Quill from "quill"; // 核心庫(kù),不包含主題、格式化及非必要模塊 import "quill/dist/quill.core.css"; // 主題樣式表 import "quill/dist/quill.snow.css"; import "quill/dist/quill.bubble.css"; import katex from "katex"; import { lineHeightStyle } from "@/js/style.js"; export default { data() { return { quill: null, //這個(gè)是ref指定quill editorIndex: 0, editorOption: { theme: "snow", //這是是snow模式,一共兩個(gè)模式,snow模式是有toobar的,另一個(gè)模式是沒(méi)有的,只有編輯框quill modules: { toolbar: [ // 這里是tootbar樣式 // 加粗 、斜體、底部橫線、中間橫線 ["bold", "italic", "underline", "strike"], // 引號(hào) 、 插入代碼 ["blockquote", "code-block"], // 有序列表、 無(wú)序列表 [{ list: "ordered" }, { list: "bullet" }], // 字體:這里可以寫一些字體,也可以自定義引入 [{ 'font': ['SimSun', 'SimHei','Microsoft-YaHei','KaiTi','FangSong','Arial'] }], // 大小 [{ size: ["12px","14px",false,"18px","20px","22px","24px","26px","28px","30px","32px","34px","36px"] }], // 標(biāo)題 [{ header: [1, 2, 3, 4, 5, 6, false] }], // 字體顏色 、 背景顏色 [{ color: [] }, { background: [] }], // 排列方式 [{ align: [] }], // 下標(biāo)、 上標(biāo) [{ 'script': 'sub' }, { 'script': 'super' }], // 縮進(jìn) [{ 'indent': '-1' }, { 'indent': '+1' }], // 方向 [{ 'direction': 'rtl' }], // 行高 可自定義 [{ lineHeight: lineHeightStyle.whitelist }], // 清除樣式 ["clean"], ["link", "video", "image"], ], formula: this.formula(), }, placeholder: "請(qǐng)輸入內(nèi)容...", readOnly: false, }, }; }, mounted() { this.init(); }, methods: { init() { // 獲取quill,在這里做一些自定義處理 this.quill = new _Quill(this.$refs["myQuillEditor"], this.editorOption); //禁用編輯器,防止頁(yè)面自動(dòng)滾動(dòng)到編輯器位置 this.quill.enable(false); // 初始值 this.quill.pasteHTML(this.value || ""); this.$nextTick(function () { //丟掉編輯器焦點(diǎn)并重新啟用編輯器 this.quill.blur(); this.quill.enable(true); }); // 自定義imageHandler 對(duì)圖片按鈕添加的自定義方法 this.quill.getModule("toolbar").addHandler("image", () => { this.showUploadImage = true; }); // 自定義插入視頻 對(duì)視頻按鈕添加的自定義方法 this.quill.getModule("toolbar").addHandler("video", () => { this.dialogFormVisible = true; }); // 監(jiān)聽記錄Index this.quill.on("selection-change", (range, oldRange) => { if (oldRange === null || oldRange.index === 0) { this.editorIndex = this.quill.getLength(); } else { this.editorIndex = oldRange.index; } }); // 值變化 this.quill.on( "text-change", debounce(() => { let html = this.$refs.myQuillEditor.children[0].innerHTML; if (html === "<p><br></p>") { html = ""; } this.onEditorChange(html); }, 400) ); }, onEditorChange(val) { this.$emit("inputvalue", val); }, confirm() { this.quill.focus(); if (!/^<iframe.+<\/iframe>$/.test(this.form.videoIframe)) { this.form.videoIframe = ""; return; } var range = this.quill.getSelection(); if (range) { // 在當(dāng)前光標(biāo)位置插入圖片 this.quill .getModule("toolbar") .quill.clipboard.dangerouslyPasteHTML( range.index, this.form.videoIframe ); // 將光標(biāo)移動(dòng)到圖片后面 this.quill.getModule("toolbar").quill.setSelection(range.index + 1); } this.form.videoIframe = ""; this.dialogFormVisible = false; }, uploadImage(imgUrl) { let index = this.editorIndex; let nextIndex = this.editorIndex + 1; let html = this.$refs.myQuillEditor.children[0].innerHTML; if (html === "<p><br></p>") { index = 0; nextIndex = 1; } this.quill.insertEmbed(index, "image", imgUrl); this.quill.getModule("toolbar").quill.setSelection(nextIndex); this.showUploadImage = false; }, }, }; </script>
上面這是簡(jiǎn)潔版,直接可以寫出一個(gè)帶有多種按鈕操作的編輯器。
效果如下
3:自定義toobar中各個(gè)小標(biāo)簽
除了可以對(duì)原來(lái)的小button設(shè)置顏色,還可以對(duì)原來(lái)的添加文字描述,并且還可以有長(zhǎng)期hover后的彈框提示。
在template中原來(lái)toobar的地方換成標(biāo)簽
<template> <div class="quill-editor-box" :class="{ 'h-min-40': height === 40 }"> <div ref="myQuillEditor" :style="style" class="quill-editor"> <!-- <slot name="toolbar"></slot> --> <div id="toolbar" style="background-color: white;"> <span class="ql-formats" > <!-- 這里clas都是上面toobar中l(wèi)ist中的寫的前面再加一個(gè)ql-這個(gè) --> <button class="ql-bold"></button> <button id="so">加粗</button> <button class="ql-italic"></button> <button class="ql-underline"></button> <button class="ql-strike"></button> <!-- spsdf --> </span> <span class="ql-formats"> <!-- <button class="ql-header" value="1"></button> <button class="ql-header" value="2"></button> --> <button class="ql-blockquote"></button> <!-- 這里是我添加的文字,自定義,嘗試過(guò)用span用div,并不能把布局弄的很好,相反會(huì)挺糟糕。 --> <button id="so">引號(hào)</button> <button class="ql-code-block"></button> </span> <span class="ql-formats" id="fom2"> <button class="ql-list" value="ordered"></button> <button class="ql-list" value="bullet"></button> <!-- 這里是我添加的文字,自定義,嘗試過(guò)用span用div,并不能把布局弄的很好,相反會(huì)挺糟糕。 --> <button id="so">列表</button> </span> <span class="ql-formats"> <select class="ql-font"> <option value="SimSun"></option> <option value="SimHei"></option> <option value="Microsoft-YaHei"></option> <option value="KaiTi"></option> <option value="FangSong"></option> <option value="Arial"></option> </select> <!-- 這里是我添加的文字,自定義,嘗試過(guò)用span用div,并不能把布局弄的很好,相反會(huì)挺糟糕。 --> <button id="so">字體</button> <select class="ql-size"> <option value="12px"></option> <option value="14px"></option> <option selected></option> <option value="18px"></option> <option value="20px"></option> <option value="22px"></option> <option value="24px"></option> <option value="26px"></option> <option value="28px"></option> <option value="30px"></option> <option value="32px"></option> <option value="34px"></option> <option value="36px"></option> </select> </span> <span class="ql-formats"> <select class="ql-header"> <option value="1"></option> <option value="2"></option> <option value="3"></option> <option value="4"></option> <option value="5"></option> <option value="6"></option> <option value="7" selected></option> </select> </span> <span class="ql-formats"> <select class="ql-color"></select> <button id="so">顏色</button> <select class="ql-background"></select> </span> <span class="ql-formats"> <select class="ql-align"></select> </span> <span class="ql-formats"> <button class="ql-script" value="sub"></button> <button id="so">下標(biāo)</button> <button class="ql-script" value="super"></button> </span> <span class="ql-formats"> <button class="ql-indent" value="-1"></button> <button class="ql-indent" value="+1"></button> </span> <span class="ql-formats"> <button class="ql-direction" value="rtl" title="設(shè)置方向"></button> </span> <span class="ql-formats"> <select class="ql-lineHeight" title="行高"> <option value="1" selected></option> <option value="1.5"></option> <option value="2"></option> <option value="2.5"></option> <option value="3"></option> <option value="3.5"></option> <option value="4"></option> <option value="4.5"></option> </select> </span> <span class="ql-formats"> <button class="ql-link"></button> <button class="ql-video"></button> <button class="ql-image"></button> <!-- <button class="ql-formula"></button> --> </span> <span class="ql-formats"> <button class="ql-clean"></button> </span> <span class="ql-formats"> <!-- title是長(zhǎng)期hover后彈出的彈框提示 --> <button id="custom-img" title="設(shè)置背景" @click="setImg"></button> </span> </div> <div class="editor" ref="editor" style="height: 380px; background-color: white; "></div> </div> </div> </template>
而script中需要改成這樣
export default { data() { return { quill: null, editorIndex: 0, editorOption: { theme: "snow", modules: { // toolbar: this.toolbar(), toolbar: '#toolbar', }, placeholder: "請(qǐng)輸入內(nèi)容...", readOnly: false, }, }; },
css
<style lang="less" scoped> .ql-formats { margin-top: 10px; margin-bottom: 20px; position: relative; } #so { position: absolute; top: 17px; padding: 0px; // left:50%-30px; left:2px; font-size: 11px; color:red; } </style>
樣式如下
原三方按鈕的顏色修改,添加提示文字(顏色和位置可以修改),添加自定義按鈕(最后一個(gè)就是),長(zhǎng)期hoverbutton后出現(xiàn)的彈框文字提示。
總:寫的時(shí)候會(huì)遇到很多問(wèn)題,尤其是自定義的時(shí)候
到此這篇關(guān)于Vue quill-editor 編輯器使用及自定義toobar示例詳解的文章就介紹到這了,更多相關(guān)vue quill-editor 編輯器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue-quill-editor富文本編輯器超詳細(xì)入門使用步驟
- vue使用vue-quill-editor富文本編輯器且將圖片上傳到服務(wù)器的功能
- 淺談vue中使用編輯器vue-quill-editor踩過(guò)的坑
- 解決Vue的文本編輯器 vue-quill-editor 小圖標(biāo)樣式排布錯(cuò)亂問(wèn)題
- Vue+Element UI+vue-quill-editor富文本編輯器及插入圖片自定義
- 詳解Vue基于vue-quill-editor富文本編輯器使用心得
- Vue-Quill-Editor富文本編輯器的使用教程
- Vue項(xiàng)目中quill-editor帶樣式編輯器的使用方法
相關(guān)文章
vue中報(bào)錯(cuò)Duplicate?keys?detected:'1'.?This?may?c
我們?cè)趘ue開發(fā)過(guò)程中常會(huì)遇到一些錯(cuò)誤,這篇文章主要給大家介紹了關(guān)于vue中報(bào)錯(cuò)Duplicate?keys?detected:‘1‘.?This?may?cause?an?update?error的解決方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03vue調(diào)用微信JSDK 掃一掃,相冊(cè)等需要注意的事項(xiàng)
這篇文章主要介紹了vue調(diào)用微信JSDK 掃一掃,相冊(cè)等需要注意的事項(xiàng),幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2021-01-01vue項(xiàng)目如何讀取本地json文件數(shù)據(jù)實(shí)例
很多時(shí)候我們需要從本地讀取JSON文件里面的內(nèi)容,這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目如何讀取本地json文件數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2023-06-06編寫Vue項(xiàng)目,如何給數(shù)組的第一位添加對(duì)象數(shù)據(jù)
這篇文章主要介紹了編寫Vue項(xiàng)目,如何給數(shù)組的第一位添加對(duì)象數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue項(xiàng)目中向數(shù)組添加元素的3種方式
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中向數(shù)組添加元素的3種方式,在Vue中添加元素到數(shù)組非常簡(jiǎn)單,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10elementui el-table中如何給表頭 el-table-column 加一個(gè)鼠
本文通過(guò)實(shí)例代碼介紹如何在使用Element UI的el-table組件時(shí)為表頭添加提示功能,通過(guò)結(jié)合el-tooltip組件實(shí)現(xiàn)鼠標(biāo)移入時(shí)顯示提示信息,感興趣的朋友跟隨小編一起看看吧2024-11-11elementui中el-input回車搜索實(shí)現(xiàn)示例
這篇文章主要介紹了elementui中el-input回車搜索實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06