UNPKG

@foreverrbum/ethsign

Version:

This package will allow you to electronically sign documents within your application

191 lines (159 loc) 10.6 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _webviewer = _interopRequireDefault(require("@pdftron/webviewer")); var _download = require("../../helpers/download"); var _reactIntl = require("react-intl"); var _pdf = require("../../helpers/pdf"); var _dashboard = require("../../helpers/dashboard"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } var DownloadPDF = function DownloadPDF(props) { var data = props.data, handleShowWebviewer = props.handleShowWebviewer, handleShowDownloadPopup = props.handleShowDownloadPopup; var doc = data.doc, password = data.password; var viewer = (0, _react.useRef)(null); var _useIntl = (0, _reactIntl.useIntl)(), formatMessage = _useIntl.formatMessage; (0, _react.useEffect)(function () { _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() { return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: (0, _webviewer.default)({ licenseKey: 'Buildblock Tech Pte. Ltd.:OEM:EthSign::B+:AMS(20220926):60A5E4AD0457F80A7360B13AC982537860615F858748CDEA9BF51DE6240C48AE4AB4B6F5C7', path: '/webviewer/lib', fullAPI: true, disabledElements: ['header', 'toolsHeader'] }, viewer.current).then( /*#__PURE__*/function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(instance) { var docViewer, annotManager, Annotations, fileArr, blob; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: docViewer = instance.docViewer, annotManager = instance.annotManager, Annotations = instance.Annotations; _context3.next = 3; return (0, _download.getFileArray)(password, doc.ipfsHash, doc.storageProvider, false, formatMessage); case 3: fileArr = _context3.sent; if (fileArr == false) { handleShowWebviewer(null); document.getElementById('password_input').value = ''; } else { (0, _dashboard.storeNotif)(formatMessage({ id: 'DOWNLOADING' }, { name: doc.name }), formatMessage({ id: 'DOWNLOAD_BEGIN' }), 'success'); blob = new Blob([fileArr], { type: 'application/pdf' }); instance.loadDocument(blob, { filename: doc.name }); // not show signatures in notesPanel // instance.setCustomNoteFilter(annot => (!(annot instanceof instance.Annotations.FreeHandAnnotation) )) docViewer.on('documentLoaded', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { var commentArrs, pdf, xfdfString, options, data, arr, blob; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return (0, _download.getCommentArrs)(doc.commentData, password, formatMessage); case 2: commentArrs = _context2.sent; _context2.next = 5; return Promise.all(commentArrs.map( /*#__PURE__*/function () { var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(commentArr, id) { var xfdf; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return (0, _pdf.getStringFromDecryptedArr)(commentArr, formatMessage); case 2: xfdf = _context.sent; _context.next = 5; return annotManager.importAnnotations(xfdf); case 5: return _context.abrupt("return", Promise.resolve('success')); case 6: case "end": return _context.stop(); } } }, _callee); })); return function (_x2, _x3) { return _ref4.apply(this, arguments); }; }())); case 5: pdf = docViewer.getDocument(); _context2.next = 8; return annotManager.exportAnnotations(); case 8: xfdfString = _context2.sent; options = { xfdfString: xfdfString, flatten: true }; _context2.next = 12; return pdf.getFileData(options); case 12: data = _context2.sent; arr = new Uint8Array(data); blob = new Blob([arr], { type: 'application/pdf' }); (0, _download.downloadBlob)(blob, doc.name); handleShowWebviewer(null); handleShowDownloadPopup(false); case 18: case "end": return _context2.stop(); } } }, _callee2); }))); } case 5: case "end": return _context3.stop(); } } }, _callee3); })); return function (_x) { return _ref2.apply(this, arguments); }; }()); case 1: case "end": return _context4.stop(); } } }, _callee4); }))(); }, []); return /*#__PURE__*/_react.default.createElement("div", { ref: viewer, className: "absolute invisible" }); }; var _default = DownloadPDF; exports.default = _default;