2017-12-07 03:12:59 +08:00
|
|
|
import crypto from 'crypto';
|
|
|
|
import URL from 'url';
|
|
|
|
import ReactDOMServer from 'react-dom/server';
|
|
|
|
import Templates from './templates';
|
|
|
|
|
|
|
|
export const RAW_TEMPLATE_NAME = 'raw';
|
|
|
|
|
|
|
|
export const DataShape = [
|
|
|
|
{
|
|
|
|
key: 'name',
|
|
|
|
label: 'Name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'title',
|
|
|
|
label: 'Title',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'phone',
|
|
|
|
label: 'Phone',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'email',
|
|
|
|
label: 'Email Address',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'fax',
|
|
|
|
label: 'Fax',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'address',
|
|
|
|
label: 'Address',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'websiteURL',
|
|
|
|
label: 'Website URL',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'facebookURL',
|
|
|
|
label: 'Facebook URL',
|
|
|
|
},
|
|
|
|
{
|
2017-12-31 01:55:09 +08:00
|
|
|
key: 'twitterHandle',
|
|
|
|
label: 'Twitter Handle',
|
2017-12-07 03:12:59 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'tintColor',
|
|
|
|
label: 'Theme Color',
|
|
|
|
placeholder: 'ex: #419bf9, purple',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
export const ResolveSignatureData = data => {
|
|
|
|
data = Object.assign({}, data);
|
|
|
|
|
2017-12-31 01:55:09 +08:00
|
|
|
['websiteURL', 'facebookURL'].forEach(key => {
|
2017-12-07 03:12:59 +08:00
|
|
|
if (data[key] && !data[key].includes(':')) {
|
|
|
|
data[key] = `http://${data[key]}`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-31 01:55:09 +08:00
|
|
|
// sanitize twitter handle
|
|
|
|
if (data.twitterHandle) {
|
|
|
|
if (data.twitterHandle.includes('/')) {
|
|
|
|
// a url was likely entered, lets grab the user (last portion).
|
|
|
|
const split = data.twitterHandle.split('/');
|
|
|
|
data.twitterHandle = split[split.length - 1];
|
|
|
|
}
|
|
|
|
if (data.twitterHandle[0] === '@') {
|
|
|
|
// an at symbol was added, lets remove it.
|
|
|
|
data.twitterHandle = data.twitterHandle.slice(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 03:12:59 +08:00
|
|
|
if (data.photoURL === 'gravatar') {
|
|
|
|
const hash = crypto
|
|
|
|
.createHash('md5')
|
|
|
|
.update((data.email || '').toLowerCase().trim())
|
|
|
|
.digest('hex');
|
|
|
|
data.photoURL = `https://www.gravatar.com/avatar/${hash}`;
|
|
|
|
}
|
|
|
|
|
2017-12-31 01:55:09 +08:00
|
|
|
if (data.photoURL === 'twitter') {
|
|
|
|
data.photoURL = `https://twitter.com/${data.twitterHandle}/profile_image?size=original`;
|
|
|
|
}
|
|
|
|
|
2017-12-07 03:12:59 +08:00
|
|
|
if (data.photoURL === 'company') {
|
|
|
|
const domain =
|
|
|
|
(data.websiteURL && URL.parse(data.websiteURL).hostname) ||
|
|
|
|
(data.email && data.email.split('@').pop());
|
|
|
|
data.photoURL = `https://logo.clearbit.com/${domain}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.photoURL === 'custom') {
|
|
|
|
data.photoURL = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function RenderSignatureData(data) {
|
|
|
|
const template = Templates.find(t => t.name === data.templateName) || Templates[0];
|
|
|
|
return ReactDOMServer.renderToStaticMarkup(template(ResolveSignatureData(data)));
|
|
|
|
}
|