2018-05-13 00:42:59 +02:00
|
|
|
import React, { PureComponent } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import FileSaver from 'file-saver'
|
|
|
|
import vCard from '../../lib/vcf/vcard'
|
|
|
|
|
|
|
|
class Vcard extends PureComponent {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
}
|
|
|
|
|
2018-05-14 01:50:11 +02:00
|
|
|
// Helper function to create base64 string from avatar image
|
|
|
|
// without the need to read image file from file system
|
2018-05-13 00:42:59 +02:00
|
|
|
toDataURL(src, callback, outputFormat) {
|
|
|
|
const img = new Image()
|
|
|
|
img.crossOrigin = 'Anonymous'
|
|
|
|
|
|
|
|
img.onload = function() {
|
2018-05-14 01:50:11 +02:00
|
|
|
// yeah, we're gonna create a fake canvas to render the image
|
|
|
|
// and then create a base64 string from the rendered result
|
2018-05-13 00:42:59 +02:00
|
|
|
const canvas = document.createElement('canvas')
|
|
|
|
const ctx = canvas.getContext('2d')
|
|
|
|
let dataURL
|
|
|
|
|
|
|
|
canvas.height = this.naturalHeight
|
|
|
|
canvas.width = this.naturalWidth
|
|
|
|
ctx.drawImage(this, 0, 0)
|
|
|
|
dataURL = canvas.toDataURL(outputFormat)
|
|
|
|
callback(dataURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
img.src = src
|
|
|
|
if (img.complete || img.complete === undefined) {
|
|
|
|
img.src =
|
|
|
|
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=='
|
|
|
|
img.src = src
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructVcard() {
|
|
|
|
const meta = this.props.meta
|
|
|
|
const contact = new vCard()
|
|
|
|
const photoSrc = meta.avatar.childImageSharp.original.src
|
|
|
|
|
2018-05-14 01:50:11 +02:00
|
|
|
// first, convert the avatar to base64,
|
|
|
|
// then construct all vCard elements
|
2018-05-13 00:42:59 +02:00
|
|
|
this.toDataURL(
|
|
|
|
photoSrc,
|
|
|
|
dataUrl => {
|
2018-05-14 01:50:11 +02:00
|
|
|
// stripping this data out of base64 string is required
|
|
|
|
// for vcard to actually display the image for whatever reason
|
2018-05-13 00:42:59 +02:00
|
|
|
const dataUrlCleaned = dataUrl.split('data:image/jpeg;base64,').join('')
|
|
|
|
contact.set('photo', dataUrlCleaned, { encoding: 'b', type: 'JPEG' })
|
|
|
|
contact.set('fn', meta.title)
|
|
|
|
contact.set('title', meta.tagline)
|
|
|
|
contact.set('email', meta.email)
|
|
|
|
contact.set('url', meta.url, { type: 'Portfolio' })
|
|
|
|
contact.add('url', meta.social.Blog, { type: 'Blog' })
|
|
|
|
contact.set('nickname', 'kremalicious')
|
|
|
|
contact.add('x-socialprofile', meta.social.Twitter, { type: 'twitter' })
|
|
|
|
contact.add('x-socialprofile', meta.social.GitHub, { type: 'GitHub' })
|
|
|
|
|
|
|
|
const vcard = contact.toString('3.0')
|
|
|
|
|
|
|
|
this.downloadVcard(vcard)
|
|
|
|
},
|
|
|
|
'image/jpeg'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-05-14 01:50:11 +02:00
|
|
|
// Construct the download from a blob of the just constructed vCard,
|
|
|
|
// and save it to user's file system
|
2018-05-13 00:42:59 +02:00
|
|
|
downloadVcard(vcard) {
|
|
|
|
const name = this.props.meta.addressbook.split('/').join('')
|
|
|
|
const blob = new Blob([vcard], { type: 'text/x-vcard' })
|
|
|
|
FileSaver.saveAs(blob, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
handleAddressbookClick = e => {
|
|
|
|
e.preventDefault()
|
|
|
|
this.constructVcard()
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<a
|
2018-05-14 01:50:11 +02:00
|
|
|
// href is kinda fake, only there for usability
|
|
|
|
// so user knows what to expect when hovering the link before clicking
|
2018-05-13 00:42:59 +02:00
|
|
|
href={this.props.meta.addressbook}
|
|
|
|
onClick={this.handleAddressbookClick}
|
|
|
|
>
|
|
|
|
Add to addressbook
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vcard.propTypes = {
|
|
|
|
meta: PropTypes.object,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Vcard
|