vue-quill-editor的使用及個(gè)性化定制操作
最近在用vue + element ui寫一個(gè)小應(yīng)用要用到富文本編輯器,以前做項(xiàng)目都一直都用ueditor,但是看了一下它與vue的兼容性并不好,又對(duì)比了幾個(gè)后,選擇了vue-quill-editor。
vue-quill-editor基于Quill、適用于 Vue 的富文本編輯器,支持服務(wù)端渲染和單頁應(yīng)用,正是我想要的☻。這里只介紹基本的安裝和部分簡單的定制。我翻了很多別人寫的東西對(duì)我的項(xiàng)目都無效,最后自己折騰出來在這記錄備忘。
一、安裝
1.安裝模塊
npm install vue-quill-editor –save
2.vue組件
<template>
<div class="edit_container">
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
@change="onEditorChange($event)">
</quill-editor>
</div>
</template>
<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor';
export default {
name: "addJournal",
components: {
quillEditor
},
data() {
return {
content: ``,
editorOption: {},
};
},
methods: {
onEditorReady(editor) {}, // 準(zhǔn)備編輯器
onEditorBlur(){}, // 失去焦點(diǎn)事件
onEditorFocus(){}, // 獲得焦點(diǎn)事件
onEditorChange(){}, // 內(nèi)容改變事件
},
computed: {
editor() {
return this.$refs.myQuillEditor.quill;
},
},
}
</script>
至此,vue-quill-editor就安裝完成了,效果圖如下:

二、定(zhe)制(teng)
這里只簡單介紹兩類操作: 樣式修改和自定義工具欄。
1.樣式修改
a) 修改vue-quill-editor編輯框高度
這個(gè)其實(shí)很簡單了,只要在vue組件的<style>標(biāo)簽里增加一個(gè)樣式即可
.quill-editor{
height: 400px;
}
在調(diào)整了編輯框的高度后,如果編輯內(nèi)容的高度超過了編輯框的高度,編輯框會(huì)出現(xiàn)滾動(dòng)條(不手動(dòng)調(diào)整此高度話會(huì)一直往下擴(kuò)展)。
b) 修改工具欄對(duì)齊方式
這里需要注意,使用webstorm創(chuàng)建的vue組件中,styte標(biāo)簽的默認(rèn)會(huì)加上scoped屬性, 也就是說,只對(duì)當(dāng)前模塊的元素有效,而工具欄是從外部引入的,因此下面的樣式要寫在無scoped屬性的style標(biāo)簽里才會(huì)有效。
.ql-toolbar.ql-snow{
text-align: left;
}
修改完后的樣式如下

2.定制工具欄按鈕
以字體大小調(diào)節(jié)為例,這是默認(rèn)的調(diào)節(jié)按鈕,我們想改成多個(gè)像素大小的下拉選框。

step1: 在vue組件中引入quill模塊,修改whitelist, 并注冊樣式
import * as Quill from 'quill';
let fontSizeStyle = Quill.import('attributors/style/size');
fontSizeStyle.whitelist = ['10px', '12px', '14px', '16px', '20px', '24px', '36px', false];//false表示默認(rèn)值
Quill.register(fontSizeStyle, true);
step2: 修改quill-editor的option屬性值
editorOption: {
modules: {
toolbar: [["bold", "italic", "underline", "strike"], ["blockquote", "code-block"], [{header: 1}, {header: 2}], [{list: "ordered"}, {list: "bullet"}], [{script: "sub"}, {script: "super"}], [{indent: "-1"}, {indent: "+1"}], [{direction: "rtl"}],
[{size: fontSizeStyle.whitelist}], [{header: [1, 2, 3, 4, 5, 6, !1]}], [{color: []}, {background: []}], [{font: []}], [{align: []}], ["clean"], ["link", "image", "video"]],
},
}
這個(gè)modules里面的值是參照vue-quill-editor模塊里的vue-quill-editor.js里的modules值設(shè)置的,只需要將你要修改的工具欄按鈕的值替換成step1里設(shè)置的whitelist值即可。
step3: 增加定制選項(xiàng)的css樣式
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='10px']::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value='10px']::before {
content: '10px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='12px']::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value='12px']::before {
content: '12px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='14px']::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value='14px']::before {
content: '14px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='16px']::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value='16px']::before {
content: '16px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='20px']::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value='20px']::before {
content: '20px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='24px']::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value='24px']::before {
content: '24px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='36px']::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value='36px']::before {
content: '36px';
}
此樣式的選擇器可以從quill.snow.css.js中找到,我們要做的只是修改它的data-value值。
修改后的工具欄:

以上這篇vue-quill-editor的使用及個(gè)性化定制操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在Vue中創(chuàng)建可重用的 Transition的方法
這篇文章主要介紹了在Vue中創(chuàng)建可重用的 Transition,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Vue ECharts設(shè)置主題實(shí)現(xiàn)方法介紹
這篇文章主要介紹了Vue ECharts設(shè)置主題,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
微信內(nèi)置開發(fā) iOS修改鍵盤換行為搜索的解決方案
今天小編就為大家分享一篇微信內(nèi)置開發(fā) iOS修改鍵盤換行為搜索的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue進(jìn)度條組件實(shí)現(xiàn)代碼(可拖拽可點(diǎn)擊)
在日常開發(fā)中隨著需求的個(gè)性化,邏輯的復(fù)雜化,自定義組件也變得越來越常見,這篇文章主要給大家介紹了關(guān)于vue進(jìn)度條組件實(shí)現(xiàn)(可拖拽可點(diǎn)擊)的相關(guān)資料,需要的朋友可以參考下2023-12-12

