@utopia-utils/dom
Advanced tools
Comparing version 0.3.24 to 0.3.25
@@ -78,6 +78,13 @@ interface PanZoomOptions { | ||
* ``` | ||
* */ | ||
*/ | ||
declare function updateCSS(css: string, key: string, option?: Options): HTMLStyleElement | undefined; | ||
/** | ||
* Check if the current environment is Alipay. (支付宝) | ||
* @returns A boolean value. | ||
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/isAlipay.ts | ||
*/ | ||
declare function isAlipay(): boolean; | ||
/** | ||
* Returns a boolean value indicating whether the current platform is Android. | ||
@@ -110,2 +117,5 @@ * @returns A boolean value. | ||
/** | ||
* Options for {@link loadCSS} function | ||
*/ | ||
interface LoadCSSOptions { | ||
@@ -124,2 +134,9 @@ /** | ||
/** | ||
* return type of {@link loadCSS} function | ||
*/ | ||
interface LoadCSSReturn { | ||
unload: () => void; | ||
linkTag: HTMLLinkElement; | ||
} | ||
/** | ||
* It loads a CSS file into the page | ||
@@ -133,8 +150,7 @@ * @param {string} path - the path to the CSS file | ||
*/ | ||
declare function loadCSS(path: string, options?: LoadCSSOptions): { | ||
/** remove the script tag */ | ||
unload: () => void; | ||
linkTag: HTMLLinkElement; | ||
}; | ||
declare function loadCSS(path: string, options?: LoadCSSOptions): LoadCSSReturn; | ||
/** | ||
* options type for {@link loadScript} function | ||
*/ | ||
interface LoadScriptOptions { | ||
@@ -160,3 +176,3 @@ /** | ||
* @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#attr-referrerpolicy | ||
*/ | ||
*/ | ||
referrerPolicy?: 'no-referrer' | 'no-referrer-when-downgrade' | 'origin' | 'origin-when-cross-origin' | 'same-origin' | 'strict-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url'; | ||
@@ -177,2 +193,9 @@ noModule?: boolean; | ||
/** | ||
* return type of {@link loadScript} function | ||
*/ | ||
interface LoadScriptReturn { | ||
unload: () => void; | ||
scriptTag: HTMLScriptElement; | ||
} | ||
/** | ||
* It loads a script tag into the DOM | ||
@@ -186,7 +209,3 @@ * @param {string} src - The URL of the script to load. | ||
*/ | ||
declare function loadScript(src: string, options?: LoadScriptOptions): { | ||
/** remove the script tag */ | ||
unload: () => void; | ||
scriptTag: HTMLScriptElement; | ||
}; | ||
declare function loadScript(src: string, options?: LoadScriptOptions): LoadScriptReturn; | ||
@@ -205,6 +224,10 @@ /** | ||
/** | ||
* Options for {@link waitForSelector} | ||
*/ | ||
interface WaitForOptions { | ||
/** Maximum time to wait in milliseconds, default is 30000 (30s) */ | ||
timeoutMillisecond?: number; | ||
/** Instructs the user agent to observe a given target (a node) | ||
/** | ||
* Instructs the user agent to observe a given target (a node) | ||
* @default document.body | ||
@@ -236,2 +259,2 @@ */ | ||
export { AppendType, ContainerType, Prepend, canUseDom, checkWebpFeature, checkWebpSupport, clearContainerCache, domContains, injectCSS, isAndroid, isIOS, isMobile, isWeixin, loadCSS, loadScript, panzoom, removeCSS, setCssVar, updateCSS, waitForSelector }; | ||
export { AppendType, ContainerType, Prepend, canUseDom, checkWebpFeature, checkWebpSupport, clearContainerCache, domContains, injectCSS, isAlipay, isAndroid, isIOS, isMobile, isWeixin, loadCSS, loadScript, panzoom, removeCSS, setCssVar, updateCSS, waitForSelector }; |
@@ -1,2 +0,2 @@ | ||
function v(e,n){let t=Math.sign(e),r=Math.min(.25,Math.abs(n*e/128));return 1-t*r}function y(e,n,t){e.style.transformOrigin="0 0 0",e.style.transform=`matrix(${n.scale}, 0, 0, ${n.scale}, ${n.x}, ${n.y})`,t==null||t(n)}function H(e,{zoomFactor:n=1,maxZoom:t=5,minZoom:r=.2,onTransform:o}={}){let d=e.parentNode;if(!d)throw new Error("target element must have a parent node!");d.style.overflow="hidden",d.style.userSelect="none";let i={x:0,y:0,scale:1},c=!1,u={x:0,y:0},m=l=>{if(!c)return;let f=l.clientX-u.x,p=l.clientY-u.y;i.x=i.x+f,i.y=i.y+p,window.requestAnimationFrame(()=>y(e,i,o)),u={x:l.clientX,y:l.clientY}},a=()=>{c=!1,document.removeEventListener("mousemove",m),document.removeEventListener("mouseup",a)},s=l=>{c=!0,u={x:l.clientX,y:l.clientY},document.addEventListener("mousemove",m),document.addEventListener("mouseup",a)};d.addEventListener("mousedown",s);function g(l){let f=d.getBoundingClientRect(),p=l.clientX-f.left,A=l.clientY-f.top;return{x:p,y:A}}let b=l=>{let f=l.deltaY;l.deltaMode>0&&(f*=100);let p=v(f,n);if(p!==1){let A=g(l);x(A.x,A.y,p),window.requestAnimationFrame(()=>y(e,i,o)),l.preventDefault()}};d.addEventListener("wheel",b);function x(l,f,p){if(isNaN(l)||isNaN(f)||isNaN(p))throw new Error("zoom requires valid numbers");let A=i.scale*p;if(A<r){if(i.scale===r)return;p=r/i.scale}if(A>t){if(i.scale===t)return;p=t/i.scale}i.x=l-p*(l-i.x),i.y=f-p*(f-i.y),i.scale*=p,window.requestAnimationFrame(()=>y(e,i,o))}}function E(){return!!(typeof window!="undefined"&&window.document&&window.document.createElement)}function k(e,n){if(!e)return!1;if(e.contains)return e.contains(n);let t=n;for(;t;){if(t===e)return!0;t=t.parentNode}return!1}var M="data-rc-order",O="data-rc-priority",R="rc-util-key",w=new Map;function T({mark:e}={}){return e?e.startsWith("data-")?e:`data-${e}`:R}function h(e){return e.attachTo?e.attachTo:document.querySelector("head")||document.body}function N(e){return e==="queue"?"prependQueue":e?"prepend":"append"}function S(e){return Array.from((w.get(e)||e).children).filter(n=>n.tagName==="STYLE")}function P(e,n={}){if(!E())return null;let{csp:t,prepend:r,priority:o=0}=n,d=N(r),i=d==="prependQueue",c=document.createElement("style");c.setAttribute(M,d),i&&o&&c.setAttribute(O,`${o}`),t!=null&&t.nonce&&(c.nonce=t==null?void 0:t.nonce),c.innerHTML=e;let u=h(n),{firstChild:m}=u;if(r){if(i){let a=S(u).filter(s=>{if(!["prepend","prependQueue"].includes(s.getAttribute(M)))return!1;let g=Number(s.getAttribute(O)||0);return o>=g});if(a.length)return u.insertBefore(c,a[a.length-1].nextSibling),c}u.insertBefore(c,m)}else u.appendChild(c);return c}function L(e,n={}){let t=h(n);return S(t).find(r=>r.getAttribute(T(n))===e)}function G(e,n={}){let t=L(e,n);t&&h(n).removeChild(t)}function Q(e,n){let t=w.get(e);if(!t||!k(document,t)){let r=P("",n);if(!r)return;let{parentNode:o}=r;if(!o)return;w.set(e,o),e.removeChild(r)}}function J(){w.clear()}function X(e,n,t={}){var i,c,u;let r=h(t);Q(r,t);let o=L(n,t);if(o)return(i=t.csp)!=null&&i.nonce&&o.nonce!==((c=t.csp)==null?void 0:c.nonce)&&(o.nonce=(u=t.csp)==null?void 0:u.nonce),o.innerHTML!==e&&(o.innerHTML=e),o;let d=P(e,t);if(d)return d.setAttribute(T(t),n),d}function V(){return typeof navigator=="undefined"?!1:/android/i.test(navigator.userAgent)}function K(){return typeof navigator=="undefined"?!1:/iPad|iPhone|iPod/.test(navigator.userAgent)}function ee(){if(typeof navigator=="undefined"||typeof window=="undefined")return!1;let e=navigator.userAgent||navigator.vendor||window.opera;return/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(e)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55\/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk\/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(e==null?void 0:e.substr(0,4))}function ne(){let e=navigator.userAgent.toLowerCase();return/MicroMessenger/i.test(e)}function oe(e,n){let{attrs:t={},media:r}=n||{},o=document.querySelector(`link[href="${e}"]`);return o||(o=document.createElement("link"),o.rel="stylesheet",o.href=e,r&&(o.media=r),Object.entries(t).forEach(([d,i])=>o==null?void 0:o.setAttribute(d,i)),document.head.appendChild(o)),{unload:()=>C(e),linkTag:o}}function C(e){let n=document.querySelector(`link[href="${e}"]`);n&&n.remove()}function se(e,n){let{async:t=!0,defer:r,type:o="text/javascript",crossOrigin:d,referrerPolicy:i,attrs:c={},noModule:u,appendPosition:m="head",onStatusChange:a}=n||{},s=document.querySelector(`script[src="${e}"]`);return s&&(a==null||a("loaded")),s||(s=document.createElement("script"),s.src=e,s.type=o,s.async=t,r&&(s.defer=r),d&&(s.crossOrigin=d),i&&(s.referrerPolicy=i),u&&(s.noModule=u),Object.entries(c).forEach(([b,x])=>s==null?void 0:s.setAttribute(b,x)),(m==="head"?document.head:document.body).appendChild(s),a==null||a("loading")),s==null||s.addEventListener("load",()=>{a==null||a("loaded")}),s==null||s.addEventListener("error",()=>{a==null||a("error")}),s==null||s.addEventListener("abort",()=>{a==null||a("error")}),{unload:()=>B(e),scriptTag:s}}function B(e){let n=document.querySelector(`script[src="${e}"]`);n&&n.remove()}import{isPlainObject as q}from"@utopia-utils/share";function de(e,n=(t=>(t=window==null?void 0:window.document)==null?void 0:t.documentElement)()){e&&q(e)&&n&&Object.keys(e).forEach(r=>{n.style.setProperty(r,e[r])})}function ue(e,n){return new Promise(t=>{if(typeof window.MutationObserver=="undefined")throw new Error("MutationObserver is not supported in this browser");let{timeoutMillisecond:r=3e4,target:o=document.body}=n||{},d=setTimeout(u,r),i=new MutationObserver(m=>{m.some(a=>Array.from(a.addedNodes).some(s=>s instanceof Element&&s.matches(e)))&&(clearTimeout(d),c())});i.observe(o,{childList:!0,subtree:!0}),c();function c(){let m=document.querySelector(e);m&&(i.disconnect(),t(m))}function u(){i.disconnect(),t(null)}})}async function D(e="lossy"){return new Promise(n=>{let t={lossy:"UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA",lossless:"UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==",alpha:"UklGRkoAAABXRUJQVlA4WAoAAAAQAAAAAAAAAAAAQUxQSAwAAAARBxAR/Q9ERP8DAABWUDggGAAAABQBAJ0BKgEAAQAAAP4AAA3AAP7mtQAAAA==",animation:"UklGRlIAAABXRUJQVlA4WAoAAAASAAAAAAAAAAAAQU5JTQYAAAD/////AABBTk1GJgAAAAAAAAAAAAAAAAAAAGQAAABWUDhMDQAAAC8AAAAQBxAREYiI/gcA"},r=new Image;r.onload=function(){let o=r.width>0&&r.height>0;n(o)},r.onerror=function(){n(!1)},r.src=`data:image/webp;base64,${t[e]}`})}async function me(){return D("lossy")}export{E as canUseDom,D as checkWebpFeature,me as checkWebpSupport,J as clearContainerCache,k as domContains,P as injectCSS,V as isAndroid,K as isIOS,ee as isMobile,ne as isWeixin,oe as loadCSS,se as loadScript,H as panzoom,G as removeCSS,de as setCssVar,X as updateCSS,ue as waitForSelector}; | ||
function x(e,n){let t=Math.sign(e),o=Math.min(.25,Math.abs(n*e/128));return 1-t*o}function y(e,n,t){e.style.transformOrigin="0 0 0",e.style.transform=`matrix(${n.scale}, 0, 0, ${n.scale}, ${n.x}, ${n.y})`,t==null||t(n)}function z(e,{zoomFactor:n=1,maxZoom:t=5,minZoom:o=.2,onTransform:r}={}){let d=e.parentNode;if(!d)throw new Error("target element must have a parent node!");d.style.overflow="hidden",d.style.userSelect="none";let i={x:0,y:0,scale:1},c=!1,u={x:0,y:0},m=l=>{if(!c)return;let f=l.clientX-u.x,p=l.clientY-u.y;i.x=i.x+f,i.y=i.y+p,window.requestAnimationFrame(()=>y(e,i,r)),u={x:l.clientX,y:l.clientY}},a=()=>{c=!1,document.removeEventListener("mousemove",m),document.removeEventListener("mouseup",a)},s=l=>{c=!0,u={x:l.clientX,y:l.clientY},document.addEventListener("mousemove",m),document.addEventListener("mouseup",a)};d.addEventListener("mousedown",s);function g(l){let f=d.getBoundingClientRect(),p=l.clientX-f.left,A=l.clientY-f.top;return{x:p,y:A}}let h=l=>{let f=l.deltaY;l.deltaMode>0&&(f*=100);let p=x(f,n);if(p!==1){let A=g(l);v(A.x,A.y,p),window.requestAnimationFrame(()=>y(e,i,r)),l.preventDefault()}};d.addEventListener("wheel",h);function v(l,f,p){if(Number.isNaN(l)||Number.isNaN(f)||Number.isNaN(p))throw new Error("zoom requires valid numbers");let A=i.scale*p;if(A<o){if(i.scale===o)return;p=o/i.scale}if(A>t){if(i.scale===t)return;p=t/i.scale}i.x=l-p*(l-i.x),i.y=f-p*(f-i.y),i.scale*=p,window.requestAnimationFrame(()=>y(e,i,r))}}function E(){return!!(typeof window!="undefined"&&window.document&&window.document.createElement)}function k(e,n){if(!e)return!1;if(e.contains)return e.contains(n);let t=n;for(;t;){if(t===e)return!0;t=t.parentNode}return!1}var S="data-rc-order",T="data-rc-priority",R="rc-util-key",b=new Map;function M({mark:e}={}){return e?e.startsWith("data-")?e:`data-${e}`:R}function w(e){return e.attachTo?e.attachTo:document.querySelector("head")||document.body}function N(e){return e==="queue"?"prependQueue":e?"prepend":"append"}function L(e){return Array.from((b.get(e)||e).children).filter(n=>n.tagName==="STYLE")}function O(e,n={}){if(!E())return null;let{csp:t,prepend:o,priority:r=0}=n,d=N(o),i=d==="prependQueue",c=document.createElement("style");c.setAttribute(S,d),i&&r&&c.setAttribute(T,`${r}`),t!=null&&t.nonce&&(c.nonce=t==null?void 0:t.nonce),c.innerHTML=e;let u=w(n),{firstChild:m}=u;if(o){if(i){let a=L(u).filter(s=>{if(!["prepend","prependQueue"].includes(s.getAttribute(S)))return!1;let g=Number(s.getAttribute(T)||0);return r>=g});if(a.length)return u.insertBefore(c,a[a.length-1].nextSibling),c}u.insertBefore(c,m)}else u.appendChild(c);return c}function P(e,n={}){let t=w(n);return L(t).find(o=>o.getAttribute(M(n))===e)}function G(e,n={}){let t=P(e,n);t&&w(n).removeChild(t)}function C(e,n){let t=b.get(e);if(!t||!k(document,t)){let o=O("",n);if(!o)return;let{parentNode:r}=o;if(!r)return;b.set(e,r),e.removeChild(o)}}function J(){b.clear()}function X(e,n,t={}){var i,c,u;let o=w(t);C(o,t);let r=P(n,t);if(r)return(i=t.csp)!=null&&i.nonce&&r.nonce!==((c=t.csp)==null?void 0:c.nonce)&&(r.nonce=(u=t.csp)==null?void 0:u.nonce),r.innerHTML!==e&&(r.innerHTML=e),r;let d=O(e,t);if(d)return d.setAttribute(M(t),n),d}function V(){let e=navigator.userAgent.toLowerCase();return/Alipay/i.test(e)}function K(){return typeof navigator=="undefined"?!1:/android/i.test(navigator.userAgent)}function ee(){return typeof navigator=="undefined"?!1:/iPad|iPhone|iPod/.test(navigator.userAgent)}function ne(){if(typeof navigator=="undefined"||typeof window=="undefined")return!1;let e=navigator.userAgent||navigator.vendor||window.opera;return/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(e)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55\/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk\/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(e==null?void 0:e.substr(0,4))}function re(){let e=navigator.userAgent.toLowerCase();return/MicroMessenger/i.test(e)}function se(e,n){let{attrs:t={},media:o}=n||{},r=document.querySelector(`link[href="${e}"]`);return r||(r=document.createElement("link"),r.rel="stylesheet",r.href=e,o&&(r.media=o),Object.entries(t).forEach(([d,i])=>r==null?void 0:r.setAttribute(d,i)),document.head.appendChild(r)),{unload:()=>Q(e),linkTag:r}}function Q(e){let n=document.querySelector(`link[href="${e}"]`);n&&n.remove()}function ce(e,n){let{async:t=!0,defer:o,type:r="text/javascript",crossOrigin:d,referrerPolicy:i,attrs:c={},noModule:u,appendPosition:m="head",onStatusChange:a}=n||{},s=document.querySelector(`script[src="${e}"]`);return s&&(a==null||a("loaded")),s||(s=document.createElement("script"),s.src=e,s.type=r,s.async=t,o&&(s.defer=o),d&&(s.crossOrigin=d),i&&(s.referrerPolicy=i),u&&(s.noModule=u),Object.entries(c).forEach(([h,v])=>s==null?void 0:s.setAttribute(h,v)),(m==="head"?document.head:document.body).appendChild(s),a==null||a("loading")),s==null||s.addEventListener("load",()=>{a==null||a("loaded")}),s==null||s.addEventListener("error",()=>{a==null||a("error")}),s==null||s.addEventListener("abort",()=>{a==null||a("error")}),{unload:()=>H(e),scriptTag:s}}function H(e){let n=document.querySelector(`script[src="${e}"]`);n&&n.remove()}import{isPlainObject as B}from"@utopia-utils/share";function ue(e,n=(t=>(t=window==null?void 0:window.document)==null?void 0:t.documentElement)()){e&&B(e)&&n&&Object.keys(e).forEach(o=>{n.style.setProperty(o,e[o])})}function me(e,n){return new Promise(t=>{if(typeof window.MutationObserver=="undefined")throw new Error("MutationObserver is not supported in this browser");let{timeoutMillisecond:o=3e4,target:r=document.body}=n||{},d=setTimeout(u,o),i=new MutationObserver(m=>{m.some(a=>Array.from(a.addedNodes).some(s=>s instanceof Element&&s.matches(e)))&&(clearTimeout(d),c())});i.observe(r,{childList:!0,subtree:!0}),c();function c(){let m=document.querySelector(e);m&&(i.disconnect(),t(m))}function u(){i.disconnect(),t(null)}})}async function q(e="lossy"){return new Promise(n=>{let t={lossy:"UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA",lossless:"UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==",alpha:"UklGRkoAAABXRUJQVlA4WAoAAAAQAAAAAAAAAAAAQUxQSAwAAAARBxAR/Q9ERP8DAABWUDggGAAAABQBAJ0BKgEAAQAAAP4AAA3AAP7mtQAAAA==",animation:"UklGRlIAAABXRUJQVlA4WAoAAAASAAAAAAAAAAAAQU5JTQYAAAD/////AABBTk1GJgAAAAAAAAAAAAAAAAAAAGQAAABWUDhMDQAAAC8AAAAQBxAREYiI/gcA"},o=new Image;o.onload=function(){let r=o.width>0&&o.height>0;n(r)},o.onerror=function(){n(!1)},o.src=`data:image/webp;base64,${t[e]}`})}async function Ae(){return q("lossy")}export{E as canUseDom,q as checkWebpFeature,Ae as checkWebpSupport,J as clearContainerCache,k as domContains,O as injectCSS,V as isAlipay,K as isAndroid,ee as isIOS,ne as isMobile,re as isWeixin,se as loadCSS,ce as loadScript,z as panzoom,G as removeCSS,ue as setCssVar,X as updateCSS,me as waitForSelector}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@utopia-utils/dom", | ||
"type": "module", | ||
"version": "0.3.24", | ||
"version": "0.3.25", | ||
"description": "Collection of common dom utils", | ||
@@ -25,4 +25,4 @@ "author": "Utopia <https://github.com/GreatAuk>", | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.js" | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
} | ||
@@ -34,3 +34,3 @@ }, | ||
"dependencies": { | ||
"@utopia-utils/share": "^0.3.19" | ||
"@utopia-utils/share": "0.3.25" | ||
}, | ||
@@ -43,4 +43,5 @@ "scripts": { | ||
"test": "vitest", | ||
"typecheck": "tsc --noEmit" | ||
"typecheck": "tsc --noEmit", | ||
"clean": "rm -rf .turbo & rm -rf node_modules & rm -rf dist" | ||
} | ||
} |
@@ -6,4 +6,4 @@ /** | ||
*/ | ||
export function canUseDom() { | ||
export function canUseDom(): boolean { | ||
return !!(typeof window !== 'undefined' && window.document && window.document.createElement) | ||
} |
@@ -12,3 +12,3 @@ /** | ||
*/ | ||
export function domContains(root: Node | null | undefined, n: Node | null) { | ||
export function domContains(root: Node | null | undefined, n: Node | null): boolean { | ||
if (!root) | ||
@@ -15,0 +15,0 @@ return false |
@@ -26,3 +26,3 @@ import { canUseDom } from './canUseDom' | ||
function getMark({ mark }: Options = {}) { | ||
function getMark({ mark }: Options = {}): string { | ||
if (mark) | ||
@@ -52,3 +52,3 @@ return mark.startsWith('data-') ? mark : `data-${mark}` | ||
*/ | ||
function findStyles(container: ContainerType) { | ||
function findStyles(container: ContainerType): HTMLStyleElement[] { | ||
return Array.from( | ||
@@ -59,3 +59,3 @@ (containerCache.get(container) || container).children, | ||
export function injectCSS(css: string, option: Options = {}) { | ||
export function injectCSS(css: string, option: Options = {}): HTMLStyleElement | null { | ||
if (!canUseDom()) | ||
@@ -117,3 +117,3 @@ return null | ||
function findExistNode(key: string, option: Options = {}) { | ||
function findExistNode(key: string, option: Options = {}): HTMLStyleElement | undefined { | ||
const container = getContainer(option) | ||
@@ -126,3 +126,3 @@ | ||
export function removeCSS(key: string, option: Options = {}) { | ||
export function removeCSS(key: string, option: Options = {}): void { | ||
const existNode = findExistNode(key, option) | ||
@@ -138,3 +138,3 @@ if (existNode) { | ||
*/ | ||
function syncRealContainer(container: ContainerType, option: Options) { | ||
function syncRealContainer(container: ContainerType, option: Options): void { | ||
const cachedRealContainer = containerCache.get(container) | ||
@@ -158,3 +158,3 @@ | ||
*/ | ||
export function clearContainerCache() { | ||
export function clearContainerCache(): void { | ||
containerCache.clear() | ||
@@ -175,4 +175,4 @@ } | ||
* ``` | ||
* */ | ||
export function updateCSS(css: string, key: string, option: Options = {}) { | ||
*/ | ||
export function updateCSS(css: string, key: string, option: Options = {}): HTMLStyleElement | undefined { | ||
const container = getContainer(option) | ||
@@ -179,0 +179,0 @@ |
@@ -5,2 +5,3 @@ export * from './panzoom' | ||
export * from './dynamicCSS' | ||
export * from './isAlipay' | ||
export * from './isAndroid' | ||
@@ -7,0 +8,0 @@ export * from './isIOS' |
@@ -6,3 +6,3 @@ /** | ||
*/ | ||
export function isAndroid() { | ||
export function isAndroid(): boolean { | ||
if (typeof navigator === 'undefined') | ||
@@ -9,0 +9,0 @@ return false |
@@ -6,3 +6,3 @@ /** | ||
*/ | ||
export function isIOS() { | ||
export function isIOS(): boolean { | ||
if (typeof navigator === 'undefined') | ||
@@ -9,0 +9,0 @@ return false |
@@ -6,3 +6,3 @@ /** | ||
*/ | ||
export function isMobile() { | ||
export function isMobile(): boolean { | ||
if (typeof navigator === 'undefined' || typeof window === 'undefined') | ||
@@ -9,0 +9,0 @@ return false |
@@ -6,5 +6,5 @@ /** | ||
*/ | ||
export function isWeixin() { | ||
export function isWeixin(): boolean { | ||
const ua = navigator.userAgent.toLowerCase() | ||
return /MicroMessenger/i.test(ua) | ||
} |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Options for {@link loadCSS} function | ||
*/ | ||
interface LoadCSSOptions { | ||
@@ -15,2 +18,10 @@ /** | ||
/** | ||
* return type of {@link loadCSS} function | ||
*/ | ||
interface LoadCSSReturn { | ||
unload: () => void | ||
linkTag: HTMLLinkElement | ||
} | ||
/** | ||
* It loads a CSS file into the page | ||
@@ -24,3 +35,3 @@ * @param {string} path - the path to the CSS file | ||
*/ | ||
export function loadCSS(path: string, options?: LoadCSSOptions) { | ||
export function loadCSS(path: string, options?: LoadCSSOptions): LoadCSSReturn { | ||
const { | ||
@@ -53,3 +64,3 @@ attrs = {}, | ||
/** remove the script tag */ | ||
function unload(path: string) { | ||
function unload(path: string): void { | ||
const linkEl = document.querySelector(`link[href="${path}"]`) | ||
@@ -56,0 +67,0 @@ if (linkEl) |
@@ -0,1 +1,4 @@ | ||
/** | ||
* options type for {@link loadScript} function | ||
*/ | ||
interface LoadScriptOptions { | ||
@@ -21,3 +24,3 @@ /** | ||
* @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#attr-referrerpolicy | ||
*/ | ||
*/ | ||
referrerPolicy?: 'no-referrer' | 'no-referrer-when-downgrade' | 'origin' | 'origin-when-cross-origin' | 'same-origin' | 'strict-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url' | ||
@@ -39,2 +42,10 @@ noModule?: boolean | ||
/** | ||
* return type of {@link loadScript} function | ||
*/ | ||
interface LoadScriptReturn { | ||
unload: () => void | ||
scriptTag: HTMLScriptElement | ||
} | ||
/** | ||
* It loads a script tag into the DOM | ||
@@ -48,3 +59,3 @@ * @param {string} src - The URL of the script to load. | ||
*/ | ||
export function loadScript(src: string, options?: LoadScriptOptions) { | ||
export function loadScript(src: string, options?: LoadScriptOptions): LoadScriptReturn { | ||
const { | ||
@@ -106,3 +117,3 @@ async = true, | ||
/** remove the script tag */ | ||
function unload(src: string) { | ||
function unload(src: string): void { | ||
const script = document.querySelector(`script[src="${src}"]`) | ||
@@ -109,0 +120,0 @@ if (script) |
@@ -87,3 +87,3 @@ import type { PanZoomOptions, Transform } from './type' | ||
function zoomTo(offsetX: number, offsetY: number, ratio: number) { | ||
if (isNaN(offsetX) || isNaN(offsetY) || isNaN(ratio)) | ||
if (Number.isNaN(offsetX) || Number.isNaN(offsetY) || Number.isNaN(ratio)) | ||
throw new Error('zoom requires valid numbers') | ||
@@ -90,0 +90,0 @@ |
import type { Transform } from './type' | ||
export function getScaleMultiplier(delta: number, zoomFactor: number) { | ||
export function getScaleMultiplier(delta: number, zoomFactor: number): number { | ||
const sign = Math.sign(delta) | ||
@@ -9,3 +9,3 @@ const deltaAdjustedSpeed = Math.min(0.25, Math.abs(zoomFactor * delta / 128)) | ||
export function applyTransform(dom: HTMLElement, transform: Transform, cb?: (val: Transform) => void) { | ||
export function applyTransform(dom: HTMLElement, transform: Transform, cb?: (val: Transform) => void): void { | ||
dom.style.transformOrigin = '0 0 0' | ||
@@ -12,0 +12,0 @@ dom.style.transform = `matrix(${transform.scale}, 0, 0, ${transform.scale}, ${transform.x}, ${transform.y})` |
@@ -23,3 +23,3 @@ import { afterEach, describe, expect, it } from 'vitest' | ||
it('should set CSS variables on the root element of a document using the provided variables object', () => { | ||
it('should set CSS variables on the root element of a document using the provided variables object2', () => { | ||
const variables = { | ||
@@ -47,3 +47,3 @@ '--color': 'red', | ||
it('should not set CSS variables on the root element of a document if the provided variables object is not a plain object', () => { | ||
it('should not set CSS variables on the root element of a document if the provided variables object is not a plain object2', () => { | ||
const variables = null | ||
@@ -50,0 +50,0 @@ |
@@ -13,3 +13,3 @@ import { isPlainObject } from '@utopia-utils/share' | ||
*/ | ||
export function setCssVar(variables: Record<string, string | null>, root = window?.document?.documentElement) { | ||
export function setCssVar(variables: Record<string, string | null>, root = window?.document?.documentElement): void { | ||
if (variables && isPlainObject(variables) && root) { | ||
@@ -16,0 +16,0 @@ Object.keys(variables).forEach((key) => { |
@@ -5,6 +5,7 @@ /** | ||
*/ | ||
export const classNameToArray = (cls = '') => | ||
cls.split(' ').filter(v => !!v.trim()) | ||
export function classNameToArray(cls = '') { | ||
return cls.split(' ').filter(v => !!v.trim()) | ||
} | ||
export const hasClass = (el: Element, cls: string): boolean => { | ||
export function hasClass(el: Element, cls: string): boolean { | ||
if (!el || !cls) | ||
@@ -17,3 +18,3 @@ return false | ||
export const addClass = (el: Element, cls: string) => { | ||
export function addClass(el: Element, cls: string): void { | ||
if (!el || !cls.trim()) | ||
@@ -24,3 +25,3 @@ return | ||
export const removeClass = (el: Element, cls: string) => { | ||
export function removeClass(el: Element, cls: string): void { | ||
if (!el || !cls.trim()) | ||
@@ -27,0 +28,0 @@ return |
@@ -0,5 +1,9 @@ | ||
/** | ||
* Options for {@link waitForSelector} | ||
*/ | ||
interface WaitForOptions { | ||
/** Maximum time to wait in milliseconds, default is 30000 (30s) */ | ||
timeoutMillisecond?: number | ||
/** Instructs the user agent to observe a given target (a node) | ||
/** | ||
* Instructs the user agent to observe a given target (a node) | ||
* @default document.body | ||
@@ -6,0 +10,0 @@ */ |
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
export async function checkWebpFeature(feature: 'lossy' | 'lossless' | 'alpha' | 'animation' = 'lossy') { | ||
export async function checkWebpFeature(feature: 'lossy' | 'lossless' | 'alpha' | 'animation' = 'lossy'): Promise<boolean> { | ||
return new Promise<boolean>((resolve) => { | ||
@@ -32,4 +32,4 @@ const kTestImages = { | ||
*/ | ||
export async function checkWebpSupport() { | ||
export async function checkWebpSupport(): Promise<boolean> { | ||
return checkWebpFeature('lossy') | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
131949
37
1222
+ Added@utopia-utils/share@0.3.25(transitive)
- Removed@utopia-utils/share@0.3.28(transitive)
Updated@utopia-utils/share@0.3.25