Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 77 additions & 14 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@ Object.defineProperty(exports, "__esModule", {
});
exports.buildShareUrl = exports.escapeICSDescription = exports.isInternetExplorer = exports.isMobile = exports.formatDuration = exports.formatDate = void 0;

var _momentTimezone = _interopRequireDefault(require("moment-timezone"));

var _enums = require("./enums");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

/**
* Converts Date String with UTC timezone to date consumable by calendar
* apps. Changes +00:00 to Z.
Expand Down Expand Up @@ -124,11 +136,62 @@ var buildShareFile = function buildShareFile(_ref3) {
timezone = _ref3$timezone === void 0 ? '' : _ref3$timezone,
_ref3$title = _ref3.title,
title = _ref3$title === void 0 ? '' : _ref3$title;
var content = ['BEGIN:VCALENDAR', 'VERSION:2.0', 'BEGIN:VEVENT', "URL:".concat(document.URL), 'METHOD:PUBLISH', // TODO: Will need to parse the date without Z for ics
var VTIMEZONEEntries = getVtimezoneFromMomentZone({
timezone: timezone,
startDatetime: startDatetime,
endDatetime: endDatetime
});
var content = ['BEGIN:VCALENDAR', 'VERSION:2.0'].concat(_toConsumableArray(VTIMEZONEEntries), ['BEGIN:VEVENT', "URL:".concat(document.URL), 'METHOD:PUBLISH', // TODO: Will need to parse the date without Z for ics
// This means I'll probably have to require a date lib - luxon most likely or datefns
timezone === '' ? "DTSTART:".concat(startDatetime) : "DTSTART;TZID=".concat(timezone, ":").concat(startDatetime), timezone === '' ? "DTEND:".concat(endDatetime) : "DTEND;TZID=".concat(timezone, ":").concat(endDatetime), "SUMMARY:".concat(title), "DESCRIPTION:".concat(escapeICSDescription(description)), "LOCATION:".concat(location), 'END:VEVENT', 'END:VCALENDAR'].join('\n');
timezone === '' ? "DTSTART:".concat(startDatetime) : "DTSTART;TZID=".concat(timezone, ":").concat(startDatetime), timezone === '' ? "DTEND:".concat(endDatetime) : "DTEND;TZID=".concat(timezone, ":").concat(endDatetime), "SUMMARY:".concat(title), "DESCRIPTION:".concat(escapeICSDescription(description)), "LOCATION:".concat(location), 'END:VEVENT', 'END:VCALENDAR']).join('\n');
return isMobile() ? encodeURI("data:text/calendar;charset=utf8,".concat(content)) : content;
};
/**
* Takes a timezone, start and end date and returns VTIMEZONE entries
* corresponding to DST changes during and around the event. N.B. This
* is suboptimal for long-spanning or recurring events. For those cases
* recurrence rules should be applied.
* @param {string} event.timezone
* @param {string} event.startDatetime
* @param {string} event.endDatetime
*/


