微信复制富文本到UEditor时宽度百分比%转成xxpx固定宽度导致移动端无法自适应解决方案

由于从微信复制富文本内容到UEditor时,微信富文本编辑器会将width转换为固定宽度,导致UEditor上传微信的富文本内容到移动端后无法实现自适应宽度,刚开始时以为问题出在UEditor编辑器内,但后来经过测试发现坑是在微信编辑器里:
将微信文本内容复制到另一个微信的富文本时,一样会将百分比宽度转换为固定宽度,所有问题是出在微信编辑器的copy事件上
如图,我将微信富文本内容复制到另一个微信的富文本上,查看html代码,发现一样会将百分比宽度转换为固定宽度,所以问题是出在微信编辑器的copy事件上。也就是说百分比转换为固定宽度是无法避免的,因为我们无法重定义微信富文本的copy事件。
但微信富文本编辑器同时提供了一个data-width属性,它的属性值和width是相等的,只是以百分比的形式保存起来了。
所以问题就是如何把标签的data-width属性值替换width的属性值

这里附上两个正则表达式实现标签的固定宽度转换为百分比宽度,只适用于同时包含data-width属性和width属性的标签。标签的data-width属性值会赋值给width属性:

// 获取富文本内容并传入函数中
function adjustMobileHtml(htmlContent){
  // 当data-width属性在width属性前面时匹配
  const beforeDataWidth = /((<(section|img|div|p|span|ul|li)[\s\S]*?data-width="([1-9]\d*\.?\d*%)"[\s\S]*?width:\s)([1-9]\d*\.?\d*px)([\s\S]*?>))/mig
  // 当data-width属性在width属性后面时匹配
  const afterDataWidth = /((<(section|img|div|p|span|ul|li)[\s\S]*?width:\s)([1-9]\d*\.?\d*px)([\s\S]*?data-width="([1-9]\d*\.?\d*%)([\s\S]*?>)))/mig
  const adjustHtmlContent = htmlContent.replace(beforeDataWidth,'$2$4$6')
  const result = adjustHtmlContent.replace(afterDataWidth,'$2$6$5')
  return result
}

第二种更好的解决方法
直接获取dom节点并更改dom节点属性值即可,相比通过正则表达式的方式而言,这种方法在更易于维护

export function adjustHtmlContent(content){
   let ctx = $(document.createElement('section')).html(content)[0]
   console.log('ctx',ctx.getElementsByTagName('section'));
  //  document.getElementsByTagName('section')
    let SECTION = ctx.querySelectorAll('section')
    for(let i=0;i<SECTION.length;i++){
      if(SECTION[i].getAttribute('data-width')){
        let data_width = SECTION[i].getAttribute('data-width')
        SECTION[i].style.width = data_width
      }
    }
    let IMG = ctx.querySelectorAll('img')
    for(let i=0;i<IMG.length;i++){
      if(IMG[i].getAttribute('data-width')){
        let inner_data_width_value = IMG[i].getAttribute('data-width')
        // 当img标签的父节点存在data-width属性时,子img标签的width设置为img的data-width属性值
        if(IMG[i].parentNode.getAttribute('data-width')){
          IMG[i].style["width"] = inner_data_width_value;
        }else {
          if(/[/s/S]*?%/.test(IMG[i].style["width"])){
            return
          }
          // 当img的父节点不存在data-width属性时,img的width设置为width/574 px
          let data_width_value = parseFloat(IMG[i].style["width"])/574;
          IMG[i].style["width"] = (data_width_value)*100 + '%';
        }
      }
    }
    return $(ctx).html();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
百度富文本编辑器UEditor是一款基于JavaScript的所见即所得富文本编辑器,可以轻松地将其集成到Java Web项目中,为用户提供更好的编辑体验。下面简单介绍一下在Java Web项目中使用百度富文本编辑器UEditor的方法。 1.下载UEditor 首先,需要从百度UEditor官网上下载UEditor的源代码。下载地址为:http://ueditor.baidu.com/website/download.html 2.将UEditor集成到Java Web项目中 下载完成后,将UEditor的源代码解压缩,并将解压后的文件夹复制到Java Web项目的WebContent目录下的任意一个子目录中(比如说WebContent/ueditor)。然后,将UEditor中的jsp目录下的所有文件复制到Java Web项目的WebContent目录下的任意一个子目录中(比如说WebContent/jsp)。 3.配置UEditor 在Java Web项目的WebContent目录下新建一个名为config.json的文件,该文件的内容如下: { "imageActionName": "uploadimage", "imageFieldName": "upfile", "imageMaxSize": 2048000, "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "imageCompressEnable": true, "imageCompressBorder": 1600, "imageInsertAlign": "none", "imageUrlPrefix": "", "imagePathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "scrawlActionName": "uploadscrawl", "scrawlFieldName": "upfile", "scrawlPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "scrawlMaxSize": 2048000, "scrawlUrlPrefix": "", "scrawlInsertAlign": "none", "snapscreenActionName": "uploadimage", "snapscreenPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "snapscreenUrlPrefix": "", "snapscreenInsertAlign": "none", "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"], "catcherActionName": "catchimage", "catcherFieldName": "source", "catcherPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "catcherUrlPrefix": "", "catcherMaxSize": 2048000, "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "videoActionName": "uploadvideo", "videoFieldName": "upfile", "videoPathFormat": "/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}", "videoUrlPrefix": "", "videoMaxSize": 102400000, "videoAllowFiles": [ ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid" ], "fileActionName": "uploadfile", "fileFieldName": "upfile", "filePathFormat": "/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}", "fileUrlPrefix": "", "fileMaxSize": 51200000, "fileAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml" ], "imageManagerActionName": "listimage", "imageManagerListPath": "/ueditor/jsp/upload/image/", "imageManagerListSize": 20, "imageManagerUrlPrefix": "", "imageManagerInsertAlign": "none", "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "fileManagerActionName": "listfile", "fileManagerListPath": "/ueditor/jsp/upload/file/", "fileManagerUrlPrefix": "", "fileManagerListSize": 20, "fileManagerAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml" ] } 该文件是UEditor的配置文件,主要配置了上传文件的相关设置。 4.在JSP页面中使用UEditor 在需要使用UEditor的JSP页面中,添加如下代码: <!-- 加载ueditor编辑器js文件 --> <script type="text/javascript" src="${pageContext.request.contextPath}/ueditor/ueditor.config.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/ueditor/ueditor.all.min.js"></script> <!-- 创建编辑器 --> <script type="text/javascript"> var editor = UE.getEditor('editor'); </script> 其中,ueditor.config.js和ueditor.all.min.js是UEditor的核心文件,需要在页面中引入。editor是页面中一个div元素的id,表示要将该div元素转化为UEditor编辑器。 至此,就完成了在Java Web项目中使用百度富文本编辑器UEditor的集成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值