UNPKG

iking-utils-pro

Version:

金合前端工具库

2 lines (1 loc) 2.98 kB
import{upperFirst as e}from"lodash-es";function t(e,t){if(!e||!t)return!1;if(-1!==t.indexOf(" "))throw new Error("className should not contain space.");return e.classList?e.classList.contains(t):(" "+e.className+" ").indexOf(" "+t+" ")>-1}function n(e,t,n){e&&t&&n&&e.addEventListener(t,n,!1)}function o(e,t,n){e&&t&&n&&e.removeEventListener(t,n,!1)}var r={checkTextOverfolw:e=>{if(!(e instanceof HTMLElement))throw new Error("参数必须是HTMLElement类型");try{const t=e.clientWidth;if(!e.textContent)return!1;const n=document.createRange();n.selectNodeContents(e);return n.getBoundingClientRect().width>t}catch(e){return console.error("检查文本溢出时发生错误:",e),!1}},copyText:async e=>{try{if(navigator&&"clipboard"in navigator)await navigator.clipboard.writeText(e);else{const t=document.createElement("textarea");t.value=e,document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)}return!0}catch(e){return!1}},addClass:function(e,n){if(!e)return;let o=e.className;const r=(n||"").split(" ");for(let n=0,c=r.length;n<c;n++){const c=r[n];c&&(e.classList?e.classList.add(c):t(e,c)||(o+=" "+c))}e.classList||(e.className=o)},removeClass:function(e,n){if(!e||!n)return;const o=n.split(" ");let r=" "+e.className+" ";for(let n=0,c=o.length;n<c;n++){const c=o[n];c&&(e.classList?e.classList.remove(c):t(e,c)&&(r=r.replace(" "+c+" "," ")))}e.classList||(e.className=(r||"").replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g,""))},getViewportOffset:function(e){const t=document.documentElement,n=t.scrollLeft,o=t.scrollTop,r=t.clientLeft,c=t.clientTop,s=window.pageXOffset,i=window.pageYOffset,l=function(e){return e&&e.getBoundingClientRect?e.getBoundingClientRect():0}(e),{left:a,top:d,width:u,height:f}=l,m=a+s-((s||n)-(r||0)),h=d+i-((i||o)-(c||0)),p=window.document.documentElement.clientWidth,w=window.document.documentElement.clientHeight;return{left:m,top:h,right:p-u-m,bottom:w-f-h,rightIncludeBody:p-m,bottomIncludeBody:w-h}},hackCss:function(t,n){const o={};return["webkit","Moz","ms","OT"].forEach((r=>{o[`${r}${e(t)}`]=n})),{...o,[t]:n}},on:n,off:o,once:function(e,t,r){const c=function(...n){r&&r.apply(this,n),o(e,t,c)};n(e,t,c)},useRafThrottle:function(e){let t=!1;return function(...n){t||(t=!0,window.requestAnimationFrame((()=>{e.apply(this,n),t=!1})))}},addStyelsheet:e=>{if(!e)throw new Error("href is required");if(!Array.from(document.querySelectorAll('link[rel="stylesheet"]')).find((t=>t.href===e))){const t=document.createElement("link");t.rel="stylesheet",t.type="text/css",t.href=e,document.head.appendChild(t)}},getDomDataset:(e,t)=>{const n=e||document.body,o=n?.dataset;return t?o?.[t]:o},useTextWidth:(e,t="14px")=>{const n=document.createElement("canvas").getContext("2d");if(!n)return 0;n.font=t;return n.measureText(e).width},intoErrorView:(e={})=>{const t=document.querySelector(".el-form-item.is-error");t&&t.scrollIntoView({behavior:"smooth",block:"end",inline:"nearest",...e})}};export{r as default};