wangeditor封装

<template>
  <div>
    <div style="border: 1px solid #ccc">
      <!-- 工具栏 -->
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editor"
        :defaultConfig="toolbarConfig"
      />
      <!-- 编辑器 -->
      <Editor
        :style="styles"
        :defaultConfig="editorConfig"
        v-model="html"
        @onChange="onChange"
        @onCreated="onCreated"
      />
    </div>
  </div>
</template>

<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { getToken } from "@/utils/auth";
import axios from "axios";
export default {
  name: "MyEditor",
  components: {
    Editor,
    Toolbar,
  },
  props: {
    /* 编辑器的内容 */
    value: {
      type: String,
    },
    /* 高度 */
    height: {
      type: Number,
      default: null,
    },
    /* 最小高度 */
    minHeight: {
      type: Number,
      default: null,
    },
    /* 只读 */
    readOnly: {
      type: Boolean,
      default: false,
    },
    // 上传文件大小限制(MB)
    fileSize: {
      type: Number,
      default: 9,
    },
    /* 类型(base64格式、url格式) */
    type: {
      type: String,
      default: "url",
    },
  },
  //监控传参,及时更新为新值
  watch: {
    value: {
      handler(val) {
        if (val !== this.html) {
          this.html = val === null ? "" : val;
        }
      },
      immediate: true,
    },
  },
  computed: {
    styles() {
      let style = {};
      if (this.minHeight) {
        style.minHeight = `${this.minHeight}px`;
      }
      if (this.height) {
        style.height = `${this.height}px`;
      }
      return style;
    },
  },
  data() {
    return {
      editorVisible: false,
      editor: null,
      currentValue: "",
      html: "",
      toolbarConfig: {
        // toolbarKeys: [ /* 显示哪些菜单,如何排序、分组 */ ],
        // excludeKeys: [ /* 隐藏哪些菜单 */ ],
      },
      showUpload: false, //是否显示上传对话框
      uploadUrl: process.env.VUE_APP_BASE_API + "api/manage/article/upload", // 上传的图片服务器地址

      headers: {
        Authorization: "Bearer " + getToken(),
      },
      editorConfig: {
        placeholder: "请输入内容你要上传的内容...",
        // 所有的菜单配置,都要在 MENU_CONF 属性下
        MENU_CONF: {
          // 配置上传图片
          uploadImage: {
            customUpload: this.update,
          },
          // 继续其他菜单配置...
          uploadVideo: {
            customUpload: this.update,
          },
        },
      },
    };
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否则会报错
      this.$nextTick(() => {
        setTimeout(() => {
          this.editor.toolbar.change(); // 重新计算工具栏样式
        }, 100);
      });
    },
    onChange(editor) {
      this.$emit("input", this.html); // onChange 时获取编辑器最新内容
    },
    insertTextHandler() {
      const editor = this.editor;
      if (editor == null) return;
      editor.insertText(" hello ");
    },
    printEditorHtml() {
      const editor = this.editor;
      if (editor == null) return;
      console.log(editor.getHtml());
    },
    disableHandler() {
      const editor = this.editor;
      if (editor == null) return;
      editor.disable();
    },
    // 上传前校检格式和大小
    handleBeforeUpload(file) {
      console.log("上传前校验");
      // 校检文件大小
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
      return true;
    },
    //自定义上传图片
    // update(file, insertFn) {
    //   if (this.handleBeforeUpload(file)) {
    //     let formData = new FormData();
    //     formData.append("file", file);
    //     console.log(file);
    //     axios({
    //       method: "post",
    //       url: this.uploadUrl,
    //       data: formData,
    //       headers: this.headers,
    //     }).then((res) => {
    //       console.log(res);
    //       if (res.data.code == 200) {
    //         console.log("上传成功");
    //         let url = process.env.VUE_APP_BASE_API + res.data.fileName;
    //         insertFn(url);
    //       } else {
    //         this.$modal.msgError("插入失败");
    //       }
    //     });
    //   }
    // },
    update(file, insertFn) {
      if (!file) {
        this.$modal.msgError("文件不能为空");
        return;
      }

      if (this.handleBeforeUpload(file)) {
        let formData = new FormData();
        formData.append("file", file);

        const headers = {
          "Content-Type": "multipart/form-data",
          ...this.headers,
        };

        // 添加文件类型检查
        const allowedTypes = [
          "video/mp4",
          "video/webm",
          "video/ogg",
          "image/jpeg",
          "image/png",
          "image/gif",
        ];
        if (!allowedTypes.includes(file.type)) {
          this.$modal.msgError("不支持的文件类型");
          return;
        }

        // 添加文件大小检查(例如限制为100MB)
        const maxSize = 100 * 1024 * 1024;
        if (file.size > maxSize) {
          this.$modal.msgError("文件大小超出限制");
          return;
        }

        axios({
          method: "post",
          url: this.uploadUrl,
          data: formData,
          headers: headers,
          // 添加上传进度处理
          onUploadProgress: (progressEvent) => {
            const percentCompleted = Math.round(
              (progressEvent.loaded * 100) / progressEvent.total
            );
            console.log("上传进度:", percentCompleted);
          },
        })
          .then((res) => {
            if (res.data.code == 200) {
              console.log("上传成功");
              let url =
                process.env.VUE_APP_BASE_API +
                "api/manage/annex/" +
                res.data.data.id;
              insertFn(url);
              this.$modal.msgSuccess("上传成功");
            } else {
              this.$modal.msgError(res.data.msg || "上传失败");
            }
          })
          .catch((error) => {
            console.error("上传失败:", error);
            this.$modal.msgError(error.response?.data?.msg || "上传失败");
          });
      }
    },
  },
  mounted() {
    this.$nextTick(() => {
    setTimeout(() => {
      console.log("初始化菜单 DOM:", document.querySelector(".w-e-menu-panel"));
      console.log("margin:", getComputedStyle(document.querySelector(".w-e-menu-panel")).margin);
    }, 200);
  });
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时,及时销毁 editor ,重要!!!
  },
};
</script>

