当前位置: 首页 > ai >正文

vue-quill-editor富文本编辑器

 

vue-quill-editor

npm install vue-quill-editor --save

 新建vue-quill-editor.vue文件,在需要的地方引入

<!-- vue-quill-editor 富文本 -->
<template><div class="quill-editor-page"><quill-editor class="ql-editor" v-model="content" ref="myQuillEditor" :options="editorOption"@blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)"></quill-editor><div class="upload-box" style="display: none"><el-upload ref="uploadRef" class="upload-append" action="" :auto-upload="true" :limit="1":show-file-list="false" :on-exceed="handleExceed":http-request="uploadFile"accept=""><el-button type="primary" class="url-css">上传</el-button></el-upload></div></div>
</template><script>
import $api from '@/api/index'
import { quillEditor } from 'vue-quill-editor'
import Quill from 'quill'import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'const Font = Quill.import('formats/font')
Font.whitelist = ['SimSun', 'SimHei', 'Microsoft-YaHei', 'KaiTi', 'FangSong'
]
Quill.register(Font, true)export default {components: {quillEditor},data() {var _this = this;const Size = Quill.import('attributors/style/size')Size.whitelist = ['12px', '14px', '16px', '18px', '20px', '24px', '30px']Quill.register(Size, true)const toolbarOptions = [['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线// ['blockquote', 'code-block'], // 引用  代码块// [{ header: 1 }, { header: 2 }], // 1、2 级标题[{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表// [{ script: 'sub' }, { script: 'super' }], // 上标/下标[{ indent: '-1' }, { indent: '+1' }], // 缩进// [{ direction: 'rtl' }], // 文本方向['image'], // 链接、图片、视频[{ size: ['12px', false, '16px', '18px', '20px', '30px'] }], // 字体大小// [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题[{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色[{ align: [] }], // 对齐方式// ['clean'], // 清除文本格式[{ font: ['Microsoft-YaHei','SimHei','SimSun', 'KaiTi', 'FangSong'] }], // 字体种类]return {content: '', //双向数据绑定数据// 富文本编辑器配置editorOption: {modules: {toolbar: {container: toolbarOptions, // 工具栏选项handlers: {'image': function (value) {if (value) {// upload点击上传事件_this.$refs.uploadRef.$el.querySelector('input').click();// document.querySelector('.avatar-uploader input').click()} else {Quill.format('image', false)}}},},},placeholder: '请输入正文'}}},methods: {// 失去焦点事件onEditorBlur(quill) {console.log('editor blur!', quill)},// 获得焦点事件onEditorFocus(quill) {console.log('editor focus!', quill)},// 准备富文本编辑器onEditorReady(quill) {console.log('editor ready!', quill)},// 内容改变事件onEditorChange({ quill, html, text }) {console.log('editor change!', quill, html, text)this.content = html},// 限制上传文件数量handleExceed(files, fileList) {this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`)},// 新增上传方法uploadFile(file) {var _this = this;// 获取 Quill 实例const quill = this.$refs.myQuillEditor?.quill;if (!quill) {this.$message.error('编辑器未初始化,请检查组件加载状态');return;}const maxSize = 5 * 1024 * 1024; // 5MB限制if (file.file.size > maxSize) {uni.showToast({ title: `${file.file.name}超过大小限制`, icon: 'none' });this.$refs.filePicker.clear(); // 清除非法文件避免回显[4,6](@ref)return;}const formData = new FormData()formData.append('file', file.file)$api.uploadApi.uploadFile(formData).then((res) => {this.$refs.uploadRef.clearFiles()const { code, data, message, error } = res.dataif (code !== 0) return this.$message.error(message || error)this.$message.success('上传成功')// 获取光标所在位置let length = quill.getSelection().index// 插入图片  dt.url为服务器返回的图片地址quill.insertEmbed(length, 'image', data.fileUrl)// 调整光标到最后quill.setSelection(length + 1)})},}
}
</script><style lang="scss">
.quill-editor-page {height: 100%;
}
.quill-editor {padding: 0;// height: 200px;white-space: inherit;.ql-container.ql-snow,.ql-toolbar.ql-snow {border:solid 1px rgba(226, 232, 240, 1)}.ql-container.ql-snow {height: 66%;}
}
/* 字体名称映射 */
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="SimHei"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="SimHei"]::before {content: "黑体";font-family: "SimHei" !important;
}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="SimSun"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="SimSun"]::before {content: "宋体";font-family: "SimSun" !important;
}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="Microsoft-YaHei"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="Microsoft-YaHei"]::before {content: "微软雅黑";font-family: "Microsoft YaHei" !important;
}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="KaiTi"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="KaiTi"]::before {content: "楷体";font-family: "KaiTi" !important;
}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="FangSong"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="FangSong"]::before {content: "仿宋";font-family: "FangSong" !important;
}/* 字体类名定义 */
.ql-font-SimSun { font-family: "SimSun"; }
.ql-font-SimHei { font-family: "SimHei"; }
.ql-font-Microsoft-YaHei { font-family: "Microsoft YaHei"; }
.ql-font-KaiTi { font-family: "KaiTi"; }
.ql-font-FangSong { font-family: "FangSong"; }/* 字号选择器样式 */
.ql-snow .ql-picker.ql-size .ql-picker-label::before,
.ql-snow .ql-picker.ql-size .ql-picker-item::before {content: "14px"; /* 默认显示 */font-size: 14px !important;
}/* 逐个字号定义 */
@each $size in 12px, 14px, 16px, 18px, 20px, 24px, 30px {.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="#{$size}"]::before,.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="#{$size}"]::before {content: "#{$size}";font-size: #{$size} !important;}.ql-editor .ql-size-#{$size} {font-size: #{$size} !important;}
}
</style>

