Vue 中使用 quill-editor 富文本,并结合elemnt ui 上传图片 并调整图片大小

1.安装

安装依赖包,包含编辑器包,拖拽包,缩放包 (拖拽包因为无法和element ui 配合且上传的是Base 64 格式的这里就看个人需求)

npm i vue-quill-editor quill quill-image-drop-module quill-image-resize-module --save 

2.main.js 引用

import VueQuillEditor from 'vue-quill-editor'

import * as Quill from 'quill'; // 富文本基于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)

import 'quill/dist/quill.core.css'

import 'quill/dist/quill.snow.css'

import 'quill/dist/quill.bubble.css'

Vue.use(VueQuillEditor);

3.配置文件

webpack.base.conf.js配置

const webpack = require('webpack');  //加入

module.exports里面加

plugins: [

      new webpack.ProvidePlugin({

        'window.Quill': 'quill/dist/quill.js',

        'Quill': 'quill/dist/quill.js'

      })

    ],


rules 里面加

  {

        test: /\.js$/,

        exclude: /node_modules(?!\/quill-image-drop-module|quill-image-resize-module)/,

        loader: 'babel-loader'

      }




4.修改原版部分样式(复制到App.vue)

.editor {

    line-height: normal !important;

    height: 800px;

  }

  .ql-snow .ql-tooltip[data-mode=link]::before {

    content: "请输入链接地址:";

  }

  .ql-snow .ql-tooltip.ql-editing a.ql-action::after {

      border-right: 0px;

      content: '保存';

      padding-right: 0px;

  }


  .ql-snow .ql-tooltip[data-mode=video]::before {

      content: "请输入视频地址:";

  }


  .ql-snow .ql-picker.ql-size .ql-picker-label::before,

  .ql-snow .ql-picker.ql-size .ql-picker-item::before {

    content: '14px';

  }

  .ql-snow .ql-picker.ql-size .ql-picker-label[data-value=small]::before,

  .ql-snow .ql-picker.ql-size .ql-picker-item[data-value=small]::before {

    content: '10px';

  }

  .ql-snow .ql-picker.ql-size .ql-picker-label[data-value=large]::before,

  .ql-snow .ql-picker.ql-size .ql-picker-item[data-value=large]::before {

    content: '18px';

  }

  .ql-snow .ql-picker.ql-size .ql-picker-label[data-value=huge]::before,

  .ql-snow .ql-picker.ql-size .ql-picker-item[data-value=huge]::before {

    content: '32px';

  }


  .ql-snow .ql-picker.ql-header .ql-picker-label::before,

  .ql-snow .ql-picker.ql-header .ql-picker-item::before {

    content: '文本';

  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,

  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {

    content: '标题1';

  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,

  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {

    content: '标题2';

  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,

  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {

    content: '标题3';

  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,

  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {

    content: '标题4';

  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,

  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {

    content: '标题5';

  }

  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,

  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {

    content: '标题6';

  }


  .ql-snow .ql-picker.ql-font .ql-picker-label::before,

  .ql-snow .ql-picker.ql-font .ql-picker-item::before {

    content: '标准字体';

  }

  .ql-snow .ql-picker.ql-font .ql-picker-label[data-value=serif]::before,

  .ql-snow .ql-picker.ql-font .ql-picker-item[data-value=serif]::before {

    content: '衬线字体';

  }

  .ql-snow .ql-picker.ql-font .ql-picker-label[data-value=monospace]::before,

  .ql-snow .ql-picker.ql-font .ql-picker-item[data-value=monospace]::before {

    content: '等宽字体';

  }


5.页面中使用:

<template>

  <div class="GzGl BaW">

        <div class="f-14 b_botm GzGlTit">

      <div class="fonW">新手帮助</div>

    </div>

        <div>

             <el-upload

        class="avatar-uploader"

        :action="uploadUrl()"  

        name="img"

       :headers="myHeader"

        :show-file-list="false"

        :on-success="uploadSuccess"

        :on-error="uploadError"

        :before-upload="beforeUpload">

      </el-upload>

            <quill-editor

            style="height: 690px;"

              v-model="content"

              ref="myQuillEditor"

              :options="editorOption"

              @change="onEditorChange($event)"

            >

            </quill-editor>

        </div>

        <div class="M-T-50 disCen">

             <el-button @click="Baocun" type="primary">保存</el-button>

        </div>

       </div>

</template>

  <script>

  import { showLoading, hideLoading } from '../../../../utils/loading'   //elemnt 提示函数 

  const toolbarOptions = [    //配置功能栏

    ['bold', 'italic', 'underline', 'strike'],       

    [{'header': 1}, {'header': 2}],               

    [{'list': 'ordered'}, {'list': 'bullet'}],

    [{'indent': '-1'}, {'indent': '+1'}],          

    [{'direction': 'rtl'}],                       

    [{'size': ['small', false, 'large', 'huge']}],  

    [{'header': [1, 2, 3, 4, 5, 6, false]}],

    [{'color': []}, {'background': []}],          

    [{'font': []}],

    [{'align': []}],

    ['link', 'image'],  //图片,超链接

    ['clean']


  ]

  export default {

   data() {

      return {

        quillUpdateImg: false, // 根据图片上传状态来确定是否显示loading动画,刚开始是false,不显示

        content: '',

        editorOption: {

          placeholder: '',

          theme: 'snow',  

          modules: {

       imageResize: {   //配置图片缩放

                displayStyles: {

                  backgroundColor: 'black',

                  border: 'none',

                  color: 'white'

                },

                modules: ['Resize', 'DisplaySize', 'Toolbar']

              },

            toolbar: {

              container: toolbarOptions,

              handlers: {

                'image': function (value) {

                  if (value) {

                    // 触发input框选择图片文件

                    document.querySelector('.avatar-uploader input').click()

                  } else {

                    this.quill.format('image', false);

                  }

                }

              }

            }

          }

        }

      }

    },

      computed: {   //在此配置 Token

      myHeader(){

        return {

        "Authorization":"Bearer " + window.sessionStorage.getItem('Token')

        }

      },

             },

    created () {

        this.WendanHuix()

    },

    methods: {

        onEditorChange({editor, html, text}) {  //内容改变事件

        console.log("---内容改变事件---")

        this.content = html

        console.log(html)

      },

      uploadUrl(){  //图片上传路径 

        return window.g.Url + '/basic/api/file/uploadimage'

      },

      // 富文本图片上传前

      beforeUpload() {

        // 显示loading动画

        this.quillUpdateImg = true

      },


      uploadSuccess(res, file) {

        // res为图片服务器返回的数据

        // 获取富文本组件实例

        console.log(res);

        let quill = this.$refs.myQuillEditor.quill

        // 如果上传成功

        if (res.Msg == '成功' ) {

          // 获取光标所在位置

          let length = quill.getSelection().index;

          // 插入图片  res.url为服务器返回的图片地址

          quill.insertEmbed(length, 'image',window.g.Url + res.Content.Items[0])  //拼接完整图片路径并放入富文本

          // 调整光标到最后

          quill.setSelection(length + 1)

        } else {

          this.$message.error('图片插入失败')

        }

        // loading动画消失

        this.quillUpdateImg = false

      },

      // 富文本图片上传失败

      uploadError() {

        // loading动画消失

        this.quillUpdateImg = false

        this.$message.error('图片插入失败')

      },

       WendanHuix(){  //文档回显

              let that = this

        this.$axios.get('url',{

           params: {

              'agreementType':5

            }

        })

          .then(res => {

            if(!res.data.Code) {

              if(res.data.Content){

                   this.content = res.data.Content

                   console.log(this.content)

              }

                    // console.log(res.data.Content)

            } else {

              that.$alert(res.data.Msg, '错误提醒', {

                confirmButtonText: '确定',

                callback: action => {

                }

              });

            }

        })

      },

      Baocun(){   //保存

      console.log(this.content)

        if(!this.content){

            this.$message.error('亲!请填写协议内容'); 

              return

          }

            showLoading()

          this.$axios.post('url', {

                          "AgreementType": 5,

                          "Content": this.content                                   

          }, )

          .then(res => {

            setTimeout(function() {

              hideLoading()

            }, 200)

            if(!res.data.Code) {

               this.$message({

                message: '恭喜你,这是一条成功消息',

                type: 'success'

                });

              console.log(res.data.Content)

            } else {

              this.$alert(res.data.Msg, '错误提醒', {

                confirmButtonText: '确定',

                callback: action => {

              }

            });

          }

        })

      }

    }

    }


  </script>

<style scoped>

.GzGl{

  padding: 5px 10px;

}

.GzGlTit{

  padding-top: 7px;

  padding-bottom: 15px;

  margin-bottom: 20px;

}


</style>


5.效果图


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341

推荐阅读更多精彩内容