UNPKG

@nativescript-community/ui-share-file

Version:
143 lines 6.86 kB
import { Application, Utils } from '@nativescript/core'; // we need to store both the controller and the delegate. // UIDocumentInteractionController is not retained by iOS // the delegate is not retained by the controller const storedData = {}; export class ShareFile { constructor() { this.myId = Date.now(); } open(args) { return new Promise((resolve, reject) => { if (!args.path) { return reject(new Error('missing_arg_path')); } try { if (Array.isArray(args.path)) { // we only support one shared file for now args.path = args.path[0]; // const activityController = UIActivityViewController.alloc().initWithActivityItemsApplicationActivities(thingsToShare, null); // const presentViewController = activityController.popoverPresentationController; // if (presentViewController) { // const page = Frame.topmost().currentPage; // if (page && page.ios.navigationItem.rightBarButtonItems && page.ios.navigationItem.rightBarButtonItems.count > 0) { // presentViewController.barButtonItem = page.ios.navigationItem.rightBarButtonItems[0]; // } else { // presentViewController.sourceView = page.ios.view; // } // } // Utils.ios.getVisibleViewController(getRootViewController()).presentViewControllerAnimatedCompletion(activityController, true, null); } // else { const appPath = this.getCurrentAppPath(); const path = args.path.replace('~', appPath); const url = NSURL.fileURLWithPath(path); const animated = args.animated !== false; const controller = UIDocumentInteractionController.interactionControllerWithURL(url); controller.UTI = (args === null || args === void 0 ? void 0 : args.type) || 'public.data, public.content'; controller.name = args.title; // attempt to use the visible view controller to support modals and such, if not fall back to root let presentingController = Utils.ios.getVisibleViewController(Application.ios.rootController); if (!presentingController) presentingController = Application.ios.rootController; const delegate = UIDocumentInteractionControllerDelegateImpl.new().initWithOwnerController(this, presentingController); controller.delegate = delegate; let rect; if (args.rect) { rect = CGRectMake(args.rect.x ? args.rect.x : 0, args.rect.y ? args.rect.y : 0, args.rect.width ? args.rect.width : 0, args.rect.height ? args.rect.height : 0); } else { rect = CGRectMake(0, 0, 0, 0); } let result = false; if (!!args.options) { result = controller.presentOptionsMenuFromRectInViewAnimated(rect, presentingController.view, animated); } else { result = controller.presentOpenInMenuFromRectInViewAnimated(rect, presentingController.view, animated); } if (!result) { return reject(new Error('failed_opening')); } storedData[this.myId] = { controller, delegate }; this.resolve = resolve; // } } catch (e) { return reject(e); } }).finally(() => { delete storedData[this.myId]; }); } dismissed() { if (this.resolve) { this.resolve(); this.resolve = null; } } getCurrentAppPath() { const currentDir = __dirname; const tnsModulesIndex = currentDir.indexOf('/tns_modules'); // Module not hosted in ~/tns_modules when bundled. Use current dir. let appPath = currentDir; if (tnsModulesIndex !== -1) { // Strip part after tns_modules to obtain app root appPath = currentDir.substring(0, tnsModulesIndex); } return appPath; } } var UIDocumentInteractionControllerDelegateImpl = /** @class */ (function (_super) { __extends(UIDocumentInteractionControllerDelegateImpl, _super); function UIDocumentInteractionControllerDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } UIDocumentInteractionControllerDelegateImpl.new = function () { return _super.new.call(this); }; UIDocumentInteractionControllerDelegateImpl.prototype.initWithOwnerController = function (owner, controller) { this._owner = owner; this.controller = controller; return this; }; UIDocumentInteractionControllerDelegateImpl.prototype.documentInteractionControllerWillBeginSendingToApplication = function (controller, app) { // console.log( // "documentInteractionControllerWillBeginSendingToApplication", // app // ); var owner = this._owner; if (owner) { owner.dismissed(); } }; UIDocumentInteractionControllerDelegateImpl.prototype.documentInteractionControllerDidDismissOpenInMenu = function (controller) { // console.log("documentInteractionControllerDidDismissOpenInMenu"); var owner = this._owner; if (owner) { owner.dismissed(); } }; UIDocumentInteractionControllerDelegateImpl.prototype.documentInteractionControllerDidDismissOptionsMenu = function (controller) { // console.log("documentInteractionControllerDidDismissOptionsMenu"); var owner = this._owner; if (owner) { owner.dismissed(); } }; UIDocumentInteractionControllerDelegateImpl.prototype.documentInteractionControllerViewControllerForPreview = function (controller) { return this.controller; }; UIDocumentInteractionControllerDelegateImpl.prototype.documentInteractionControllerViewForPreview = function (controller) { return this.controller.view; }; UIDocumentInteractionControllerDelegateImpl.prototype.documentInteractionControllerRectForPreview = function (controller) { return this.controller.view.bounds; }; UIDocumentInteractionControllerDelegateImpl.ObjCProtocols = [UIDocumentInteractionControllerDelegate]; return UIDocumentInteractionControllerDelegateImpl; }(NSObject)); //# sourceMappingURL=share-file.ios.js.map