vue富⽂本编辑器--vue-quill-editor
下载依赖:
npm install vue-quill-editor --save
全局注册:
import Vue from 'vue'
import VueQuillEditor from 'vue-quill-editor'
// require styles
import 'quill/ss'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Vue.use(VueQuillEditor, /* { default global options } */)
或者局部引⼊:
import 'quill/ss'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
components: {
quillEditor
}
}
使⽤⽅法(我是⽤elementUI的,代码中⽤了其中⽂件上传的组件el-upload,来代替富⽂本中的图⽚上传)注释很清楚,结合⽂档理解:
<template>
<div class="navone">
<div v-loading="quillUpdateImg" element-loading-text="请稍等,图⽚上传中">
<!-- 图⽚上传组件辅助-->
<el-upload
class="avatar-uploader"
:action="serverUrl"
name="file"
:headers="header"
:show-file-list="false"
:data="uploadData"
:
on-success="uploadSuccess"
:on-error="uploadError"
:before-upload="beforeUpload">
</el-upload>
<quill-editor
asp富文本编辑器class="editor"
v-model="content"
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@change="onEditorChange($event)">
</quill-editor>
</div>
</div>
</template>
<script type="text/ecmascript-6">
import { quillEditor } from "vue-quill-editor";
import "quill/ss";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
// ⼯具栏配置
const toolbarOptions = [
["bold", "italic", "underline", "strike"], // 加粗斜体下划线删除线
["blockquote", "code-block"], // 引⽤代码块
[{ header: 1 }, { header: 2 }], // 1、2 级标题
[{ list: "ordered" }, { list: "bullet" }], // 有序、⽆序列表
[{ script: "sub" }, { script: "super" }], // 上标/下标
[{ indent: "-1" }, { indent: "+1" }], // 缩进
// [{'direction': 'rtl'}], // ⽂本⽅向
[{ size: ["small", false, "large", "huge"] }], // 字体⼤⼩
[{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
[{ color: [] }, { background: [] }], // 字体颜⾊、字体背景颜⾊
[{ font: [] }], // 字体种类
[{ align: [] }], // 对齐⽅式
["clean"], // 清除⽂本格式
["link", "image", "video"] // 链接、图⽚、视频
];
export default {
name: 'wxmaterial',
data() {
return {
uploadData: {}, // 图⽚⽂件
serverUrl: "/dev-api/wxmaterial/newsImgUpload", // 这⾥写你要上传的图⽚服务器地址
header: { // 有的图⽚服务器要求请求头需要有token,有的可以加上
Authorization: 'Bearer '
} ,
quillUpdateImg: false, // 根据图⽚上传状态来确定是否显⽰loading动画,刚开始是false,不显⽰ content: '', // 内容
editorOption: { // 配置项
placeholder: "",
theme: "snow", // or 'bubble'
placeholder: "请输⼊⽂章内容",
modules: {
toolbar: {
container: toolbarOptions,
// container: "#toolbar",
handlers: {
image: function(value) {
if (value) {
// 触发input框选择图⽚⽂件
document.querySelector(".avatar-uploader input").click();
} else {
this.quill.format("image", false);
}
},
link: function(value) {
if (value) {
var href = prompt('注意!只⽀持图⽂链接');
this.quill.format("link", href);
} else {
this.quill.format("link", false);
}
},
}
}
}
},
}
components: {
quillEditor
},
mounted () {
},
methods: {
onEditorBlur(editor) {
//失去焦点事件
console.t)
},
onEditorFocus(editor) {
//获得焦点事件
},
onEditorChange(editor) {
//内容改变事件
},
// 富⽂本图⽚上传前
beforeUpload() {
// 显⽰loading动画
this.quillUpdateImg = true
},
uploadSuccess(res, file) {
// res为图⽚服务器返回的数据
// 获取富⽂本组件实例
let quill = this.$QuillEditor.quill
// 如果上传成功
if(res.link){
// 获取光标所在位置
let length = Selection().index;
// 插⼊图⽚ res.info为服务器返回的图⽚地址 quill.insertEmbed(length, 'image', res.link) // 调整光标到最后
quill.setSelection(length + 1)
} else {
this.$('图⽚插⼊失败')
}
// loading动画消失
this.quillUpdateImg = false;
},
// 富⽂本图⽚上传失败
uploadError() {
// loading动画消失
this.quillUpdateImg = false;
this.$("图⽚插⼊失败");
}
}
}
</script>
<style scoped lang="less">
.editor {
line-height: normal !important;
height: 500px;
width: 700px;
margin: 20px auto;
}
.ql-snow .ql-tooltip[data-mode=link]::before {
content: "请输⼊链接地址:";
}
.ql-snow .ql-tooltip.ql-editing a.ql-action::after { border-right: 0px;
content: '保存';
padding-right: 0px;
.ql-snow .ql-tooltip[data-mode=video]::before {
content: "请输⼊视频地址:";
}
.ql-snow .ql-picker.ql-size .ql-picker-label::before,
.ql-snow .ql-picker.ql-size .ql-picker-item::before {
content: '14px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value=small]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value=small]::before {
content: '10px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value=large]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value=large]::before {
content: '18px';
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value=huge]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value=huge]::before {
content: '32px';
}
.ql-snow .ql-picker.ql-header .ql-picker-label::before,
.ql-snow .ql-picker.ql-header .ql-picker-item::before {
content: '⽂本';
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
content: '标题1';
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
.
ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
content: '标题2';
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
content: '标题3';
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
content: '标题4';
}
.
ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
content: '标题5';
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
content: '标题6';
}
.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
content: '标准字体';
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value=serif]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value=serif]::before {
content: '衬线字体';
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value=monospace]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value=monospace]::before { content: '等宽字体';
}
</style>
效果:
就酱~
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论