diff --git a/package-lock.json b/package-lock.json index e8e84d998..ce8ff5498 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "react-pdf", + "name": "@peergrade/react-pdf", "version": "2.1.6", "lockfileVersion": 1, "requires": true, diff --git a/package.json b/package.json index 38a4f79ae..0e555b23c 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "react-pdf", + "name": "@peergrade/react-pdf", "version": "2.1.6", "description": "Easily display PDF files in your React application.", "main": "build/entry.js", @@ -7,6 +7,7 @@ "scripts": { "build": "babel src -d build", "eslint": "eslint ./src", + "prepare": "npm run build", "prepublishOnly": "npm run build", "test": "npm run eslint" }, @@ -58,6 +59,6 @@ "files": ["LICENSE", "README.md", "build/", "src/"], "repository": { "type": "git", - "url": "https://github.com/wojtekmaj/react-pdf.git" + "url": "https://github.com/peergradeio/react-pdf.git" } } diff --git a/src/Page.jsx b/src/Page.jsx index c6d9083d0..588112d73 100644 --- a/src/Page.jsx +++ b/src/Page.jsx @@ -4,6 +4,7 @@ import mergeClassNames from 'merge-class-names'; import PageCanvas from './PageCanvas'; import PageTextContent from './PageTextContent'; +import PageAnnotations from './PageAnnotations'; import { callIfDefined, @@ -18,6 +19,7 @@ import { eventsProps } from './shared/propTypes'; export default class Page extends Component { state = { page: null, + scaleChanged: false, } componentDidMount() { @@ -25,12 +27,19 @@ export default class Page extends Component { } componentWillReceiveProps(nextProps) { + const { scale, pdf } = this.props + const { scale: nextScale, pdf: nextPdf } = nextProps + if ( - nextProps.pdf !== this.props.pdf || + nextPdf !== pdf || this.getPageNumber(nextProps) !== this.getPageNumber() ) { this.loadPage(nextProps); } + + if (scale !== nextScale) { + this.setState({scaleChanged: true}) + } } componentWillUnmount() { @@ -116,13 +125,18 @@ export default class Page extends Component { } get scale() { - const { scale, width } = this.props; - const { page } = this.state; + const { scale, width, initialWidth } = this.props; + const { page, scaleChanged } = this.state; const { rotate } = this; // Be default, we'll render page at 100% * scale width. let pageScale = 1; + if (initialWidth && !scaleChanged) { + const viewport = page.getViewport(scale, rotate); + pageScale = initialWidth / viewport.width; + } + // If width is defined, calculate the scale of the page so it could be of desired width. if (width) { const viewport = page.getViewport(scale, rotate); @@ -190,6 +204,7 @@ export default class Page extends Component { onGetTextSuccess, onRenderError, onRenderSuccess, + renderAnnotations, renderTextLayer, } = this.props; const { rotate, scale } = this; @@ -219,6 +234,7 @@ export default class Page extends Component { scale={scale} /> } + {renderAnnotations && } {children} ); @@ -248,9 +264,11 @@ Page.propTypes = { getPage: PropTypes.func.isRequired, numPages: PropTypes.number.isRequired, }), + renderAnnotations: PropTypes.bool, renderTextLayer: PropTypes.bool, rotate: PropTypes.number, scale: PropTypes.number, width: PropTypes.number, + initialWidth: PropTypes.number, ...eventsProps(), }; diff --git a/src/PageAnnotations.jsx b/src/PageAnnotations.jsx new file mode 100644 index 000000000..0490dc4ac --- /dev/null +++ b/src/PageAnnotations.jsx @@ -0,0 +1,69 @@ +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; + +import { + makeCancellable, +} from './shared/util'; + +export default class PageAnnotations extends Component { + componentDidMount() { + this.getAnnotations(); + } + + componentWillReceiveProps(nextProps) { + if (nextProps.page !== this.props.page) { + this.getAnnotations(); + } + } + + componentDidUpdate(prevProps) { + if (prevProps.scale !== this.props.scale) { + this.getAnnotations(true); + } + } + + componentWillUnmount() { + if (this.runningTask && this.runningTask.cancel) { + this.runningTask.cancel(); + } + } + + getAnnotations(update = false) { + this.runningTask = makeCancellable(this.props.page.getAnnotations()); + + return this.runningTask.promise.then((annotations) => { + this.renderAnnotations(annotations, update); + }); + } + + renderAnnotations(annotations, update) { + const { page, scale } = this.props; + const viewport = page.getViewport(scale).clone({ dontFlip: true }); + const parameters = { + div: this.annotationLayer, + annotations, + page, + viewport, + }; + + if (update) { + PDFJS.AnnotationLayer.update(parameters); + } else { + PDFJS.AnnotationLayer.render(parameters); + } + } + + render() { + return ( +
{ this.annotationLayer = ref; }} /> + ); + } +} + +PageAnnotations.propTypes = { + page: PropTypes.shape({ + getAnnotations: PropTypes.func.isRequired, + getViewport: PropTypes.func.isRequired, + }).isRequired, + scale: PropTypes.number, +}; diff --git a/src/PageTextContent.jsx b/src/PageTextContent.jsx index 3c3fa349a..c500c370b 100644 --- a/src/PageTextContent.jsx +++ b/src/PageTextContent.jsx @@ -197,6 +197,7 @@ PageTextContent.propTypes = { commonObjs: PropTypes.shape({ objs: PropTypes.object.isRequired, }).isRequired, + getAnnotations: PropTypes.func.isRequired, getTextContent: PropTypes.func.isRequired, getViewport: PropTypes.func.isRequired, transport: PropTypes.shape({