Skip to content

Commit

Permalink
Merge branch 'master' of github.com:avdeev/vanilla-sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
avdeev committed May 14, 2018
2 parents 615fc05 + f71176e commit 3321111
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ mail({
})
```

### `email(options)`

Share via user's email

```js
mail({
url: string,
title: string,
description: string
})

### `linkedin(options)`

Share on LinkedIn
Expand Down
10 changes: 10 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ <h1><a href="https://alexey-avdeev.com/vanilla-sharing/">vanilla-sharing</a> dem
<button id="vk" type="button">Share on VK</button>
<button id="ok" type="button">Share on Odnoklassniki</button>
<button id="mail" type="button">Share on Mail.ru</button>
<button id="email" type="button">Share via users's email</button>
<button id="linkedin" type="button">Share on Linkedin</button>

<button id="whatsapp" type="button">Share via Whatsapp</button>
Expand Down Expand Up @@ -130,6 +131,15 @@ <h1><a href="https://alexey-avdeev.com/vanilla-sharing/">vanilla-sharing</a> dem
});
});

document.getElementById('email').addEventListener('click', function() {
VanillaSharing.email({
url: META.URL,
title: META.TITLE,
description: META.DESCRIPTION,
image: META.IMAGE,
});
});

document.getElementById('linkedin').addEventListener('click', function() {
VanillaSharing.linkedin({
url: META.URL,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vanilla-sharing",
"description": "Small simple tool for sharing url, title, description and image to VK, Facebook, OK, G+, Twitter and Mail",
"description": "Small simple tool for sharing url, title, description and image to VK, Facebook, OK, G+, Twitter, email, and Mail",
"keywords": [
"sharing",
"vk",
Expand Down Expand Up @@ -35,11 +35,11 @@
"size-limit": [
{
"path": "dist/vanilla-sharing.umd.js",
"limit": "1.2 KB"
"limit": "1.3 KB"
},
{
"path": "dist/vanilla-sharing.esm.js",
"limit": "1.2 KB"
"limit": "1.3 KB"
}
],
"lint-staged": {
Expand Down
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fbShare from './sharers/fbShare';
import fbButton from './sharers/fbButton';
import gp from './sharers/gp';
import mail from './sharers/mail';
import email from './sharers/email';
import ok from './sharers/ok';
import telegram from './sharers/telegram';
import tw from './sharers/tw';
Expand All @@ -23,6 +24,7 @@ export {
fbButton,
gp,
mail,
email,
ok,
telegram,
tw,
Expand Down
9 changes: 9 additions & 0 deletions src/sharers/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function email(options = {}) {
const {
url, title, description,
} = options;

const body = `${title || ''}\r\n${description || ''}\r\n${url || ''}`;
const uri = `mailto:?subject=&body=${encodeURIComponent(body)}`;
return window.location.assign(uri);
}
37 changes: 37 additions & 0 deletions src/sharers/tests/email.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import faker from 'faker';

import email from '../email';

describe('email', () => {
beforeEach(() => {
window.open = jest.fn();
window.location.assign = jest.fn();
});

it('should call without params', () => {
email();
expect(window.location.assign).toBeCalledWith('mailto:?subject=&body=%0D%0A%0D%0A');
});

it('should call with url', () => {
const fixture = faker.internet.url();
email({ url: fixture });
expect(window.location.assign).toBeCalledWith(`mailto:?subject=&body=%0D%0A%0D%0A${encodeURIComponent(fixture)}`);
});

it('should call with title', () => {
const fixture = faker.lorem.sentence();

email({ title: fixture });

expect(window.location.assign).toBeCalledWith(`mailto:?subject=&body=${encodeURIComponent(fixture)}%0D%0A%0D%0A`);
});

it('should call with description', () => {
const fixture = faker.lorem.sentences();

email({ description: fixture });

expect(window.location.assign).toBeCalledWith(`mailto:?subject=&body=%0D%0A${encodeURIComponent(fixture)}%0D%0A`);
});
});

0 comments on commit 3321111

Please sign in to comment.