vue-quill-editor(富文本) 在 vue 中如何使用

2024-01-29 18:38

本文主要是介绍vue-quill-editor(富文本) 在 vue 中如何使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  1. 安装 vue-quill-editor
    npm install vue-quill-editor -S
  2. 安装quill
    npm install quill -S
  3. 引入
    import { quillEditor } from 'vue-quill-editor' // 调用富文本编辑器
    import 'quill/dist/quill.snow.css' // 富文本编辑器外部引用样式 三种样式三选一引入即可
    //import 'quill/dist/quill.core.css'
    //import 'quill/dist/quill.bubble.css'
    import * as Quill from 'quill'; // 富文本基于quill
  4. 使用

<!-- template部分 -->

<quill-editorv-model="content"ref="myQuillEditor":options="editorOption"@focus="onEditorFocus($event)"@blur="onEditorBlur($event)"@change="onEditorChange($event)">
</quill-editor>

<!-- js data部分 -->

``

editor: null,   // 富文本编辑器对象
content: `<p></p><p><br></p><ol></ol>`, // 富文本编辑器默认内容
editorOption: { //  富文本编辑器配置modules: {toolbar: '#toolbar'},theme: 'snow',placeholder: '请输入正文'
},

<!-- js mounted部分 -->

this.editor = this.$refs.myQuillEditor.quill;

<!-- js beforeDestroy部分 -->

this.editor = null;
delete this.editor;

<!-- js methods部分 -->

// 准备富文本编辑器
onEditorReady (editor) {},
// 富文本编辑器 失去焦点事件
onEditorBlur (editor) {},
// 富文本编辑器 获得焦点事件
onEditorFocus (editor) {},
// 富文本编辑器 内容改变事件
onEditorChange (editor) {},

<!-- js components部分 -->

components: {quillEditor
}
  1. 如果想自定义工具栏:
<!-- template部分 -->
<div id="toolbar" slot="toolbar"><button class="ql-bold" title="加粗">Bold</button><select class="ql-header" title="段落格式"><option selected>正文</option><option value="2">标题1</option><option value="3">标题2</option><option value="4">标题3</option></select><button class="ql-list" value="ordered" title="有序列表"></button><button class="ql-list" value="bullet" title="无序列表"></button><select class="ql-color" value="color" title="字体颜色"></select><span class="icon-pic custom-icon" title="图片" @click="insertImgClick($event)"></span>  <!-- 插入图片 --><span class="icon-video custom-icon" title="视频" @click="insertImgClick($event)"></span>  <!-- 插入视频 -->
</div>
<input style="display: none;" type="file" id="insert_image" @change="fileInsert($event)">  <!-- 选择图片input -->
<input style="display: none;" type="file" id="insert_video" @change="fileInsert($event)">  <!-- 选择视频input --><!-- js methods部分 -->
// 富文本编辑器 点击插入图片或者视频
insertImgClick (e) {if (e.target.className.indexOf('icon-pic') != -1) {document.getElementById('insert_image').click();} else if (e.target.className.indexOf('icon-video') != -1) {document.getElementById('insert_video').click();}
},
// 富文本编辑器 点击插入图片或者视频上传并预览
fileInsert (e) {var oFile = e.target.files[0];if (typeof (oFile) === 'undefined') {return;}let sExtensionName = oFile.name.substring(oFile.name.lastIndexOf('.') + 1).toLowerCase();   // 文件扩展名let sfileType = ''; // 上传文件类型if (e.target.id == 'insert_image') {sfileType = 'image'if (sExtensionName !== 'png' && sExtensionName !== 'jpg' && sExtensionName !== 'jpeg') {alert('不支持该类型图片');return;}}if (e.target.id == 'insert_video') {sfileType = 'video';if (sExtensionName !== 'mp4' && sExtensionName !== 'avi' && sExtensionName !== 'mov') {alert('不支持该类型视频');return;}let maxSize = 100*1024*1024;    // 100MBif (oFile.size > maxSize) {alert('上传视频大小不能超过100MB');return;}}var reader = new FileReader();reader.readAsDataURL(oFile);reader.onloadend = () => {let formData = new FormData(); // 通过formdata上传formData.append('file', oFile);let sUrl = '';if (sfileType == 'image') {sUrl = 'Pic';}if (sfileType == 'video') {sUrl = 'Vie';}var url = this.api_config + '/dealerIndex/upload' + sUrl + '.htm';this.axios.post(url, formData, {headers: { 'Content-Type': 'multipart/form-data' }}).then((res) => {this.editor.insertEmbed(this.editor.selection.savedRange.index, sfileType, res.data.data);  // 这个方法用来手动插入dom到编辑器里let isAndroid = this.$is_android(); // 判断是ios还是androidif (isAndroid) {$('video').removeAttr('controls');$('video').attr('x5-video-player-type', 'h5');}this.editor.setSelection(this.editor.selection.savedRange.index + 1);  // 这个方法可以获取光标位置}).catch((response) => {console.log('失败', response);})}
},
关于编辑器里面的一些操作可以到quill官方查看,用法和配置项的的介绍很详细,很好用
  1. 如果想更好地操作插入的video,可以把iframe改成h5的video,通过下面这段代码:
