vue-quill-editor 修改上传图片为后端返回的http地址 并回显

本文介绍了一个基于Vue的富文本编辑器实现方案,包括Quill编辑器的配置及图片上传功能。通过定义工具栏选项和事件处理,实现了丰富的文本格式化功能,并集成了图片上传到指定服务器的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自用复习

<template>
  <div>
    <el-upload
      class="avatar-uploader"
      :action="serverUrl"
      name="img"
      :data="fileUpload"
      :show-file-list="false"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :before-upload="beforeUpload"
    >
    </el-upload>
    <quill-editor
      v-model="docContent"
      ref="myQuillEditor"
      :options="editorOption"
      @blur="handleEditorBlur($event)"
      @focus="handleEditorFocus($event)"
      @change="handleEditorChange($event)"
    >
    </quill-editor>

    <el-button @click="submit()"></el-button>
  </div>
</template>

<script>

import data from '../../../public/data.json'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

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

import quillConfig from './quill-config.js'
const titleConfig = [
  { 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: '两端对齐' }
]
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' }], // 文本方向
  [{ size: ['small', false, 'large', 'huge'] }], // 字体大小
  [{ header: [1, 2, 3, 4, 5, 6, false] }], // 几级标题
  [{ color: [] }, { background: [] }], // 字体颜色,字体背景颜色
  [{ font: [] }], // 字体
  [{ align: [] }], // 对齐方式
  ['clean'], // 清除字体样式
  ['image', 'video'] // 上传图片、上传视频
]

export default {
  data() {
    return {
      docContent: data.richText,
      fileUpload: {
        // 附件上传
        file: null
      },
      serverUrl: 'http://1111/daily_sy1111111stem/up11111111load_image/', // 上传图片的地址
      editorOption: {
        placeholder: '',
        theme: 'snow',
        modules: {
          toolbar: {
            container: toolbarOptions, // 工具栏
            handlers: {
              image: function(value) {
                if (value) {
                  document.querySelector('.avatar-uploader input').click()
                } else {
                  this.quill.format('image', false)
                }
              }
            }
          }
        }
      }
    }
  },
  components: {
    quillEditor
  },
  methods: {
    submit() {
      console.log(this.docContent)
    },
    uploadError() {
      // loading动画消失
      this.quillUpdateImg = false
      this.$message.error('图片插入失败')
    },
    uploadSuccess(res, file) {
      console.log(res)
      // 首先获取富文本编辑器的实例
      const quill = this.$refs.myQuillEditor.quill
      // 上传成功所执行
      if (res.result === true) {
        // 获取光标所在位置
        const length = quill.getSelection().index
        // 插入图片res为服务器返回的数据
        quill.insertEmbed(length, 'image', res.url)
        // 光标移动至文本末端
        quill.setSelection(length + 1)
      } else {
        this.$message.error('图片插入失败')
      }
      // loading动画消失
      this.quillUpdateImg = false
    },
    beforeUpload(file) {
      this.fileUpload.file = file
      // 显示loading动画
      this.quillUpdateImg = true
    },
    handleEditorBlur() {},
    handleEditorFocus() {},

    handleEditorChange(e) {

    }
  },
  mounted() {
    document.getElementsByClassName('ql-editor')[0].dataset.placeholder = ''
    for (const item of titleConfig) {
      const tip = document.querySelector('.quill-editor ' + item.Choice)
      if (!tip) continue
      tip.setAttribute('title', item.title)
    }
    // axios({
    //   method: 'post',
    //   url: 'http://172.roup_rich_text/',
    //   data: {
    //     data: {
    //       group_id: 1,
    //       time: '2021-05-06'
    //     }
    //   }
    // }).then(res => {
    //   console.log(res)
    // }).catch(err => {
    //   console.log(err)
    // })
    // axios.post('http:_rich_text/', {
    //   data: {
    //     group_id: 1,
    //     time: '2021-05-06'
    //   }
    // })
  }
}
</script>

<style scoped></style>

### 实现 Vue-quill-editor 内容回显功能 为了确保 `vue-quill-editor` 的内容能够正常回显保持样式一致,在 HTML 结构上需要做特定处理。当从服务器获取到富文本内容后,应该将其放置于指定的容器内,应用相应的 CSS 类来保证样式的正确渲染。 对于 `vue-quill-editor` 组件而言,可以通过设置初始值的方式完成编辑器的内容加载。具体来说,就是利用组件属性绑定机制将服务端返回的数据赋给 editor 的 value 属性[^1]。 另外一种方法是在模板中创建一个 div 容器用于承载通过 v-html 插入的 html 片段,同时为该 div 添加 quill 默认使用的类名以匹配其预期的 DOM 结构: ```html <div class="ql-container ql-snow"> <div class="ql-editor" v-html="content"></div> </div> ``` 这里假设 content 是存储着来自数据库或其他远程资源中的已保存文章正文字符串变量[^2]。 值得注意的是,如果直接使用 v-model 或者 :value 来传递整个 HTML 字符串可能会遇到一些兼容性和安全方面的问题;因此推荐先解析成 Delta 格式再传入编辑器实例作为初始化参数,这样不仅可以规避潜在风险还能更好地支持双向数据绑定特性。 最后提醒一点,由于浏览器的安全策略限制,某些情况下即使设置了正确的标签也可能无法完全重现原始排版效果。此时可能还需要额外调整全局样式表或者引入 Quill 自带的主题文件才能达到理想状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值