From f329464710ecb22e3cd22972e7c8ea933e720803 Mon Sep 17 00:00:00 2001 From: Mike Cousins Date: Wed, 28 Sep 2016 14:12:20 -0600 Subject: [PATCH] Updating dependencies and fixing some lint issues --- .editorconfig | 10 +++ .eslintrc | 18 ++--- package.json | 32 ++++---- src/{Pdf.js => Pdf.jsx} | 144 ++++++++++++++++++----------------- webpack.config.base.js | 8 +- webpack.config.production.js | 3 +- 6 files changed, 115 insertions(+), 100 deletions(-) create mode 100644 .editorconfig rename src/{Pdf.js => Pdf.jsx} (95%) diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..d1d8a417 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +# http://editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.eslintrc b/.eslintrc index e54ec63e..b16b3d95 100755 --- a/.eslintrc +++ b/.eslintrc @@ -1,18 +1,18 @@ { - "extends": "eslint-config-airbnb", + "parser": "babel-eslint", + "extends": "airbnb", + "plugins": [ + "react" + ], "env": { "browser": true, "mocha": true, "node": true }, "rules": { - "max-len": [2, 200, 2], - "consistent-return": 0, - "linebreak-style": 0, - "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }], - "no-return-assign": 0 + "consistent-return": "off", + "react/forbid-prop-types": "off", + "no-bitwise": "off" }, - "plugins": [ - "react" - ] + } diff --git a/package.json b/package.json index 24372b42..294601e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-pdf-js", - "version": "1.0.27", + "version": "1.0.28", "description": "A React component to wrap PDF.js", "main": "./lib/index.js", "jsnext:main": "./src/index.js", @@ -14,7 +14,7 @@ "build:umd": "webpack src/index.js dist/react-pdf-js.js --config webpack.config.development.js", "build:umd:min": "webpack src/index.js dist/react-pdf-js.min.js --config webpack.config.production.js", "clean": "rimraf dist lib", - "lint": "eslint src", + "lint": "eslint src --ext .js --ext .jsx", "prepublish": "npm run lint && npm run clean && npm run build" }, "keywords": [ @@ -30,28 +30,28 @@ }, "homepage": "https://github.com/mikecousins/react-pdf-js", "dependencies": { - "pdfjs-dist": "1.5.376", - "react": "15.3.0" + "pdfjs-dist": "1.5.496", + "react": "15.3.2" }, "devDependencies": { - "babel-cli": "6.11.4", - "babel-core": "6.13.2", - "babel-eslint": "6.1.2", - "babel-loader": "6.2.4", + "babel-cli": "6.14.0", + "babel-core": "6.14.0", + "babel-eslint": "7.0.0", + "babel-loader": "6.2.5", "babel-plugin-react-transform": "2.0.2", - "babel-plugin-transform-runtime": "6.12.0", + "babel-plugin-transform-runtime": "6.15.0", "babel-plugin-typecheck": "3.9.0", - "babel-preset-es2015": "6.13.2", + "babel-preset-es2015": "6.14.0", "babel-preset-react": "6.11.1", "babel-runtime": "6.11.6", - "eslint": "3.3.0", - "eslint-config-airbnb": "10.0.1", - "eslint-plugin-import": "1.13.0", - "eslint-plugin-react": "6.1.0", - "eslint-plugin-jsx-a11y": "2.1.0", + "eslint": "3.6.1", + "eslint-config-airbnb": "12.0.0", + "eslint-plugin-import": "1.16.0", + "eslint-plugin-react": "6.3.0", + "eslint-plugin-jsx-a11y": "2.2.2", "rifraf": "2.0.2", "rimraf": "2.5.4", - "webpack": "1.13.1" + "webpack": "1.13.2" }, "npmName": "react-pdf-js", "npmFileMap": [ diff --git a/src/Pdf.js b/src/Pdf.jsx similarity index 95% rename from src/Pdf.js rename to src/Pdf.jsx index ea61ac15..2f511a99 100755 --- a/src/Pdf.js +++ b/src/Pdf.jsx @@ -7,10 +7,10 @@ const makeCancelable = (promise) => { let hasCanceled = false; const wrappedPromise = new Promise((resolve, reject) => { - promise.then((val) => ( + promise.then(val => ( hasCanceled ? reject({ pdf: val, isCanceled: true }) : resolve(val) )); - promise.catch((error) => ( + promise.catch(error => ( hasCanceled ? reject({ isCanceled: true }) : reject(error) )); }); @@ -24,6 +24,71 @@ const makeCancelable = (promise) => { }; class Pdf extends React.Component { + static onDocumentError(err) { + if (err.isCanceled && err.pdf) { + err.pdf.destroy(); + } + } + + // Converts an ArrayBuffer directly to base64, without any intermediate 'convert to string then + // use window.btoa' step and without risking a blow of the stack. According to [Jon Leightons's] + // tests, this appears to be a faster approach: http://jsperf.com/encoding-xhr-image-data/5 + // Jon Leighton https://gist.github.com/jonleighton/958841 + static defaultBinaryToBase64(arrayBuffer) { + let base64 = ''; + const encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + + const bytes = new Uint8Array(arrayBuffer); + const byteLength = bytes.byteLength; + const byteRemainder = byteLength % 3; + const mainLength = byteLength - byteRemainder; + + let a; + let b; + let c; + let d; + let chunk; + + // Main loop deals with bytes in chunks of 3 + for (let i = 0; i < mainLength; i += 3) { + // Combine the three bytes into a single integer + chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]; + + // Use bitmasks to extract 6-bit segments from the triplet + a = (chunk & 16515072) >> 18; // 16515072 = (2^6 - 1) << 18 + b = (chunk & 258048) >> 12; // 258048 = (2^6 - 1) << 12 + c = (chunk & 4032) >> 6; // 4032 = (2^6 - 1) << 6 + d = chunk & 63; // 63 = 2^6 - 1 + + // Convert the raw binary segments to the appropriate ASCII encoding + base64 = [base64, encodings[a], encodings[b], encodings[c], encodings[d]].join(''); + } + + // Deal with the remaining bytes and padding + if (byteRemainder === 1) { + chunk = bytes[mainLength]; + + a = (chunk & 252) >> 2; // 252 = (2^6 - 1) << 2 + + // Set the 4 least significant bits to zero + b = (chunk & 3) << 4; // 3 = 2^2 - 1 + + base64 = [base64, encodings[a], encodings[b], '=='].join(''); + } else if (byteRemainder === 2) { + chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]; + + a = (chunk & 64512) >> 10; // 64512 = (2^6 - 1) << 10 + b = (chunk & 1008) >> 4; // 1008 = (2^6 - 1) << 4 + + // Set the 2 least significant bits to zero + c = (chunk & 15) << 2; // 15 = 2^4 - 1 + + base64 = [base64, encodings[a], encodings[b], encodings[c], '='].join(''); + } + + return base64; + } + constructor(props) { super(props); this.state = {}; @@ -103,12 +168,6 @@ class Pdf extends React.Component { pdf.getPage(this.props.page).then(this.onPageComplete); } - onDocumentError(err) { - if (err.isCanceled && err.pdf) { - err.pdf.destroy(); - } - } - onPageComplete(page) { this.setState({ page }); this.renderPdf(); @@ -151,7 +210,7 @@ class Pdf extends React.Component { const bytes = window.atob(props.content); const byteLength = bytes.length; const byteArray = new Uint8Array(new ArrayBuffer(byteLength)); - for (let index = 0; index < byteLength; index++) { + for (let index = 0; index < byteLength; index += 1) { byteArray[index] = bytes.charCodeAt(index); } this.loadByteArray(byteArray); @@ -162,65 +221,6 @@ class Pdf extends React.Component { } } - // Converts an ArrayBuffer directly to base64, without any intermediate 'convert to string then - // use window.btoa' step and without risking a blow of the stack. According to [Jon Leightons's] - // tests, this appears to be a faster approach: http://jsperf.com/encoding-xhr-image-data/5 - // Jon Leighton https://gist.github.com/jonleighton/958841 - defaultBinaryToBase64(arrayBuffer) { - let base64 = ''; - const encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; - - const bytes = new Uint8Array(arrayBuffer); - const byteLength = bytes.byteLength; - const byteRemainder = byteLength % 3; - const mainLength = byteLength - byteRemainder; - - let a; - let b; - let c; - let d; - let chunk; - - // Main loop deals with bytes in chunks of 3 - for (let i = 0; i < mainLength; i += 3) { - // Combine the three bytes into a single integer - chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]; - - // Use bitmasks to extract 6-bit segments from the triplet - a = (chunk & 16515072) >> 18; // 16515072 = (2^6 - 1) << 18 - b = (chunk & 258048) >> 12; // 258048 = (2^6 - 1) << 12 - c = (chunk & 4032) >> 6; // 4032 = (2^6 - 1) << 6 - d = chunk & 63; // 63 = 2^6 - 1 - - // Convert the raw binary segments to the appropriate ASCII encoding - base64 = [base64, encodings[a], encodings[b], encodings[c], encodings[d]].join(''); - } - - // Deal with the remaining bytes and padding - if (byteRemainder === 1) { - chunk = bytes[mainLength]; - - a = (chunk & 252) >> 2; // 252 = (2^6 - 1) << 2 - - // Set the 4 least significant bits to zero - b = (chunk & 3) << 4; // 3 = 2^2 - 1 - - base64 = [base64, encodings[a], encodings[b], '=='].join(''); - } else if (byteRemainder === 2) { - chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]; - - a = (chunk & 64512) >> 10; // 64512 = (2^6 - 1) << 10 - b = (chunk & 1008) >> 4; // 1008 = (2^6 - 1) << 4 - - // Set the 2 least significant bits to zero - c = (chunk & 15) << 2; // 15 = 2^4 - 1 - - base64 = [base64, encodings[a], encodings[b], encodings[c], '='].join(''); - } - - return base64; - } - renderPdf() { const { page } = this.state; if (page) { @@ -237,15 +237,17 @@ class Pdf extends React.Component { render() { const { loading } = this.props; const { page } = this.state; - return page ? this.canvas = c} /> : loading ||
Loading PDF...
; + return page ? + { this.canvas = c; }} /> : + loading ||
Loading PDF...
; } } Pdf.displayName = 'react-pdf-js'; Pdf.propTypes = { content: React.PropTypes.string, - documentInitParameters: React.PropTypes.object, - binaryContent: React.PropTypes.object, + documentInitParameters: React.PropTypes.shape, + binaryContent: React.PropTypes.shape, file: React.PropTypes.any, // Could be File object or URL string. loading: React.PropTypes.any, page: React.PropTypes.number, diff --git a/webpack.config.base.js b/webpack.config.base.js index 6b4f401e..252bb912 100644 --- a/webpack.config.base.js +++ b/webpack.config.base.js @@ -14,7 +14,11 @@ module.exports = { }, module: { loaders: [ - { test: /\.js$/, loaders: ['babel-loader'], exclude: /node_modules/ } + { + test: /\.jsx?$/, + loader: 'babel', + exclude: /node_modules/ + } ] }, output: { @@ -22,6 +26,6 @@ module.exports = { libraryTarget: 'umd' }, resolve: { - extensions: ['', '.js'] + extensions: ['', '.js', '.jsx'] } }; diff --git a/webpack.config.production.js b/webpack.config.production.js index f4589cc0..63a96d35 100644 --- a/webpack.config.production.js +++ b/webpack.config.production.js @@ -10,8 +10,7 @@ config.plugins = [ 'process.env.NODE_ENV': JSON.stringify('production') }), new webpack.optimize.UglifyJsPlugin({ - compressor: { - screw_ie8: true, + compress: { warnings: false } })