vue3-social-sharing
Version:
Vue 3 social sharing plugin
132 lines (131 loc) • 5.28 kB
JavaScript
import { toValue as u, inject as p, defineComponent as a, h as c } from "vue";
const l = {
baidu: "http://cang.baidu.com/do/add?iu=@u&it=@t",
bluesky: "https://bsky.app/intent/compose?text=@t%0A@u",
buffer: "https://bufferapp.com/add?text=@t&url=@u",
email: "mailto:?subject=@t&body=@u%0D%0A@d",
evernote: "https://www.evernote.com/clip.action?url=@u&title=@t",
facebook: "https://www.facebook.com/sharer/sharer.php?u=@u&title=@t&description=@d"e=@q&hashtag=@h",
flipboard: "https://share.flipboard.com/bookmarklet/popout?v=2&url=@u&title=@t",
hackernews: "https://news.ycombinator.com/submitlink?u=@u&t=@t",
instapaper: "http://www.instapaper.com/edit?url=@u&title=@t&description=@d",
line: "http://line.me/R/msg/text/?@t%0D%0A@u%0D%0A@d",
linkedin: "https://www.linkedin.com/sharing/share-offsite/?url=@u",
messenger: "fb-messenger://share/?link=@u",
odnoklassniki: "https://connect.ok.ru/dk?st.cmd=WidgetSharePreview&st.shareUrl=@u&st.comments=@t",
pinterest: "https://pinterest.com/pin/create/button/?url=@u&media=@m&description=@t",
pocket: "https://getpocket.com/save?url=@u&title=@t",
quora: "https://www.quora.com/share?url=@u&title=@t",
reddit: "https://www.reddit.com/submit?url=@u&title=@t",
skype: "https://web.skype.com/share?url=@t%0D%0A@u%0D%0A@d",
sms: "sms:?body=@t%0D%0A@u%0D%0A@d",
stumbleupon: "https://www.stumbleupon.com/submit?url=@u&title=@t",
telegram: "https://t.me/share/url?url=@u&text=@t%0D%0A@d",
tumblr: "https://www.tumblr.com/share/link?url=@u&name=@t&description=@d",
twitter: "https://twitter.com/intent/tweet?text=@t&url=@u&hashtags=@h&via=@tu",
x: "https://x.com/intent/post?text=@t&url=@u&hashtags=@h&via=@tu",
viber: "viber://forward?text=@t%0D%0A@u%0D%0A@d",
vk: "https://vk.com/share.php?url=@u&title=@t&image=@m&noparse=true",
weibo: "http://service.weibo.com/share/share.php?url=@u&title=@t&pic=@m",
whatsapp: "https://api.whatsapp.com/send?text=@t%0D%0A@u%0D%0A@d",
wordpress: "https://wordpress.com/press-this.php?u=@u&t=@t&s=@d&i=@m",
xing: "https://www.xing.com/social/share/spi?op=share&url=@u&title=@t",
yammer: "https://www.yammer.com/messages/new?login=true&status=@t%0D%0A@u%0D%0A@d",
threads: "https://www.threads.net/intent/post?url=@u&text=@t"
}, i = () => typeof window < "u", w = (e, t) => {
if (!i())
return null;
const r = window.innerWidth || document.documentElement.clientWidth || window.screenX, s = window.innerHeight || document.documentElement.clientHeight || window.screenY, n = r / window.screen.availWidth, o = (r - e) / 2 / n + (window.screenLeft !== void 0 ? window.screenLeft : window.screenX);
return {
top: (s - t) / 2 / n + (window.screenTop !== void 0 ? window.screenTop : window.screenY),
left: o,
width: e,
height: t
};
}, d = (e) => Object.entries(e).map(([t, r]) => r ? `${t}=${r}` : "").filter((t) => t).join(","), h = (e) => {
if (!i())
return null;
if (e.substring(0, 4) !== "http")
return window.open(e, "_blank");
const t = w(500, 500);
return t ? window.open(e, "sharer", d(t)) : null;
}, m = (e, t) => {
const r = window.navigator.userAgent.toLowerCase();
return e === "sms" && (r.includes("iphone") || r.includes("ipad")) ? t[e].replace(":?", ":&") : t[e];
}, g = (e, t) => t === "facebook" && e && e.length > 0 ? `%23${e.split(",")[0]}` : e, f = () => ({
network: "",
url: "",
title: "",
description: "",
quote: "",
hashtags: "",
twitterUser: "",
media: ""
}), k = (e) => {
const t = f();
return Object.fromEntries(Object.keys(t).map((r) => {
let s = u(e[r]) || t[r];
return s = r === "hashtags" ? g(s, r) : encodeURIComponent(s), [r, s];
}));
}, b = (e, t) => e.replace(/@tu/g, t.twitterUser).replace(/@u/g, t.url).replace(/@t/g, t.title).replace(/@d/g, t.description).replace(/@q/g, t.quote).replace(/@h/g, t.hashtags).replace(/@m/g, t.media), v = () => {
const e = Object.assign({}, l, p("share-networks", {})), t = (s) => {
const n = k(s);
if (!n.network || !e[n.network])
throw new Error(`Network ${n.network} does not exist`);
let o = m(n.network, e);
return n.network === "twitter" && (n.hashtags.length || (o = o.replace("&hashtags=@h", "")), n.twitterUser.length || (o = o.replace("&via=@tu", ""))), b(o, n);
};
return { getSharingLink: t, shareLink: (s) => {
const n = t(s);
return h(n);
} };
}, y = a({
props: {
network: {
type: String,
required: !0
},
url: {
type: String,
default: ""
},
title: {
type: String,
default: ""
},
description: {
type: String,
default: ""
},
quote: {
type: String,
default: ""
},
hashtags: {
type: String,
default: ""
},
twitterUser: {
type: String,
default: ""
},
media: {
type: String,
default: ""
}
},
setup(e, t) {
const { shareLink: r } = v();
return () => t.slots.default ? t.slots.default({ share: () => r(e) }) : c("div", { onClick: () => r(e) }, "Share");
}
}), x = {
install(e, t = {}) {
e.provide("share-networks", t.shareNetworks || {}), e.component("share-network", y);
}
};
export {
y as ShareNetwork,
l as availableNetworks,
x as default,
v as useShareLink
};