-
Notifications
You must be signed in to change notification settings - Fork 34
feat: add text layer support to PDF viewer #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
jhpedemonte
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested locally in Storybook and works great. But have many suggestions/questions for the code.
...discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewer.tsx
Outdated
Show resolved
Hide resolved
...discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewer.tsx
Outdated
Show resolved
Hide resolved
...discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewer.tsx
Outdated
Show resolved
Hide resolved
...discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewer.tsx
Outdated
Show resolved
Hide resolved
...discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewer.tsx
Outdated
Show resolved
Hide resolved
...-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewerTextLayer.tsx
Outdated
Show resolved
Hide resolved
.../discovery-react-components/src/components/DocumentPreview/components/PdfViewer/typings.d.ts
Show resolved
Hide resolved
packages/discovery-styles/scss/components/document-preview/_document-preview-pdf-viewer.scss
Outdated
Show resolved
Hide resolved
packages/discovery-styles/scss/components/document-preview/_document-preview-pdf-viewer.scss
Outdated
Show resolved
Hide resolved
| /** | ||
| * Render text layer | ||
| */ | ||
| showTextLayer?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason for making this an option? Why not always show the text layer when rendering a PDF?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we reuse this component to render thumbnail image or tile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm worried that this would take up a lot of resources, if we need to create many "thumbnails". We'll have to test this out and make sure we don't overwhelm the browser.
SDU Annotator has a server-side process to create the thumbnails. I don't believe we will be able to make use of that, but a similar approach may be needed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Let's think about it when we implement thumbnails. With that, our usage is only to display a (full-page) document and I drop the option.
packages/discovery-styles/scss/components/document-preview/_document-preview-pdf-viewer.scss
Outdated
Show resolved
Hide resolved
...discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewer.tsx
Outdated
Show resolved
Hide resolved
|
|
||
| const { RenderingCancelledException } = PDFJSLib as any; | ||
|
|
||
| interface Props { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these just a subset of the PdfViewer props? If so, should this just extend those? Something like:
type Props = Pick<PdfViewerProps, 'loadedPage' | 'scale'> & { setTextLayerInfo?: ... };There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only scale prop is so. I updated the type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did this change? I see same Props object as previously (and my comment above still stands).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use the same property both in PdfViewer and PdfViewerTextLayer
...-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewerTextLayer.tsx
Outdated
Show resolved
Hide resolved
...react-components/src/components/DocumentPreview/components/PdfViewer/useAsyncFunctionCall.ts
Show resolved
Hide resolved
...react-components/src/components/DocumentPreview/components/PdfViewer/useAsyncFunctionCall.ts
Outdated
Show resolved
Hide resolved
...react-components/src/components/DocumentPreview/components/PdfViewer/useAsyncFunctionCall.ts
Show resolved
Hide resolved
...discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewer.tsx
Show resolved
Hide resolved
| const { textContent, viewport, scale, page } = loadedText; | ||
|
|
||
| const builder = new TextLayerBuilder({ | ||
| textLayerDiv, | ||
| viewport, | ||
| eventBus: new EventBus(), | ||
| pageIndex: page - 1 | ||
| }); | ||
| signal.addEventListener('abort', () => builder.cancel()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm really liking this AbortController usage pattern. We should consider bringing this in to main Tooling (as an import from this library).
packages/discovery-styles/scss/components/document-preview/_document-preview-pdf-viewer.scss
Show resolved
Hide resolved
packages/discovery-styles/scss/components/document-preview/_pdfjs_web_mixins.scss
Show resolved
Hide resolved
|
This pull request introduces 1 alert when merging 6ba0703 into f904849 - view on LGTM.com new alerts:
|
|
This pull request introduces 1 alert when merging 052336a into f904849 - view on LGTM.com new alerts:
|
jhpedemonte
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code is looking good. I have a few more suggestions, plus a few more from previous discussions (which I haven't closed as "resolved").
With those fixed, approved.
| @@ -0,0 +1,19 @@ | |||
| #!/bin/sh | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: let's rename this to something more clear, like update-styles-from-pdfjs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we should update the NPM script name in package.json to match.
| PDFJS_SCSS=$BASEDIR/scss/components/document-preview/_pdfjs_web_mixins.scss | ||
| node $BASEDIR/scripts/generate-pdfjs_web_mixin.js "$PDFJS_WEB_CSS" "$PDFJS_SCSS" | ||
|
|
||
| # perttier |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # perttier | |
| # prettier |
| const originalPdfjsWebCss = process.argv[2]; | ||
| const mixinPdfjsWebScss = process.argv[3]; | ||
|
|
||
| // load teh original style |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // load teh original style | |
| // load the original style |
| } | ||
| `; | ||
|
|
||
| fs.writeFileSync(mixinPdfjsWebScss, generatedCss, { encoding: 'utf-8' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great. Thanks.
| # pdfjs textLayer styles | ||
| PDFJS_WEB_CSS=$BASEDIR/../../node_modules/pdfjs-dist/web/pdf_viewer.css | ||
| PDFJS_SCSS=$BASEDIR/scss/components/document-preview/_pdfjs_web_mixins.scss | ||
| node $BASEDIR/scripts/generate-pdfjs_web_mixin.js "$PDFJS_WEB_CSS" "$PDFJS_SCSS" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yarn v2 recommends running using yarn node instead of simply node, since yarn adds some environment values which may be necessary when importing.
| node $BASEDIR/scripts/generate-pdfjs_web_mixin.js "$PDFJS_WEB_CSS" "$PDFJS_SCSS" | |
| yarn node $BASEDIR/scripts/generate-pdfjs_web_mixin.js "$PDFJS_WEB_CSS" "$PDFJS_SCSS" |
| node $BASEDIR/scripts/generate-pdfjs_web_mixin.js "$PDFJS_WEB_CSS" "$PDFJS_SCSS" | ||
|
|
||
| # perttier | ||
| ../../node_modules/.bin/prettier --write "$PDFJS_SCSS" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ../../node_modules/.bin/prettier --write "$PDFJS_SCSS" | |
| yarn run prettier --write "$PDFJS_SCSS" |
What do these changes do/fix?
Add
showTextLayeroption to render text layer onPDFVieweras it's necessary for implementing highlighting on PDF. It would be useful for text selection and in-document search feature for future. Users can disable text layer rendering when they don't need it (for such as rendering mini-map images).For implementation, this PR does the following things, which are tied to commits:
@types/pdfjs-distfor better codePDFViewer(properly handles cancelled rendering)PDFViewerTextLayercomponentShow text layeroptionHow do you test/verify these changes?
Have you documented your changes (if necessary)?
Are there any breaking changes included in this pull request?
no