Skip to content

Commit

Permalink
better prop for pdf container width + test update
Browse files Browse the repository at this point in the history
  • Loading branch information
Zasa-san committed Dec 20, 2024
1 parent 23b6b1f commit 2e8b1cc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
7 changes: 5 additions & 2 deletions app/react/PDF/components/PDF.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PDF extends Component {
this.pageLoading = this.pageLoading.bind(this);
this.onPageVisible = this.onPageVisible.bind(this);
this.onPageHidden = this.onPageHidden.bind(this);
this.containerWidth = 0;
}

componentDidMount() {
Expand All @@ -42,6 +43,8 @@ class PDF extends Component {
});
document.addEventListener('textlayerrendered', this.props.onPageLoaded, { once: true });
}

this.containerWidth = this.props.parentRef.current?.clientWidth;
}

shouldComponentUpdate(nextProps, nextState) {
Expand Down Expand Up @@ -173,7 +176,7 @@ class PDF extends Component {
page={page}
pdf={this.state.pdf}
highlightReference={this.props.highlightReference}
containerWidth={document.getElementById(this.props.pdfParentId).clientWidth}
containerWidth={this.containerWidth}
/>
</SelectionRegion>
</div>
Expand Down Expand Up @@ -209,7 +212,7 @@ PDF.propTypes = {
onLoad: PropTypes.func.isRequired,
style: PropTypes.object,
highlightReference: PropTypes.func,
pdfParentId: PropTypes.string.isRequired,
parentRef: PropTypes.object.isRequired,
};

export default PDF;
1 change: 1 addition & 0 deletions app/react/PDF/components/specs/PDF.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('PDF', () => {
file: 'file_url',
filename: 'original.pdf',
onLoad: jasmine.createSpy('onLoad'),
parentRef: { current: { clientWidth: 500 } },
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`PDFPage render should pass pass proper height when state.height 1`] = `
<div
className="doc-page"
className="pdf-page"
id="page-2"
style={
Object {
Expand All @@ -14,7 +14,7 @@ exports[`PDFPage render should pass pass proper height when state.height 1`] = `

exports[`PDFPage render should render a div as pageContainer 1`] = `
<div
className="doc-page"
className="pdf-page"
id="page-2"
style={
Object {
Expand Down
14 changes: 7 additions & 7 deletions app/react/Viewer/components/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'app/Viewer/scss/conversion_base.scss';
import 'app/Viewer/scss/document.scss';

import PropTypes from 'prop-types';
import React, { Component } from 'react';
import React, { Component, createRef } from 'react';

import { Loader } from 'app/components/Elements/Loader';
import { PDF } from 'app/PDF';
Expand All @@ -29,6 +29,7 @@ class Document extends Component {
this.onTextSelected = this.onTextSelected.bind(this);
this.handleClick = this.handleClick.bind(this);
this.highlightReference = this.highlightReference.bind(this);
this.containerRef = createRef();
}

componentDidUpdate(prevProps) {
Expand Down Expand Up @@ -93,8 +94,8 @@ class Document extends Component {

handleOver() {}

renderPDF(file, pdfContainerParentId) {
if (!file._id) {
renderPDF(file) {
if (!file._id && this.containerRef) {
return <Loader />;
}

Expand All @@ -111,15 +112,14 @@ class Document extends Component {
highlightReference={this.highlightReference}
activeReference={this.props.activeReference}
key={file.filename}
pdfParentId={pdfContainerParentId}
parentRef={this.containerRef}
/>
);
}

render() {
const { file } = this.props;
const Header = this.props.header;
const pdfContainerParentId = `${this.props.doc.get('sharedId')}-pages`;

return (
<div>
Expand All @@ -131,11 +131,11 @@ class Document extends Component {
<Header />
<div
className="pages"
id={pdfContainerParentId}
ref={this.containerRef}
onMouseOver={this.handleOver.bind(this)}
onClick={this.handleClick}
>
{this.renderPDF(file, pdfContainerParentId)}
{this.renderPDF(file)}
</div>
</div>
</div>
Expand Down
11 changes: 8 additions & 3 deletions app/react/Viewer/components/specs/Document.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import Immutable from 'immutable';
import { PDF } from 'app/PDF';
import { Document } from 'app/Viewer/components/Document.js';

jest.mock('V2/atoms', () => ({
atomStore: { get: () => 2 },
}));

// eslint-disable-next-line max-statements
describe('Document', () => {
let component;
let instance;
Expand Down Expand Up @@ -116,7 +121,7 @@ describe('Document', () => {
});

describe('onTextSelected', () => {
it('should set the selection changing regionId to page', () => {
it('should set the selection changing regionId to page and adjusting based on rendering scale', () => {
render();

const textSelection = {
Expand All @@ -131,8 +136,8 @@ describe('Document', () => {
expect(props.setSelection).toHaveBeenCalledWith(
{
selectionRectangles: [
{ height: 12, left: 27, page: '51', top: 186, width: 23 },
{ height: 89, left: 47, page: '52', top: 231, width: 11 },
{ height: 6, left: 13.5, page: '51', top: 93, width: 11.5 },
{ height: 44.5, left: 23.5, page: '52', top: 115.5, width: 5.5 },
],
text: 'Wham Bam Shang-A-Lang',
},
Expand Down

0 comments on commit 2e8b1cc

Please sign in to comment.