Skip to content
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

Fix for broken search functionality when showPage=false #393

Merged
merged 10 commits into from
Oct 12, 2018

Conversation

AntonioDell
Copy link

Prologue
This is my first fork/PR of an open source porject, so please point out errors if found, thanks!

Issues getting fixed by this PR

General Overview
While working on an integration in a current web-frontend project I noticed, that the search functionality was not working when displaying only a single page (with showAll = false).
I saw that the viewer used a PDFPageView to display a single page and a PDFViewer for all pages. By this, the content of the PDF could only be searched, when the PDFViewer was displaying all pages.

This fix uses the PDFSinglePageViewer (https://github.com/mozilla/pdf.js/blob/master/web/pdf_single_page_viewer.js) instead of a PDFPageView, which causes a lot of changes internally, thus the many changes in this PR.

Most important changes
Please note the changes to the following methods:

  • setupViewer(): Removed in favor of setupMultiPageViewer() and setupSinglePageViewer()
  • renderPage(), removeAllChildNodes(): Removed, because no longer needed
  • update(), render(), updateSize(): All these methods and the methods called by them lost the check to showAll, since now they don't care anymore if there is a single page or multiple (by just calling currentViewer). PDFSinglePageViewer handles everything else
  • resetPdfDocument(): New important method used to show/hide the active/inactive viewer by calling setDocument() with either null (to hide the viewer) or the pdf (to show it)
  • utils/event-bus-utils.ts: Class necessary as described in the commits of the 12. October

The PDFFindController of PDF.js wasn't able to search on a PDFPageView,
replacing it with the PDFSinglePageViewer solved the issue.
This change unfortunately introduced some major changes in the inner functioning
of the pdf-viewer.
Errors included trailing whitespaces, unneccessary type declarations and
member ordering.
The setupViewer methods didn't need to be public.
Cleanup included removing trailing whitespaces.
The reason that we have to specify a new eventbus is the default behavior
of pdfjs viewers. If you don't specify an eventbus at creation, a viewer will
get a singleton instance of a global eventbus. Meaning that two viewer instances
(in our case PDFSinglePageViewer and PDFViewer) share one eventbus.
Since PDFSinglePageViewer reacts to 'pagesinit' events by calling
'_ensurePageViewVisible()' (see pdfjs source code) the single viewer throws
an error.
Using an unmodified new EventBus instance however resulted in important events
not being thrown at document level (f.e. 'pagerendered'). By copying the
implementation of a global EventBus from 'pdf.js/web/dom_events.js', the normal
behavior is restored with separat EventBus instances.
The EventBus creation shouldn't be a responsibility of the pdf-viewer, so
it was moved to a utility file.
When pdfjs exports their methods contained in 'pdf.js/web/dom_events.js'
these utility classes can be removed in favor of them.
When trying to package the project it threw the following error:
"packagr Exported variable 'createEventBus' has or is using private name
'_createEventBus'."
This should be fixed now.
Forgot to replace all instances of pdfMultiPageViewer (former pdfViewer)
with getCurrentViewer().
Previously using the PDFPageView and changing the page there resulted in
a just the pdf page get changed out with the new one.
By using the PDFSInglePageViewer a page change results in focusing the new
pdf page requesting focus "snapping" into view.
If the page change is triggered by an input element whose position is fixed,
it may get out of the users viewport, which isn't very pretty. The input still
receives inputs (like pressing up/down keys on keyboard) but the user can't
see the input element itself (an example of this behavior can be found in
by executing "ng serve".
@VadimDez
Copy link
Owner

Great job, I'll review it as soon as I can!

@VadimDez VadimDez merged commit 3143ecd into VadimDez:master Oct 12, 2018
@VadimDez VadimDez added this to the 5.2.0 milestone Oct 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants