@utopia-utils/dom
Advanced tools
Comparing version 0.3.6 to 0.3.7
@@ -1,17 +0,1 @@ | ||
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) | ||
* @default document.body | ||
*/ | ||
target?: Node; | ||
} | ||
/** | ||
* waits for an element to appear in the DOM | ||
* @param {string} selector - The CSS selector to wait for. | ||
* @param {WaitForOptions} [options] | ||
* @returns A promise that resolves to an element that matches the selector. | ||
*/ | ||
declare function waitForSelector<T extends Element>(selector: string, options?: WaitForOptions): Promise<T | null>; | ||
interface PanZoomOptions { | ||
@@ -37,2 +21,117 @@ zoomFactor?: number; | ||
export { panzoom, waitForSelector }; | ||
/** | ||
* Returns a boolean value indicating whether the current platform is Android. | ||
* @returns A boolean value. | ||
*/ | ||
declare function isAndroid(): boolean; | ||
/** | ||
* If the user agent string contains the word 'iPad', 'iPhone', or 'iPod', then we're on iOS. | ||
* @returns A boolean value. | ||
*/ | ||
declare function isIOS(): boolean; | ||
/** | ||
* detect current environment is a mobile device | ||
* @returns A boolean value. | ||
*/ | ||
declare function isMobile(): boolean; | ||
/** | ||
* Check if the current environment is Weixin. (垎俥) | ||
* @returns A boolean value. | ||
*/ | ||
declare function isWeixin(): boolean; | ||
interface LoadCSSOptions { | ||
/** | ||
* Media query for styles to apply | ||
* @example 'print', 'all', 'screen and (max-width: 768px)' and etc. | ||
*/ | ||
media?: string; | ||
/** | ||
* Add custom attribute to the script tag | ||
* | ||
*/ | ||
attrs?: Record<string, string>; | ||
} | ||
/** | ||
* It loads a CSS file into the page | ||
* @param {string} path - the path to the CSS file | ||
* @param {LoadCSSOptions} [options] - { | ||
* @returns An object with two properties: | ||
* - unload: a function that removes the script tag | ||
* - linkTag: the link tag that was created | ||
*/ | ||
declare function loadCSS(path: string, options?: LoadCSSOptions): { | ||
/** remove the script tag */ | ||
unload: () => void; | ||
linkTag: HTMLLinkElement; | ||
}; | ||
interface LoadScriptOptions { | ||
/** | ||
* Add `async` attribute to the script tag | ||
* @default true | ||
*/ | ||
async?: boolean; | ||
/** | ||
* Add `defer` attribute to the script tag | ||
*/ | ||
defer?: boolean; | ||
/** | ||
* Script type | ||
* | ||
* @default 'text/javascript' | ||
* @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#attr-type | ||
*/ | ||
type?: string; | ||
crossOrigin?: 'anonymous' | 'use-credentials'; | ||
/** | ||
* @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'; | ||
noModule?: boolean; | ||
/** | ||
* Add custom attribute to the script tag | ||
* | ||
*/ | ||
attrs?: Record<string, string>; | ||
/** | ||
* Append the script tag to the document.head or document.body | ||
* @default 'head' | ||
*/ | ||
appendPosition?: 'head' | 'body'; | ||
onStatusChange?: (status: 'loading' | 'loaded' | 'error') => void; | ||
} | ||
/** | ||
* It loads a script tag into the DOM | ||
* @param {string} src - The URL of the script to load. | ||
* @param {LoadScriptOptions} [options] - LoadScriptOptions | ||
* @returns An object with two properties: | ||
* - unload: a function that removes the script tag | ||
* - scriptTag: the script tag that was created | ||
*/ | ||
declare function loadScript(src: string, options?: LoadScriptOptions): { | ||
/** remove the script tag */ | ||
unload: () => void; | ||
scriptTag: HTMLScriptElement; | ||
}; | ||
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) | ||
* @default document.body | ||
*/ | ||
target?: Node; | ||
} | ||
/** | ||
* waits for an element to appear in the DOM | ||
* @param {string} selector - The CSS selector to wait for. | ||
* @param {WaitForOptions} [options] | ||
* @returns A promise that resolves to an element that matches the selector. | ||
*/ | ||
declare function waitForSelector<T extends Element>(selector: string, options?: WaitForOptions): Promise<T | null>; | ||
export { isAndroid, isIOS, isMobile, isWeixin, loadCSS, loadScript, panzoom, waitForSelector }; |
@@ -1,2 +0,2 @@ | ||
function h(o,s){return new Promise(r=>{if(typeof window.MutationObserver=="undefined")throw new Error("MutationObserver is not supported in this browser");let{timeoutMillisecond:u=3e4,target:f=document.body}=s||{},l=setTimeout(d,u),e=new MutationObserver(c=>{c.some(p=>Array.from(p.addedNodes).some(y=>y instanceof Element&&y.matches(o)))&&(clearTimeout(l),m())});e.observe(f,{childList:!0,subtree:!0}),m();function m(){let c=document.querySelector(o);c&&(e.disconnect(),r(c))}function d(){e.disconnect(),r(null)}})}function w(o,s){let r=Math.sign(o),u=Math.min(.25,Math.abs(s*o/128));return 1-r*u}function v(o,s,r){o.style.transformOrigin="0 0 0",o.style.transform=`matrix(${s.scale}, 0, 0, ${s.scale}, ${s.x}, ${s.y})`,r==null||r(s)}function L(o,{zoomFactor:s=1,maxZoom:r=5,minZoom:u=.2,onTransform:f}={}){let l=o.parentNode;if(!l)throw new Error("target element must have a parent node!");l.style.overflow="hidden",l.style.userSelect="none";let e={x:0,y:0,scale:1},m=!1,d={x:0,y:0},c=t=>{if(!m)return;let i=t.clientX-d.x,n=t.clientY-d.y;e.x=e.x+i,e.y=e.y+n,window.requestAnimationFrame(()=>v(o,e,f)),d={x:t.clientX,y:t.clientY}},p=()=>{m=!1,document.removeEventListener("mousemove",c),document.removeEventListener("mouseup",p)},y=t=>{m=!0,d={x:t.clientX,y:t.clientY},document.addEventListener("mousemove",c),document.addEventListener("mouseup",p)};l.addEventListener("mousedown",y);function x(t){let i=l.getBoundingClientRect(),n=t.clientX-i.left,a=t.clientY-i.top;return{x:n,y:a}}let E=t=>{let i=t.deltaY;t.deltaMode>0&&(i*=100);let n=w(i,s);if(n!==1){let a=x(t);M(a.x,a.y,n),window.requestAnimationFrame(()=>v(o,e,f)),t.preventDefault()}};l.addEventListener("wheel",E);function M(t,i,n){if(isNaN(t)||isNaN(i)||isNaN(n))throw new Error("zoom requires valid numbers");let a=e.scale*n;if(a<u){if(e.scale===u)return;n=u/e.scale}if(a>r){if(e.scale===r)return;n=r/e.scale}e.x=t-n*(t-e.x),e.y=i-n*(i-e.y),e.scale*=n,window.requestAnimationFrame(()=>v(o,e,f))}}export{L as panzoom,h as waitForSelector}; | ||
function h(e,n){let a=Math.sign(e),c=Math.min(.25,Math.abs(n*e/128));return 1-a*c}function v(e,n,a){e.style.transformOrigin="0 0 0",e.style.transform=`matrix(${n.scale}, 0, 0, ${n.scale}, ${n.x}, ${n.y})`,a==null||a(n)}function L(e,{zoomFactor:n=1,maxZoom:a=5,minZoom:c=.2,onTransform:s}={}){let l=e.parentNode;if(!l)throw new Error("target element must have a parent node!");l.style.overflow="hidden",l.style.userSelect="none";let o={x:0,y:0,scale:1},f=!1,u={x:0,y:0},p=i=>{if(!f)return;let m=i.clientX-u.x,d=i.clientY-u.y;o.x=o.x+m,o.y=o.y+d,window.requestAnimationFrame(()=>v(e,o,s)),u={x:i.clientX,y:i.clientY}},r=()=>{f=!1,document.removeEventListener("mousemove",p),document.removeEventListener("mouseup",r)},t=i=>{f=!0,u={x:i.clientX,y:i.clientY},document.addEventListener("mousemove",p),document.addEventListener("mouseup",r)};l.addEventListener("mousedown",t);function b(i){let m=l.getBoundingClientRect(),d=i.clientX-m.left,g=i.clientY-m.top;return{x:d,y:g}}let w=i=>{let m=i.deltaY;i.deltaMode>0&&(m*=100);let d=h(m,n);if(d!==1){let g=b(i);y(g.x,g.y,d),window.requestAnimationFrame(()=>v(e,o,s)),i.preventDefault()}};l.addEventListener("wheel",w);function y(i,m,d){if(isNaN(i)||isNaN(m)||isNaN(d))throw new Error("zoom requires valid numbers");let g=o.scale*d;if(g<c){if(o.scale===c)return;d=c/o.scale}if(g>a){if(o.scale===a)return;d=a/o.scale}o.x=i-d*(i-o.x),o.y=m-d*(m-o.y),o.scale*=d,window.requestAnimationFrame(()=>v(e,o,s))}}function q(){return typeof navigator=="undefined"?!1:/android/i.test(navigator.userAgent)}function z(){return typeof navigator=="undefined"?!1:/iPad|iPhone|iPod/.test(navigator.userAgent)}function T(){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 N(){let e=navigator.userAgent.toLowerCase();return/MicroMessenger/i.test(e)}function H(e,n){let{attrs:a={},media:c}=n||{},s=document.querySelector(`link[href="${e}"]`);return s||(s=document.createElement("link"),s.rel="stylesheet",s.href=e,c&&(s.media=c),Object.entries(a).forEach(([l,o])=>s==null?void 0:s.setAttribute(l,o)),document.head.appendChild(s)),{unload:()=>x(e),linkTag:s}}function x(e){let n=document.querySelector(`link[href="${e}"]`);n&&n.remove()}function F(e,n){let{async:a=!0,defer:c,type:s="text/javascript",crossOrigin:l,referrerPolicy:o,attrs:f={},noModule:u,appendPosition:p="head",onStatusChange:r}=n||{},t=document.querySelector(`script[src="${e}"]`);return t&&(r==null||r("loaded")),t||(t=document.createElement("script"),t.src=e,t.type=s,t.async=a,c&&(t.defer=c),l&&(t.crossOrigin=l),o&&(t.referrerPolicy=o),u&&(t.noModule=u),Object.entries(f).forEach(([w,y])=>t==null?void 0:t.setAttribute(w,y)),(p==="head"?document.head:document.body).appendChild(t),r==null||r("loading")),t==null||t.addEventListener("load",()=>{r==null||r("loaded")}),t==null||t.addEventListener("error",()=>{r==null||r("error")}),t==null||t.addEventListener("abort",()=>{r==null||r("error")}),{unload:()=>k(e),scriptTag:t}}function k(e){let n=document.querySelector(`script[src="${e}"]`);n&&n.remove()}function Y(e,n){return new Promise(a=>{if(typeof window.MutationObserver=="undefined")throw new Error("MutationObserver is not supported in this browser");let{timeoutMillisecond:c=3e4,target:s=document.body}=n||{},l=setTimeout(u,c),o=new MutationObserver(p=>{p.some(r=>Array.from(r.addedNodes).some(t=>t instanceof Element&&t.matches(e)))&&(clearTimeout(l),f())});o.observe(s,{childList:!0,subtree:!0}),f();function f(){let p=document.querySelector(e);p&&(o.disconnect(),a(p))}function u(){o.disconnect(),a(null)}})}export{q as isAndroid,z as isIOS,T as isMobile,N as isWeixin,H as loadCSS,F as loadScript,L as panzoom,Y as waitForSelector}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@utopia-utils/dom", | ||
"type": "module", | ||
"version": "0.3.6", | ||
"version": "0.3.7", | ||
"description": "Collection of common dom utils", | ||
@@ -6,0 +6,0 @@ "author": "Utopia <https://github.com/GreatAuk>", |
@@ -0,2 +1,8 @@ | ||
export * from './panzoom' | ||
export * from './isAndroid' | ||
export * from './isIOS' | ||
export * from './isMobile' | ||
export * from './isWeixin' | ||
export * from './loadCSS' | ||
export * from './loadScript' | ||
export * from './waitForSelector' | ||
export * from './panzoom' |
@@ -1,4 +0,1 @@ | ||
/** | ||
* @vitest-environment happy-dom | ||
*/ | ||
import { waitForSelector } from './waitForSelector' | ||
@@ -35,4 +32,4 @@ | ||
const timeEnd = Date.now() | ||
expect(timeEnd - timeStart).toBeGreaterThanOrEqual(10) | ||
expect(timeEnd - timeStart).toBeGreaterThanOrEqual(9) | ||
}) | ||
}) |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76159
28
746
1