Skip to content

Commit

Permalink
Updating dependencies and fixing some lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecousins committed Sep 28, 2016
1 parent 2c017e7 commit f329464
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 100 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
18 changes: 9 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -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"
]

}
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": [
Expand All @@ -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": [
Expand Down
144 changes: 73 additions & 71 deletions src/Pdf.js → src/Pdf.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
});
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -237,15 +237,17 @@ class Pdf extends React.Component {
render() {
const { loading } = this.props;
const { page } = this.state;
return page ? <canvas ref={c => this.canvas = c} /> : loading || <div>Loading PDF...</div>;
return page ?
<canvas ref={(c) => { this.canvas = c; }} /> :
loading || <div>Loading PDF...</div>;
}
}

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,
Expand Down
8 changes: 6 additions & 2 deletions webpack.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ module.exports = {
},
module: {
loaders: [
{ test: /\.js$/, loaders: ['babel-loader'], exclude: /node_modules/ }
{
test: /\.jsx?$/,
loader: 'babel',
exclude: /node_modules/
}
]
},
output: {
library: 'ReduxForm',
libraryTarget: 'umd'
},
resolve: {
extensions: ['', '.js']
extensions: ['', '.js', '.jsx']
}
};
3 changes: 1 addition & 2 deletions webpack.config.production.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ config.plugins = [
'process.env.NODE_ENV': JSON.stringify('production')
}),
new webpack.optimize.UglifyJsPlugin({
compressor: {
screw_ie8: true,
compress: {
warnings: false
}
})
Expand Down

0 comments on commit f329464

Please sign in to comment.