mirror of
https://github.com/encounter/react-add-to-calendar.git
synced 2026-07-10 21:18:44 -07:00
adjusting the way the data URIs are handled since most browsers are not able to download the link without “save as” command. this resolves #8
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
node_modules
|
||||||
|
.sass-cache
|
||||||
|
npm-debug.log
|
||||||
|
.idea
|
||||||
|
dist
|
||||||
|
lib
|
||||||
|
tmp
|
||||||
|
coverage
|
||||||
|
0.md
|
||||||
+4730
-3055
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
"author": "Jason Salzman",
|
"author": "Jason Salzman",
|
||||||
"name": "react-add-to-calendar",
|
"name": "react-add-to-calendar",
|
||||||
"description": "A simple and reusable add to calendar button component for React",
|
"description": "A simple and reusable add to calendar button component for React",
|
||||||
"version": "0.0.9",
|
"version": "0.0.11",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"homepage": "https://github.com/jasonsalzman/react-add-to-calendar",
|
"homepage": "https://github.com/jasonsalzman/react-add-to-calendar",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
+28
-12
@@ -39,19 +39,35 @@ export default class ReactAddToCalendar extends React.Component {
|
|||||||
this.setState({optionsOpen: showOptions});
|
this.setState({optionsOpen: showOptions});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleDropdownLinkClick(e) {
|
handleDropdownLinkClick(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
let url = e.currentTarget.getAttribute('href');
|
let url = e.currentTarget.getAttribute("href");
|
||||||
|
|
||||||
if (this.state.isCrappyIE) {
|
if (url.startsWith("data") || url.startsWith("BEGIN")) {
|
||||||
let blob = new Blob([url], {type: 'text/calendar'});
|
let filename = "download.ics";
|
||||||
window.navigator.msSaveOrOpenBlob(blob, 'download.ics');
|
let blob = new Blob([url], { type: "text/calendar;charset=utf-8" });
|
||||||
} else {
|
|
||||||
window.open(url, '_blank');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.toggleCalendarDropdown();
|
if (this.state.isCrappyIE) {
|
||||||
}
|
window.navigator.msSaveOrOpenBlob(blob, filename);
|
||||||
|
} else {
|
||||||
|
/****************************************************************
|
||||||
|
// many browsers do not properly support downloading data URIs
|
||||||
|
// (even with "download" attribute in use) so this solution
|
||||||
|
// ensures the event will download cross-browser
|
||||||
|
****************************************************************/
|
||||||
|
let link = document.createElement("a");
|
||||||
|
link.href = window.URL.createObjectURL(blob);
|
||||||
|
link.setAttribute("download", filename);
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
window.open(url, "_blank");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.toggleCalendarDropdown();
|
||||||
|
}
|
||||||
|
|
||||||
renderDropdown() {
|
renderDropdown() {
|
||||||
let self = this;
|
let self = this;
|
||||||
@@ -69,7 +85,7 @@ export default class ReactAddToCalendar extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<li key={helpers.getRandomKey()}>
|
<li key={helpers.getRandomKey()}>
|
||||||
<a className={currentItem + '-link'} onClick={self.handleDropdownLinkClick}
|
<a className={currentItem + '-link'} onClick={self.handleDropdownLinkClick}
|
||||||
href={helpers.buildUrl(self.props.event, currentItem, self.state.isCrappyIE)}
|
href={helpers.buildUrl(self.props.event, currentItem)}
|
||||||
target="_blank">
|
target="_blank">
|
||||||
{icon}
|
{icon}
|
||||||
{currentLabel}</a>
|
{currentLabel}</a>
|
||||||
|
|||||||
+3
-10
@@ -25,14 +25,11 @@ export default class helpers {
|
|||||||
return Math.floor(duration.asHours()) + moment.utc(difference).format(':mm');
|
return Math.floor(duration.asHours()) + moment.utc(difference).format(':mm');
|
||||||
}
|
}
|
||||||
|
|
||||||
buildUrl (event, type, isCrappyIE) {
|
buildUrl (event, type) {
|
||||||
let calendarUrl = '';
|
let calendarUrl = '';
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'google':
|
case 'google':
|
||||||
// This is all I changed, there were 2 problems,
|
|
||||||
// first, the root URL of Google calendar changed, and second,
|
|
||||||
// somehow the order of the params creates a bad request so I fixed that too
|
|
||||||
calendarUrl = 'https://calendar.google.com/calendar/render';
|
calendarUrl = 'https://calendar.google.com/calendar/render';
|
||||||
calendarUrl += '?action=TEMPLATE';
|
calendarUrl += '?action=TEMPLATE';
|
||||||
calendarUrl += '&dates=' + this.formatTime(event.startTime);
|
calendarUrl += '&dates=' + this.formatTime(event.startTime);
|
||||||
@@ -64,7 +61,7 @@ export default class helpers {
|
|||||||
calendarUrl += "&uid=" + this.getRandomKey();
|
calendarUrl += "&uid=" + this.getRandomKey();
|
||||||
calendarUrl += "&path=/calendar/view/Month";
|
calendarUrl += "&path=/calendar/view/Month";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
calendarUrl = [
|
calendarUrl = [
|
||||||
'BEGIN:VCALENDAR',
|
'BEGIN:VCALENDAR',
|
||||||
@@ -79,12 +76,8 @@ export default class helpers {
|
|||||||
'END:VEVENT',
|
'END:VEVENT',
|
||||||
'END:VCALENDAR'
|
'END:VCALENDAR'
|
||||||
].join('\n');
|
].join('\n');
|
||||||
|
|
||||||
if (!isCrappyIE) {
|
|
||||||
calendarUrl = encodeURI('data:text/calendar;charset=utf8,' + calendarUrl);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return calendarUrl;
|
return calendarUrl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user