vue使用富文本编辑器+自由伸缩图片

news2024/9/24 17:09:39

首先要下载依赖,下方是本人使用的package.json,下载完依赖如果有启动项目失败的情况,建议将依赖版本降低或使用和下方一样的版本
在这里插入图片描述
package.json代码

{
  "name": "l",
  "version": "0.1.0",
  "private": true,
  "description": "## Project setup ```npm install ```",
  "author": "lhs",
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "main": ".eslintrc.js",
  "dependencies": {
    "axios": "^0.19.2",
    "core-js": "^3.31.1",
    "echarts": "^4.8.0",
    "element-ui": "^2.15.2",
    "html2canvas": "^1.4.0",
    "jquery": "^3.5.1",
    "jspdf": "^2.5.0",
    "postcss-px2rem": "^0.3.0",
    "px2rem-loader": "^0.1.9",
    "quill": "^1.3.7",
    "quill-image-drop-module": "^1.0.3",
    "quill-image-resize-module": "^3.0.0",
    "script-loader": "^0.7.2",
    "scss": "^0.2.4",
    "style-resources-loader": "^1.3.3",
    "vue": "^2.6.12",
    "vue-qr": "^3.2.4",
    "vue-quill-editor": "^3.0.6",
    "vue-router": "^3.0.7",
    "vuex": "^3.4.0",
    "vuex-persistedstate": "^2.7.1"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-plugin-vuex": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "sass": "^1.28.0",
    "sass-loader": "^8.0.2",
    "vue-template-compiler": "^2.6.11",
    "webpack": "^4.0.0"
  },
  "license": "ISC"
}

使用前还要进行引入和注册
main.js代码如下

import Quill from "quill";
import ImageResize from "quill-image-resize-module"; // 引用
import { ImageDrop } from "quill-image-drop-module";
Quill.register("modules/imageDrop", ImageDrop);
Quill.register("modules/imageResize", ImageResize); // 注册

vue.config.js代码

const webpack = require('webpack')
 

module.exports = {
 configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        'window.Quill': 'quill/dist/quill.js',
        Quill: 'quill/dist/quill.js'
      })
    ]
  }
 }

使用页面代码如下

 <quill-editor
                          class="editor"
                          v-model="formValidator.inputCenter"
                          ref="myQuillEditor"
                          :options="editorOption"
                          @blur="onEditorBlur($event)"
                          @focus="onEditorFocus($event)"
                          @change="onEditorChange($event)"
                          @ready="onEditorReady($event)"
                        >
                  </quill-editor>


    <el-upload
      class="upload-demo"
      ref="upload"
      v-show="false"
      action
      :file-list="fileList"
      :auto-upload="true"
      :http-request="handleUploadForm"
      :headers="headers"
      :show-file-list="false"
    >
      <span id="upload123" @submit="submitUpload">上传</span>
    </el-upload>


<script>
//这里的css放到文章结束了,可自行下载
import '@/assets/css/quill/quill.core.css'
import '@/assets/css/quill/quill.snow.css'
import '@/assets/css/quill/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor' //