http://www.xdnf.cn/news/6408.html

相关文章:

  • PYTHON训练营DAY26
  • 开发技术.前端开发相关问题
  • RiDoc:高效文档扫描与图像处理工具,助力高效办公
  • 语音识别——通过PyAudio录入音频
  • Secs/Gem第六讲(基于secs4net项目的ChatGpt介绍)
  • gRPC为什么高性能
  • 图神经网络如何模拟人类“理解场景”的过程?
  • 连接指定数据库时提示not currently accepting connections
  • 从代码学习深度学习 - 实战 Kaggle 比赛:图像分类 (CIFAR-10 PyTorch版)
  • Docker构建Nginx、PHP、MySQL及WordPress部署及解释
  • 2025 后端自学UNIAPP【项目实战:旅游项目】5、个人中心页面:微信登录,同意授权,获取用户信息
  • 作业帮Java后台开发面试题及参考答案(下)
  • 双重差分模型学习笔记2(理论)
  • TCVectorDB 向量数据库简介
  • GPU异步执行漏洞攻防实战:从CUDA Stream竞争到安全编程规范
  • 一文读懂--程序的编译汇编和链接
  • MarkitDown:AI时代的文档转换利器
  • 鸽巢原理/抽屉原理
  • RK3588 Uboot 读U盘配置ENV环境变量
  • 鸿蒙OSUniApp制作自定义的下拉菜单组件(鸿蒙系统适配版)#三方框架 #Uniapp
  • 湖北理元理律师事务所:债务优化如何实现“减负不降质”?
  • ChromaDB 向量库优化技巧实战
  • 如何在夸克浏览器里-安装梦精灵AI提示词管理工具
  • Apollo学习——planning模块(2)之planning_component
  • 《山东欧曼谛:美业梦想的启航港》
  • [Linux性能优化] 线程卡顿优化。Linux加入USB(HID)热插拔线程占用CPU优化。Linux中CPU使用率过高优化
  • 【steganalysis】Enhancing practicality and efficiency of deepfake detection
  • 【Linux专栏】Linux进程间关系和守护进程
  • 【Docker】Docker安装Redis
  • Claude官方63组提示词模板全解析:从工作到生活的AI应用指南