vue 富文本组件+上传oss+el上传组件

 一.应用场景
  管理端有时需要编辑富文本,方便前端自由编辑一些图文混排的详情页,如商品介绍,店铺介绍等。项目中用过一些插件,感觉Vue-Quill-Editor还不错,有更多需求的可以查看文档,这里做一个简单项目实用整理,方便快速上手。
 
 二.安装
  npm install vue-quill-editor --save
 
  三.全局挂载 
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Vue.use(VueQuillEditor);
当然最好是页面组件引入
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

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

export default {
  components: {
    quillEditor
  }
}
四.使用(搭配el上传组件,自定义上传云)
  1.新建组件quilleditor.vue文件,一般放components下
  
<template>
  <div>
    <el-upload
      class="avatar-uploader quilleditorbox"
      action
      :before-upload="beforeUpload"
      :http-request="uploadHttp"
      :show-file-list="false"
    >
    </el-upload>
    <quill-editor
      class="editor"
      ref="myTextEditor"
      v-model="qecontent"
      :options="editorOption"
      @change="onEditorChange($event)"
    >
    </quill-editor>
  </div>
</template>
<script>
/* eslint-disable */
import Api from "../../api/index";
import $ from "jquery"; //引入jquery
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"], // 清除文本格式
  ["link", "image", "video"], // 链接、图片、视频
];
export default {
  data() {
    return {
      qecontent: "",
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions, // 自定义工具栏
            handlers: {
              // 自定义图片上传,结合 element el-upload 实现
              image: function(value) {
                if (value) {
                  $(".quilleditorbox input")[0].click();
                } else {
                  this.quill.format("image", false);
                }
              },
            },
          },
        },
        placeholder: "请在这里添加内容", //提示
        readyOnly: false, //是否只读
        theme: "snow", //主题 snow/bubble
        syntax: true, //语法检测
      },
    };
  },
  props: {
    editorcon: {
      type: String,
    },
  },
  watch: {
    editorcon(val) {
      this.qecontent = val;
    },
    qecontent(val) {
      this.$emit("update:editorcon", val);
    },
  },
  mounted() {},
  methods: {
    // 值发生变化
    onEditorChange(editor) {
      this.qecontent = editor.html;
    },
    //自定义上传
    uploadHttp(file) {
      const fileObj = file.file;
      const filename = "imgPc_" + Date.now() + ".png";
      let formData = new FormData();
      const loadingupimg = this.$loading({
        lock: true,
        text: "上传中…",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.5)",
      });
      this.$http.getPcOssSign().then((res) => {
        if (res.errorCode == 0) {
          let params = res.data;
          formData.append("key", `${params.dir}${filename}`);
          formData.append("policy", params.policy);
          formData.append("OSSAccessKeyId", params.accessid);
          formData.append("success_action_status", "200");
          formData.append("Signature", params.signature);
          formData.append("file", fileObj, filename);
          this.$http
            .putImageToOss(params.host, formData)
            .then((resolve) => {
              loadingupimg.close();
              console.log(params.host + "/" + params.dir + filename);
              let imgurl = params.host + "/" + params.dir + filename;
              let quill = this.$refs.myTextEditor.quill;
              let length = quill.getSelection().index; //获取光标所在位置
              quill.insertEmbed(length, "image", imgurl); // 插入图片
              quill.setSelection(length + 1); //调整光标到最后
              this.$message.success("上传成功");
            })
            .catch((e) => {
              console.log(e);
              this.$message.error("上传失败");
              loadingupimg.close();
            });
        }
      });
    },
    //上传前调用
    beforeUpload(file) {
      const isJPEG = file.name.split(".")[1] === "jpeg";
      const isJPG = file.name.split(".")[1] === "jpg";
      const isPNG = file.name.split(".")[1] === "png";
      if (!isJPG && !isJPEG && !isPNG) {
        this.$message.error("上传图片只能是 JPEG/JPG/PNG 格式!");
      }
      return isJPEG || isJPG || isPNG;
    },
  },
};
</script>
<style scoped>
.ql-container {
  width: 100%;
  height: 25rem;
}
.quilleditorbox {
  display: none;
}
</style>

 

 
  2.引入组件
  
 <quilleditor :editorcon.sync="formedit.content"></quilleditor>

 import quilleditor from "@/components/quilleditor";

export default {
  components: {
    quillEditor
  }
}

 

 

  

posted @ 2023-03-02 15:34  说好范特西  阅读(82)  评论(0)    收藏  举报