export default {
  components: {
    quillEditor,
  },
  data() {
    return {
      editorOption: {//富文本表头的配置项
        placeholder: '请输入',
        theme: 'snow',
        modules: {
          toolbar: {
            container: [
              ['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' }], // 文本方向
              [
                {
                  size: [
                    '12',
                    '14',
                    '16',
                    '18',
                    '20',
                    '22',
                    '24',
                    '28',
                    '32',
                    '36',
                  ],
                },
              ], // 字体大小
              [{ header: [1, 2, 3, 4, 5, 6] }], // 标题
              [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
              // [{ font: ['songti'] }], // 字体种类
              [{ align: [] }], // 对齐方式
              ['clean'], // 清除文本格式
              ['image'], // 链接、图片,需要视频的可以加上video
            ],
            handlers: {
              //此处是图片上传时候需要使用到的
              image: function (value) {
                console.log(value)
                if (value) {
                  // 点击图片,这里的#upload123要和上传组件的id一致
                  document.querySelector('#upload123').click()
                }
              },
            },
          },
          imageDrop: true, // 图片拖拽
          imageResize: {
            // 图片放大缩小
            displayStyles: {
              backgroundColor: 'black',
              border: 'none',
              color: 'white',
            },
            modules: ['Resize', 'DisplaySize', 'Toolbar'],
          },
        },
      },
      titleConfig: [//这个是用来配置表头的鼠标悬浮文字提示的,可自行更改
        { Choice: '.ql-insertMetric', title: '跳转配置' },
        { Choice: '.ql-bold', title: '加粗' },
        { Choice: '.ql-italic', title: '斜体' },
        { Choice: '.ql-underline', title: '下划线' },
        { Choice: '.ql-header', title: '段落格式' },
        { Choice: '.ql-strike', title: '删除线' },
        { Choice: '.ql-blockquote', title: '块引用' },
        { Choice: '.ql-code', title: '插入代码' },
        { Choice: '.ql-code-block', title: '插入代码段' },
        { Choice: '.ql-font', title: '字体' },
        { Choice: '.ql-size', title: '字体大小' },
        { Choice: '.ql-list[value="ordered"]', title: '编号列表' },
        { Choice: '.ql-list[value="bullet"]', title: '项目列表' },
        { Choice: '.ql-direction', title: '文本方向' },
        { Choice: '.ql-header[value="1"]', title: 'h1' },
        { Choice: '.ql-header[value="2"]', title: 'h2' },
        { Choice: '.ql-align', title: '对齐方式' },
        { Choice: '.ql-color', title: '字体颜色' },
        { Choice: '.ql-background', title: '背景颜色' },
        { Choice: '.ql-image', title: '图像' },
        { Choice: '.ql-video', title: '视频' },
        { Choice: '.ql-link', title: '添加链接' },
        { Choice: '.ql-formula', title: '插入公式' },
        { Choice: '.ql-clean', title: '清除字体格式' },
        { Choice: '.ql-script[value="sub"]', title: '下标' },
        { Choice: '.ql-script[value="super"]', title: '上标' },
        { Choice: '.ql-indent[value="-1"]', title: '向左缩进' },
        { Choice: '.ql-indent[value="+1"]', title: '向右缩进' },
        { Choice: '.ql-header .ql-picker-label', title: '标题大小' },
        {
          Choice: '.ql-header .ql-picker-item[data-value="1"]',
          title: '标题一',
        },
        {
          Choice: '.ql-header .ql-picker-item[data-value="2"]',
          title: '标题二',
        },
        {
          Choice: '.ql-header .ql-picker-item[data-value="3"]',
          title: '标题三',
        },
        {
          Choice: '.ql-header .ql-picker-item[data-value="4"]',
          title: '标题四',
        },
        {
          Choice: '.ql-header .ql-picker-item[data-value="5"]',
          title: '标题五',
        },
        {
          Choice: '.ql-header .ql-picker-item[data-value="6"]',
          title: '标题六',
        },
        { Choice: '.ql-header .ql-picker-item:last-child', title: '标准' },
        {
          Choice: '.ql-size .ql-picker-item[data-value="small"]',
          title: '小号',
        },
        {
          Choice: '.ql-size .ql-picker-item[data-value="large"]',
          title: '大号',
        },
        {
          Choice: '.ql-size .ql-picker-item[data-value="huge"]',
          title: '超大号',
        },
        { Choice: '.ql-size .ql-picker-item:nth-child(2)', title: '标准' },
        { Choice: '.ql-align .ql-picker-item:first-child', title: '居左对齐' },
        {
          Choice: '.ql-align .ql-picker-item[data-value="center"]',
          title: '居中对齐',
        },
        {
          Choice: '.ql-align .ql-picker-item[data-value="right"]',
          title: '居右对齐',
        },
        {
          Choice: '.ql-align .ql-picker-item[data-value="justify"]',
          title: '两端对齐',
        },
      ],
    }
  },

  created() {},
  mounted() {
 
  },
  destroyed() {},
  computed: {
  },
  methods: {
    async onUploadHandler(e) {
      const imageUrl = 上传七牛云后返回来的一串在线链接

      // 获取光标所在位置
      let quill = this.$refs.myQuillEditor.quill
      let length = quill.getSelection().index
      // 插入图片
      quill.insertEmbed(length, 'image', imageUrl)
      // 调整光标到最后
      quill.setSelection(length + 1)
      // this.content += url
    },
    // 失去焦点事件
    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.formValidator.inputCenter = html
    },
    // 预览
    handlePreview(val) {
      this.imageDialog.src = this.$Api.Image.GetOriginalImgUrl + val
      this.imageDialog.visible = true
    },
    submitUpload(e) {
      this.$refs.upload.submit()
    },
    async handleUploadForm(param) {
      //文件上传
      var _this = this
      let formData = new FormData()
      formData.append('file', param.file)
      var imgtype = param.file.type.toLowerCase().split('/')
      if (
        imgtype[1] != 'png' &&
        imgtype[1] != 'jpeg' &&
        imgtype[1] != 'bmp' &&
        imgtype[1] != 'jpg'
      ) {
        return this.$notify({
          title: '警告',
          message: '图片格式不正确!',
          type: 'warning',
        })
      }
      const res = await this.$Api.Image.UploadImg(formData)
      if (res.ok) {
        this.scussFile.id = res.data[0]
        this.scussFile.size = param.file.size
        this.photoId1 = res.data[0]

        const imageUrl = this.$Api.Image.GetThumbnailImgUrl + this.photoId1

        // 获取光标所在位置
        let quill = this.$refs.myQuillEditor.quill
        let length = quill.getSelection().index
        // 插入图片
        quill.insertEmbed(length, 'image', imageUrl)
        // 调整光标到最后
        quill.setSelection(length + 1)

        this.photoId1Img = this.$Api.Image.GetThumbnailImgUrl + this.photoId1
      } else {
        this.$message('上传文件失败,' + data.message)
      }
    }
  },
}
</script>

