vue富文本vue-quill-editor
这个富文本需要装一下插件
"quill": "^1.3.6"
"quill-image-drop-module": "^1.0.3", //压缩图片
"quill-image-resize-module": "^3.0.0", //图片大小控制
"vue-quill-editor": "^3.0.6",
使用
webpack中加一下配置
plugins: [
new webpack.ProvidePlugin({
'window.Quill': 'quill/dist/quill.js',
'Quill': 'quill/dist/quill.js'
})
],
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)
页面使用
<template>
<quill-editor
v-model="content"
:content="content"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@ready="onEditorReady($event)">
</quill-editor>
</template> <script>
import { Quill } from 'vue-quill-editor'
import { ImageDrop } from 'quill-image-drop-module'
import ImageResize from 'quill-image-resize-module'
Quill.register('modules/imageDrop', ImageDrop)
Quill.register('modules/imageResize', ImageResize) export default {
data () {
return {
name: 'register-modules-example',
content: `1231`,
editorOption: {
modules: {
history: {
delay: 1000,
maxStack: 50,
userOnly: false
},
toolbar: [
['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] }],
[{ 'font': [] }],
[{ 'color': [] }, { 'background': [] }],
[{ 'align': [] }],
['clean'],
['link', 'image', 'video']
],
imageDrop: true,
imageResize: {
displayStyles: {
backgroundColor: 'black',
border: 'none',
color: 'white'
},
modules: [ 'Resize', 'DisplaySize', 'Toolbar' ]
}
}
}
}
},
methods: {
onEditorBlur (editor) {
// console.log('editor blur!', editor)
console.log(editor)
},
onEditorFocus (editor) {
// console.log('editor focus!', editor)
},
onEditorReady (editor) {
// console.log('editor ready!', editor)
}
}
}
</script>
自定义图片上传 参考处
toolbar: {
container: toolbarOptions,
handlers: {
'image': function (value) {
if (value) {
// 调用element UI图片上传
document.querySelector('#uploadImg .el-upload').click()
} else {
this.quill.format('image', false)
}
}
}
},
handers处重写 图片的点击事件
触发 element 的 upload的点击事件
upload上传成功的回调事件
// 图片上传成功方法
handleSuccess (response, file, fileList) {
// 获取富文本组件实例
let quill = this.$refs.QuillEditor.quill
// 如果上传成功
if (response) {
// 获取光标所在位置
let length = quill.getSelection().index
// 插入图片,第三个参数为服务器返回的图片链接地址
quill.insertEmbed(length, 'image', response.data.url)
// 调整光标到最后
quill.setSelection(length + 1)
} else {
// 提示信息,需引入Message
alert('图片插入失败')
}
}
代码
<template>
<div class="quill-editor">
<el-upload
class="upload-demo"
action="http://mock/2/api/richUpload"
accept=".jpg,.jpeg,.png,.gif]"
:headers="headers"
:show-file-list="false"
:on-success="handleSuccess"
:before-upload="beforeUpload"
id="uploadImg"
ref="uploadImg"
>
上传
</el-upload>
<quill-editor
v-model="content"
:content="content"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@ready="onEditorReady($event)"
ref="QuillEditor">
</quill-editor>
</div>
</template> <script>
import { Quill } from 'vue-quill-editor'
import { ImageDrop } from 'quill-image-drop-module'
import ImageResize from 'quill-image-resize-module'
Quill.register('modules/imageDrop', ImageDrop)
Quill.register('modules/imageResize', ImageResize) const toolbarOptions = [
['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] }],
[{ 'font': [] }],
[{ 'color': [] }, { 'background': [] }],
[{ 'align': [] }],
// ['clean'],
['link', 'image', 'video']
]
export default {
data () {
return {
name: 'register-modules-example',
content: `1231`,
editorOption: {
modules: {
history: {
delay: 1000,
maxStack: 50,
userOnly: false
},
toolbar: {
container: toolbarOptions,
handlers: {
'image': function (value) {
if (value) {
// 调用iview图片上传
console.log(document.querySelector('#uploadImg .el-upload'))
document.querySelector('#uploadImg .el-upload').click()
console.log(value, 1231)
} else {
console.log(212222222222222)
this.quill.format('image', false)
}
}
}
},
imageDrop: true,
imageResize: {
displayStyles: {
backgroundColor: 'black',
border: 'none',
color: 'white'
},
modules: [ 'Resize', 'DisplaySize', 'Toolbar' ]
}
}
},
headers: {
'Authorization': 'Bearer ' + sessionStorage.getItem('token')
}
}
},
methods: {
onEditorBlur (editor) {
// console.log('editor blur!', editor)
console.log(editor)
},
onEditorFocus (editor) {
// console.log('editor focus!', editor)
},
onEditorReady (editor) {
// console.log('editor ready!', editor)
},
// 上传之前加认证信息
beforeUpload () {
this.headers.Authorization = 'Bearer ' + sessionStorage.getItem('token')
},
// 图片上传成功方法
handleSuccess (response, file, fileList) {
// 获取富文本组件实例
let quill = this.$refs.QuillEditor.quill
// 如果上传成功
if (response) {
// 获取光标所在位置
let length = quill.getSelection().index
// 插入图片,res为服务器返回的图片链接地址
quill.insertEmbed(length, 'image', response.data.url)
// 调整光标到最后
quill.setSelection(length + 1)
} else {
// 提示信息,需引入Message
// Message.error('图片插入失败')
alert('图片插入失败')
}
}
}
}
</script>
tip: 在显示的时候会出现样式丢失的问题,需要加一个 class = "ql-editor"
<div class=" ql-editor" v-html="item.content"></div>
vue富文本vue-quill-editor的更多相关文章
- vue 富文本编辑器 项目实战用法
1.挑个富文本编辑器 首先针对自己项目的类型,确定自己要用啥编辑器. 1.1 wangeditor 如果一般类似博客这种项目不需要花里胡哨的,功能也不要求贼多的,推荐一下wangeditor(点击跳转 ...
- vue+富文本编辑器UEditor
vue+富文本编辑器UEditor 昨天的需求是把textarea换成富文本编辑器的形式, 网上找了几种富文本编辑器ueditor.tinymce等, 觉得ueditor实现双向绑定还挺有意思, 分享 ...
- 富文本编辑器...quill 的使用放...
移动端 quill 时候用的 是 div 而不是 textarea.... 引入 dom <link href="//cdn.quilljs.com/1.3.6/quill.snow. ...
- 现代富文本编辑器Quill的内容渲染机制
DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...
- vue富文本编辑器vue-quill-editor使用总结(包含图片上传,拖拽,放大和缩小)
vue-quill-editor是vue很好的富文本编辑器,富文本的功能基本上都支持,样式是黑白色,简洁大方. 第一步下载 vue-quill-editor: npm i vue-quill-edit ...
- Vue富文本编辑器(图片拖拽缩放)
富文本编辑器(图片拖拽缩放) 需求: 根据业务要求,需要能够上传图片,且上传的图片能在移动端中占满屏幕宽度,故需要能等比缩放上传的图片,还需要能拖拽.缩放.改变图片大小.尝试多个第三方富文本编辑器,很 ...
- 富文本编辑器Quill(一)简单介绍
Quill是一个很流行的富文本编辑器,github上star大约21k: github:https://github.com/quilljs/quill/ 官网: https://quilljs.co ...
- 富文本编辑器Quill(二)上传图片与视频
image与video在Quill formats中属于Embeds,要在富文本中插入图片或者视频需要使用insertEmbed api. insertEmbed insertEmbed(index: ...
- 现代富文本编辑器Quill的模块化机制
DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...
随机推荐
- Windows 08 R2_NLB负载均衡(图文详解)
目录 目录 Load Balance 使用NLB来部署Web Farm集群 环境准备 在Win08r2pc1中配置DNS服务 在Win08r2pc1中部署File Service文件服务 在Win08 ...
- 畜禽免疫系统使用LODOP打印
<div class="btn_box"> <asp:Button ID="btnPrint" Text="预览并打印" ...
- PAT_A1131#Subway Map
Source: PAT A1131 Subway Map (30 分) Description: In the big cities, the subway systems always look s ...
- leetcode.矩阵.73矩阵置零-Java
1. 具体题目 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: 输出:[ [ [1,1,1], [1,0,1], [ ...
- Python中反射的简单应用
● 共两个文件:userInfo,reflex.py alex|123456|Manager hezewei|666|Student taibai|2222|Teachar userInfo #!/u ...
- jsp页面通过ajax取值/展示数据及分页显示
jsp页面通过ajax从后台获取数据,在页面展示,并实现分页效果代码: [JavaScript部分代码] 1 <script> function getComposition(pageno ...
- TOPO DN 解析
介绍 有一个算法,是将大量TOPO DN实例解析成结构形式.并依据DN获取对应的数据.本人感觉值得分享.并供大家讨论与优化. 注意:TOPO DN实例的顺序,是被我的其他算法预处理过的 ...
- css 深入理解
场景一.边框半透明,背景绿色 默认情况下背景会延伸到边框所在的下边 css2 中我们只能接受 css3 中我们可以通过 background-clip 属性来实现 border: 10px soli ...
- 案例-3D旋转木马
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android消息处理:EventBus、BroadCast和Handler-优缺点比较
上一篇研究了EventBus的使用方法,但随之而来的一系列问题也是值得思考,EventBus到底给项目带来了什么?它与Android原有的消息处理机制有什么区别和优缺点?项目在什么场景下采用Event ...