vue+elementUI 使用富文本編輯器

安裝富文本編輯器

npm install vue-quill-editor

制作組件方便后續(xù)引入

  • 位置 compoents\Editor\index.vue
注意:
代碼中,serverUrl:為上傳圖片的接口,

您需要處理自己的格式
在代碼中的uploadSuccess中配置
我自己的返回格式
{
    code: 200
    data: {url: "http://image.funkits.cn/funkits/1560243185.png"}
    url: "http://image.funkits.cn/funkits/1560243185.png"
    msg: "success"
}
引入使用方式: <QuillEditor v-model="content"/>
WX20190611-170319@2x.png
<template>
    <div>
        <!-- 圖片上傳組件輔助-->
      <el-upload
        class="avatar-uploader"
        :action="serverUrl"
        name="pic"
        :headers="header"
        :show-file-list="false"
        :on-success="uploadSuccess"
        :on-error="uploadError"
        :before-upload="beforeUpload">
      </el-upload>

        <quill-editor 
        class="editor"
        v-model="content"
        ref="myQuillEditor" 
        :options="editorOption" 
        @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
        @change="onEditorChange($event)">
        </quill-editor>
    </div>
</template>
<script>
// 工具欄配置
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // 加粗 斜體 下劃線 刪除線
  ["blockquote", "code-block"], // 引用  代碼塊
  [{ header: 1 }, { header: 2 }], // 1、2 級(jí)標(biāo)題
  [{ list: "ordered" }, { list: "bullet" }], // 有序、無(wú)序列表
  [{ script: "sub" }, { script: "super" }], // 上標(biāo)/下標(biāo)
  [{ indent: "-1" }, { indent: "+1" }], // 縮進(jìn)
  // [{'direction': 'rtl'}],                         // 文本方向
  [{ size: ["small", false, "large", "huge"] }], // 字體大小
  [{ header: [1, 2, 3, 4, 5, 6, false] }], // 標(biāo)題
  [{ color: [] }, { background: [] }], // 字體顏色、字體背景顏色
  [{ font: [] }], // 字體種類
  [{ align: [] }], // 對(duì)齊方式
  ["clean"], // 清除文本格式
  ["link", "image", "video"] // 鏈接、圖片、視頻
];

import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

export default {
  props: {
    /*編輯器的內(nèi)容*/
    value: {
      type: String
    },
    /*圖片大小*/
    maxSize: {
      type: Number,
      default: 4000 //kb
    }
  },

  components: {
    quillEditor
  },

  data() {
    return {
      content: this.value,
      quillUpdateImg: false, // 根據(jù)圖片上傳狀態(tài)來(lái)確定是否顯示loading動(dòng)畫,剛開始是false,不顯示
      editorOption: {
        placeholder: "",
        theme: "snow", // or 'bubble'
        placeholder: "您想說(shuō)點(diǎn)什么?",
        modules: {
          toolbar: {
            container: toolbarOptions,
            // container: "#toolbar",
            handlers: {
              image: function(value) {
                if (value) {
                  // 觸發(fā)input框選擇圖片文件
                  document.querySelector(".avatar-uploader input").click();
                } else {
                  this.quill.format("image", false);
                }
              },
              // link: function(value) {
              //   if (value) {
              //     var href = prompt('請(qǐng)輸入url');
              //     this.quill.format("link", href);
              //   } else {
              //     this.quill.format("link", false);
              //   }
              // },
            }
          }
        }
      },
      serverUrl: process.env.BASE_API+'upload', // 這里寫你要上傳的圖片服務(wù)器地址
      header: {
        // token: sessionStorage.token
      } // 有的圖片服務(wù)器要求請(qǐng)求頭需要有token
    };
  },

  methods: {
    onEditorBlur() {
      //失去焦點(diǎn)事件
    },
    onEditorFocus() {
      //獲得焦點(diǎn)事件
    },
    onEditorChange() {
      //內(nèi)容改變事件
      this.$emit("input", this.content);
    },

    // 富文本圖片上傳前
    beforeUpload() {
      // 顯示loading動(dòng)畫
      this.quillUpdateImg = true;
    },

    uploadSuccess(res, file) {
      debugger
      // res為圖片服務(wù)器返回的數(shù)據(jù)
      // 獲取富文本組件實(shí)例
      let quill = this.$refs.myQuillEditor.quill;
      // 如果上傳成功
      if (res.code == 200) {
        // 獲取光標(biāo)所在位置
        let length = quill.getSelection().index;
        // 插入圖片  res.url為服務(wù)器返回的圖片地址
        quill.insertEmbed(length, "image", res.data.url);
        // 調(diào)整光標(biāo)到最后
        quill.setSelection(length + 1);
      } else {
        this.$message.error("圖片插入失敗");
      }
      // loading動(dòng)畫消失
      this.quillUpdateImg = false;
    },
    // 富文本圖片上傳失敗
    uploadError() {
      // loading動(dòng)畫消失
      this.quillUpdateImg = false;
      this.$message.error("圖片插入失敗");
    }
  }
};
</script> 

<style>
.editor {
  line-height: normal !important;
  height: 400px;
}
.ql-snow .ql-tooltip[data-mode=link]::before {
  content: "請(qǐng)輸入鏈接地址:";
}
.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: "請(qǐng)輸入視頻地址:";
}

.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: '標(biāo)題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: '標(biāo)題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: '標(biāo)題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: '標(biāo)題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: '標(biāo)題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: '標(biāo)題6';
}

.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
  content: '標(biāo)準(zhǔn)字體';
}
.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: '等寬字體';
}
</style>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容