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:
parent
e293c12afc
commit
e0c811f2ed
@ -46,7 +46,7 @@ pdfLinkService.setViewer(pdfViewer);
|
||||
|
||||
// (Optionally) enable find controller.
|
||||
var pdfFindController = new pdfjsViewer.PDFFindController({
|
||||
pdfViewer: pdfViewer,
|
||||
linkService: pdfLinkService,
|
||||
});
|
||||
pdfViewer.setFindController(pdfFindController);
|
||||
|
||||
|
@ -46,7 +46,7 @@ pdfLinkService.setViewer(pdfSinglePageViewer);
|
||||
|
||||
// (Optionally) enable find controller.
|
||||
var pdfFindController = new pdfjsViewer.PDFFindController({
|
||||
pdfViewer: pdfSinglePageViewer,
|
||||
linkService: pdfLinkService,
|
||||
});
|
||||
pdfSinglePageViewer.setFindController(pdfFindController);
|
||||
|
||||
|
@ -31,7 +31,6 @@ var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/';
|
||||
var CMAP_PACKED = true;
|
||||
|
||||
var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf';
|
||||
var SEARCH_FOR = ''; // try 'Mozilla';
|
||||
|
||||
var container = document.getElementById('viewerContainer');
|
||||
|
||||
@ -46,19 +45,9 @@ var pdfViewer = new pdfjsViewer.PDFViewer({
|
||||
});
|
||||
pdfLinkService.setViewer(pdfViewer);
|
||||
|
||||
// (Optionally) enable find controller.
|
||||
var pdfFindController = new pdfjsViewer.PDFFindController({
|
||||
pdfViewer: pdfViewer,
|
||||
});
|
||||
pdfViewer.setFindController(pdfFindController);
|
||||
|
||||
container.addEventListener('pagesinit', function () {
|
||||
// We can use pdfViewer now, e.g. let's change default scale.
|
||||
pdfViewer.currentScaleValue = 'page-width';
|
||||
|
||||
if (SEARCH_FOR) { // We can try search for things
|
||||
pdfFindController.executeCommand('find', { query: SEARCH_FOR, });
|
||||
}
|
||||
});
|
||||
|
||||
// Loading document.
|
||||
@ -72,5 +61,4 @@ pdfjsLib.getDocument({
|
||||
pdfViewer.setDocument(pdfDocument);
|
||||
|
||||
pdfLinkService.setDocument(pdfDocument, null);
|
||||
pdfFindController.setDocument(pdfDocument);
|
||||
});
|
||||
|
@ -343,7 +343,7 @@ let PDFViewerApplication = {
|
||||
pdfLinkService.setHistory(this.pdfHistory);
|
||||
|
||||
this.findController = new PDFFindController({
|
||||
pdfViewer: this.pdfViewer,
|
||||
linkService: pdfLinkService,
|
||||
eventBus,
|
||||
});
|
||||
this.findController.onUpdateResultsCount = (matchesCount) => {
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user