|
| 1 | +<template> |
| 2 | + <a :href="shareLink" target="_blank" class="has-margin-right-20" @click="shared()"> |
| 3 | + <b-icon :icon="socialMetaConfig.icon" :type="socialMetaConfig.type" /> |
| 4 | + </a> |
| 5 | +</template> |
| 6 | + |
| 7 | +<script lang="ts"> |
| 8 | +import { Component, Prop, Vue } from 'nuxt-property-decorator' |
| 9 | +import { PageMeta, SocialMetaConfig, SocialMetaType } from '~/types' |
| 10 | +
|
| 11 | +@Component({ |
| 12 | + components: {} |
| 13 | +}) |
| 14 | +export default class SocialShare extends Vue { |
| 15 | + @Prop({ type: Object, required: true }) readonly socialMetaConfig: SocialMetaConfig |
| 16 | + @Prop({ type: Object, required: true }) readonly pageMeta: PageMeta |
| 17 | +
|
| 18 | + get rawLink () { |
| 19 | + const ua = navigator.userAgent.toLowerCase() |
| 20 | +
|
| 21 | + /** |
| 22 | + * On IOS, SMS sharing link need a special formatting |
| 23 | + * Source: https://weblog.west-wind.com/posts/2013/Oct/09/Prefilling-an-SMS-on-Mobile-Devices-with-the-sms-Uri-Scheme#Body-only |
| 24 | + */ |
| 25 | + if (this.socialMetaConfig.type === SocialMetaType.SMS && (ua?.includes('iphone') || ua?.includes('ipad'))) { |
| 26 | + return this.socialMetaConfig.shareLink.replace(':?', ':&') |
| 27 | + } |
| 28 | +
|
| 29 | + return this.socialMetaConfig.shareLink |
| 30 | + } |
| 31 | +
|
| 32 | + get encodedHashtags () { |
| 33 | + if (this.socialMetaConfig.type === SocialMetaType.FACEBOOK && this.pageMeta.tags.length) { |
| 34 | + return '%23' + this.pageMeta.tags.split(',')[0] + ' %23ahanda' |
| 35 | + } |
| 36 | +
|
| 37 | + return this.pageMeta.tags |
| 38 | + } |
| 39 | +
|
| 40 | + get shareLink () { |
| 41 | + let link = this.rawLink |
| 42 | +
|
| 43 | + /** |
| 44 | + * Twitter sharing shouldn't include empty parameter |
| 45 | + * Source: https://github.com/nicolasbeauvais/vue-social-sharing/issues/143 |
| 46 | + */ |
| 47 | + if (this.socialMetaConfig.type === SocialMetaType.TWITTER) { |
| 48 | + if (!this.pageMeta.tags?.length) { |
| 49 | + link = link.replace('&hashtags=@h', '') |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + console.log('LINK', link) |
| 54 | +
|
| 55 | + return link |
| 56 | + .replace(/@u/g, encodeURIComponent(this.pageMeta.url)) |
| 57 | + .replace(/@t/g, encodeURIComponent(this.pageMeta.title)) |
| 58 | + .replace(/@d/g, encodeURIComponent(this.pageMeta.description)) |
| 59 | + .replace(/@h/g, this.encodedHashtags) |
| 60 | + .replace(/@m/g, encodeURIComponent(this.pageMeta.image.src)) |
| 61 | + } |
| 62 | +
|
| 63 | + shared () { |
| 64 | + console.log('Clicked on ' + this.socialMetaConfig.type) |
| 65 | + } |
| 66 | +} |
| 67 | +</script> |
0 commit comments