在Vue项目使用quill-editor带样式编辑器(更改插入图片和视频)
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框,用于点击图片或者视频图标上传文件
<template>
<div>
<!-- quill-editor插件标签 分别绑定各个事件-->
<quill-editor v-model="content" ref="myQuillEditor" :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
@change="onEditorChange($event)">
</quill-editor>
<div class="limit">当前已输入 <span>{{nowLength}}</span> 个字符,您还可以输入 <span>{{SurplusLength}}</span> 个字符。</div>
<!-- 文件上传input 将它隐藏-->
<el-upload class="upload-demo" :action="qnLocation" :before-upload='beforeUpload' :data="uploadData" :on-success='upScuccess'
ref="upload" style="display:none">
<el-button size="small" type="primary" id="imgInput" v-loading.fullscreen.lock="fullscreenLoading" element-loading-text="插入中,请稍候">点击上传</el-button>
</el-upload>
</el-table>
</div>
</template>
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 'util/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 === 'video') {
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 {
(<any>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) {
(<any>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项目引入自定义.css的样式文件
ES6的引入方式: .vue文件中 css文件引入 <template></template> <style scoped> @import "../as ...
- 在 Vue 项目中引入 tinymce 富文本编辑器
项目中原本使用的富文本编辑器是 wangEditor,这是一个很轻量.简洁编辑器 但是公司的业务升级,想要一个功能更全面的编辑器,我找了好久,目前常见的编辑器有这些: UEditor:百度前端的开源项 ...
- vue 项目全局修改element-ui的样式
引入了element-ui,但是和我们自己的样式颜色有很大的不同, 修改例子:在src文件下创建 element-var.scss,代码如下 $--color-primary: yellow; /* ...
- vue项目中引入element-ui时,如何更改主题色
在我们做项目时,我们经常会遇到切换主题色的功能,下面我们就来说一下通过颜色选择器我们就能改变项目的主题颜色 代码如下: 颜色选择器所在组件: top-theme.vue内容如下: <templa ...
- vue项目中使用百度富文本编辑器ueditor
第一步,安装依赖,并且把ueditor整个文件夹放入public里边 第二步,在你需要编辑的地方引入,或者main.js中全局引入 XX.vue文件中写入下面代码,创建编辑器. <vue-ued ...
- vue项目中,无需打包而动态更改背景图以及标题
1.背景 今天,项目经理对已完成的项目提出了一个需求,即项目的基础功能目前针对于各个基层法院都适用,而对于不同的法院,我们每次都需要前端研发来更改所属法院的法院名称以及背景图,这样对于演示者来说是非常 ...
- vue2.0项目中使用Ueditor富文本编辑器示例
最近在vue项目中需要使用富文本编辑器,于是将Ueditor集成进来,作为公共组件. 在线预览:https://suweiteng.github.io/vue2-management-platform ...
- SpringBoot + Vue + nginx项目部署(零基础带你部署)
一.环境.工具 jdk1.8 maven spring-boot idea VSVode vue 百度网盘(vue+springboot+nginx源码): 链接:https://pan.baidu. ...
- vue quill editor输入文字出现首字母的问题
当使用vue quill editor输入中文时,第一个中文的汉语拼音第一个字母会显示如图. 解决的办法就是升级vue quill editor js文件的版本,目前的我升级之后ok的版本是 < ...
随机推荐
- 万能选项卡,tab选项卡
//万能选项卡 function PaPtabs(thisObj, num) { if (thisObj.className == "active") return; var ta ...
- Parallels Desktop 12
我微新solq123987654 备注:PD 科普:PD12有什么用,PD是让mac系统可以运行wind系统的软件,如果你不习惯mac os 或工作需要那PD绝对是个好软件正版要七八百授权,只要十五就 ...
- K:java中的序列化与反序列化
Java序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java序列化与反序列化?以下内容将围绕这些问题进行展开讨论. Java序列化与反序列化 简单来说Java序列化是指把Java对象转 ...
- 在Maven Central发布中文API的Java库
原址: https://zhuanlan.zhihu.com/p/28024364 相关问题: 哪些Java库有中文命名的API? 且记下随想. 之前没有发布过, 看了SO上的推荐:Publish a ...
- Vuejs之开发环境搭建
Vue.js Vue.js是目前很火的一个前端框架,采用MVVM模式设计,它是以数据驱动和组件化的思想构建的.相比于Angular.js,Vue.js提供了更加简洁.更易于理解的API,使得我们能够快 ...
- 讲述Sagit.Framework解决:双向引用导致的IOS内存泄漏(下)- block中任性用self
前言: 在处理完框架内存泄漏的问题后,见上篇:讲述Sagit.Framework解决:双向引用导致的IOS内存泄漏(中)- IOS不为人知的Bug 发现业务代码有一个地方的内存没释放,原因很也简单: ...
- TPYBoard实例之利用WHID为隔离主机建立隐秘通道
本文作者:xiaowuyi,来自FreeBuf.COM(MicroPythonQQ交流群:157816561,公众号:MicroPython玩家汇) 0引言 从2014年BADUSB出现以后,USB- ...
- java web 之 AJAX用法
AJAX :Asynchronous JavaScript And XML 指异步 JavaScript 及 XML一种日渐流行的Web编程方式 Better Faster User-Friendly ...
- 【转】Jenkins 安装与配置
基本配置: 1.Linux安装配置jdk环境 1.1.上传到 Linux 服务器:例如: 上传至: cd /usr/local 1.2.解压: rpm -ivh jdk-8u111-linux-x64 ...
- bootstrapru软件官网
一 bootstrap:http://v3.bootcss.com/ 二 起步:http://v3.bootcss.com/getting-started/ 三 全局css样式:htt ...