<style src="@wangeditor/editor/dist/css/style.css">
.w-e-toolbar {
  z-index: 10000 !important;
}
/* .w-e-bar-item {
  .title {
    margin: 0 auto;
  }
} */

.w-e-menu-panel {
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
}
</style>

Vue2 项目中对 WangEditor 进行二次封装,主要是为了更好地集成和统一使用方式,使其适应项目的整体结构,并且方便后续的维护与扩展。以下是一个详细的实现步骤: ### 1. 创建基础组件 首先,在 `components` 目录下创建一个名为 `SimpleEditor.vue` 的组件文件。该组件将作为 WangEditor封装组件。 ```vue <template> <div> <div ref="editor" class="editor"></div> </div> </template> <script> import E from 'wangeditor' export default { name: 'SimpleEditor', data() { return { editor: null } }, mounted() { this.initEditor() }, methods: { initEditor() { const editor = new E(this.$refs.editor) editor.config.menus = [ 'bold', 'fontSize', 'fontName', 'italic', 'underline', 'strikeThrough', 'indent', 'lineHeight', 'foreColor', 'backColor', 'link', 'list', 'justify', 'quote', 'emoticon', 'image', 'table', 'code', 'undo', 'redo' ] editor.config.uploadImgServer = '/api/upload' // 图片上传接口地址 editor.create() this.editor = editor }, setHtml(content) { if (this.editor) { this.editor.txt.html(content) } }, getHtml() { if (this.editor) { return this.editor.txt.html() } return '' } }, beforeDestroy() { if (this.editor) { this.editor.destroy() this.editor = null } } } </script> <style scoped> .editor { text-align: left; } </style> ``` ### 2. 在父组件中使用封装后的组件 在需要使用富文本编辑器的页面或弹窗组件中引入并使用封装好的 `SimpleEditor` 组件。 ```vue <template> <div> <!-- 其他表单内容 --> <simple-editor ref="editor" style="margin-left: 70px" /> </div> </template> <script> import SimpleEditor from '@/components/SimpleEditor' export default { name: 'DrawerInfo', components: { SimpleEditor }, data() { return { visible: false, vo: {} } }, methods: { open(vo) { this.vo = vo if (!vo.createTime) { setTimeout(() => { this.$refs.editor.setHtml('') }, 0) } else { this.http({}).then(r => { let { content } = r this.$refs.editor.setHtml(content) }) } this.visible = true }, sendForm() { let content = this.$refs.editor.getHtml() // 提交逻辑 } } } </script> ``` ### 3. 扩展功能 如果需要添加更多功能,例如限制字数、增加插件支持等,可以在 `initEditor` 方法中进一步配置。 #### 字数统计 ```javascript editor.config.onchange = () => { this.wordCount = editor.txt.text().length } ``` #### 插入自定义按钮 ```javascript editor.config.customBtns = [ { icon: 'wangEditor-bold', title: '插入代码片段', event(editor) { const code = prompt('请输入代码:') if (code) { editor.cmd.do('insertHTML', `<pre><code>${code}</code></pre>`) } } } ] ``` 通过以上步骤,可以完成对 WangEditor 的基本封装,并在实际项目中灵活调用和扩展其功能,从而提升开发效率和用户体验[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值