@utopia-utils/dom
Advanced tools
Comparing version 0.3.13 to 0.3.14
@@ -25,2 +25,59 @@ interface PanZoomOptions { | ||
/** | ||
* The function checks if the DOM (Document Object Model) can be used in the current environment. | ||
* @returns a boolean value indicating whether the DOM (Document Object Model) can be used. | ||
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/canUseDom.ts | ||
*/ | ||
declare function canUseDom(): boolean; | ||
/** | ||
* The function checks if a given node is contained within a root node, using either the native | ||
* `contains` method or a fallback method for older browsers. | ||
* @param {Node | null | undefined} root - The `root` parameter represents the root node of a tree or | ||
* subtree. | ||
* @param {Node | null} n - The parameter `n` represents a Node object that we want to check if it is | ||
* contained within the `root` Node object. | ||
* @returns a boolean value. It returns `true` if the `root` node contains the `n` node, and `false` | ||
* otherwise. | ||
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/contains.ts | ||
*/ | ||
declare function domContains(root: Node | null | undefined, n: Node | null): boolean; | ||
type ContainerType = Element | ShadowRoot; | ||
type Prepend = boolean | 'queue'; | ||
type AppendType = 'prependQueue' | 'append' | 'prepend'; | ||
interface Options { | ||
attachTo?: ContainerType; | ||
csp?: { | ||
nonce?: string; | ||
}; | ||
prepend?: Prepend; | ||
/** | ||
* Config the `priority` of `prependQueue`. Default is `0`. | ||
* It's useful if you need to insert style before other style. | ||
*/ | ||
priority?: number; | ||
mark?: string; | ||
} | ||
declare function injectCSS(css: string, option?: Options): HTMLStyleElement | null; | ||
declare function removeCSS(key: string, option?: Options): void; | ||
/** | ||
* manually clear container cache to avoid global cache in unit testes | ||
*/ | ||
declare function clearContainerCache(): void; | ||
/** | ||
* Update CSS text in style element | ||
* @param css CSS text | ||
* @param key style key | ||
* @param option Options | ||
* @returns style element | ||
* @fork https://github.com/react-component/util/blob/master/src/Dom/dynamicCSS.ts | ||
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/dynamicCSS.ts | ||
* @example | ||
* ```js | ||
* updateCSS('body { color: red }', 'my-style') | ||
* ``` | ||
* */ | ||
declare function updateCSS(css: string, key: string, option?: Options): HTMLStyleElement | undefined; | ||
/** | ||
* Returns a boolean value indicating whether the current platform is Android. | ||
@@ -147,2 +204,2 @@ * @returns A boolean value. | ||
export { isAndroid, isIOS, isMobile, isWeixin, loadCSS, loadScript, panzoom, waitForSelector }; | ||
export { AppendType, ContainerType, Prepend, canUseDom, clearContainerCache, domContains, injectCSS, isAndroid, isIOS, isMobile, isWeixin, loadCSS, loadScript, panzoom, removeCSS, updateCSS, waitForSelector }; |
@@ -1,2 +0,2 @@ | ||
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}; | ||
function E(e,n){let t=Math.sign(e),s=Math.min(.25,Math.abs(n*e/128));return 1-t*s}function w(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 $(e,{zoomFactor:n=1,maxZoom:t=5,minZoom:s=.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 o={x:0,y:0,scale:1},c=!1,l={x:0,y:0},m=u=>{if(!c)return;let f=u.clientX-l.x,p=u.clientY-l.y;o.x=o.x+f,o.y=o.y+p,window.requestAnimationFrame(()=>w(e,o,r)),l={x:u.clientX,y:u.clientY}},a=()=>{c=!1,document.removeEventListener("mousemove",m),document.removeEventListener("mouseup",a)},i=u=>{c=!0,l={x:u.clientX,y:u.clientY},document.addEventListener("mousemove",m),document.addEventListener("mouseup",a)};d.addEventListener("mousedown",i);function g(u){let f=d.getBoundingClientRect(),p=u.clientX-f.left,y=u.clientY-f.top;return{x:p,y}}let h=u=>{let f=u.deltaY;u.deltaMode>0&&(f*=100);let p=E(f,n);if(p!==1){let y=g(u);x(y.x,y.y,p),window.requestAnimationFrame(()=>w(e,o,r)),u.preventDefault()}};d.addEventListener("wheel",h);function x(u,f,p){if(isNaN(u)||isNaN(f)||isNaN(p))throw new Error("zoom requires valid numbers");let y=o.scale*p;if(y<s){if(o.scale===s)return;p=s/o.scale}if(y>t){if(o.scale===t)return;p=t/o.scale}o.x=u-p*(u-o.x),o.y=f-p*(f-o.y),o.scale*=p,window.requestAnimationFrame(()=>w(e,o,r))}}function k(){return!!(typeof window!="undefined"&&window.document&&window.document.createElement)}function M(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 O="data-rc-order",T="data-rc-priority",P="rc-util-key",b=new Map;function S({mark:e}={}){return e?e.startsWith("data-")?e:`data-${e}`:P}function v(e){return e.attachTo?e.attachTo:document.querySelector("head")||document.body}function C(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 N(e,n={}){if(!k())return null;let{csp:t,prepend:s,priority:r=0}=n,d=C(s),o=d==="prependQueue",c=document.createElement("style");c.setAttribute(O,d),o&&r&&c.setAttribute(T,`${r}`),t!=null&&t.nonce&&(c.nonce=t==null?void 0:t.nonce),c.innerHTML=e;let l=v(n),{firstChild:m}=l;if(s){if(o){let a=L(l).filter(i=>{if(!["prepend","prependQueue"].includes(i.getAttribute(O)))return!1;let g=Number(i.getAttribute(T)||0);return r>=g});if(a.length)return l.insertBefore(c,a[a.length-1].nextSibling),c}l.insertBefore(c,m)}else l.appendChild(c);return c}function A(e,n={}){let t=v(n);return L(t).find(s=>s.getAttribute(S(n))===e)}function _(e,n={}){let t=A(e,n);t&&v(n).removeChild(t)}function q(e,n){let t=b.get(e);if(!t||!M(document,t)){let s=N("",n);if(!s)return;let{parentNode:r}=s;if(!r)return;b.set(e,r),e.removeChild(s)}}function I(){b.clear()}function X(e,n,t={}){var o,c,l;let s=v(t);q(s,t);let r=A(n,t);if(r)return(o=t.csp)!=null&&o.nonce&&r.nonce!==((c=t.csp)==null?void 0:c.nonce)&&(r.nonce=(l=t.csp)==null?void 0:l.nonce),r.innerHTML!==e&&(r.innerHTML=e),r;let d=N(e,t);if(d)return d.setAttribute(S(t),n),d}function K(){return typeof navigator=="undefined"?!1:/android/i.test(navigator.userAgent)}function G(){return typeof navigator=="undefined"?!1:/iPad|iPhone|iPod/.test(navigator.userAgent)}function V(){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 ee(){let e=navigator.userAgent.toLowerCase();return/MicroMessenger/i.test(e)}function ne(e,n){let{attrs:t={},media:s}=n||{},r=document.querySelector(`link[href="${e}"]`);return r||(r=document.createElement("link"),r.rel="stylesheet",r.href=e,s&&(r.media=s),Object.entries(t).forEach(([d,o])=>r==null?void 0:r.setAttribute(d,o)),document.head.appendChild(r)),{unload:()=>z(e),linkTag:r}}function z(e){let n=document.querySelector(`link[href="${e}"]`);n&&n.remove()}function oe(e,n){let{async:t=!0,defer:s,type:r="text/javascript",crossOrigin:d,referrerPolicy:o,attrs:c={},noModule:l,appendPosition:m="head",onStatusChange:a}=n||{},i=document.querySelector(`script[src="${e}"]`);return i&&(a==null||a("loaded")),i||(i=document.createElement("script"),i.src=e,i.type=r,i.async=t,s&&(i.defer=s),d&&(i.crossOrigin=d),o&&(i.referrerPolicy=o),l&&(i.noModule=l),Object.entries(c).forEach(([h,x])=>i==null?void 0:i.setAttribute(h,x)),(m==="head"?document.head:document.body).appendChild(i),a==null||a("loading")),i==null||i.addEventListener("load",()=>{a==null||a("loaded")}),i==null||i.addEventListener("error",()=>{a==null||a("error")}),i==null||i.addEventListener("abort",()=>{a==null||a("error")}),{unload:()=>H(e),scriptTag:i}}function H(e){let n=document.querySelector(`script[src="${e}"]`);n&&n.remove()}function se(e,n){return new Promise(t=>{if(typeof window.MutationObserver=="undefined")throw new Error("MutationObserver is not supported in this browser");let{timeoutMillisecond:s=3e4,target:r=document.body}=n||{},d=setTimeout(l,s),o=new MutationObserver(m=>{m.some(a=>Array.from(a.addedNodes).some(i=>i instanceof Element&&i.matches(e)))&&(clearTimeout(d),c())});o.observe(r,{childList:!0,subtree:!0}),c();function c(){let m=document.querySelector(e);m&&(o.disconnect(),t(m))}function l(){o.disconnect(),t(null)}})}export{k as canUseDom,I as clearContainerCache,M as domContains,N as injectCSS,K as isAndroid,G as isIOS,V as isMobile,ee as isWeixin,ne as loadCSS,oe as loadScript,$ as panzoom,_ as removeCSS,X as updateCSS,se as waitForSelector}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@utopia-utils/dom", | ||
"type": "module", | ||
"version": "0.3.13", | ||
"version": "0.3.14", | ||
"description": "Collection of common dom utils", | ||
@@ -6,0 +6,0 @@ "author": "Utopia <https://github.com/GreatAuk>", |
export * from './panzoom' | ||
export * from './canUseDom' | ||
export * from './domContains' | ||
export * from './dynamicCSS' | ||
export * from './isAndroid' | ||
@@ -3,0 +6,0 @@ export * from './isIOS' |
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
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
111995
31
1027