import { Component, Prop, Vue } from "vue-property-decorator"; import axios from 'axios'; import Button from "@/components/Button/index.vue"; import Icon from "@/components/Icon/index.vue"; import Skeleton from "@/components/Skeleton/index.vue"; @Component({ components: { CopyToClipboard: () => import("../CopyToClipboard/index.vue"), Button, Icon, Skeleton, }, }) export default class Share extends Vue { @Prop({ type: Boolean, default: false }) loading?: boolean; @Prop({ type: String, default: "" }) text?: string; @Prop({ type: String, default: "" }) title?: string; @Prop({ type: Boolean, default: false }) footer?: boolean; @Prop({ type: String, default: "" }) urlTlg?: string; @Prop({ type: String, default: "" }) urlWApp?: string; @Prop({ type: String, default: "" }) titlePost?: string; get qrcode(): string { if (this.text && this.text.match(/^http/)) { return `https://tools.zarinpal.com/qr?r=q&s=256&q=${this.text}`; } return ""; } private socialAdrs: any = { telegram: "https://t.me/share/url", whatsapp: "https://api.whatsapp.com/send", twitter: "https://twitter.com/share", }; private mkScoialUrl(socialName: any, zplAdr: any, text: any) { let res; const socialAdr = this.socialAdrs[socialName]; switch (socialName) { case "telegram": case "twitter": res = `${socialAdr}?url=${zplAdr}`; if (text) { res += `&text=${text}`; } break; default: // whatsapp res = `${socialAdr}?text=${zplAdr}`; if (text) { res += ` – ${text}`; } } return res; } get urlAdrTlg() { return this.mkScoialUrl("telegram", this.urlTlg, this.titlePost); } get urlAdrWApp() { return this.mkScoialUrl("whatsapp", this.urlWApp, this.titlePost); } private downloadSrc(blob, fileName) { const iurl = URL.createObjectURL(blob); const a = document.createElement(`a`); a.setAttribute("href", iurl); a.setAttribute("download", fileName); a.setAttribute("target", "_blank"); a.click(); URL.revokeObjectURL(iurl); } private fetchImgSrc(srcData, fileName, imgElm: HTMLElement | null = null, main = false) { const canvas = document.createElement("canvas") as HTMLCanvasElement; canvas.style.opacity = "0"; document.body.appendChild(canvas); const ctx = canvas.getContext("2d") as CanvasRenderingContext2D; let imgElement; if (!imgElm) { imgElement = new Image(); document.body.appendChild(imgElement); } else { imgElement = imgElm; imgElement.style.cssText = ""; } if (!main) { imgElement.style.opacity = "0"; } const drawCanvas = () => { const { height, width } = imgElement?.getBoundingClientRect(); canvas.width = width; canvas.height = height; ctx.drawImage(imgElement, 0, 0); canvas.toBlob((blob: any) => { this.downloadSrc(blob, fileName); document.body.removeChild(canvas); if (!main) { imgElement.parentNode.removeChild(imgElement); } }); }; if (!imgElm) { imgElement.onload = drawCanvas; imgElement.src = srcData; } else { drawCanvas(); } } download() { this.$emit('click:download', this.qrcode) } }