1:下载富文本和更改图片大小
- npm install quill-image-resize-module --save//更改img图片大小
- npm install vue-quill-editor –save//下载富文本编辑
2:在main.js中引入
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Vue.use(VueQuillEditor)
3:在vue.config.js中配置
module.exports = {
configureWebpack: {
plugins: [
new webpack.ProvidePlugin({
"window.Quill": "quill/dist/quill.js",
Quill: "quill/dist/quill.js",
}),
]
}
}
4.视频接入 video.js
import Quill from "quill";
const BlockEmbed = Quill.import('blots/block/embed')
class VideoBlot extends BlockEmbed {
static create (value) {
let node = super.create()
node.setAttribute('src', value.url)
node.setAttribute('controls', value.controls)
node.setAttribute('width', value.width)
node.setAttribute('height', value.height)
node.setAttribute('webkit-playsinline', true)
node.setAttribute('playsinline', true)
node.setAttribute('x5-playsinline', true)
return node;
}
static value (node) {
return {
url: node.getAttribute('src'),
controls: node.getAttribute('controls'),
width: node.getAttribute('width'),
height: node.getAttribute('height')
};
}
}
VideoBlot.blotName = 'video'
VideoBlot.tagName = 'video'
VideoBlot.className = 'ql-video'
export default VideoBlot;
5:在组件中编写
<template>
<div>
<!-- 图片上传组件辅助 -->
<el-upload :action="uploadImgUrl" ref="uploadFileImage" :data="imgCol" :before-upload="handleBeforeUpload"
:on-success="handleUploadSuccess" :on-error="uploadError" name="file" :show-file-list="false"
:headers="headers" style="display: none" />
<!--视频上传组件辅助 -->
<el-upload :action="uploadImgUrl" ref="uploadFileVideo" :data="imgColVideo"
:before-upload="handleBeforeUploadVideo" :on-success="handleUploadSuccessVideo"
:on-error="handleUploadErrorVideo" name="file" :show-file-list="false" :headers="headers"
style="display: none" />
<!-- 富文本组件 -->
<!-- <quill-editor :id="randomString(3)" class="editor" v-model="content" :ref="toref" :options="editorOption"
@blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)"></quill-editor> -->
<quill-editor class="editor" v-model="content" ref="toref" :options="editorOption" @blur="onEditorBlur($event)"
@focus="onEditorFocus($event)" @change="onEditorChange($event)" v-loading="quillUpdateImg" >
</quill-editor>
</div>
</template>
<script>
import ImageResize from 'quill-image-resize-module'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Quill.register('modules/imageResize', ImageResize)
import { quillEditor } from 'vue-quill-editor'
let Size = Quill.import('attributors/style/size')
Size.whitelist = ['10px', '12px', '14px', '16px', '18px', '20px']
Quill.register(Size, true)
var fonts = ['SimSun', 'SimHei', 'Microsoft-YaHei', 'KaiTi', 'FangSong', 'Arial', 'Times-New-Roman', 'sans-serif']
var Font = Quill.import('formats/font')
Font.whitelist = fonts
Quill.register(Font, true)
import video from './video'
Quill.register(video, true)
export default {
props: {
value: {
type: String
},
maxSize: {
type: Number,
default: 4000
},
quillIndex: {
type: Number,
default: 0
},
toref: {
type: String,
default: "quillEditor"
},
},
components: { quillEditor },
data() {
return {
content: this.value,
editorOption: {
placeholder: '输入文本...',
theme: 'snow',
modules: {
imageResize: {
displayStyles: {
backgroundColor: 'black',
border: 'none',
color: 'white'
},
modules: ['Resize', 'DisplaySize', 'Toolbar']
},
toolbar: {
container: [
['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' }],
[{ 'header': [1, 2, 3, 4, 5, 6, false] }],
[{ 'color': [] }, { 'background': [blur()] }],
[{ 'align': [] }],
['clean'],
['image', 'video']
],
handlers: {
video: this.handelVideo,
image: this.handelImage,
}
}
}
},
quillUpdateImg:false,
imgCol: {
},
imgColVideo: {
},
uploadImgUrl: '',
headers: {
},
};
},
watch: {
value: function () {
this.content = this.value;
}
},
methods: {
onEditorBlur() {
},
onEditorFocus() {
},
onEditorChange() {
this.$emit("input", this.content);
},
handelVideo() {
this.uploadType = "video";
this.$nextTick(() => {
this.$refs.uploadFileVideo.$children[0].$refs.input.click();
});
},
handelImage() {
this.uploadType = "image";
this.$nextTick(() => {
this.$refs.uploadFileImage.$children[0].$refs.input.click();
});
},
handleBeforeUpload(file) {
this.quillUpdateImg = true
if (this.uploadType === "image") {
let regArr = [".gif", ".jpg", ".jpeg", ".png"];
let lastName = file.name.slice(file.name.indexOf("."));
if (regArr.indexOf(lastName) == -1) {
this.$message.error(`仅支持.gif/.jpg/.jpeg/.png格式!`);
this.quillUpdateImg = false
return false;
}
}
},
handleUploadSuccess(res, file) {
let quill = this.$refs.toref.quill
console.log(quill)
console.log(quill.getSelection)
if (res.status == '0') {
if (this.uploadType === "image") {
let length = quill.getSelection().index;
quill.insertEmbed(length, 'image', res.data.fileUrl)
quill.setSelection(length + 1)
}
} else {
this.$message.error("插入失败");
}
this.quillUpdateImg = false
},
uploadError() {
if (this.uploadType != "image") {
this.$message.error("图片插入失败!");
}
},
handleBeforeUploadVideo(file) {
this.quillUpdateImg = true
console.log(file)
if (this.uploadType === "video") {
let acceptType = [
"mp4",
"avi",
"mpg",
"rmvb",
];
let isVideo = null;
acceptType.some((d) => {
if (file.type.includes(d)) {
isVideo = true;
}
});
if (!isVideo) {
this.$message.error(
"上传视频只能是 mp4, avi, mpg, rmvb格式!"
);
this.quillUpdateImg = false
return false;
}
return true;
}
},
handleUploadSuccessVideo(res, file) {
let quill = this.$refs.toref.quill
if (res.status == '0') {
if (this.uploadType === "video") {
let length = quill.getSelection().index;
quill.insertEmbed(length, 'video', {
url:res.data.fileUrl,
controls: 'controls',
width: '100%',
height: 'auto'
})
}
quill.setSelection(length + 1);
this.$message.success(res.data.msg)
} else {
this.$message.error("插入失败");
}
this.quillUpdateImg = false
},
handleUploadErrorVideo() {
if (this.uploadType != "video") {
this.$message.error("视频插入失败!");
}
},
}
};
</script>
<style>
</style>
6:在父组件中引入
<template>
<div>
<Editor v-model="输入地址" :quillIndex="2" toref="Editorc" placeholder="请输入内容"/>
</div>
</template>
<script>
import Editor from '@/components/index.vue'
export default {
components: {
Editor
},
data() {
return {
}
}
}
</script>