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