@acrool/js-utils
Version:
Common javascript utils methods for project development
75 lines (74 loc) • 2.27 kB
JavaScript
function d(e, o) {
return !!e.className.match(new RegExp(`(\\s|^)${o}(\\s|$)`));
}
function r(e, o) {
d(e, o) || e.classList.add(o);
}
function a(e, o) {
e.classList.remove(o);
}
function s(e, o, t) {
if (document.getElementById(e) === null) {
const n = document.createElement("iframe");
n.id = e, n.src = o, n.scrolling = "no", n.frameborder = "0", n.width = 0, n.height = 0, n.onload = () => {
t && t(n);
}, document.body.appendChild(n);
}
}
function m(e, o) {
if (document.getElementById(e) === null) {
const t = document.createElement("script");
t.id = e, t.append(o), document.head.appendChild(t);
}
}
function p(e, o, t) {
if (document.getElementById(e) === null) {
const n = document.createElement("script");
n.id = e, n.src = o, t && (n.onload = t), document.head.appendChild(n);
}
return !1;
}
const u = async (e, o) => {
if (navigator.clipboard && window.ClipboardItem)
try {
const t = new Blob([e], { type: "text/plain" }), n = new ClipboardItem({ "text/plain": t });
await navigator.clipboard.write([n]), console.log("Text copied using Clipboard API");
return;
} catch {
console.warn("Clipboard API failed, falling back to execCommand");
}
return new Promise((t, n) => {
const i = document.createElement("textarea");
i.style.position = "fixed", i.style.opacity = "0", i.inputMode = "none", o ? i.value = e : i.innerText = e, document.body.appendChild(i), i.select();
try {
document.execCommand("copy") ? (console.log("Text copied using execCommand"), t()) : n(new Error("execCommand failed"));
} catch (c) {
n(c);
} finally {
i.remove();
}
});
}, f = (e, o) => {
l(window.URL.createObjectURL(e), o);
}, l = (e, o) => {
const t = document.createElement("a");
t.href = e, t.download = o, t.click();
}, b = (e) => {
const t = e.getBoundingClientRect().top;
return window.innerHeight - t > t ? "bottom" : "top";
}, w = () => {
document.activeElement.blur();
};
export {
w as activeElementBlur,
r as addClass,
u as copyToClipboard,
f as downloadBlob,
l as downloadUrl,
b as getVisiblePosition,
d as hasClass,
s as insertIFrame,
m as insertScriptContent,
p as insertScriptSrc,
a as removeClass
};