备注:这个富文本框接受从其他地方复制进来内容,复制word文档里面的格式可能会有标识丢失的问题,图片要单独复制进来,和文字一起选中复制进入富文本框可能会导致图片上传失败

参考文档:
vue-quill-editor富文本编辑器使用步骤
Quill官方中文文档
quill.js源码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2033657.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux中线程常用接口(创建,等待,退出,取消)

pthread_create #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); Compile and link with -pthread. 编译时应注意。 #include<iostream> #in…

使用Playwright解决reCAPTCHA的分步指南

您是否在您的网络爬虫中遇到过CAPTCHA&#xff1f;许多网站使用CAPTCHA系统&#xff08;最常见的是reCAPTCHA&#xff09;来防止自动化访问。但是&#xff0c;本文将指导您使用Playwright&#xff08;一种强大的浏览器自动化工具&#xff09;和CapSolver&#xff08;一个设计用…

# 利刃出鞘_Tomcat 核心原理解析(二)

利刃出鞘_Tomcat 核心原理解析&#xff08;二&#xff09; 一、 Tomcat专题 - Tomcat架构 - HTTP工作流程 1、Http 工作原理 HTTP 协议&#xff1a;是浏览器与服务器之间的数据传送协议。作为应用层协议&#xff0c;HTTP 是基于 TCP/IP 协议来传递数据的&#xff08;HTML文件…

AI 的偏见来自数据集,而数据集的偏见来自人类 | Open AGI Forum

作者 | Annie Xu 采访、责编 | Eric Wang 出品丨GOSIM 开源创新汇 Richard Vencu&#xff0c;现任 Stability AI 机器学习运维负责人、LAION 工程负责人兼创始人&#xff0c;他的人生可谓十分精彩。 已过知天命之年的他是个中国通&#xff0c;极其热爱中国的武术、茶叶、诱人…

BugKu CTF Misc:被勒索了 disordered_zip simple MQTT 请攻击这个压缩包

前言 BugKu是一个由乌云知识库&#xff08;wooyun.org&#xff09;推出的在线漏洞靶场。乌云知识库是一个致力于收集、整理和分享互联网安全漏洞信息的社区平台。 BugKu旨在提供一个实践和学习网络安全的平台&#xff0c;供安全爱好者和渗透测试人员进行挑战和练习。它包含了…

03. 剑指offer刷题-二叉树篇(第二部分)

