欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 美景 > vue3使用VueQuill插入自定义按钮

vue3使用VueQuill插入自定义按钮

2025/2/23 10:14:44 来源:https://blog.csdn.net/qq_44732146/article/details/143717868  浏览:    关键词:vue3使用VueQuill插入自定义按钮

在 Vue 3 项目中使用 VueQuill 编辑器时,我们可以自定义内容来满足特定的需求。本文将介绍如何在 VueQuill 中插入自定义内容,比如插入特定的标签或样式元素。

Quill官方中文文档

1. 项目设置和依赖安装

如果你还没有创建 Vue 3 项目,可以使用以下命令来初始化项目:

npm init vue@latest

选择 Vue 3 的相关配置,然后进入项目目录并安装依赖项。

安装 VueQuill
npm install @vueup/vue-quill

此库是 Quill 编辑器的 Vue 3 兼容版本。


2. 基础配置 VueQuill

src/components 中创建一个 QuillEditor.vue 文件,并引入 vue3-quill,将 VueQuillEditor 作为编辑器组件使用。

template 内容
<template><div class="editor"><quill-editor ref="quillEditorRef" v-model:content="content" content-type="html" :options="options" :style="styles" @text-change="textChangeFn" /></div>
</template>
options 内容:
import '@vueup/vue-quill/dist/vue-quill.snow.css';import { Quill } from '@vueup/vue-quill';
const options = ref<any>({theme: 'snow',bounds: document.body,debug: 'warn',modules: {// 工具栏配置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'], // 链接、图片、视频['newFunction'] // 新添加的按钮],handlers: {newFunction: (value: boolean) => {// 添加处理方法const quill = quillEditorRef.value.getQuill();// 插入自定义按钮quill.insertEmbed(0, 'customSpan', 'test');}}}},placeholder: props.readOnly ? '' : '请输入内容',readOnly:false
});

以上代码创建了一个基础的 VueQuillEditor 组件,我们可以在其中输入和编辑文本。


3. 自定义内容的插入

接下来,我们会在 Quill 编辑器中插入自定义内容,比如一个带特定样式的 span 标签。为此,我们需要创建一个 Quill 的自定义 Blot 元素。

新建 CustomSpanBlot.ts 文件

src/quill-blots 文件夹下新建 CustomSpanBlot.ts 文件,用于定义我们自定义的 span 标签格式:

import { Quill } from '@vueup/vue-quill';const Inline = Quill.import("blots/inline");class CustomSpanBlot extends Inline {static blotName = "customSpan";static tagName = "span";static className = "custom-span";static create(value: string) {const node = super.create();node.setAttribute("data-custom", value);node.innerHTML = value;return node;}static formats(node: HTMLElement) {return node.getAttribute("data-custom");}format(name: string, value: string) {if (name === CustomSpanBlot.blotName && value) {this.domNode.setAttribute("data-custom", value);this.domNode.innerHTML = value;} else {super.format(name, value);}}
}
export { CustomSpanBlot };

4. 插入内容到编辑器

QuillEditor.vue 中引入自定义的 CustomSpanBlot,并编写插入自定义内容的方法:

<script lang="ts">
import { CustomSpanBlot } from './CustomSpanBlot';
// 进行注册
Quill.register(CustomSpanBlot);
// 初始化
onMounted(() => {// 新增自定义功能const newFunctionButton = document.querySelector('.ql-newFunction');newFunctionButton.setAttribute('style', 'width:80px; border:1px solid #ccc; border-radius:5px');newFunctionButton.textContent = '新功能';
});
</script>

在这里插入图片描述


版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词