@hopeio/utils
Version:
javascript utils
166 lines (165 loc) • 6.42 kB
JavaScript
import { m as h } from "./message-CtbwkVlK.js";
import { useEventListener as m } from "@vueuse/core";
import { copyTextToClipboard as N, isFunction as w, subBefore as $, subAfter as x, isArray as D, isObject as f, debounce as C, throttle as k } from "@pureadmin/utils";
const X = {
mounted(e, t) {
const { value: n } = t;
if (n) {
e.copyValue = n;
const o = t.arg ?? "dblclick";
m(e, o, () => {
N(e.copyValue) ? h("复制成功", { type: "success" }) : h("复制失败", { type: "error" });
});
} else
throw new Error(
'[Directive: copy]: need value! Like v-copy="modelValue"'
);
},
updated(e, t) {
e.copyValue = t.value;
}
}, Y = {
mounted(e, t) {
var o;
const n = t.value;
if (n && w(n)) {
let r = null, i = null, s = 500, a = null;
const l = ((o = t == null ? void 0 : t.arg) == null ? void 0 : o.includes(":")) ?? !1;
l ? (s = Number($(t.arg, ":")), a = Number(x(t.arg, ":"))) : t.arg && (s = Number(t.arg));
const p = () => {
r && (clearTimeout(r), r = null), i && (clearInterval(i), i = null);
}, u = (c) => {
c.preventDefault(), i === null && (i = setInterval(() => n(), a));
};
m(e, "pointerdown", (c) => {
p(), c.preventDefault(), r === null && (r = setTimeout(l ? () => {
n(), u(c);
} : () => n(), s));
}), m(e, "pointerup", p), m(e, "pointerleave", p);
} else
throw new Error(
'[Directive: longpress]: need callback and callback must be a function! Like v-longpress="callback"'
);
}
}, q = {
mounted(e, t) {
const { value: n } = t, o = t.arg ?? "debounce", r = ["debounce", "throttle"].find((i) => i === o);
if (r)
if (n && n.event && w(n.fn)) {
let i = n == null ? void 0 : n.params;
if (i)
if (D(i) || f(i))
i = f(i) ? Array.of(i) : i;
else
throw new Error(
"[Directive: optimize]: `params` must be an array or object"
);
m(
e,
n.event,
r === "debounce" ? C(
i ? () => n.fn(...i) : n.fn,
(n == null ? void 0 : n.timeout) ?? 200,
(n == null ? void 0 : n.immediate) ?? !1
) : k(
i ? () => n.fn(...i) : n.fn,
(n == null ? void 0 : n.timeout) ?? 1e3
)
);
} else
throw new Error(
"[Directive: optimize]: `event` and `fn` are required, and `fn` must be a function"
);
else
throw new Error(
"[Directive: optimize]: only `debounce` and `throttle` are supported"
);
}
};
function y(e, t) {
e.style.transform = t, e.style.webkitTransform = t;
}
const z = (e, t, n = {}) => {
var c;
const o = t.getBoundingClientRect();
let r = e.clientX - o.left, i = e.clientY - o.top, s = 0, a = 0.3;
(c = t._ripple) != null && c.circle ? (a = 0.15, s = t.clientWidth / 2, s = n.center ? s : s + Math.sqrt((r - s) ** 2 + (i - s) ** 2) / 4) : s = Math.sqrt(t.clientWidth ** 2 + t.clientHeight ** 2) / 2;
const l = `${(t.clientWidth - s * 2) / 2}px`, p = `${(t.clientHeight - s * 2) / 2}px`, u = n.center ? l : `${r - s}px`, d = n.center ? p : `${i - s}px`;
return { radius: s, scale: a, x: u, y: d, centerX: l, centerY: p };
}, _ = {
show(e, t, n = {}) {
var v;
if (!((v = t == null ? void 0 : t._ripple) != null && v.enabled))
return;
const o = document.createElement("span"), r = document.createElement("span");
o.appendChild(r), o.className = "v-ripple__container", n.class && (o.className += ` ${n.class}`);
const { radius: i, scale: s, x: a, y: l, centerX: p, centerY: u } = z(e, t, n), d = `${i * 2}px`;
r.className = "v-ripple__animation", r.style.width = d, r.style.height = d, t.appendChild(o);
const c = window.getComputedStyle(t);
c && c.position === "static" && (t.style.position = "relative", t.dataset.previousPosition = "static"), r.classList.add("v-ripple__animation--enter"), r.classList.add("v-ripple__animation--visible"), y(
r,
`translate(${a}, ${l}) scale3d(${s},${s},${s})`
), r.dataset.activated = String(performance.now()), setTimeout(() => {
r.classList.remove("v-ripple__animation--enter"), r.classList.add("v-ripple__animation--in"), y(r, `translate(${p}, ${u}) scale3d(1,1,1)`);
}, 0);
},
hide(e) {
var i;
if (!((i = e == null ? void 0 : e._ripple) != null && i.enabled)) return;
const t = e.getElementsByClassName("v-ripple__animation");
if (t.length === 0) return;
const n = t[t.length - 1];
if (n.dataset.isHiding) return;
n.dataset.isHiding = "true";
const o = performance.now() - Number(n.dataset.activated), r = Math.max(250 - o, 0);
setTimeout(() => {
n.classList.remove("v-ripple__animation--in"), n.classList.add("v-ripple__animation--out"), setTimeout(() => {
var a;
e.getElementsByClassName("v-ripple__animation").length === 1 && e.dataset.previousPosition && (e.style.position = e.dataset.previousPosition, delete e.dataset.previousPosition), ((a = n.parentNode) == null ? void 0 : a.parentNode) === e && e.removeChild(n.parentNode);
}, 300);
}, r);
}
};
function b(e) {
return typeof e > "u" || !!e;
}
function E(e) {
const t = {}, n = e.currentTarget;
!(n != null && n._ripple) || n._ripple.touched || (t.center = n._ripple.centered, n._ripple.class && (t.class = n._ripple.class), _.show(e, n, t));
}
function g(e) {
const t = e.currentTarget;
t != null && t._ripple && (window.setTimeout(() => {
t._ripple && (t._ripple.touched = !1);
}), _.hide(t));
}
function L(e, t, n) {
const { value: o, modifiers: r } = t, i = b(o);
i || _.hide(e), e._ripple = e._ripple ?? {}, e._ripple.enabled = i, e._ripple.centered = r.center, e._ripple.circle = r.circle, f(o) && o.class && (e._ripple.class = o.class), i && !n ? (e.addEventListener("pointerdown", E), e.addEventListener("pointerup", g)) : !i && n && T(e);
}
function T(e) {
e.removeEventListener("pointerdown", E), e.removeEventListener("pointerup", g);
}
function V(e, t) {
L(e, t, !1);
}
function H(e) {
delete e._ripple, T(e);
}
function B(e, t) {
if (t.value === t.oldValue)
return;
const n = b(t.oldValue);
L(e, t, n);
}
const A = {
mounted: V,
unmounted: H,
updated: B
};
export {
A as Ripple,
X as copy,
Y as longpress,
q as optimize
};