diff --git a/build/Document.js b/build/Document.js index 9a635e114..212082d18 100644 --- a/build/Document.js +++ b/build/Document.js @@ -55,10 +55,10 @@ var Document = function (_Component) { _this.setState({ pdf: null }); if (!source) { - return; + return null; } - PDFJS.getDocument(source).then(_this.onLoadSuccess).catch(_this.onLoadError); + return PDFJS.getDocument(source).then(_this.onLoadSuccess).catch(_this.onLoadError); }, _this.onSourceError = function (error) { (0, _util.callIfDefined)(_this.props.onSourceError, error); @@ -89,9 +89,7 @@ var Document = function (_Component) { // File is a string if ((0, _util.isString)(file)) { - if (_util.isLocalFileSystem) { - // @TODO: Display CORS warning - } + (0, _util.displayCORSWarning)(); return resolve(file); } @@ -110,8 +108,8 @@ var Document = function (_Component) { var _fileBlobURL = (0, _util.dataURItoURL)(modifiedFile.url); modifiedFile.url = _fileBlobURL; - } else if (_util.isLocalFileSystem) { - // @TODO: Display CORS warning + } else { + (0, _util.displayCORSWarning)(); } } @@ -205,7 +203,7 @@ var Document = function (_Component) { value: function loadDocument() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props; - this.findDocumentSource(props).then(this.onSourceSuccess).catch(this.onSourceError); + return this.findDocumentSource(props).then(this.onSourceSuccess).catch(this.onSourceError); } /** @@ -310,8 +308,8 @@ Document.propTypes = { })]), loading: _propTypes2.default.node, noData: _propTypes2.default.node, - onLoadSuccess: _propTypes2.default.func, onLoadError: _propTypes2.default.func, + onLoadSuccess: _propTypes2.default.func, onSourceError: _propTypes2.default.func, onSourceSuccess: _propTypes2.default.func, rotate: _propTypes2.default.number diff --git a/build/Outline.js b/build/Outline.js index 3231a05cf..fc952586f 100644 --- a/build/Outline.js +++ b/build/Outline.js @@ -28,6 +28,12 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function" function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +// eslint-disable-next-line no-underscore-dangle +if (!window._babelPolyfill) { + // eslint-disable-next-line global-require + require('babel-polyfill'); +} + var Ref = function () { function Ref(_ref) { var num = _ref.num, @@ -72,7 +78,7 @@ var Outline = function (_Component) { }, _this.onLoadSuccess = function (outline) { (0, _util.callIfDefined)(_this.props.onLoadSuccess); - _this.parseOutline(outline).then(_this.onParseSuccess).catch(_this.onParseError); + return _this.parseOutline(outline).then(_this.onParseSuccess).catch(_this.onParseError); }, _this.onLoadError = function (error) { (0, _util.callIfDefined)(_this.props.onLoadError, error); @@ -350,7 +356,7 @@ var Outline = function (_Component) { this.setState({ outline: null }); } - pdf.getOutline().then(this.onLoadSuccess).catch(this.onLoadError); + return pdf.getOutline().then(this.onLoadSuccess).catch(this.onLoadError); } }, { key: 'renderOutline', diff --git a/build/Page.js b/build/Page.js index bf08e51a0..1e7215315 100644 --- a/build/Page.js +++ b/build/Page.js @@ -112,7 +112,6 @@ var Page = function (_Component) { } if ((0, _util.isProvided)(props.pageNumber)) { - // @TODO: Page number isn't always the same return props.pageNumber - 1; } @@ -128,7 +127,6 @@ var Page = function (_Component) { } if ((0, _util.isProvided)(props.pageIndex)) { - // @TODO: Page index isn't always the same return props.pageIndex + 1; } @@ -150,7 +148,7 @@ var Page = function (_Component) { this.setState({ page: null }); } - pdf.getPage(pageNumber).then(this.onLoadSuccess).catch(this.onLoadError); + return pdf.getPage(pageNumber).then(this.onLoadSuccess).catch(this.onLoadError); } }, { key: 'render', @@ -205,7 +203,6 @@ var Page = function (_Component) { }, { key: 'pageNumber', get: function get() { - // @TODO: Page numer isn't always the same return this.getPageNumber(); } }, { @@ -263,7 +260,6 @@ Page.propTypes = { onLoadSuccess: _propTypes2.default.func, onRenderError: _propTypes2.default.func, onRenderSuccess: _propTypes2.default.func, - // @TODO: Check if > 0, < pdf.numPages pageIndex: _propTypes2.default.number, // eslint-disable-line react/no-unused-prop-types pageNumber: _propTypes2.default.number, // eslint-disable-line react/no-unused-prop-types pdf: _propTypes2.default.shape({ diff --git a/build/PageCanvas.js b/build/PageCanvas.js index f15404546..c8b410d47 100644 --- a/build/PageCanvas.js +++ b/build/PageCanvas.js @@ -46,7 +46,7 @@ var PageCanvas = function (_Component) { (0, _util.callIfDefined)(_this.props.onRenderError, error); }, _this.drawPageOnCanvas = function (canvas) { if (!canvas) { - return; + return null; } var page = _this.props.page; @@ -77,7 +77,7 @@ var PageCanvas = function (_Component) { _this.renderer = page.render(renderContext); - _this.renderer.then(_this.onRenderSuccess).catch(function (dismiss) { + return _this.renderer.then(_this.onRenderSuccess).catch(function (dismiss) { if (dismiss === 'cancelled') { // Everything's alright return; diff --git a/build/PageTextContent.js b/build/PageTextContent.js index 59428c023..f59b33e9a 100644 --- a/build/PageTextContent.js +++ b/build/PageTextContent.js @@ -28,6 +28,12 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } +// eslint-disable-next-line no-underscore-dangle +if (!window._babelPolyfill) { + // eslint-disable-next-line global-require + require('babel-polyfill'); +} + // Render disproportion above which font will be considered broken and fallback will be used var BROKEN_FONT_ALARM_THRESHOLD = 0.1; @@ -126,7 +132,7 @@ var PageTextContent = function (_Component) { this.setState({ textItems: null }); } - page.getTextContent().then(this.onGetTextSuccess).catch(this.onGetTextError); + return page.getTextContent().then(this.onGetTextSuccess).catch(this.onGetTextError); } }, { key: 'getFontData', diff --git a/build/entry.js b/build/entry.js index 75bcfdf0c..3e8c57b1a 100644 --- a/build/entry.js +++ b/build/entry.js @@ -17,11 +17,20 @@ var _Page = require('./Page'); var _Page2 = _interopRequireDefault(_Page); +var _util = require('./shared/util'); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -require('pdfjs-dist'); +if (_util.isLocalFileSystem) { + // eslint-disable-next-line no-console + (0, _util.warnOnDev)('You are running React-PDF from your local file system. PDF.js Worker may fail to load due to browser\'s security policies. If you\'re on Google Chrome, you can use --allow-file-access-from-files flag for debugging purposes.'); +} + +var pdfjs = require('pdfjs-dist'); require('pdfjs-dist/web/compatibility'); +pdfjs.PDFJS.workerSrc = 'pdf.worker.js'; + exports.Document = _Document2.default; exports.Outline = _Outline2.default; exports.Page = _Page2.default; \ No newline at end of file diff --git a/build/entry.noworker.js b/build/entry.noworker.js new file mode 100644 index 000000000..cb78ced57 --- /dev/null +++ b/build/entry.noworker.js @@ -0,0 +1,29 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Page = exports.Outline = exports.Document = undefined; + +var _Document = require('./Document'); + +var _Document2 = _interopRequireDefault(_Document); + +var _Outline = require('./Outline'); + +var _Outline2 = _interopRequireDefault(_Outline); + +var _Page = require('./Page'); + +var _Page2 = _interopRequireDefault(_Page); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var pdfjs = require('pdfjs-dist/build/pdf.combined'); +require('pdfjs-dist/web/compatibility'); + +pdfjs.PDFJS.disableWorker = true; + +exports.Document = _Document2.default; +exports.Outline = _Outline2.default; +exports.Page = _Page2.default; \ No newline at end of file diff --git a/build/entry.webpack.js b/build/entry.webpack.js index d3e2d19cd..b9e6e711a 100644 --- a/build/entry.webpack.js +++ b/build/entry.webpack.js @@ -21,9 +21,9 @@ var _util = require('./shared/util'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -if ((0, _util.isLocalFileSystem)()) { +if (_util.isLocalFileSystem) { // eslint-disable-next-line no-console - console.warn('You are running React-PDF from your local file system. PDF.js Worker may fail to load due to browser\'s security policies. If you\'re on Google Chrome, you can use --allow-file-access-from-files flag for debugging purposes.'); + (0, _util.warnOnDev)('You are running React-PDF from your local file system. PDF.js Worker may fail to load due to browser\'s security policies. If you\'re on Google Chrome, you can use --allow-file-access-from-files flag for debugging purposes.'); } require('pdfjs-dist/webpack'); diff --git a/build/shared/util.js b/build/shared/util.js index 92ae40a15..660e154d9 100644 --- a/build/shared/util.js +++ b/build/shared/util.js @@ -6,16 +6,17 @@ Object.defineProperty(exports, "__esModule", { /** * Checks if we're running in a browser environment. */ -var isBrowser = exports.isBrowser = function isBrowser() { - return typeof window !== 'undefined'; -}; +var isBrowser = exports.isBrowser = typeof window !== 'undefined'; /** * Checks whether we're running from a local file system. */ -var isLocalFileSystem = exports.isLocalFileSystem = function isLocalFileSystem() { - return isBrowser() && window.location.protocol === 'file:'; -}; +var isLocalFileSystem = exports.isLocalFileSystem = isBrowser && window.location.protocol === 'file:'; + +/** + * Checks whether we're running on a production build or not. + */ +var isProduction = exports.isProduction = process.env.NODE_ENV === 'production'; /** * Checks whether a variable is defined. @@ -59,7 +60,7 @@ var isArrayBuffer = exports.isArrayBuffer = function isArrayBuffer(variable) { * @param {*} variable Variable to check */ var isBlob = exports.isBlob = function isBlob(variable) { - if (!isBrowser()) { + if (!isBrowser) { throw new Error('Attempted to check if a variable is a Blob on a non-browser environment.'); } @@ -72,7 +73,7 @@ var isBlob = exports.isBlob = function isBlob(variable) { * @param {*} variable Variable to check */ var isFile = exports.isFile = function isFile(variable) { - if (!isBrowser()) { + if (!isBrowser) { throw new Error('Attempted to check if a variable is a Blob on a non-browser environment.'); } @@ -149,4 +150,18 @@ var callIfDefined = exports.callIfDefined = function callIfDefined(fn, args) { var getPixelRatio = exports.getPixelRatio = function getPixelRatio() { return window.devicePixelRatio || 1; +}; + +var warnOnDev = exports.warnOnDev = function warnOnDev(message) { + if (!isProduction) { + // eslint-disable-next-line no-console + console.warn(message); + } +}; + +var displayCORSWarning = exports.displayCORSWarning = function displayCORSWarning() { + if (isLocalFileSystem) { + // eslint-disable-next-line no-console + warnOnDev('Loading PDF as base64 strings/URLs might not work on protocols other than HTTP/HTTPS. On Google Chrome, you can use --allow-file-access-from-files flag for debugging purposes.'); + } }; \ No newline at end of file