UNPKG

@alessiofrittoli/web-utils

Version:
3 lines (2 loc) 3.82 kB
'use strict';var urlUtils=require('@alessiofrittoli/url-utils');require('@alessiofrittoli/date-utils'),require('@alessiofrittoli/math-utils');var U=e=>typeof window<"u"?window.matchMedia(e).matches:false;var u=e=>new Map(e);var g=(e={})=>{let t=Math.max(100,e.width||600),n=Math.max(100,e.height||800),{url:r,context:a}=e,{features:s={}}=e,{left:o=window.screen.width/2-(t/2+10),top:p=window.screen.height/2-(n/2+50),...x}=s,m=u([["popup","yes"],["width",t.toString()],["height",n.toString()],["left",o.toString()],["top",p.toString()]]);Object.entries(x).map(([f,w])=>{m.set(f,w);});let y=Array.from(m).map(f=>f.join("=")).join(",");return window.open(r&&urlUtils.Url.format(r),a,y)};var l=e=>Array.isArray(e)?e.filter(Boolean).map(t=>{if(typeof t!="string"){if(!t.email)return null;let n=t.name?.trim(),r=t.email.trim();return n?`${n}<${r}>`:r}return t.trim()}).filter(Boolean).join(","):l([e]),d=(e={})=>{let{to:t,body:n,subject:r,cc:a,bcc:s}=e,o=new URLSearchParams;if(n&&o.append("body",n),r&&o.append("subject",r),a){let p=l(a);p&&o.append("cc",p);}if(s){let p=l(s);p&&o.append("bcc",p);}return ["mailto:",t&&l(t),o.size>0&&`?${o.toString().replace(/\+/g," ")}`].filter(Boolean).join("")};var P=()=>typeof navigator<"u"&&typeof navigator.canShare=="function"&&typeof navigator.share=="function",S=e=>P()&&navigator.canShare(e),V=async(e={})=>{let{title:t=document.title,text:n,url:r,files:a}=e,s=Object.keys(e).length>0?{title:t,text:n,url:r?urlUtils.Url.format(r):void 0,files:a}:{title:t,url:urlUtils.Url.format(location)};return S(s)?navigator.share(s):false},_=(e={})=>{window.location.href=d(e);},i=e=>{let{url:t=location,sharer:n,urlParam:r="url",...a}=e,s=urlUtils.Url.format(t),o=urlUtils.Url.parse(n);return o.searchParams.append(r,s),g({url:o,...a,context:"sharer"})},N=(e={})=>{let{title:t=document.title,...n}=e,r=new URL("https://facebook.com/sharer/sharer.php");return t&&r.searchParams.append("t",t),i({...n,sharer:r,urlParam:"u"})},J=(e={})=>{let{title:t=document.title,...n}=e,r=new URL("https://work.workplace.com/sharer.php");return t&&r.searchParams.append("t",t),i({...n,sharer:r})},z=(e={})=>{let{title:t=document.title,...n}=e,r=new URL("https://reddit.com/submit");return t&&r.searchParams.append("title",t),i({...n,sharer:r})},H=(e={})=>i({...e,sharer:"https://service.weibo.com/share/share.php"}),Z=(e={})=>i({...e,sharer:"https://vk.com/share.php"}),q=(e={})=>i({...e,sharer:"https://tumblr.com/widgets/share/tool",urlParam:"canonicalUrl"}),X=(e={})=>i({...e,sharer:"https://social-plugins.line.me/lineit/share"}),G=(e={})=>i({...e,sharer:"https://x.com/intent/tweet"}),Q=(e={})=>i({...e,sharer:"https://linkedin.com/sharing/share-offsite"}),Y=(e={})=>i({...e,sharer:"https://telegram.me/share/url"}),ee=(e={})=>{let{url:t=location,media:n,description:r=document.title||document.querySelector('[name="description"]')?.getAttribute("content"),...a}=e,s=urlUtils.Url.format(t),o=new URL("https://pinterest.it/pin-builder");return o.searchParams.append("method","button"),o.searchParams.append("media",n?urlUtils.Url.format(n):s),r&&o.searchParams.append("description",r),i({...a,url:t,sharer:o})},te=(e={})=>{let{url:t=location,text:n,...r}=e,a=urlUtils.Url.format(t),s=new URL("https://api.whatsapp.com/send"),o=n?`${n} ${a}`:a;return s.search=new URLSearchParams({text:o}).toString(),g({url:s,...r,context:"sharer"})};exports.canWebApiShare=P;exports.canWebApiShareData=S;exports.getMediaMatches=U;exports.openBrowserPopUp=g;exports.openSharerPopUp=i;exports.share=V;exports.shareOnFacebook=N;exports.shareOnLine=X;exports.shareOnLinkedIn=Q;exports.shareOnPinterest=ee;exports.shareOnReddit=z;exports.shareOnTelegram=Y;exports.shareOnTumblr=q;exports.shareOnVK=Z;exports.shareOnWeibo=H;exports.shareOnWhatsApp=te;exports.shareOnWorkplace=J;exports.shareOnX=G;exports.shareViaEmail=_;