UNPKG

vue-amazing-ui

Version:

An Amazing Vue3 UI Components Library, Using TypeScript.

2 lines (1 loc) 7.84 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("vue"),h=require("@ctrl/tinycolor"),V=require("@ant-design/colors");function C(e=Date.now(),n="YYYY-MM-DD HH:mm:ss"){try{let t;if(typeof e=="number"||typeof e=="string"){if(t=new Date(e),isNaN(t.getTime()))throw new Error("Invalid date")}else t=e;const r=(l,s=2)=>String(l).padStart(s,"0"),u=l=>{switch(l){case"YYYY":return r(t.getFullYear());case"YY":return r(t.getFullYear()).slice(2,4);case"MM":return r(t.getMonth()+1);case"M":return String(t.getMonth()+1);case"DD":return r(t.getDate());case"D":return String(t.getDate());case"HH":return r(t.getHours());case"H":return String(t.getHours());case"mm":return r(t.getMinutes());case"m":return String(t.getMinutes());case"ss":return r(t.getSeconds());case"s":return String(t.getSeconds());case"SSS":return r(t.getMilliseconds(),3);default:return l}};return n.replace(/(YYYY|YY|M{1,2}|D{1,2}|H{1,2}|m{1,2}|s{1,2}|SSS)/g,u)}catch(t){return console.error("Error formatting date:",t),""}}function N(e,n=2,t=",",r=".",u,l){typeof e!="number"&&typeof e!="string"&&console.warn("Expected value to be of type number or string"),typeof n!="number"&&console.warn("Expected precision to be of type number");const s=Number(e);if(isNaN(s)||!isFinite(s))return"";if(s===0)return s.toFixed(n);let c=s.toFixed(n);if(typeof t=="string"&&t!==""){const[f,a]=c.split(".");c=f.replace(/(\d)(?=(\d{3})+$)/g,"$1"+t)+(a?r+a:"")}return(u||"")+c+(l||"")}function H(e,n=0,t=!1){let r=null;function u(s){if(r||(r=s),s-r>=n){try{e()}catch(f){console.error("Error executing rafTimeout function:",f)}t&&(r=s,l.id=requestAnimationFrame(u))}else l.id=requestAnimationFrame(u)}const l={id:requestAnimationFrame(u)};return l}function U(e){e&&e.id&&typeof e.id=="number"?cancelAnimationFrame(e.id):console.warn("cancelRaf received an invalid id:",e)}function F(e,n=300){let t=!0;return function(...r){if(!t)return!1;t&&(e(...r),t=!1,setTimeout(()=>{t=!0},n))}}function R(e,n=300){let t=null;return function(...r){t&&clearTimeout(t),t=setTimeout(()=>{e(...r)},n)}}function j(e,n){if(Number.isNaN(e)||Number.isNaN(n))throw new Error("Both num1 and num2 must be valid numbers.");const t=e%1!==0,r=n%1!==0;if(!t&&!r)return e+n;const u=String(e).split(".")[1]??"",l=String(n).split(".")[1]??"",s=Math.max(u.length,l.length),c=Math.pow(10,s),f=e.toFixed(s),a=n.toFixed(s);return(+f.replace(".","")+ +a.replace(".",""))/c}function q(e,n){if(!e){console.error("无效的 url");return}const t=n||e.split("?")[0].split("/").pop()||"download";try{fetch(e).then(r=>{r.ok?r.blob().then(u=>{const l=URL.createObjectURL(u),s=document.createElement("a");s.href=l,s.download=t,document.body.appendChild(s),s.click(),document.body.removeChild(s),URL.revokeObjectURL(l)}):console.error("请求文件失败,状态码:",r.status)})}catch(r){console.error("文件下载失败:",r)}}function B(){const e=document.documentElement;e.classList.toggle("dark"),e.classList.contains("dark")?e.style.colorScheme="dark":e.style.colorScheme="light"}function A(){const e=o.ref(!1),n=o.getCurrentInstance();return n&&o.onMounted(()=>{e.value=!0},n),e}function y(e){const n=A();return o.computed(()=>(n.value,!!e()))}function I(e,n,t){o.onMounted(()=>e.addEventListener(n,t)),o.onUnmounted(()=>e.removeEventListener(n,t))}function k(e,n,t={}){const r=y(()=>window&&"MutationObserver"in window),u=o.ref(!1);let l;const s=o.computed(()=>{const i=o.toValue(e);return i?Array.isArray(i)?i.map(m=>o.toValue(m)).filter(m=>m):[i]:[]}),c=()=>{l&&(l.disconnect(),l=void 0)},f=()=>{r.value&&s.value.length&&!u.value&&(l=new MutationObserver(n),s.value.forEach(i=>l.observe(i,t)))};o.watch(()=>s.value,()=>{c(),f()},{immediate:!0,flush:"post"});const a=()=>{u.value=!1,f()},v=()=>{u.value=!0,c()};return o.onBeforeUnmount(()=>c()),{start:a,stop:v}}function P(e=window,n=0,t,r){const u=o.ref(0),l=o.ref(0),s=o.ref(0),c=o.ref(0),f=o.ref(!1),a=o.ref(!1),v=o.ref(!1),i=o.ref(!1),m=o.ref(!1),b=o.ref(0),w=o.ref(0);function D(d){f.value=!0;const g=d.target.documentElement??d.target;u.value=g.scrollLeft,s.value=g.scrollTop,a.value=u.value<b.value,v.value=u.value>b.value,i.value=s.value<w.value,m.value=s.value>w.value,b.value=u.value,w.value=s.value,S(d),t&&t(d)}const M=F(D,n);function O(d){f.value&&(f.value=!1,a.value=!1,v.value=!1,i.value=!1,m.value=!1,r&&r(d))}const S=R(O,n+200),L=o.computed(()=>{const d=o.toValue(e);return d||null});o.watch(()=>L.value,(d,g)=>{if(g&&x(g),d){const p=d?.document?.documentElement||d?.documentElement||d;l.value=p.scrollWidth-p.clientWidth,c.value=p.scrollHeight-p.clientHeight,p.addEventListener("scroll",M),p.addEventListener("scrollend",S)}},{immediate:!0,flush:"post"});function x(d){const g=d?.document?.documentElement||d?.documentElement||d;g.removeEventListener("scroll",M),g.removeEventListener("scrollend",S)}return o.onBeforeUnmount(()=>x(L.value)),{x:u,xScrollMax:l,y:s,yScrollMax:c,isScrolling:f,left:a,right:v,top:i,bottom:m}}function z(){const e=o.ref(0),n=o.ref(0);let t=performance.now();const r=10,u=l=>{if(n.value++,n.value>=r){const s=l-t;e.value=Math.round(1e3/(s/r)),t=l,n.value=0}requestAnimationFrame(u)};return requestAnimationFrame(u),{fps:e}}function G(e){if(!e||typeof e!="string"||e.trim()==="")throw new Error("Invalid mediaQuery parameter. It must be a non-empty string.");const n=o.ref(window&&window.matchMedia(e).matches),t=window.matchMedia(e),r=u=>{n.value=u.matches};return o.onMounted(()=>{t.addEventListener("change",r)}),o.onBeforeUnmount(()=>{t.removeEventListener("change",r)}),{match:n}}function W(e,n,t={}){const r=y(()=>window&&"ResizeObserver"in window);let u;const l=o.ref(!1),s=o.computed(()=>{const i=o.toValue(e);return i?Array.isArray(i)?i.map(m=>o.toValue(m)).filter(m=>m):[i]:[]}),c=()=>{u&&(u.disconnect(),u=void 0)},f=()=>{r.value&&s.value.length&&!l.value&&(u=new ResizeObserver(n),s.value.forEach(i=>u.observe(i,t)))};o.watch(()=>s.value,()=>{c(),f()},{immediate:!0,flush:"post"});const a=()=>{l.value=!1,f()},v=()=>{l.value=!0,c()};return o.onBeforeUnmount(()=>c()),{start:a,stop:v}}function $(e){const n=o.useSlots(),t=r=>{const u=n[r]?.(),l=s=>{if(s.type===o.Comment||Array.isArray(s.children)&&!s.children.length)return!1;if(s.type!==o.Text)return!0;if(typeof s.children=="string")return s.children.trim()!==""};return u&&u?.length?u.some(c=>l(c)):!1};if(Array.isArray(e)){const r=o.reactive({});return e.forEach(u=>{const l=o.computed(()=>t(u));r[u]=l}),r}else return o.computed(()=>t(e))}function Z(e){const n=Y("#1677ff"),t=o.inject("common",o.reactive({colorPalettes:n,shadowColor:T(n[0])})),r=o.inject("components",null);return r!==null&&e in r&&r[e].colorPalettes.length?o.toRefs(r[e]):o.toRefs(t)}function Y(e){return V.generate(e)}function E(e){return e>=0&&e<=255}function T(e,n="#ffffff"){const{r:t,g:r,b:u,a:l}=new h.TinyColor(e).toRgb();if(l<1)return e;const{r:s,g:c,b:f}=new h.TinyColor(n).toRgb();for(let a=.01;a<=1;a+=.01){const v=Math.round((t-s*(1-a))/a),i=Math.round((r-c*(1-a))/a),m=Math.round((u-f*(1-a))/a);if(E(v)&&E(i)&&E(m))return new h.TinyColor({r:v,g:i,b:m,a:Math.round(a*100)/100}).toRgbString()}return new h.TinyColor({r:t,g:r,b:u,a:1}).toRgbString()}function J(e){const n=o.ref(!1);try{const t={get[e](){return n.value=!0,!1}};window.addEventListener("test",()=>null,t),window.removeEventListener("test",()=>null,t)}catch{n.value=!1}return{isSupported:n}}exports.add=j;exports.cancelRaf=U;exports.dateFormat=C;exports.debounce=R;exports.downloadFile=q;exports.formatNumber=N;exports.getAlphaColor=T;exports.getColorPalettes=Y;exports.rafTimeout=H;exports.throttle=F;exports.toggleDark=B;exports.useEventListener=I;exports.useFps=z;exports.useInject=Z;exports.useMediaQuery=G;exports.useMounted=A;exports.useMutationObserver=k;exports.useOptionsSupported=J;exports.useResizeObserver=W;exports.useScroll=P;exports.useSlotsExist=$;exports.useSupported=y;