insomnia/packages/insomnia-app/app/ui/components/pdf-viewer.js
Gregory Schier 549ce23ce8
Merge All Repositories into Monorepo for easier maintenance (#629)
* All projects into monorepo

* Update CI

* More CI updates

* Extracted a bunch of things into packages

* Publish

 - insomnia-plugin-base64@1.0.1
 - insomnia-plugin-default-headers@1.0.2
 - insomnia-plugin-file@1.0.1
 - insomnia-plugin-hash@1.0.1
 - insomnia-plugin-now@1.0.1
 - insomnia-plugin-request@1.0.1
 - insomnia-plugin-response@1.0.1
 - insomnia-plugin-uuid@1.0.1
 - insomnia-cookies@0.0.2
 - insomnia-importers@1.5.2
 - insomnia-prettify@0.0.3
 - insomnia-url@0.0.2
 - insomnia-xpath@0.0.2

* A bunch of small fixes

* Improved build script

* Fixed

* Merge dangling files

* Usability refactor

* Handle duplicate plugin names
2017-11-26 20:45:40 +00:00

113 lines
2.7 KiB
JavaScript

// @flow
import * as React from 'react';
import autobind from 'autobind-decorator';
import PDF from 'pdfjs-dist/webpack';
type Props = {
body: Buffer,
uniqueKey: string
};
type State = {
numPages: number | null;
};
@autobind
class PDFViewer extends React.PureComponent<Props, State> {
container: ?HTMLDivElement;
debounceTimeout: any;
setRef (n: ?HTMLDivElement) {
this.container = n;
}
loadPDF () {
clearTimeout(this.debounceTimeout);
this.debounceTimeout = setTimeout(async () => {
// get node for this react component
const container = this.container;
if (!container) {
return;
}
container.innerHTML = '';
const containerWidth = container.clientWidth;
const pdf = await PDF.getDocument({data: this.props.body.toString('binary')});
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i);
const density = window.devicePixelRatio || 1;
const {width: pdfWidth, height: pdfHeight} = page.getViewport(1);
const ratio = pdfHeight / pdfWidth;
const scale = containerWidth / pdfWidth;
const viewport = page.getViewport(scale * density);
// set canvas for page
const canvas = document.createElement('canvas');
canvas.width = containerWidth * density;
// canvas.height = containerWidth * (viewport.height / viewport.width);
canvas.height = containerWidth * ratio * density;
canvas.style.width = `${containerWidth}px`;
canvas.style.height = `${containerWidth * ratio}px`;
container.appendChild(canvas);
// get context and render page
const context = canvas.getContext('2d');
const renderContext = {
id: `${this.props.uniqueKey}.${i}`,
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
}
}, 100);
}
handleResize (e: SyntheticEvent<HTMLDivElement>) {
if (!this.container) {
return;
}
clearTimeout(this.debounceTimeout);
this.debounceTimeout = setTimeout(this.loadPDF, 300);
}
componentDidUpdate () {
this.loadPDF();
}
componentDidMount () {
this.loadPDF();
window.addEventListener('resize', this.handleResize);
}
componentWillUnmount () {
window.removeEventListener('resize', this.handleResize);
}
render () {
const styles = {
width: '100%',
height: '100%',
overflowX: 'hidden',
overflowY: 'scroll',
padding: '0px'
};
return (
<div className="S-PDF-ID" ref={this.setRef} style={styles}>
<div className="faded text-center vertically-center tall">
Loading PDF...
</div>
</div>
);
}
}
export default PDFViewer;