Vue项目中quill

网友投稿 454 2023-04-22


Vue项目中quill

vue-quill-editor默认插入图片是直接将图片转为base64再放入内容中,如果图片比较大的话,富文本的内容就会很大。

插入视频是直接弹框输入URL地址,某些需求下我们需要让用户去本地选择自己的视频,我们可以通过vue-quill-editor内部的某些方法进行更改

该方法使用了 element-ui 和 文件上传七牛

一、npm 安装 vue-quill-editor

二、在main.js中引入

import VueQuillEditor from 'vue-quill-editor'

Vue.use(VueQuillEditor)

HTML部分:为编辑器绑定各个API事件,定义一个隐藏的input框,用于点击图片或者视频图标上传文件

@change="onEditorChange($event)">

@change="onEditorChange($event)">

ref="upload" style="display:none">

点击上传

ref="upload" style="display:none">

点击上传

css部分:

.quill-editor {

height: 745px;

.ql-container {

height: 680px;

}

}

.limit {

height: 30px;

border: 1px solid #ccc;

line-height: 30px;

text-align: right;

span {

color: #ee2a7b;

}

}

.ql-snow .ql-editor img {

max-width: 480px;

}

.ql-editor .ql-video {

max-width: 480px;

}

JS部分:

import Vue from 'uhttp://til/vueExt'

import { Component } from 'vue-property-decorator'

import * as Template from './editor.vue'

import * as Quill from 'quill' // 引入编辑器

const STATICDOMAIN = '//ss.yidejia.com/'

const STATVIDEO = '//flv.yidejia.com/'

@Component({

mixins: [Template]

})

export default class Editor extends Vue {

content = '' // 文章内容

editorOption = {} // 编辑器选项

addRange: any = new Array()

uploadData = {}

photoUrl = '' // 上传图片地址

uploadType = '' // 上传的文件类型(图片、视频)

fullscreenLoading = false

$refs: {

myQuillEditor: any,

imgInput: any

}

get nowLength() {

return this.content.length

}

get SurplusLength() { // 计算属性 获得当前输入字符长度

let num = 10000 - Number(this.content.length)

if (num > 0) {

return num

} else {

return 0

}

}

// 上传七牛的actiond地址

get qnLocation() {

if (location.protocol === 'http:') {

return 'http://up-z0.qiniu.com'

}

return 'https://up-z0.qbox.me'

}

// 图片上传前获得数据token数据

qnUpload(file) {

this.fullscreenLoading = true

const suffix = file.name.split('.')

const ext = suffix.splice(suffix.length - 1, 1)[0]

console.log(this.uploadType)

if (this.uploadType === 'image') { // 如果是点击插入图片

return this.api.getQNToken().then(res => {

this.uploadData = {

key: `image/${suffix.join('.')}_${new Date().getTime()}.${ext}`,

token: res

}

})

} else if (this.uploadType === 'video') { // 如果是点击插入视频

return this.api.getVideoQNToken().then(res => {

this.uploadData = {

key: `video/${suffix.join('.')}_${new Date().getTime()}.${ext}`,

token: res

}

})

}

}

// 图片上传之前调取的函数

beforeUpload(file) {

return this.qnUpload(file)

}

// 图片上传成功回调 插入到编辑器中

upScuccess(e, file, fileList) {

this.fullscreenLoading = false

let vm = this

let url = ''

if (this.uploadType === 'image') { // 获得文件上传后的URL地址

url = STATICDOMAIN + e.key

} else if (this.uploadType === 'vidhttp://eo') {

url = STATVIDEO + e.key

}

if (url != null && url.length > 0) { // 将文件上传后的URL地址插入到编辑器文本中

let value = url

vm.addRange = vm.$refs.myQuillEditor.quill.getSelection()

value = value.indexOf('http') !== -1 ? value : 'http:' + value

vm.$refs.myQuillEditor.quill.insertEmbed(vm.addRange !== null ? vm.addRange.index : 0, vm.uploadType, value, Quill.sources.USER) // 调用编辑器的 insertEmbed 方法,插入URL

} else {

(this).$message.error(`${vm.uploadType}插入失败`)

}

this.$refs['upload'].clearFiles() // 插入成功后清除input的内容

}

// 点击图片ICON触发事件

imgHandler(state) {

this.addRange = this.$refs.myQuillEditor.quill.getSelection()

if (state) {

let fileInput = document.getElementById('imgInput')

fileInput.click() // 加一个触发事件

}

this.uploadType = 'image'

}

// 点击视频ICON触发事件

videoHandler(state) {

this.addRange = this.$refs.myQuillEditor.quill.getSelection()

if (state) {

let fileInput = document.getElementById('imgInput')

fileInput.click() // 加一个触发事件

}

this.uploadType = 'video'

}

// 编辑器光标离开 将编辑器内容发射给父组件

onEditorBlur(editor) {

this.$emit('getValue', this.content)

}

// 编辑器获得光标

onEditorFocus(editor) {

editor.enable(true) // 实现达到上限字符可删除

}

// 编辑器文本发生变化

onEditorChange({ editor, html, text }) {

let textLength = text.length

if (textLength > 10000) {

(this).$message.error('最多输入10000个字符')

editor.enable(false)

}

this.$emit('getValue', this.content)

}

// 清除编辑器内容

callMethod() {

this.content = ''

}

// 页面加载后执行 为编辑器的图片图标和视频图标绑定点击事件

mounted() {

// 为图片ICON绑定事件 getModule 为编辑器的内部属性

this.$refs.myQuillEditor.quill.getModule('toolbar').addHandler('image', this.imgHandler)

this.$refs.myQuillEditor.quill.getModule('toolbar').addHandler('video', this.videoHandler) // 为视频ICON绑定事件

}

}

相关参考链接:

vue-quill-editor实现图片上传功能

vue-quill-editor API文档地址

本文已被整理到了《Vue.js前端组件学习教程》,欢迎大家学习阅读。

关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:用Vue.extend构建消息提示组件的方法实例
下一篇:线上课程接口设计(线上课堂设计定义)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~