From e293c12afcf3a9a55669b6bf095d657e7780ba16 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Fri, 21 Sep 2018 17:19:33 +0200 Subject: [PATCH] Implement the `setDocument` method for the find controller Now it follows the same pattern as e.g., the document properties component, which allows us to have one instance of the find controller and set a new document to search upon switching documents. Moreover, this allows us to get rid of the dependency on `pdfViewer` in order to fetch the text content for a page. This is working towards getting rid of the `pdfViewer` dependency upon initializing the component entirely in future commits. Finally, we make the `reset` method private since it's not supposed to be used from the outside anymore now that `setDocument` takes care of this, similar to other components. --- examples/components/simpleviewer.js | 3 +- examples/components/singlepageviewer.js | 3 +- examples/svgviewer/viewer.js | 3 +- web/app.js | 3 +- web/base_viewer.js | 8 --- web/pdf_document_properties.js | 2 +- web/pdf_find_controller.js | 75 ++++++++++++++++--------- 7 files changed, 59 insertions(+), 38 deletions(-) diff --git a/examples/components/simpleviewer.js b/examples/components/simpleviewer.js index 4cbf344ea..1ca0a6e99 100644 --- a/examples/components/simpleviewer.js +++ b/examples/components/simpleviewer.js @@ -55,7 +55,7 @@ container.addEventListener('pagesinit', function () { pdfViewer.currentScaleValue = 'page-width'; if (SEARCH_FOR) { // We can try search for things - pdfFindController.executeCommand('find', {query: SEARCH_FOR}); + pdfFindController.executeCommand('find', { query: SEARCH_FOR, }); } }); @@ -70,4 +70,5 @@ pdfjsLib.getDocument({ pdfViewer.setDocument(pdfDocument); pdfLinkService.setDocument(pdfDocument, null); + pdfFindController.setDocument(pdfDocument); }); diff --git a/examples/components/singlepageviewer.js b/examples/components/singlepageviewer.js index dbe7b03d6..c15ae24ee 100644 --- a/examples/components/singlepageviewer.js +++ b/examples/components/singlepageviewer.js @@ -55,7 +55,7 @@ container.addEventListener('pagesinit', function () { pdfSinglePageViewer.currentScaleValue = 'page-width'; if (SEARCH_FOR) { // We can try search for things - pdfFindController.executeCommand('find', {query: SEARCH_FOR}); + pdfFindController.executeCommand('find', { query: SEARCH_FOR, }); } }); @@ -70,4 +70,5 @@ pdfjsLib.getDocument({ pdfSinglePageViewer.setDocument(pdfDocument); pdfLinkService.setDocument(pdfDocument, null); + pdfFindController.setDocument(pdfDocument); }); diff --git a/examples/svgviewer/viewer.js b/examples/svgviewer/viewer.js index 17d545a98..bf8330b32 100644 --- a/examples/svgviewer/viewer.js +++ b/examples/svgviewer/viewer.js @@ -57,7 +57,7 @@ container.addEventListener('pagesinit', function () { pdfViewer.currentScaleValue = 'page-width'; if (SEARCH_FOR) { // We can try search for things - pdfFindController.executeCommand('find', {query: SEARCH_FOR}); + pdfFindController.executeCommand('find', { query: SEARCH_FOR, }); } }); @@ -72,4 +72,5 @@ pdfjsLib.getDocument({ pdfViewer.setDocument(pdfDocument); pdfLinkService.setDocument(pdfDocument, null); + pdfFindController.setDocument(pdfDocument); }); diff --git a/web/app.js b/web/app.js index 3f2dfc95b..cca1e034f 100644 --- a/web/app.js +++ b/web/app.js @@ -592,6 +592,7 @@ let PDFViewerApplication = { if (this.pdfDocument) { this.pdfDocument = null; + this.findController.setDocument(null); this.pdfThumbnailViewer.setDocument(null); this.pdfViewer.setDocument(null); this.pdfLinkService.setDocument(null); @@ -608,7 +609,6 @@ let PDFViewerApplication = { this.pdfOutlineViewer.reset(); this.pdfAttachmentViewer.reset(); - this.findController.reset(); this.findBar.reset(); this.toolbar.reset(); this.secondaryToolbar.reset(); @@ -916,6 +916,7 @@ let PDFViewerApplication = { } else if (PDFJSDev.test('CHROME')) { baseDocumentUrl = location.href.split('#')[0]; } + this.findController.setDocument(pdfDocument); this.pdfLinkService.setDocument(pdfDocument, baseDocumentUrl); this.pdfDocumentProperties.setDocument(pdfDocument, this.url); diff --git a/web/base_viewer.js b/web/base_viewer.js index 3bd723384..38e819812 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -913,14 +913,6 @@ class BaseViewer { return false; } - getPageTextContent(pageIndex) { - return this.pdfDocument.getPage(pageIndex + 1).then(function(page) { - return page.getTextContent({ - normalizeWhitespace: true, - }); - }); - } - /** * @param {HTMLDivElement} textLayerDiv * @param {number} pageIndex diff --git a/web/pdf_document_properties.js b/web/pdf_document_properties.js index 329c8dddc..168ac14f2 100644 --- a/web/pdf_document_properties.js +++ b/web/pdf_document_properties.js @@ -184,7 +184,7 @@ class PDFDocumentProperties { * Note that the overlay will contain no information if this method * is not called. * - * @param {Object} pdfDocument - A reference to the PDF document. + * @param {PDFDocumentProxy} pdfDocument - A reference to the PDF document. * @param {string} url - The URL of the document. */ setDocument(pdfDocument, url = null) { diff --git a/web/pdf_find_controller.js b/web/pdf_find_controller.js index 8a8bc89a9..9dfa107ba 100644 --- a/web/pdf_find_controller.js +++ b/web/pdf_find_controller.js @@ -51,7 +51,7 @@ class PDFFindController { this.onUpdateResultsCount = null; this.onUpdateState = null; - this.reset(); + this._reset(); eventBus.on('findbarclose', () => { this._highlightMatches = false; @@ -87,8 +87,51 @@ class PDFFindController { return this._state; } - reset() { + /** + * Set a reference to the PDF document in order to search it. + * Note that searching is not possible if this method is not called. + * + * @param {PDFDocumentProxy} pdfDocument - The PDF document to search. + */ + setDocument(pdfDocument) { + if (this._pdfDocument) { + this._reset(); + } + if (!pdfDocument) { + return; + } + this._pdfDocument = pdfDocument; + } + + executeCommand(cmd, state) { + if (!this._pdfDocument) { + return; + } + + if (this._state === null || cmd !== 'findagain') { + this._dirtyMatch = true; + } + this._state = state; + this._updateUIState(FindState.PENDING); + + this._firstPagePromise.then(() => { + this._extractText(); + + clearTimeout(this._findTimeout); + if (cmd === 'find') { + // Trigger the find action with a small delay to avoid starting the + // search when the user is still typing (saving resources). + this._findTimeout = + setTimeout(this._nextMatch.bind(this), FIND_TIMEOUT); + } else { + this._nextMatch(); + } + }); + } + + _reset() { this._highlightMatches = false; + this._pdfDocument = null; this._pageMatches = []; this._pageMatchesLength = null; this._state = null; @@ -118,28 +161,6 @@ class PDFFindController { }); } - executeCommand(cmd, state) { - if (this._state === null || cmd !== 'findagain') { - this._dirtyMatch = true; - } - this._state = state; - this._updateUIState(FindState.PENDING); - - this._firstPagePromise.then(() => { - this._extractText(); - - clearTimeout(this._findTimeout); - if (cmd === 'find') { - // Trigger the find action with a small delay to avoid starting the - // search when the user is still typing (saving resources). - this._findTimeout = - setTimeout(this._nextMatch.bind(this), FIND_TIMEOUT); - } else { - this._nextMatch(); - } - }); - } - _normalize(text) { return text.replace(this._normalizationRegex, function(ch) { return CHARACTERS_TO_NORMALIZE[ch]; @@ -326,7 +347,11 @@ class PDFFindController { this._extractTextPromises[i] = extractTextCapability.promise; promise = promise.then(() => { - return this._pdfViewer.getPageTextContent(i).then((textContent) => { + return this._pdfDocument.getPage(i + 1).then((pdfPage) => { + return pdfPage.getTextContent({ + normalizeWhitespace: true, + }); + }).then((textContent) => { const textItems = textContent.items; const strBuf = [];