wangEditor 使用之 Vue2封装组件

1、安装 wangEditor

npm 安装npm i wangeditor --save

注:wangeditor 全小写

package.json 文件

"wangeditor": "^4.7.11"



2-1、封装 wangEditor 组件(wangEditor.vue)

src  /  components  /  wangEditor  /  index.vue

<!-- 组件功能:wangEditor 富文本编辑器 -->

<template lang="html">

  <div class="editor">

    <div ref="toolbar" class="toolbar">

    <div ref="editor" class="text">

import E from "wangeditor";

import fileMenu from "./fileMenu";

export default {

name:"editoritem",

  data() {

return {

editor:"",

      info_:null,

    };

  },

  model: {

prop:"value",

    event:"change",

  },

  props: {

value: {

type:String,

      default:"",

    },

    isClear: {

type:Boolean,

      default:false,

    },

    quTitle: {

type:String,

      default:"",

    },

  },

  watch: {

isClear(val) {

// 触发清除文本域内容

      if (val) {

this.editor.txt.clear();

        this.info_ =null;

      }

},

    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.quTitle);

    // this.editor.unFullScreen()

  },

  methods: {

seteditor() {

// http://192.168.2.125:8080/admin/storage/create

      this.editor =new E(this.$refs.toolbar, this.$refs.editor);

      this.editor.config.uploadImgShowBase64 =false; // base 64 存储图片

      // this.editor.config.uploadImgServer = 'http://baidu.com' // 配置服务器地址, 这个是处理图片上传问题的

      // this.editor.config.uploadImgServer = Settings.apiUrl + "/api/CoreService/File/UploadFile"; // 配置服务器端地址

      this.editor.config.uploadImgHeaders = {}; // 自定义header

      this.editor.config.uploadFileName ="file"; // 后端接受上传文件的参数名

      this.editor.config.uploadImgMaxSize =6 *1024 *1024; // 将图片大小限制为6M

      this.editor.config.uploadImgMaxLength =6; // 限制一次最多上传 6 张图片

      this.editor.config.uploadImgTimeout =3 *60 *1000; // 设置超时时间

      // 配置菜单

      this.editor.config.menus = [

"head", // 标题

        "bold", // 粗体

        "fontSize", // 字号

        "fontName", // 字体

        "italic", // 斜体

        "underline", // 下划线

        "strikeThrough", // 删除线

        "foreColor", // 文字颜色

        "backColor", // 背景颜色

        "link", // 插入链接

        "list", // 列表

        "justify", // 对齐方式

        "quote", // 引用

        "emoticon", // 表情

        "image", // 插入图片

        "emoticon", // 插入表情

        "table", // 表格

        "video", // 插入视频

        "code", // 插入代码

        "undo", // 撤销

        "redo", // 重复

        "fullscreen", // 全屏

        // 附件 , 多图上传

      ];

      // 加上以下代码, 上传视频的 tab 和图标显示出来

      this.editor.config.uploadVideoServer ="/api/upload-video";

      this.editor.config.showFullScreen =true;

      this.editor.config.showLinkImg =false; // 隐藏网络图片的tab

      this.editor.config.showLinkImgAlt =false; // 配置 alt 选项

      this.editor.config.showLinkImgHref =false; // 配置超链接

      // this.editor.fullScreen()

// this.editor.unFullScreen()

      // 上传图片

      this.editor.config.uploadImgHooks = {

fail: (xhr, editor, result) => {

// 插入图片失败回调

        },

        success: (xhr, editor, result) => {

// 图片上传成功回调

          if (result.assertion) {

console.log(result.message);

          }

},

        timeout: (xhr, editor) => {

// 网络超时的回调

        },

        error: (xhr, editor) => {

// 图片上传错误的回调

        },

        customInsert: (insertImg, result, editor) => {

// 图片上传成功,插入图片的回调

          // result: 上传图片成功时返回的数据,打印返回格式是: data:[{url:"路径的形式"},...]

// console.log(result.data[0].url)

          // insertImg()为插入图片的函数

          // 循环插入图片

          const {code, data, msg } =result;

          if (code ==0) {

insertImg("/CMMS" +data.image_url);

          }else {

message.error(msg);

          }

// let url = "http://otp.cdinfotech" + result.url;

// let url = Settings.apiUrl + ":1889/" + result.objectEntity;

// insertImg(url);

        },

      };

      // 自己实现上传图片

      this.editor.config.customUploadImg =function (files, insert) {

console.log(files, "files-----------");

        // files 是 input 中选中的文件列表

        // insert 是获取图片 url 后 , 插入到编辑器的方法

        // let formData = new FormData()

// for (let i = 0; i < files.length; i++) {

        //  formData.append('file', files[i], files[i].name)  // 多张图片放进一个formData

// }

// insert(imgUrl)

      };

      // 上传视频 server接口返回格式, 很重要!

      // 接口返回 application/json 格式, 格式要求如下:

/*

{

          // error 即错误代码, 0: 表示没有错误          // 如果有错误, error != 0, 可通过下文中的监听函数 fail 拿到该错误进行自定义处理"error": 0

          // data 是一个对象, 返回视频的线上地址"data": {

            "url": "视频1地址"

}

}

*/

      this.editor.config.onchange = (html) => {

let data =this.editor.txt.getJSon;

        console.log(data, "data---------");

        this.info_ =html; // 绑定当前逐渐地值

        this.$emit("change", this.info_); // 将内容同步到父组件中

      };

      this.editor.config.onchangeTimeout =500; // 修改为500ms

      // 用户选取操作自动触发

      this.editor.config.onSelectionChange = (newSelection) => {

console.log("onSelectionChange", newSelection);

      };

      // 创建富文本编辑器

      this.editor.create();

      fileMenu(this.editor, this.$refs.toolbar);

    },

    openFileAlert(value) {

this.showEnclosureAlert =value;

    },

    // 保存

    advancedSave() {

console.log("附件保存");

    },

  },

};