var getVtimezoneFromMomentZone = function getVtimezoneFromMomentZone(_ref4) {
var _ref4$timezone = _ref4.timezone,
timezone = _ref4$timezone === void 0 ? "" : _ref4$timezone,
startDatetime = _ref4.startDatetime,
endDatetime = _ref4.endDatetime;
if (timezone === "") return [];

var zone = _momentTimezone.default.tz.zone(timezone);

var header = "BEGIN:VTIMEZONE\nTZID:".concat(timezone);
var footer = "END:VTIMEZONE"; // Find the timestamp for DST changes immediately before
// and after the event.

var prevDSTSwitch = zone.untils.findIndex(function (u) {
return u > (0, _momentTimezone.default)(startDatetime).unix() * 1000;
}) - 1;
var lastDSTSwitch = zone.untils.findIndex(function (u) {
return u > (0, _momentTimezone.default)(endDatetime).unix() * 1000;
});
var zTZitems = []; // Generate VTIMEZONE entries
// FIXME: Handle case when lastDSTSwitch does not exist, or is the last entry

for (var i = prevDSTSwitch; i < lastDSTSwitch + 1; i++) {
// Determine which mode is ENDING. Even entries are DST, odd entries are standard.
var type = i % 2 ? "STANDARD" : "DAYLIGHT";

var momDtStart = _momentTimezone.default.tz(zone.untils[i - 1], timezone);

var momNext = _momentTimezone.default.tz(zone.untils[i], timezone);

zTZitems.push("BEGIN:".concat(type, "\nDTSTART:").concat(momDtStart.format("YYYYMMDDTHHmmss"), "\nTZOFFSETFROM:").concat(momDtStart.format("ZZ"), "\nTZOFFSETTO:").concat(momNext.format("ZZ"), "\nTZNAME:").concat(zone.abbrs[i], "\nEND:").concat(type));
}

return [header].concat(zTZitems, [footer]);
};
/**
* Takes an event object and a type of URL and returns either a calendar event
* URL or the contents of an ics file.
Expand All @@ -142,18 +205,18 @@ var buildShareFile = function buildShareFile(_ref3) {
*/


var buildShareUrl = function buildShareUrl(_ref4, type) {
var _ref4$description = _ref4.description,
description = _ref4$description === void 0 ? '' : _ref4$description,
duration = _ref4.duration,
endDatetime = _ref4.endDatetime,
_ref4$location = _ref4.location,
location = _ref4$location === void 0 ? '' : _ref4$location,
startDatetime = _ref4.startDatetime,
_ref4$timezone = _ref4.timezone,
timezone = _ref4$timezone === void 0 ? '' : _ref4$timezone,
_ref4$title = _ref4.title,
title = _ref4$title === void 0 ? '' : _ref4$title;
var buildShareUrl = function buildShareUrl(_ref5, type) {
var _ref5$description = _ref5.description,
description = _ref5$description === void 0 ? '' : _ref5$description,
duration = _ref5.duration,
endDatetime = _ref5.endDatetime,
_ref5$location = _ref5.location,
location = _ref5$location === void 0 ? '' : _ref5$location,
startDatetime = _ref5.startDatetime,
_ref5$timezone = _ref5.timezone,
timezone = _ref5$timezone === void 0 ? '' : _ref5$timezone,
_ref5$title = _ref5.title,
title = _ref5$title === void 0 ? '' : _ref5$title;
var encodeURI = type !== _enums.SHARE_SITES.ICAL && type !== _enums.SHARE_SITES.OUTLOOK;
var data = {
description: encodeURI ? encodeURIComponent(description) : description,
Expand Down
56 changes: 38 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-add-to-calendar-hoc",
"version": "1.0.10",
"version": "1.0.12",
"description": "Simple Unopinionated React Add to Calendar Button. Bring your own components.",
"main": "lib/index.js",
"scripts": {
Expand All @@ -19,6 +19,7 @@
},
"dependencies": {
"core-js": "^2.5.7",
"moment-timezone": "^0.5.21",
"prop-types": "^15.6.2"
},
"devDependencies": {
Expand All @@ -40,7 +41,6 @@
"html-webpack-plugin": "^3.2.0",
"jest": "^23.1.0",
"luxon": "^1.4.4",
"moment-timezone": "^0.5.21",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-highlight": "^0.12.0",
Expand All @@ -53,9 +53,5 @@
"webpack-dev-server": "^3.1.3"
},
"author": "Jason Leibowitz <[email protected]>",
"homepage": "http://leibowitz.me/react-add-to-calendar-hoc/",
"repository": {
"type": "git",
"url": "[email protected]:jasonleibowitz/react-add-to-calendar-hoc.git"
}
"homepage": "http://leibowitz.me/react-add-to-calendar-hoc/"
}
Loading