wangeditor富文本编辑器的使用

安装wangeditor

npm i wangeditor --save

封装组件

src -> compontents -> wangeditor

index.vue

<template lang="html">  
  <div class="editor">  
    <div ref="toolbar" class="toolbar">  
    </div>  
    <div ref="editor" class="text1">  
    </div>  
  </div>  
</template>  
  
<script>
import local from "../../libs/local";
import API from "../../api/index";
const E = process.browser ? require("wangeditor") : undefined;
export default {
  name: "EditorBar",
  data() {
    return {
      // uploadPath,
      editor: null,
      info_: null
    };
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    value: {
      type: String,
      default: ""
    },
    isClear: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    destory: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isClear(val) {
      // 触发清除文本域内容
      if (val) {
        console.log("this.editor", this.editor, val);
        this.editor.txt.clear();
        this.info_ = null;
      }
    },
    // disabled(val){
    //   console.log('this.editor',this.editor,val)
    //   if(val){
    //     this.editor.disable()
    //   }
    // },
    value: function(value) {
      if (value !== this.editor.txt.html()) {
        this.editor.txt.html(this.value);
      }
    }
    //value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
  },
  mounted() {
    this.seteditor();
    this.editor.txt.html(this.value);
    console.log(this.disabled);
  },
  methods: {
    seteditor() {
      this.editor = new E(this.$refs.toolbar, this.$refs.editor);
      console.log(this.editor);

      this.editor.config.uploadImgShowBase64 = false; // base 64 存储图片
      this.editor.config.uploadImgServer = API.FILE_API + "/common/upload"; // 配置服务器端地址
      this.editor.config.uploadImgHeaders = {
        "X-Admin-Token": local.get("loginToken"),
        // "Content-Type": "multipart/form-data"
      }; // 自定义 header
      this.editor.config.uploadFileName = "file"; // 后端接受上传文件的参数名
      this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024; // 将图片大小限制为 2M
      this.editor.config.uploadImgMaxLength = 6; // 限制一次最多上传 3 张图片
      this.editor.config.uploadImgTimeout = 3 * 60 * 1000; // 设置超时时间

      this.editor.config.placeholder =
        "主要包括待出让地块的具体描述,内容自行确定,可包含待出让地块行政区域的社会、经济、交通、产业发展简介,地块周边的教育、医疗卫生、公共设施、房地产销售和土地出让等信息,待出让地块的规划设计条件、公租房(养老、幼儿园)等配建、物业自持、业态等限制条件,工业用地也可提供亩均税收、产业准入、投资强度等要求。";

      this.editor.config.uploadImgHooks = {
        before: function(xhr, editor, files) {
          // 图片上传之前触发
          // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,files 是选择的图片文件
          // 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
          // return {
          //     prevent: true,
          //     msg: '放弃上传'
          // }
          console.log("before", xhr, editor, files);
        },
        success: function(xhr, editor, result) {
          // 图片上传并返回结果,图片插入成功之后触发
          // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
          console.log("success", xhr, editor, result);
        },
        fail: function(xhr, editor, result) {
          // 图片上传并返回结果,但图片插入错误时触发
          // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
          console.log("fail", xhr, editor, result);
        },
        error: function(xhr, editor) {
          // 图片上传出错时触发
          // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
          console.log("error", xhr, editor);
        },
        timeout: function(xhr, editor) {
          // 图片上传超时时触发
          // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
          console.log("timeout", xhr, editor);
        },

        // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
        // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
        customInsert: function(insertImg, result, editor) {
          // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
          // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果

          // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
          // insertImg(result.data);
          insertImg(result.data.url);
          console.log("customInsert", insertImg, result, editor);

          // result 必须是一个 JSON 格式字符串!!!否则报错
        }
      };
      // 配置菜单
      this.editor.config.menus = [
        "head", // 标题
        "bold", // 粗体
        "fontSize", // 字号
        "fontName", // 字体
        "italic", // 斜体
        "underline", // 下划线
        "strikeThrough", // 删除线
        "foreColor", // 文字颜色
        "backColor", // 背景颜色
        "link", // 插入链接
        "list", // 列表
        "justify", // 对齐方式
        "quote", // 引用
        "emoticon", // 表情
        "image", // 插入图片
        "table", // 表格
        "video", // 插入视频
        "code", // 插入代码
        "undo", // 撤销
        "redo", // 重复
        "fullscreen" // 全屏
      ];

      this.editor.config.onchange = html => {
        this.info_ = html; // 绑定当前逐渐地值
        this.$emit("change", this.info_); // 将内容同步到父组件中
      };
      // 创建富文本编辑器
      this.editor.create();
      if (this.disabled) {
        this.editor.disable();
      }
    }
  }
};
</script>  
  
<style lang="css">
.editor {
  width: 100%;
  margin: 0 auto;
  position: relative;
  z-index: 0;
  height: 230px;
}
.toolbar {
  border: 1px solid #ccc;
}
.text1 {
  border: 1px solid #ccc;
  height: 167px;
}
</style>  

使用

test.vue

<template>
    <EditorBar
          v-model="form.content"
          v-if="destory"
      ></EditorBar>
</template>
<script>
import EditorBar from "../components/wangeditor";
export default {
    name: "text",
    components: {EditorBar},
    data() {
        return {
           form:{content:'aaa'}
           destory:false
        }
    },
    created() {
    },
    methods:{}
}
</script>

 

posted @ 2021-03-04 15:40  SophialIana  阅读(501)  评论(0编辑  收藏  举报