Skip to content

Commit

Permalink
new: replaced old Pdf renderer with new react-pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
parasharrajat committed Feb 19, 2021
1 parent a66ed4d commit 603d7b9
Show file tree
Hide file tree
Showing 346 changed files with 149 additions and 113,897 deletions.
21 changes: 21 additions & 0 deletions assets/css/pdf.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@import './AnnotationLayer.css';

.react-pdf__Page {
direction: ltr;
margin: 1px auto -8px auto;
position: relative;
overflow: visible;
border: 9px solid transparent;
background-clip: content-box;
border-image: url(../images/shadow.png) 9 9 repeat;
background-color: rgba(255, 255, 255, 1);
}
.react-pdf__message {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.react-pdf__Page__annotations{
height: 0;
}
File renamed without changes
8 changes: 4 additions & 4 deletions config/webpack/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ module.exports = {
{from: 'web/favicon-unread.png'},
{from: 'web/og-preview-image.png'},
{from: 'assets/css', to: 'css'},
{from: 'node_modules/react-pdf/dist/esm/Page/AnnotationLayer.css', to: 'css/AnnotationLayer.css'},
{from: 'assets/images/shadow.png', to: 'images/shadow.png'},

// These files are copied over as per instructions here
// https://github.com/mozilla/pdf.js/wiki/Setup-pdf.js-in-a-website#examples
{from: 'src/vendor/pdf-js/web', to: 'pdf/web'},
{from: 'src/vendor/pdf-js/js', to: 'pdf/build'},
// https://github.com/wojtekmaj/react-pdf#copying-cmaps
{from: 'node_modules/pdfjs-dist/cmaps/', to: 'cmaps/'},
],
}),
new IgnorePlugin(/^\.\/locale$/, /moment$/),
Expand Down Expand Up @@ -115,7 +116,6 @@ module.exports = {
alias: {
'react-native-config': 'react-web-config',
'react-native$': 'react-native-web',
'react-native-webview': 'react-native-web-webview',
},

// React Native libraries may have web-specific module implementations that appear with the extension `.web.js`
Expand Down
70 changes: 48 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@
"react-native-safe-area-context": "^3.1.4",
"react-native-svg": "^12.1.0",
"react-native-web": "^0.14.1",
"react-native-web-webview": "^1.0.2",
"react-native-webview": "^11.0.2",
"react-pdf": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-router-native": "^5.2.0",
"react-web-config": "^1.0.0",
Expand Down
72 changes: 59 additions & 13 deletions src/components/PDFView/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import React, {memo} from 'react';
import React, {memo, PureComponent} from 'react';
import PropTypes from 'prop-types';
import {WebView} from 'react-native-webview';
import CONST from '../../CONST';
import {View} from 'react-native';
import {Document, Page} from 'react-pdf/dist/esm/entry.webpack';
import styles from '../../styles/styles';
import withWindowDimensions, {windowDimensionsPropTypes} from '../withWindowDimensions';
import variables from '../../styles/variables';

const propTypes = {
// URL to full-sized image
sourceURL: PropTypes.string,

...windowDimensionsPropTypes,

// Any additional styles to apply
// eslint-disable-next-line react/forbid-prop-types
style: PropTypes.any,
Expand All @@ -18,21 +23,62 @@ const defaultProps = {
};

/**
* On web, we use a WebView pointed to a pdf renderer
* On web, we use this pointed to a pdf renderer
*
* @param props
* @returns {JSX.Element}
* @class PDFView
* @extends {PureComponent}
*/
class PDFView extends PureComponent {
constructor(props) {
super(props);
this.state = {
numPages: null,
};
this.onDocumentLoadSuccess = this.onDocumentLoadSuccess.bind(this);
console.debug(props);
}

onDocumentLoadSuccess = ({numPages}) => {
this.setState({numPages});
}

render() {
const {isSmallScreenWidth, windowWidth} = this.props;
const pageWidth = isSmallScreenWidth ? windowWidth - 30 : variables.pdfPageWidth;
return (
<View
style={[styles.PDFView, this.props.style]}
>
<Document
file={this.props.sourceURL}
options={{
cMapUrl: 'cmaps/',
cMapPacked: true,

const PDFView = props => (
<WebView
source={{uri: `${CONST.PDF_VIEWER_URL}?file=${encodeURIComponent(props.sourceURL)}`}}
style={props.style}
/>
);
}}
externalLinkTarget="_blank"
onLoadSuccess={this.onDocumentLoadSuccess}
>
{
Array.from(
new Array(this.state.numPages),
(el, index) => (
<Page
width={pageWidth}
key={`page_${index + 1}`}
pageNumber={index + 1}
/>
),
)
}
</Document>
</View>
);
}
}

PDFView.propTypes = propTypes;
PDFView.defaultProps = defaultProps;
PDFView.displayName = 'PDFView';

export default memo(PDFView);
export default withWindowDimensions(memo(PDFView));
2 changes: 1 addition & 1 deletion src/components/PDFView/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const PDFView = props => (
styles.imageModalPDF,
{
width: props.windowWidth,
height: props.windwoHeight,
height: props.windowHeight,
},
]}
/>
Expand Down
12 changes: 11 additions & 1 deletion src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,17 @@ const styles = {

imageModalPDF: {
flex: 1,
backgroundColor: themeColors.componentBG,
backgroundColor: themeColors.modalBackground,
},
PDFView: {
flex: 1,
backgroundColor: themeColors.modalBackground,
width: '100%',
height: '100%',
flexDirection: 'row',
justifyContent: 'center',
overflow: 'hidden',
overflowY: 'auto',
},

modalCenterContentContainer: {
Expand Down
1 change: 1 addition & 0 deletions src/styles/themes/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default {
offline: colors.gray3,
sidebarButtonBG: 'rgba(198, 201, 202, 0.25)',
modalBackdrop: colors.gray3,
modalBackground: colors.gray2,
pillBG: colors.gray2,
buttonDisabledBG: colors.gray2,
buttonHoveredBG: colors.gray1,
Expand Down
2 changes: 2 additions & 0 deletions src/styles/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default {
iconSizeSmall: 12,
iconSizeNormal: 20,
mobileResponsiveWidthBreakpoint: 800,
desktopResponsiveWidthBreakpoint: 1000,
safeInsertPercentage: 0.7,
sideBarWidth: 375,
pdfPageWidth: 992,
};
Loading

0 comments on commit 603d7b9

Please sign in to comment.