@acrool/js-utils
Version:
Common javascript utils methods for project development
75 lines (74 loc) • 2.3 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, n) {
if (document.getElementById(e) === null) {
const t = document.createElement("iframe");
t.id = e, t.src = o, t.scrolling = "no", t.frameborder = "0", t.width = 0, t.height = 0, t.onload = () => {
n && n(t);
}, document.body.appendChild(t);
}
}
function m(e, o, n) {
if (document.getElementById(e) === null) {
const t = document.createElement("script");
t.id = e, n && (t.type = n), t.append(o), document.head.appendChild(t);
}
}
function p(e, o, n) {
if (document.getElementById(e) === null) {
const t = document.createElement("script");
t.id = e, t.src = o, n && (t.onload = n), document.head.appendChild(t);
}
return !1;
}
const u = async (e, o) => {
if (navigator.clipboard && window.ClipboardItem)
try {
const n = new Blob([e], { type: "text/plain" }), t = new ClipboardItem({ "text/plain": n });
await navigator.clipboard.write([t]), console.log("Text copied using Clipboard API");
return;
} catch {
console.warn("Clipboard API failed, falling back to execCommand");
}
return new Promise((n, t) => {
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"), n()) : t(new Error("execCommand failed"));
} catch (c) {
t(c);
} finally {
i.remove();
}
});
}, f = (e, o) => {
l(window.URL.createObjectURL(e), o);
}, l = (e, o) => {
const n = document.createElement("a");
n.href = e, n.download = o, n.click();
}, b = (e) => {
const n = e.getBoundingClientRect().top;
return window.innerHeight - n > n ? "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
};