富文本编辑使用组件

有时候涉及到前端关于富文本编辑使用的地方,手写难度太大,所以还是那别人造好的轮子来进行使用,今天正好学习了一下,关于前端编辑富文本,使用到的一个编辑器叫做wangRditor,下面就开始对这个来进行使用

我们使用是将其封装成一个组件了,因为在后台很多编辑的地方,都要用到。

下面就是我们在vue3项目中封装的该组件

<template>
  <div class="editor">
    <div ref="toolbar" class="toolbar"></div>
    <div ref="editor" class="text"></div>
  </div>
</template>
<script lang="ts">
import E from 'wangeditor';
import { defineComponent, ref, onMounted, watch } from 'vue';
import { message } from 'ant-design-vue';
export default defineComponent({
  name: 'editoritem',
  props: {
    value: {
      type: String,
      default: '',
    },
    isClear: {
      type: Boolean,
      default: false,
    },
    
  },
  emits: ['update:value'],
  setup(props, ctx) {
    let wangEnduit: any;
    const info_ = ref('');
    const toolbar = ref(null);
    const editor = ref(null);
    const seteditor = () => {
      wangEnduit = new E(toolbar.value, editor.value);
      wangEnduit.config.uploadImgShowBase64 = false; // base 64 存储图片
      wangEnduit.config.uploadImgServer = '/dpm-api/file/upload'; // 配置服务器端地址
      wangEnduit.config.uploadImgHeaders = {
        Authorization: 'Bearer ' + localStorage.getItem('token'),
      }; // 自定义 header
      wangEnduit.config.uploadFileName = 'file'; // 后端接受上传文件的参数名
      wangEnduit.config.uploadImgMaxSize = 200 * 1024 * 1024; // 将图片大小限制为 2M
      wangEnduit.config.uploadImgAccept = [];
      wangEnduit.config.uploadImgMaxLength = 6; // 限制一次最多上传 3 张图片
      wangEnduit.config.uploadImgTimeout = 3 * 60 * 1000; // 设置超时时间
      // 配置alt选项
      wangEnduit.config.showLinkImgAlt = false;
      // 配置超链接
      wangEnduit.config.showLinkImgHref = false;
      // 上传视频配置-----未完成
      wangEnduit.config.customUploadVideo = function (resultFiles: any) {
        if (resultFiles[0].size > 50 * 1024 * 1024) {
          message.error('视频大小不超过50M');
          return;
        }
        // uploadFileList = resultFiles;
        // uploadFileList.forEach((item: any) => {
        //   item.chunkList = [];
        //   item.uploadProgress = 0;
        // });
        // handleUpload();
      };

      // 配置菜单
      wangEnduit.config.menus = [
        'head', // 标题
        'bold', // 粗体
        'fontSize', // 字号
        'fontName', // 字体
        'italic', // 斜体
        'underline', // 下划线
        'strikeThrough', // 删除线
        'foreColor', // 文字颜色
        'backColor', // 背景颜色
        'link', // 插入链接
        'list', // 列表
        'justify', // 对齐方式
        'quote', // 引用
        'image', // 插入图片
        'video', // 插入视频
        'undo', // 撤销
        'redo', // 重复
        'fullscreen', // 全屏
      ];

      wangEnduit.config.uploadImgHooks = {
        fail: (xhr: any, editor: any, result: any) => {
          // 插入图片失败回调
          console.log(xhr, editor, result);
        },
        success: (xhr: any, editor: any, result: any) => {
          // 图片上传成功回调
          console.log(xhr, editor, result);
        },
        timeout: (xhr: any, editor: any) => {
          // 网络超时的回调
          console.log(xhr, editor);
        },
        error: (xhr: any, editor: any) => {
          // 图片上传错误的回调
          console.log(xhr, editor);
        },
        customInsert: (insertImg: any, result: any) => {
          let url = result.data.url;
          insertImg(url);
        },
      };
      wangEnduit.config.focus = false;
      wangEnduit.config.onchange = (html: any) => {
        // console.log(html,'infoValue')
        info_.value = html; // 绑定当前逐渐地值
        ctx.emit('update:value', info_.value); // 将内容同步到父组件中
      };
      // 创建富文本编辑器
      wangEnduit.create();
    };
    onMounted(() => {
      seteditor();
      // console.log(props.value,'inContent')
      wangEnduit.txt.html(props.value);
    });
    watch(
      () => props.value,
      (val) => {
        if (val !== wangEnduit.txt.html()) {
          wangEnduit.txt.html(props.value);
        }
      },
      {
        deep: true,
      },
    );
    return {
      toolbar,
      editor,
      info_,
    };
  },
});
</script>

<style scoped lang="less">
.editor {
  width: 100%;
  margin: 20px 0;
  position: relative;
  z-index: 0;
  p {
    line-height: 1 !important;
  }
}
.toolbar {
  border: 1px solid #ccc;
}
.text {
  font-size: 14px;
  min-height: 350px;
  max-height: 350px;
  overflow: auto;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  transition: border-color 0.2s cubic-bezier(0.65, 0.05, 0.36, 1);
  ::v-deep(p) {
    font-size: 14px !important;
    line-height: 1 !important;
  }
  :deep(.w-e-text-container) {
    min-height: 348px;
  }
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值