UNPKG

@acrool/js-utils

Version:

Common javascript utils methods for project development

2 lines (1 loc) 1.97 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function c(e,o){return!!e.className.match(new RegExp(`(\\s|^)${o}(\\s|$)`))}function d(e,o){c(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(r){t(r)}finally{i.remove()}})},b=(e,o)=>{l(window.URL.createObjectURL(e),o)},l=(e,o)=>{const n=document.createElement("a");n.href=e,n.download=o,n.click()},f=e=>{const n=e.getBoundingClientRect().top;return window.innerHeight-n>n?"bottom":"top"},C=()=>{document.activeElement.blur()};exports.activeElementBlur=C;exports.addClass=d;exports.copyToClipboard=u;exports.downloadBlob=b;exports.downloadUrl=l;exports.getVisiblePosition=f;exports.hasClass=c;exports.insertIFrame=s;exports.insertScriptContent=m;exports.insertScriptSrc=p;exports.removeClass=a;