class Solution { public:TreeNode* Convert(TreeNode* pRootOfTree) {if(pRootOfTree nullptr) return nullptr;vector<TreeNode*> cur traversal(pRootOfTree);return cur[0];}// 这道题需要用到「分解问题」的思维&#xff0c;想把整棵链表&#xff0c;可以先把左右…

[upload]-做题笔记

项目下载地址&#xff1a;https://github.com/c0ny1/upload-labs 第一关 查看源代码&#xff0c;可以看到是前端js限制上传jpg,png,gif后缀文件 function checkFile() {var file document.getElementsByName(upload_file)[0].value;if (file null || file "") …

Unity读取Android外部文件

最近近到个小需求,需要读Android件夹中的图片.在这里做一个记录. 首先读写部分,这里以图片为例子: 一读写部分 写入部分: 需要注意的是因为只有这个地址支持外部读写,所以这里用到的地址都以 :Application.persistentDataPath为地址起始. private Texture2D __CaptureCamera…

促进服务消费高质量发展虽好,但不能缺钱

近日&#xff0c;国务院印发《关于促进服务消费高质量发展的意见》&#xff0c;提出6方面20项重点任务。 百度图片&#xff1a;2024讲党课ppt国务院关于促进服务消费高质量发展​ 一是挖掘餐饮住宿、家政服务、养老托育等基础型消费潜力&#xff1b; 二是激发文化娱乐、旅游、…

Upload 上传图标不显示

el-upload如果在使用 Element UI 的 <el-upload> 组件时上传图标不显示&#xff0c;可能是由几个不同的原因造成的。以下是一些排查和解决这个问题的步骤&#xff1a; 如果在使用 Element UI 的 <el-upload> 组件时上传图标不显示&#xff0c;可能是由几个不同的原…

antd react echarts地图组件及使用

地图组件&#xff1a; import { useRef, useEffect } from "react"; import * as echarts from "echarts"; import chinaJson from ./chinaJson;const MapIndex ({option,width "100%",height "100%", }) > {const ref useRef…

08:【stm32】中断二:EXTI(外部中断)

EXTI&#xff08;外部中断&#xff09; 1、EXTI简介2、EXTI的内部结构2.1、EXTI通道2.2、内部寄存器 3、EXTI的编写程序3.1、EXTI的编程接口3.1.1、EXTI_Init 4、编写实验 1、EXTI简介 外部中断控制器&#xff0c;能够检测外部输入信号的变化边沿并由此产生中断。通过检测上升沿…

BugKu CTF Misc:密室逃脱 铁子,来一道 想要种子吗 哥哥的秘密

前言 BugKu是一个由乌云知识库&#xff08;wooyun.org&#xff09;推出的在线漏洞靶场。乌云知识库是一个致力于收集、整理和分享互联网安全漏洞信息的社区平台。 BugKu旨在提供一个实践和学习网络安全的平台&#xff0c;供安全爱好者和渗透测试人员进行挑战和练习。它包含了…

Sql语句出现ORA-00933: SQL command not properly ended的解决方法

目录 1. 问题所示2. 原理分析3. 解决方法1. 问题所示 执行sql语句的时候出现如下问题: ORA-00933: SQL command not properly ended截图如下所示: 2. 原理分析 ORA-00933: SQL command not properly ended 是 Oracle 数据库中的错误,指示 SQL 语句存在语法问题 MySQL 和…

聚观早报 | 马斯克xAI新计划;iPhone SE 4将配A18芯片

聚观早报每日整理最值得关注的行业重点事件&#xff0c;帮助大家及时了解最新行业动态&#xff0c;每日读报&#xff0c;就读聚观365资讯简报。 整理丨Cutie 8月13日消息 马斯克xAI新计划 iPhone SE 4将配A18芯片 真我GT7 Pro参数曝光 谷歌Pixel 9 Pro Fold最新配色 苹果…

TikTok达人影响力解析:品牌出海中的信任桥梁与口碑加速器

在全球化背景下&#xff0c;品牌出海已成为企业拓展市场的必然选择。然而&#xff0c;在陌生的国际市场上&#xff0c;如何快速建立品牌信任、提升品牌知名度、并实现有效的口碑传播&#xff0c;成为了企业面临的巨大挑战。在这一过程中&#xff0c;TikTok达人发挥着不可替代的…

systemverilog绿皮书随记(八)-- 功能覆盖率

代码覆盖率&#xff1a; 路径覆盖率&#xff1a;在穿过代码和表达式的路径中有哪些已经被执行过行覆盖率: 源代码中每一行代码是否被执行至少一次翻转覆盖率&#xff1a;哪些单比特变量的值为0或1有限状态机覆盖率&#xff1a;状态机中哪些状态和状态转换已经被访问过 语句覆…

Think | 大模型迈向AGI的探索和对齐

注&#xff1a;节选自我于24年初所写的「融合RL与LLM思想探寻世界模型以迈向AGI」散文式风格文章&#xff0c;感兴趣的小伙伴儿可以访问我的主页置顶或专栏收录&#xff0c;并制作了电子书供大家参考&#xff0c;有需要的小伙伴可以关注私信我&#xff0c;因为属于技术散文风格…

NVDLA专题2:具体模块介绍——Bridge DMA

对于NVDLA&#xff0c;输入图像和处理结果存储在外部DRAM中&#xff0c;但外部DRAM带宽和延迟通常不足以让NVDLA充分利用其MAC阵列。因此&#xff0c;NVDLA给片内SRAM配置了第二个存储器接口。 为了利用片内SRAM&#xff0c;NVDLA需要在外部DRAM和SRAM之间移动数据。Bridge DM…

Ubuntu20.04 运行深蓝路径规划hw1

前言 环境&#xff1a; ubuntu 20.04 &#xff1b; ROS版本&#xff1a; noetic&#xff1b; 问题 1、出现PCL报错&#xff1a;#error PCL requires C14 or above catkin_make 编译时&#xff0c;出现如下错误 解决&#xff1a; 在grid_path_searcher文件夹下面的CMakeLis…