<style lang="css">

.editor {

width:100%;

  margin:0 auto;

  position:relative;

  z-index:0;

}

.toolbar {

border:1px solid #ccc;

}

.text {

border:1px solid #ccc;

  min-height:500px;

}

</style>

this.editor.customConfig.uploadImgServer = Settings.apiUrl + ‘/api/CoreService/File/UploadFile’  // 配置服务器端地址,这个是处理图片上传问题的


2-2、封装 wangEditor 组件(wangEditor.vue) 

src  /  components  /  wangEditor  /  fileMenu.js

/**

* editor: wangEditor 的实例

* editorSelector: wangEditor 挂载点的节点

* options: 一些配置

*/

import uploadFile from "./uploadFile";

import vue from "vue";

export default (editor, editorSelector, options) => {

  editor.fileMenu = {

    init: function (editor, editorSelector) {

      const div = document.createElement("div");

      div.className = "w-e-toolbar w-e-menu";

      div.style.zIndex = 10001;

      div.setAttribute("data-title", "附件");

      const rdn = new Date().getTime();

      div.onclick = function (e) {

        if (e.stopPropagation) {

          e.stopPropagation();

        } else {

          e.cancelBubble = true;

        }

        // document.getElementById(`up-${rdn}`).click()

      };

      const input = document.createElement("input");

      input.type = "file";

      input.name = "file";

      input.id = `up-${rdn}`;

      input.className = "upload-file-input";

      // <i class="el-icon-folder-add" style="margin-left:260px"></i> <span class="upload-file-span" style="margin-left:260px;">附件</span>

      div.innerHTML = `<i class="el-icon-folder-add"></i>`;

      div.appendChild(input);

      editorSelector.getElementsByClassName("w-e-toolbar")[0].appendChild(div);

      input.onchange = (e) => {

        // console.log(e, 'change')

        console.log(e.target.files[0], "files");

        // 使用 uploadFile 上传文件

        // uploadFile(e.target.files, {

        //  onOk: (data) => {

        //    console.log(data);

        //    // 可以使用 editor.txt.html(data) 进行更新

        //  },

        //  onFail: (err) => {

        //    console.log(err);

        //  },

        //  onprogress: (percent) => {

        //    console.log(percent);

        //  },

        // });

      };

    },

  };

  // 创建完之后立即实例化

  editor.fileMenu.init(editor, editorSelector);

};




2-3、封装 wangEditor 组件(wangEditor.vue) 

