【UEditor富文本框】React支持图片粘贴

本文介绍了一种富文本编辑器中实现图片粘贴功能的方法,包括对多种图片格式的支持及批量上传处理机制。针对不同数量的图片粘贴情况进行了详细说明,并提供了错误提示和用户体验优化。

支持图片粘贴

  /**
   * 图本粘贴处理
   * @param url String 图片链接
   */
  function pasteWordWithImg(htmlStr, imgArr) {
    let str = htmlStr;
    if (imgArr.length === 1) {
      const { upImgUrl, upWidth, upHeight } = imgArr[0];
      getImgToBase64(
        upImgUrl,
        parseInt(upWidth) + "",
        parseInt(upHeight) + "",
        (data) => {
          if (data) {
            // 只能是ClipboardItempng格式,但可以复制JPG
            const blob = base64ToBlob(data, "image/png");
            const file = new window.File([blob], "image.png", {
              type: blob.type,
            });
            requestUploadImg(file, width, pageHeight).then((res) => {
              str = replaceImgUrl(str, [
                {
                  originUrl: upImgUrl,
                  replaceUrl: res.datas.url,
                },
              ]);
              editor.execCommand("inserthtml", str);
            });
          } else {
            message.error("图片粘贴不成功。试试单独复制原图并用Ctrl+V粘贴");
            str = replaceImgUrl(str, [
              {
                originUrl: upImgUrl,
                replaceUrl: "",
              },
            ]);
            editor.execCommand("inserthtml", str);
          }
        }
      );
    } else if (imgArr.length > 1 && imgArr.length <= 10) {
      let total = 0;
      imgArr.forEach((citem, index) => {
        const { upImgUrl, upWidth, upHeight } = ci
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值