quill富文本编辑器——修改默认图片、视频上传功能

本文介绍了如何修改Quill富文本编辑器的默认图片和视频上传功能。针对图片上传,由于默认的base64格式可能导致字段过长,文章提出了上传解决方案。对于视频上传,原生的iframe插入不符合需求,文章讲解了如何注册自定义的video.js模块,并给出了上传成功后的回调处理方式。
摘要由CSDN通过智能技术生成

quill富文本编辑器默认的图片上传是将图片地址转换为base64格式,可能会导致字段过长;默认的视频上传是插入iframe标签,与需要的video标签不符合
在这里插入图片描述

图片上传

在这里插入图片描述

初始化编辑器时重写image上传事件

const editor = this.$refs.editor;
this.Quill = new Quill(editor, this.options);

// 如果设置了上传地址则自定义图片上传事件
if (this.type === "url") {
   
	let toolbar = this.Quill.getModule("toolbar");
	toolbar.addHandler("image", (value) => {
   
		this.uploadType = "image";
		if (value) {
   
			// 使用的是elm上传组件的点击事件
			this.$refs.upload.$children[0].$refs.input.click();
		} else {
   
			this.quill.format("image", false);
		}
	});
}

this.Quill.pasteHTML(this.currentValue);

视频上传

先引入注册自定义video.js模块

import Quill from "quill";
import video from "./video.js";	// 自定义的video.js
// Quill.register(video);
Quill.register({
    'formats/video': video }, true); 
// 工具栏配置
 toolbar: {
   
  container: [
    ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
    ["blockquote", "code-block"], // 引用  代码块
    [{
    list: "ordered" }, {
    list: "bullet" }], // 有序、无序列表
    [{
    indent: "-1" }, {
    indent: "+1" }], // 缩进
    [{
    size: ["small", false, "large", "huge"] }], // 字体大小
    [{
    header: [1, 2, 3, 4, 5, 6, false] }], // 标题
    [{
    color: [] }, {
    background: [] }], // 字体颜色、字体背景颜色
    [{
    align: [] }], // 对齐方式
    ["clean"], // 清除文本格式
    ["link", "image", "video"], // 链接、图片、视频
  ],
  handlers: {
   
    video: this.handelVideo,
  },
},
...
handelVideo() {
   
	//自定义上传视频事件
	this.uploadType = "video";
	this.$nextTick(() => {
   
		this.$refs.uploadVideo.$children[0].$refs.input.click();
	});
},

...

上传成功的回调

handleUploadSuccess(res, file) {
   
	// 获取富文本组件实例
	let quill = this.Quill;
	// 如果上传成功
	if (res.code == 200) {
   
		// 获取光标所在位置
		let length = quill.getSelection().index;
		if (this.uploadType === "image") {
   
			// 插入图片  res.url为服务器返回的图片地址
			quill.insertEmbed(length, "image", this.fullImgUrl(res.fileName));
		} else if (this.uploadType === "video") {
   
			// 插入视频  res.url为服务器返回的视频地址
			const url = this.fullImgUrl(res.fileName)
			quill.insertEmbed(length, "video", {
   
				url: url,	// 注意使用url,不要使用src
				width: '100%',
				height: 'auto'
			});
		}
		// 调整光标到最后
		quill.setSelection(length + 1);
	} else {
   
		this.$message.error("插入失败");
	}
},

video.js


                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
vue2quill是一个基于Vue.js和Quill.js实现的富文本编辑器组件。它支持上传图片视频,但需要你自己实现上传的逻辑。 对于上传视频的实现,你可以使用一些常用的视频上传方案,比如通过后端接口实现文件上传、使用第三方云存储服务等。在vue2quill中,你需要做的就是通过配置参数来指定上传视频的接口地址或者上传视频的回调函数。 以下是一个使用vue2quill上传视频的示例代码: ``` <template> <div> <quill-editor v-model="content" :options="editorOption" @image-added="onImageAdded" @video-added="onVideoAdded"></quill-editor> </div> </template> <script> import QuillEditor from 'vue-quill-editor' export default { components: { QuillEditor }, data () { return { content: '', editorOption: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // toggled buttons ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], // custom button values [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], // superscript/subscript [{ 'indent': '-1' }, { 'indent': '+1' }], // outdent/indent [{ 'direction': 'rtl' }], // text direction [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme [{ 'font': [] }], [{ 'align': [] }], ['clean'], // remove formatting button ['link', 'image', 'video'] // link and image, video ] }, placeholder: '请输入正文', theme: 'snow' } } }, methods: { onImageAdded (file, callback) { // 处理图片上传 let formData = new FormData() formData.append('file', file) axios.post('/upload/image', formData) .then(res => { callback(res.data.url) }) }, onVideoAdded (file, callback) { // 处理视频上传 let formData = new FormData() formData.append('file', file) axios.post('/upload/video', formData) .then(res => { callback(res.data.url) }) } } } </script> ``` 在上述代码中,我们通过配置QuillEditor组件的options参数来设置富文本编辑器的选项,包括工具栏、占位符等。在onImageAdded和onVideoAdded回调函数中,我们可以处理上传图片上传视频的逻辑,并通过callback函数将上传后的图片视频的地址返回给富文本编辑器组件。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凡小多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值