import { Quill } from 'vue-quill-editor'// 源码中是import直接倒入,这里要用Quill.import引入
const BlockEmbed = Quill.import('blots/block/embed')
const Link = Quill.import('formats/link')const ATTRIBUTES = ['height', 'width']class Video extends BlockEmbed {static create (value) {const node = super.create(value)// 添加video标签所需的属性node.setAttribute('controls', 'controls')   // 控制播放器node.setAttribute('type', 'video/mp4')node.setAttribute('style', 'object-fit:fill;width: 100%;')node.setAttribute('preload', 'auto')    // auto - 当页面加载后载入整个视频  meta - 当页面加载后只载入元数据  none - 当页面加载后不载入视频node.setAttribute('webkit-playsinline', 'true') // 兼容ios 不全屏播放node.setAttribute('playsinline', 'true')node.setAttribute('x-webkit-airplay', 'allow')// node.setAttribute('x5-video-player-type', 'h5') // 启用H5播放器,是wechat安卓版特性node.setAttribute('x5-video-orientation', 'portraint') // 竖屏播放 声明了h5才能使用  播放器支付的方向,landscape横屏,portraint竖屏,默认值为竖屏node.setAttribute('x5-playsinline', 'true') // 兼容安卓 不全屏播放node.setAttribute('x5-video-player-fullscreen', 'true')    // 全屏设置,设置为 true 是防止横屏node.setAttribute('src', this.sanitize(value))return node}static formats (domNode) {return ATTRIBUTES.reduce((formats, attribute) => {if (domNode.hasAttribute(attribute)) {formats[attribute] = domNode.getAttribute(attribute)}return formats}, {})}static sanitize (url) {return Link.sanitize(url) // eslint-disable-line import/no-named-as-default-member}static value (domNode) {return domNode.getAttribute('src')}format (name, value) {if (ATTRIBUTES.indexOf(name) > -1) {if (value) {this.domNode.setAttribute(name, value)} else {this.domNode.removeAttribute(name)}} else {super.format(name, value)}}html () {const { video } = this.value()return `<a href="${video}">${video}</a>`}
}
Video.blotName = 'video' // 这里不用改,楼主不用iframe,直接替换掉原来,如果需要也可以保留原来的,这里用个新的blot
Video.className = 'ql-video'
Video.tagName = 'video' // 用video标签替换iframeexport default Video
然后再把它引入到有编辑器的那个文件里:import Video from '../../plugins/video.js'; // 插入h5 video视频
Quill.register(Video, true);  // 注册video

就是这么多了~~~

这篇关于vue-quill-editor(富文本) 在 vue 中如何使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/657828

相关文章

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Springboot 中使用Sentinel的详细步骤

《Springboot中使用Sentinel的详细步骤》文章介绍了如何在SpringBoot中使用Sentinel进行限流和熔断降级,首先添加依赖,配置Sentinel控制台地址,定义受保护的资源,... 目录步骤 1: 添加 Sentinel 依赖步骤 2: 配置 Sentinel步骤 3: 定义受保护的

Python中Markdown库的使用示例详解

《Python中Markdown库的使用示例详解》Markdown库是一个用于处理Markdown文本的Python工具,这篇文章主要为大家详细介绍了Markdown库的具体使用,感兴趣的... 目录一、背景二、什么是 Markdown 库三、如何安装这个库四、库函数使用方法1. markdown.mark

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

如何使用Spring boot的@Transactional进行事务管理

《如何使用Springboot的@Transactional进行事务管理》这篇文章介绍了SpringBoot中使用@Transactional注解进行声明式事务管理的详细信息,包括基本用法、核心配置... 目录一、前置条件二、基本用法1. 在方法上添加注解2. 在类上添加注解三、核心配置参数1. 传播行为(