在vue项目中使用富文本框vue-quill-editor可以上传文件和视频
下载Vue-Quill-Editornpm install vue-quill-editor --save下载quill(Vue-Quill-Editor需要依赖)npm install quill --save代码redact.vue编辑页<template><div :class="prefixCls"><quill-e...
·
- 下载Vue-Quill-Editor
npm install vue-quill-editor --save
- 下载quill(Vue-Quill-Editor需要依赖)
npm install quill --save
- 代码
redact.vue编辑页
<template>
<div :class="prefixCls">
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@ready="onEditorReady($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'
import quillConfig from '@/utils/quill-config'
export default {
name: 'QuillEditor',
components: {
quillEditor
},
data() {
return {
content: '请做',
editorOption: quillConfig
}
},
methods: {
onEditorBlur(quill) {
console.log('editor blur!', quill)
},
onEditorFocus(quill) {
console.log('editor focus!', quill)
},
onEditorReady(quill) {
console.log('editor ready!', quill)
},
onEditorChange({ quill, html, text }) {
this.content = html
console.log('editor change!', quill, html, text)
//this.$emit('change', html)
}
},
watch: {
value(val) {
this.content = val
}
}
}
</script>
<style lang="less" scoped>
@import url('../index.less');
/* 覆盖 quill 默认边框圆角为 ant 默认圆角,用于统一 ant 组件风格 */
.ant-editor-quill {
/deep/ .ql-toolbar.ql-snow {
border-radius: @border-radius-base @border-radius-base 0 0;
}
/deep/ .ql-container.ql-snow {
border-radius: 0 0 @border-radius-base @border-radius-base;
}
}
</style>
4.vue-quill-editor --save上传图片和视频的配置(utils/quill-config)
import { fileUpload } from '@/api/exam/personalInfo'
/*富文本编辑图片上传配置*/
const uploadConfig = {
// action: 'https://www.lumingtec.cn/Business/serviceInterface/fileUpload.json', // 必填参数 图片上传地址
methods: 'POST', // 必填参数 图片上传方式
token: '', // 可选参数 如果需要token验证,假设你的token有存放在sessionStorage
name: 'file', // 必填参数 文件的参数名
size: 700, // 可选参数 图片大小,单位为Kb, 1M = 1024Kb
accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon', // 可选 可上传的图片格式
type:'audio/mp4,video/mp4'
}
// toolbar工具栏的工具选项(默认展示全部)
const toolOptions = [
['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: ['small', false, 'large', 'huge'] }],
[{ header: [1, 2, 3, 4, 5, 6, false] }],
[{ color: [] }, { background: [] }],
[{ font: [] }],
[{ align: [] }],
['clean'],
['link', 'image', 'video']
]
const handlers = {
//配置上传图片
image: function image() {
var self = this
var fileInput = this.container.querySelector('input.ql-image[type=file]')
if (fileInput === null) {
fileInput = document.createElement('input')
fileInput.setAttribute('type', 'file')
// 设置图片参数名
if (uploadConfig.name) {
fileInput.setAttribute('name', uploadConfig.name)
}
// 可设置上传图片的格式
fileInput.setAttribute('accept', uploadConfig.accept)
fileInput.classList.add('ql-image')
// 监听选择文件
fileInput.addEventListener('change', function() {
// 创建formData
var formData = new FormData()
formData.append(uploadConfig.name, fileInput.files[0])
// 如果需要token且存在token
if (uploadConfig.token) {
formData.append('token', uploadConfig.token)
}
fileUpload(formData).then(response => {
console.log('upload response:', response)
let url = 'https://www.lumingtec.cn/Business/file/' + response.fileName
let length = self.quill.getSelection().index //获取当前鼠标焦点位置
self.quill.insertEmbed(length, 'image',url)
self.quill.setSelection(length + 1)
})
})
this.container.appendChild(fileInput)
}
fileInput.click()
},
//配置上传视频
video: function(value) {
var self = this
var fileInput = this.container.querySelector('input.ql-video[type=file]')
if (fileInput === null) {
fileInput = document.createElement('input')
fileInput.setAttribute('type', 'file')
if (uploadConfig.name) {
fileInput.setAttribute('name', uploadConfig.name)
}
fileInput.setAttribute('accept', uploadConfig.type)
fileInput.classList.add('ql-video')
fileInput.addEventListener('change', function() {
var formData = new FormData()
formData.append(uploadConfig.name, fileInput.files[0])
if (uploadConfig.token) {
formData.append('token', uploadConfig.token)
}
fileUpload(formData).then(response => {
console.log('upload response:', response)
let url = 'https://www.lumingtec.cn/Business/file/' + response.fileName
let length = self.quill.getSelection().index //获取当前鼠标焦点位置
self.quill.insertEmbed(length, 'video',url)
})
})
this.container.appendChild(fileInput)
}
fileInput.click()
}
}
export default {
placeholder: '',
theme: 'snow', // 主题
modules: {
toolbar: {
container: toolOptions, // 工具栏选项
handlers: handlers // 事件重写
}
}
}
更多推荐
已为社区贡献3条内容
所有评论(0)