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

首先要下载依赖,下方是本人使用的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源码
tinymce富文本

H5移动端富媒体编辑器是一种适用于移动设备的富媒体内容编辑工具。随着移动设备的普及和网络技术的发展,人们对于在移动端进行富媒体内容编辑的需求日益增加。H5移动端富媒体编辑器应运而生。 H5移动端富媒体编辑器可以帮助用户在手机或平板等移动设备上轻松创建和编辑富媒体内容。它提供了丰富的功能,包括文字编辑、图片插入、视频添加、音频嵌入等。用户可以通过拖拽、缩放、旋转等操作方式来调整和排版内容,使之符合自己的需求。 H5移动端富媒体编辑器具有良好的兼容性,可以适应不同的移动设备和操作系统。同时,它也支持各种主流的富媒体格式,如HTML5、CSS3等,可以输出兼容性良好的富媒体内容。用户可以选择将编辑好的内容保存为HTML文件或直接发布到网页、社交媒体等平台上。 H5移动端富媒体编辑器提供了直观友好的界面,使得用户可以方便地进行操作。不需要具备专业的编程技能,任何人都可以使用它来创作出具有吸引力的富媒体内容。它还支持多人协作编辑,可以与团队成员共同编辑和审阅内容。 总之,H5移动端富媒体编辑器为用户在移动设备上进行富媒体内容编辑提供了便利和灵活性。它使得移动端创作、分享和传播富媒体内容变得更加简单和高效。无论是个人用户还是企业机构,都可以通过使用H5移动端富媒体编辑器,轻松创建出精美的富媒体作品。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值