Skip to content

Commit

Permalink
Fix pdf viewer for analysis plugin
Browse files Browse the repository at this point in the history
The newer version of react-pdf needd to wait for the pdf Document to be
loaded before the Page is displayed.
  • Loading branch information
Dinika committed Nov 6, 2024
1 parent 9bc6cd4 commit dfada44
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 53 deletions.
2 changes: 1 addition & 1 deletion cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default defineConfig({
projectLabel,
resource: resourcePayload,
});
if (!createResource) {
if (!createdResource) {
throw new Error('Test Resource was not created');
}
return createdResource;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"react-infinite-scroll-component": "^4.5.3",
"react-json-view": "^1.21.3",
"react-mde": "^11.0.0",
"react-pdf": "7.5.1",
"react-pdf": "9.1.1",
"react-player": "2.13.0",
"react-query": "^3.13.4",
"react-redux": "^8.0.2",
Expand Down
5 changes: 4 additions & 1 deletion src/shared/components/FileInfo/PDFFileInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export default ({
<PDFThumbnail
previewDisabled={previewDisabled}
url={src}
onPreview={() => !previewDisabled && setIsVisible(true)}
onPreview={React.useCallback(
() => !previewDisabled && setIsVisible(true),
[]
)}
/>
)}
{isVisible && src && (
Expand Down
54 changes: 28 additions & 26 deletions src/shared/components/Preview/PDFPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import './PDFThumbnail.scss';
import './PDFPreview.scss';

pdfjs.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.js',
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url
).toString();

Expand All @@ -36,7 +36,7 @@ const PDFThumbnail = ({
height: number;
width: number;
}>();

const [isReady, setIsReady] = React.useState(false);
/**
* Returns either height or width prop for specifying
* size of PDF document for display on screen depending
Expand All @@ -55,37 +55,40 @@ const PDFThumbnail = ({
return { width: Math.floor(bounds.width * 0.8) };
}, [bounds, pdfDimensions]);

const onDocumentLoadSuccess = React.useCallback(() => {
setIsReady(true);
}, []);
return (
<div className="pdf-thumbnail-wrapper" ref={wrapperHeightRef}>
<Document
loading={<>loading...</>}
file={url}
onLoadError={console.error}
renderMode="svg"
options={{ isEvalSupported: false }}
onLoadSuccess={onDocumentLoadSuccess}
>
<Page
className="pdf-thumbnail-page"
pageNumber={1}
renderMode="svg"
onLoadSuccess={page => {
setPdfDimensions({
height: page.originalHeight,
width: page.originalWidth,
});
}}
{...calculatePDFSizingProp}
>
{!previewDisabled && (
<div className="ant-image-mask" onClick={() => onPreview()}>
<div className="ant-image-mask-info">
<EyeOutlined size={16} />
<br />
Preview
{isReady && (
<Page
className="pdf-thumbnail-page"
pageNumber={1}
onLoadSuccess={page => {
setPdfDimensions({
height: page.originalHeight,
width: page.originalWidth,
});
}}
{...calculatePDFSizingProp}
>
{!previewDisabled && (
<div className="ant-image-mask" onClick={() => onPreview()}>
<div className="ant-image-mask-info">
<EyeOutlined size={16} />
<br />
Preview
</div>
</div>
</div>
)}
</Page>
)}
</Page>
)}
</Document>
</div>
);
Expand Down Expand Up @@ -177,7 +180,6 @@ const PDFViewer: React.FC<{
<TransformComponent>
<Page
pageNumber={pageNumber}
renderMode="svg"
renderTextLayer={false}
renderAnnotationLayer={false}
onLoadSuccess={page => {
Expand Down
80 changes: 56 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8546,10 +8546,10 @@ merge-descriptors@1.0.1:
resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61"
integrity sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==

merge-refs@^1.2.1:
version "1.2.2"
resolved "https://registry.yarnpkg.com/merge-refs/-/merge-refs-1.2.2.tgz#6142633398dd0d10a37626cae77ddeb1db26db0c"
integrity sha512-RwcT7GsQR3KbuLw1rRuodq4Nt547BKEBkliZ0qqsrpyNne9bGTFtsFIsIpx82huWhcl3kOlOlH4H0xkPk/DqVw==
merge-refs@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/merge-refs/-/merge-refs-1.3.0.tgz#65d7f8c5058917b9d1fc204ae4b9a727614d0119"
integrity sha512-nqXPXbso+1dcKDpPCXvwZyJILz+vSLqGGOnDrYHQYE+B8n9JTCekVLC65AfCpR4ggVyA/45Y0iR9LDyS2iI+zA==

merge-stream@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -9492,10 +9492,10 @@ path-type@^4.0.0:
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"
integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==

path2d-polyfill@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/path2d-polyfill/-/path2d-polyfill-2.0.1.tgz#24c554a738f42700d6961992bf5f1049672f2391"
integrity sha512-ad/3bsalbbWhmBo0D6FZ4RNMwsLsPpL6gnvhuSaU5Vm7b06Kr5ubSltQQ0T7YKsiJQO+g22zJ4dJKNTXIyOXtA==
path2d@^0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/path2d/-/path2d-0.2.1.tgz#faf98e5e2222541805a6ac232adc026332330765"
integrity sha512-Fl2z/BHvkTNvkuBzYTpTuirHZg6wW9z8+4SND/3mDTEcYbbNKWAy21dz9D3ePNNwrrK8pqZO5vLPZ1hLF6T7XA==

pathe@^1.1.0, pathe@^1.1.1, pathe@^1.1.2:
version "1.1.2"
Expand Down Expand Up @@ -9531,13 +9531,13 @@ pdfast@^0.2.0:
resolved "https://registry.yarnpkg.com/pdfast/-/pdfast-0.2.0.tgz#8cbc556e1bf2522177787c0de2e0d4373ba885c9"
integrity sha512-cq6TTu6qKSFUHwEahi68k/kqN2mfepjkGrG9Un70cgdRRKLKY6Rf8P8uvP2NvZktaQZNF3YE7agEkLj0vGK9bA==

pdfjs-dist@3.11.174:
version "3.11.174"
resolved "https://registry.yarnpkg.com/pdfjs-dist/-/pdfjs-dist-3.11.174.tgz#5ff47b80f2d58c8dd0d74f615e7c6a7e7e704c4b"
integrity sha512-TdTZPf1trZ8/UFu5Cx/GXB7GZM30LT+wWUNfsi6Bq8ePLnb+woNKtDymI2mxZYBpMbonNFqKmiz684DIfnd8dA==
pdfjs-dist@4.4.168:
version "4.4.168"
resolved "https://registry.yarnpkg.com/pdfjs-dist/-/pdfjs-dist-4.4.168.tgz#4487716376a33c68753ed37f782ae91d1c9ef8fa"
integrity sha512-MbkAjpwka/dMHaCfQ75RY1FXX3IewBVu6NGZOcxerRFlaBiIkZmUoR0jotX5VUzYZEXAGzSFtknWs5xRKliXPA==
optionalDependencies:
canvas "^2.11.2"
path2d-polyfill "^2.0.1"
path2d "^0.2.0"

pend@~1.2.0:
version "1.2.0"
Expand Down Expand Up @@ -10555,19 +10555,19 @@ react-mde@^11.0.0:
resolved "https://registry.yarnpkg.com/react-mde/-/react-mde-11.5.0.tgz#3e81a505071aa80287fb23a1c0ce5e8b34c82055"
integrity sha512-CH/VK6d+tpVjJ8rTXfh1dDt6GWedTgCU0668p8toqhAc3vy0Lu872O2RKYDSpkUrlbHI08fjUPTl++nExp6gag==

react-pdf@7.5.1:
version "7.5.1"
resolved "https://registry.yarnpkg.com/react-pdf/-/react-pdf-7.5.1.tgz#ef6e3f72d0bc02aee16b77b54196825068ef23b7"
integrity sha512-NVno97L3wfX3RLGB3C+QtroOiQrgCKPHLMFKMSQaRqDlH3gkq2CB2NyXJ+IDQNLrT/gSMPPgtZQL8cOUySc/3w==
react-pdf@9.1.1:
version "9.1.1"
resolved "https://registry.yarnpkg.com/react-pdf/-/react-pdf-9.1.1.tgz#10b1d1012e1ad15a12b7d16fbaec5a3dc81068d7"
integrity sha512-Cn3RTJZMqVOOCgLMRXDamLk4LPGfyB2Np3OwQAUjmHIh47EpuGW1OpAA1Z1GVDLoHx4d5duEDo/YbUkDbr4QFQ==
dependencies:
clsx "^2.0.0"
dequal "^2.0.3"
make-cancellable-promise "^1.3.1"
make-event-props "^1.6.0"
merge-refs "^1.2.1"
pdfjs-dist "3.11.174"
prop-types "^15.6.2"
merge-refs "^1.3.0"
pdfjs-dist "4.4.168"
tiny-invariant "^1.0.0"
tiny-warning "^1.0.0"
warning "^4.0.0"

react-player@2.13.0:
version "2.13.0"
Expand Down Expand Up @@ -11790,7 +11790,7 @@ string-similarity@^4.0.4:
resolved "https://registry.yarnpkg.com/string-similarity/-/string-similarity-4.0.4.tgz#42d01ab0b34660ea8a018da8f56a3309bb8b2a5b"
integrity sha512-/q/8Q4Bl4ZKAPjj8WerIBJWALKkaPRfrvhfF8k/B23i4nzrlRj2/go1m90In7nG/3XDSbOo0+pu6RvCTM9RGMQ==

"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand All @@ -11808,6 +11808,15 @@ string-width@^1.0.1:
is-fullwidth-code-point "^1.0.0"
strip-ansi "^3.0.0"

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-2.1.1.tgz#ab93f27a8dc13d28cac815c462143a6d9012ae9e"
Expand Down Expand Up @@ -11894,7 +11903,7 @@ stringify-object@^3.3.0:
is-obj "^1.0.1"
is-regexp "^1.0.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -11922,6 +11931,13 @@ strip-ansi@^5.0.0, strip-ansi@^5.1.0, strip-ansi@^5.2.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
Expand Down Expand Up @@ -12973,6 +12989,13 @@ warning@^3.0.0:
dependencies:
loose-envify "^1.0.0"

warning@^4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/warning/-/warning-4.0.3.tgz#16e9e077eb8a86d6af7d64aa1e05fd85b4678ca3"
integrity sha512-rpJyN222KWIvHJ/F53XSZv0Zl/accqHR8et1kpaMTD/fLCRxtV8iX8czMzY7sVZupTI3zcUTg8eycS2kNF9l6w==
dependencies:
loose-envify "^1.0.0"

wcwidth@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/wcwidth/-/wcwidth-1.0.1.tgz#f0b0dcf915bc5ff1528afadb2c0e17b532da2fe8"
Expand Down Expand Up @@ -13142,7 +13165,7 @@ wordwrap@^1.0.0:
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand Down Expand Up @@ -13177,6 +13200,15 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down

0 comments on commit dfada44

Please sign in to comment.