src  /  components  /  wangEditor  /  uploadFile.js

import { message } from "element-ui";

function uploadFile(files, options) {

  if (!files || !files.length) {

    return;

  }

  let uploadFileServer = commonApi.imgUploadApi; // 文件上传地址

  const maxSize = 100 * 1024 * 1024; // 100M

  const maxSizeM = maxSize / 1000 / 1000;

  const maxLength = 1;

  const uploadFileName = "file";

  const uploadFileParams = {};

  const uploadFileParamsWithUrl = {};

  const timeout = 5 * 60 * 1000; // 5 min

  // ---------------------- 验证文件信息 ---------------------------

  const resultFiles = [];

  const errInfo = [];

  for (let file of files) {

    const name = file.name;

    const size = file.size;

    // chrome 低版本 name ===== undefined

    if (!name || !size) return;

    if (maxSize < size) {

      // 上传附件过大

      errInfo.push("\u3010" + name + "\u3011\u5927\u4E8E" + maxSizeM + "M");

      return;

    }

    // 验证通过的加入结果列表

    resultFiles.push(file);

  }

  // 抛出验证信息

  if (errInfo.length) {

    this._alert("附件验证未通过, \n" + errInfo.join("\n"));

    return;

  }

  if (resultFiles.length > maxLength) {

    this._alert("一次最多上传" + maxLength + "个文件");

    return;

  }

  // ----------------------- 自定义上传 ----------------------

  const formdata = new FormData();

  for (let file of resultFiles) {

    const name = uploadFileName || file.name;

    formdata.append(name, file);

  }

  // ----------------------- 上传附件 -------------------------

  if (uploadFileServer && typeof uploadFileServer === "string") {

    for (key in uploadFileParams) {

      val = encodeURIComponent(uploadFileParams[val]);

      formdata.append(key, val);

    }

  }

  // 定义 xhr

  const xhr = new XMLHttpRequest();

  xhr.open("POST", uploadFileServer);

  // 设置超时

  xhr.timeout = timeout;

  xhr.ontimeout = function () {

    if (options.timeout && typeof options.timeout === "function") {

      options.timeout(xhr, editor);

    }

    message.error("上传附件超时");

  };

  // 监控 progress

  if (xhr.upload) {

    xhr.upload.onprogress = function (e) {

      let percent = void 0;

      // 进度条

      if (e.lengthComputable) {

        percent = e.loaded / e.total;

        if (options.onprogress && typeof options.onprogress === "function") {

          options.onprogress(percent);

        }

      }

    };

  }

  // 返回数据

  xhr.onreadystatechange = function () {

    let result = void 0;

    if (xhr.status < 200 || xhr.status >= 300) {

      if (options.onFail && typeof options.onprogress === "function") {

        options.onFail(xhr, editor);

      }

      return;

    }

    result = xhr.responseText;

    if (

      (typeof result === "undefined" ? "undefined" : typeof result) !== "object"

    ) {

      try {

        result = JSON.parse(result);

      } catch (ex) {

        if (options.onFail && typeof options.onFail === "function") {

          options.onFail(xhr, editor, result);

        }

        return;

      }

    }

    const data = result || [];

    if (data.code == 0) {

      options.onOk && options.onOk(data.data);

    }

  };

  // 自定义 headers

  for (let key in uploadFileHeaders) {

    xhr.setRequestHeader(key, uploadFileHeaders[key]);

  }

  // 跨域传 token

  xhr.widthCredentials = false;

  // 发送请求

  xhr.send(formdata);

}

export default uploadFile;


3、导入并引用组件

src  /  views  /  edit  /  components  /  parameter.vue

<template>

  <div>

    <editorBar

      :isClear="isClear"

      :quTitle="titleName"

      @change="editorTitleChange"

    ></editorBar>

  </div>

</template>

<script>

import editorBar from "../../../components/wangEditor/index.vue";

export default {

  components: {

    editorBar, // 注册 富文本编辑器 组件

  },

  data() {

    return {

      isClear: false,

      titleName: "", // 标题

      tempTitle: "",

    };

  },

  methods: {

    // 标题高级编辑器change事件

    editorTitleChange(val) {

      this.tempTitle = val;

    },

  },

};

</script>



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

推荐阅读更多精彩内容