赞
踩
用户反馈,粘贴表格时,宽度不受控制,有时候会变得很宽很宽。
搜索me.addListener('afterpaste'
找到粘贴表格的事件,修改其代码:
me.addListener('afterpaste', function () { utils.each(domUtils.getElementsByTagName(me.body, "table"), function (table) { if (table.offsetWidth > me.body.offsetWidth) { debugger if(table.style==undefined||table.style==""||table.style.width==undefined||table.style.width=="0px"){ if(table.width==undefined||table.width<=0){ var defaultValue = getDefaultValue(me, table); table.style.width = me.body.offsetWidth - (needIEHack ? parseInt(domUtils.getComputedStyle(me.body, 'margin-left'), 10) * 2 : 0) - defaultValue.tableBorder * 2 - (me.options.offsetWidth || 0) + 'px' }else{ table.style.width=table.width+"px"; } } } let lastTd=table.lastChild.lastChild.lastChild; lastTd.innerText=lastTd.innerText.replace(/ /gi, "") lastTd.innerText=lastTd.innerText.replace(/\s/g, "") }) });
搜索afterpaste
找到调用afterpaste的function filter(div)
函数,修改其代码:
1.添加表格样式宽度:
if(div.firstChild.nodeName.toLowerCase()=="table"){
let width=div.firstChild.getAttribute("width");
if(width!=undefined&&width>0){
div.firstChild.setAttribute("style","width:"+width+"px;table-layout: fixed;");
}
}
2.表格不调用beforepaste
3.修改后的filter函数完整代码如下:
function filter(div) { var html; if (div.firstChild) { //去掉cut中添加的边界值 var nodes = domUtils.getElementsByTagName(div, 'span'); for (var i = 0, ni; ni = nodes[i++];) { if (ni.id == '_baidu_cut_start' || ni.id == '_baidu_cut_end') { domUtils.remove(ni); } } if (browser.webkit) { var brs = div.querySelectorAll('div br'); for (var i = 0, bi; bi = brs[i++];) { var pN = bi.parentNode; if (pN.tagName == 'DIV' && pN.childNodes.length == 1) { pN.innerHTML = '<p><br/></p>'; domUtils.remove(pN); } } var divs = div.querySelectorAll('#baidu_pastebin'); for (var i = 0, di; di = divs[i++];) { var tmpP = me.document.createElement('p'); di.parentNode.insertBefore(tmpP, di); while (di.firstChild) { tmpP.appendChild(di.firstChild); } domUtils.remove(di); } var metas = div.querySelectorAll('meta'); for (var i = 0, ci; ci = metas[i++];) { domUtils.remove(ci); } var brs = div.querySelectorAll('br'); for (i = 0; ci = brs[i++];) { if (/^apple-/i.test(ci.className)) { domUtils.remove(ci); } } } if (browser.gecko) { var dirtyNodes = div.querySelectorAll('[_moz_dirty]'); for (i = 0; ci = dirtyNodes[i++];) { ci.removeAttribute('_moz_dirty'); } } if (!browser.ie) { var spans = div.querySelectorAll('span.Apple-style-span'); for (var i = 0, ci; ci = spans[i++];) { domUtils.remove(ci, true); } } if(div.firstChild.nodeName.toLowerCase()=="table"){ let width=div.firstChild.getAttribute("width"); if(width!=undefined&&width>0){ div.firstChild.setAttribute("style","width:"+width+"px;table-layout: fixed;"); } } //ie下使用innerHTML会产生多余的\r\n字符,也会产生 这里过滤掉 html = div.innerHTML;//.replace(/>(?:(\s| )*?)</g,'><'); //过滤word粘贴过来的冗余属性 html = UE.filterWord(html); //取消了忽略空白的第二个参数,粘贴过来的有些是有空白的,会被套上相关的标签 var root = div.firstChild.nodeName.toLowerCase()!="table"?UE.htmlparser(html):UE.htmlparser(html,true); //如果给了过滤规则就先进行过滤 if (me.options.filterRules) { UE.filterNode(root, me.options.filterRules); } //执行默认的处理 me.filterInputRule(root); //针对chrome的处理 if (browser.webkit) { var br = root.lastChild(); if (br && br.type == 'element' && br.tagName == 'br') { root.removeChild(br) } utils.each(me.body.querySelectorAll('div'), function (node) { if (domUtils.isEmptyBlock(node)) { domUtils.remove(node,true) } }) } html = {'html': root.toHtml()}; if(div.firstChild.nodeName.toLowerCase()!="table"){ me.fireEvent('beforepaste', html, root); //抢了默认的粘贴,那后边的内容就不执行了,比如表格粘贴 if(!html.html){ return; } root = UE.htmlparser(html.html,true); //如果开启了纯文本模式 if (me.queryCommandState('pasteplain') === 1) { me.execCommand('insertHtml', UE.filterNode(root, me.options.filterTxtRules).toHtml(), true); } else { //文本模式 UE.filterNode(root, me.options.filterTxtRules); txtContent = root.toHtml(); //完全模式 htmlContent = html.html; address = me.selection.getRange().createAddress(true); me.execCommand('insertHtml', me.getOpt('retainOnlyLabelPasted') === true ? getPureHtml(htmlContent) : htmlContent, true); } me.fireEvent("afterpaste", html); }else{ //如果开启了纯文本模式 if (me.queryCommandState('pasteplain') === 1) { me.execCommand('insertHtml', UE.filterNode(root, me.options.filterTxtRules).toHtml(), true); } else { //文本模式 UE.filterNode(root, me.options.filterTxtRules); txtContent = root.toHtml(); //完全模式 htmlContent = html.html; address = me.selection.getRange().createAddress(true); me.execCommand('insertHtml', me.getOpt('retainOnlyLabelPasted') === true ? getPureHtml(htmlContent) : htmlContent, false); } me.fireEvent("afterpaste", html); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。