ng-navigator-share
Version:
Lightweight Angular wrapper on Web Share API to share PWA apps, Text, URL.
84 lines (79 loc) • 3.67 kB
JavaScript
import { __awaiter, __generator, __decorate, __metadata } from 'tslib';
import { ɵɵdefineInjectable, Injectable } from '@angular/core';
var NgNavigatorShareService = /** @class */ (function () {
function NgNavigatorShareService() {
this.webNavigator = null;
this.webNavigator = window.navigator;
}
NgNavigatorShareService.prototype.canShare = function () {
return this.webNavigator !== null && this.webNavigator.share !== undefined;
};
NgNavigatorShareService.prototype.canShareFile = function (file) {
return this.webNavigator !== null && this.webNavigator.share !== undefined && this.webNavigator.canShare({ files: file });
};
NgNavigatorShareService.prototype.share = function (_a) {
var _this = this;
var title = _a.title, text = _a.text, url = _a.url, files = _a.files;
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var shareObject, isShared, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this.webNavigator !== null && this.webNavigator.share !== undefined)) return [3 /*break*/, 5];
if (!((text === undefined || text === null) &&
(url === undefined || url === null))) return [3 /*break*/, 1];
console.warn("text and url both can't be empty, at least provide either text or url");
return [3 /*break*/, 4];
case 1:
_a.trys.push([1, 3, , 4]);
shareObject = {
title: title,
text: text,
url: url
};
if (files && files.length !== 0) {
shareObject.files = files;
}
return [4 /*yield*/, this.webNavigator.share(shareObject)];
case 2:
isShared = _a.sent();
resolve({
shared: true
});
return [3 /*break*/, 4];
case 3:
error_1 = _a.sent();
reject({
shared: false,
error: error_1
});
return [3 /*break*/, 4];
case 4: return [3 /*break*/, 6];
case 5:
reject({
shared: false,
error: "This service/api is not supported in your Browser"
});
_a.label = 6;
case 6: return [2 /*return*/];
}
});
}); });
};
NgNavigatorShareService.ɵprov = ɵɵdefineInjectable({ factory: function NgNavigatorShareService_Factory() { return new NgNavigatorShareService(); }, token: NgNavigatorShareService, providedIn: "root" });
NgNavigatorShareService = __decorate([
Injectable({
providedIn: 'root'
}),
__metadata("design:paramtypes", [])
], NgNavigatorShareService);
return NgNavigatorShareService;
}());
/*
* Public API Surface of ng-navigator-share
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgNavigatorShareService };
//# sourceMappingURL=ng-navigator-share.js.map