Skip to content

Conversation

@fukudasjp
Copy link
Contributor

What do these changes do/fix?

Add showTextLayer option to render text layer on PDFViewer as 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:

  • Add @types/pdfjs-dist for better code
  • Control PDF rendering tasks properly in PDFViewer (properly handles cancelled rendering)
  • Add PDFViewerTextLayer component
    • Update the storybook withShow text layer option

How do you test/verify these changes?

  • Use storybook to test the feature

Have you documented your changes (if necessary)?

  • No

Are there any breaking changes included in this pull request?

no

Copy link
Member

@jhpedemonte jhpedemonte left a 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.

/**
* Render text layer
*/
showTextLayer?: boolean;
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.


const { RenderingCancelledException } = PDFJSLib as any;

interface Props {
Copy link
Member

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?: ... };

Copy link
Contributor Author

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.

Copy link
Member

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).

Copy link
Contributor Author

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

Comment on lines +77 to +85
const { textContent, viewport, scale, page } = loadedText;

const builder = new TextLayerBuilder({
textLayerDiv,
viewport,
eventBus: new EventBus(),
pageIndex: page - 1
});
signal.addEventListener('abort', () => builder.cancel());
Copy link
Member

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).

@lgtm-com
Copy link
Contributor

lgtm-com bot commented Dec 2, 2021

This pull request introduces 1 alert when merging 6ba0703 into f904849 - view on LGTM.com

new alerts:

  • 1 for Useless conditional

@lgtm-com
Copy link
Contributor

lgtm-com bot commented Dec 2, 2021

This pull request introduces 1 alert when merging 052336a into f904849 - view on LGTM.com

new alerts:

  • 1 for Useless conditional

Copy link
Member

@jhpedemonte jhpedemonte left a 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
Copy link
Member

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

Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# perttier
# prettier

const originalPdfjsWebCss = process.argv[2];
const mixinPdfjsWebScss = process.argv[3];

// load teh original style
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// load teh original style
// load the original style

}
`;

fs.writeFileSync(mixinPdfjsWebScss, generatedCss, { encoding: 'utf-8' });
Copy link
Member

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"
Copy link
Member

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.

Suggested change
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
../../node_modules/.bin/prettier --write "$PDFJS_SCSS"
yarn run prettier --write "$PDFJS_SCSS"

@fukudasjp fukudasjp merged commit b9dc342 into master Dec 6, 2021
@fukudasjp fukudasjp deleted the feat/redner-pdf-text branch December 6, 2021 04:41
@fukudasjp fukudasjp mentioned this pull request Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants