1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Use the link service for getting and setting page information

This removes the dependency on a `PDFViewer` instance from the find
controller, which makes it more similar to other components and makes it
easier to unit test with a mock link service.

Finally, we remove the search capabilities from the SVG example since it
doesn't work there because there is no separate text layer.
This commit is contained in:
Tim van der Meij 2018-09-21 19:40:11 +02:00
parent e293c12afc
commit e0c811f2ed
No known key found for this signature in database
GPG key ID: 8C3FD2925A5F2762
5 changed files with 21 additions and 24 deletions

View file

@ -343,7 +343,7 @@ let PDFViewerApplication = {
pdfLinkService.setHistory(this.pdfHistory);
this.findController = new PDFFindController({
pdfViewer: this.pdfViewer,
linkService: pdfLinkService,
eventBus,
});
this.findController.onUpdateResultsCount = (matchesCount) => {

View file

@ -40,12 +40,21 @@ const CHARACTERS_TO_NORMALIZE = {
'\u00BE': '3/4', // Vulgar fraction three quarters
};
/**
* @typedef {Object} PDFFindControllerOptions
* @property {IPDFLinkService} linkService - The navigation/linking service.
* @property {EventBus} eventBus - The application event bus.
*/
/**
* Provides search functionality to find a given string in a PDF document.
*/
class PDFFindController {
constructor({ pdfViewer, eventBus = getGlobalEventBus(), }) {
this._pdfViewer = pdfViewer;
/**
* @param {PDFFindControllerOptions} options
*/
constructor({ linkService, eventBus = getGlobalEventBus(), }) {
this._linkService = linkService;
this._eventBus = eventBus;
this.onUpdateResultsCount = null;
@ -342,7 +351,7 @@ class PDFFindController {
}
let promise = Promise.resolve();
for (let i = 0, ii = this._pdfViewer.pagesCount; i < ii; i++) {
for (let i = 0, ii = this._linkService.pagesCount; i < ii; i++) {
const extractTextCapability = createPromiseCapability();
this._extractTextPromises[i] = extractTextCapability.promise;
@ -375,9 +384,9 @@ class PDFFindController {
_updatePage(index) {
if (this._selected.pageIdx === index) {
// If the page is selected, scroll the page into view, which triggers
// rendering the page, which adds the textLayer. Once the textLayer is
// build, it will scroll onto the selected match.
this._pdfViewer.currentPageNumber = index + 1;
// rendering the page, which adds the text layer. Once the text layer
// is built, it will scroll to the selected match.
this._linkService.page = index + 1;
}
this._eventBus.dispatch('updatetextlayermatches', {
@ -388,8 +397,8 @@ class PDFFindController {
_nextMatch() {
const previous = this._state.findPrevious;
const currentPageIndex = this._pdfViewer.currentPageNumber - 1;
const numPages = this._pdfViewer.pagesCount;
const currentPageIndex = this._linkService.page - 1;
const numPages = this._linkService.pagesCount;
this._highlightMatches = true;
@ -501,7 +510,7 @@ class PDFFindController {
_advanceOffsetPage(previous) {
const offset = this._offset;
const numPages = this._extractTextPromises.length;
const numPages = this._linkService.pagesCount;
offset.pageIdx = (previous ? offset.pageIdx - 1 : offset.pageIdx + 1);
offset.matchIdx = null;